@0x:contracts-utils Refactored utils to exclusively use library RichErrors

This commit is contained in:
James Towle
2019-07-10 16:33:22 -05:00
committed by Amir Bandeali
parent f937a0b038
commit 96ab74dea4
10 changed files with 64 additions and 76 deletions

View File

@@ -20,11 +20,11 @@ pragma solidity ^0.5.9;
import "./LibAddressArrayRichErrors.sol";
import "./LibBytes.sol";
import "./LibRichErrors.sol";
library LibAddressArray {
/// @dev Append a new address to an array of addresses.
/// The `addressArray` may need to be reallocated to make space
/// for the new address. Because of this we return the resulting
@@ -54,10 +54,10 @@ library LibAddressArray {
// `freeMemPtr` > `addressArrayEndPtr`: Some value occupies memory after `addressArray`
// `freeMemPtr` < `addressArrayEndPtr`: Memory has not been managed properly.
if (freeMemPtr < addressArrayEndPtr) {
LibAddressArrayRichErrors.MismanagedMemoryErrorRevert(
LibRichErrors._rrevert(LibAddressArrayRichErrors.MismanagedMemoryError(
freeMemPtr,
addressArrayEndPtr
);
));
}
// If free memory begins at the end of `addressArray`

View File

@@ -18,29 +18,26 @@
pragma solidity ^0.5.9;
import "./LibRichErrors.sol";
library LibAddressArrayRichErrors {
using LibRichErrors for *;
// bytes4(keccak256("MismanagedMemoryError(uint256,uint256)"))
bytes4 internal constant MISMANAGED_MEMORY_ERROR_SELECTOR =
0x5fc83722;
// solhint-disable func-name-mixedcase
function MismanagedMemoryErrorRevert(
function MismanagedMemoryError(
uint256 freeMemPtr,
uint256 addressArrayEndPtr
)
internal
pure
returns (bytes memory)
{
abi.encodeWithSelector(
return abi.encodeWithSelector(
MISMANAGED_MEMORY_ERROR_SELECTOR,
freeMemPtr,
addressArrayEndPtr
)._rrevert();
);
}
}

View File

@@ -19,6 +19,7 @@
pragma solidity ^0.5.9;
import "./LibBytesRichErrors.sol";
import "./LibRichErrors.sol";
library LibBytes {
@@ -179,18 +180,18 @@ library LibBytes {
// Ensure that the from and to positions are valid positions for a slice within
// the byte array that is being used.
if (from > to) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.FromLessThanOrEqualsToRequired,
from,
to
);
));
}
if (to > b.length) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.ToLessThanOrEqualsLengthRequired,
to,
b.length
);
));
}
// Create a new bytes structure and copy contents
@@ -221,18 +222,18 @@ library LibBytes {
// Ensure that the from and to positions are valid positions for a slice within
// the byte array that is being used.
if (from > to) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.FromLessThanOrEqualsToRequired,
from,
to
);
));
}
if (to > b.length) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.ToLessThanOrEqualsLengthRequired,
to,
b.length
);
));
}
// Create a new bytes structure around [from, to) in-place.
@@ -252,11 +253,11 @@ library LibBytes {
returns (bytes1 result)
{
if (b.length == 0) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.LengthGreaterThanZeroRequired,
b.length,
0
);
));
}
// Store last byte.
@@ -279,11 +280,11 @@ library LibBytes {
returns (address result)
{
if (b.length < 20) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsTwentyRequired,
b.length,
20 // 20 is length of address
);
));
}
// Store last 20 bytes.
@@ -328,11 +329,11 @@ library LibBytes {
returns (address result)
{
if (b.length < index + 20) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsTwentyRequired,
b.length,
index + 20 // 20 is length of address
);
));
}
// Add offset to index:
@@ -363,11 +364,11 @@ library LibBytes {
pure
{
if (b.length < index + 20) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsTwentyRequired,
b.length,
index + 20 // 20 is length of address
);
));
}
// Add offset to index:
@@ -412,11 +413,11 @@ library LibBytes {
returns (bytes32 result)
{
if (b.length < index + 32) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsThirtyTwoRequired,
b.length,
index + 32
);
));
}
// Arrays are prefixed by a 256 bit length parameter
@@ -442,11 +443,11 @@ library LibBytes {
pure
{
if (b.length < index + 32) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsThirtyTwoRequired,
b.length,
index + 32
);
));
}
// Arrays are prefixed by a 256 bit length parameter
@@ -502,11 +503,11 @@ library LibBytes {
returns (bytes4 result)
{
if (b.length < index + 4) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsFourRequired,
b.length,
index + 4
);
));
}
// Arrays are prefixed by a 32 byte length field
@@ -543,12 +544,12 @@ library LibBytes {
// Assert length of <b> is valid, given
// length of nested bytes
if (b.length < index + nestedBytesLength) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors
.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsNestedBytesLengthRequired,
b.length,
index + nestedBytesLength
);
));
}
// Return a pointer to the byte array as it exists inside `b`
@@ -573,12 +574,12 @@ library LibBytes {
// Assert length of <b> is valid, given
// length of input
if (b.length < index + 32 + input.length) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors
.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsNestedBytesLengthRequired,
b.length,
index + 32 + input.length // 32 bytes to store length
);
));
}
// Copy <input> into <b>
@@ -602,12 +603,12 @@ library LibBytes {
uint256 sourceLen = source.length;
// Dest length must be >= source length, or some bytes would not be copied.
if (dest.length < sourceLen) {
LibBytesRichErrors.InvalidByteOperationErrorRevert(
LibRichErrors._rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors
.InvalidByteOperationErrorCodes.DestinationLengthGreaterThanOrEqualSourceLengthRequired,
dest.length,
sourceLen
);
));
}
memCopy(
dest.contentAddress(),

View File

@@ -18,13 +18,9 @@
pragma solidity ^0.5.9;
import "./LibRichErrors.sol";
library LibBytesRichErrors {
using LibRichErrors for *;
enum InvalidByteOperationErrorCodes {
FromLessThanOrEqualsToRequired,
ToLessThanOrEqualsLengthRequired,
@@ -41,19 +37,20 @@ library LibBytesRichErrors {
0x28006595;
// solhint-disable func-name-mixedcase
function InvalidByteOperationErrorRevert(
function InvalidByteOperationError(
InvalidByteOperationErrorCodes errorCode,
uint256 endpoint,
uint256 required
)
internal
pure
returns (bytes memory)
{
abi.encodeWithSelector(
return abi.encodeWithSelector(
INVALID_BYTE_OPERATION_ERROR_SELECTOR,
errorCode,
endpoint,
required
)._rrevert();
);
}
}

View File

@@ -1,11 +1,8 @@
pragma solidity ^0.5.9;
import "./RichErrors.sol";
library LibOwnableRichErrors {
contract MixinOwnableRichErrors is
RichErrors
{
// bytes4(keccak256("OnlyOwnerError(address,address)"))
bytes4 internal constant ONLY_OWNER_SELECTOR =
0x1de45ad1;

View File

@@ -18,12 +18,9 @@
pragma solidity ^0.5.9;
import "./RichErrors.sol";
library LibReentrancyGuardRichErrors {
contract MixinReentrancyGuardRichErrors is
RichErrors
{
// bytes4(keccak256("IllegalReentrancyError()"))
bytes internal constant ILLEGAL_REENTRANCY_ERROR_SELECTOR_BYTES =
hex"0c3b823f";

View File

@@ -1,11 +1,8 @@
pragma solidity ^0.5.9;
import "./RichErrors.sol";
library LibSafeMathRichErrors {
contract MixinSafeMathRichErrors is
RichErrors
{
// bytes4(keccak256("SafeMathError(uint8,uint256,uint256)"))
bytes4 internal constant SAFE_MATH_ERROR =
0x35a51a70;
@@ -23,7 +20,8 @@ contract MixinSafeMathRichErrors is
uint256 b
)
internal
pure returns (bytes memory)
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
SAFE_MATH_ERROR,

View File

@@ -1,11 +1,11 @@
pragma solidity ^0.5.9;
import "./interfaces/IOwnable.sol";
import "./MixinOwnableRichErrors.sol";
import "./LibOwnableRichErrors.sol";
import "./LibRichErrors.sol";
contract Ownable is
MixinOwnableRichErrors,
IOwnable
{
address public owner;
@@ -18,7 +18,7 @@ contract Ownable is
modifier onlyOwner() {
if (msg.sender != owner) {
_rrevert(OnlyOwnerError(
LibRichErrors._rrevert(LibOwnableRichErrors.OnlyOwnerError(
msg.sender,
owner
));

View File

@@ -18,12 +18,12 @@
pragma solidity ^0.5.9;
import "./MixinReentrancyGuardRichErrors.sol";
import "./LibReentrancyGuardRichErrors.sol";
import "./LibRichErrors.sol";
contract ReentrancyGuard is
MixinReentrancyGuardRichErrors
{
contract ReentrancyGuard {
// Mutex counter.
// Starts at 1 and increases whenever a nonReentrant function is called.
uint256 private reentrancyGuardCounter = 1;
@@ -37,7 +37,9 @@ contract ReentrancyGuard is
// If the counter value is different from what we remember, the function
// was called more than once and an illegal reentrancy occured.
if (localCounter != reentrancyGuardCounter) {
_rrevert(IllegalReentrancyError());
LibRichErrors._rrevert(
LibReentrancyGuardRichErrors.IllegalReentrancyError()
);
}
}
}

View File

@@ -1,11 +1,10 @@
pragma solidity ^0.5.9;
import "./MixinSafeMathRichErrors.sol";
import "./LibRichErrors.sol";
import "./LibSafeMathRichErrors.sol";
contract SafeMath is
MixinSafeMathRichErrors
{
contract SafeMath {
function _safeMul(uint256 a, uint256 b)
internal
@@ -17,8 +16,8 @@ contract SafeMath is
}
uint256 c = a * b;
if (c / a != b) {
_rrevert(SafeMathError(
SafeMathErrorCodes.UINT256_MULTIPLICATION_OVERFLOW,
LibRichErrors._rrevert(LibSafeMathRichErrors.SafeMathError(
LibSafeMathRichErrors.SafeMathErrorCodes.UINT256_MULTIPLICATION_OVERFLOW,
a,
b
));
@@ -41,8 +40,8 @@ contract SafeMath is
returns (uint256)
{
if (b > a) {
_rrevert(SafeMathError(
SafeMathErrorCodes.UINT256_SUBTRACTION_UNDERFLOW,
LibRichErrors._rrevert(LibSafeMathRichErrors.SafeMathError(
LibSafeMathRichErrors.SafeMathErrorCodes.UINT256_SUBTRACTION_UNDERFLOW,
a,
b
));
@@ -57,8 +56,8 @@ contract SafeMath is
{
uint256 c = a + b;
if (c < a) {
_rrevert(SafeMathError(
SafeMathErrorCodes.UINT256_ADDITION_OVERFLOW,
LibRichErrors._rrevert(LibSafeMathRichErrors.SafeMathError(
LibSafeMathRichErrors.SafeMathErrorCodes.UINT256_ADDITION_OVERFLOW,
a,
b
));