Commit 1ae59c64 authored by smartcontracts's avatar smartcontracts Committed by GitHub

refactor[contracts]: Various minor cleanups to library contracts (#613)

parent 9a7dd608
...@@ -36,8 +36,7 @@ contract Lib_ResolvedDelegateProxy { ...@@ -36,8 +36,7 @@ contract Lib_ResolvedDelegateProxy {
constructor( constructor(
address _libAddressManager, address _libAddressManager,
string memory _implementationName string memory _implementationName
) ) {
{
addressManager[address(this)] = Lib_AddressManager(_libAddressManager); addressManager[address(this)] = Lib_AddressManager(_libAddressManager);
implementationName[address(this)] = _implementationName; implementationName[address(this)] = _implementationName;
} }
......
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
pragma solidity >0.5.0 <0.8.0; pragma solidity >0.5.0 <0.8.0;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
/* Library Imports */
import { Lib_BytesUtils } from "../utils/Lib_BytesUtils.sol";
/** /**
* @title Lib_RLPWriter * @title Lib_RLPWriter
* @author Bakaoh (with modifications) * @author Bakaoh (with modifications)
...@@ -18,7 +15,7 @@ library Lib_RLPWriter { ...@@ -18,7 +15,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a byte string. * RLP encodes a byte string.
* @param _in The byte string to encode. * @param _in The byte string to encode.
* @return _out The RLP encoded string in bytes. * @return The RLP encoded string in bytes.
*/ */
function writeBytes( function writeBytes(
bytes memory _in bytes memory _in
...@@ -26,7 +23,7 @@ library Lib_RLPWriter { ...@@ -26,7 +23,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
bytes memory encoded; bytes memory encoded;
...@@ -43,7 +40,7 @@ library Lib_RLPWriter { ...@@ -43,7 +40,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a list of RLP encoded byte byte strings. * RLP encodes a list of RLP encoded byte byte strings.
* @param _in The list of RLP encoded byte strings. * @param _in The list of RLP encoded byte strings.
* @return _out The RLP encoded list of items in bytes. * @return The RLP encoded list of items in bytes.
*/ */
function writeList( function writeList(
bytes[] memory _in bytes[] memory _in
...@@ -51,7 +48,7 @@ library Lib_RLPWriter { ...@@ -51,7 +48,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
bytes memory list = _flatten(_in); bytes memory list = _flatten(_in);
...@@ -61,7 +58,7 @@ library Lib_RLPWriter { ...@@ -61,7 +58,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a string. * RLP encodes a string.
* @param _in The string to encode. * @param _in The string to encode.
* @return _out The RLP encoded string in bytes. * @return The RLP encoded string in bytes.
*/ */
function writeString( function writeString(
string memory _in string memory _in
...@@ -69,7 +66,7 @@ library Lib_RLPWriter { ...@@ -69,7 +66,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
return writeBytes(bytes(_in)); return writeBytes(bytes(_in));
...@@ -78,7 +75,7 @@ library Lib_RLPWriter { ...@@ -78,7 +75,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes an address. * RLP encodes an address.
* @param _in The address to encode. * @param _in The address to encode.
* @return _out The RLP encoded address in bytes. * @return The RLP encoded address in bytes.
*/ */
function writeAddress( function writeAddress(
address _in address _in
...@@ -86,7 +83,7 @@ library Lib_RLPWriter { ...@@ -86,7 +83,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
return writeBytes(abi.encodePacked(_in)); return writeBytes(abi.encodePacked(_in));
...@@ -95,7 +92,7 @@ library Lib_RLPWriter { ...@@ -95,7 +92,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a uint. * RLP encodes a uint.
* @param _in The uint256 to encode. * @param _in The uint256 to encode.
* @return _out The RLP encoded uint256 in bytes. * @return The RLP encoded uint256 in bytes.
*/ */
function writeUint( function writeUint(
uint256 _in uint256 _in
...@@ -103,7 +100,7 @@ library Lib_RLPWriter { ...@@ -103,7 +100,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
return writeBytes(_toBinary(_in)); return writeBytes(_toBinary(_in));
...@@ -112,7 +109,7 @@ library Lib_RLPWriter { ...@@ -112,7 +109,7 @@ library Lib_RLPWriter {
/** /**
* RLP encodes a bool. * RLP encodes a bool.
* @param _in The bool to encode. * @param _in The bool to encode.
* @return _out The RLP encoded bool in bytes. * @return The RLP encoded bool in bytes.
*/ */
function writeBool( function writeBool(
bool _in bool _in
...@@ -120,7 +117,7 @@ library Lib_RLPWriter { ...@@ -120,7 +117,7 @@ library Lib_RLPWriter {
internal internal
pure pure
returns ( returns (
bytes memory _out bytes memory
) )
{ {
bytes memory encoded = new bytes(1); bytes memory encoded = new bytes(1);
...@@ -137,7 +134,7 @@ library Lib_RLPWriter { ...@@ -137,7 +134,7 @@ library Lib_RLPWriter {
* Encode the first byte, followed by the `len` in binary form if `length` is more than 55. * Encode the first byte, followed by the `len` in binary form if `length` is more than 55.
* @param _len The length of the string or the payload. * @param _len The length of the string or the payload.
* @param _offset 128 if item is string, 192 if item is list. * @param _offset 128 if item is string, 192 if item is list.
* @return _encoded RLP encoded bytes. * @return RLP encoded bytes.
*/ */
function _writeLength( function _writeLength(
uint256 _len, uint256 _len,
...@@ -146,7 +143,7 @@ library Lib_RLPWriter { ...@@ -146,7 +143,7 @@ library Lib_RLPWriter {
private private
pure pure
returns ( returns (
bytes memory _encoded bytes memory
) )
{ {
bytes memory encoded; bytes memory encoded;
...@@ -176,7 +173,7 @@ library Lib_RLPWriter { ...@@ -176,7 +173,7 @@ library Lib_RLPWriter {
* Encode integer in big endian binary form with no leading zeroes. * Encode integer in big endian binary form with no leading zeroes.
* @notice TODO: This should be optimized with assembly to save gas costs. * @notice TODO: This should be optimized with assembly to save gas costs.
* @param _x The integer to encode. * @param _x The integer to encode.
* @return _binary RLP encoded bytes. * @return RLP encoded bytes.
*/ */
function _toBinary( function _toBinary(
uint256 _x uint256 _x
...@@ -184,7 +181,7 @@ library Lib_RLPWriter { ...@@ -184,7 +181,7 @@ library Lib_RLPWriter {
private private
pure pure
returns ( returns (
bytes memory _binary bytes memory
) )
{ {
bytes memory b = abi.encodePacked(_x); bytes memory b = abi.encodePacked(_x);
...@@ -243,7 +240,7 @@ library Lib_RLPWriter { ...@@ -243,7 +240,7 @@ library Lib_RLPWriter {
* Flattens a list of byte strings into one byte string. * Flattens a list of byte strings into one byte string.
* @notice From: https://github.com/sammayo/solidity-rlp-encoder/blob/master/RLPEncode.sol. * @notice From: https://github.com/sammayo/solidity-rlp-encoder/blob/master/RLPEncode.sol.
* @param _list List of byte strings to flatten. * @param _list List of byte strings to flatten.
* @return _flattened The flattened byte string. * @return The flattened byte string.
*/ */
function _flatten( function _flatten(
bytes[] memory _list bytes[] memory _list
...@@ -251,7 +248,7 @@ library Lib_RLPWriter { ...@@ -251,7 +248,7 @@ library Lib_RLPWriter {
private private
pure pure
returns ( returns (
bytes memory _flattened bytes memory
) )
{ {
if (_list.length == 0) { if (_list.length == 0) {
...@@ -280,4 +277,4 @@ library Lib_RLPWriter { ...@@ -280,4 +277,4 @@ library Lib_RLPWriter {
return flattened; return flattened;
} }
} }
\ No newline at end of file
...@@ -17,7 +17,9 @@ library Lib_BytesUtils { ...@@ -17,7 +17,9 @@ library Lib_BytesUtils {
) )
internal internal
pure pure
returns (bytes memory) returns (
bytes memory
)
{ {
require(_length + 31 >= _length, "slice_overflow"); require(_length + 31 >= _length, "slice_overflow");
require(_start + _length >= _start, "slice_overflow"); require(_start + _length >= _start, "slice_overflow");
...@@ -87,7 +89,9 @@ library Lib_BytesUtils { ...@@ -87,7 +89,9 @@ library Lib_BytesUtils {
) )
internal internal
pure pure
returns (bytes memory) returns (
bytes memory
)
{ {
if (_bytes.length - _start == 0) { if (_bytes.length - _start == 0) {
return bytes(''); return bytes('');
...@@ -101,7 +105,9 @@ library Lib_BytesUtils { ...@@ -101,7 +105,9 @@ library Lib_BytesUtils {
) )
internal internal
pure pure
returns (bytes32) returns (
bytes32
)
{ {
bytes32 ret; bytes32 ret;
uint256 len = _bytes.length <= 32 ? _bytes.length : 32; uint256 len = _bytes.length <= 32 ? _bytes.length : 32;
...@@ -116,7 +122,9 @@ library Lib_BytesUtils { ...@@ -116,7 +122,9 @@ library Lib_BytesUtils {
) )
internal internal
pure pure
returns (bytes32) returns (
bytes32
)
{ {
if (_bytes.length < 32) { if (_bytes.length < 32) {
bytes32 ret; bytes32 ret;
...@@ -134,12 +142,23 @@ library Lib_BytesUtils { ...@@ -134,12 +142,23 @@ library Lib_BytesUtils {
) )
internal internal
pure pure
returns (uint256) returns (
uint256
)
{ {
return uint256(toBytes32(_bytes)); return uint256(toBytes32(_bytes));
} }
function toUint24(bytes memory _bytes, uint256 _start) internal pure returns (uint24) { function toUint24(
bytes memory _bytes,
uint256 _start
)
internal
pure
returns (
uint24
)
{
require(_start + 3 >= _start, "toUint24_overflow"); require(_start + 3 >= _start, "toUint24_overflow");
require(_bytes.length >= _start + 3 , "toUint24_outOfBounds"); require(_bytes.length >= _start + 3 , "toUint24_outOfBounds");
uint24 tempUint; uint24 tempUint;
...@@ -151,7 +170,16 @@ library Lib_BytesUtils { ...@@ -151,7 +170,16 @@ library Lib_BytesUtils {
return tempUint; return tempUint;
} }
function toUint8(bytes memory _bytes, uint256 _start) internal pure returns (uint8) { function toUint8(
bytes memory _bytes,
uint256 _start
)
internal
pure
returns (
uint8
)
{
require(_start + 1 >= _start, "toUint8_overflow"); require(_start + 1 >= _start, "toUint8_overflow");
require(_bytes.length >= _start + 1 , "toUint8_outOfBounds"); require(_bytes.length >= _start + 1 , "toUint8_outOfBounds");
uint8 tempUint; uint8 tempUint;
...@@ -163,7 +191,16 @@ library Lib_BytesUtils { ...@@ -163,7 +191,16 @@ library Lib_BytesUtils {
return tempUint; return tempUint;
} }
function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) { function toAddress(
bytes memory _bytes,
uint256 _start
)
internal
pure
returns (
address
)
{
require(_start + 20 >= _start, "toAddress_overflow"); require(_start + 20 >= _start, "toAddress_overflow");
require(_bytes.length >= _start + 20, "toAddress_outOfBounds"); require(_bytes.length >= _start + 20, "toAddress_outOfBounds");
address tempAddress; address tempAddress;
...@@ -180,7 +217,9 @@ library Lib_BytesUtils { ...@@ -180,7 +217,9 @@ library Lib_BytesUtils {
) )
internal internal
pure pure
returns (bytes memory) returns (
bytes memory
)
{ {
bytes memory nibbles = new bytes(_bytes.length * 2); bytes memory nibbles = new bytes(_bytes.length * 2);
...@@ -197,7 +236,9 @@ library Lib_BytesUtils { ...@@ -197,7 +236,9 @@ library Lib_BytesUtils {
) )
internal internal
pure pure
returns (bytes memory) returns (
bytes memory
)
{ {
bytes memory ret = new bytes(_bytes.length / 2); bytes memory ret = new bytes(_bytes.length / 2);
...@@ -214,7 +255,9 @@ library Lib_BytesUtils { ...@@ -214,7 +255,9 @@ library Lib_BytesUtils {
) )
internal internal
pure pure
returns (bool) returns (
bool
)
{ {
return keccak256(_bytes) == keccak256(_other); return keccak256(_bytes) == keccak256(_other);
} }
......
...@@ -12,16 +12,16 @@ import { Lib_Bytes32Utils } from "./Lib_Bytes32Utils.sol"; ...@@ -12,16 +12,16 @@ import { Lib_Bytes32Utils } from "./Lib_Bytes32Utils.sol";
*/ */
library Lib_EthUtils { library Lib_EthUtils {
/*********************************** /**********************
* Internal Functions: Code Access * * Internal Functions *
***********************************/ **********************/
/** /**
* Gets the code for a given address. * Gets the code for a given address.
* @param _address Address to get code for. * @param _address Address to get code for.
* @param _offset Offset to start reading from. * @param _offset Offset to start reading from.
* @param _length Number of bytes to read. * @param _length Number of bytes to read.
* @return _code Code read from the contract. * @return Code read from the contract.
*/ */
function getCode( function getCode(
address _address, address _address,
...@@ -31,23 +31,24 @@ library Lib_EthUtils { ...@@ -31,23 +31,24 @@ library Lib_EthUtils {
internal internal
view view
returns ( returns (
bytes memory _code bytes memory
) )
{ {
bytes memory code;
assembly { assembly {
_code := mload(0x40) code := mload(0x40)
mstore(0x40, add(_code, add(_length, 0x20))) mstore(0x40, add(code, add(_length, 0x20)))
mstore(_code, _length) mstore(code, _length)
extcodecopy(_address, add(_code, 0x20), _offset, _length) extcodecopy(_address, add(code, 0x20), _offset, _length)
} }
return _code; return code;
} }
/** /**
* Gets the full code for a given address. * Gets the full code for a given address.
* @param _address Address to get code for. * @param _address Address to get code for.
* @return _code Full code of the contract. * @return Full code of the contract.
*/ */
function getCode( function getCode(
address _address address _address
...@@ -55,7 +56,7 @@ library Lib_EthUtils { ...@@ -55,7 +56,7 @@ library Lib_EthUtils {
internal internal
view view
returns ( returns (
bytes memory _code bytes memory
) )
{ {
return getCode( return getCode(
...@@ -68,7 +69,7 @@ library Lib_EthUtils { ...@@ -68,7 +69,7 @@ library Lib_EthUtils {
/** /**
* Gets the size of a contract's code in bytes. * Gets the size of a contract's code in bytes.
* @param _address Address to get code size for. * @param _address Address to get code size for.
* @return _codeSize Size of the contract's code in bytes. * @return Size of the contract's code in bytes.
*/ */
function getCodeSize( function getCodeSize(
address _address address _address
...@@ -76,20 +77,21 @@ library Lib_EthUtils { ...@@ -76,20 +77,21 @@ library Lib_EthUtils {
internal internal
view view
returns ( returns (
uint256 _codeSize uint256
) )
{ {
uint256 codeSize;
assembly { assembly {
_codeSize := extcodesize(_address) codeSize := extcodesize(_address)
} }
return _codeSize; return codeSize;
} }
/** /**
* Gets the hash of a contract's code. * Gets the hash of a contract's code.
* @param _address Address to get a code hash for. * @param _address Address to get a code hash for.
* @return _codeHash Hash of the contract's code. * @return Hash of the contract's code.
*/ */
function getCodeHash( function getCodeHash(
address _address address _address
...@@ -97,50 +99,47 @@ library Lib_EthUtils { ...@@ -97,50 +99,47 @@ library Lib_EthUtils {
internal internal
view view
returns ( returns (
bytes32 _codeHash bytes32
) )
{ {
bytes32 codeHash;
assembly { assembly {
_codeHash := extcodehash(_address) codeHash := extcodehash(_address)
} }
return _codeHash; return codeHash;
} }
/*****************************************
* Internal Functions: Contract Creation *
*****************************************/
/** /**
* Creates a contract with some given initialization code. * Creates a contract with some given initialization code.
* @param _code Contract initialization code. * @param _code Contract initialization code.
* @return _created Address of the created contract. * @return Address of the created contract.
*/ */
function createContract( function createContract(
bytes memory _code bytes memory _code
) )
internal internal
returns ( returns (
address _created address
) )
{ {
address created;
assembly { assembly {
_created := create( created := create(
0, 0,
add(_code, 0x20), add(_code, 0x20),
mload(_code) mload(_code)
) )
} }
return _created; return created;
} }
/** /**
* Computes the address that would be generated by CREATE. * Computes the address that would be generated by CREATE.
* @param _creator Address creating the contract. * @param _creator Address creating the contract.
* @param _nonce Creator's nonce. * @param _nonce Creator's nonce.
* @return _address Address to be generated by CREATE. * @return Address to be generated by CREATE.
*/ */
function getAddressForCREATE( function getAddressForCREATE(
address _creator, address _creator,
...@@ -149,7 +148,7 @@ library Lib_EthUtils { ...@@ -149,7 +148,7 @@ library Lib_EthUtils {
internal internal
pure pure
returns ( returns (
address _address address
) )
{ {
bytes[] memory encoded = new bytes[](2); bytes[] memory encoded = new bytes[](2);
...@@ -165,7 +164,7 @@ library Lib_EthUtils { ...@@ -165,7 +164,7 @@ library Lib_EthUtils {
* @param _creator Address creating the contract. * @param _creator Address creating the contract.
* @param _bytecode Bytecode of the contract to be created. * @param _bytecode Bytecode of the contract to be created.
* @param _salt 32 byte salt value mixed into the hash. * @param _salt 32 byte salt value mixed into the hash.
* @return _address Address to be generated by CREATE2. * @return Address to be generated by CREATE2.
*/ */
function getAddressForCREATE2( function getAddressForCREATE2(
address _creator, address _creator,
...@@ -174,7 +173,9 @@ library Lib_EthUtils { ...@@ -174,7 +173,9 @@ library Lib_EthUtils {
) )
internal internal
pure pure
returns (address _address) returns (
address
)
{ {
bytes32 hashedData = keccak256(abi.encodePacked( bytes32 hashedData = keccak256(abi.encodePacked(
byte(0xff), byte(0xff),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment