From 24119cdde793da5bff1281a0721384be84838d5d Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Fri, 14 Dec 2018 18:58:34 +0530 Subject: [PATCH 01/15] Renamed SafeCore contract and references of safecore to anchor --- .../gateway/{SafeCore.sol => Anchor.sol} | 28 +++++++------- .../core/{MockSafeCore.sol => MockAnchor.sol} | 17 ++++----- contracts/test/gateway/MockGatewayBase.sol | 2 +- .../anchor_state_root.js} | 38 +++++++++---------- .../{safe_core => anchor}/constructor.js | 0 .../get_latest_state_root_block_height.js | 16 ++++---- .../get_remote_chainId.js | 10 ++--- .../{safe_core => anchor}/get_state_root.js | 16 ++++---- .../set_co_core_address.js | 24 ++++++------ test/gateway/eip20_cogateway/redeem.js | 8 ++-- test/gateway/gateway_base/prove_gateway.js | 2 +- 11 files changed, 81 insertions(+), 80 deletions(-) rename contracts/gateway/{SafeCore.sol => Anchor.sol} (87%) rename contracts/test/core/{MockSafeCore.sol => MockAnchor.sol} (78%) rename test/gateway/{safe_core/commit_state_root.js => anchor/anchor_state_root.js} (79%) rename test/gateway/{safe_core => anchor}/constructor.js (100%) rename test/gateway/{safe_core => anchor}/get_latest_state_root_block_height.js (84%) rename test/gateway/{safe_core => anchor}/get_remote_chainId.js (82%) rename test/gateway/{safe_core => anchor}/get_state_root.js (85%) rename test/gateway/{safe_core => anchor}/set_co_core_address.js (80%) diff --git a/contracts/gateway/SafeCore.sol b/contracts/gateway/Anchor.sol similarity index 87% rename from contracts/gateway/SafeCore.sol rename to contracts/gateway/Anchor.sol index 5d7b8301..d5244704 100644 --- a/contracts/gateway/SafeCore.sol +++ b/contracts/gateway/Anchor.sol @@ -1,6 +1,6 @@ pragma solidity ^0.5.0; -// Copyright 2017 OpenST Ltd. +// 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. @@ -20,7 +20,6 @@ pragma solidity ^0.5.0; // // ---------------------------------------------------------------------------- - import "./WorkersInterface.sol"; import "../StateRootInterface.sol"; import "../lib/IsMemberInterface.sol"; @@ -30,15 +29,18 @@ 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 + * @notice Anchor 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. */ -contract SafeCore is StateRootInterface, Organized { +contract Anchor is StateRootInterface, Organized { + + /** Usings */ + using SafeMath for uint256; @@ -58,7 +60,7 @@ contract SafeCore is StateRootInterface, Organized { */ uint256 private remoteChainId; - /** Latest block height of block for which state root was committed. */ + /** Latest block height of block for which state root was anchored. */ uint256 private latestStateRootBlockHeight; /** Address of the core on the auxiliary chain. Can be zero. */ @@ -143,9 +145,9 @@ contract SafeCore is StateRootInterface, Organized { } /** - * @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 @@ -156,10 +158,10 @@ contract SafeCore is StateRootInterface, Organized { } /** - * @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. @@ -167,7 +169,7 @@ contract SafeCore is StateRootInterface, Organized { * * @return bytes32 stateRoot */ - function commitStateRoot( + function anchorStateRoot( uint256 _blockHeight, bytes32 _stateRoot ) @@ -184,7 +186,7 @@ contract SafeCore is StateRootInterface, Organized { // Input block height should be valid require( _blockHeight > latestStateRootBlockHeight, - "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; diff --git a/contracts/test/core/MockSafeCore.sol b/contracts/test/core/MockAnchor.sol similarity index 78% rename from contracts/test/core/MockSafeCore.sol rename to contracts/test/core/MockAnchor.sol index 3b58d3e7..33f502bd 100644 --- a/contracts/test/core/MockSafeCore.sol +++ b/contracts/test/core/MockAnchor.sol @@ -1,24 +1,23 @@ 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 */ /** - * @notice Contract constructor + * @notice Contract constructor. * * @param _remoteChainId _remoteChainId is the chain id of the chain that * this core tracks. - * @param _blockHeight block height at which _stateRoot needs to store. - * @param _stateRoot state root hash of given _blockHeight. + * @param _blockHeight Block height at which _stateRoot needs to store. + * @param _stateRoot State root hash of given _blockHeight. * @param _membersManager Address of a members manager contract. */ constructor( @@ -27,7 +26,7 @@ contract MockSafeCore is SafeCore { bytes32 _stateRoot, IsMemberInterface _membersManager ) - SafeCore( + Anchor( _remoteChainId, _blockHeight, _stateRoot, diff --git a/contracts/test/gateway/MockGatewayBase.sol b/contracts/test/gateway/MockGatewayBase.sol index d27d6229..35f240c2 100644 --- a/contracts/test/gateway/MockGatewayBase.sol +++ b/contracts/test/gateway/MockGatewayBase.sol @@ -42,7 +42,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 + * stateRoots mapping. stateRoot is committed in anchorStateRoot * 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 diff --git a/test/gateway/safe_core/commit_state_root.js b/test/gateway/anchor/anchor_state_root.js similarity index 79% rename from test/gateway/safe_core/commit_state_root.js rename to test/gateway/anchor/anchor_state_root.js index b1337b57..5584ca93 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,13 +28,13 @@ 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, membersManager, - safeCore, + anchor, owner, worker; @@ -47,7 +47,7 @@ contract('SafeCore.commitStateRoot()', function (accounts) { stateRoot = web3.utils.sha3("dummy_state_root"); membersManager = await MockMembersManager.new(owner, worker); - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -64,7 +64,7 @@ contract('SafeCore.commitStateRoot()', function (accounts) { blockHeight = blockHeight.addn(1); await Utils.expectRevert( - safeCore.commitStateRoot( + anchor.anchorStateRoot( blockHeight, stateRoot, { from: worker }, @@ -74,32 +74,32 @@ contract('SafeCore.commitStateRoot()', function (accounts) { }); - 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); await Utils.expectRevert( - safeCore.commitStateRoot( + anchor.anchorStateRoot( blockHeight, stateRoot, { from: worker }, ), - '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.', ); }); - 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( + anchor.anchorStateRoot( blockHeight, stateRoot, { from: worker }, ), - '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.', ); }); @@ -109,7 +109,7 @@ contract('SafeCore.commitStateRoot()', function (accounts) { blockHeight = blockHeight.addn(1); await Utils.expectRevert( - safeCore.commitStateRoot( + anchor.anchorStateRoot( blockHeight, stateRoot, { from: accounts[6] }, @@ -123,7 +123,7 @@ 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 }, @@ -132,23 +132,23 @@ contract('SafeCore.commitStateRoot()', function (accounts) { 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 }, ); - 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, @@ -161,13 +161,13 @@ contract('SafeCore.commitStateRoot()', function (accounts) { blockHeight = blockHeight.addn(1); - let tx = await safeCore.commitStateRoot( + let tx = await anchor.anchorStateRoot( blockHeight, stateRoot, { from: worker }, ); - let event = EventDecoder.getEvents(tx, safeCore); + let event = EventDecoder.getEvents(tx, anchor); assert.isDefined( event.StateRootAvailable, diff --git a/test/gateway/safe_core/constructor.js b/test/gateway/anchor/constructor.js similarity index 100% rename from test/gateway/safe_core/constructor.js rename to test/gateway/anchor/constructor.js 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 84% 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 7b3b6276..0f8a3fca 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,18 +18,18 @@ // // ---------------------------------------------------------------------------- -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, membersManager, - safeCore, + anchor, owner, worker; @@ -41,8 +41,8 @@ contract('SafeCore.getLatestStateRootBlockHeight()', function (accounts) { blockHeight = new BN(5); stateRoot = web3.utils.sha3("dummy_state_root"); membersManager = await MockMembersManager.new(owner, worker); - - safeCore = await SafeCore.new( + + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -53,7 +53,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, @@ -66,13 +66,13 @@ contract('SafeCore.getLatestStateRootBlockHeight()', function (accounts) { blockHeight = blockHeight.addn(50000); - await safeCore.commitStateRoot( + await anchor.anchorStateRoot( blockHeight, stateRoot, { 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 82% rename from test/gateway/safe_core/get_remote_chainId.js rename to test/gateway/anchor/get_remote_chainId.js index 55992601..61954672 100644 --- a/test/gateway/safe_core/get_remote_chainId.js +++ b/test/gateway/anchor/get_remote_chainId.js @@ -18,13 +18,13 @@ // // ---------------------------------------------------------------------------- -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, membersManager, safeCore; + let remoteChainId, blockHeight, stateRoot, membersManager, anchor; beforeEach(async function () { @@ -33,7 +33,7 @@ contract('SafeCore.getRemoteChainId()', function (accounts) { stateRoot = web3.utils.sha3("dummy_state_root"); membersManager = accounts[1]; - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -44,7 +44,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 85% rename from test/gateway/safe_core/get_state_root.js rename to test/gateway/anchor/get_state_root.js index 0e599676..fd0c36f9 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,13 +26,13 @@ const BN = require('bn.js'); const zeroBytes = "0x0000000000000000000000000000000000000000000000000000000000000000"; -contract('SafeCore.getStateRoot()', function (accounts) { +contract('Anchor.getStateRoot()', function (accounts) { let remoteChainId, blockHeight, stateRoot, membersManager, - safeCore, + anchor, owner, worker; @@ -45,7 +45,7 @@ contract('SafeCore.getStateRoot()', function (accounts) { stateRoot = web3.utils.sha3("dummy_state_root"); membersManager = await MockMembersManager.new(owner, worker); - safeCore = await SafeCore.new( + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -57,7 +57,7 @@ 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); + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, stateRoot, @@ -70,7 +70,7 @@ contract('SafeCore.getStateRoot()', function (accounts) { blockHeight = blockHeight.addn(500); - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, zeroBytes, @@ -84,13 +84,13 @@ contract('SafeCore.getStateRoot()', function (accounts) { blockHeight = blockHeight.addn(50000); stateRoot = web3.utils.sha3("dummy_state_root_1"); - await safeCore.commitStateRoot( + await anchor.anchorStateRoot( blockHeight, stateRoot, { 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_core_address.js similarity index 80% rename from test/gateway/safe_core/set_co_core_address.js rename to test/gateway/anchor/set_co_core_address.js index 3b0df8ee..2a714666 100644 --- a/test/gateway/safe_core/set_co_core_address.js +++ b/test/gateway/anchor/set_co_core_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,13 +26,13 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; -contract('SafeCore.setCoCoreAddress()', function (accounts) { +contract('Anchor.setCoCoreAddress()', function (accounts) { let remoteChainId, blockHeight, stateRoot, membersManager, - safeCore, + anchor, owner, worker, coCoreAddress; @@ -46,8 +46,8 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { stateRoot = web3.utils.sha3("dummy_state_root"); membersManager = await MockMembersManager.new(owner, worker); coCoreAddress = accounts[6]; - - safeCore = await SafeCore.new( + + anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, @@ -61,7 +61,7 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { coCoreAddress = NullAddress; await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: owner }), + anchor.setCoCoreAddress(coCoreAddress, { from: owner }), 'Co-Core address must not be 0.', ); @@ -72,7 +72,7 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { let notOwner = accounts[7]; await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: notOwner }), + anchor.setCoCoreAddress(coCoreAddress, { from: notOwner }), 'Only the organization is allowed to call this method.', ); @@ -80,7 +80,7 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { it('should pass with correct params', async () => { - let result = await safeCore.setCoCoreAddress.call( + let result = await anchor.setCoCoreAddress.call( coCoreAddress, { from: owner }, ); @@ -91,9 +91,9 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { 'Return value of setCoCoreAddress must be true.', ); - await safeCore.setCoCoreAddress(coCoreAddress, { from: owner }); + await anchor.setCoCoreAddress(coCoreAddress, { from: owner }); - let coCore = await safeCore.coCore.call(); + let coCore = await anchor.coCore.call(); assert.strictEqual( coCore, @@ -105,10 +105,10 @@ contract('SafeCore.setCoCoreAddress()', function (accounts) { it('should fail to set coCore address if it\'s already set', async () => { - await safeCore.setCoCoreAddress(coCoreAddress, { from: owner }); + await anchor.setCoCoreAddress(coCoreAddress, { from: owner }); await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: owner }), + anchor.setCoCoreAddress(coCoreAddress, { from: owner }), 'Co-Core has already been set and cannot be updated.', ); diff --git a/test/gateway/eip20_cogateway/redeem.js b/test/gateway/eip20_cogateway/redeem.js index a6c04031..36028e7a 100644 --- a/test/gateway/eip20_cogateway/redeem.js +++ b/test/gateway/eip20_cogateway/redeem.js @@ -19,7 +19,7 @@ // ---------------------------------------------------------------------------- const TestEIP20CoGateway = artifacts.require('TestEIP20CoGateway'), - MockSafeCore = artifacts.require('MockSafeCore'), + MockAnchor = artifacts.require('MockAnchor'), MessageBus = artifacts.require('MessageBus'), UtilityToken = artifacts.require('UtilityToken'), EIP20Token = artifacts.require('EIP20Token'), @@ -30,7 +30,7 @@ const TestEIP20CoGateway = artifacts.require('TestEIP20CoGateway'), let testEIP20CoGateway, burner, valueToken, - mockSafeCore, + mockAnchor, membersManager, gateway, utilityToken, @@ -50,7 +50,7 @@ let MessageStatusEnum = { async function _setup(accounts) { valueToken = accounts[0]; - mockSafeCore = accounts[1]; + mockAnchor = accounts[1]; membersManager = accounts[2]; gateway = accounts[3]; owner = accounts[8]; @@ -63,7 +63,7 @@ async function _setup(accounts) { testEIP20CoGateway = await TestEIP20CoGateway.new( valueToken, utilityToken.address, - mockSafeCore, + mockAnchor, bountyAmount, membersManager, gateway, diff --git a/test/gateway/gateway_base/prove_gateway.js b/test/gateway/gateway_base/prove_gateway.js index 0e322bd7..d37f8d5d 100644 --- a/test/gateway/gateway_base/prove_gateway.js +++ b/test/gateway/gateway_base/prove_gateway.js @@ -1,5 +1,5 @@ const GatewayBase = artifacts.require("./MockGatewayBase.sol") - , Core = artifacts.require("./MockSafeCore.sol") + , Core = artifacts.require("./MockAnchor.sol") , BN = require('bn.js'); const MockMembersManager = artifacts.require('MockMembersManager.sol'); From 71a8f810e1f943303741645cd1fb5e2cf0e21073 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Fri, 14 Dec 2018 19:04:37 +0530 Subject: [PATCH 02/15] Modified the documentation --- contracts/gateway/Anchor.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/gateway/Anchor.sol b/contracts/gateway/Anchor.sol index d5244704..ae50aaef 100644 --- a/contracts/gateway/Anchor.sol +++ b/contracts/gateway/Anchor.sol @@ -32,7 +32,7 @@ import "../lib/SafeMath.sol"; * @title Anchor contract which implements StateRootInterface. * * @notice Anchor 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 + * the co-core, which will be the anchor 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. From 4116f548e29aeaaf480ed8dd1f7a59a8d180a8e7 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Sun, 16 Dec 2018 00:05:57 +0530 Subject: [PATCH 03/15] Changed the name of the setCoCoreAddress method to setCoAnchorAddress,modified unit test cases and removed reference of safe core --- contracts/gateway/Anchor.sol | 40 ++- test/gateway/anchor/anchor_state_root.js | 320 +++++++++--------- test/gateway/anchor/constructor.js | 170 +++++----- .../get_latest_state_root_block_height.js | 112 +++--- test/gateway/anchor/get_remote_chainId.js | 60 ++-- test/gateway/anchor/get_state_root.js | 146 ++++---- test/gateway/anchor/set_co_anchor_address.js | 117 +++++++ test/gateway/anchor/set_co_core_address.js | 117 ------- 8 files changed, 540 insertions(+), 542 deletions(-) create mode 100644 test/gateway/anchor/set_co_anchor_address.js delete mode 100644 test/gateway/anchor/set_co_core_address.js diff --git a/contracts/gateway/Anchor.sol b/contracts/gateway/Anchor.sol index ae50aaef..b45e723d 100644 --- a/contracts/gateway/Anchor.sol +++ b/contracts/gateway/Anchor.sol @@ -32,10 +32,10 @@ import "../lib/SafeMath.sol"; * @title Anchor contract which implements StateRootInterface. * * @notice Anchor stores another chain's state roots. It stores the address of - * the co-core, which will be the anchor 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. + * 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 Anchor is StateRootInterface, Organized { @@ -55,7 +55,7 @@ contract Anchor is StateRootInterface, Organized { 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; @@ -63,8 +63,8 @@ contract Anchor is StateRootInterface, Organized { /** Latest block height of block for which state root was anchored. */ uint256 private latestStateRootBlockHeight; - /** 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 */ @@ -73,7 +73,7 @@ contract Anchor is StateRootInterface, Organized { * @notice Contract constructor. * * @param _remoteChainId _remoteChainId is the chain id of the chain that - * this core tracks. + * this anchor tracks. * @param _blockHeight Block height at which _stateRoot needs to store. * @param _stateRoot State root hash of given _blockHeight. * @param _membersManager Address of a members manager contract. @@ -102,27 +102,28 @@ contract Anchor is StateRootInterface, Organized { /* 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; } @@ -158,10 +159,7 @@ contract Anchor is StateRootInterface, Organized { } /** - * @notice External function anchorStateRoot. - * - * @dev anchorStateRoot Called from game process. - * Anchor new state root for a block height. + * @notice Anchor new state root for a block height. * * @param _blockHeight Block height for which stateRoots mapping needs to * update. @@ -198,7 +196,7 @@ contract Anchor is StateRootInterface, Organized { } /** - * @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/test/gateway/anchor/anchor_state_root.js b/test/gateway/anchor/anchor_state_root.js index 5584ca93..4902a6d2 100644 --- a/test/gateway/anchor/anchor_state_root.js +++ b/test/gateway/anchor/anchor_state_root.js @@ -26,168 +26,168 @@ const Utils = require('../../../test/test_lib/utils'); const EventDecoder = require('../../test_lib/event_decoder.js'); const zeroBytes = - "0x0000000000000000000000000000000000000000000000000000000000000000"; + "0x0000000000000000000000000000000000000000000000000000000000000000"; contract('Anchor.anchorStateRoot()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - membersManager, - anchor, - owner, - worker; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - membersManager = await MockMembersManager.new(owner, worker); - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - membersManager.address, - ); - - stateRoot = web3.utils.sha3("dummy_state_root_1"); - - }); - - it('should fail when state root is zero', async () => { - - stateRoot = zeroBytes; - blockHeight = blockHeight.addn(1); - - await Utils.expectRevert( - anchor.anchorStateRoot( + + let remoteChainId, blockHeight, stateRoot, - { from: worker }, - ), - 'State root must not be zero.', - ); - - }); - - it('should fail when block height is less than the latest anchored ' + - 'state root\'s block height', async () => { - - blockHeight = blockHeight.subn(1); - - 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 anchored ' + - 'state root\'s block height', async () => { - - 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( - anchor.anchorStateRoot( - blockHeight, - stateRoot, - { from: accounts[6] }, - ), - 'Only whitelisted workers are allowed to call this method.', - ); - - }); - - it('should pass with correct params', async () => { - - blockHeight = blockHeight.addn(1); - - let result = await anchor.anchorStateRoot.call( - blockHeight, - stateRoot, - { from: worker }, - ); - - assert.strictEqual( - result, - true, - 'Return value of anchorStateRoot must be true.', - ); - - await anchor.anchorStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ); - - let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - let latestStateRoot = await anchor.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - }); - - it('should emit `StateRootAvailable` event', async () => { - - blockHeight = blockHeight.addn(1); - - let tx = await anchor.anchorStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ); - - let event = EventDecoder.getEvents(tx, anchor); - - assert.isDefined( - event.StateRootAvailable, - 'Event `StateRootAvailable` must be emitted.', - ); - - let eventData = event.StateRootAvailable; - - assert.strictEqual( - eventData._stateRoot, - stateRoot, - `The _stateRoot value in the event should be equal to ${stateRoot}` - ); - - assert.strictEqual( - blockHeight.eq(eventData._blockHeight), - true, - `The _blockHeight in the event should be equal to ${blockHeight}` - ); - - }); - + membersManager, + anchor, + owner, + worker; + + beforeEach(async function () { + + owner = accounts[2]; + worker = accounts[3]; + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + membersManager = await MockMembersManager.new(owner, worker); + + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + membersManager.address, + ); + + stateRoot = web3.utils.sha3("dummy_state_root_1"); + + }); + + it('should fail when state root is zero', async () => { + + stateRoot = zeroBytes; + blockHeight = blockHeight.addn(1); + + await Utils.expectRevert( + anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ), + 'State root must not be zero.', + ); + + }); + + it('should fail when block height is less than the latest anchored ' + + 'state root\'s block height', async () => { + + blockHeight = blockHeight.subn(1); + + 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 anchored ' + + 'state root\'s block height', async () => { + + 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( + anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: accounts[6]}, + ), + 'Only whitelisted workers are allowed to call this method.', + ); + + }); + + it('should pass with correct params', async () => { + + blockHeight = blockHeight.addn(1); + + let result = await anchor.anchorStateRoot.call( + blockHeight, + stateRoot, + {from: worker}, + ); + + assert.strictEqual( + result, + true, + 'Return value of anchorStateRoot must be true.', + ); + + await anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ); + + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); + assert.strictEqual( + blockHeight.eq(latestBlockHeight), + true, + `Latest block height from the contract must be ${blockHeight}.`, + ); + + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + stateRoot, + `Latest state root from the contract must be ${stateRoot}.`, + ); + + }); + + it('should emit `StateRootAvailable` event', async () => { + + blockHeight = blockHeight.addn(1); + + let tx = await anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ); + + let event = EventDecoder.getEvents(tx, anchor); + + assert.isDefined( + event.StateRootAvailable, + 'Event `StateRootAvailable` must be emitted.', + ); + + let eventData = event.StateRootAvailable; + + assert.strictEqual( + eventData._stateRoot, + stateRoot, + `The _stateRoot value in the event should be equal to ${stateRoot}` + ); + + assert.strictEqual( + blockHeight.eq(eventData._blockHeight), + true, + `The _blockHeight in the event should be equal to ${blockHeight}` + ); + + }); + }); diff --git a/test/gateway/anchor/constructor.js b/test/gateway/anchor/constructor.js index 8d214090..9faee583 100644 --- a/test/gateway/anchor/constructor.js +++ b/test/gateway/anchor/constructor.js @@ -18,95 +18,95 @@ // // ---------------------------------------------------------------------------- -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) { - - let remoteChainId, blockHeight, stateRoot, membersManager, safeCore; - - beforeEach(async function () { - - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - membersManager = accounts[1]; - - }); - - it('should fail when remote chain id is zero', async () => { - - remoteChainId = new BN(0); - - await Utils.expectRevert( - SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - membersManager, - ), - 'Remote chain Id must not be 0.', - ); - - }); - - it('should fail when members manager address is zero', async () => { - - membersManager = NullAddress; - - await Utils.expectRevert( - SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - membersManager, - ), - 'MembersManager contract address must not be zero.', - ); - - }); - - it('should pass with correct params', async () => { - - safeCore = await SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - membersManager, - ); - - let chainId = await safeCore.getRemoteChainId.call(); - assert.strictEqual( - remoteChainId.eq(chainId), - true, - `Remote chain id from the contract must be ${remoteChainId}.`, - ); - - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - let membersManagerAddress = await safeCore.membersManager.call(); - assert.strictEqual( - membersManagerAddress, - membersManager, - `Members manager address from the contract must be ${membersManager}.`, - ); - - }); - +contract('Anchor.constructor()', function (accounts) { + + let remoteChainId, blockHeight, stateRoot, membersManager, anchor; + + beforeEach(async function () { + + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + membersManager = accounts[1]; + + }); + + it('should fail when remote chain id is zero', async () => { + + remoteChainId = new BN(0); + + await Utils.expectRevert( + Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + membersManager, + ), + 'Remote chain Id must not be 0.', + ); + + }); + + it('should fail when members manager address is zero', async () => { + + membersManager = NullAddress; + + await Utils.expectRevert( + Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + membersManager, + ), + 'MembersManager contract address must not be zero.', + ); + + }); + + it('should pass with correct params', async () => { + + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + membersManager, + ); + + let chainId = await anchor.getRemoteChainId.call(); + assert.strictEqual( + remoteChainId.eq(chainId), + true, + `Remote chain id from the contract must be ${remoteChainId}.`, + ); + + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); + assert.strictEqual( + blockHeight.eq(latestBlockHeight), + true, + `Latest block height from the contract must be ${blockHeight}.`, + ); + + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + stateRoot, + `Latest state root from the contract must be ${stateRoot}.`, + ); + + let membersManagerAddress = await anchor.membersManager.call(); + assert.strictEqual( + membersManagerAddress, + membersManager, + `Members manager address from the contract must be ${membersManager}.`, + ); + + }); + }); diff --git a/test/gateway/anchor/get_latest_state_root_block_height.js b/test/gateway/anchor/get_latest_state_root_block_height.js index 0f8a3fca..8b4e0b89 100644 --- a/test/gateway/anchor/get_latest_state_root_block_height.js +++ b/test/gateway/anchor/get_latest_state_root_block_height.js @@ -24,61 +24,61 @@ const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - membersManager, - anchor, - owner, - worker; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - membersManager = await MockMembersManager.new(owner, worker); - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - membersManager.address, - ); - - }); - - it('should return the state root that was set while deployment', async () => { - - let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - }); - - it('should return the latest committed state root block height', async () => { - - blockHeight = blockHeight.addn(50000); - - await anchor.anchorStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ); - - let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - }); - + let remoteChainId, + blockHeight, + stateRoot, + membersManager, + anchor, + owner, + worker; + + beforeEach(async function () { + + owner = accounts[2]; + worker = accounts[3]; + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + membersManager = await MockMembersManager.new(owner, worker); + + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + membersManager.address, + ); + + }); + + it('should return the state root that was set while deployment', async () => { + + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); + assert.strictEqual( + blockHeight.eq(latestBlockHeight), + true, + `Latest block height from the contract must be ${blockHeight}.`, + ); + + }); + + it('should return the latest committed state root block height', async () => { + + blockHeight = blockHeight.addn(50000); + + await anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ); + + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); + assert.strictEqual( + blockHeight.eq(latestBlockHeight), + true, + `Latest block height from the contract must be ${blockHeight}.`, + ); + + }); + }); diff --git a/test/gateway/anchor/get_remote_chainId.js b/test/gateway/anchor/get_remote_chainId.js index 61954672..6100fc53 100644 --- a/test/gateway/anchor/get_remote_chainId.js +++ b/test/gateway/anchor/get_remote_chainId.js @@ -23,34 +23,34 @@ const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); contract('Anchor.getRemoteChainId()', function (accounts) { - - let remoteChainId, blockHeight, stateRoot, membersManager, anchor; - - beforeEach(async function () { - - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - membersManager = accounts[1]; - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - membersManager, - ); - - }); - - it('should return correct remote chain id', async () => { - - let chainId = await anchor.getRemoteChainId.call(); - assert.strictEqual( - remoteChainId.eq(chainId), - true, - `Remote chain id from the contract must be ${remoteChainId}.`, - ); - - }); - + + let remoteChainId, blockHeight, stateRoot, membersManager, anchor; + + beforeEach(async function () { + + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + membersManager = accounts[1]; + + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + membersManager, + ); + + }); + + it('should return correct remote chain id', async () => { + + let chainId = await anchor.getRemoteChainId.call(); + assert.strictEqual( + remoteChainId.eq(chainId), + true, + `Remote chain id from the contract must be ${remoteChainId}.`, + ); + + }); + }); diff --git a/test/gateway/anchor/get_state_root.js b/test/gateway/anchor/get_state_root.js index fd0c36f9..a3d30b06 100644 --- a/test/gateway/anchor/get_state_root.js +++ b/test/gateway/anchor/get_state_root.js @@ -24,79 +24,79 @@ const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); const zeroBytes = - "0x0000000000000000000000000000000000000000000000000000000000000000"; + "0x0000000000000000000000000000000000000000000000000000000000000000"; contract('Anchor.getStateRoot()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - membersManager, - anchor, - owner, - worker; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - membersManager = await MockMembersManager.new(owner, worker); - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - membersManager.address, - ); - - }); - - it('should return the latest state root block height that was set ' + - 'while deployment', async () => { - - 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 () => { - - blockHeight = blockHeight.addn(500); - - let latestStateRoot = await anchor.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - zeroBytes, - `Latest state root from the contract must be ${zeroBytes}.`, - ); - - }); - - it('should return the latest committed state root', async () => { - - blockHeight = blockHeight.addn(50000); - stateRoot = web3.utils.sha3("dummy_state_root_1"); - - await anchor.anchorStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ); - - let latestStateRoot = await anchor.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - }); - + + let remoteChainId, + blockHeight, + stateRoot, + membersManager, + anchor, + owner, + worker; + + beforeEach(async function () { + + owner = accounts[2]; + worker = accounts[3]; + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + membersManager = await MockMembersManager.new(owner, worker); + + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + membersManager.address, + ); + + }); + + it('should return the latest state root block height that was set ' + + 'while deployment', async () => { + + 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 () => { + + blockHeight = blockHeight.addn(500); + + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + zeroBytes, + `Latest state root from the contract must be ${zeroBytes}.`, + ); + + }); + + it('should return the latest committed state root', async () => { + + blockHeight = blockHeight.addn(50000); + stateRoot = web3.utils.sha3("dummy_state_root_1"); + + await anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ); + + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + stateRoot, + `Latest state root from the contract must be ${stateRoot}.`, + ); + + }); + }); diff --git a/test/gateway/anchor/set_co_anchor_address.js b/test/gateway/anchor/set_co_anchor_address.js new file mode 100644 index 00000000..6148bf73 --- /dev/null +++ b/test/gateway/anchor/set_co_anchor_address.js @@ -0,0 +1,117 @@ +// 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. +// +// ---------------------------------------------------------------------------- +// +// http://www.simpletoken.org/ +// +// ---------------------------------------------------------------------------- + +const Anchor = artifacts.require("./Anchor.sol"); +const MockMembersManager = artifacts.require('MockMembersManager.sol'); +const web3 = require('../../test_lib/web3.js'); +const BN = require('bn.js'); +const Utils = require('../../../test/test_lib/utils'); + +const NullAddress = "0x0000000000000000000000000000000000000000"; + +contract('Anchor.setCoAnchorAddress()', function (accounts) { + + let remoteChainId, + blockHeight, + stateRoot, + membersManager, + anchor, + owner, + worker, + coAnchorAddress; + + beforeEach(async function () { + + owner = accounts[2]; + worker = accounts[3]; + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + membersManager = await MockMembersManager.new(owner, worker); + coAnchorAddress = accounts[6]; + + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + membersManager.address, + ); + + }); + + it('should fail when coAnchor address is zero', async () => { + + coAnchorAddress = NullAddress; + + await Utils.expectRevert( + anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), + "Co-Anchor address must not be 0.", + ); + + }); + + it('should fail when caller is not organisation owner', async () => { + + let notOwner = accounts[7]; + + await Utils.expectRevert( + anchor.setCoAnchorAddress(coAnchorAddress, {from: notOwner}), + 'Only the organization is allowed to call this method.', + ); + + }); + + it('should pass with correct params', async () => { + + let result = await anchor.setCoAnchorAddress.call( + coAnchorAddress, + {from: owner}, + ); + + assert.strictEqual( + result, + true, + 'Return value of setAnchorAddress must be true.', + ); + + await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); + + let coAnchor = await anchor.coAnchor.call(); + + assert.strictEqual( + coAnchor, + coAnchorAddress, + `CoAnchor address must be equal to ${coAnchorAddress}.`, + ); + + }); + + it('should fail to set coAnchor address if it\'s already set', async () => { + + await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); + + await Utils.expectRevert( + anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), + 'Co-Anchor has already been set and cannot be updated.', + ); + + }); + +}); diff --git a/test/gateway/anchor/set_co_core_address.js b/test/gateway/anchor/set_co_core_address.js deleted file mode 100644 index 2a714666..00000000 --- a/test/gateway/anchor/set_co_core_address.js +++ /dev/null @@ -1,117 +0,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. -// -// ---------------------------------------------------------------------------- -// -// http://www.simpletoken.org/ -// -// ---------------------------------------------------------------------------- - -const Anchor = artifacts.require("./Anchor.sol"); -const MockMembersManager = artifacts.require('MockMembersManager.sol'); -const web3 = require('../../test_lib/web3.js'); -const BN = require('bn.js'); -const Utils = require('../../../test/test_lib/utils'); - -const NullAddress = "0x0000000000000000000000000000000000000000"; - -contract('Anchor.setCoCoreAddress()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - membersManager, - anchor, - owner, - worker, - coCoreAddress; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - membersManager = await MockMembersManager.new(owner, worker); - coCoreAddress = accounts[6]; - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - membersManager.address, - ); - - }); - - it('should fail when coCore address is zero', async () => { - - coCoreAddress = NullAddress; - - await Utils.expectRevert( - anchor.setCoCoreAddress(coCoreAddress, { from: owner }), - 'Co-Core address must not be 0.', - ); - - }); - - it('should fail when caller is not organisation owner', async () => { - - let notOwner = accounts[7]; - - await Utils.expectRevert( - anchor.setCoCoreAddress(coCoreAddress, { from: notOwner }), - 'Only the organization is allowed to call this method.', - ); - - }); - - it('should pass with correct params', async () => { - - let result = await anchor.setCoCoreAddress.call( - coCoreAddress, - { from: owner }, - ); - - assert.strictEqual( - result, - true, - 'Return value of setCoCoreAddress must be true.', - ); - - await anchor.setCoCoreAddress(coCoreAddress, { from: owner }); - - let coCore = await anchor.coCore.call(); - - assert.strictEqual( - coCore, - coCoreAddress, - `CoCore address must be equal to ${coCoreAddress}.`, - ); - - }); - - it('should fail to set coCore address if it\'s already set', async () => { - - await anchor.setCoCoreAddress(coCoreAddress, { from: owner }); - - await Utils.expectRevert( - anchor.setCoCoreAddress(coCoreAddress, { from: owner }), - 'Co-Core has already been set and cannot be updated.', - ); - - }); - -}); From cdf2b22d5587ef37a99a2a332e5326b9369ce408 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Tue, 18 Dec 2018 12:11:59 +0530 Subject: [PATCH 04/15] Changes done as per PR feedback --- contracts/StateRootInterface.sol | 4 ++-- contracts/gateway/Anchor.sol | 2 +- contracts/gateway/GatewayBase.sol | 2 +- contracts/lib/GatewayLib.sol | 2 +- contracts/test/core/MockAnchor.sol | 4 ++-- contracts/test/gateway/MockGatewayBase.sol | 6 +++--- test/gateway/anchor/get_latest_state_root_block_height.js | 2 +- test/gateway/anchor/get_state_root.js | 4 ++-- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/contracts/StateRootInterface.sol b/contracts/StateRootInterface.sol index f3129a3f..0f97fb23 100644 --- a/contracts/StateRootInterface.sol +++ b/contracts/StateRootInterface.sol @@ -18,9 +18,9 @@ pragma solidity ^0.5.0; interface StateRootInterface { /** - * @notice Gets the block number of latest committed state root. + * @notice Gets the block number 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 diff --git a/contracts/gateway/Anchor.sol b/contracts/gateway/Anchor.sol index b45e723d..96df8a54 100644 --- a/contracts/gateway/Anchor.sol +++ b/contracts/gateway/Anchor.sol @@ -181,7 +181,7 @@ contract Anchor is StateRootInterface, Organized { "State root must not be zero." ); - // Input block height should be valid + // Input block height should be valid. require( _blockHeight > latestStateRootBlockHeight, "Given block height is lower or equal to highest anchored state root block height." diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index 6435705d..74cc40d2 100644 --- a/contracts/gateway/GatewayBase.sol +++ b/contracts/gateway/GatewayBase.sol @@ -156,7 +156,7 @@ contract GatewayBase is Organized { /** * @notice 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 + * stateRoots mapping. stateRoot is anchored in anchorStateRoot * 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 diff --git a/contracts/lib/GatewayLib.sol b/contracts/lib/GatewayLib.sol index 1b54ac74..b1d2dd1d 100644 --- a/contracts/lib/GatewayLib.sol +++ b/contracts/lib/GatewayLib.sol @@ -126,7 +126,7 @@ library GatewayLib { ); /** - * Verify the remote OpenST contract against the committed state + * Verify the remote OpenST contract against the anchored state * root with the state trie Merkle proof */ require(MerklePatriciaProof.verify(hashedAccount, _encodedPath, diff --git a/contracts/test/core/MockAnchor.sol b/contracts/test/core/MockAnchor.sol index 33f502bd..f7f805ad 100644 --- a/contracts/test/core/MockAnchor.sol +++ b/contracts/test/core/MockAnchor.sol @@ -15,7 +15,7 @@ contract MockAnchor is Anchor { * @notice Contract constructor. * * @param _remoteChainId _remoteChainId is the chain id of the chain that - * this core tracks. + * this anchor tracks. * @param _blockHeight Block height at which _stateRoot needs to store. * @param _stateRoot State root hash of given _blockHeight. * @param _membersManager Address of a members manager contract. @@ -39,7 +39,7 @@ contract MockAnchor is Anchor { * @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 35f240c2..9f3a4d7f 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 { @@ -42,7 +42,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 anchorStateRoot + * stateRoots mapping. stateRoot is anchored in anchorStateRoot * 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 diff --git a/test/gateway/anchor/get_latest_state_root_block_height.js b/test/gateway/anchor/get_latest_state_root_block_height.js index 8b4e0b89..87a67174 100644 --- a/test/gateway/anchor/get_latest_state_root_block_height.js +++ b/test/gateway/anchor/get_latest_state_root_block_height.js @@ -62,7 +62,7 @@ contract('Anchor.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); diff --git a/test/gateway/anchor/get_state_root.js b/test/gateway/anchor/get_state_root.js index a3d30b06..d2ed658f 100644 --- a/test/gateway/anchor/get_state_root.js +++ b/test/gateway/anchor/get_state_root.js @@ -66,7 +66,7 @@ contract('Anchor.getStateRoot()', function (accounts) { }); - 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); @@ -79,7 +79,7 @@ contract('Anchor.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"); From e66da546f2f50d58f313a65e504b559c42e037bf Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Tue, 18 Dec 2018 13:19:12 +0530 Subject: [PATCH 05/15] Changed all references of core in the constructor --- .solcover.js | 2 +- contracts/gateway/EIP20CoGateway.sol | 6 ++--- contracts/gateway/EIP20Gateway.sol | 6 ++--- contracts/gateway/GatewayBase.sol | 14 +++++------ contracts/test/TestEIP20CoGateway.sol | 6 ++--- contracts/test/TestEIP20Gateway.sol | 6 ++--- contracts/test/gateway/MockGatewayBase.sol | 8 +++---- test/gateway/eip20_cogateway/constructor.js | 22 ++++++++--------- .../gateway/eip20_gateway/activate_gateway.js | 4 ++-- test/gateway/eip20_gateway/constructor.js | 24 +++++++++---------- .../eip20_gateway/deactivate_gateway.js | 4 ++-- test/gateway/eip20_gateway/stake.js | 4 ++-- test/gateway/gateway_base/bounty_change.js | 8 +++---- test/gateway/gateway_base/construction.js | 22 ++++++++--------- test/gateway/gateway_base/get_nonce.js | 4 ++-- test/gateway/gateway_base/prove_gateway.js | 6 ++--- test/gateway/helpers/helper.js | 8 +++---- 17 files changed, 77 insertions(+), 77 deletions(-) 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/gateway/EIP20CoGateway.sol b/contracts/gateway/EIP20CoGateway.sol index 3c0d6b9a..020078ea 100644 --- a/contracts/gateway/EIP20CoGateway.sol +++ b/contracts/gateway/EIP20CoGateway.sol @@ -203,7 +203,7 @@ 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 _anchor Anchor contract address. * @param _bounty The amount that facilitator stakes to initiate the stake * process. * @param _membersManager Address of a contract that manages workers. @@ -213,14 +213,14 @@ contract EIP20CoGateway is GatewayBase { constructor( address _valueToken, address _utilityToken, - StateRootInterface _core, + StateRootInterface _anchor, uint256 _bounty, IsMemberInterface _membersManager, address _gateway, address payable _burner ) GatewayBase( - _core, + _anchor, _bounty, _membersManager ) diff --git a/contracts/gateway/EIP20Gateway.sol b/contracts/gateway/EIP20Gateway.sol index 08ee0215..3dafbbb6 100644 --- a/contracts/gateway/EIP20Gateway.sol +++ b/contracts/gateway/EIP20Gateway.sol @@ -229,7 +229,7 @@ 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 _anchor Anchor contract address. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. @@ -238,13 +238,13 @@ contract EIP20Gateway is GatewayBase { constructor( EIP20Interface _token, EIP20Interface _baseToken, - StateRootInterface _core, + StateRootInterface _anchor, uint256 _bounty, IsMemberInterface _membersManager, address _burner ) GatewayBase( - _core, + _anchor, _bounty, _membersManager ) diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index 74cc40d2..86428619 100644 --- a/contracts/gateway/GatewayBase.sol +++ b/contracts/gateway/GatewayBase.sol @@ -74,8 +74,8 @@ contract GatewayBase is Organized { */ MessageBus.MessageBox messageBox; - /** Address of core contract. */ - StateRootInterface public core; + /** Address of anchor contract. */ + StateRootInterface public anchor; /** Path to make Merkle account proof for Gateway/CoGateway contract. */ bytes public encodedGatewayPath; @@ -128,13 +128,13 @@ contract GatewayBase is Organized { /** * @notice Initialize the contract and set default values. * - * @param _core Core contract address. + * @param _anchor Anchor contract address. * @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 _anchor, uint256 _bounty, IsMemberInterface _membersManager ) @@ -142,11 +142,11 @@ contract GatewayBase is Organized { public { require( - address(_core) != address(0), + address(_anchor) != address(0), "Core contract address must not be zero." ); - core = _core; + anchor = _anchor; bounty = _bounty; } @@ -192,7 +192,7 @@ contract GatewayBase is Organized { "Length of RLP parent nodes is 0" ); - bytes32 stateRoot = core.getStateRoot(_blockHeight); + bytes32 stateRoot = anchor.getStateRoot(_blockHeight); // State root should be present for the block height require( diff --git a/contracts/test/TestEIP20CoGateway.sol b/contracts/test/TestEIP20CoGateway.sol index 27999e40..9cfc67d8 100644 --- a/contracts/test/TestEIP20CoGateway.sol +++ b/contracts/test/TestEIP20CoGateway.sol @@ -39,7 +39,7 @@ 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 _anchor Anchor contract address. * @param _bounty The amount that facilitator will stakes to initiate the * staking process. * @param _membersManager Address of a members manager contract. @@ -48,7 +48,7 @@ contract TestEIP20CoGateway is EIP20CoGateway { constructor( address _valueToken, address _utilityToken, - StateRootInterface _core, + StateRootInterface _anchor, uint256 _bounty, IsMemberInterface _membersManager, address _gateway, @@ -57,7 +57,7 @@ contract TestEIP20CoGateway is EIP20CoGateway { EIP20CoGateway( _valueToken, _utilityToken, - _core, + _anchor, _bounty, _membersManager, _gateway, diff --git a/contracts/test/TestEIP20Gateway.sol b/contracts/test/TestEIP20Gateway.sol index 27e91654..2d2f2ae3 100644 --- a/contracts/test/TestEIP20Gateway.sol +++ b/contracts/test/TestEIP20Gateway.sol @@ -36,7 +36,7 @@ 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 _anchor Anchor contract address. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. @@ -45,7 +45,7 @@ contract TestEIP20Gateway is EIP20Gateway { constructor( EIP20Interface _token, EIP20Interface _baseToken, - StateRootInterface _core, + StateRootInterface _anchor, uint256 _bounty, IsMemberInterface _membersManager, address _burner @@ -53,7 +53,7 @@ contract TestEIP20Gateway is EIP20Gateway { EIP20Gateway( _token, _baseToken, - _core, + _anchor, _bounty, _membersManager, _burner diff --git a/contracts/test/gateway/MockGatewayBase.sol b/contracts/test/gateway/MockGatewayBase.sol index 9f3a4d7f..6867624c 100644 --- a/contracts/test/gateway/MockGatewayBase.sol +++ b/contracts/test/gateway/MockGatewayBase.sol @@ -17,19 +17,19 @@ contract MockGatewayBase is GatewayBase { /** * @notice This is used for testing. * - * @param _core Core contract address. + * @param _anchor Anchor contract address. * @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 _anchor, uint256 _bounty, IsMemberInterface _membersManager ) public GatewayBase( - _core, + _anchor, _bounty, _membersManager ) @@ -78,7 +78,7 @@ contract MockGatewayBase is GatewayBase { "Length of RLP parent nodes must not be 0." ); - bytes32 stateRoot = core.getStateRoot(_blockHeight); + bytes32 stateRoot = anchor.getStateRoot(_blockHeight); //State root should be present for the block height require( diff --git a/test/gateway/eip20_cogateway/constructor.js b/test/gateway/eip20_cogateway/constructor.js index c25beb4c..db148ee2 100644 --- a/test/gateway/eip20_cogateway/constructor.js +++ b/test/gateway/eip20_cogateway/constructor.js @@ -30,7 +30,7 @@ const NullAddress = "0x0000000000000000000000000000000000000000"; contract('EIP20CoGateway.constructor() ', function (accounts) { - let valueToken, utilityToken, bountyAmount, coreAddress, owner, worker, + let valueToken, utilityToken, bountyAmount, anchorAddress, owner, worker, membersManager, coGateway, gatewayAddress = accounts[6], burner = NullAddress; @@ -38,7 +38,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { valueToken = await MockToken.new(); utilityToken = await MockToken.new(); - coreAddress = accounts[1]; + anchorAddress = accounts[1]; bountyAmount = new BN(100); owner = accounts[2]; @@ -51,7 +51,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueToken.address, utilityToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, gatewayAddress, @@ -69,7 +69,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueToken.address, utilityToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, gatewayAddress, @@ -94,7 +94,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { let coreAdd = await coGateway.core.call(); assert.strictEqual( coreAdd, - coreAddress, + anchorAddress, 'Invalid core address from contract.' ); @@ -113,7 +113,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueTokenAddress, utilityToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, gatewayAddress, @@ -130,7 +130,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueToken.address, utilityTokenAddress, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, gatewayAddress, @@ -140,14 +140,14 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { ); }); - it('should not deploy contract if core address is passed as zero.', async function () { - let coreAddress = NullAddress; + it('should not deploy contract if anchor address is passed as zero.', async function () { + let anchorAddress = NullAddress; await Utils.expectRevert( CoGateway.new( valueToken.address, utilityToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, gatewayAddress, @@ -165,7 +165,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueToken.address, utilityToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, gatewayAddress, diff --git a/test/gateway/eip20_gateway/activate_gateway.js b/test/gateway/eip20_gateway/activate_gateway.js index 11fa7008..b6fe70a5 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], + anchorAddress = 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, + anchorAddress, bountyAmount, membersManager.address, burner diff --git a/test/gateway/eip20_gateway/constructor.js b/test/gateway/eip20_gateway/constructor.js index 47af2ba8..cdc17871 100644 --- a/test/gateway/eip20_gateway/constructor.js +++ b/test/gateway/eip20_gateway/constructor.js @@ -29,14 +29,14 @@ const NullAddress = "0x0000000000000000000000000000000000000000"; contract('EIP20Gateway.constructor() ', function (accounts) { - let mockToken, baseToken, bountyAmount, coreAddress, membersManager, + let mockToken, baseToken, bountyAmount, anchorAddress, membersManager, gateway, owner, worker, burner = NullAddress; beforeEach(async function () { mockToken = await MockToken.new(); baseToken = await MockToken.new(); - coreAddress = accounts[1]; + anchorAddress = accounts[1]; bountyAmount = new BN(100); owner = accounts[2]; @@ -49,7 +49,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, burner @@ -66,7 +66,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, burner @@ -87,10 +87,10 @@ contract('EIP20Gateway.constructor() ', function (accounts) { "Invalid bounty token address from contract." ); - let coreAdd = await gateway.core.call(); + let coreAdd = await gateway.anchor.call(); assert.equal( coreAdd, - coreAddress, + anchorAddress, "Invalid core address from contract" ); @@ -121,7 +121,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken, baseToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, burner @@ -137,7 +137,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseTokenAddress, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, burner @@ -147,13 +147,13 @@ contract('EIP20Gateway.constructor() ', function (accounts) { }); it('should not deploy contract if core address is passed as zero.', async function () { - let coreAddress = NullAddress; + let anchorAddress = NullAddress; await Utils.expectRevert( Gateway.new( mockToken.address, baseToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, burner @@ -170,7 +170,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager, burner @@ -187,7 +187,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseToken.address, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, burner diff --git a/test/gateway/eip20_gateway/deactivate_gateway.js b/test/gateway/eip20_gateway/deactivate_gateway.js index 27570382..90f6c439 100644 --- a/test/gateway/eip20_gateway/deactivate_gateway.js +++ b/test/gateway/eip20_gateway/deactivate_gateway.js @@ -18,7 +18,7 @@ contract('EIP20Gateway.deactivateGateway()', function (accounts) { let mockToken = accounts[0], baseToken = accounts[1], - coreAddress = accounts[2], + anchorAddress = accounts[2], bountyAmount = new BN(100); membersManager = await MockMembersManager.new(owner, worker); @@ -26,7 +26,7 @@ contract('EIP20Gateway.deactivateGateway()', function (accounts) { gateway = await Gateway.new( mockToken, baseToken, - coreAddress, + anchorAddress, bountyAmount, membersManager.address, burner diff --git a/test/gateway/eip20_gateway/stake.js b/test/gateway/eip20_gateway/stake.js index 6bf6ce8d..b0bc354d 100644 --- a/test/gateway/eip20_gateway/stake.js +++ b/test/gateway/eip20_gateway/stake.js @@ -157,7 +157,7 @@ contract('EIP20Gateway.stake() ', function (accounts) { gateway = await Gateway.new( mockToken.address, baseToken.address, - accounts[1], //core address + accounts[1], //anchor address bountyAmount, accounts[2], // organisation address, burner @@ -318,7 +318,7 @@ contract('EIP20Gateway.stake() ', function (accounts) { gateway = await Gateway.new( mockToken.address, baseToken.address, - accounts[1], //core address + accounts[1], //anchor address bountyAmount, accounts[2], // organisation address burner diff --git a/test/gateway/gateway_base/bounty_change.js b/test/gateway/gateway_base/bounty_change.js index ddd70328..205cb809 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 anchor = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - core, + anchor, bounty, membersManager.address ); @@ -103,13 +103,13 @@ contract('GatewayBase.sol', function (accounts) { beforeEach(async function () { - let core = accounts[0] + let anchor = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - core, + anchor, bounty, membersManager.address ); diff --git a/test/gateway/gateway_base/construction.js b/test/gateway/gateway_base/construction.js index ec817122..d49b05f1 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 anchor, bounty, worker, membersManager; beforeEach(async function () { owner = accounts[2] , worker = accounts[3] - , core = accounts[0] + , anchor = accounts[0] , bounty = new BN(100); membersManager = await MockMembersManager.new(owner, worker); @@ -23,14 +23,14 @@ contract('GatewayBase.sol', function (accounts) { it('should pass with right set of parameters', async function () { gatewayBaseInstance = await GatewayBase.new( - core, + anchor, bounty, membersManager.address ); assert.strictEqual( - core, - await gatewayBaseInstance.core.call(), + anchor, + await gatewayBaseInstance.anchor.call(), "Core contract address doesn't match." ); assert((await gatewayBaseInstance.bounty.call()).eq(bounty)); @@ -41,20 +41,20 @@ contract('GatewayBase.sol', function (accounts) { bounty = new BN(0); gatewayBaseInstance = await GatewayBase.new( - core, + anchor, bounty, membersManager.address ); - assert.equal(core, await gatewayBaseInstance.core.call()); + assert.equal(anchor, await gatewayBaseInstance.anchor.call()); assert((await gatewayBaseInstance.bounty.call()).eq(bounty)); }); - it('should fail if core address is not passed', async function () { + it('should fail if anchor address is zero', async function () { - core = NullAddress; + anchor = NullAddress; await Utils.expectRevert( - GatewayBase.new(core, bounty, membersManager.address), + GatewayBase.new(anchor, bounty, membersManager.address), "Core 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(anchor, 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..ef57d0bb 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] + , anchor = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - core, + anchor, bounty, membersManager.address ); diff --git a/test/gateway/gateway_base/prove_gateway.js b/test/gateway/gateway_base/prove_gateway.js index d37f8d5d..20969074 100644 --- a/test/gateway/gateway_base/prove_gateway.js +++ b/test/gateway/gateway_base/prove_gateway.js @@ -1,5 +1,5 @@ const GatewayBase = artifacts.require("./MockGatewayBase.sol") - , Core = artifacts.require("./MockAnchor.sol") + , MockAnchor = artifacts.require("./MockAnchor.sol") , BN = require('bn.js'); const MockMembersManager = artifacts.require('MockMembersManager.sol'); @@ -22,10 +22,10 @@ contract('GatewayBase.sol', function (accounts) { , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); - let core = await Core.new(1, 0, stateRoot, membersManager.address); + let anchor = await MockAnchor.new(1, 0, stateRoot, membersManager.address); gatewayBaseInstance = await GatewayBase.new( - core.address, + anchor.address, bounty, membersManager.address ); diff --git a/test/gateway/helpers/helper.js b/test/gateway/helpers/helper.js index 4802f57e..7de8635f 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, + anchorAddress = params.anchor, bountyAmount = params.bounty, organizationAddress = params.organization; @@ -27,7 +27,7 @@ GatewayHelper.prototype = { await utils.expectThrow(Gateway.new( valueTokenAddress, bountyToken, - coreAddress, + anchorAddress, bountyAmount, organizationAddress )); @@ -35,7 +35,7 @@ GatewayHelper.prototype = { this.gateway = await Gateway.new( valueTokenAddress, bountyToken, - coreAddress, + anchorAddress, bountyAmount, organizationAddress ); @@ -67,7 +67,7 @@ GatewayHelper.prototype = { let coreAdd = await this.gateway.core.call(); assert.equal( coreAdd, - coreAddress, + anchorAddress, "Invalid core address from contract" ); From 8bf24fbfe75d1e30add898d82cd86284b3d0105e Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Tue, 18 Dec 2018 13:40:48 +0530 Subject: [PATCH 06/15] Changed all references of core in the constructor --- contracts/gateway/GatewayBase.sol | 2 +- test/gateway/eip20_cogateway/constructor.js | 8 ++++---- test/gateway/eip20_gateway/constructor.js | 10 +++++----- test/gateway/gateway_base/construction.js | 4 ++-- test/gateway/helpers/helper.js | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index 86428619..a7b7db9e 100644 --- a/contracts/gateway/GatewayBase.sol +++ b/contracts/gateway/GatewayBase.sol @@ -143,7 +143,7 @@ contract GatewayBase is Organized { { require( address(_anchor) != address(0), - "Core contract address must not be zero." + "Anchor contract address must not be zero." ); anchor = _anchor; diff --git a/test/gateway/eip20_cogateway/constructor.js b/test/gateway/eip20_cogateway/constructor.js index db148ee2..e3a64d57 100644 --- a/test/gateway/eip20_cogateway/constructor.js +++ b/test/gateway/eip20_cogateway/constructor.js @@ -91,11 +91,11 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { 'Invalid bounty token address from contract.' ); - let coreAdd = await coGateway.core.call(); + let anchorAdd = await coGateway.anchor.call(); assert.strictEqual( - coreAdd, + anchorAdd, anchorAddress, - 'Invalid core address from contract.' + 'Invalid anchor address from contract.' ); let bounty = await coGateway.bounty.call(); @@ -153,7 +153,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { gatewayAddress, burner ), - 'Core contract address must not be zero.' + 'Anchor contract address must not be zero.' ); }); diff --git a/test/gateway/eip20_gateway/constructor.js b/test/gateway/eip20_gateway/constructor.js index cdc17871..85501def 100644 --- a/test/gateway/eip20_gateway/constructor.js +++ b/test/gateway/eip20_gateway/constructor.js @@ -87,11 +87,11 @@ contract('EIP20Gateway.constructor() ', function (accounts) { "Invalid bounty token address from contract." ); - let coreAdd = await gateway.anchor.call(); + let anchorAdd = await gateway.anchor.call(); assert.equal( - coreAdd, + anchorAdd, anchorAddress, - "Invalid core address from contract" + "Invalid anchor address from contract" ); let bounty = await gateway.bounty.call(); @@ -146,7 +146,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { ); }); - it('should not deploy contract if core address is passed as zero.', async function () { + it('should not deploy contract if anchor address is passed as zero.', async function () { let anchorAddress = NullAddress; await Utils.expectRevert( @@ -158,7 +158,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { membersManager.address, burner ), - "Core contract address must not be zero." + "Anchor contract address must not be zero." ); }); diff --git a/test/gateway/gateway_base/construction.js b/test/gateway/gateway_base/construction.js index d49b05f1..155d624a 100644 --- a/test/gateway/gateway_base/construction.js +++ b/test/gateway/gateway_base/construction.js @@ -31,7 +31,7 @@ contract('GatewayBase.sol', function (accounts) { assert.strictEqual( anchor, await gatewayBaseInstance.anchor.call(), - "Core contract address doesn't match." + "Anchor contract address doesn't match." ); assert((await gatewayBaseInstance.bounty.call()).eq(bounty)); }); @@ -55,7 +55,7 @@ contract('GatewayBase.sol', function (accounts) { anchor = NullAddress; await Utils.expectRevert( GatewayBase.new(anchor, bounty, membersManager.address), - "Core contract address must not be zero." + "Anchor contract address must not be zero." ); }); diff --git a/test/gateway/helpers/helper.js b/test/gateway/helpers/helper.js index 7de8635f..717b6add 100644 --- a/test/gateway/helpers/helper.js +++ b/test/gateway/helpers/helper.js @@ -64,11 +64,11 @@ GatewayHelper.prototype = { "Invalid bounty token address from contract" ); - let coreAdd = await this.gateway.core.call(); + let anchorAdd = await this.gateway.anchor.call(); assert.equal( - coreAdd, + anchorAdd, anchorAddress, - "Invalid core address from contract" + "Invalid anchor address from contract" ); let bounty = await this.gateway.bounty.call(); From ca4e4aeb373b80265e4cd69d050170a417711995 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 19 Dec 2018 13:14:07 +0530 Subject: [PATCH 07/15] Fixed styling --- contracts/gateway/Anchor.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/gateway/Anchor.sol b/contracts/gateway/Anchor.sol index 44c01dd9..18a013fb 100644 --- a/contracts/gateway/Anchor.sol +++ b/contracts/gateway/Anchor.sol @@ -45,12 +45,12 @@ contract Anchor is StateRootInterface, Organized, CircularBufferUint { using SafeMath for uint256; - /** Events */ + /* Events */ event StateRootAvailable(uint256 _blockHeight, bytes32 _stateRoot); - /** Storage */ + /* Storage */ /** Maps block heights to their respective state root. */ mapping (uint256 => bytes32) private stateRoots; From e5377c4a044e8e99f15085ee9a2553ac6b3711e3 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 19 Dec 2018 16:40:25 +0530 Subject: [PATCH 08/15] Added maxNumberStateRoots in the constructor and updated the unit test-cases --- contracts/gateway/Anchor.sol | 2 +- test/gateway/anchor/anchor_state_root.js | 51 ++++++++++++++++++- test/gateway/anchor/constructor.js | 13 ++++- .../get_latest_state_root_block_height.js | 3 ++ test/gateway/anchor/get_remote_chainId.js | 11 +++- test/gateway/anchor/get_state_root.js | 3 ++ test/gateway/anchor/set_co_anchor_address.js | 3 ++ 7 files changed, 80 insertions(+), 6 deletions(-) diff --git a/contracts/gateway/Anchor.sol b/contracts/gateway/Anchor.sol index 18a013fb..8f844460 100644 --- a/contracts/gateway/Anchor.sol +++ b/contracts/gateway/Anchor.sol @@ -189,7 +189,7 @@ contract Anchor is StateRootInterface, Organized, CircularBufferUint { // 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; diff --git a/test/gateway/anchor/anchor_state_root.js b/test/gateway/anchor/anchor_state_root.js index 4902a6d2..c878f9cf 100644 --- a/test/gateway/anchor/anchor_state_root.js +++ b/test/gateway/anchor/anchor_state_root.js @@ -36,7 +36,8 @@ contract('Anchor.anchorStateRoot()', function (accounts) { membersManager, anchor, owner, - worker; + worker, + maxNumberOfStateRoots; beforeEach(async function () { @@ -45,12 +46,14 @@ contract('Anchor.anchorStateRoot()', function (accounts) { remoteChainId = new BN(1410); blockHeight = new BN(5); stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager.address, ); @@ -189,5 +192,51 @@ contract('Anchor.anchorStateRoot()', function (accounts) { ); }); + + it('should store only the given number of max store roots', async () => { + /* + * It should store the given state roots and they should be + * available for querying afterwards. After the max number of state + * roots has been exceeded, the old state roots should no longer be + * available. + */ + let iterations = maxNumberOfStateRoots.muln(2).toNumber(); + for (let i = 0; i < iterations; i++) { + blockHeight = blockHeight.addn(1); + await anchor.anchorStateRoot( + blockHeight, + stateRoot, + { 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 anchor.getStateRoot.call( + prunedBlockHeight, + ); + assert.strictEqual( + storedStateRoot, + '0x0000000000000000000000000000000000000000000000000000000000000000', + 'There should not be any state root stored at a ' + + 'pruned height. It should have been reset by now.', + ); + + /* + * The state root that is one block younger than the pruned + * one should still be available. + */ + let existingBlockHeight = prunedBlockHeight.addn(1); + storedStateRoot = await anchor.getStateRoot.call( + existingBlockHeight, + ); + assert.strictEqual( + storedStateRoot, + stateRoot, + 'The stored state root should still exist.', + ); + } + } + }); }); diff --git a/test/gateway/anchor/constructor.js b/test/gateway/anchor/constructor.js index 9faee583..e1f9cb52 100644 --- a/test/gateway/anchor/constructor.js +++ b/test/gateway/anchor/constructor.js @@ -26,14 +26,20 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('Anchor.constructor()', function (accounts) { - - let remoteChainId, blockHeight, stateRoot, membersManager, anchor; + + let remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager, + safeCore; beforeEach(async function () { remoteChainId = new BN(1410); blockHeight = new BN(5); stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); membersManager = accounts[1]; }); @@ -47,6 +53,7 @@ contract('Anchor.constructor()', function (accounts) { remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager, ), 'Remote chain Id must not be 0.', @@ -63,6 +70,7 @@ contract('Anchor.constructor()', function (accounts) { remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager, ), 'MembersManager contract address must not be zero.', @@ -76,6 +84,7 @@ contract('Anchor.constructor()', function (accounts) { remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager, ); diff --git a/test/gateway/anchor/get_latest_state_root_block_height.js b/test/gateway/anchor/get_latest_state_root_block_height.js index 87a67174..8a408bda 100644 --- a/test/gateway/anchor/get_latest_state_root_block_height.js +++ b/test/gateway/anchor/get_latest_state_root_block_height.js @@ -29,6 +29,7 @@ contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { blockHeight, stateRoot, membersManager, + maxNumberOfStateRoots, anchor, owner, worker; @@ -40,12 +41,14 @@ contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { remoteChainId = new BN(1410); blockHeight = new BN(5); stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager.address, ); diff --git a/test/gateway/anchor/get_remote_chainId.js b/test/gateway/anchor/get_remote_chainId.js index 6100fc53..f8ec5555 100644 --- a/test/gateway/anchor/get_remote_chainId.js +++ b/test/gateway/anchor/get_remote_chainId.js @@ -23,20 +23,27 @@ const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); contract('Anchor.getRemoteChainId()', function (accounts) { - - let remoteChainId, blockHeight, stateRoot, membersManager, anchor; + + let remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager, + anchor; beforeEach(async function () { remoteChainId = new BN(1410); blockHeight = new BN(5); stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); membersManager = accounts[1]; anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager, ); diff --git a/test/gateway/anchor/get_state_root.js b/test/gateway/anchor/get_state_root.js index d2ed658f..06e6f4e2 100644 --- a/test/gateway/anchor/get_state_root.js +++ b/test/gateway/anchor/get_state_root.js @@ -31,6 +31,7 @@ contract('Anchor.getStateRoot()', function (accounts) { let remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager, anchor, owner, @@ -43,12 +44,14 @@ contract('Anchor.getStateRoot()', function (accounts) { remoteChainId = new BN(1410); blockHeight = new BN(5); stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); anchor = await Anchor.new( remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager.address, ); diff --git a/test/gateway/anchor/set_co_anchor_address.js b/test/gateway/anchor/set_co_anchor_address.js index 6148bf73..b538c14f 100644 --- a/test/gateway/anchor/set_co_anchor_address.js +++ b/test/gateway/anchor/set_co_anchor_address.js @@ -31,6 +31,7 @@ contract('Anchor.setCoAnchorAddress()', function (accounts) { let remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager, anchor, owner, @@ -44,6 +45,7 @@ contract('Anchor.setCoAnchorAddress()', function (accounts) { remoteChainId = new BN(1410); blockHeight = new BN(5); stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); coAnchorAddress = accounts[6]; @@ -51,6 +53,7 @@ contract('Anchor.setCoAnchorAddress()', function (accounts) { remoteChainId, blockHeight, stateRoot, + maxNumberOfStateRoots, membersManager.address, ); From 667c5fdff8aa9d535d122c67d68b87ba718c9b9d Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 19 Dec 2018 16:47:07 +0530 Subject: [PATCH 09/15] Deleting safe core unit test-cases --- test/gateway/safe_core/commit_state_root.js | 242 ------------------ test/gateway/safe_core/constructor.js | 121 --------- .../get_latest_state_root_block_height.js | 87 ------- test/gateway/safe_core/get_remote_chainId.js | 63 ----- test/gateway/safe_core/get_state_root.js | 105 -------- test/gateway/safe_core/set_co_core_address.js | 120 --------- 6 files changed, 738 deletions(-) delete mode 100644 test/gateway/safe_core/commit_state_root.js delete mode 100644 test/gateway/safe_core/constructor.js delete mode 100644 test/gateway/safe_core/get_latest_state_root_block_height.js delete mode 100644 test/gateway/safe_core/get_remote_chainId.js delete mode 100644 test/gateway/safe_core/get_state_root.js delete mode 100644 test/gateway/safe_core/set_co_core_address.js diff --git a/test/gateway/safe_core/commit_state_root.js b/test/gateway/safe_core/commit_state_root.js deleted file mode 100644 index 2b793b03..00000000 --- a/test/gateway/safe_core/commit_state_root.js +++ /dev/null @@ -1,242 +0,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. -// -// ---------------------------------------------------------------------------- -// -// http://www.simpletoken.org/ -// -// ---------------------------------------------------------------------------- - -const SafeCore = artifacts.require("./SafeCore.sol"); -const MockMembersManager = artifacts.require('MockMembersManager.sol'); -const web3 = require('../../test_lib/web3.js'); -const BN = require('bn.js'); -const Utils = require('../../../test/test_lib/utils'); -const EventDecoder = require('../../test_lib/event_decoder.js'); - -const zeroBytes = - "0x0000000000000000000000000000000000000000000000000000000000000000"; - -contract('SafeCore.commitStateRoot()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - safeCore, - owner, - worker; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = await MockMembersManager.new(owner, worker); - - safeCore = await SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager.address, - ); - - stateRoot = web3.utils.sha3("dummy_state_root_1"); - - }); - - it('should fail when state root is zero', async () => { - - stateRoot = zeroBytes; - blockHeight = blockHeight.addn(1); - - await Utils.expectRevert( - safeCore.commitStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ), - 'State root must not be zero.', - ); - - }); - - it('should fail when block height is less than the latest committed ' + - 'state root\'s block height', async () => { - - 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.', - ); - - }); - - it('should fail when block height is equal to the latest committed ' + - '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.', - ); - - }); - - it('should fail when caller is not worker address', async () => { - - blockHeight = blockHeight.addn(1); - - await Utils.expectRevert( - safeCore.commitStateRoot( - blockHeight, - stateRoot, - { from: accounts[6] }, - ), - 'Only whitelisted workers are allowed to call this method.', - ); - - }); - - it('should pass with correct params', async () => { - - blockHeight = blockHeight.addn(1); - - let result = await safeCore.commitStateRoot.call( - blockHeight, - stateRoot, - { from: worker }, - ); - - assert.strictEqual( - result, - true, - 'Return value of commitStateRoot must be true.', - ); - - await safeCore.commitStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ); - - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - }); - - it('should emit `StateRootAvailable` event', async () => { - - blockHeight = blockHeight.addn(1); - - let tx = await safeCore.commitStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ); - - let event = EventDecoder.getEvents(tx, safeCore); - - assert.isDefined( - event.StateRootAvailable, - 'Event `StateRootAvailable` must be emitted.', - ); - - let eventData = event.StateRootAvailable; - - assert.strictEqual( - eventData._stateRoot, - stateRoot, - `The _stateRoot value in the event should be equal to ${stateRoot}` - ); - - assert.strictEqual( - blockHeight.eq(eventData._blockHeight), - true, - `The _blockHeight in the event should be equal to ${blockHeight}` - ); - - }); - - it('should store only the given number of max store roots', async () => { - /* - * It should store the given state roots and they should be - * available for querying afterwards. After the max number of state - * roots has been exceeded, the old state roots should no longer be - * available. - */ - let iterations = maxNumberOfStateRoots.muln(2).toNumber(); - for (let i = 0; i < iterations; i++) { - blockHeight = blockHeight.addn(1); - await safeCore.commitStateRoot( - blockHeight, - stateRoot, - { 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( - prunedBlockHeight, - ); - assert.strictEqual( - storedStateRoot, - '0x0000000000000000000000000000000000000000000000000000000000000000', - 'There should not be any state root stored at a ' + - 'pruned height. It should have been reset by now.', - ); - - /* - * The state root that is one block younger than the pruned - * one should still be available. - */ - let existingBlockHeight = prunedBlockHeight.addn(1); - storedStateRoot = await safeCore.getStateRoot.call( - existingBlockHeight, - ); - assert.strictEqual( - storedStateRoot, - stateRoot, - 'The stored state root should still exist.', - ); - } - } - }); - -}); diff --git a/test/gateway/safe_core/constructor.js b/test/gateway/safe_core/constructor.js deleted file mode 100644 index e9f7f916..00000000 --- a/test/gateway/safe_core/constructor.js +++ /dev/null @@ -1,121 +0,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. -// -// ---------------------------------------------------------------------------- -// -// http://www.simpletoken.org/ -// -// ---------------------------------------------------------------------------- - -const SafeCore = artifacts.require("./SafeCore.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) { - - let remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - safeCore; - - beforeEach(async function () { - - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = accounts[1]; - - }); - - it('should fail when remote chain id is zero', async () => { - - remoteChainId = new BN(0); - - await Utils.expectRevert( - SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - ), - 'Remote chain Id must not be 0.', - ); - - }); - - it('should fail when members manager address is zero', async () => { - - membersManager = NullAddress; - - await Utils.expectRevert( - SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - ), - 'MembersManager contract address must not be zero.', - ); - - }); - - it('should pass with correct params', async () => { - - safeCore = await SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - ); - - let chainId = await safeCore.getRemoteChainId.call(); - assert.strictEqual( - remoteChainId.eq(chainId), - true, - `Remote chain id from the contract must be ${remoteChainId}.`, - ); - - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - let membersManagerAddress = await safeCore.membersManager.call(); - assert.strictEqual( - membersManagerAddress, - membersManager, - `Members manager address from the contract must be ${membersManager}.`, - ); - - }); - -}); diff --git a/test/gateway/safe_core/get_latest_state_root_block_height.js b/test/gateway/safe_core/get_latest_state_root_block_height.js deleted file mode 100644 index 530a2de8..00000000 --- a/test/gateway/safe_core/get_latest_state_root_block_height.js +++ /dev/null @@ -1,87 +0,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. -// -// ---------------------------------------------------------------------------- -// -// http://www.simpletoken.org/ -// -// ---------------------------------------------------------------------------- - -const SafeCore = artifacts.require("./SafeCore.sol"); -const MockMembersManager = artifacts.require('MockMembersManager.sol'); -const web3 = require('../../test_lib/web3.js'); -const BN = require('bn.js'); - -contract('SafeCore.getLatestStateRootBlockHeight()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - safeCore, - owner, - worker; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = await MockMembersManager.new(owner, worker); - - safeCore = await SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager.address, - ); - - }); - - it('should return the state root that was set while deployment', async () => { - - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - }); - - it('should return the latest committed state root block height', async () => { - - blockHeight = blockHeight.addn(50000); - - await safeCore.commitStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ); - - let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - }); - -}); diff --git a/test/gateway/safe_core/get_remote_chainId.js b/test/gateway/safe_core/get_remote_chainId.js deleted file mode 100644 index bacc542e..00000000 --- a/test/gateway/safe_core/get_remote_chainId.js +++ /dev/null @@ -1,63 +0,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. -// -// ---------------------------------------------------------------------------- -// -// http://www.simpletoken.org/ -// -// ---------------------------------------------------------------------------- - -const SafeCore = artifacts.require("./SafeCore.sol"); -const web3 = require('../../test_lib/web3.js'); -const BN = require('bn.js'); - -contract('SafeCore.getRemoteChainId()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - safeCore; - - beforeEach(async function () { - - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = accounts[1]; - - safeCore = await SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - ); - - }); - - it('should return correct remote chain id', async () => { - - let chainId = await safeCore.getRemoteChainId.call(); - assert.strictEqual( - remoteChainId.eq(chainId), - true, - `Remote chain id from the contract must be ${remoteChainId}.`, - ); - - }); - -}); diff --git a/test/gateway/safe_core/get_state_root.js b/test/gateway/safe_core/get_state_root.js deleted file mode 100644 index a4c8b1f9..00000000 --- a/test/gateway/safe_core/get_state_root.js +++ /dev/null @@ -1,105 +0,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. -// -// ---------------------------------------------------------------------------- -// -// http://www.simpletoken.org/ -// -// ---------------------------------------------------------------------------- - -const SafeCore = artifacts.require("./SafeCore.sol"); -const MockMembersManager = artifacts.require('MockMembersManager.sol'); -const web3 = require('../../test_lib/web3.js'); -const BN = require('bn.js'); - -const zeroBytes = - "0x0000000000000000000000000000000000000000000000000000000000000000"; - -contract('SafeCore.getStateRoot()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - safeCore, - owner, - worker; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = await MockMembersManager.new(owner, worker); - - safeCore = await SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager.address, - ); - - }); - - 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}.`, - ); - - }); - - it('should return the zero bytes for non committed block heights', async () => { - - blockHeight = blockHeight.addn(500); - - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - zeroBytes, - `Latest state root from the contract must be ${zeroBytes}.`, - ); - - }); - - it('should return the latest committed state root', async () => { - - blockHeight = blockHeight.addn(50000); - stateRoot = web3.utils.sha3("dummy_state_root_1"); - - await safeCore.commitStateRoot( - blockHeight, - stateRoot, - { from: worker }, - ); - - let latestStateRoot = await safeCore.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - }); - -}); diff --git a/test/gateway/safe_core/set_co_core_address.js b/test/gateway/safe_core/set_co_core_address.js deleted file mode 100644 index 6356ae80..00000000 --- a/test/gateway/safe_core/set_co_core_address.js +++ /dev/null @@ -1,120 +0,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. -// -// ---------------------------------------------------------------------------- -// -// http://www.simpletoken.org/ -// -// ---------------------------------------------------------------------------- - -const SafeCore = artifacts.require("./SafeCore.sol"); -const MockMembersManager = artifacts.require('MockMembersManager.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.setCoCoreAddress()', function (accounts) { - - let remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - safeCore, - owner, - worker, - coCoreAddress; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = await MockMembersManager.new(owner, worker); - coCoreAddress = accounts[6]; - - safeCore = await SafeCore.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager.address, - ); - - }); - - it('should fail when coCore address is zero', async () => { - - coCoreAddress = NullAddress; - - await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: owner }), - 'Co-Core address must not be 0.', - ); - - }); - - it('should fail when caller is not organisation owner', async () => { - - let notOwner = accounts[7]; - - await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: notOwner }), - 'Only the organization is allowed to call this method.', - ); - - }); - - it('should pass with correct params', async () => { - - let result = await safeCore.setCoCoreAddress.call( - coCoreAddress, - { from: owner }, - ); - - assert.strictEqual( - result, - true, - 'Return value of setCoCoreAddress must be true.', - ); - - await safeCore.setCoCoreAddress(coCoreAddress, { from: owner }); - - let coCore = await safeCore.coCore.call(); - - assert.strictEqual( - coCore, - coCoreAddress, - `CoCore address must be equal to ${coCoreAddress}.`, - ); - - }); - - it('should fail to set coCore address if it\'s already set', async () => { - - await safeCore.setCoCoreAddress(coCoreAddress, { from: owner }); - - await Utils.expectRevert( - safeCore.setCoCoreAddress(coCoreAddress, { from: owner }), - 'Co-Core has already been set and cannot be updated.', - ); - - }); - -}); From 7626721faf00240ec1c072839ece19c42b205961 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 19 Dec 2018 16:58:35 +0530 Subject: [PATCH 10/15] Renamed of variable to mockAnchor from mockSafeCore --- test/gateway/anchor/anchor_state_root.js | 312 +++++++++--------- test/gateway/anchor/constructor.js | 2 +- .../get_latest_state_root_block_height.js | 110 +++--- test/gateway/anchor/get_remote_chainId.js | 54 +-- test/gateway/anchor/get_state_root.js | 140 ++++---- test/gateway/anchor/set_co_anchor_address.js | 176 +++++----- test/gateway/eip20_cogateway/progress_mint.js | 6 +- 7 files changed, 400 insertions(+), 400 deletions(-) diff --git a/test/gateway/anchor/anchor_state_root.js b/test/gateway/anchor/anchor_state_root.js index c878f9cf..74a83073 100644 --- a/test/gateway/anchor/anchor_state_root.js +++ b/test/gateway/anchor/anchor_state_root.js @@ -26,172 +26,172 @@ const Utils = require('../../../test/test_lib/utils'); const EventDecoder = require('../../test_lib/event_decoder.js'); const zeroBytes = - "0x0000000000000000000000000000000000000000000000000000000000000000"; + "0x0000000000000000000000000000000000000000000000000000000000000000"; contract('Anchor.anchorStateRoot()', function (accounts) { + + let remoteChainId, + blockHeight, + stateRoot, + membersManager, + anchor, + owner, + worker, + maxNumberOfStateRoots; + + beforeEach(async function () { + + owner = accounts[2]; + worker = accounts[3]; + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); + membersManager = await MockMembersManager.new(owner, worker); + + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager.address, + ); + + stateRoot = web3.utils.sha3("dummy_state_root_1"); + + }); + + it('should fail when state root is zero', async () => { + + stateRoot = zeroBytes; + blockHeight = blockHeight.addn(1); - let remoteChainId, + await Utils.expectRevert( + anchor.anchorStateRoot( blockHeight, stateRoot, - membersManager, - anchor, - owner, - worker, - maxNumberOfStateRoots; + {from: worker}, + ), + 'State root must not be zero.', + ); - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = await MockMembersManager.new(owner, worker); - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager.address, - ); - - stateRoot = web3.utils.sha3("dummy_state_root_1"); - - }); + }); + + it('should fail when block height is less than the latest anchored ' + + 'state root\'s block height', async () => { - it('should fail when state root is zero', async () => { - - stateRoot = zeroBytes; - blockHeight = blockHeight.addn(1); - - await Utils.expectRevert( - anchor.anchorStateRoot( - blockHeight, - stateRoot, - {from: worker}, - ), - 'State root must not be zero.', - ); - - }); + blockHeight = blockHeight.subn(1); - it('should fail when block height is less than the latest anchored ' + - 'state root\'s block height', async () => { - - blockHeight = blockHeight.subn(1); - - await Utils.expectRevert( - anchor.anchorStateRoot( - blockHeight, - stateRoot, - {from: worker}, - ), - 'Given block height is lower or equal to highest anchored 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 anchored ' + - 'state root\'s block height', async () => { - - 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 anchored ' + + 'state root\'s block height', async () => { - it('should fail when caller is not worker address', async () => { - - blockHeight = blockHeight.addn(1); - - await Utils.expectRevert( - anchor.anchorStateRoot( - blockHeight, - stateRoot, - {from: accounts[6]}, - ), - 'Only whitelisted workers are allowed to call this method.', - ); - - }); + 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 pass with correct params', async () => { - - blockHeight = blockHeight.addn(1); - - let result = await anchor.anchorStateRoot.call( - blockHeight, - stateRoot, - {from: worker}, - ); - - assert.strictEqual( - result, - true, - 'Return value of anchorStateRoot must be true.', - ); - - await anchor.anchorStateRoot( - blockHeight, - stateRoot, - {from: worker}, - ); - - let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - let latestStateRoot = await anchor.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - }); + }); + + it('should fail when caller is not worker address', async () => { - it('should emit `StateRootAvailable` event', async () => { - - blockHeight = blockHeight.addn(1); - - let tx = await anchor.anchorStateRoot( - blockHeight, - stateRoot, - {from: worker}, - ); - - let event = EventDecoder.getEvents(tx, anchor); - - assert.isDefined( - event.StateRootAvailable, - 'Event `StateRootAvailable` must be emitted.', - ); - - let eventData = event.StateRootAvailable; - - assert.strictEqual( - eventData._stateRoot, - stateRoot, - `The _stateRoot value in the event should be equal to ${stateRoot}` - ); - - assert.strictEqual( - blockHeight.eq(eventData._blockHeight), - true, - `The _blockHeight in the event should be equal to ${blockHeight}` - ); - - }); + blockHeight = blockHeight.addn(1); + + await Utils.expectRevert( + anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: accounts[6]}, + ), + 'Only whitelisted workers are allowed to call this method.', + ); + + }); + + it('should pass with correct params', async () => { + + blockHeight = blockHeight.addn(1); + + let result = await anchor.anchorStateRoot.call( + blockHeight, + stateRoot, + {from: worker}, + ); + + assert.strictEqual( + result, + true, + 'Return value of anchorStateRoot must be true.', + ); + + await anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ); + + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); + assert.strictEqual( + blockHeight.eq(latestBlockHeight), + true, + `Latest block height from the contract must be ${blockHeight}.`, + ); + + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + stateRoot, + `Latest state root from the contract must be ${stateRoot}.`, + ); + + }); + + it('should emit `StateRootAvailable` event', async () => { + + blockHeight = blockHeight.addn(1); + + let tx = await anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ); + + let event = EventDecoder.getEvents(tx, anchor); + + assert.isDefined( + event.StateRootAvailable, + 'Event `StateRootAvailable` must be emitted.', + ); + + let eventData = event.StateRootAvailable; + + assert.strictEqual( + eventData._stateRoot, + stateRoot, + `The _stateRoot value in the event should be equal to ${stateRoot}` + ); + + assert.strictEqual( + blockHeight.eq(eventData._blockHeight), + true, + `The _blockHeight in the event should be equal to ${blockHeight}` + ); + + }); it('should store only the given number of max store roots', async () => { /* @@ -206,7 +206,7 @@ contract('Anchor.anchorStateRoot()', function (accounts) { await anchor.anchorStateRoot( blockHeight, stateRoot, - { from: worker }, + {from: worker}, ); // Check that the older state root has been deleted when i > max state roots. @@ -238,5 +238,5 @@ contract('Anchor.anchorStateRoot()', function (accounts) { } } }); - + }); diff --git a/test/gateway/anchor/constructor.js b/test/gateway/anchor/constructor.js index e1f9cb52..ea5d641d 100644 --- a/test/gateway/anchor/constructor.js +++ b/test/gateway/anchor/constructor.js @@ -32,7 +32,7 @@ contract('Anchor.constructor()', function (accounts) { stateRoot, maxNumberOfStateRoots, membersManager, - safeCore; + anchor; beforeEach(async function () { diff --git a/test/gateway/anchor/get_latest_state_root_block_height.js b/test/gateway/anchor/get_latest_state_root_block_height.js index 8a408bda..da07e7d7 100644 --- a/test/gateway/anchor/get_latest_state_root_block_height.js +++ b/test/gateway/anchor/get_latest_state_root_block_height.js @@ -24,64 +24,64 @@ const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { + + let remoteChainId, + blockHeight, + stateRoot, + membersManager, + maxNumberOfStateRoots, + anchor, + owner, + worker; + + beforeEach(async function () { - let remoteChainId, - blockHeight, - stateRoot, - membersManager, - maxNumberOfStateRoots, - anchor, - owner, - worker; + owner = accounts[2]; + worker = accounts[3]; + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); + membersManager = await MockMembersManager.new(owner, worker); - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = await MockMembersManager.new(owner, worker); - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager.address, - ); - - }); + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager.address, + ); - it('should return the state root that was set while deployment', async () => { - - let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - }); + }); + + it('should return the state root that was set while deployment', async () => { - it('should return the latest anchored state root block height', async () => { - - blockHeight = blockHeight.addn(50000); - - await anchor.anchorStateRoot( - blockHeight, - stateRoot, - {from: worker}, - ); - - let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - }); + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); + assert.strictEqual( + blockHeight.eq(latestBlockHeight), + true, + `Latest block height from the contract must be ${blockHeight}.`, + ); + }); + + it('should return the latest anchored state root block height', async () => { + + blockHeight = blockHeight.addn(50000); + + await anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ); + + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); + assert.strictEqual( + blockHeight.eq(latestBlockHeight), + true, + `Latest block height from the contract must be ${blockHeight}.`, + ); + + }); + }); diff --git a/test/gateway/anchor/get_remote_chainId.js b/test/gateway/anchor/get_remote_chainId.js index f8ec5555..5698e776 100644 --- a/test/gateway/anchor/get_remote_chainId.js +++ b/test/gateway/anchor/get_remote_chainId.js @@ -30,34 +30,34 @@ contract('Anchor.getRemoteChainId()', function (accounts) { maxNumberOfStateRoots, membersManager, anchor; + + beforeEach(async function () { + + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); + membersManager = accounts[1]; - beforeEach(async function () { - - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = accounts[1]; - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - ); - - }); + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager, + ); - it('should return correct remote chain id', async () => { - - let chainId = await anchor.getRemoteChainId.call(); - assert.strictEqual( - remoteChainId.eq(chainId), - true, - `Remote chain id from the contract must be ${remoteChainId}.`, - ); - - }); + }); + + it('should return correct remote chain id', async () => { + let chainId = await anchor.getRemoteChainId.call(); + assert.strictEqual( + remoteChainId.eq(chainId), + true, + `Remote chain id from the contract must be ${remoteChainId}.`, + ); + + }); + }); diff --git a/test/gateway/anchor/get_state_root.js b/test/gateway/anchor/get_state_root.js index 06e6f4e2..0ad8c4a1 100644 --- a/test/gateway/anchor/get_state_root.js +++ b/test/gateway/anchor/get_state_root.js @@ -24,82 +24,82 @@ const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); const zeroBytes = - "0x0000000000000000000000000000000000000000000000000000000000000000"; + "0x0000000000000000000000000000000000000000000000000000000000000000"; contract('Anchor.getStateRoot()', function (accounts) { + + let remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager, + anchor, + owner, + worker; + + beforeEach(async function () { - let remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - anchor, - owner, - worker; + owner = accounts[2]; + worker = accounts[3]; + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); + membersManager = await MockMembersManager.new(owner, worker); - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = await MockMembersManager.new(owner, worker); - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager.address, - ); - - }); + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager.address, + ); - it('should return the latest state root block height that was set ' + - 'while deployment', async () => { - - let latestStateRoot = await anchor.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - }); + }); + + it('should return the latest state root block height that was set ' + + 'while deployment', async () => { - it('should return the zero bytes for non anchored block heights', async () => { - - blockHeight = blockHeight.addn(500); - - let latestStateRoot = await anchor.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - zeroBytes, - `Latest state root from the contract must be ${zeroBytes}.`, - ); - - }); + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + stateRoot, + `Latest state root from the contract must be ${stateRoot}.`, + ); - it('should return the latest anchored state root', async () => { - - blockHeight = blockHeight.addn(50000); - stateRoot = web3.utils.sha3("dummy_state_root_1"); - - await anchor.anchorStateRoot( - blockHeight, - stateRoot, - {from: worker}, - ); - - 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 anchored block heights', async () => { + blockHeight = blockHeight.addn(500); + + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + zeroBytes, + `Latest state root from the contract must be ${zeroBytes}.`, + ); + + }); + + it('should return the latest anchored state root', async () => { + + blockHeight = blockHeight.addn(50000); + stateRoot = web3.utils.sha3("dummy_state_root_1"); + + await anchor.anchorStateRoot( + blockHeight, + stateRoot, + {from: worker}, + ); + + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + stateRoot, + `Latest state root from the contract must be ${stateRoot}.`, + ); + + }); + }); diff --git a/test/gateway/anchor/set_co_anchor_address.js b/test/gateway/anchor/set_co_anchor_address.js index b538c14f..71ee984d 100644 --- a/test/gateway/anchor/set_co_anchor_address.js +++ b/test/gateway/anchor/set_co_anchor_address.js @@ -27,94 +27,94 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('Anchor.setCoAnchorAddress()', function (accounts) { + + let remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager, + anchor, + owner, + worker, + coAnchorAddress; + + beforeEach(async function () { - let remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - anchor, - owner, - worker, - coAnchorAddress; - - beforeEach(async function () { - - owner = accounts[2]; - worker = accounts[3]; - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = await MockMembersManager.new(owner, worker); - coAnchorAddress = accounts[6]; - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager.address, - ); - - }); - - it('should fail when coAnchor address is zero', async () => { - - coAnchorAddress = NullAddress; - - await Utils.expectRevert( - anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), - "Co-Anchor address must not be 0.", - ); - - }); - - it('should fail when caller is not organisation owner', async () => { - - let notOwner = accounts[7]; - - await Utils.expectRevert( - anchor.setCoAnchorAddress(coAnchorAddress, {from: notOwner}), - 'Only the organization is allowed to call this method.', - ); - - }); - - it('should pass with correct params', async () => { - - let result = await anchor.setCoAnchorAddress.call( - coAnchorAddress, - {from: owner}, - ); - - assert.strictEqual( - result, - true, - 'Return value of setAnchorAddress must be true.', - ); - - await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); - - let coAnchor = await anchor.coAnchor.call(); - - assert.strictEqual( - coAnchor, - coAnchorAddress, - `CoAnchor address must be equal to ${coAnchorAddress}.`, - ); - - }); - - it('should fail to set coAnchor address if it\'s already set', async () => { - - await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); - - await Utils.expectRevert( - anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), - 'Co-Anchor has already been set and cannot be updated.', - ); - - }); + owner = accounts[2]; + worker = accounts[3]; + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); + membersManager = await MockMembersManager.new(owner, worker); + coAnchorAddress = accounts[6]; + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager.address, + ); + + }); + + it('should fail when coAnchor address is zero', async () => { + + coAnchorAddress = NullAddress; + + await Utils.expectRevert( + anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), + "Co-Anchor address must not be 0.", + ); + + }); + + it('should fail when caller is not organisation owner', async () => { + + let notOwner = accounts[7]; + + await Utils.expectRevert( + anchor.setCoAnchorAddress(coAnchorAddress, {from: notOwner}), + 'Only the organization is allowed to call this method.', + ); + + }); + + it('should pass with correct params', async () => { + + let result = await anchor.setCoAnchorAddress.call( + coAnchorAddress, + {from: owner}, + ); + + assert.strictEqual( + result, + true, + 'Return value of setAnchorAddress must be true.', + ); + + await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); + + let coAnchor = await anchor.coAnchor.call(); + + assert.strictEqual( + coAnchor, + coAnchorAddress, + `CoAnchor address must be equal to ${coAnchorAddress}.`, + ); + + }); + + it('should fail to set coAnchor address if it\'s already set', async () => { + + await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); + + await Utils.expectRevert( + anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), + 'Co-Anchor has already been set and cannot be updated.', + ); + + }); + }); diff --git a/test/gateway/eip20_cogateway/progress_mint.js b/test/gateway/eip20_cogateway/progress_mint.js index 99cefbfc..108d8669 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, + mockAnchor, membersManager, coGateway, testUtilityToken, @@ -57,7 +57,7 @@ async function _setup(accounts) { valueToken = accounts[0]; burner = accounts[10]; - mockSafeCore = accounts[11]; + mockAnchor = accounts[11]; membersManager = accounts[2]; coGateway = accounts[3]; owner = accounts[8]; @@ -112,7 +112,7 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { testEIP20CoGateway = await TestEIP20CoGateway.new( valueToken, testUtilityToken.address, - mockSafeCore, + mockAnchor, bountyAmount, membersManager, coGateway, From b7cbd2d88d379bf3eb6af17e47dbea3e6a77cb42 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 19 Dec 2018 21:32:38 +0530 Subject: [PATCH 11/15] Changes done for introducing generic name for variable name for state root interface --- contracts/StateRootInterface.sol | 4 +- contracts/gateway/EIP20CoGateway.sol | 6 +- contracts/gateway/EIP20Gateway.sol | 6 +- contracts/gateway/GatewayBase.sol | 12 ++-- contracts/lib/GatewayLib.sol | 4 +- contracts/test/TestEIP20CoGateway.sol | 6 +- contracts/test/TestEIP20Gateway.sol | 6 +- contracts/test/gateway/MockGatewayBase.sol | 8 +-- test/gateway/eip20_cogateway/constructor.js | 26 ++++----- test/gateway/eip20_cogateway/progress_mint.js | 6 +- test/gateway/eip20_cogateway/redeem.js | 6 +- .../gateway/eip20_gateway/activate_gateway.js | 4 +- test/gateway/eip20_gateway/constructor.js | 30 +++++----- .../eip20_gateway/deactivate_gateway.js | 4 +- test/gateway/gateway_base/construction.js | 58 +++++++++---------- test/gateway/gateway_base/prove_gateway.js | 4 +- test/gateway/helpers/helper.js | 10 ++-- 17 files changed, 100 insertions(+), 100 deletions(-) diff --git a/contracts/StateRootInterface.sol b/contracts/StateRootInterface.sol index 07d32779..07dddb88 100644 --- a/contracts/StateRootInterface.sol +++ b/contracts/StateRootInterface.sol @@ -24,9 +24,9 @@ pragma solidity ^0.5.0; interface StateRootInterface { /** - * @notice Gets the block number of latest anchored state root. + * @notice Gets the block number of latest committed state root. * - * @return uint256 Block height of the latest anchored state root. + * @return height_ Block height of the latest committed state root. */ function getLatestStateRootBlockHeight() external diff --git a/contracts/gateway/EIP20CoGateway.sol b/contracts/gateway/EIP20CoGateway.sol index 8efc8f3b..2a211aef 100644 --- a/contracts/gateway/EIP20CoGateway.sol +++ b/contracts/gateway/EIP20CoGateway.sol @@ -204,7 +204,7 @@ 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 _anchor Anchor contract address. + * @param _stateRootProvider State roots provider contract address. * @param _bounty The amount that facilitator stakes to initiate the stake * process. * @param _membersManager Address of a contract that manages workers. @@ -214,14 +214,14 @@ contract EIP20CoGateway is GatewayBase { constructor( address _valueToken, address _utilityToken, - StateRootInterface _anchor, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager, address _gateway, address payable _burner ) GatewayBase( - _anchor, + _stateRootProvider, _bounty, _membersManager ) diff --git a/contracts/gateway/EIP20Gateway.sol b/contracts/gateway/EIP20Gateway.sol index 935042b9..a728534b 100644 --- a/contracts/gateway/EIP20Gateway.sol +++ b/contracts/gateway/EIP20Gateway.sol @@ -231,7 +231,7 @@ contract EIP20Gateway is GatewayBase { * in auxiliary chain. * @param _baseToken The ERC20 token address that will be used for * staking bounty from the facilitators. - * @param _anchor Anchor contract address. + * @param _stateRootProvider State roots provider contract address. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. @@ -240,13 +240,13 @@ contract EIP20Gateway is GatewayBase { constructor( EIP20Interface _token, EIP20Interface _baseToken, - StateRootInterface _anchor, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager, address _burner ) GatewayBase( - _anchor, + _stateRootProvider, _bounty, _membersManager ) diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index 4950d56a..9d4301b0 100644 --- a/contracts/gateway/GatewayBase.sol +++ b/contracts/gateway/GatewayBase.sol @@ -75,7 +75,7 @@ contract GatewayBase is Organized { MessageBus.MessageBox messageBox; /** Address of anchor contract. */ - StateRootInterface public anchor; + StateRootInterface public stateRootProvider; /** Path to make Merkle account proof for Gateway/CoGateway contract. */ bytes public encodedGatewayPath; @@ -128,13 +128,13 @@ contract GatewayBase is Organized { /** * @notice Initialize the contract and set default values. * - * @param _anchor Anchor contract address. + * @param _stateRootProvider State roots provider contract address. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. */ constructor( - StateRootInterface _anchor, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager ) @@ -142,11 +142,11 @@ contract GatewayBase is Organized { public { require( - address(_anchor) != address(0), + address(_stateRootProvider) != address(0), "Anchor contract address must not be zero." ); - anchor = _anchor; + stateRootProvider = _stateRootProvider; bounty = _bounty; } @@ -192,7 +192,7 @@ contract GatewayBase is Organized { "Length of RLP parent nodes is 0" ); - bytes32 stateRoot = anchor.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 b1d2dd1d..f90b537b 100644 --- a/contracts/lib/GatewayLib.sol +++ b/contracts/lib/GatewayLib.sol @@ -126,8 +126,8 @@ library GatewayLib { ); /** - * Verify the remote OpenST contract against the anchored state - * root with the state trie Merkle proof + * Verify the remote OpenST contract against the committed state + * 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 6abe79b7..b32a95c3 100644 --- a/contracts/test/TestEIP20CoGateway.sol +++ b/contracts/test/TestEIP20CoGateway.sol @@ -39,7 +39,7 @@ 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 _anchor Anchor contract address. + * @param _stateRootProvider State roots provider contract address. * @param _bounty The amount that facilitator will stakes to initiate the * staking process. * @param _membersManager Address of a members manager contract. @@ -48,7 +48,7 @@ contract TestEIP20CoGateway is EIP20CoGateway { constructor( address _valueToken, address _utilityToken, - StateRootInterface _anchor, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager, address _gateway, @@ -57,7 +57,7 @@ contract TestEIP20CoGateway is EIP20CoGateway { EIP20CoGateway( _valueToken, _utilityToken, - _anchor, + _stateRootProvider, _bounty, _membersManager, _gateway, diff --git a/contracts/test/TestEIP20Gateway.sol b/contracts/test/TestEIP20Gateway.sol index 20d44f87..d396a6cf 100644 --- a/contracts/test/TestEIP20Gateway.sol +++ b/contracts/test/TestEIP20Gateway.sol @@ -36,7 +36,7 @@ contract TestEIP20Gateway is EIP20Gateway { * in auxiliary chain. * @param _baseToken The ERC20 token address that will be used for * staking bounty from the facilitators. - * @param _anchor Anchor contract address. + * @param _stateRootProvider State roots provider contract address. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. @@ -45,7 +45,7 @@ contract TestEIP20Gateway is EIP20Gateway { constructor( EIP20Interface _token, EIP20Interface _baseToken, - StateRootInterface _anchor, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager, address payable _burner @@ -53,7 +53,7 @@ contract TestEIP20Gateway is EIP20Gateway { EIP20Gateway( _token, _baseToken, - _anchor, + _stateRootProvider, _bounty, _membersManager, _burner diff --git a/contracts/test/gateway/MockGatewayBase.sol b/contracts/test/gateway/MockGatewayBase.sol index 6867624c..b68cf9bd 100644 --- a/contracts/test/gateway/MockGatewayBase.sol +++ b/contracts/test/gateway/MockGatewayBase.sol @@ -17,19 +17,19 @@ contract MockGatewayBase is GatewayBase { /** * @notice This is used for testing. * - * @param _anchor Anchor contract address. + * @param _stateRootProvider State roots provider contract address. * @param _bounty The amount that facilitator will stakes to initiate the * stake process. * @param _membersManager Address of a contract that manages workers. */ constructor( - StateRootInterface _anchor, + StateRootInterface _stateRootProvider, uint256 _bounty, IsMemberInterface _membersManager ) public GatewayBase( - _anchor, + _stateRootProvider, _bounty, _membersManager ) @@ -78,7 +78,7 @@ contract MockGatewayBase is GatewayBase { "Length of RLP parent nodes must not be 0." ); - bytes32 stateRoot = anchor.getStateRoot(_blockHeight); + bytes32 stateRoot = stateRootProvider.getStateRoot(_blockHeight); //State root should be present for the block height require( diff --git a/test/gateway/eip20_cogateway/constructor.js b/test/gateway/eip20_cogateway/constructor.js index e3a64d57..57895da5 100644 --- a/test/gateway/eip20_cogateway/constructor.js +++ b/test/gateway/eip20_cogateway/constructor.js @@ -30,15 +30,15 @@ const NullAddress = "0x0000000000000000000000000000000000000000"; contract('EIP20CoGateway.constructor() ', function (accounts) { - let valueToken, utilityToken, bountyAmount, anchorAddress, owner, worker, - membersManager, coGateway, gatewayAddress = accounts[6], + 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(); - anchorAddress = accounts[1]; + dummyStateRootProvider = accounts[1]; bountyAmount = new BN(100); owner = accounts[2]; @@ -51,7 +51,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueToken.address, utilityToken.address, - anchorAddress, + dummyStateRootProvider, bountyAmount, membersManager.address, gatewayAddress, @@ -69,7 +69,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueToken.address, utilityToken.address, - anchorAddress, + dummyStateRootProvider, bountyAmount, membersManager.address, gatewayAddress, @@ -91,10 +91,10 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { 'Invalid bounty token address from contract.' ); - let anchorAdd = await coGateway.anchor.call(); + let stateRootProviderAdd = await coGateway.stateRootProvider.call(); assert.strictEqual( - anchorAdd, - anchorAddress, + stateRootProviderAdd, + dummyStateRootProvider, 'Invalid anchor address from contract.' ); @@ -113,7 +113,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueTokenAddress, utilityToken.address, - anchorAddress, + dummyStateRootProvider, bountyAmount, membersManager.address, gatewayAddress, @@ -130,7 +130,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueToken.address, utilityTokenAddress, - anchorAddress, + dummyStateRootProvider, bountyAmount, membersManager.address, gatewayAddress, @@ -141,13 +141,13 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { }); it('should not deploy contract if anchor address is passed as zero.', async function () { - let anchorAddress = NullAddress; + let stateRootProviderAddress = NullAddress; await Utils.expectRevert( CoGateway.new( valueToken.address, utilityToken.address, - anchorAddress, + stateRootProviderAddress, bountyAmount, membersManager.address, gatewayAddress, @@ -165,7 +165,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { CoGateway.new( valueToken.address, utilityToken.address, - anchorAddress, + dummyStateRootProvider, bountyAmount, membersManager.address, gatewayAddress, diff --git a/test/gateway/eip20_cogateway/progress_mint.js b/test/gateway/eip20_cogateway/progress_mint.js index 108d8669..2c6529cc 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, - mockAnchor, + dummyStateRootProvider, membersManager, coGateway, testUtilityToken, @@ -57,7 +57,7 @@ async function _setup(accounts) { valueToken = accounts[0]; burner = accounts[10]; - mockAnchor = accounts[11]; + dummyStateRootProvider = accounts[11]; membersManager = accounts[2]; coGateway = accounts[3]; owner = accounts[8]; @@ -112,7 +112,7 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { testEIP20CoGateway = await TestEIP20CoGateway.new( valueToken, testUtilityToken.address, - mockAnchor, + dummyStateRootProvider, bountyAmount, membersManager, coGateway, diff --git a/test/gateway/eip20_cogateway/redeem.js b/test/gateway/eip20_cogateway/redeem.js index 25486a74..b8527a5d 100644 --- a/test/gateway/eip20_cogateway/redeem.js +++ b/test/gateway/eip20_cogateway/redeem.js @@ -30,7 +30,7 @@ const TestEIP20CoGateway = artifacts.require('TestEIP20CoGateway'), let testEIP20CoGateway, burner, valueToken, - mockAnchor, + dummyStateRootProvider, membersManager, gateway, utilityToken, @@ -50,7 +50,7 @@ let MessageStatusEnum = { async function _setup(accounts) { valueToken = accounts[0]; - mockAnchor = accounts[1]; + dummyStateRootProvider = accounts[1]; membersManager = accounts[2]; gateway = accounts[3]; owner = accounts[8]; @@ -63,7 +63,7 @@ async function _setup(accounts) { testEIP20CoGateway = await TestEIP20CoGateway.new( valueToken, utilityToken.address, - mockAnchor, + dummyStateRootProvider, bountyAmount, membersManager, gateway, diff --git a/test/gateway/eip20_gateway/activate_gateway.js b/test/gateway/eip20_gateway/activate_gateway.js index b6fe70a5..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], - anchorAddress = 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, - anchorAddress, + dummyStateRootProvider, bountyAmount, membersManager.address, burner diff --git a/test/gateway/eip20_gateway/constructor.js b/test/gateway/eip20_gateway/constructor.js index 85501def..5a6a3966 100644 --- a/test/gateway/eip20_gateway/constructor.js +++ b/test/gateway/eip20_gateway/constructor.js @@ -29,14 +29,14 @@ const NullAddress = "0x0000000000000000000000000000000000000000"; contract('EIP20Gateway.constructor() ', function (accounts) { - let mockToken, baseToken, bountyAmount, anchorAddress, membersManager, - gateway, owner, worker, burner = NullAddress; + let mockToken, baseToken, bountyAmount, dummyRootProviderAddress, + membersManager, gateway, owner, worker, burner = NullAddress; beforeEach(async function () { mockToken = await MockToken.new(); baseToken = await MockToken.new(); - anchorAddress = accounts[1]; + dummyRootProviderAddress = accounts[1]; bountyAmount = new BN(100); owner = accounts[2]; @@ -49,7 +49,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseToken.address, - anchorAddress, + dummyRootProviderAddress, bountyAmount, membersManager.address, burner @@ -66,7 +66,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseToken.address, - anchorAddress, + dummyRootProviderAddress, bountyAmount, membersManager.address, burner @@ -87,11 +87,11 @@ contract('EIP20Gateway.constructor() ', function (accounts) { "Invalid bounty token address from contract." ); - let anchorAdd = await gateway.anchor.call(); + let stateRootProviderAdd = await gateway.stateRootProvider.call(); assert.equal( - anchorAdd, - anchorAddress, - "Invalid anchor address from contract" + stateRootProviderAdd, + dummyRootProviderAddress, + "Invalid stateRootProvider address from contract" ); let bounty = await gateway.bounty.call(); @@ -121,7 +121,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken, baseToken.address, - anchorAddress, + dummyRootProviderAddress, bountyAmount, membersManager.address, burner @@ -137,7 +137,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseTokenAddress, - anchorAddress, + dummyRootProviderAddress, bountyAmount, membersManager.address, burner @@ -147,13 +147,13 @@ contract('EIP20Gateway.constructor() ', function (accounts) { }); it('should not deploy contract if anchor address is passed as zero.', async function () { - let anchorAddress = NullAddress; + let stateRootProvider = NullAddress; await Utils.expectRevert( Gateway.new( mockToken.address, baseToken.address, - anchorAddress, + stateRootProvider, bountyAmount, membersManager.address, burner @@ -170,7 +170,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseToken.address, - anchorAddress, + dummyRootProviderAddress, bountyAmount, membersManager, burner @@ -187,7 +187,7 @@ contract('EIP20Gateway.constructor() ', function (accounts) { Gateway.new( mockToken.address, baseToken.address, - anchorAddress, + dummyRootProviderAddress, bountyAmount, membersManager.address, burner diff --git a/test/gateway/eip20_gateway/deactivate_gateway.js b/test/gateway/eip20_gateway/deactivate_gateway.js index 90f6c439..9bccf789 100644 --- a/test/gateway/eip20_gateway/deactivate_gateway.js +++ b/test/gateway/eip20_gateway/deactivate_gateway.js @@ -18,7 +18,7 @@ contract('EIP20Gateway.deactivateGateway()', function (accounts) { let mockToken = accounts[0], baseToken = accounts[1], - anchorAddress = accounts[2], + dummyStateRootProvider = accounts[2], bountyAmount = new BN(100); membersManager = await MockMembersManager.new(owner, worker); @@ -26,7 +26,7 @@ contract('EIP20Gateway.deactivateGateway()', function (accounts) { gateway = await Gateway.new( mockToken, baseToken, - anchorAddress, + dummyStateRootProvider, bountyAmount, membersManager.address, burner diff --git a/test/gateway/gateway_base/construction.js b/test/gateway/gateway_base/construction.js index 155d624a..8f0cbe20 100644 --- a/test/gateway/gateway_base/construction.js +++ b/test/gateway/gateway_base/construction.js @@ -6,67 +6,67 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('GatewayBase.sol', function (accounts) { - + describe('Construction', async () => { - - let anchor, bounty, worker, membersManager; - + + let dummyStateRootProvider, bounty, worker, membersManager; + beforeEach(async function () { - + owner = accounts[2] , worker = accounts[3] - , anchor = accounts[0] + , dummyStateRootProvider = accounts[0] , bounty = new BN(100); - + membersManager = await MockMembersManager.new(owner, worker); }); - + it('should pass with right set of parameters', async function () { gatewayBaseInstance = await GatewayBase.new( - anchor, + dummyStateRootProvider, bounty, membersManager.address ); - + assert.strictEqual( - anchor, - await gatewayBaseInstance.anchor.call(), - "Anchor 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)); }); - + it('should pass with right set of parameters and zero bounty', async function () { - + bounty = new BN(0); - + gatewayBaseInstance = await GatewayBase.new( - anchor, + dummyStateRootProvider, bounty, membersManager.address ); - - assert.equal(anchor, await gatewayBaseInstance.anchor.call()); + + assert.equal(dummyStateRootProvider, await gatewayBaseInstance.stateRootProvider.call()); assert((await gatewayBaseInstance.bounty.call()).eq(bounty)); }); - - it('should fail if anchor address is zero', async function () { - - anchor = NullAddress; + + it('should fail if state root provider address is zero', async function () { + + let stateRootProvider = NullAddress; await Utils.expectRevert( - GatewayBase.new(anchor, bounty, membersManager.address), + GatewayBase.new(stateRootProvider, bounty, membersManager.address), "Anchor contract address must not be zero." ); - + }); - + it('should fail if worker manager address is not passed', async function () { - + await Utils.expectRevert( - GatewayBase.new(anchor, bounty, NullAddress), + GatewayBase.new(dummyStateRootProvider, bounty, NullAddress), "MembersManager contract address must not be zero." ); - + }); }); }); diff --git a/test/gateway/gateway_base/prove_gateway.js b/test/gateway/gateway_base/prove_gateway.js index 9282cd5e..a5de0938 100644 --- a/test/gateway/gateway_base/prove_gateway.js +++ b/test/gateway/gateway_base/prove_gateway.js @@ -44,7 +44,7 @@ contract('GatewayBase.sol', function (accounts) { let maxStateRoots = new BN(1000); let membersManager = await MockMembersManager.new(owner, worker); - let anchor = await MockAnchor.new( + let mockAnchor = await MockAnchor.new( 1, 0, stateRoot, @@ -53,7 +53,7 @@ contract('GatewayBase.sol', function (accounts) { ); gatewayBaseInstance = await GatewayBase.new( - anchor.address, + mockAnchor.address, bounty, membersManager.address ); diff --git a/test/gateway/helpers/helper.js b/test/gateway/helpers/helper.js index 717b6add..9ac2e833 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, - anchorAddress = params.anchor, + stateRootProviderAddress = params.stateRootProviderAddress, bountyAmount = params.bounty, organizationAddress = params.organization; @@ -27,7 +27,7 @@ GatewayHelper.prototype = { await utils.expectThrow(Gateway.new( valueTokenAddress, bountyToken, - anchorAddress, + stateRootProviderAddress, bountyAmount, organizationAddress )); @@ -35,7 +35,7 @@ GatewayHelper.prototype = { this.gateway = await Gateway.new( valueTokenAddress, bountyToken, - anchorAddress, + stateRootProviderAddress, bountyAmount, organizationAddress ); @@ -67,8 +67,8 @@ GatewayHelper.prototype = { let anchorAdd = await this.gateway.anchor.call(); assert.equal( anchorAdd, - anchorAddress, - "Invalid anchor address from contract" + stateRootProviderAddress, + "Invalid state root provider address from contract" ); let bounty = await this.gateway.bounty.call(); From 630bcf66458e340d2dec12bec29c9ef457d4d50d Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 19 Dec 2018 22:52:25 +0530 Subject: [PATCH 12/15] Changes done to update unit test cases,documentation for stateRootInterface variable in constructor and corrected js styling --- contracts/gateway/EIP20CoGateway.sol | 3 +- contracts/gateway/EIP20Gateway.sol | 3 +- contracts/gateway/GatewayBase.sol | 9 +- contracts/test/TestEIP20CoGateway.sol | 3 +- contracts/test/TestEIP20Gateway.sol | 3 +- contracts/test/gateway/MockGatewayBase.sol | 3 +- test/gateway/eip20_cogateway/constructor.js | 296 +++--- test/gateway/eip20_cogateway/progress_mint.js | 132 +-- test/gateway/eip20_cogateway/redeem.js | 962 +++++++++--------- test/gateway/eip20_gateway/constructor.js | 340 +++---- .../eip20_gateway/deactivate_gateway.js | 102 +- test/gateway/eip20_gateway/stake.js | 454 ++++----- test/gateway/gateway_base/bounty_change.js | 4 +- test/gateway/gateway_base/construction.js | 38 +- test/gateway/gateway_base/get_nonce.js | 4 +- 15 files changed, 1182 insertions(+), 1174 deletions(-) diff --git a/contracts/gateway/EIP20CoGateway.sol b/contracts/gateway/EIP20CoGateway.sol index 2a211aef..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 _stateRootProvider State roots provider 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. diff --git a/contracts/gateway/EIP20Gateway.sol b/contracts/gateway/EIP20Gateway.sol index a728534b..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 _stateRootProvider State roots provider 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. diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index 9d4301b0..c8b16dcc 100644 --- a/contracts/gateway/GatewayBase.sol +++ b/contracts/gateway/GatewayBase.sol @@ -74,7 +74,9 @@ contract GatewayBase is Organized { */ MessageBus.MessageBox messageBox; - /** Address of anchor contract. */ + /** Address of state root provider contract which implements + * StateRootInterface. + */ StateRootInterface public stateRootProvider; /** Path to make Merkle account proof for Gateway/CoGateway contract. */ @@ -128,7 +130,8 @@ contract GatewayBase is Organized { /** * @notice Initialize the contract and set default values. * - * @param _stateRootProvider State roots provider 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. @@ -143,7 +146,7 @@ contract GatewayBase is Organized { { require( address(_stateRootProvider) != address(0), - "Anchor contract address must not be zero." + "State root provider contract address must not be zero." ); stateRootProvider = _stateRootProvider; diff --git a/contracts/test/TestEIP20CoGateway.sol b/contracts/test/TestEIP20CoGateway.sol index b32a95c3..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 _stateRootProvider State roots provider 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. diff --git a/contracts/test/TestEIP20Gateway.sol b/contracts/test/TestEIP20Gateway.sol index d396a6cf..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 _stateRootProvider State roots provider 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. diff --git a/contracts/test/gateway/MockGatewayBase.sol b/contracts/test/gateway/MockGatewayBase.sol index b68cf9bd..3b9d950b 100644 --- a/contracts/test/gateway/MockGatewayBase.sol +++ b/contracts/test/gateway/MockGatewayBase.sol @@ -17,7 +17,8 @@ contract MockGatewayBase is GatewayBase { /** * @notice This is used for testing. * - * @param _stateRootProvider State roots provider 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. diff --git a/test/gateway/eip20_cogateway/constructor.js b/test/gateway/eip20_cogateway/constructor.js index 57895da5..686ca0b5 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, 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 anchor 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 anchor 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 - ), - 'Anchor 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.' - ); - }); + 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 anchor 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 2c6529cc..6bac5bc1 100644 --- a/test/gateway/eip20_cogateway/progress_mint.js +++ b/test/gateway/eip20_cogateway/progress_mint.js @@ -54,7 +54,7 @@ let MessageStatusEnum = { }; async function _setup(accounts) { - + valueToken = accounts[0]; burner = accounts[10]; dummyStateRootProvider = accounts[11]; @@ -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, @@ -118,9 +118,9 @@ contract('EIP20CoGateway.progressMint() ', function (accounts) { 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 b8527a5d..86d3baa4 100644 --- a/test/gateway/eip20_cogateway/redeem.js +++ b/test/gateway/eip20_cogateway/redeem.js @@ -19,497 +19,497 @@ // ---------------------------------------------------------------------------- const TestEIP20CoGateway = artifacts.require('TestEIP20CoGateway'), - MockAnchor = artifacts.require('MockAnchor'), - 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"); + MockAnchor = artifacts.require('MockAnchor'), + 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, + burner, + valueToken, + dummyStateRootProvider, + membersManager, + gateway, + utilityToken, + bountyAmount, + owner, + redeemer, + redeemerBalance; + +let MessageStatusEnum = { + Undeclared: 0, + Declared: 1, + Progressed: 2, + DeclaredRevocation: 3, + Revoked: 4 +}; + +async function _setup(accounts) { + + 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, - utilityToken, - bountyAmount, - owner, - redeemer, - redeemerBalance; + burner + ); -let MessageStatusEnum = { - Undeclared: 0, - Declared: 1, - Progressed: 2, - DeclaredRevocation: 3, - Revoked: 4 -}; + await utilityToken.transfer(redeemer, redeemerBalance, {from: owner}); + + await utilityToken.approve( + testEIP20CoGateway.address, + redeemerBalance, + {from: redeemer}, + ); -async function _setup(accounts) { - - 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/constructor.js b/test/gateway/eip20_gateway/constructor.js index 5a6a3966..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, 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 anchor address is passed as zero.', async function () { - let stateRootProvider = NullAddress; - - await Utils.expectRevert( - Gateway.new( - mockToken.address, - baseToken.address, - stateRootProvider, - bountyAmount, - membersManager.address, - burner - ), - "Anchor 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." - ); - }); + 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 9bccf789..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], - dummyStateRootProvider = 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, - dummyStateRootProvider, - 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 fe900461..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], //anchor 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], //anchor 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 205cb809..4f2e4f21 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 anchor = accounts[0] + let dummyStateRootProviderAddress = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - anchor, + dummyStateRootProviderAddress, bounty, membersManager.address ); diff --git a/test/gateway/gateway_base/construction.js b/test/gateway/gateway_base/construction.js index 8f0cbe20..15621fc5 100644 --- a/test/gateway/gateway_base/construction.js +++ b/test/gateway/gateway_base/construction.js @@ -6,28 +6,28 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('GatewayBase.sol', function (accounts) { - + describe('Construction', async () => { - + let dummyStateRootProvider, bounty, worker, membersManager; - + beforeEach(async function () { - + owner = accounts[2] , worker = accounts[3] , dummyStateRootProvider = accounts[0] , bounty = new BN(100); - + membersManager = await MockMembersManager.new(owner, worker); }); - + it('should pass with right set of parameters', async function () { gatewayBaseInstance = await GatewayBase.new( dummyStateRootProvider, bounty, membersManager.address ); - + assert.strictEqual( dummyStateRootProvider, await gatewayBaseInstance.stateRootProvider.call(), @@ -35,38 +35,38 @@ contract('GatewayBase.sol', function (accounts) { ); assert((await gatewayBaseInstance.bounty.call()).eq(bounty)); }); - + it('should pass with right set of parameters and zero bounty', async function () { - + bounty = new BN(0); - + gatewayBaseInstance = await GatewayBase.new( dummyStateRootProvider, bounty, membersManager.address ); - + assert.equal(dummyStateRootProvider, await gatewayBaseInstance.stateRootProvider.call()); assert((await gatewayBaseInstance.bounty.call()).eq(bounty)); }); - - it('should fail if state root provider address is zero', async function () { - + + it('should fail if state root provider contract address is zero', async function () { + let stateRootProvider = NullAddress; await Utils.expectRevert( GatewayBase.new(stateRootProvider, bounty, membersManager.address), - "Anchor contract address must not be zero." + "State root provider contract address must not be zero." ); - + }); - + it('should fail if worker manager address is not passed', async function () { - + await Utils.expectRevert( 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 ef57d0bb..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] - , anchor = accounts[0] + , dummyStateRootProviderAddress = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - anchor, + dummyStateRootProviderAddress, bounty, membersManager.address ); From 34030e3873e6fe470dd96dfb34fc215e97107f60 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 19 Dec 2018 23:12:38 +0530 Subject: [PATCH 13/15] Changes done to update unit test cases messages and corrected js styling --- contracts/gateway/GatewayBase.sol | 4 +- test/gateway/anchor/anchor_state_root.js | 84 ++++----- test/gateway/anchor/constructor.js | 172 +++++++++--------- .../get_latest_state_root_block_height.js | 28 +-- test/gateway/anchor/get_remote_chainId.js | 18 +- test/gateway/anchor/get_state_root.js | 36 ++-- test/gateway/anchor/set_co_anchor_address.js | 50 ++--- test/gateway/eip20_cogateway/constructor.js | 2 +- test/gateway/gateway_base/bounty_change.js | 4 +- test/gateway/helpers/helper.js | 4 +- 10 files changed, 201 insertions(+), 201 deletions(-) diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index c8b16dcc..ceceeae6 100644 --- a/contracts/gateway/GatewayBase.sol +++ b/contracts/gateway/GatewayBase.sol @@ -74,8 +74,8 @@ contract GatewayBase is Organized { */ MessageBus.MessageBox messageBox; - /** Address of state root provider contract which implements - * StateRootInterface. + /** + * Address of contract which implements StateRootInterface. */ StateRootInterface public stateRootProvider; diff --git a/test/gateway/anchor/anchor_state_root.js b/test/gateway/anchor/anchor_state_root.js index 74a83073..fceec881 100644 --- a/test/gateway/anchor/anchor_state_root.js +++ b/test/gateway/anchor/anchor_state_root.js @@ -29,7 +29,7 @@ const zeroBytes = "0x0000000000000000000000000000000000000000000000000000000000000000"; contract('Anchor.anchorStateRoot()', function (accounts) { - + let remoteChainId, blockHeight, stateRoot, @@ -38,9 +38,9 @@ contract('Anchor.anchorStateRoot()', function (accounts) { owner, worker, maxNumberOfStateRoots; - + beforeEach(async function () { - + owner = accounts[2]; worker = accounts[3]; remoteChainId = new BN(1410); @@ -48,7 +48,7 @@ contract('Anchor.anchorStateRoot()', function (accounts) { stateRoot = web3.utils.sha3("dummy_state_root"); maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); - + anchor = await Anchor.new( remoteChainId, blockHeight, @@ -56,16 +56,16 @@ contract('Anchor.anchorStateRoot()', function (accounts) { maxNumberOfStateRoots, membersManager.address, ); - + stateRoot = web3.utils.sha3("dummy_state_root_1"); - + }); - + it('should fail when state root is zero', async () => { - + stateRoot = zeroBytes; blockHeight = blockHeight.addn(1); - + await Utils.expectRevert( anchor.anchorStateRoot( blockHeight, @@ -74,14 +74,14 @@ contract('Anchor.anchorStateRoot()', function (accounts) { ), 'State root must not be zero.', ); - + }); - + it('should fail when block height is less than the latest anchored ' + 'state root\'s block height', async () => { - + blockHeight = blockHeight.subn(1); - + await Utils.expectRevert( anchor.anchorStateRoot( blockHeight, @@ -90,12 +90,12 @@ contract('Anchor.anchorStateRoot()', function (accounts) { ), '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 anchored ' + 'state root\'s block height', async () => { - + await Utils.expectRevert( anchor.anchorStateRoot( blockHeight, @@ -104,13 +104,13 @@ contract('Anchor.anchorStateRoot()', function (accounts) { ), '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( anchor.anchorStateRoot( blockHeight, @@ -119,80 +119,80 @@ contract('Anchor.anchorStateRoot()', function (accounts) { ), 'Only whitelisted workers are allowed to call this method.', ); - + }); - + it('should pass with correct params', async () => { - + blockHeight = blockHeight.addn(1); - + let result = await anchor.anchorStateRoot.call( blockHeight, stateRoot, {from: worker}, ); - + assert.strictEqual( result, true, 'Return value of anchorStateRoot must be true.', ); - + await anchor.anchorStateRoot( blockHeight, stateRoot, {from: worker}, ); - + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); assert.strictEqual( blockHeight.eq(latestBlockHeight), true, `Latest block height from the contract must be ${blockHeight}.`, ); - + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, stateRoot, `Latest state root from the contract must be ${stateRoot}.`, ); - + }); - + it('should emit `StateRootAvailable` event', async () => { - + blockHeight = blockHeight.addn(1); - + let tx = await anchor.anchorStateRoot( blockHeight, stateRoot, {from: worker}, ); - + let event = EventDecoder.getEvents(tx, anchor); - + assert.isDefined( event.StateRootAvailable, 'Event `StateRootAvailable` must be emitted.', ); - + let eventData = event.StateRootAvailable; - + assert.strictEqual( eventData._stateRoot, stateRoot, `The _stateRoot value in the event should be equal to ${stateRoot}` ); - + assert.strictEqual( blockHeight.eq(eventData._blockHeight), true, `The _blockHeight in the event should be equal to ${blockHeight}` ); - + }); - + it('should store only the given number of max store roots', async () => { /* * It should store the given state roots and they should be @@ -208,7 +208,7 @@ contract('Anchor.anchorStateRoot()', function (accounts) { stateRoot, {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); @@ -221,7 +221,7 @@ contract('Anchor.anchorStateRoot()', function (accounts) { 'There should not be any state root stored at a ' + 'pruned height. It should have been reset by now.', ); - + /* * The state root that is one block younger than the pruned * one should still be available. @@ -238,5 +238,5 @@ contract('Anchor.anchorStateRoot()', function (accounts) { } } }); - + }); diff --git a/test/gateway/anchor/constructor.js b/test/gateway/anchor/constructor.js index ea5d641d..d5250750 100644 --- a/test/gateway/anchor/constructor.js +++ b/test/gateway/anchor/constructor.js @@ -26,96 +26,96 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('Anchor.constructor()', function (accounts) { - + let remoteChainId, blockHeight, stateRoot, maxNumberOfStateRoots, membersManager, anchor; - - beforeEach(async function () { - - remoteChainId = new BN(1410); - blockHeight = new BN(5); - stateRoot = web3.utils.sha3("dummy_state_root"); - maxNumberOfStateRoots = new BN(10); - membersManager = accounts[1]; - - }); - - it('should fail when remote chain id is zero', async () => { - - remoteChainId = new BN(0); - - await Utils.expectRevert( - Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - ), - 'Remote chain Id must not be 0.', - ); - - }); - - it('should fail when members manager address is zero', async () => { - - membersManager = NullAddress; - - await Utils.expectRevert( - Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - ), - 'MembersManager contract address must not be zero.', - ); - - }); - - it('should pass with correct params', async () => { - - anchor = await Anchor.new( - remoteChainId, - blockHeight, - stateRoot, - maxNumberOfStateRoots, - membersManager, - ); - - let chainId = await anchor.getRemoteChainId.call(); - assert.strictEqual( - remoteChainId.eq(chainId), - true, - `Remote chain id from the contract must be ${remoteChainId}.`, - ); - - let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); - assert.strictEqual( - blockHeight.eq(latestBlockHeight), - true, - `Latest block height from the contract must be ${blockHeight}.`, - ); - - let latestStateRoot = await anchor.getStateRoot.call(blockHeight); - assert.strictEqual( - latestStateRoot, - stateRoot, - `Latest state root from the contract must be ${stateRoot}.`, - ); - - let membersManagerAddress = await anchor.membersManager.call(); - assert.strictEqual( - membersManagerAddress, - membersManager, - `Members manager address from the contract must be ${membersManager}.`, - ); - - }); - + + beforeEach(async function () { + + remoteChainId = new BN(1410); + blockHeight = new BN(5); + stateRoot = web3.utils.sha3("dummy_state_root"); + maxNumberOfStateRoots = new BN(10); + membersManager = accounts[1]; + + }); + + it('should fail when remote chain id is zero', async () => { + + remoteChainId = new BN(0); + + await Utils.expectRevert( + Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager, + ), + 'Remote chain Id must not be 0.', + ); + + }); + + it('should fail when members manager address is zero', async () => { + + membersManager = NullAddress; + + await Utils.expectRevert( + Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager, + ), + 'MembersManager contract address must not be zero.', + ); + + }); + + it('should pass with correct params', async () => { + + anchor = await Anchor.new( + remoteChainId, + blockHeight, + stateRoot, + maxNumberOfStateRoots, + membersManager, + ); + + let chainId = await anchor.getRemoteChainId.call(); + assert.strictEqual( + remoteChainId.eq(chainId), + true, + `Remote chain id from the contract must be ${remoteChainId}.`, + ); + + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); + assert.strictEqual( + blockHeight.eq(latestBlockHeight), + true, + `Latest block height from the contract must be ${blockHeight}.`, + ); + + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); + assert.strictEqual( + latestStateRoot, + stateRoot, + `Latest state root from the contract must be ${stateRoot}.`, + ); + + let membersManagerAddress = await anchor.membersManager.call(); + assert.strictEqual( + membersManagerAddress, + membersManager, + `Members manager address from the contract must be ${membersManager}.`, + ); + + }); + }); diff --git a/test/gateway/anchor/get_latest_state_root_block_height.js b/test/gateway/anchor/get_latest_state_root_block_height.js index da07e7d7..311c7394 100644 --- a/test/gateway/anchor/get_latest_state_root_block_height.js +++ b/test/gateway/anchor/get_latest_state_root_block_height.js @@ -24,7 +24,7 @@ const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { - + let remoteChainId, blockHeight, stateRoot, @@ -33,9 +33,9 @@ contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { anchor, owner, worker; - + beforeEach(async function () { - + owner = accounts[2]; worker = accounts[3]; remoteChainId = new BN(1410); @@ -43,7 +43,7 @@ contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { stateRoot = web3.utils.sha3("dummy_state_root"); maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); - + anchor = await Anchor.new( remoteChainId, blockHeight, @@ -51,37 +51,37 @@ contract('Anchor.getLatestStateRootBlockHeight()', function (accounts) { maxNumberOfStateRoots, membersManager.address, ); - + }); - + it('should return the state root that was set while deployment', async () => { - + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); assert.strictEqual( blockHeight.eq(latestBlockHeight), true, `Latest block height from the contract must be ${blockHeight}.`, ); - + }); - + it('should return the latest anchored state root block height', async () => { - + blockHeight = blockHeight.addn(50000); - + await anchor.anchorStateRoot( blockHeight, stateRoot, {from: worker}, ); - + let latestBlockHeight = await anchor.getLatestStateRootBlockHeight.call(); assert.strictEqual( blockHeight.eq(latestBlockHeight), true, `Latest block height from the contract must be ${blockHeight}.`, ); - + }); - + }); diff --git a/test/gateway/anchor/get_remote_chainId.js b/test/gateway/anchor/get_remote_chainId.js index 5698e776..c31e03f5 100644 --- a/test/gateway/anchor/get_remote_chainId.js +++ b/test/gateway/anchor/get_remote_chainId.js @@ -23,22 +23,22 @@ const web3 = require('../../test_lib/web3.js'); const BN = require('bn.js'); contract('Anchor.getRemoteChainId()', function (accounts) { - + let remoteChainId, blockHeight, stateRoot, maxNumberOfStateRoots, membersManager, anchor; - + beforeEach(async function () { - + remoteChainId = new BN(1410); blockHeight = new BN(5); stateRoot = web3.utils.sha3("dummy_state_root"); maxNumberOfStateRoots = new BN(10); membersManager = accounts[1]; - + anchor = await Anchor.new( remoteChainId, blockHeight, @@ -46,18 +46,18 @@ contract('Anchor.getRemoteChainId()', function (accounts) { maxNumberOfStateRoots, membersManager, ); - + }); - + it('should return correct remote chain id', async () => { - + let chainId = await anchor.getRemoteChainId.call(); assert.strictEqual( remoteChainId.eq(chainId), true, `Remote chain id from the contract must be ${remoteChainId}.`, ); - + }); - + }); diff --git a/test/gateway/anchor/get_state_root.js b/test/gateway/anchor/get_state_root.js index 0ad8c4a1..9abec0c7 100644 --- a/test/gateway/anchor/get_state_root.js +++ b/test/gateway/anchor/get_state_root.js @@ -27,7 +27,7 @@ const zeroBytes = "0x0000000000000000000000000000000000000000000000000000000000000000"; contract('Anchor.getStateRoot()', function (accounts) { - + let remoteChainId, blockHeight, stateRoot, @@ -36,9 +36,9 @@ contract('Anchor.getStateRoot()', function (accounts) { anchor, owner, worker; - + beforeEach(async function () { - + owner = accounts[2]; worker = accounts[3]; remoteChainId = new BN(1410); @@ -46,7 +46,7 @@ contract('Anchor.getStateRoot()', function (accounts) { stateRoot = web3.utils.sha3("dummy_state_root"); maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); - + anchor = await Anchor.new( remoteChainId, blockHeight, @@ -54,52 +54,52 @@ contract('Anchor.getStateRoot()', function (accounts) { maxNumberOfStateRoots, membersManager.address, ); - + }); - + it('should return the latest state root block height that was set ' + 'while deployment', async () => { - + 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 anchored block heights', async () => { - + blockHeight = blockHeight.addn(500); - + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, zeroBytes, `Latest state root from the contract must be ${zeroBytes}.`, ); - + }); - + it('should return the latest anchored state root', async () => { - + blockHeight = blockHeight.addn(50000); stateRoot = web3.utils.sha3("dummy_state_root_1"); - + await anchor.anchorStateRoot( blockHeight, stateRoot, {from: worker}, ); - + let latestStateRoot = await anchor.getStateRoot.call(blockHeight); assert.strictEqual( latestStateRoot, stateRoot, `Latest state root from the contract must be ${stateRoot}.`, ); - + }); - + }); diff --git a/test/gateway/anchor/set_co_anchor_address.js b/test/gateway/anchor/set_co_anchor_address.js index 71ee984d..da6d1106 100644 --- a/test/gateway/anchor/set_co_anchor_address.js +++ b/test/gateway/anchor/set_co_anchor_address.js @@ -27,7 +27,7 @@ const Utils = require('../../../test/test_lib/utils'); const NullAddress = "0x0000000000000000000000000000000000000000"; contract('Anchor.setCoAnchorAddress()', function (accounts) { - + let remoteChainId, blockHeight, stateRoot, @@ -37,9 +37,9 @@ contract('Anchor.setCoAnchorAddress()', function (accounts) { owner, worker, coAnchorAddress; - + beforeEach(async function () { - + owner = accounts[2]; worker = accounts[3]; remoteChainId = new BN(1410); @@ -48,7 +48,7 @@ contract('Anchor.setCoAnchorAddress()', function (accounts) { maxNumberOfStateRoots = new BN(10); membersManager = await MockMembersManager.new(owner, worker); coAnchorAddress = accounts[6]; - + anchor = await Anchor.new( remoteChainId, blockHeight, @@ -56,65 +56,65 @@ contract('Anchor.setCoAnchorAddress()', function (accounts) { maxNumberOfStateRoots, membersManager.address, ); - + }); - + it('should fail when coAnchor address is zero', async () => { - + coAnchorAddress = NullAddress; - + await Utils.expectRevert( anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}), "Co-Anchor address must not be 0.", ); - + }); - + it('should fail when caller is not organisation owner', async () => { - + let notOwner = accounts[7]; - + await Utils.expectRevert( anchor.setCoAnchorAddress(coAnchorAddress, {from: notOwner}), 'Only the organization is allowed to call this method.', ); - + }); - + it('should pass with correct params', async () => { - + let result = await anchor.setCoAnchorAddress.call( coAnchorAddress, {from: owner}, ); - + assert.strictEqual( result, true, 'Return value of setAnchorAddress must be true.', ); - + await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); - + let coAnchor = await anchor.coAnchor.call(); - + assert.strictEqual( coAnchor, coAnchorAddress, `CoAnchor address must be equal to ${coAnchorAddress}.`, ); - + }); - + it('should fail to set coAnchor address if it\'s already set', async () => { - + await anchor.setCoAnchorAddress(coAnchorAddress, {from: owner}); - + await Utils.expectRevert( 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 686ca0b5..32664efc 100644 --- a/test/gateway/eip20_cogateway/constructor.js +++ b/test/gateway/eip20_cogateway/constructor.js @@ -95,7 +95,7 @@ contract('EIP20CoGateway.constructor() ', function (accounts) { assert.strictEqual( stateRootProviderAdd, dummyStateRootProvider, - 'Invalid anchor address from contract.' + 'Invalid stateRootProvider address from contract.' ); let bounty = await coGateway.bounty.call(); diff --git a/test/gateway/gateway_base/bounty_change.js b/test/gateway/gateway_base/bounty_change.js index 4f2e4f21..3159dff5 100644 --- a/test/gateway/gateway_base/bounty_change.js +++ b/test/gateway/gateway_base/bounty_change.js @@ -103,13 +103,13 @@ contract('GatewayBase.sol', function (accounts) { beforeEach(async function () { - let anchor = accounts[0] + let dummyStateRootProviderAddress = accounts[0] , bounty = new BN(100); let membersManager = await MockMembersManager.new(owner, worker); gatewayBaseInstance = await GatewayBase.new( - anchor, + dummyStateRootProviderAddress, bounty, membersManager.address ); diff --git a/test/gateway/helpers/helper.js b/test/gateway/helpers/helper.js index 9ac2e833..9c522f89 100644 --- a/test/gateway/helpers/helper.js +++ b/test/gateway/helpers/helper.js @@ -64,9 +64,9 @@ GatewayHelper.prototype = { "Invalid bounty token address from contract" ); - let anchorAdd = await this.gateway.anchor.call(); + let stateRootProviderAdd = await this.gateway.stateRootProvider.call(); assert.equal( - anchorAdd, + stateRootProviderAdd, stateRootProviderAddress, "Invalid state root provider address from contract" ); From 873525810aa67bcc80677c731a75fa6fd6902e7d Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Thu, 20 Dec 2018 19:17:00 +0530 Subject: [PATCH 14/15] Code cleanup and updated documentation as suggested in PR feedback --- contracts/gateway/GatewayBase.sol | 4 +--- contracts/test/gateway/MockGatewayBase.sol | 4 +--- test/gateway/eip20_cogateway/redeem.js | 1 - 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index ceceeae6..e15e719b 100644 --- a/contracts/gateway/GatewayBase.sol +++ b/contracts/gateway/GatewayBase.sol @@ -159,9 +159,7 @@ contract GatewayBase is Organized { /** * @notice 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 anchored in anchorStateRoot - * 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. diff --git a/contracts/test/gateway/MockGatewayBase.sol b/contracts/test/gateway/MockGatewayBase.sol index 3b9d950b..8caa73a2 100644 --- a/contracts/test/gateway/MockGatewayBase.sol +++ b/contracts/test/gateway/MockGatewayBase.sol @@ -43,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 anchored in anchorStateRoot - * 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. diff --git a/test/gateway/eip20_cogateway/redeem.js b/test/gateway/eip20_cogateway/redeem.js index 86d3baa4..596b24f9 100644 --- a/test/gateway/eip20_cogateway/redeem.js +++ b/test/gateway/eip20_cogateway/redeem.js @@ -19,7 +19,6 @@ // ---------------------------------------------------------------------------- const TestEIP20CoGateway = artifacts.require('TestEIP20CoGateway'), - MockAnchor = artifacts.require('MockAnchor'), MessageBus = artifacts.require('MessageBus'), UtilityToken = artifacts.require('UtilityToken'), EIP20Token = artifacts.require('EIP20Token'), From aff1bde8f628c701aa4cf10de05584ed380cbe2c Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Fri, 21 Dec 2018 13:13:54 +0530 Subject: [PATCH 15/15] Added license in the contract and updated documentation --- contracts/gateway/GatewayBase.sol | 34 +++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/contracts/gateway/GatewayBase.sol b/contracts/gateway/GatewayBase.sol index e15e719b..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. * @@ -154,15 +174,17 @@ contract GatewayBase is Organized { 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. 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. *