Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move mappings key to constants #261

Merged
merged 33 commits into from Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8eb0ab5
Add GAS_PRICE constant key
patitonar Aug 2, 2019
2041250
Add REQUIRED_BLOCK_CONFIRMATIONS constant key
patitonar Aug 2, 2019
43972ee
Add TOTAL_SPENT_PER_DAY constant key
patitonar Aug 2, 2019
faee375
Add TOTAL_EXECUTED_PER_DAY constant key
patitonar Aug 2, 2019
7934c08
Add MIN_PER_TX constant key
patitonar Aug 2, 2019
c9ed318
Add MAX_PER_TX constant key
patitonar Aug 2, 2019
381e44a
Add DAILY_LIMIT constant key
patitonar Aug 2, 2019
8278fa4
Add EXECUTION_MAX_PER_TX constant key
patitonar Aug 2, 2019
1af28bd
Add EXECUTION_DAILY_LIMIT constant key
patitonar Aug 2, 2019
40bbf14
Add Initializable base contract
patitonar Aug 2, 2019
6811276
Add REQUIRED_SIGNATURES constant key
patitonar Aug 2, 2019
a045471
Add HOME_FEE_STORAGE_KEY constant key
patitonar Aug 2, 2019
841b716
Add FOREIGN_FEE_STORAGE_KEY constant key
patitonar Aug 2, 2019
7d014b2
Remove TOTAL_SPENT_PER_DAY and TOTAL_EXECUTED_PER_DAY keys
patitonar Aug 2, 2019
40a90fc
Add VALIDATOR_COUNT constant key
patitonar Aug 2, 2019
d592306
Add FEE_MANAGER_CONTRACT constant key
patitonar Aug 2, 2019
bcf43b5
Add VALIDATOR_CONTRACT constant key
patitonar Aug 2, 2019
0b2da1c
Fix ValidatorsFeeManager
patitonar Aug 2, 2019
4cc6a10
Add ERC20Bridge contract
patitonar Aug 2, 2019
0d904c6
Add BlockRewardBridge contract
patitonar Aug 2, 2019
d4220e1
Add ERC677_TOKEN constant key
patitonar Aug 2, 2019
baae8a5
Add ValidatorStorage contract
patitonar Aug 2, 2019
59d4009
Add OUT_OF_LIMIT_AMOUNT constant key
patitonar Aug 2, 2019
09852c5
Add TOTAL_BURNT_COINS constant key
patitonar Aug 2, 2019
1204cb8
Merge branch 'develop' into storage-key-constants
patitonar Aug 2, 2019
08f3308
Replace method signature with constant selector in BlockReward
patitonar Aug 5, 2019
5956590
Replace method signature with constant selector in BlockRewardBridge
patitonar Aug 5, 2019
f18ec5f
Replace method signature with constant selector in Claimable
patitonar Aug 5, 2019
16b8105
Replace method signature with constant selector in ERC677BridgeToken
patitonar Aug 5, 2019
9704758
Replace method signature with constant selector in HomeBridgeErcToErc…
patitonar Aug 5, 2019
1adc26e
Replace method signature with constant selector in RewardableBridge
patitonar Aug 5, 2019
8b696e3
Replace method signature with constant selector in RewardableHomeBrid…
patitonar Aug 5, 2019
be9b8e8
Revert "Replace method signature with constant selector in ERC677Brid…
patitonar Aug 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion contracts/mocks/BlockReward.sol
Expand Up @@ -11,6 +11,7 @@ contract BlockReward {
uint256 public feeAmount = 0;
mapping(bytes32 => uint256) internal uintStorage;
bytes32 internal constant MINTED_TOTALLY_BY_BRIDGE = "mintedTotallyByBridge";
bytes4 internal constant MINT_REWARD = 0xe2f764a3; // mintReward(address[],uint256[])
address public token;

function() external payable {
Expand Down Expand Up @@ -86,7 +87,7 @@ contract BlockReward {
rewards[i] = feeToDistribute;
}

require(token.call(abi.encodeWithSignature("mintReward(address[],uint256[])", receivers, rewards)));
require(token.call(abi.encodeWithSelector(MINT_REWARD, receivers, rewards)));
}

function random(uint256 _count) public view returns (uint256) {
Expand Down
26 changes: 8 additions & 18 deletions contracts/upgradeable_contracts/BaseBridgeValidators.sol
Expand Up @@ -2,13 +2,15 @@ pragma solidity 0.4.24;

import "./Ownable.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "../upgradeability/EternalStorage.sol";
import "./Initializable.sol";

contract BaseBridgeValidators is EternalStorage, Ownable {
contract BaseBridgeValidators is Initializable, Ownable {
using SafeMath for uint256;

address public constant F_ADDR = 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF;
uint256 internal constant MAX_VALIDATORS = 100;
bytes32 internal constant REQUIRED_SIGNATURES = keccak256(abi.encodePacked("requiredSignatures"));
bytes32 internal constant VALIDATOR_COUNT = keccak256(abi.encodePacked("validatorCount"));

event ValidatorAdded(address indexed validator);
event ValidatorRemoved(address indexed validator);
Expand All @@ -17,7 +19,7 @@ contract BaseBridgeValidators is EternalStorage, Ownable {
function setRequiredSignatures(uint256 _requiredSignatures) external onlyOwner {
require(validatorCount() >= _requiredSignatures);
require(_requiredSignatures != 0);
uintStorage[keccak256(abi.encodePacked("requiredSignatures"))] = _requiredSignatures;
uintStorage[REQUIRED_SIGNATURES] = _requiredSignatures;
emit RequiredSignaturesChanged(_requiredSignatures);
}

Expand Down Expand Up @@ -74,25 +76,17 @@ contract BaseBridgeValidators is EternalStorage, Ownable {
}

function requiredSignatures() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("requiredSignatures"))];
return uintStorage[REQUIRED_SIGNATURES];
}

function validatorCount() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("validatorCount"))];
return uintStorage[VALIDATOR_COUNT];
}

