Skip to content

Commit

Permalink
Merge branch 'develop' into update_safe_math
Browse files Browse the repository at this point in the history
  • Loading branch information
schemar authored Oct 29, 2018
2 parents f2c204b + f816266 commit ef2d0ae
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 41 deletions.
36 changes: 34 additions & 2 deletions contracts/core/OriginCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ contract OriginCore is OriginCoreInterface, OriginCoreConfig {
/** head is the block header hash of the latest committed block. */
bytes32 public head;

/**
* The maximum amount of gas that a meta-block could accumulate on an
* auxiliary chain before proposing a new meta-block.
*/
uint256 public maxAccumulateGasLimit;

/**
* Mapping of block hashes to block headers that were reported with the
* respective hash.
Expand Down Expand Up @@ -84,13 +90,18 @@ contract OriginCore is OriginCoreInterface, OriginCoreConfig {
* of this meta-blockchain must have so that the
* meta-blockchain is not considered halted. Used in
* the constructor of the Stake contract.
* @param _maxAccumulateGasLimit The maximum amount of gas that a
* meta-block could accumulate on an
* auxiliary chain before proposing a new
* meta-block.
*/
constructor(
bytes32 _auxiliaryCoreIdentifier,
address _ost,
uint256 _initialAuxiliaryGas,
bytes32 _initialTransactionRoot,
uint256 _minimumWeight
uint256 _minimumWeight,
uint256 _maxAccumulateGasLimit
)
public
{
Expand All @@ -99,9 +110,14 @@ contract OriginCore is OriginCoreInterface, OriginCoreConfig {
_initialTransactionRoot != bytes32(0),
"Auxiliary transaction root should be defined."
);
require(
_maxAccumulateGasLimit != uint256(0),
"Max accumulated gas limit should not be zero."
);

auxiliaryCoreIdentifier = _auxiliaryCoreIdentifier;
Ost = OstInterface(_ost);
maxAccumulateGasLimit = _maxAccumulateGasLimit;

// deploy stake contract
stake = new Stake(
Expand Down Expand Up @@ -195,7 +211,7 @@ contract OriginCore is OriginCoreInterface, OriginCoreConfig {
);

require(
_accumulatedGas > latestMetaBlockHeader.transition.gas,
_accumulatedGas > latestMetaBlockHeader.transition.accumulatedGas,
"Gas consumed should be greater than last meta-block gas."
);

Expand Down Expand Up @@ -323,6 +339,22 @@ contract OriginCore is OriginCoreInterface, OriginCoreConfig {
revert("Method not implemented.");
}

/**
* @notice Get accumulated gas target for next meta-block.
*
* @return Accumulated gas target.
*/
function getAccumulatedGasTarget()
external
view
returns (uint256 accumulateGasTarget_)
{
MetaBlock.Header storage lastCommittedMetaBlock = reportedHeaders[head];
accumulateGasTarget_ = lastCommittedMetaBlock.transition.accumulatedGas.add(
maxAccumulateGasLimit
);
}

/* Private Functions */

/**
Expand Down
10 changes: 10 additions & 0 deletions contracts/core/OriginCoreInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,14 @@ interface OriginCoreInterface {
external
view
returns (bytes32 stateRoot_);

/**
* @notice Get accumulated gas target for next meta-block.
*
* @return Accumulated gas target.
*/
function getAccumulatedGasTarget()
external
view
returns (uint256 accumulateGasTarget_);
}
9 changes: 5 additions & 4 deletions contracts/lib/MetaBlock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ library MetaBlock {
* The total gas that has been consumed on auxiliary for all blocks
* that are inside this meta-block.
*/
uint256 gas;
uint256 accumulatedGas;

/**
* Dynasty of origin block within latest meta-block
Expand Down Expand Up @@ -132,7 +132,8 @@ library MetaBlock {
* on the auxiliary chain.
* @param _auxiliaryBlockHash The block hash where the meta-block closes
* on the auxiliary chain.
* @param _gas The total consumed gas on auxiliary within this meta-block.
* @param _accumulatedGas The total consumed gas on auxiliary within this
* meta-block.
* @param _originDynasty Dynasty of origin block within latest meta-block
* reported at auxiliary chain.
* @param _originBlockHash Block hash of origin block within latest
Expand All @@ -147,7 +148,7 @@ library MetaBlock {
bytes32 _kernelHash,
uint256 _auxiliaryDynasty,
bytes32 _auxiliaryBlockHash,
uint256 _gas,
uint256 _accumulatedGas,
uint256 _originDynasty,
bytes32 _originBlockHash,
bytes32 _transactionRoot
Expand All @@ -156,7 +157,7 @@ library MetaBlock {
pure
returns (bytes32)
{
return keccak256(abi.encode(_gas));
return keccak256(abi.encode(_accumulatedGas));
}

/**
Expand Down
38 changes: 30 additions & 8 deletions test/core/origin_core/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,26 @@ const OriginCore = artifacts.require('OriginCore');
contract('OriginCore.constructor()', async (accounts) => {


let originCore;
let auxiliaryCoreIdentifier = web3.utils.sha3("1"),
gas, transactionRoot, ost = accounts[0];
let originCore, gas, transactionRoot, ost, maxAccumulateGasLimit;
let auxiliaryCoreIdentifier = web3.utils.sha3("1");
let minimumWeight = new BN('1');

beforeEach(async () => {

ost = accounts[0]
, gas = 1000
, transactionRoot = web3.utils.sha3("1");
ost = accounts[0];
gas = new BN(1000);
transactionRoot = web3.utils.sha3("1");
maxAccumulateGasLimit = new BN(105000);
});

it('should be able to deploy Origin core', async function () {


originCore = await OriginCore.new(
auxiliaryCoreIdentifier,
ost,
gas,
transactionRoot,
minimumWeight,
maxAccumulateGasLimit
);

assert(web3.utils.isAddress(originCore.address));
Expand All @@ -63,6 +62,7 @@ contract('OriginCore.constructor()', async (accounts) => {
gas,
transactionRoot,
minimumWeight,
maxAccumulateGasLimit
);

assert(web3.utils.isAddress(originCore.address));
Expand All @@ -80,6 +80,7 @@ contract('OriginCore.constructor()', async (accounts) => {
gas,
transactionRoot,
minimumWeight,
maxAccumulateGasLimit
);

assert(web3.utils.isAddress(originCore.address));
Expand All @@ -99,6 +100,7 @@ contract('OriginCore.constructor()', async (accounts) => {
gas,
transactionRoot,
minimumWeight,
maxAccumulateGasLimit
),
);
});
Expand All @@ -114,7 +116,27 @@ contract('OriginCore.constructor()', async (accounts) => {
gas,
transactionRoot,
minimumWeight,
maxAccumulateGasLimit
),
);
});

it('should not deploy origin core if max accumulated gas limit is zero',
async function () {

maxAccumulateGasLimit = new BN(0);

await Utils.expectThrow(
OriginCore.new(
auxiliaryCoreIdentifier,
ost,
gas,
transactionRoot,
minimumWeight,
maxAccumulateGasLimit
),
"Max accumulated gas limit should not be zero."
);
});
});

84 changes: 84 additions & 0 deletions test/core/origin_core/getAccumulatedGasTarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 web3 = require('../../test_lib/web3.js');
const BN = require('bn.js');
const OriginCore = artifacts.require('OriginCore');

contract('OriginCore.getAccumulatedGasTarget()', async (accounts) => {

/**
* @dev while writing this test commitMetaBlock function is not
* implemented, So once its updated the tests should be updated to use
* commitMetaBlock function. Currently this tests are using the genesis
* meta block for testing
*/

let originCore,
maxAccumulateGasLimit,
gas,
auxiliaryCoreIdentifier,
transactionRoot,
ost,
minimumWeight
;

/** Deploys the origin core contract */
async function deployOriginCore(){
originCore = await OriginCore.new(
auxiliaryCoreIdentifier,
ost,
gas,
transactionRoot,
minimumWeight,
maxAccumulateGasLimit,
);
}

/** Asserts the accumulated gas target */
async function testAccumulatedGasTarget(){
await deployOriginCore();
let accumulatedGasTarget = await originCore.getAccumulatedGasTarget.call();
assert(
accumulatedGasTarget.eq(gas.add(maxAccumulateGasLimit)),
`Accumulated gas target should` +
`be ${gas.add(maxAccumulateGasLimit).toString(10)}`
)
}

beforeEach(async () => {
auxiliaryCoreIdentifier = web3.utils.sha3("1");
transactionRoot= web3.utils.sha3("1");
ost = accounts[0];
minimumWeight = new BN('1');
});

it('should return 200 as accumulate gas target', async function () {
gas = new BN('100');
maxAccumulateGasLimit = new BN('100');
await testAccumulatedGasTarget();
});

it('should return 1245 as accumulate gas target', async function () {
gas = new BN('245');
maxAccumulateGasLimit = new BN('1000');
await testAccumulatedGasTarget();
});

});
20 changes: 11 additions & 9 deletions test/core/origin_core/propose_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ contract('OriginCore.proposeBlock()', async (accounts) => {
let auxiliaryCoreIdentifier = web3.utils.sha3("1");

let height, kernelHash, auxiliaryDynasty, auxiliaryBlockHash, gas,
originDynasty, originBlockHash, transactionRoot;
originDynasty, originBlockHash, transactionRoot, maxAccumulateGasLimit;

beforeEach(async () => {

height = 1,
kernelHash = web3.utils.sha3("1"),
auxiliaryDynasty = 50,
auxiliaryBlockHash = web3.utils.sha3("1"),
gas = 1000,
originDynasty = 1,
originBlockHash = web3.utils.sha3("1"),
transactionRoot = web3.utils.sha3("1");
height = 1;
kernelHash = web3.utils.sha3("1");
auxiliaryDynasty = 50;
auxiliaryBlockHash = web3.utils.sha3("1");
gas = 1000;
originDynasty = 1;
originBlockHash = web3.utils.sha3("1");
transactionRoot = web3.utils.sha3("1");
maxAccumulateGasLimit = new BN(105000);

let ost = accounts[0];

Expand All @@ -54,6 +55,7 @@ contract('OriginCore.proposeBlock()', async (accounts) => {
0,
web3.utils.sha3("1"),
minimumWeight,
maxAccumulateGasLimit
);
});

Expand Down
18 changes: 0 additions & 18 deletions test/gateway/eip20gateway/helpers/eip20gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ EIP20GatewayKlass.prototype = {
hashLock = params.hashLock,
signature = params.signature;

// Get the initial balances for all the addresses involved.
let initialStakerTokenBalance = await this.token.balanceOf.call(staker),
initialGatewayTokenBalance = await this.token.balanceOf.call(
this.gateway.address
),
initialFacilitatorTokenBalance = await this.token.balanceOf.call(
txOptions.from
);

// Get the initial balances of base token for all the addresses involved.
let initialStakerBaseTokenBalance = await this.baseToken.balanceOf.call(staker),
initialGatewayBaseTokenBalance = await this.baseToken.balanceOf.call(
this.gateway.address
),
initialFacilitatorBaseTokenBalance = await this.baseToken.balanceOf.call(
txOptions.from
);

let initialBalance = await this._getBalances(
staker,
this.gateway.address,
Expand Down

0 comments on commit ef2d0ae

Please sign in to comment.