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