Cleanup rich reverts

This commit is contained in:
Amir Bandeali 2019-09-24 18:13:30 -07:00
parent e5aaf68277
commit 57f5b12e24
9 changed files with 59 additions and 198 deletions

View File

@ -146,7 +146,7 @@ contract StakingProxy is
if (_epochDurationInSeconds < 5 days || _epochDurationInSeconds > 30 days) {
LibRichErrors.rrevert(
LibStakingRichErrors.InvalidParamValueError(
LibStakingRichErrors.InvalidParamValueErrorCode.InvalidEpochDuration
LibStakingRichErrors.InvalidParamValueErrorCodes.InvalidEpochDuration
));
}
@ -155,7 +155,7 @@ contract StakingProxy is
if (cobbDouglasAlphaNumerator > _cobbDouglasAlphaDenominator || _cobbDouglasAlphaDenominator == 0) {
LibRichErrors.rrevert(
LibStakingRichErrors.InvalidParamValueError(
LibStakingRichErrors.InvalidParamValueErrorCode.InvalidCobbDouglasAlpha
LibStakingRichErrors.InvalidParamValueErrorCodes.InvalidCobbDouglasAlpha
));
}
@ -163,7 +163,7 @@ contract StakingProxy is
if (rewardDelegatedStakeWeight > PPM_DENOMINATOR) {
LibRichErrors.rrevert(
LibStakingRichErrors.InvalidParamValueError(
LibStakingRichErrors.InvalidParamValueErrorCode.InvalidRewardDelegatedStakeWeight
LibStakingRichErrors.InvalidParamValueErrorCodes.InvalidRewardDelegatedStakeWeight
));
}
@ -171,7 +171,7 @@ contract StakingProxy is
if (maximumMakersInPool == 0) {
LibRichErrors.rrevert(
LibStakingRichErrors.InvalidParamValueError(
LibStakingRichErrors.InvalidParamValueErrorCode.InvalidMaximumMakersInPool
LibStakingRichErrors.InvalidParamValueErrorCodes.InvalidMaximumMakersInPool
));
}
@ -179,7 +179,7 @@ contract StakingProxy is
if (minimumPoolStake < 2) {
LibRichErrors.rrevert(
LibStakingRichErrors.InvalidParamValueError(
LibStakingRichErrors.InvalidParamValueErrorCode.InvalidMinimumPoolStake
LibStakingRichErrors.InvalidParamValueErrorCodes.InvalidMinimumPoolStake
));
}
}

View File