function isValidator(address _validator) public view returns (bool) {
return _validator != F_ADDR && getNextValidator(_validator) != address(0);
}

function isInitialized() public view returns (bool) {
return boolStorage[keccak256(abi.encodePacked("isInitialized"))];
}

function deployedAtBlock() external view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("deployedAtBlock"))];
}

function getNextValidator(address _address) public view returns (address) {
return addressStorage[keccak256(abi.encodePacked("validatorsList", _address))];
}
Expand All @@ -102,14 +96,10 @@ contract BaseBridgeValidators is EternalStorage, Ownable {
}

function setValidatorCount(uint256 _validatorCount) internal {
uintStorage[keccak256(abi.encodePacked("validatorCount"))] = _validatorCount;
uintStorage[VALIDATOR_COUNT] = _validatorCount;
}

function setNextValidator(address _prevValidator, address _validator) internal {
addressStorage[keccak256(abi.encodePacked("validatorsList", _prevValidator))] = _validator;
}

function setInitialize() internal {
boolStorage[keccak256(abi.encodePacked("isInitialized"))] = true;
}
}
10 changes: 6 additions & 4 deletions contracts/upgradeable_contracts/BaseFeeManager.sol
Expand Up @@ -13,6 +13,8 @@ contract BaseFeeManager is EternalStorage, FeeTypes {

// This is not a real fee value but a relative value used to calculate the fee percentage
uint256 internal constant MAX_FEE = 1 ether;
bytes32 internal constant HOME_FEE_STORAGE_KEY = keccak256(abi.encodePacked("homeFee"));
bytes32 internal constant FOREIGN_FEE_STORAGE_KEY = keccak256(abi.encodePacked("foreignFee"));

function calculateFee(uint256 _value, bool _recover, bytes32 _feeType) public view returns (uint256) {
uint256 fee = _feeType == HOME_FEE ? getHomeFee() : getForeignFee();
Expand All @@ -29,21 +31,21 @@ contract BaseFeeManager is EternalStorage, FeeTypes {
}

function setHomeFee(uint256 _fee) external validFee(_fee) {
uintStorage[keccak256(abi.encodePacked("homeFee"))] = _fee;
uintStorage[HOME_FEE_STORAGE_KEY] = _fee;
emit HomeFeeUpdated(_fee);
}

function getHomeFee() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("homeFee"))];
return uintStorage[HOME_FEE_STORAGE_KEY];
}

function setForeignFee(uint256 _fee) external validFee(_fee) {
uintStorage[keccak256(abi.encodePacked("foreignFee"))] = _fee;
uintStorage[FOREIGN_FEE_STORAGE_KEY] = _fee;
emit ForeignFeeUpdated(_fee);
}

