diff --git a/.solcover.js b/.solcover.js index f7c21b44..e61bb21a 100644 --- a/.solcover.js +++ b/.solcover.js @@ -1,5 +1,5 @@ module.exports = { port: 8555, testrpcOptions: '--port 8555 --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea0,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea1,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea2,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea3,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea4,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea5,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea6,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea7,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea8,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5ea9,0" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb0,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb1,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb2,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb3,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb4,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb5,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb6,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb7,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb8,10000000000000000000000000000000000" --account="0x8fbbbaceff30d4eea3e2ffa2dfedc3c053f78c1f53103e4ddc31309e6b1d5eb9,0"', - skipFiles: ['gateway/MockSafeCore.sol','gateway/EIP20TokenMock.sol','gateway/OpenSTUtilityMock.sol','gateway/OpenSTValueMock.sol','gateway/ProtocolVersionedMock.sol','gateway/SafeMathMock.sol','gateway/UtilityTokenAbstractMock.sol','truffle/Migrations.sol'] + skipFiles: ['gateway/MockAnchor.sol','gateway/EIP20TokenMock.sol','gateway/OpenSTUtilityMock.sol','gateway/OpenSTValueMock.sol','gateway/ProtocolVersionedMock.sol','gateway/SafeMathMock.sol','gateway/UtilityTokenAbstractMock.sol','truffle/Migrations.sol'] }; \ No newline at end of file diff --git a/contracts/StateRootInterface.sol b/contracts/StateRootInterface.sol index 5fb4fa5e..07dddb88 100644 --- a/contracts/StateRootInterface.sol +++ b/contracts/StateRootInterface.sol @@ -26,7 +26,7 @@ interface StateRootInterface { /** * @notice Gets the block number of latest committed state root. * - * @return uint256 Block height of the latest committed state root. + * @return height_ Block height of the latest committed state root. */ function getLatestStateRootBlockHeight() external diff --git a/contracts/gateway/SafeCore.sol b/contracts/gateway/Anchor.sol similarity index 73% rename from contracts/gateway/SafeCore.sol rename to contracts/gateway/Anchor.sol index 0b31175d..8f844460 100644 --- a/contracts/gateway/SafeCore.sol +++ b/contracts/gateway/Anchor.sol @@ -20,7 +20,6 @@ pragma solidity ^0.5.0; // // ---------------------------------------------------------------------------- - import "./WorkersInterface.sol"; import "../StateRootInterface.sol"; import "../lib/CircularBufferUint.sol"; @@ -31,15 +30,15 @@ import "../lib/RLP.sol"; import "../lib/SafeMath.sol"; /** - * @title SafeCore contract which implements StateRootInterface. + * @title Anchor contract which implements StateRootInterface. * - * @notice SafeCore stores another chain's state roots. It stores the address of - * the co-core, which will be the safe core on the other chain. State - * roots are exchanged bidirectionally between the core and the co-core - * by the workers that are registered as part of the `Organized` - * interface. + * @notice Anchor stores another chain's state roots. It stores the address of + * the co-anchor, which will be the anchor on the other chain. State + * roots are exchanged bidirectionally between the anchor and the + * co-anchor by the workers that are registered as part of the + * `Organized` interface. */ -contract SafeCore is StateRootInterface, Organized, CircularBufferUint { +contract Anchor is StateRootInterface, Organized, CircularBufferUint { /* Usings */ @@ -57,13 +56,13 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint { mapping (uint256 => bytes32) private stateRoots; /** - * The remote chain ID is the remote chain id where core contract is + * The remote chain ID is the remote chain id where anchor contract is * deployed. */ uint256 private remoteChainId; - /** Address of the core on the auxiliary chain. Can be zero. */ - address public coCore; + /** Address of the anchor on the auxiliary chain. Can be zero. */ + address public coAnchor; /* Constructor */ @@ -72,7 +71,7 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint { * @notice Contract constructor. * * @param _remoteChainId The chain id of the chain that is tracked by this - * core. + * anchor. * @param _blockHeight Block height at which _stateRoot needs to store. * @param _stateRoot State root hash of given _blockHeight. * @param _maxStateRoots The max number of state roots to store in the @@ -105,27 +104,28 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint { /* External functions */ /** - * @notice The Co-Core address is the address of the core that is + * @notice The Co-Anchor address is the address of the anchor that is * deployed on the other (origin/auxiliary) chain. * - * @param _coCore Address of the Co-Core on auxiliary. + * @param _coAnchor Address of the Co-Anchor on auxiliary. */ - function setCoCoreAddress(address _coCore) + function setCoAnchorAddress(address _coAnchor) external onlyOrganization returns (bool success_) { + require( - coCore == address(0), - "Co-Core has already been set and cannot be updated." + _coAnchor != address(0), + "Co-Anchor address must not be 0." ); require( - _coCore != address(0), - "Co-Core address must not be 0." + coAnchor == address(0), + "Co-Anchor has already been set and cannot be updated." ); - coCore = _coCore; + coAnchor = _coAnchor; success_ = true; } @@ -148,9 +148,9 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint { } /** - * @notice Gets the block height of latest committed state root. + * @notice Gets the block height of latest anchored state root. * - * @return uint256 Block height of the latest committed state root. + * @return uint256 Block height of the latest anchored state root. */ function getLatestStateRootBlockHeight() external @@ -161,10 +161,10 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint { } /** - * @notice External function commitStateRoot. + * @notice External function anchorStateRoot. * - * @dev commitStateRoot Called from game process. - * Commit new state root for a block height. + * @dev anchorStateRoot Called from game process. + * Anchor new state root for a block height. * * @param _blockHeight Block height for which stateRoots mapping needs to * update. @@ -172,7 +172,7 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint { * * @return bytes32 stateRoot */ - function commitStateRoot( + function anchorStateRoot( uint256 _blockHeight, bytes32 _stateRoot ) @@ -186,10 +186,10 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint { "State root must not be zero." ); - // Input block height should be valid + // Input block height should be valid. require( _blockHeight > CircularBufferUint.head(), - "Given block height is lower or equal to highest committed state root block height." + "Given block height is lower or equal to highest anchored state root block height." ); stateRoots[_blockHeight] = _stateRoot; @@ -202,7 +202,7 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint { } /** - * @notice Get the remote chain id of this core. + * @notice Get the remote chain id of this anchor. * * @return remoteChainId_ The remote chain id. */ diff --git a/contracts/gateway/EIP20CoGateway.sol b/contracts/gateway/EIP20CoGateway.sol index 1db7ab58..23166f8a 100644 --- a/contracts/gateway/EIP20CoGateway.sol +++ b/contracts/gateway/EIP20CoGateway.sol @@ -204,7 +204,8 @@ contract EIP20CoGateway is GatewayBase { * @param _valueToken The value token contract address. * @param _utilityToken The utility token address that will be used for * minting the utility token. - * @param _core Core contract address. + * @param _stateRootProvider Contract address which implements + * StateRootInterface. * @param _bounty The amount that facilitator stakes to initiate the stake * process. * @param _membersManager Address of a contract that manages workers. @@ -214,14 +215,14 @@ contract EIP20CoGateway is GatewayBase { constructor( address _valueToken, address _utilityToken, - StateRootInterface _core, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager, address _gateway, address payable _burner ) GatewayBase( - _core, + _stateRootProvider, _bounty, _membersManager ) diff --git a/contracts/gateway/EIP20Gateway.sol b/contracts/gateway/EIP20Gateway.sol index 3db2419b..a1867d61 100644 --- a/contracts/gateway/EIP20Gateway.sol +++ b/contracts/gateway/EIP20Gateway.sol @@ -231,7 +231,8 @@ contract EIP20Gateway is GatewayBase { * in auxiliary chain. * @param _baseToken The ERC20 token address that will be used for * staking bounty from the facilitators. - * @param _core Core contract address. + * @param _stateRootProvider Contract address which implements + * StateRootInterface. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. @@ -240,13 +241,13 @@ contract EIP20Gateway is GatewayBase { constructor( EIP20Interface _token, EIP20Interface _baseToken, - StateRootInterface _core, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager, address _burner ) GatewayBase( - _core, + _stateRootProvider, _bounty, _membersManager ) diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index 5bb78fb7..ae1ec195 100644 --- a/contracts/gateway/GatewayBase.sol +++ b/contracts/gateway/GatewayBase.sol @@ -1,5 +1,26 @@ pragma solidity ^0.5.0; +// Copyright 2018 OpenST Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ---------------------------------------------------------------------------- +// Origin Chain: Gateway Contract +// +// http://www.simpletoken.org/ +// +// ---------------------------------------------------------------------------- + import "./EIP20Interface.sol"; import "../lib/MessageBus.sol"; import "../StateRootInterface.sol"; @@ -8,7 +29,6 @@ import "../lib/IsMemberInterface.sol"; import "../lib/Organized.sol"; import "../lib/SafeMath.sol"; - /** * @title GatewayBase contract. * @@ -74,8 +94,10 @@ contract GatewayBase is Organized { */ MessageBus.MessageBox messageBox; - /** Address of core contract. */ - StateRootInterface public core; + /** + * Address of contract which implements StateRootInterface. + */ + StateRootInterface public stateRootProvider; /** Path to make Merkle account proof for Gateway/CoGateway contract. */ bytes public encodedGatewayPath; @@ -128,13 +150,14 @@ contract GatewayBase is Organized { /** * @notice Initialize the contract and set default values. * - * @param _core Core contract address. + * @param _stateRootProvider Contract address which implements + * StateRootInterface. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. */ constructor( - StateRootInterface _core, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager ) @@ -142,26 +165,26 @@ contract GatewayBase is Organized { public { require( - address(_core) != address(0), - "Core contract address must not be zero." + address(_stateRootProvider) != address(0), + "State root provider contract address must not be zero." ); - core = _core; + stateRootProvider = _stateRootProvider; bounty = _bounty; } + /* External functions */ /** - * @notice proveGateway can be called by anyone to verify merkle proof of + * @notice This can be called by anyone to verify merkle proof of * gateway/co-gateway contract address. Trust factor is brought by - * stateRoots mapping. stateRoot is committed in commitStateRoot - * function by mosaic process which is a trusted decentralized system - * running separately. It's important to note that in replay calls of - * proveGateway bytes _rlpParentNodes variable is not validated. In - * this case input storage root derived from merkle proof account - * nodes is verified with stored storage root of given blockHeight. + * state roots of the contract which implements StateRootInterface. + * It's important to note that in replay calls of proveGateway + * bytes _rlpParentNodes variable is not validated. In this case + * input storage root derived from merkle proof account nodes is + * verified with stored storage root of given blockHeight. * GatewayProven event has parameter wasAlreadyProved to * differentiate between first call and replay calls. * @@ -192,7 +215,7 @@ contract GatewayBase is Organized { "Length of RLP parent nodes is 0" ); - bytes32 stateRoot = core.getStateRoot(_blockHeight); + bytes32 stateRoot = stateRootProvider.getStateRoot(_blockHeight); // State root should be present for the block height require( diff --git a/contracts/lib/GatewayLib.sol b/contracts/lib/GatewayLib.sol index 1b54ac74..f90b537b 100644 --- a/contracts/lib/GatewayLib.sol +++ b/contracts/lib/GatewayLib.sol @@ -127,7 +127,7 @@ library GatewayLib { /** * Verify the remote OpenST contract against the committed state - * root with the state trie Merkle proof + * root with the state trie Merkle proof. */ require(MerklePatriciaProof.verify(hashedAccount, _encodedPath, _rlpParentNodes, _stateRoot), "Account proof is not verified."); diff --git a/contracts/test/TestEIP20CoGateway.sol b/contracts/test/TestEIP20CoGateway.sol index 4e0a4a75..d5695773 100644 --- a/contracts/test/TestEIP20CoGateway.sol +++ b/contracts/test/TestEIP20CoGateway.sol @@ -39,7 +39,8 @@ contract TestEIP20CoGateway is EIP20CoGateway { * @param _valueToken The value token contract address. * @param _utilityToken The utility token address that will be used for * minting the utility token. - * @param _core Core contract address. + * @param _stateRootProvider Contract address which implements + * StateRootInterface. * @param _bounty The amount that facilitator will stakes to initiate the * staking process. * @param _membersManager Address of a members manager contract. @@ -48,7 +49,7 @@ contract TestEIP20CoGateway is EIP20CoGateway { constructor( address _valueToken, address _utilityToken, - StateRootInterface _core, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager, address _gateway, @@ -57,7 +58,7 @@ contract TestEIP20CoGateway is EIP20CoGateway { EIP20CoGateway( _valueToken, _utilityToken, - _core, + _stateRootProvider, _bounty, _membersManager, _gateway, diff --git a/contracts/test/TestEIP20Gateway.sol b/contracts/test/TestEIP20Gateway.sol index bf3eca7b..78303d5a 100644 --- a/contracts/test/TestEIP20Gateway.sol +++ b/contracts/test/TestEIP20Gateway.sol @@ -36,7 +36,8 @@ contract TestEIP20Gateway is EIP20Gateway { * in auxiliary chain. * @param _baseToken The ERC20 token address that will be used for * staking bounty from the facilitators. - * @param _core Core contract address. + * @param _stateRootProvider Contract address which implements + * StateRootInterface. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. @@ -45,7 +46,7 @@ contract TestEIP20Gateway is EIP20Gateway { constructor( EIP20Interface _token, EIP20Interface _baseToken, - StateRootInterface _core, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager, address payable _burner @@ -53,7 +54,7 @@ contract TestEIP20Gateway is EIP20Gateway { EIP20Gateway( _token, _baseToken, - _core, + _stateRootProvider, _bounty, _membersManager, _burner diff --git a/contracts/test/core/MockSafeCore.sol b/contracts/test/core/MockAnchor.sol similarity index 84% rename from contracts/test/core/MockSafeCore.sol rename to contracts/test/core/MockAnchor.sol index 57282d8a..dd9dd18a 100644 --- a/contracts/test/core/MockSafeCore.sol +++ b/contracts/test/core/MockAnchor.sol @@ -1,14 +1,13 @@ pragma solidity ^0.5.0; -import "../../gateway/SafeCore.sol"; +import "../../gateway/Anchor.sol"; /** - * @title MockSafeCore contract + * @title MockAnchor contract. * - * @notice Used for test only + * @notice Used for test only. */ -contract MockSafeCore is SafeCore { - +contract MockAnchor is Anchor { /* Public functions */ @@ -16,7 +15,7 @@ contract MockSafeCore is SafeCore { * @notice Contract constructor. * * @param _remoteChainId The chain id of the chain that is tracked by this - * core. + * anchor. * @param _blockHeight Block height at which _stateRoot needs to store. * @param _stateRoot State root hash of given _blockHeight. * @param _maxStateRoots The max number of state roots to store in the @@ -30,7 +29,7 @@ contract MockSafeCore is SafeCore { uint256 _maxStateRoots, IsMemberInterface _membersManager ) - SafeCore( + Anchor( _remoteChainId, _blockHeight, _stateRoot, @@ -44,7 +43,7 @@ contract MockSafeCore is SafeCore { * @notice Get the mocked state root. * * @dev This is for testing only so the data is mocked here. Also please - * note the Core contract has defined this function as view, + * note the Anchor contract has defined this function as view, * so to keep this overridden function as view, reading a storage * variable. * diff --git a/contracts/test/gateway/MockGatewayBase.sol b/contracts/test/gateway/MockGatewayBase.sol index d27d6229..8caa73a2 100644 --- a/contracts/test/gateway/MockGatewayBase.sol +++ b/contracts/test/gateway/MockGatewayBase.sol @@ -6,9 +6,9 @@ import "../../gateway/GatewayBase.sol"; import "../../lib/IsMemberInterface.sol"; /** - * @title MockGatewayBase contract + * @title MockGatewayBase contract. * - * @notice Used for test only + * @notice Used for test only. */ contract MockGatewayBase is GatewayBase { @@ -17,19 +17,20 @@ contract MockGatewayBase is GatewayBase { /** * @notice This is used for testing. * - * @param _core Core contract address. + * @param _stateRootProvider Contract address which implements + * StateRootInterface. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. */ constructor( - StateRootInterface _core, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager ) public GatewayBase( - _core, + _stateRootProvider, _bounty, _membersManager ) @@ -42,9 +43,7 @@ contract MockGatewayBase is GatewayBase { * * @dev proveGateway can be called by anyone to verify merkle proof of * gateway/co-gateway contract address. Trust factor is brought by - * stateRoots mapping. stateRoot is committed in commitStateRoot - * function by mosaic process which is a trusted decentralized system - * running separately. It's important to note that in replay calls of + * stateRoots mapping. It's important to note that in replay calls of * proveGateway bytes _rlpParentNodes variable is not validated. In * this case input storage root derived from merkle proof account * nodes is verified with stored storage root of given blockHeight. @@ -78,7 +77,7 @@ contract MockGatewayBase is GatewayBase { "Length of RLP parent nodes must not be 0." ); - bytes32 stateRoot = core.getStateRoot(_blockHeight); + bytes32 stateRoot = stateRootProvider.getStateRoot(_blockHeight); //State root should be present for the block height require( diff --git a/test/gateway/safe_core/commit_state_root.js b/test/gateway/anchor/anchor_state_root.js similarity index 74% rename from test/gateway/safe_core/commit_state_root.js rename to test/gateway/anchor/anchor_state_root.js index 2b793b03..fceec881 100644 --- a/test/gateway/safe_core/commit_state_root.js +++ b/test/gateway/anchor/anchor_state_root.js @@ -18,7 +18,7 @@ // // ---------------------------------------------------------------------------- -const SafeCore = artifacts.require("./SafeCore.sol"); +const Anchor = artifacts.require("./Anchor.sol"); const MockMembersManager = artifacts.require('MockMembersManager.sol'); const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); @@ -28,16 +28,16 @@ const EventDecoder = require('../../test_lib/event_decoder.js'); const zeroBytes = "0x0000000000000000000000000000000000000000000000000000000000000000"; -contract('SafeCore.commitStateRoot()', function (accounts) { +contract('Anchor.anchorStateRoot()', function (accounts) { let remoteChainId, blockHeight, stateRoot, - maxNumberOfStateRoots, membersManager, - safeCore, + anchor, owner, - worker; + worker, + maxNumberOfStateRoots; beforeEach(async function () { @@ -49,7 +49,7 @@ contract('SafeCore.commitStateRoot()', function (accounts) { maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -67,55 +67,55 @@ contract('SafeCore.commitStateRoot()', function (accounts) { blockHeight = blockHeight.addn(1); await Utils.expectRevert( - safeCore.commitStateRoot( + anchor.anchorStateRoot( blockHeight, stateRoot, - { from: worker }, + {from: worker}, ), 'State root must not be zero.', ); }); - it('should fail when block height is less than the latest committed ' + + it('should fail when block height is less than the latest anchored ' + 'state root\'s block height', async () => { - blockHeight = blockHeight.subn(1); + blockHeight = blockHeight.subn(1); - await Utils.expectRevert( - safeCore.commitStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ), - 'Given block height is lower or equal to highest committed state root block height.', - ); + await Utils.expectRevert( + anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ), + 'Given block height is lower or equal to highest anchored state root block height.', + ); - }); + }); - it('should fail when block height is equal to the latest committed ' + + it('should fail when block height is equal to the latest anchored ' + 'state root\'s block height', async () => { - await Utils.expectRevert( - safeCore.commitStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ), - 'Given block height is lower or equal to highest committed state root block height.', - ); + await Utils.expectRevert( + anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ), + 'Given block height is lower or equal to highest anchored state root block height.', + ); - }); + }); it('should fail when caller is not worker address', async () => { blockHeight = blockHeight.addn(1); await Utils.expectRevert( - safeCore.commitStateRoot( + anchor.anchorStateRoot( blockHeight, stateRoot, - { from: accounts[6] }, + {from: accounts[6]}, ), 'Only whitelisted workers are allowed to call this method.', ); @@ -126,32 +126,32 @@ contract('SafeCore.commitStateRoot()', function (accounts) { blockHeight = blockHeight.addn(1); - let result = await safeCore.commitStateRoot.call( + let result = await anchor.anchorStateRoot.call( blockHeight, stateRoot, - { from: worker }, + {from: worker}, ); assert.strictEqual( result, true, - 'Return value of commitStateRoot must be true.', + 'Return value of anchorStateRoot must be true.', ); - await safeCore.commitStateRoot( + await anchor.anchorStateRoot( blockHeight, stateRoot, - { from: worker }, + {from: worker}, ); - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); assert.strictEqual( blockHeight.eq(latestBlockHeight), true, `Latest block height from the contract must be ${blockHeight}.`, ); - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, stateRoot, @@ -164,13 +164,13 @@ contract('SafeCore.commitStateRoot()', function (accounts) { blockHeight = blockHeight.addn(1); - let tx = await safeCore.commitStateRoot( + let tx = await anchor.anchorStateRoot( blockHeight, stateRoot, - { from: worker }, + {from: worker}, ); - let event = EventDecoder.getEvents(tx, safeCore); + let event = EventDecoder.getEvents(tx, anchor); assert.isDefined( event.StateRootAvailable, @@ -203,16 +203,16 @@ contract('SafeCore.commitStateRoot()', function (accounts) { let iterations = maxNumberOfStateRoots.muln(2).toNumber(); for (let i = 0; i < iterations; i++) { blockHeight = blockHeight.addn(1); - await safeCore.commitStateRoot( + await anchor.anchorStateRoot( blockHeight, stateRoot, - { from: worker }, + {from: worker}, ); // Check that the older state root has been deleted when i > max state roots. if (maxNumberOfStateRoots.ltn(i)) { let prunedBlockHeight = blockHeight.sub(maxNumberOfStateRoots); - let storedStateRoot = await safeCore.getStateRoot.call( + let storedStateRoot = await anchor.getStateRoot.call( prunedBlockHeight, ); assert.strictEqual( @@ -227,7 +227,7 @@ contract('SafeCore.commitStateRoot()', function (accounts) { * one should still be available. */ let existingBlockHeight = prunedBlockHeight.addn(1); - storedStateRoot = await safeCore.getStateRoot.call( + storedStateRoot = await anchor.getStateRoot.call( existingBlockHeight, ); assert.strictEqual( diff --git a/test/gateway/safe_core/constructor.js b/test/gateway/anchor/constructor.js similarity index 85% rename from test/gateway/safe_core/constructor.js rename to test/gateway/anchor/constructor.js index e9f7f916..d5250750 100644 --- a/test/gateway/safe_core/constructor.js +++ b/test/gateway/anchor/constructor.js @@ -18,21 +18,21 @@ // // ---------------------------------------------------------------------------- -const SafeCore = artifacts.require("./SafeCore.sol"); +const Anchor = artifacts.require("./Anchor.sol"); const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; -contract('SafeCore.constructor()', function (accounts) { +contract('Anchor.constructor()', function (accounts) { let remoteChainId, blockHeight, stateRoot, maxNumberOfStateRoots, membersManager, - safeCore; + anchor; beforeEach(async function () { @@ -49,7 +49,7 @@ contract('SafeCore.constructor()', function (accounts) { remoteChainId = new BN(0); await Utils.expectRevert( - SafeCore.new( + Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -66,7 +66,7 @@ contract('SafeCore.constructor()', function (accounts) { membersManager = NullAddress; await Utils.expectRevert( - SafeCore.new( + Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -80,7 +80,7 @@ contract('SafeCore.constructor()', function (accounts) { it('should pass with correct params', async () => { - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -88,28 +88,28 @@ contract('SafeCore.constructor()', function (accounts) { membersManager, ); - let chainId = await safeCore.getRemoteChainId.call(); + let chainId = await anchor.getRemoteChainId.call(); assert.strictEqual( remoteChainId.eq(chainId), true, `Remote chain id from the contract must be ${remoteChainId}.`, ); - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); assert.strictEqual( blockHeight.eq(latestBlockHeight), true, `Latest block height from the contract must be ${blockHeight}.`, ); - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, stateRoot, `Latest state root from the contract must be ${stateRoot}.`, ); - let membersManagerAddress = await safeCore.membersManager.call(); + let membersManagerAddress = await anchor.membersManager.call(); assert.strictEqual( membersManagerAddress, membersManager, diff --git a/test/gateway/safe_core/get_latest_state_root_block_height.js b/test/gateway/anchor/get_latest_state_root_block_height.js similarity index 80% rename from test/gateway/safe_core/get_latest_state_root_block_height.js rename to test/gateway/anchor/get_latest_state_root_block_height.js index 530a2de8..311c7394 100644 --- a/test/gateway/safe_core/get_latest_state_root_block_height.js +++ b/test/gateway/anchor/get_latest_state_root_block_height.js @@ -18,19 +18,19 @@ // // ---------------------------------------------------------------------------- -const SafeCore = artifacts.require("./SafeCore.sol"); +const Anchor = artifacts.require("./Anchor.sol"); const MockMembersManager = artifacts.require('MockMembersManager.sol'); const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); -contract('SafeCore.getLatestStateRootBlockHeight()', function (accounts) { +contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { let remoteChainId, blockHeight, stateRoot, - maxNumberOfStateRoots, membersManager, - safeCore, + maxNumberOfStateRoots, + anchor, owner, worker; @@ -44,7 +44,7 @@ contract('SafeCore.getLatestStateRootBlockHeight()', function (accounts) { maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -56,7 +56,7 @@ contract('SafeCore.getLatestStateRootBlockHeight()', function (accounts) { it('should return the state root that was set while deployment', async () => { - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); assert.strictEqual( blockHeight.eq(latestBlockHeight), true, @@ -65,17 +65,17 @@ contract('SafeCore.getLatestStateRootBlockHeight()', function (accounts) { }); - it('should return the latest committed state root block height', async () => { + it('should return the latest anchored state root block height', async () => { blockHeight = blockHeight.addn(50000); - await safeCore.commitStateRoot( + await anchor.anchorStateRoot( blockHeight, stateRoot, - { from: worker }, + {from: worker}, ); - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); assert.strictEqual( blockHeight.eq(latestBlockHeight), true, diff --git a/test/gateway/safe_core/get_remote_chainId.js b/test/gateway/anchor/get_remote_chainId.js similarity index 87% rename from test/gateway/safe_core/get_remote_chainId.js rename to test/gateway/anchor/get_remote_chainId.js index bacc542e..c31e03f5 100644 --- a/test/gateway/safe_core/get_remote_chainId.js +++ b/test/gateway/anchor/get_remote_chainId.js @@ -18,18 +18,18 @@ // // ---------------------------------------------------------------------------- -const SafeCore = artifacts.require("./SafeCore.sol"); +const Anchor = artifacts.require("./Anchor.sol"); const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); -contract('SafeCore.getRemoteChainId()', function (accounts) { +contract('Anchor.getRemoteChainId()', function (accounts) { let remoteChainId, blockHeight, stateRoot, maxNumberOfStateRoots, membersManager, - safeCore; + anchor; beforeEach(async function () { @@ -39,7 +39,7 @@ contract('SafeCore.getRemoteChainId()', function (accounts) { maxNumberOfStateRoots = new BN(10); membersManager = accounts[1]; - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -51,7 +51,7 @@ contract('SafeCore.getRemoteChainId()', function (accounts) { it('should return correct remote chain id', async () => { - let chainId = await safeCore.getRemoteChainId.call(); + let chainId = await anchor.getRemoteChainId.call(); assert.strictEqual( remoteChainId.eq(chainId), true, diff --git a/test/gateway/safe_core/get_state_root.js b/test/gateway/anchor/get_state_root.js similarity index 74% rename from test/gateway/safe_core/get_state_root.js rename to test/gateway/anchor/get_state_root.js index a4c8b1f9..9abec0c7 100644 --- a/test/gateway/safe_core/get_state_root.js +++ b/test/gateway/anchor/get_state_root.js @@ -18,7 +18,7 @@ // // ---------------------------------------------------------------------------- -const SafeCore = artifacts.require("./SafeCore.sol"); +const Anchor = artifacts.require("./Anchor.sol"); const MockMembersManager = artifacts.require('MockMembersManager.sol'); const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); @@ -26,14 +26,14 @@ const BN = require('bn.js'); const zeroBytes = "0x0000000000000000000000000000000000000000000000000000000000000000"; -contract('SafeCore.getStateRoot()', function (accounts) { +contract('Anchor.getStateRoot()', function (accounts) { let remoteChainId, blockHeight, stateRoot, maxNumberOfStateRoots, membersManager, - safeCore, + anchor, owner, worker; @@ -47,7 +47,7 @@ contract('SafeCore.getStateRoot()', function (accounts) { maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -60,20 +60,20 @@ contract('SafeCore.getStateRoot()', function (accounts) { it('should return the latest state root block height that was set ' + 'while deployment', async () => { - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + stateRoot, + `Latest state root from the contract must be ${stateRoot}.`, + ); - }); + }); - it('should return the zero bytes for non committed block heights', async () => { + it('should return the zero bytes for non anchored block heights', async () => { blockHeight = blockHeight.addn(500); - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, zeroBytes, @@ -82,18 +82,18 @@ contract('SafeCore.getStateRoot()', function (accounts) { }); - it('should return the latest committed state root', async () => { + it('should return the latest anchored state root', async () => { blockHeight = blockHeight.addn(50000); stateRoot = web3.utils.sha3("dummy_state_root_1"); - await safeCore.commitStateRoot( + await anchor.anchorStateRoot( blockHeight, stateRoot, - { from: worker }, + {from: worker}, ); - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, stateRoot, diff --git a/test/gateway/safe_core/set_co_core_address.js b/test/gateway/anchor/set_co_anchor_address.js similarity index 65% rename from test/gateway/safe_core/set_co_core_address.js rename to test/gateway/anchor/set_co_anchor_address.js index 6356ae80..da6d1106 100644 --- a/test/gateway/safe_core/set_co_core_address.js +++ b/test/gateway/anchor/set_co_anchor_address.js @@ -18,7 +18,7 @@ // // ---------------------------------------------------------------------------- -const SafeCore = artifacts.require("./SafeCore.sol"); +const Anchor = artifacts.require("./Anchor.sol"); const MockMembersManager = artifacts.require('MockMembersManager.sol'); const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); @@ -26,17 +26,17 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; -contract('SafeCore.setCoCoreAddress()', function (accounts) { +contract('Anchor.setCoAnchorAddress()', function (accounts) { let remoteChainId, blockHeight, stateRoot, maxNumberOfStateRoots, membersManager, - safeCore, + anchor, owner, worker, - coCoreAddress; + coAnchorAddress; beforeEach(async function () { @@ -47,9 +47,9 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { stateRoot = web3.utils.sha3("dummy_state_root"); maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); - coCoreAddress = accounts[6]; + coAnchorAddress = accounts[6]; - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -59,13 +59,13 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { }); - it('should fail when coCore address is zero', async () => { + it('should fail when coAnchor address is zero', async () => { - coCoreAddress = NullAddress; + coAnchorAddress = NullAddress; await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: owner }), - 'Co-Core address must not be 0.', + anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), + "Co-Anchor address must not be 0.", ); }); @@ -75,7 +75,7 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { let notOwner = accounts[7]; await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: notOwner }), + anchor.setCoAnchorAddress(coAnchorAddress, {from: notOwner}), 'Only the organization is allowed to call this method.', ); @@ -83,36 +83,36 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { it('should pass with correct params', async () => { - let result = await safeCore.setCoCoreAddress.call( - coCoreAddress, - { from: owner }, + let result = await anchor.setCoAnchorAddress.call( + coAnchorAddress, + {from: owner}, ); assert.strictEqual( result, true, - 'Return value of setCoCoreAddress must be true.', + 'Return value of setAnchorAddress must be true.', ); - await safeCore.setCoCoreAddress(coCoreAddress, { from: owner }); + await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); - let coCore = await safeCore.coCore.call(); + let coAnchor = await anchor.coAnchor.call(); assert.strictEqual( - coCore, - coCoreAddress, - `CoCore address must be equal to ${coCoreAddress}.`, + coAnchor, + coAnchorAddress, + `CoAnchor address must be equal to ${coAnchorAddress}.`, ); }); - it('should fail to set coCore address if it\'s already set', async () => { + it('should fail to set coAnchor address if it\'s already set', async () => { - await safeCore.setCoCoreAddress(coCoreAddress, { from: owner }); + await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: owner }), - 'Co-Core has already been set and cannot be updated.', + anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), + 'Co-Anchor has already been set and cannot be updated.', ); }); diff --git a/test/gateway/eip20_cogateway/constructor.js b/test/gateway/eip20_cogateway/constructor.js index c25beb4c..32664efc 100644 --- a/test/gateway/eip20_cogateway/constructor.js +++ b/test/gateway/eip20_cogateway/constructor.js @@ -23,158 +23,158 @@ const MockToken = artifacts.require("MockToken"); const MockMembersManager = artifacts.require('MockMembersManager.sol'); const Utils = require("./../../test_lib/utils"), - BN = require('bn.js'); + BN = require('bn.js'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('EIP20CoGateway.constructor() ', function (accounts) { - let valueToken, utilityToken, bountyAmount, coreAddress, owner, worker, - membersManager, coGateway, gatewayAddress = accounts[6], - burner = NullAddress; - - beforeEach(async function () { - - valueToken = await MockToken.new(); - utilityToken = await MockToken.new(); - coreAddress = accounts[1]; - bountyAmount = new BN(100); - - owner = accounts[2]; - worker = accounts[3]; - membersManager = await MockMembersManager.new(owner, worker); - }); - - it('should able to deploy contract with correct parameters.', async function () { - coGateway = await - CoGateway.new( - valueToken.address, - utilityToken.address, - coreAddress, - bountyAmount, - membersManager.address, - gatewayAddress, - burner - ); - - assert( - web3.utils.isAddress(coGateway.address), - 'Returned value is not a valid address.' - ); - }); - - it('should initialize coGateway contract with correct parameters.', async function () { - coGateway = await - CoGateway.new( - valueToken.address, - utilityToken.address, - coreAddress, - bountyAmount, - membersManager.address, - gatewayAddress, - burner - ); - - let valueTokenAddress = await coGateway.valueToken.call(); - - assert.strictEqual( - valueTokenAddress, - valueToken.address, - 'Invalid valueTokenAddress address from contract.' - ); - - let utilityTokenAddress = await coGateway.utilityToken.call(); - assert.strictEqual( - utilityTokenAddress, - utilityToken.address, - 'Invalid bounty token address from contract.' - ); - - let coreAdd = await coGateway.core.call(); - assert.strictEqual( - coreAdd, - coreAddress, - 'Invalid core address from contract.' - ); - - let bounty = await coGateway.bounty.call(); - assert( - bounty.eq(bountyAmount), - 'Invalid bounty amount from contract' - ); - - }); - - it('should not deploy contract if value token is passed as zero.', async function () { - let valueTokenAddress = NullAddress; - - await Utils.expectRevert( - CoGateway.new( - valueTokenAddress, - utilityToken.address, - coreAddress, - bountyAmount, - membersManager.address, - gatewayAddress, - burner - ), - 'Value token address must not be zero.' - ); - }); - - it('should not deploy contract if utility token is passed as zero.', async function () { - let utilityTokenAddress = NullAddress; - - await Utils.expectRevert( - CoGateway.new( - valueToken.address, - utilityTokenAddress, - coreAddress, - bountyAmount, - membersManager.address, - gatewayAddress, - burner - ), - 'Utility token address must not be zero.' - ); - }); - - it('should not deploy contract if core address is passed as zero.', async function () { - let coreAddress = NullAddress; - - await Utils.expectRevert( - CoGateway.new( - valueToken.address, - utilityToken.address, - coreAddress, - bountyAmount, - membersManager.address, - gatewayAddress, - burner - ), - 'Core contract address must not be zero.' - ); - - }); - - it('should able to deploy contract with zero bounty.', async function () { - let bountyAmount = new BN(0); - - coGateway = await - CoGateway.new( - valueToken.address, - utilityToken.address, - coreAddress, - bountyAmount, - membersManager.address, - gatewayAddress, - burner - ); - - assert( - web3.utils.isAddress(coGateway.address), - 'Returned value is not a valid address.' - ); - }); + let valueToken, utilityToken, bountyAmount, dummyStateRootProvider, + owner, worker, membersManager, coGateway, gatewayAddress = accounts[6], + burner = NullAddress; + + beforeEach(async function () { + + valueToken = await MockToken.new(); + utilityToken = await MockToken.new(); + dummyStateRootProvider = accounts[1]; + bountyAmount = new BN(100); + + owner = accounts[2]; + worker = accounts[3]; + membersManager = await MockMembersManager.new(owner, worker); + }); + + it('should able to deploy contract with correct parameters.', async function () { + coGateway = await + CoGateway.new( + valueToken.address, + utilityToken.address, + dummyStateRootProvider, + bountyAmount, + membersManager.address, + gatewayAddress, + burner + ); + + assert( + web3.utils.isAddress(coGateway.address), + 'Returned value is not a valid address.' + ); + }); + + it('should initialize coGateway contract with correct parameters.', async function () { + coGateway = await + CoGateway.new( + valueToken.address, + utilityToken.address, + dummyStateRootProvider, + bountyAmount, + membersManager.address, + gatewayAddress, + burner + ); + + let valueTokenAddress = await coGateway.valueToken.call(); + + assert.strictEqual( + valueTokenAddress, + valueToken.address, + 'Invalid valueTokenAddress address from contract.' + ); + + let utilityTokenAddress = await coGateway.utilityToken.call(); + assert.strictEqual( + utilityTokenAddress, + utilityToken.address, + 'Invalid bounty token address from contract.' + ); + + let stateRootProviderAdd = await coGateway.stateRootProvider.call(); + assert.strictEqual( + stateRootProviderAdd, + dummyStateRootProvider, + 'Invalid stateRootProvider address from contract.' + ); + + let bounty = await coGateway.bounty.call(); + assert( + bounty.eq(bountyAmount), + 'Invalid bounty amount from contract' + ); + + }); + + it('should not deploy contract if value token is passed as zero.', async function () { + let valueTokenAddress = NullAddress; + + await Utils.expectRevert( + CoGateway.new( + valueTokenAddress, + utilityToken.address, + dummyStateRootProvider, + bountyAmount, + membersManager.address, + gatewayAddress, + burner + ), + 'Value token address must not be zero.' + ); + }); + + it('should not deploy contract if utility token is passed as zero.', async function () { + let utilityTokenAddress = NullAddress; + + await Utils.expectRevert( + CoGateway.new( + valueToken.address, + utilityTokenAddress, + dummyStateRootProvider, + bountyAmount, + membersManager.address, + gatewayAddress, + burner + ), + 'Utility token address must not be zero.' + ); + }); + + it('should not deploy contract if state root provider contract address is passed as zero.', async function () { + let stateRootProviderAddress = NullAddress; + + await Utils.expectRevert( + CoGateway.new( + valueToken.address, + utilityToken.address, + stateRootProviderAddress, + bountyAmount, + membersManager.address, + gatewayAddress, + burner + ), + 'State root provider contract address must not be zero.' + ); + + }); + + it('should able to deploy contract with zero bounty.', async function () { + let bountyAmount = new BN(0); + + coGateway = await + CoGateway.new( + valueToken.address, + utilityToken.address, + dummyStateRootProvider, + bountyAmount, + membersManager.address, + gatewayAddress, + burner + ); + + assert( + web3.utils.isAddress(coGateway.address), + 'Returned value is not a valid address.' + ); + }); }); diff --git a/test/gateway/eip20_cogateway/progress_mint.js b/test/gateway/eip20_cogateway/progress_mint.js index 99cefbfc..6bac5bc1 100644 --- a/test/gateway/eip20_cogateway/progress_mint.js +++ b/test/gateway/eip20_cogateway/progress_mint.js @@ -28,7 +28,7 @@ const BN = require('bn.js'), let valueToken, burner, - mockSafeCore, + dummyStateRootProvider, membersManager, coGateway, testUtilityToken, @@ -54,10 +54,10 @@ let MessageStatusEnum = { }; async function _setup(accounts) { - + valueToken = accounts[0]; burner = accounts[10]; - mockSafeCore = accounts[11]; + dummyStateRootProvider = accounts[11]; membersManager = accounts[2]; coGateway = accounts[3]; owner = accounts[8]; @@ -72,11 +72,11 @@ async function _setup(accounts) { staker = accounts[7]; stakerBalance = new BN(1000000); rewardAmount = new BN(100); - + } contract('EIP20CoGateway.progressMint() ', function (accounts) { - + let amount = new BN(200), beneficiary = accounts[4], gasPrice, @@ -90,16 +90,16 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { testEIP20CoGateway, messageHash; helper = new EIP20CoGatewayHelper(); - + beforeEach(async function () { - + await _setup(accounts); amount = new BN(200); hashLock = hashLockObj.l; unlockSecret = hashLockObj.s; gasPrice = new BN(10); gasLimit = new BN(10); - + intentHash = await helper.hashRedeemIntent( amount, beneficiary, @@ -112,15 +112,15 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { testEIP20CoGateway = await TestEIP20CoGateway.new( valueToken, testUtilityToken.address, - mockSafeCore, + dummyStateRootProvider, bountyAmount, membersManager, coGateway, burner, ); - + await testUtilityToken.setCoGatewayAddress(testEIP20CoGateway.address); - + messageHash = await testEIP20CoGateway.setStakeMessage.call( intentHash, nonce, @@ -137,72 +137,72 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { hashLock, staker, ); - + await testEIP20CoGateway.setMints(messageHash, beneficiary, amount); - + }); - + it('should progress mint for non-zero facilitator reward', async function () { - + await testEIP20CoGateway.setInboxStatus( messageHash, MessageStatusEnum.Declared, ); - + let progressMintValues = await testEIP20CoGateway.progressMint.call( messageHash, unlockSecret, {from: facilitator}, ); - + let expectedMintedToken = new BN(100), expectedReward = new BN(100); - + assert.strictEqual( progressMintValues.beneficiary_, beneficiary, `Beneficiary address should be ${beneficiary}`, ); - + assert.strictEqual( amount.eq(progressMintValues.stakeAmount_), true, `Staked amount should be ${amount}.`, ); - + assert.strictEqual( expectedMintedToken.eq(progressMintValues.mintedAmount_), true, `Minted amount should be ${expectedMintedToken}.`, ); - + assert.strictEqual( expectedReward.eq(progressMintValues.rewardAmount_), true, `Reward to facilitator should be ${expectedReward}.`, ); - + let response = await testEIP20CoGateway.progressMint( messageHash, unlockSecret, {from: facilitator}, ); - + let facilitatorBalance = await testUtilityToken.balanceOf(facilitator); let beneficiaryBalance = await testUtilityToken.balanceOf(beneficiary); - + assert.strictEqual( facilitatorBalance.eq(expectedReward), true, `Facilitator reward should be ${expectedReward}.`, ); - + assert.strictEqual( beneficiaryBalance.eq(amount.sub(expectedReward)), true, `Beneficiary balance should be ${amount.sub(expectedReward)}.` ); - + let expectedEvent = { MintProgressed: { _messageHash: messageHash, @@ -214,22 +214,22 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { _unlockSecret: unlockSecret } }; - + assert.equal( response.receipt.status, 1, 'Receipt status is unsuccessful.', ); - + let eventData = response.logs; Utils.validateEvents(eventData, expectedEvent); - + }); - + it('should progress mint for zero facilitator reward', async function () { - + gasPrice = new BN(0); - + let messageHash = await testEIP20CoGateway.setStakeMessage.call( intentHash, nonce, @@ -238,7 +238,7 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { hashLock, staker, ); - + await testEIP20CoGateway.setStakeMessage( intentHash, nonce, @@ -252,28 +252,28 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { MessageStatusEnum.Declared, ); await testEIP20CoGateway.setMints(messageHash, beneficiary, amount); - + let response = await testEIP20CoGateway.progressMint( messageHash, unlockSecret, {from: facilitator}, ); - + let facilitatorBalance = await testUtilityToken.balanceOf(facilitator); let beneficiaryBalance = await testUtilityToken.balanceOf(beneficiary); - + assert.strictEqual( beneficiaryBalance.eq(amount), true, `Balance for beneficiary should be ${amount}`, ); - + assert.strictEqual( facilitatorBalance.eq(new BN(0)), true, 'Facilitator reward should be zero', ); - + let expectedEvent = { MintProgressed: { _messageHash: messageHash, @@ -285,27 +285,27 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { _unlockSecret: unlockSecret } }; - + assert.equal( response.receipt.status, 1, 'Receipt status is unsuccessful.', ); - + let eventData = response.logs; Utils.validateEvents(eventData, expectedEvent); - + }); - + it('should fail when messagehash is zero', async function () { - + await testEIP20CoGateway.setInboxStatus( messageHash, MessageStatusEnum.Declared, ); - + messageHash = zeroBytes; - + await Utils.expectRevert( testEIP20CoGateway.progressMint( messageHash, @@ -314,16 +314,16 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { ), 'Message hash must not be zero.', ); - + }); - + it('should fail when message status is declared revocation', async function () { - + await testEIP20CoGateway.setInboxStatus( messageHash, MessageStatusEnum.DeclaredRevocation, ); - + await Utils.expectRevert( testEIP20CoGateway.progressMint( messageHash, @@ -332,16 +332,16 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { ), 'Message on target status must be Declared.', ); - + }); - + it('should fail when message status is revoked', async function () { - + await testEIP20CoGateway.setInboxStatus( messageHash, MessageStatusEnum.Revoked, ); - + await Utils.expectRevert( testEIP20CoGateway.progressMint( messageHash, @@ -350,16 +350,16 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { ), 'Message on target status must be Declared.', ); - + }); - + it('should fail when message status is undeclared', async function () { - + await testEIP20CoGateway.setInboxStatus( messageHash, MessageStatusEnum.Undeclared, ); - + await Utils.expectRevert( testEIP20CoGateway.progressMint( messageHash, @@ -368,18 +368,18 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { ), 'Message on target status must be Declared.', ); - + }); - + it('should fail when unlock secret is invalid', async function () { - + await testEIP20CoGateway.setInboxStatus( messageHash, MessageStatusEnum.Undeclared, ); - + unlockSecret = zeroBytes; - + await Utils.expectRevert( testEIP20CoGateway.progressMint( messageHash, @@ -388,22 +388,22 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { ), 'Invalid unlock secret.', ); - + }); - + it('should fail when message status is already progressed', async function () { - + await testEIP20CoGateway.setInboxStatus( messageHash, MessageStatusEnum.Declared, ); - + await testEIP20CoGateway.progressMint( messageHash, unlockSecret, {from: facilitator}, ); - + await Utils.expectRevert( testEIP20CoGateway.progressMint( messageHash, @@ -412,8 +412,8 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { ), 'Message on target status must be Declared.', ); - + }); - + }); diff --git a/test/gateway/eip20_cogateway/redeem.js b/test/gateway/eip20_cogateway/redeem.js index 92c20182..596b24f9 100644 --- a/test/gateway/eip20_cogateway/redeem.js +++ b/test/gateway/eip20_cogateway/redeem.js @@ -19,497 +19,496 @@ // ---------------------------------------------------------------------------- const TestEIP20CoGateway = artifacts.require('TestEIP20CoGateway'), - MockSafeCore = artifacts.require('MockSafeCore'), - MessageBus = artifacts.require('MessageBus'), - UtilityToken = artifacts.require('UtilityToken'), - EIP20Token = artifacts.require('EIP20Token'), - BN = require('bn.js'), - MockToken = artifacts.require('MockToken'), - Utils = require("../../test_lib/utils"); + MessageBus = artifacts.require('MessageBus'), + UtilityToken = artifacts.require('UtilityToken'), + EIP20Token = artifacts.require('EIP20Token'), + BN = require('bn.js'), + MockToken = artifacts.require('MockToken'), + Utils = require("../../test_lib/utils"); let testEIP20CoGateway, - burner, - valueToken, - mockSafeCore, - membersManager, - gateway, - utilityToken, - bountyAmount, - owner, - redeemer, - redeemerBalance; + burner, + valueToken, + dummyStateRootProvider, + membersManager, + gateway, + utilityToken, + bountyAmount, + owner, + redeemer, + redeemerBalance; let MessageStatusEnum = { - Undeclared: 0, - Declared: 1, - Progressed: 2, - DeclaredRevocation: 3, - Revoked: 4 + Undeclared: 0, + Declared: 1, + Progressed: 2, + DeclaredRevocation: 3, + Revoked: 4 }; async function _setup(accounts) { - - valueToken = accounts[0]; - mockSafeCore = accounts[1]; - membersManager = accounts[2]; - gateway = accounts[3]; - owner = accounts[8]; - utilityToken = await MockToken.new({from: owner}); - bountyAmount = new BN(100); - redeemer = accounts[7]; - redeemerBalance = new BN(1000); - burner = accounts[10]; - - testEIP20CoGateway = await TestEIP20CoGateway.new( - valueToken, - utilityToken.address, - mockSafeCore, - bountyAmount, - membersManager, - gateway, - burner - ); - - await utilityToken.transfer(redeemer, redeemerBalance, {from: owner}); - - await utilityToken.approve( - testEIP20CoGateway.address, - redeemerBalance, - {from: redeemer}, - ); - + + valueToken = accounts[0]; + dummyStateRootProvider = accounts[1]; + membersManager = accounts[2]; + gateway = accounts[3]; + owner = accounts[8]; + utilityToken = await MockToken.new({from: owner}); + bountyAmount = new BN(100); + redeemer = accounts[7]; + redeemerBalance = new BN(1000); + burner = accounts[10]; + + testEIP20CoGateway = await TestEIP20CoGateway.new( + valueToken, + utilityToken.address, + dummyStateRootProvider, + bountyAmount, + membersManager, + gateway, + burner + ); + + await utilityToken.transfer(redeemer, redeemerBalance, {from: owner}); + + await utilityToken.approve( + testEIP20CoGateway.address, + redeemerBalance, + {from: redeemer}, + ); + } contract('EIP20CoGateway.redeem() ', function (accounts) { - - let amount, - beneficiary = accounts[4], - gasPrice = new BN(10), - gasLimit = new BN(10), - nonce = new BN(1), - hashLockObj = Utils.generateHashLock(), - hashLock = hashLockObj.l; - - beforeEach(async function () { - - await _setup(accounts); - amount = new BN(100); - - }); - - it('should fail when the bounty amount is less than expected bounty amount', async function () { - - let bounty = new BN(10); - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bounty}, - ), - 'Payable amount should be equal to the bounty amount.', - ); - }); - - it('should fail when the bounty amount is more than expected bounty amount', async function () { - - let bounty = new BN(110); - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bounty}, - ), - 'Payable amount should be equal to the bounty amount.', - ); - }); - - it('should fail when redeem amount is zero', async function () { - - amount = 0; - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ), - 'Redeem amount must not be zero.', - ); - }); - - it('should fail when redeem with same nonce is already initiated', async function () { - - await testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - amount = new BN(200); - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ), - 'Invalid nonce.', - ); - - }); - - it('should fail when previous redeem is in progress', async function () { - - await testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce.addn(1), - hashLock, - {from: redeemer, value: bountyAmount}, - ), - 'Previous process is not completed.', - ); - - }); - - it('should fail when cogateway is not approved with redeem amount', async function () { - - amount = new BN(100000); - - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ), - "Underflow when subtracting.", - ); - - }); - - it('should fail when the redeemer \'s base token balance is less than the bounty amount', async function () { - - bountyAmount = new BN(10); - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ), - "Payable amount should be equal to the bounty amount.", - ) - }); - - it('should fail when the redeemer\'s BT balance is less than the redeem amount', async function () { - - let amount = new BN(10000); - - await utilityToken.approve( - testEIP20CoGateway.address, - amount, - {from: redeemer}, - ); - - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ), - "Underflow when subtracting.", - ) - }); - - it('should fail when the message status is progressed', async function () { - - let messageHash = await testEIP20CoGateway.redeem.call( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - await testEIP20CoGateway.setOutboxStatus( - messageHash, - MessageStatusEnum.Progressed, - ); - - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount} - ), - "Message on source must be Undeclared." - ); - - }); - - it('should fail when the message status is declared revocation', async function () { - - let messageHash = await testEIP20CoGateway.redeem.call( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - await testEIP20CoGateway.setOutboxStatus( - messageHash, - MessageStatusEnum.DeclaredRevocation, - ); - - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount} - ), - "Message on source must be Undeclared." - ); - - }); - - it('should fail when the message status is declared', async function () { - - let messageHash = await testEIP20CoGateway.redeem.call( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - await testEIP20CoGateway.setOutboxStatus( - messageHash, - MessageStatusEnum.Declared, - ); - - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount} - ), - "Message on source must be Undeclared." - ); - - }); - - it('should fail when the message status is revoked', async function () { - - let messageHash = await testEIP20CoGateway.redeem.call( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - await testEIP20CoGateway.setOutboxStatus( - messageHash, - MessageStatusEnum.Revoked, - ); - - await Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount} - ), - "Message on source must be Undeclared." - ); - - }); - - it('should fail if the previous process is in revocation declared state', async function () { - - let messageHash = await testEIP20CoGateway.redeem.call( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - await testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - await testEIP20CoGateway.setOutboxStatus( - messageHash, - MessageStatusEnum.DeclaredRevocation, - ); - - Utils.expectRevert( - testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce.addn(1), - hashLock, - {from: redeemer, value: bountyAmount}, - ), - 'Previous process is not completed.' - ); - - }); - - - it('should successfully redeem', async function () { - - let messageHash = "0x193fa194eef3c001da102ee129c23b1e13a723cb9335edefe9100e85132c77d8"; - - let actualMessageHash = await testEIP20CoGateway.redeem.call( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - assert.strictEqual( - actualMessageHash, - messageHash, - "Incorrect messageHash from contract", - ); - - let response = await testEIP20CoGateway.redeem( - amount, - beneficiary, - gasPrice, - gasLimit, - nonce, - hashLock, - {from: redeemer, value: bountyAmount}, - ); - - let eip20CoGatewayBaseBalance = new BN( - await web3.eth.getBalance(testEIP20CoGateway.address), - ); - - assert.strictEqual( - bountyAmount.eq(eip20CoGatewayBaseBalance), - true, - "Bounty is not transferred to CoGateway", - ); - - let eip20CoGatewayBalance = await utilityToken.balanceOf(testEIP20CoGateway.address); - - assert.strictEqual( - eip20CoGatewayBalance.eq(amount), - true, - "EIP20CoGateway address did not receive redeemed amount", - ); - - let expectedBalance = redeemerBalance.sub(amount); - - assert.strictEqual( - (await utilityToken.balanceOf(redeemer)).eq(expectedBalance), - true, - "Redeemer EIP20 token balance should be equal to ${expectedBalance}", - ); - - let expectedEvent = { - RedeemIntentDeclared: { - _messageHash: messageHash, - _redeemer: redeemer, - _redeemerNonce: nonce, - _beneficiary: beneficiary, - _amount: amount - } - }; - - assert.equal( - response.receipt.status, - 1, - "Receipt status is unsuccessful" - ); - - let eventData = response.logs; - await Utils.validateEvents(eventData, expectedEvent); - - }); - + + let amount, + beneficiary = accounts[4], + gasPrice = new BN(10), + gasLimit = new BN(10), + nonce = new BN(1), + hashLockObj = Utils.generateHashLock(), + hashLock = hashLockObj.l; + + beforeEach(async function () { + + await _setup(accounts); + amount = new BN(100); + + }); + + it('should fail when the bounty amount is less than expected bounty amount', async function () { + + let bounty = new BN(10); + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bounty}, + ), + 'Payable amount should be equal to the bounty amount.', + ); + }); + + it('should fail when the bounty amount is more than expected bounty amount', async function () { + + let bounty = new BN(110); + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bounty}, + ), + 'Payable amount should be equal to the bounty amount.', + ); + }); + + it('should fail when redeem amount is zero', async function () { + + amount = 0; + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ), + 'Redeem amount must not be zero.', + ); + }); + + it('should fail when redeem with same nonce is already initiated', async function () { + + await testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + amount = new BN(200); + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ), + 'Invalid nonce.', + ); + + }); + + it('should fail when previous redeem is in progress', async function () { + + await testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce.addn(1), + hashLock, + {from: redeemer, value: bountyAmount}, + ), + 'Previous process is not completed.', + ); + + }); + + it('should fail when cogateway is not approved with redeem amount', async function () { + + amount = new BN(100000); + + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ), + "Underflow when subtracting.", + ); + + }); + + it('should fail when the redeemer \'s base token balance is less than the bounty amount', async function () { + + bountyAmount = new BN(10); + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ), + "Payable amount should be equal to the bounty amount.", + ) + }); + + it('should fail when the redeemer\'s BT balance is less than the redeem amount', async function () { + + let amount = new BN(10000); + + await utilityToken.approve( + testEIP20CoGateway.address, + amount, + {from: redeemer}, + ); + + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ), + "Underflow when subtracting.", + ) + }); + + it('should fail when the message status is progressed', async function () { + + let messageHash = await testEIP20CoGateway.redeem.call( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + await testEIP20CoGateway.setOutboxStatus( + messageHash, + MessageStatusEnum.Progressed, + ); + + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount} + ), + "Message on source must be Undeclared." + ); + + }); + + it('should fail when the message status is declared revocation', async function () { + + let messageHash = await testEIP20CoGateway.redeem.call( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + await testEIP20CoGateway.setOutboxStatus( + messageHash, + MessageStatusEnum.DeclaredRevocation, + ); + + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount} + ), + "Message on source must be Undeclared." + ); + + }); + + it('should fail when the message status is declared', async function () { + + let messageHash = await testEIP20CoGateway.redeem.call( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + await testEIP20CoGateway.setOutboxStatus( + messageHash, + MessageStatusEnum.Declared, + ); + + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount} + ), + "Message on source must be Undeclared." + ); + + }); + + it('should fail when the message status is revoked', async function () { + + let messageHash = await testEIP20CoGateway.redeem.call( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + await testEIP20CoGateway.setOutboxStatus( + messageHash, + MessageStatusEnum.Revoked, + ); + + await Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount} + ), + "Message on source must be Undeclared." + ); + + }); + + it('should fail if the previous process is in revocation declared state', async function () { + + let messageHash = await testEIP20CoGateway.redeem.call( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + await testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + await testEIP20CoGateway.setOutboxStatus( + messageHash, + MessageStatusEnum.DeclaredRevocation, + ); + + Utils.expectRevert( + testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce.addn(1), + hashLock, + {from: redeemer, value: bountyAmount}, + ), + 'Previous process is not completed.' + ); + + }); + + + it('should successfully redeem', async function () { + + let messageHash = "0x193fa194eef3c001da102ee129c23b1e13a723cb9335edefe9100e85132c77d8"; + + let actualMessageHash = await testEIP20CoGateway.redeem.call( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + assert.strictEqual( + actualMessageHash, + messageHash, + "Incorrect messageHash from contract", + ); + + let response = await testEIP20CoGateway.redeem( + amount, + beneficiary, + gasPrice, + gasLimit, + nonce, + hashLock, + {from: redeemer, value: bountyAmount}, + ); + + let eip20CoGatewayBaseBalance = new BN( + await web3.eth.getBalance(testEIP20CoGateway.address), + ); + + assert.strictEqual( + bountyAmount.eq(eip20CoGatewayBaseBalance), + true, + "Bounty is not transferred to CoGateway", + ); + + let eip20CoGatewayBalance = await utilityToken.balanceOf(testEIP20CoGateway.address); + + assert.strictEqual( + eip20CoGatewayBalance.eq(amount), + true, + "EIP20CoGateway address did not receive redeemed amount", + ); + + let expectedBalance = redeemerBalance.sub(amount); + + assert.strictEqual( + (await utilityToken.balanceOf(redeemer)).eq(expectedBalance), + true, + "Redeemer EIP20 token balance should be equal to ${expectedBalance}", + ); + + let expectedEvent = { + RedeemIntentDeclared: { + _messageHash: messageHash, + _redeemer: redeemer, + _redeemerNonce: nonce, + _beneficiary: beneficiary, + _amount: amount + } + }; + + assert.equal( + response.receipt.status, + 1, + "Receipt status is unsuccessful" + ); + + let eventData = response.logs; + await Utils.validateEvents(eventData, expectedEvent); + + }); + }); diff --git a/test/gateway/eip20_gateway/activate_gateway.js b/test/gateway/eip20_gateway/activate_gateway.js index 11fa7008..9af9bfda 100644 --- a/test/gateway/eip20_gateway/activate_gateway.js +++ b/test/gateway/eip20_gateway/activate_gateway.js @@ -19,7 +19,7 @@ contract('EIP20Gateway.activateGateway()', function (accounts) { let mockToken = accounts[0], baseToken = accounts[1], - coreAddress = accounts[2], + dummyStateRootProvider = accounts[2], bountyAmount = new BN(100); membersManager = await MockMembersManager.new(owner, worker); @@ -27,7 +27,7 @@ contract('EIP20Gateway.activateGateway()', function (accounts) { gateway = await Gateway.new( mockToken, baseToken, - coreAddress, + dummyStateRootProvider, bountyAmount, membersManager.address, burner diff --git a/test/gateway/eip20_gateway/constructor.js b/test/gateway/eip20_gateway/constructor.js index 47af2ba8..093606cb 100644 --- a/test/gateway/eip20_gateway/constructor.js +++ b/test/gateway/eip20_gateway/constructor.js @@ -23,179 +23,179 @@ const MockToken = artifacts.require("MockToken"); const MockMembersManager = artifacts.require('MockMembersManager.sol'); const Utils = require("./../../test_lib/utils"), - BN = require('bn.js'); + BN = require('bn.js'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('EIP20Gateway.constructor() ', function (accounts) { - let mockToken, baseToken, bountyAmount, coreAddress, membersManager, - gateway, owner, worker, burner = NullAddress; - - beforeEach(async function () { - - mockToken = await MockToken.new(); - baseToken = await MockToken.new(); - coreAddress = accounts[1]; - bountyAmount = new BN(100); - - owner = accounts[2]; - worker = accounts[3]; - membersManager = await MockMembersManager.new(owner, worker); - }); - - it('should able to deploy contract with correct parameters.', async function () { - gateway = await - Gateway.new( - mockToken.address, - baseToken.address, - coreAddress, - bountyAmount, - membersManager.address, - burner - ); - - assert( - web3.utils.isAddress(gateway.address), - "Returned value is not a valid address." - ); - }); - - it('should initialize gateway contract with correct parameters.', async function () { - gateway = await - Gateway.new( - mockToken.address, - baseToken.address, - coreAddress, - bountyAmount, - membersManager.address, - burner - ); - - let tokenAddress = await gateway.token.call(); - - assert.equal( - tokenAddress, - mockToken.address, - "Invalid valueTokenAddress address from contract." - ); - - let bountyTokenAdd = await gateway.baseToken.call(); - assert.equal( - bountyTokenAdd, - baseToken.address, - "Invalid bounty token address from contract." - ); - - let coreAdd = await gateway.core.call(); - assert.equal( - coreAdd, - coreAddress, - "Invalid core address from contract" - ); - - let bounty = await gateway.bounty.call(); - assert( - bounty.eq(bountyAmount), - "Invalid bounty amount from contract" - ); - - let isActivated = await gateway.activated.call(); - assert( - !isActivated, - "Gateway is not deactivated by default." - ); - - let storedMembersManager = await gateway.membersManager(); - assert.equal( - membersManager.address, - storedMembersManager, - "Incorrect membersManager from contract" - ); - }); - - it('should not deploy contract if token is passed as zero.', async function () { - let mockToken = NullAddress; - - await Utils.expectRevert( - Gateway.new( - mockToken, - baseToken.address, - coreAddress, - bountyAmount, - membersManager.address, - burner - ), - "Token contract address must not be zero." - ); - }); - - it('should not deploy contract if base token is passed as zero.', async function () { - let baseTokenAddress = NullAddress; - - await Utils.expectRevert( - Gateway.new( - mockToken.address, - baseTokenAddress, - coreAddress, - bountyAmount, - membersManager.address, - burner - ), - "Base token contract address for bounty must not be zero." - ); - }); - - it('should not deploy contract if core address is passed as zero.', async function () { - let coreAddress = NullAddress; - - await Utils.expectRevert( - Gateway.new( - mockToken.address, - baseToken.address, - coreAddress, - bountyAmount, - membersManager.address, - burner - ), - "Core contract address must not be zero." - ); - - }); - - it('should fail when members manager address is passed as zero', async function () { - let membersManager = NullAddress; - - await Utils.expectRevert( - Gateway.new( - mockToken.address, - baseToken.address, - coreAddress, - bountyAmount, - membersManager, - burner - ), - "MembersManager contract address must not be zero." - ); - - }); - - it('should able to deploy contract with zero bounty.', async function () { - let bountyAmount = new BN(0); - - gateway = await - Gateway.new( - mockToken.address, - baseToken.address, - coreAddress, - bountyAmount, - membersManager.address, - burner - ); - - assert( - web3.utils.isAddress(gateway.address), - "Returned value is not a valid address." - ); - }); + let mockToken, baseToken, bountyAmount, dummyRootProviderAddress, + membersManager, gateway, owner, worker, burner = NullAddress; + + beforeEach(async function () { + + mockToken = await MockToken.new(); + baseToken = await MockToken.new(); + dummyRootProviderAddress = accounts[1]; + bountyAmount = new BN(100); + + owner = accounts[2]; + worker = accounts[3]; + membersManager = await MockMembersManager.new(owner, worker); + }); + + it('should able to deploy contract with correct parameters.', async function () { + gateway = await + Gateway.new( + mockToken.address, + baseToken.address, + dummyRootProviderAddress, + bountyAmount, + membersManager.address, + burner + ); + + assert( + web3.utils.isAddress(gateway.address), + "Returned value is not a valid address." + ); + }); + + it('should initialize gateway contract with correct parameters.', async function () { + gateway = await + Gateway.new( + mockToken.address, + baseToken.address, + dummyRootProviderAddress, + bountyAmount, + membersManager.address, + burner + ); + + let tokenAddress = await gateway.token.call(); + + assert.equal( + tokenAddress, + mockToken.address, + "Invalid valueTokenAddress address from contract." + ); + + let bountyTokenAdd = await gateway.baseToken.call(); + assert.equal( + bountyTokenAdd, + baseToken.address, + "Invalid bounty token address from contract." + ); + + let stateRootProviderAdd = await gateway.stateRootProvider.call(); + assert.equal( + stateRootProviderAdd, + dummyRootProviderAddress, + "Invalid stateRootProvider address from contract" + ); + + let bounty = await gateway.bounty.call(); + assert( + bounty.eq(bountyAmount), + "Invalid bounty amount from contract" + ); + + let isActivated = await gateway.activated.call(); + assert( + !isActivated, + "Gateway is not deactivated by default." + ); + + let storedMembersManager = await gateway.membersManager(); + assert.equal( + membersManager.address, + storedMembersManager, + "Incorrect membersManager from contract" + ); + }); + + it('should not deploy contract if token is passed as zero.', async function () { + let mockToken = NullAddress; + + await Utils.expectRevert( + Gateway.new( + mockToken, + baseToken.address, + dummyRootProviderAddress, + bountyAmount, + membersManager.address, + burner + ), + "Token contract address must not be zero." + ); + }); + + it('should not deploy contract if base token is passed as zero.', async function () { + let baseTokenAddress = NullAddress; + + await Utils.expectRevert( + Gateway.new( + mockToken.address, + baseTokenAddress, + dummyRootProviderAddress, + bountyAmount, + membersManager.address, + burner + ), + "Base token contract address for bounty must not be zero." + ); + }); + + it('should not deploy contract if state root provider contract address is passed as zero.', async function () { + let stateRootProvider = NullAddress; + + await Utils.expectRevert( + Gateway.new( + mockToken.address, + baseToken.address, + stateRootProvider, + bountyAmount, + membersManager.address, + burner + ), + "State root provider contract address must not be zero." + ); + + }); + + it('should fail when members manager address is passed as zero', async function () { + let membersManager = NullAddress; + + await Utils.expectRevert( + Gateway.new( + mockToken.address, + baseToken.address, + dummyRootProviderAddress, + bountyAmount, + membersManager, + burner + ), + "MembersManager contract address must not be zero." + ); + + }); + + it('should able to deploy contract with zero bounty.', async function () { + let bountyAmount = new BN(0); + + gateway = await + Gateway.new( + mockToken.address, + baseToken.address, + dummyRootProviderAddress, + bountyAmount, + membersManager.address, + burner + ); + + assert( + web3.utils.isAddress(gateway.address), + "Returned value is not a valid address." + ); + }); }); diff --git a/test/gateway/eip20_gateway/deactivate_gateway.js b/test/gateway/eip20_gateway/deactivate_gateway.js index 27570382..ba0e5ba7 100644 --- a/test/gateway/eip20_gateway/deactivate_gateway.js +++ b/test/gateway/eip20_gateway/deactivate_gateway.js @@ -1,5 +1,5 @@ const Gateway = artifacts.require("./EIP20Gateway.sol") - , BN = require('bn.js'); + , BN = require('bn.js'); const MockMembersManager = artifacts.require('MockMembersManager.sol'); const Utils = require('../../../test/test_lib/utils'); @@ -7,70 +7,70 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('EIP20Gateway.deactivateGateway()', function (accounts) { - let gateway; - let owner = accounts[2]; - let worker = accounts[3]; - let coGateway = accounts[5]; - let membersManager; - let burner = NullAddress; + let gateway; + let owner = accounts[2]; + let worker = accounts[3]; + let coGateway = accounts[5]; + let membersManager; + let burner = NullAddress; - beforeEach(async function () { + beforeEach(async function () { - let mockToken = accounts[0], - baseToken = accounts[1], - coreAddress = accounts[2], - bountyAmount = new BN(100); + let mockToken = accounts[0], + baseToken = accounts[1], + dummyStateRootProvider = accounts[2], + bountyAmount = new BN(100); - membersManager = await MockMembersManager.new(owner, worker); + membersManager = await MockMembersManager.new(owner, worker); - gateway = await Gateway.new( - mockToken, - baseToken, - coreAddress, - bountyAmount, - membersManager.address, - burner - ); + gateway = await Gateway.new( + mockToken, + baseToken, + dummyStateRootProvider, + bountyAmount, + membersManager.address, + burner + ); - await gateway.activateGateway(coGateway, {from: owner}); + await gateway.activateGateway(coGateway, {from: owner}); - }); + }); - it('should deactivate if activated', async function () { + it('should deactivate if activated', async function () { - let isSuccess = await gateway.deactivateGateway.call({from: owner}); + let isSuccess = await gateway.deactivateGateway.call({from: owner}); - assert.strictEqual( - isSuccess, - true, - "Gateway deactivation failed, deactivateGateway returned false.", - ); + assert.strictEqual( + isSuccess, + true, + "Gateway deactivation failed, deactivateGateway returned false.", + ); - await gateway.deactivateGateway({from: owner}); - let isActivated = await gateway.activated.call(); + await gateway.deactivateGateway({from: owner}); + let isActivated = await gateway.activated.call(); - assert.strictEqual( - isActivated, - false, - 'Activation flag is true but expected as false.' - ); - }); + assert.strictEqual( + isActivated, + false, + 'Activation flag is true but expected as false.' + ); + }); - it('should not deactivate if already deactivated', async function () { + it('should not deactivate if already deactivated', async function () { - await gateway.deactivateGateway({from: owner}); - await Utils.expectRevert( - gateway.deactivateGateway.call({from: owner}), - 'Gateway is already deactivated.' - ); - }); + await gateway.deactivateGateway({from: owner}); + await Utils.expectRevert( + gateway.deactivateGateway.call({from: owner}), + 'Gateway is already deactivated.' + ); + }); - it('should deactivated by organization only', async function () { + it('should deactivated by organization only', async function () { - await Utils.expectRevert( - gateway.deactivateGateway.call({from: accounts[0]}), - 'Only the organization is allowed to call this method.' - ); - }); + await Utils.expectRevert( + gateway.deactivateGateway.call({from: accounts[0]}), + 'Only the organization is allowed to call this method.' + ); + }); }); diff --git a/test/gateway/eip20_gateway/stake.js b/test/gateway/eip20_gateway/stake.js index 476c6bf5..27d535af 100644 --- a/test/gateway/eip20_gateway/stake.js +++ b/test/gateway/eip20_gateway/stake.js @@ -20,266 +20,266 @@ // ---------------------------------------------------------------------------- const Gateway = artifacts.require("TestEIP20Gateway"), - MockToken = artifacts.require("MockToken"), - MessageBus = artifacts.require("MessageBus"), - GatewayLib = artifacts.require("GatewayLib"); + MockToken = artifacts.require("MockToken"), + MessageBus = artifacts.require("MessageBus"), + GatewayLib = artifacts.require("GatewayLib"); const utils = require("./../../test_lib/utils"), - BN = require('bn.js'), - EIP20GatewayKlass = require("./helpers/eip20_gateway"), - HelperKlass = require("./helpers/helper"); + BN = require('bn.js'), + EIP20GatewayKlass = require("./helpers/eip20_gateway"), + HelperKlass = require("./helpers/helper"); const PENALTY_PERCENT = 1.5; const NullAddress = "0x0000000000000000000000000000000000000000"; let stakeAmount, - beneficiary, - stakerAddress, - gasPrice, - gasLimit, - nonce, - hashLock, - messageHash, - bountyAmount; - bountyAmount, - burner = NullAddress; + beneficiary, + stakerAddress, + gasPrice, + gasLimit, + nonce, + hashLock, + messageHash, + bountyAmount, + burner = NullAddress; let mockToken, - baseToken, - gateway, - helper, - hashLockObj, - gatewayTest, - errorMessage; + baseToken, + gateway, + helper, + hashLockObj, + gatewayTest, + errorMessage, + dummyStateRootProviderAddress; async function _setup(accounts, gateway) { - helper = new HelperKlass(gateway); - gatewayTest = new EIP20GatewayKlass(gateway, mockToken, baseToken); - + helper = new HelperKlass(gateway); + gatewayTest = new EIP20GatewayKlass(gateway, mockToken, baseToken); - hashLockObj = utils.generateHashLock(); + hashLockObj = utils.generateHashLock(); - stakerAddress = accounts[4]; - nonce = await helper.getNonce(accounts[1]); - stakeAmount = new BN(100000000000); - beneficiary = accounts[2]; - stakerAddress = accounts[1]; - gasPrice = new BN(200); - gasLimit = new BN(900000); - hashLock = hashLockObj.l; + stakerAddress = accounts[4]; + nonce = await helper.getNonce(accounts[1]); + stakeAmount = new BN(100000000000); + beneficiary = accounts[2]; + stakerAddress = accounts[1]; + gasPrice = new BN(200); + gasLimit = new BN(900000); + hashLock = hashLockObj.l; + dummyStateRootProviderAddress = accounts[1]; - await mockToken.transfer(stakerAddress, stakeAmount, { from: accounts[0] }); - await mockToken.approve(gateway.address, stakeAmount, { from: stakerAddress }); + await mockToken.transfer(stakerAddress, stakeAmount, {from: accounts[0]}); + await mockToken.approve(gateway.address, stakeAmount, {from: stakerAddress}); - await baseToken.transfer(stakerAddress, bountyAmount, { from: accounts[0] }); - await baseToken.approve(gateway.address, bountyAmount, { from: stakerAddress }); + await baseToken.transfer(stakerAddress, bountyAmount, {from: accounts[0]}); + await baseToken.approve(gateway.address, bountyAmount, {from: stakerAddress}); - errorMessage = ""; + errorMessage = ""; } async function _prepareData() { - let typeHash = await helper.stakeTypeHash(); - - let intentHash = await helper.hashStakeIntent( - stakeAmount, - beneficiary, - stakerAddress, - nonce, - gasPrice, - gasLimit, - mockToken.address - ); + let typeHash = await helper.stakeTypeHash(); - messageHash = await utils.messageHash( - typeHash, - intentHash, - nonce, - gasPrice, - gasLimit, - stakerAddress - ); + let intentHash = await helper.hashStakeIntent( + stakeAmount, + beneficiary, + stakerAddress, + nonce, + gasPrice, + gasLimit, + mockToken.address + ); + + messageHash = await utils.messageHash( + typeHash, + intentHash, + nonce, + gasPrice, + gasLimit, + stakerAddress + ); } async function _stake(resultType) { - let params = { - amount: stakeAmount, - beneficiary: beneficiary, - staker: stakerAddress, - gasPrice: gasPrice, - gasLimit: gasLimit, - nonce: nonce, - hashLock: hashLock - }; - - let expectedResult = { - returns: { messageHash: messageHash }, - events: { - StakeIntentDeclared: { - _messageHash: messageHash, - _staker: stakerAddress, - _stakerNonce: nonce, - _beneficiary: beneficiary, - _amount: stakeAmount - } - }, - errorMessage: errorMessage - }; - - let txOption = { - from: stakerAddress - }; - - await gatewayTest.stake( - params, - resultType, - expectedResult, - txOption - ); + let params = { + amount: stakeAmount, + beneficiary: beneficiary, + staker: stakerAddress, + gasPrice: gasPrice, + gasLimit: gasLimit, + nonce: nonce, + hashLock: hashLock + }; + + let expectedResult = { + returns: {messageHash: messageHash}, + events: { + StakeIntentDeclared: { + _messageHash: messageHash, + _staker: stakerAddress, + _stakerNonce: nonce, + _beneficiary: beneficiary, + _amount: stakeAmount + } + }, + errorMessage: errorMessage + }; + + let txOption = { + from: stakerAddress + }; + + await gatewayTest.stake( + params, + resultType, + expectedResult, + txOption + ); } contract('EIP20Gateway.stake() ', function (accounts) { - beforeEach(async function () { - - mockToken = await MockToken.new(); - baseToken = await MockToken.new(); + beforeEach(async function () { - bountyAmount = new BN(100); - gateway = await Gateway.new( - mockToken.address, - baseToken.address, - accounts[1], //core address - bountyAmount, - accounts[2], // organisation address, - burner - ); - - await _setup(accounts, gateway); - }); - - it('should fail to stake when stake amount is 0', async function () { - stakeAmount = new BN(0); - errorMessage = "Stake amount must not be zero"; - await _prepareData(); - await _stake(utils.ResultType.FAIL); - }); + mockToken = await MockToken.new(); + baseToken = await MockToken.new(); + + bountyAmount = new BN(100); + gateway = await Gateway.new( + mockToken.address, + baseToken.address, + accounts[1], //Dummy state root provider address + bountyAmount, + accounts[2], // organisation address, + burner + ); + + await _setup(accounts, gateway); + }); + + it('should fail to stake when stake amount is 0', async function () { + stakeAmount = new BN(0); + errorMessage = "Stake amount must not be zero"; + await _prepareData(); + await _stake(utils.ResultType.FAIL); + }); + + it('should fail to stake when beneficiary address is 0', async function () { + beneficiary = "0x0000000000000000000000000000000000000000"; + errorMessage = "Beneficiary address must not be zero"; + await _prepareData(); + await _stake(utils.ResultType.FAIL); + }); + + + it('should fail to stake when staker has balance less than the stake amount', async function () { + stakeAmount = new BN(200000000000); + await mockToken.approve(gateway.address, stakeAmount, {from: stakerAddress}); + await _prepareData(); + errorMessage = "revert"; + await _stake(utils.ResultType.FAIL); + }); + + it('should fail to stake when stakerAddress has balance less than the bounty amount', async function () { + await baseToken.transfer(accounts[0], new BN(50), {from: stakerAddress}); + await _prepareData(); + errorMessage = "revert"; + await _stake(utils.ResultType.FAIL); + }); + + it('should fail to stake when gateway is not approved by the staker', async function () { + stakerAddress = accounts[5]; + await mockToken.transfer(stakerAddress, stakeAmount, {from: accounts[0]}); + await _prepareData(); + errorMessage = "revert"; + await _stake(utils.ResultType.FAIL); + }); + + it('should successfully stake', async function () { + await _prepareData(); + await _stake(utils.ResultType.SUCCESS); + }); + + it('should fail when its already staked with same data (replay attack)', async function () { + + await _prepareData(); + await _stake(utils.ResultType.SUCCESS); + + await mockToken.transfer(stakerAddress, stakeAmount, {from: accounts[0]}); + await baseToken.transfer(stakerAddress, bountyAmount, {from: accounts[0]}); + await mockToken.approve(gateway.address, stakeAmount, {from: stakerAddress}); + await baseToken.approve(gateway.address, bountyAmount, {from: stakerAddress}); + + errorMessage = "Invalid nonce"; + await _stake(utils.ResultType.FAIL); + }); + + it('should fail to stake when previous stake for same address is not progressed', async function () { + + await _prepareData(); + await _stake(utils.ResultType.SUCCESS); + + await mockToken.transfer(stakerAddress, stakeAmount, {from: accounts[0]}); + await baseToken.transfer(stakerAddress, bountyAmount, {from: accounts[0]}); + await mockToken.approve(gateway.address, stakeAmount, {from: stakerAddress}); + await baseToken.approve(gateway.address, bountyAmount, {from: stakerAddress}); + + nonce = new BN(2); + await _prepareData(); + errorMessage = "Previous process is not completed"; + await _stake(utils.ResultType.FAIL); + + }); + + it('should fail when previous stake for same address is in revocation', async function () { + + await _prepareData(); + await _stake(utils.ResultType.SUCCESS); + + let penalty = new BN(bountyAmount * PENALTY_PERCENT); + + // funding staker for penalty amount + await baseToken.transfer(stakerAddress, penalty, {from: accounts[0]}); + // approving gateway for penalty amount + await baseToken.approve(gateway.address, penalty, {from: stakerAddress}); + + //revertStaking + await gateway.revertStake(messageHash, {from: stakerAddress}); + + await mockToken.transfer(stakerAddress, stakeAmount, {from: accounts[0]}); + await baseToken.transfer(stakerAddress, bountyAmount, {from: accounts[0]}); + await mockToken.approve(gateway.address, stakeAmount, {from: stakerAddress}); + await baseToken.approve(gateway.address, bountyAmount, {from: stakerAddress}); + + nonce = new BN(2); + await _prepareData(); + errorMessage = "Previous process is not completed"; + await _stake(utils.ResultType.FAIL); + }); + + it('should fail stake if gateway is not activated.', async function () { + + let mockToken = await MockToken.new(); + let baseToken = await MockToken.new(); + let bountyAmount = new BN(100); + + gateway = await Gateway.new( + mockToken.address, + baseToken.address, + accounts[1], //dummy state root provider address + bountyAmount, + accounts[2], // organisation address + burner + ); - it('should fail to stake when beneficiary address is 0', async function () { - beneficiary = "0x0000000000000000000000000000000000000000"; - errorMessage = "Beneficiary address must not be zero"; - await _prepareData(); - await _stake(utils.ResultType.FAIL); - }); - - - it('should fail to stake when staker has balance less than the stake amount', async function () { - stakeAmount = new BN(200000000000); - await mockToken.approve(gateway.address, stakeAmount, { from: stakerAddress }); - await _prepareData(); - errorMessage = "revert"; - await _stake(utils.ResultType.FAIL); - }); - - it('should fail to stake when stakerAddress has balance less than the bounty amount', async function () { - await baseToken.transfer(accounts[0], new BN(50), { from: stakerAddress }); - await _prepareData(); - errorMessage = "revert"; - await _stake(utils.ResultType.FAIL); - }); - - it('should fail to stake when gateway is not approved by the staker', async function () { - stakerAddress = accounts[5]; - await mockToken.transfer(stakerAddress, stakeAmount, { from: accounts[0] }); - await _prepareData(); - errorMessage = "revert"; - await _stake(utils.ResultType.FAIL); - }); - - it('should successfully stake', async function () { - await _prepareData(); - await _stake(utils.ResultType.SUCCESS); - }); - - it('should fail when its already staked with same data (replay attack)', async function () { - - await _prepareData(); - await _stake(utils.ResultType.SUCCESS); - - await mockToken.transfer(stakerAddress, stakeAmount, { from: accounts[0] }); - await baseToken.transfer(stakerAddress, bountyAmount, { from: accounts[0] }); - await mockToken.approve(gateway.address, stakeAmount, { from: stakerAddress }); - await baseToken.approve(gateway.address, bountyAmount, { from: stakerAddress }); - - errorMessage = "Invalid nonce"; - await _stake(utils.ResultType.FAIL); - }); - - it('should fail to stake when previous stake for same address is not progressed', async function () { - - await _prepareData(); - await _stake(utils.ResultType.SUCCESS); - - await mockToken.transfer(stakerAddress, stakeAmount, { from: accounts[0] }); - await baseToken.transfer(stakerAddress, bountyAmount, { from: accounts[0] }); - await mockToken.approve(gateway.address, stakeAmount, { from: stakerAddress }); - await baseToken.approve(gateway.address, bountyAmount, { from: stakerAddress }); - - nonce = new BN(2); - await _prepareData(); - errorMessage = "Previous process is not completed"; - await _stake(utils.ResultType.FAIL); - - }); - - it('should fail when previous stake for same address is in revocation', async function () { - - await _prepareData(); - await _stake(utils.ResultType.SUCCESS); - - let penalty = new BN(bountyAmount * PENALTY_PERCENT); - - // funding staker for penalty amount - await baseToken.transfer(stakerAddress, penalty, { from: accounts[0] }); - // approving gateway for penalty amount - await baseToken.approve(gateway.address, penalty, { from: stakerAddress }); - - //revertStaking - await gateway.revertStake(messageHash, { from: stakerAddress }); - - await mockToken.transfer(stakerAddress, stakeAmount, { from: accounts[0] }); - await baseToken.transfer(stakerAddress, bountyAmount, { from: accounts[0] }); - await mockToken.approve(gateway.address, stakeAmount, { from: stakerAddress }); - await baseToken.approve(gateway.address, bountyAmount, { from: stakerAddress }); - - nonce = new BN(2); - await _prepareData(); - errorMessage = "Previous process is not completed"; - await _stake(utils.ResultType.FAIL); - }); - - it('should fail stake if gateway is not activated.', async function () { - - let mockToken = await MockToken.new(); - let baseToken = await MockToken.new(); - let bountyAmount = new BN(100); - - gateway = await Gateway.new( - mockToken.address, - baseToken.address, - accounts[1], //core address - bountyAmount, - accounts[2], // organisation address - burner - ); - - await _setup(accounts, gateway); - await _prepareData(); - await _stake(utils.ResultType.FAIL); - }); + await _setup(accounts, gateway); + await _prepareData(); + await _stake(utils.ResultType.FAIL); + }); }); diff --git a/test/gateway/gateway_base/bounty_change.js b/test/gateway/gateway_base/bounty_change.js index ddd70328..3159dff5 100644 --- a/test/gateway/gateway_base/bounty_change.js +++ b/test/gateway/gateway_base/bounty_change.js @@ -45,13 +45,13 @@ contract('GatewayBase.sol', function (accounts) { beforeEach(async function () { - let core = accounts[0] + let dummyStateRootProviderAddress = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - core, + dummyStateRootProviderAddress, bounty, membersManager.address ); @@ -103,13 +103,13 @@ contract('GatewayBase.sol', function (accounts) { beforeEach(async function () { - let core = accounts[0] + let dummyStateRootProviderAddress = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - core, + dummyStateRootProviderAddress, bounty, membersManager.address ); diff --git a/test/gateway/gateway_base/construction.js b/test/gateway/gateway_base/construction.js index ec817122..15621fc5 100644 --- a/test/gateway/gateway_base/construction.js +++ b/test/gateway/gateway_base/construction.js @@ -9,13 +9,13 @@ contract('GatewayBase.sol', function (accounts) { describe('Construction', async () => { - let core, bounty, worker, membersManager; + let dummyStateRootProvider, bounty, worker, membersManager; beforeEach(async function () { owner = accounts[2] , worker = accounts[3] - , core = accounts[0] + , dummyStateRootProvider = accounts[0] , bounty = new BN(100); membersManager = await MockMembersManager.new(owner, worker); @@ -23,15 +23,15 @@ contract('GatewayBase.sol', function (accounts) { it('should pass with right set of parameters', async function () { gatewayBaseInstance = await GatewayBase.new( - core, + dummyStateRootProvider, bounty, membersManager.address ); assert.strictEqual( - core, - await gatewayBaseInstance.core.call(), - "Core contract address doesn't match." + dummyStateRootProvider, + await gatewayBaseInstance.stateRootProvider.call(), + "State root provider contract address doesn't match." ); assert((await gatewayBaseInstance.bounty.call()).eq(bounty)); }); @@ -41,21 +41,21 @@ contract('GatewayBase.sol', function (accounts) { bounty = new BN(0); gatewayBaseInstance = await GatewayBase.new( - core, + dummyStateRootProvider, bounty, membersManager.address ); - assert.equal(core, await gatewayBaseInstance.core.call()); + assert.equal(dummyStateRootProvider, await gatewayBaseInstance.stateRootProvider.call()); assert((await gatewayBaseInstance.bounty.call()).eq(bounty)); }); - it('should fail if core address is not passed', async function () { + it('should fail if state root provider contract address is zero', async function () { - core = NullAddress; + let stateRootProvider = NullAddress; await Utils.expectRevert( - GatewayBase.new(core, bounty, membersManager.address), - "Core contract address must not be zero." + GatewayBase.new(stateRootProvider, bounty, membersManager.address), + "State root provider contract address must not be zero." ); }); @@ -63,7 +63,7 @@ contract('GatewayBase.sol', function (accounts) { it('should fail if worker manager address is not passed', async function () { await Utils.expectRevert( - GatewayBase.new(core, bounty, NullAddress), + GatewayBase.new(dummyStateRootProvider, bounty, NullAddress), "MembersManager contract address must not be zero." ); diff --git a/test/gateway/gateway_base/get_nonce.js b/test/gateway/gateway_base/get_nonce.js index 11f6ca41..dc038f13 100644 --- a/test/gateway/gateway_base/get_nonce.js +++ b/test/gateway/gateway_base/get_nonce.js @@ -13,13 +13,13 @@ contract('GatewayBase.sol', function (accounts) { let owner = accounts[2] , worker = accounts[3] - , core = accounts[0] + , dummyStateRootProviderAddress = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - core, + dummyStateRootProviderAddress, bounty, membersManager.address ); diff --git a/test/gateway/gateway_base/prove_gateway.js b/test/gateway/gateway_base/prove_gateway.js index 69bac3bd..a5de0938 100644 --- a/test/gateway/gateway_base/prove_gateway.js +++ b/test/gateway/gateway_base/prove_gateway.js @@ -19,7 +19,7 @@ // ---------------------------------------------------------------------------- const GatewayBase = artifacts.require("./MockGatewayBase.sol") - , Core = artifacts.require("./MockSafeCore.sol") + , MockAnchor = artifacts.require("./MockAnchor.sol") , BN = require('bn.js'); const MockMembersManager = artifacts.require('MockMembersManager.sol'); @@ -44,7 +44,7 @@ contract('GatewayBase.sol', function (accounts) { let maxStateRoots = new BN(1000); let membersManager = await MockMembersManager.new(owner, worker); - let core = await Core.new( + let mockAnchor = await MockAnchor.new( 1, 0, stateRoot, @@ -53,7 +53,7 @@ contract('GatewayBase.sol', function (accounts) { ); gatewayBaseInstance = await GatewayBase.new( - core.address, + mockAnchor.address, bounty, membersManager.address ); diff --git a/test/gateway/helpers/helper.js b/test/gateway/helpers/helper.js index 4802f57e..9c522f89 100644 --- a/test/gateway/helpers/helper.js +++ b/test/gateway/helpers/helper.js @@ -19,7 +19,7 @@ GatewayHelper.prototype = { let valueTokenAddress = params.token, bountyToken = params.bountyToken, - coreAddress = params.core, + stateRootProviderAddress = params.stateRootProviderAddress, bountyAmount = params.bounty, organizationAddress = params.organization; @@ -27,7 +27,7 @@ GatewayHelper.prototype = { await utils.expectThrow(Gateway.new( valueTokenAddress, bountyToken, - coreAddress, + stateRootProviderAddress, bountyAmount, organizationAddress )); @@ -35,7 +35,7 @@ GatewayHelper.prototype = { this.gateway = await Gateway.new( valueTokenAddress, bountyToken, - coreAddress, + stateRootProviderAddress, bountyAmount, organizationAddress ); @@ -64,11 +64,11 @@ GatewayHelper.prototype = { "Invalid bounty token address from contract" ); - let coreAdd = await this.gateway.core.call(); + let stateRootProviderAdd = await this.gateway.stateRootProvider.call(); assert.equal( - coreAdd, - coreAddress, - "Invalid core address from contract" + stateRootProviderAdd, + stateRootProviderAddress, + "Invalid state root provider address from contract" ); let bounty = await this.gateway.bounty.call();