@ -51,7 +51,8 @@ contract MixinExchangeManager is
onlyAuthorized
{
if (validExchanges[addr]) {
LibRichErrors.rrevert(LibStakingRichErrors.ExchangeAddressAlreadyRegisteredError(
LibRichErrors.rrevert(LibStakingRichErrors.ExchangeManagerError(
LibStakingRichErrors.ExchangeManagerErrorCodes.ExchangeAlreadyRegistered,
addr
));
}
@ -66,7 +67,8 @@ contract MixinExchangeManager is
onlyAuthorized
{
if (!validExchanges[addr]) {
LibRichErrors.rrevert(LibStakingRichErrors.ExchangeAddressNotRegisteredError(
LibRichErrors.rrevert(LibStakingRichErrors.ExchangeManagerError(
LibStakingRichErrors.ExchangeManagerErrorCodes.ExchangeNotRegistered,
addr
));
}

View File

@ -34,12 +34,12 @@ library LibStakingRichErrors {
MismatchedFeeAndPayment
}
enum InitializationErrorCode {
enum InitializationErrorCodes {
MixinSchedulerAlreadyInitialized,
MixinParamsAlreadyInitialized
}
enum InvalidParamValueErrorCode {
enum InvalidParamValueErrorCodes {
InvalidCobbDouglasAlpha,
InvalidRewardDelegatedStakeWeight,
InvalidMaximumMakersInPool,
@ -54,21 +54,18 @@ library LibStakingRichErrors {
PoolIsFull
}
// bytes4(keccak256("MiscalculatedRewardsError(uint256,uint256)"))
bytes4 internal constant MISCALCULATED_REWARDS_ERROR_SELECTOR =
0xf7806c4e;
enum ExchangeManagerErrorCodes {
ExchangeAlreadyRegistered,
ExchangeNotRegistered
}
// bytes4(keccak256("OnlyCallableByExchangeError(address)"))
bytes4 internal constant ONLY_CALLABLE_BY_EXCHANGE_ERROR_SELECTOR =
0xb56d2df0;
// bytes4(keccak256("ExchangeAddressAlreadyRegisteredError(address)"))
bytes4 internal constant EXCHANGE_ADDRESS_ALREADY_REGISTERED_ERROR_SELECTOR =
0xc87a78b7;
// bytes4(keccak256("ExchangeAddressNotRegisteredError(address)"))
bytes4 internal constant EXCHANGE_ADDRESS_NOT_REGISTERED_ERROR_SELECTOR =
0x7dc025b0;
// bytes4(keccak256("ExchangeManagerError(uint8,address)"))
bytes4 internal constant EXCHANGE_MANAGER_ERROR_SELECTOR =
0xb9588e43;
// bytes4(keccak256("InsufficientBalanceError(uint256,uint256)"))
bytes4 internal constant INSUFFICIENT_BALANCE_ERROR_SELECTOR =
@ -82,10 +79,6 @@ library LibStakingRichErrors {
bytes4 internal constant MAKER_POOL_ASSIGNMENT_ERROR_SELECTOR =
0x69945e3f;
// bytes4(keccak256("WithdrawAmountExceedsMemberBalanceError(uint256,uint256)"))
bytes4 internal constant WITHDRAW_AMOUNT_EXCEEDS_MEMBER_BALANCE_ERROR_SELECTOR =
0xfc9c065f;
// bytes4(keccak256("BlockTimestampTooLowError(uint256,uint256)"))
bytes4 internal constant BLOCK_TIMESTAMP_TOO_LOW_ERROR_SELECTOR =
0xa6bcde47;
@ -102,10 +95,6 @@ library LibStakingRichErrors {
bytes internal constant ONLY_CALLABLE_IF_NOT_IN_CATASTROPHIC_FAILURE_ERROR =
hex"7dd020ce";
// bytes4(keccak256("AmountExceedsBalanceOfPoolError(uint256,uint96)"))
bytes4 internal constant AMOUNT_EXCEEDS_BALANCE_OF_POOL_ERROR_SELECTOR =
0x4c5c09dd;
// bytes4(keccak256("OperatorShareError(uint8,bytes32,uint32)"))
bytes4 internal constant OPERATOR_SHARE_ERROR_SELECTOR =
0x22df9597;
@ -139,21 +128,6 @@ library LibStakingRichErrors {
0x614b800a;
// solhint-disable func-name-mixedcase
function MiscalculatedRewardsError(
uint256 totalRewardsPaid,
uint256 initialContractBalance
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
MISCALCULATED_REWARDS_ERROR_SELECTOR,
totalRewardsPaid,
initialContractBalance
);
}
function OnlyCallableByExchangeError(
address senderAddress
)
@ -167,7 +141,8 @@ library LibStakingRichErrors {
);
}
function ExchangeAddressAlreadyRegisteredError(
function ExchangeManagerError(
ExchangeManagerErrorCodes errorCodes,
address exchangeAddress
)
internal
@ -175,20 +150,8 @@ library LibStakingRichErrors {
returns (bytes memory)
{
return abi.encodeWithSelector(
EXCHANGE_ADDRESS_ALREADY_REGISTERED_ERROR_SELECTOR,
exchangeAddress
);
}
function ExchangeAddressNotRegisteredError(
address exchangeAddress
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
EXCHANGE_ADDRESS_NOT_REGISTERED_ERROR_SELECTOR,
EXCHANGE_MANAGER_ERROR_SELECTOR,
errorCodes,
exchangeAddress
);
}
@ -224,7 +187,7 @@ library LibStakingRichErrors {
}
function MakerPoolAssignmentError(
MakerPoolAssignmentErrorCodes errorCode,
MakerPoolAssignmentErrorCodes errorCodes,
address makerAddress,
bytes32 poolId
)
@ -234,27 +197,12 @@ library LibStakingRichErrors {
{
return abi.encodeWithSelector(
MAKER_POOL_ASSIGNMENT_ERROR_SELECTOR,
errorCode,
errorCodes,
makerAddress,
poolId
);
}
function WithdrawAmountExceedsMemberBalanceError(
uint256 withdrawAmount,
uint256 balance
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
WITHDRAW_AMOUNT_EXCEEDS_MEMBER_BALANCE_ERROR_SELECTOR,
withdrawAmount,
balance
);
}
function BlockTimestampTooLowError(
uint256 epochEndTime,
uint256 currentBlockTimestamp
@ -299,23 +247,8 @@ library LibStakingRichErrors {
return ONLY_CALLABLE_IF_NOT_IN_CATASTROPHIC_FAILURE_ERROR;
}
function AmountExceedsBalanceOfPoolError(
uint256 amount,
uint96 poolBalance
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
AMOUNT_EXCEEDS_BALANCE_OF_POOL_ERROR_SELECTOR,
amount,
poolBalance
);
}
function OperatorShareError(
OperatorShareErrorCodes errorCode,
OperatorShareErrorCodes errorCodes,
bytes32 poolId,
uint32 operatorShare
)
@ -325,7 +258,7 @@ library LibStakingRichErrors {
{
return abi.encodeWithSelector(
OPERATOR_SHARE_ERROR_SELECTOR,
errorCode,
errorCodes,
poolId,
operatorShare
);
@ -347,7 +280,7 @@ library LibStakingRichErrors {
}
function InvalidProtocolFeePaymentError(
ProtocolFeePaymentErrorCodes errorCode,
ProtocolFeePaymentErrorCodes errorCodes,
uint256 expectedProtocolFeePaid,
uint256 actualProtocolFeePaid
)
@ -357,7 +290,7 @@ library LibStakingRichErrors {
{
return abi.encodeWithSelector(
INVALID_PROTOCOL_FEE_PAYMENT_ERROR_SELECTOR,
errorCode,
errorCodes,
expectedProtocolFeePaid,
actualProtocolFeePaid
);
@ -374,7 +307,7 @@ library LibStakingRichErrors {
);
}
function InitializationError(InitializationErrorCode code)
function InitializationError(InitializationErrorCodes code)
internal
pure
returns (bytes memory)
@ -385,7 +318,7 @@ library LibStakingRichErrors {
);
}
function InvalidParamValueError(InvalidParamValueErrorCode code)
function InvalidParamValueError(InvalidParamValueErrorCodes code)
internal
pure
returns (bytes memory)

View File

@ -118,7 +118,7 @@ contract MixinParams is
) {
LibRichErrors.rrevert(
LibStakingRichErrors.InitializationError(
LibStakingRichErrors.InitializationErrorCode.MixinParamsAlreadyInitialized
LibStakingRichErrors.InitializationErrorCodes.MixinParamsAlreadyInitialized
)
);
}

View File

@ -97,7 +97,7 @@ contract MixinScheduler is
if (currentEpochStartTimeInSeconds != 0) {
LibRichErrors.rrevert(
LibStakingRichErrors.InitializationError(
LibStakingRichErrors.InitializationErrorCode.MixinSchedulerAlreadyInitialized
LibStakingRichErrors.InitializationErrorCodes.MixinSchedulerAlreadyInitialized
)
);
}

View File

@ -37,7 +37,10 @@ blockchainTests('Exchange Integrations', env => {
const isValidAddressValid = await validExchanges.callAsync(exchange);
expect(isValidAddressValid).to.be.true();
// 3 try adding valid address again
let revertError = new StakingRevertErrors.ExchangeAddressAlreadyRegisteredError(exchange);
let revertError = new StakingRevertErrors.ExchangeManagerError(
StakingRevertErrors.ExchangeManagerErrorCodes.ExchangeAlreadyRegistered,
exchange,
);
let tx = addExchangeAddress.awaitTransactionSuccessAsync(exchange);
await expect(tx).to.revertWith(revertError);
// 4 remove valid address
@ -45,7 +48,10 @@ blockchainTests('Exchange Integrations', env => {
const isValidAddressStillValid = await validExchanges.callAsync(exchange);
expect(isValidAddressStillValid).to.be.false();
// 5 try removing valid address again
revertError = new StakingRevertErrors.ExchangeAddressNotRegisteredError(exchange);
revertError = new StakingRevertErrors.ExchangeManagerError(
StakingRevertErrors.ExchangeManagerErrorCodes.ExchangeNotRegistered,
exchange,
);
tx = removeExchangeAddress.awaitTransactionSuccessAsync(exchange);
await expect(tx).to.revertWith(revertError);
// @todo should not be able to add / remove an exchange if not contract owner.

View File

@ -224,7 +224,7 @@ blockchainTests('Migration tests', env => {
epochDurationInSeconds: fiveDays.minus(1),
});
const expectedError = new StakingRevertErrors.InvalidParamValueError(
StakingRevertErrors.InvalidParamValueErrorCode.InvalidEpochDuration,
StakingRevertErrors.InvalidParamValueErrorCodes.InvalidEpochDuration,
);
expect(tx).to.revertWith(expectedError);
});
@ -234,7 +234,7 @@ blockchainTests('Migration tests', env => {
epochDurationInSeconds: thirtyDays.plus(1),
});
const expectedError = new StakingRevertErrors.InvalidParamValueError(
StakingRevertErrors.InvalidParamValueErrorCode.InvalidEpochDuration,
StakingRevertErrors.InvalidParamValueErrorCodes.InvalidEpochDuration,
);
expect(tx).to.revertWith(expectedError);
});
@ -258,7 +258,7 @@ blockchainTests('Migration tests', env => {
cobbDouglasAlphaDenominator: constants.ZERO_AMOUNT,
});
const expectedError = new StakingRevertErrors.InvalidParamValueError(
StakingRevertErrors.InvalidParamValueErrorCode.InvalidCobbDouglasAlpha,
StakingRevertErrors.InvalidParamValueErrorCodes.InvalidCobbDouglasAlpha,
);
expect(tx).to.revertWith(expectedError);
});
@ -269,7 +269,7 @@ blockchainTests('Migration tests', env => {
cobbDouglasAlphaDenominator: new BigNumber(100),
});
const expectedError = new StakingRevertErrors.InvalidParamValueError(
StakingRevertErrors.InvalidParamValueErrorCode.InvalidCobbDouglasAlpha,
StakingRevertErrors.InvalidParamValueErrorCodes.InvalidCobbDouglasAlpha,
);
expect(tx).to.revertWith(expectedError);
});
@ -295,7 +295,7 @@ blockchainTests('Migration tests', env => {
rewardDelegatedStakeWeight: new BigNumber(stakingConstants.PPM).plus(1),
});
const expectedError = new StakingRevertErrors.InvalidParamValueError(
StakingRevertErrors.InvalidParamValueErrorCode.InvalidRewardDelegatedStakeWeight,
StakingRevertErrors.InvalidParamValueErrorCodes.InvalidRewardDelegatedStakeWeight,
);
expect(tx).to.revertWith(expectedError);
});
@ -312,7 +312,7 @@ blockchainTests('Migration tests', env => {
maximumMakersInPool: constants.ZERO_AMOUNT,
});
const expectedError = new StakingRevertErrors.InvalidParamValueError(
StakingRevertErrors.InvalidParamValueErrorCode.InvalidMaximumMakersInPool,
StakingRevertErrors.InvalidParamValueErrorCodes.InvalidMaximumMakersInPool,
);
expect(tx).to.revertWith(expectedError);
});

View File

@ -7,14 +7,6 @@ library LibSafeMathRichErrors {
bytes4 internal constant UINT256_BINOP_ERROR_SELECTOR =
0xe946c1bb;
// bytes4(keccak256("Uint96BinOpError(uint8,uint96,uint96)"))
bytes4 internal constant UINT96_BINOP_ERROR_SELECTOR =
0xe486a353;
// bytes4(keccak256("Uint64BinOpError(uint8,uint64,uint64)"))
bytes4 internal constant UINT64_BINOP_ERROR_SELECTOR =
0x67e71b32;
// bytes4(keccak256("Uint256DowncastError(uint8,uint256)"))
bytes4 internal constant UINT256_DOWNCAST_ERROR_SELECTOR =
0xc996af7b;
@ -50,40 +42,6 @@ library LibSafeMathRichErrors {
);
}
function Uint96BinOpError(
BinOpErrorCodes errorCode,
uint96 a,
uint96 b
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
UINT96_BINOP_ERROR_SELECTOR,
errorCode,
a,
b
);
}
function Uint64BinOpError(
BinOpErrorCodes errorCode,
uint64 a,
uint64 b
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
UINT64_BINOP_ERROR_SELECTOR,
errorCode,
a,
b
);
}
function Uint256DowncastError(
DowncastErrorCodes errorCode,
uint256 a

View File

@ -19,7 +19,7 @@ export enum ProtocolFeePaymentErrorCodes {
MismatchedFeeAndPayment,
}
export enum InvalidParamValueErrorCode {
export enum InvalidParamValueErrorCodes {
InvalidCobbDouglasAlpha,
InvalidRewardDelegatedStakeWeight,
InvalidMaximumMakersInPool,
@ -27,19 +27,14 @@ export enum InvalidParamValueErrorCode {
InvalidEpochDuration,
}
export enum InitializationErrorCode {
export enum InitializationErrorCodes {
MixinSchedulerAlreadyInitialized,
MixinParamsAlreadyInitialized,
}
export class MiscalculatedRewardsError extends RevertError {
constructor(totalRewardsPaid?: BigNumber | number | string, initialContractBalance?: BigNumber | number | string) {
super(
'MiscalculatedRewardsError',
'MiscalculatedRewardsError(uint256 totalRewardsPaid, uint256 initialContractBalance)',
{ totalRewardsPaid, initialContractBalance },
);
}
export enum ExchangeManagerErrorCodes {
ExchangeAlreadyRegistered,
ExchangeNotRegistered,
}
export class OnlyCallableByExchangeError extends RevertError {
@ -48,20 +43,11 @@ export class OnlyCallableByExchangeError extends RevertError {
}
}
export class ExchangeAddressAlreadyRegisteredError extends RevertError {
constructor(exchangeAddress?: string) {
super(
'ExchangeAddressAlreadyRegisteredError',
'ExchangeAddressAlreadyRegisteredError(address exchangeAddress)',
{ exchangeAddress },
);
}
}
export class ExchangeAddressNotRegisteredError extends RevertError {
constructor(exchangeAddress?: string) {
super('ExchangeAddressNotRegisteredError', 'ExchangeAddressNotRegisteredError(address exchangeAddress)', {
exchangeAddress,
export class ExchangeManagerError extends RevertError {
constructor(error?: ExchangeManagerErrorCodes, senderAddress?: string) {
super('ExchangeManagerError', 'ExchangeManagerError(uint8 errorCode, address senderAddress)', {
error,
senderAddress,
});
}
}
@ -99,16 +85,6 @@ export class MakerPoolAssignmentError extends RevertError {
}
}
export class WithdrawAmountExceedsMemberBalanceError extends RevertError {
constructor(withdrawAmount?: BigNumber | number | string, balance?: BigNumber | number | string) {
super(
'WithdrawAmountExceedsMemberBalanceError',
'WithdrawAmountExceedsMemberBalanceError(uint256 withdrawAmount, uint256 balance)',
{ withdrawAmount, balance },
);
}
}
export class BlockTimestampTooLowError extends RevertError {
constructor(epochEndTime?: BigNumber | number | string, currentBlockTimestamp?: BigNumber | number | string) {
super(
@ -139,16 +115,6 @@ export class OnlyCallableIfNotInCatastrophicFailureError extends RevertError {
}
}
export class AmountExceedsBalanceOfPoolError extends RevertError {
constructor(amount?: BigNumber | number | string, poolBalance?: BigNumber | number | string) {
super(
'AmountExceedsBalanceOfPoolError',
'AmountExceedsBalanceOfPoolError(uint256 amount, uint96 poolBalance)',
{ amount, poolBalance },
);
}
}
export class OperatorShareError extends RevertError {
constructor(error?: OperatorShareErrorCodes, poolId?: string, operatorShare?: BigNumber | number | string) {
super('OperatorShareError', 'OperatorShareError(uint8 error, bytes32 poolId, uint32 operatorShare)', {
@ -169,7 +135,7 @@ export class PoolExistenceError extends RevertError {
}
export class InvalidParamValueError extends RevertError {
constructor(error?: InvalidParamValueErrorCode) {
constructor(error?: InvalidParamValueErrorCodes) {
super('InvalidParamValueError', 'InvalidParamValueError(uint8 error)', {
error,
});
@ -197,7 +163,7 @@ export class InvalidProtocolFeePaymentError extends RevertError {
}
export class InitializationError extends RevertError {
constructor(error?: InitializationErrorCode) {
constructor(error?: InitializationErrorCodes) {
super('InitializationError', 'InitializationError(uint8 error)', { error });
}
}
@ -219,17 +185,14 @@ export class PreviousEpochNotFinalizedError extends RevertError {
}
const types = [
AmountExceedsBalanceOfPoolError,
BlockTimestampTooLowError,
ExchangeAddressAlreadyRegisteredError,
ExchangeAddressNotRegisteredError,
ExchangeManagerError,
InitializationError,
InsufficientBalanceError,
InvalidProtocolFeePaymentError,
InvalidStakeStatusError,
InvalidParamValueError,
MakerPoolAssignmentError,
MiscalculatedRewardsError,
OnlyCallableByExchangeError,
OnlyCallableByPoolOperatorOrMakerError,
OnlyCallableByStakingContractError,
@ -239,7 +202,6 @@ const types = [
PoolExistenceError,
PreviousEpochNotFinalizedError,
ProxyDestinationCannotBeNilError,
WithdrawAmountExceedsMemberBalanceError,
];
// Register the types we've defined.