function getForeignFee() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("foreignFee"))];
return uintStorage[FOREIGN_FEE_STORAGE_KEY];
}

/* solcov ignore next */
Expand Down
52 changes: 24 additions & 28 deletions contracts/upgradeable_contracts/BasicBridge.sol
Expand Up @@ -2,47 +2,51 @@ pragma solidity 0.4.24;

import "../interfaces/IBridgeValidators.sol";
import "./Upgradeable.sol";
import "../upgradeability/EternalStorage.sol";
import "./Initializable.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/AddressUtils.sol";
import "./Validatable.sol";
import "./Ownable.sol";
import "./Claimable.sol";

contract BasicBridge is EternalStorage, Validatable, Ownable, Upgradeable, Claimable {
contract BasicBridge is Initializable, Validatable, Ownable, Upgradeable, Claimable {
using SafeMath for uint256;

event GasPriceChanged(uint256 gasPrice);
event RequiredBlockConfirmationChanged(uint256 requiredBlockConfirmations);
event DailyLimitChanged(uint256 newLimit);
event ExecutionDailyLimitChanged(uint256 newLimit);

bytes32 internal constant GAS_PRICE = keccak256(abi.encodePacked("gasPrice"));
bytes32 internal constant REQUIRED_BLOCK_CONFIRMATIONS = keccak256(abi.encodePacked("requiredBlockConfirmations"));
bytes32 internal constant MIN_PER_TX = keccak256(abi.encodePacked("minPerTx"));
bytes32 internal constant MAX_PER_TX = keccak256(abi.encodePacked("maxPerTx"));
bytes32 internal constant DAILY_LIMIT = keccak256(abi.encodePacked("dailyLimit"));
bytes32 internal constant EXECUTION_MAX_PER_TX = keccak256(abi.encodePacked("executionMaxPerTx"));
bytes32 internal constant EXECUTION_DAILY_LIMIT = keccak256(abi.encodePacked("executionDailyLimit"));

function getBridgeInterfacesVersion() external pure returns (uint64 major, uint64 minor, uint64 patch) {
return (2, 3, 0);
}

function setGasPrice(uint256 _gasPrice) external onlyOwner {
require(_gasPrice > 0);
uintStorage[keccak256(abi.encodePacked("gasPrice"))] = _gasPrice;
uintStorage[GAS_PRICE] = _gasPrice;
emit GasPriceChanged(_gasPrice);
}

function gasPrice() external view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("gasPrice"))];
return uintStorage[GAS_PRICE];
}

function setRequiredBlockConfirmations(uint256 _blockConfirmations) external onlyOwner {
require(_blockConfirmations > 0);
uintStorage[keccak256(abi.encodePacked("requiredBlockConfirmations"))] = _blockConfirmations;
uintStorage[REQUIRED_BLOCK_CONFIRMATIONS] = _blockConfirmations;
emit RequiredBlockConfirmationChanged(_blockConfirmations);
}

function requiredBlockConfirmations() external view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("requiredBlockConfirmations"))];
}

function deployedAtBlock() external view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("deployedAtBlock"))];
return uintStorage[REQUIRED_BLOCK_CONFIRMATIONS];
}

function setTotalSpentPerDay(uint256 _day, uint256 _value) internal {
Expand All @@ -62,23 +66,15 @@ contract BasicBridge is EternalStorage, Validatable, Ownable, Upgradeable, Claim
}

function minPerTx() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("minPerTx"))];
return uintStorage[MIN_PER_TX];
}

function maxPerTx() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("maxPerTx"))];
return uintStorage[MAX_PER_TX];
}

function executionMaxPerTx() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("executionMaxPerTx"))];
}

function setInitialize() internal {
boolStorage[keccak256(abi.encodePacked("isInitialized"))] = true;
}

function isInitialized() public view returns (bool) {
return boolStorage[keccak256(abi.encodePacked("isInitialized"))];
return uintStorage[EXECUTION_MAX_PER_TX];
}

function getCurrentDay() public view returns (uint256) {
Expand All @@ -88,37 +84,37 @@ contract BasicBridge is EternalStorage, Validatable, Ownable, Upgradeable, Claim

function setDailyLimit(uint256 _dailyLimit) external onlyOwner {
require(_dailyLimit > maxPerTx() || _dailyLimit == 0);
uintStorage[keccak256(abi.encodePacked("dailyLimit"))] = _dailyLimit;
uintStorage[DAILY_LIMIT] = _dailyLimit;
emit DailyLimitChanged(_dailyLimit);
}

function dailyLimit() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("dailyLimit"))];
return uintStorage[DAILY_LIMIT];
}

function setExecutionDailyLimit(uint256 _dailyLimit) external onlyOwner {
require(_dailyLimit > executionMaxPerTx() || _dailyLimit == 0);
uintStorage[keccak256(abi.encodePacked("executionDailyLimit"))] = _dailyLimit;
uintStorage[EXECUTION_DAILY_LIMIT] = _dailyLimit;
emit ExecutionDailyLimitChanged(_dailyLimit);
}

function executionDailyLimit() public view returns (uint256) {
return uintStorage[keccak256(abi.encodePacked("executionDailyLimit"))];
return uintStorage[EXECUTION_DAILY_LIMIT];
}

function setExecutionMaxPerTx(uint256 _maxPerTx) external onlyOwner {
require(_maxPerTx < executionDailyLimit());
uintStorage[keccak256(abi.encodePacked("executionMaxPerTx"))] = _maxPerTx;
uintStorage[EXECUTION_MAX_PER_TX] = _maxPerTx;
}

function setMaxPerTx(uint256 _maxPerTx) external onlyOwner {
require(_maxPerTx < dailyLimit());
uintStorage[keccak256(abi.encodePacked("maxPerTx"))] = _maxPerTx;
uintStorage[MAX_PER_TX] = _maxPerTx;
}

function setMinPerTx(uint256 _minPerTx) external onlyOwner {
require(_minPerTx < dailyLimit() && _minPerTx < maxPerTx());
uintStorage[keccak256(abi.encodePacked("minPerTx"))] = _minPerTx;
uintStorage[MIN_PER_TX] = _minPerTx;
}

function withinLimit(uint256 _amount) public view returns (bool) {
Expand Down
31 changes: 31 additions & 0 deletions contracts/upgradeable_contracts/BlockRewardBridge.sol
@@ -0,0 +1,31 @@
pragma solidity 0.4.24;

import "../interfaces/IBlockReward.sol";
import "../upgradeability/EternalStorage.sol";
import "openzeppelin-solidity/contracts/AddressUtils.sol";

contract BlockRewardBridge is EternalStorage {
bytes32 internal constant BLOCK_REWARD_CONTRACT = keccak256(abi.encodePacked("blockRewardContract"));
bytes4 internal constant BLOCK_REWARD_CONTRACT_ID = 0x2ee57f8d; // blockRewardContractId()
bytes4 internal constant BRIDGES_ALLOWED_LENGTH = 0x10f2ee7c; // bridgesAllowedLength()

function _blockRewardContract() internal view returns (IBlockReward) {
return IBlockReward(addressStorage[BLOCK_REWARD_CONTRACT]);
}

function _setBlockRewardContract(address _blockReward) internal {
require(AddressUtils.isContract(_blockReward));

// Before store the contract we need to make sure that it is the block reward contract in actual fact,
// call a specific method from the contract that should return a specific value
bool isBlockRewardContract = false;
if (_blockReward.call(BLOCK_REWARD_CONTRACT_ID)) {
isBlockRewardContract =
IBlockReward(_blockReward).blockRewardContractId() == bytes4(keccak256("blockReward"));
} else if (_blockReward.call(BRIDGES_ALLOWED_LENGTH)) {
isBlockRewardContract = IBlockReward(_blockReward).bridgesAllowedLength() != 0;
}
require(isBlockRewardContract);
addressStorage[BLOCK_REWARD_CONTRACT] = _blockReward;
}
}
8 changes: 2 additions & 6 deletions contracts/upgradeable_contracts/BlockRewardFeeManager.sol
@@ -1,9 +1,9 @@
pragma solidity 0.4.24;

import "./BaseFeeManager.sol";
import "../interfaces/IBlockReward.sol";
import "./BlockRewardBridge.sol";

contract BlockRewardFeeManager is BaseFeeManager {
contract BlockRewardFeeManager is BaseFeeManager, BlockRewardBridge {
function distributeFeeFromAffirmation(uint256 _fee) external {
distributeFeeFromBlockReward(_fee);
}
Expand All @@ -12,10 +12,6 @@ contract BlockRewardFeeManager is BaseFeeManager {
distributeFeeFromBlockReward(_fee);
}

function _blockRewardContract() internal view returns (IBlockReward) {
return IBlockReward(addressStorage[keccak256(abi.encodePacked("blockRewardContract"))]);
}

/* solcov ignore next */
function distributeFeeFromBlockReward(uint256 _fee) internal;
}
4 changes: 2 additions & 2 deletions contracts/upgradeable_contracts/BridgeValidators.sol
Expand Up @@ -34,8 +34,8 @@ contract BridgeValidators is BaseBridgeValidators {
}

setValidatorCount(_initialValidators.length);
uintStorage[keccak256(abi.encodePacked("requiredSignatures"))] = _requiredSignatures;
uintStorage[keccak256(abi.encodePacked("deployedAtBlock"))] = block.number;
uintStorage[REQUIRED_SIGNATURES] = _requiredSignatures;
uintStorage[DEPLOYED_AT_BLOCK] = block.number;
setInitialize();
emit RequiredSignaturesChanged(_requiredSignatures);

Expand Down
4 changes: 3 additions & 1 deletion contracts/upgradeable_contracts/Claimable.sol
Expand Up @@ -4,6 +4,8 @@ import "openzeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol";
import "./Sacrifice.sol";

contract Claimable {
bytes4 internal constant TRANSFER = 0xa9059cbb; // transfer(address,uint256)

modifier validAddress(address _to) {
require(_to != address(0));
/* solcov ignore next */
Expand Down Expand Up @@ -34,7 +36,7 @@ contract Claimable {
function safeTransfer(address _token, address _to, uint256 _value) internal {
bytes memory returnData;
bool returnDataResult;
bytes memory callData = abi.encodeWithSignature("transfer(address,uint256)", _to, _value);
bytes memory callData = abi.encodeWithSelector(TRANSFER, _to, _value);
assembly {
let result := call(gas, _token, 0x0, add(callData, 0x20), mload(callData), 0, 32)
returnData := mload(0)
Expand Down
18 changes: 18 additions & 0 deletions contracts/upgradeable_contracts/ERC20Bridge.sol
@@ -0,0 +1,18 @@
pragma solidity 0.4.24;

import "../upgradeability/EternalStorage.sol";
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol";
import "openzeppelin-solidity/contracts/AddressUtils.sol";

contract ERC20Bridge is EternalStorage {
bytes32 internal constant ERC20_TOKEN = keccak256(abi.encodePacked("erc20token"));

function erc20token() public view returns (ERC20Basic) {
return ERC20Basic(addressStorage[ERC20_TOKEN]);
}

function setErc20token(address _token) internal {
require(AddressUtils.isContract(_token));
addressStorage[ERC20_TOKEN] = _token;
}
}
7 changes: 4 additions & 3 deletions contracts/upgradeable_contracts/ERC677Bridge.sol
Expand Up @@ -3,15 +3,16 @@ pragma solidity 0.4.24;
import "./BasicBridge.sol";
import "../interfaces/ERC677.sol";
import "../interfaces/ERC677Receiver.sol";
import "./ERC677Storage.sol";

contract ERC677Bridge is BasicBridge, ERC677Receiver {
contract ERC677Bridge is BasicBridge, ERC677Receiver, ERC677Storage {
function erc677token() public view returns (ERC677) {
return ERC677(addressStorage[keccak256(abi.encodePacked("erc677token"))]);
return ERC677(addressStorage[ERC677_TOKEN]);
}

function setErc677token(address _token) internal {
require(AddressUtils.isContract(_token));
addressStorage[keccak256(abi.encodePacked("erc677token"))] = _token;
addressStorage[ERC677_TOKEN] = _token;
}

function onTokenTransfer(
Expand Down
5 changes: 5 additions & 0 deletions contracts/upgradeable_contracts/ERC677Storage.sol
@@ -0,0 +1,5 @@
pragma solidity 0.4.24;

contract ERC677Storage {
bytes32 internal constant ERC677_TOKEN = keccak256(abi.encodePacked("erc677token"));
}