Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Gateways should store roots in ring buffer (Contract approach) #726

Merged
merged 7 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions contracts/gateway/EIP20CoGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ contract EIP20CoGateway is GatewayBase {
* @param _organization Address of an organization contract.
* @param _gateway Gateway contract address.
* @param _burner Address where base coin will be burned.
* @param _maxStorageRootItems Defines how many storage roots should be
* stored in circular buffer.
*/
constructor(
address _valueToken,
Expand All @@ -219,12 +221,14 @@ contract EIP20CoGateway is GatewayBase {
uint256 _bounty,
OrganizationInterface _organization,
address _gateway,
address payable _burner
address payable _burner,
uint256 _maxStorageRootItems
)
GatewayBase(
_stateRootProvider,
_bounty,
_organization
_organization,
_maxStorageRootItems
)
public
{
Expand Down
8 changes: 6 additions & 2 deletions contracts/gateway/EIP20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,23 @@ contract EIP20Gateway is GatewayBase {
* stake process.
* @param _organization Address of an organization contract.
* @param _burner Address where base tokens will be burned.
* @param _maxStorageRootItems Defines how many storage roots should be
* stored in circular buffer.
*/
constructor(
EIP20Interface _valueToken,
EIP20Interface _baseToken,
StateRootInterface _stateRootProvider,
uint256 _bounty,
OrganizationInterface _organization,
address _burner
address _burner,
uint256 _maxStorageRootItems
)
GatewayBase(
_stateRootProvider,
_bounty,
_organization
_organization,
_maxStorageRootItems
)
public
{
Expand Down
19 changes: 13 additions & 6 deletions contracts/gateway/GatewayBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pragma solidity ^0.5.0;
//
// ----------------------------------------------------------------------------

import "../lib/CircularBufferUint.sol";
import "../lib/EIP20Interface.sol";
import "../lib/GatewayLib.sol";
import "../lib/MessageBus.sol";
Expand All @@ -31,7 +32,7 @@ import "../lib/StateRootInterface.sol";
/**
* @title GatewayBase is the base contract for EIP20Gateway and EIP20CoGateway.
*/
contract GatewayBase is Organized {
contract GatewayBase is Organized, CircularBufferUint {

/* Usings */

Expand Down Expand Up @@ -67,12 +68,12 @@ contract GatewayBase is Organized {
/* Constants */

/** Position of message bus in the storage. */
uint8 constant MESSAGE_BOX_OFFSET = 7;
uint8 public constant MESSAGE_BOX_OFFSET = 9;

/**
* Penalty in bounty amount percentage charged to message sender on revert message.
*/
uint8 constant REVOCATION_PENALTY = 150;
uint8 public constant REVOCATION_PENALTY = 150;

//todo identify how to get block time for both chains
/**
Expand Down Expand Up @@ -112,8 +113,8 @@ contract GatewayBase is Organized {

/**
* Message box.
* @dev Keep this is at location 1, in case this is changed then update
* constant MESSAGE_BOX_OFFSET accordingly.
* @dev update constant MESSAGE_BOX_OFFSET according to the index of messageBox.
*
*/
MessageBus.MessageBox internal messageBox;

Expand Down Expand Up @@ -159,13 +160,17 @@ contract GatewayBase is Organized {
* @param _bounty The amount that facilitator will stakes to initiate the
* message transfers.
* @param _organization Address of an organization contract.
* @param _maxStorageRootItems Defines how many storage roots should be
* stored in circular buffer.
*/
constructor(
StateRootInterface _stateRootProvider,
uint256 _bounty,
OrganizationInterface _organization
OrganizationInterface _organization,
uint256 _maxStorageRootItems
)
Organized(_organization)
CircularBufferUint(_maxStorageRootItems)
public
{
require(
Expand Down Expand Up @@ -257,6 +262,8 @@ contract GatewayBase is Organized {
);

storageRoots[_blockHeight] = storageRoot;
uint256 oldestStoredBlockHeight = CircularBufferUint.store(_blockHeight);
delete storageRoots[oldestStoredBlockHeight];
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved

// wasAlreadyProved is false since Gateway is called for the first time
// for a block height
Expand Down
33 changes: 29 additions & 4 deletions contracts/test/gateway/MockGatewayBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ contract MockGatewayBase is GatewayBase {
* @param _bounty The amount that facilitator will stakes to initiate the
* stake process.
* @param _organization Address of a contract that manages workers.
* @param _maxStorageRootItems Defines how many storage roots should be
* stored in circular buffer.
*/
constructor(
StateRootInterface _stateRootProvider,
uint256 _bounty,
OrganizationInterface _organization
OrganizationInterface _organization,
uint256 _maxStorageRootItems
)
public
GatewayBase(
_stateRootProvider,
_bounty,
_organization
_organization,
_maxStorageRootItems
)
{}

Expand Down Expand Up @@ -102,9 +106,9 @@ contract MockGatewayBase is GatewayBase {
// return true
return true;
}

// On successful proof verification storage root is returned other wise
// transaction is reverted.
// transaction is reverted.
bytes32 storageRoot = MockGatewayLib.proveAccount(
_rlpAccount,
_rlpParentNodes,
Expand All @@ -113,6 +117,8 @@ contract MockGatewayBase is GatewayBase {
);

storageRoots[_blockHeight] = storageRoot;
uint256 oldestStoredBlockHeight = CircularBufferUint.store(_blockHeight);
delete storageRoots[oldestStoredBlockHeight];

// wasAlreadyProved is false since Gateway is called for the first time
// for a block height
Expand All @@ -125,4 +131,23 @@ contract MockGatewayBase is GatewayBase {

return true;
}

/**
* @notice Get the storage root for the given block height. This is only
* testing
*
* @param _blockHeight Block height for which the storage root is to be
* fetched.
*
* @return storageRoot_ Storage root for the given block height.
*/
function getStorageRoot(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be moved to GatewayBase.sol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us evaluate and create a ticket for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

uint256 _blockHeight
)
external
view
returns (bytes32 storageRoot_)
{
storageRoot_ = storageRoots[_blockHeight];
}
}
8 changes: 6 additions & 2 deletions contracts/test/gateway/TestEIP20CoGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ contract TestEIP20CoGateway is EIP20CoGateway {
* @param _organization Address of an organization contract.
* @param _gateway Gateway contract address.
* @param _burner An address where base tokens are sent when they should be burnt.
* @param _maxStorageRootItems Defines how many storage roots should be
* stored in circular buffer.
*/
constructor(
address _valueToken,
Expand All @@ -55,7 +57,8 @@ contract TestEIP20CoGateway is EIP20CoGateway {
uint256 _bounty,
OrganizationInterface _organization,
address _gateway,
address payable _burner
address payable _burner,
uint256 _maxStorageRootItems
)
EIP20CoGateway(
_valueToken,
Expand All @@ -64,7 +67,8 @@ contract TestEIP20CoGateway is EIP20CoGateway {
_bounty,
_organization,
_gateway,
_burner
_burner,
_maxStorageRootItems
)
public
{}
Expand Down
8 changes: 6 additions & 2 deletions contracts/test/gateway/TestEIP20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,26 @@ contract TestEIP20Gateway is EIP20Gateway {
* stake process.
* @param _organization Address of an organization contract.
* @param _burner Address where base tokens will be burned.
* @param _maxStorageRootItems Defines how many storage roots should be
* stored in circular buffer.
*/
constructor(
EIP20Interface _valueToken,
EIP20Interface _baseToken,
StateRootInterface _stateRootProvider,
uint256 _bounty,
OrganizationInterface _organization,
address payable _burner
address payable _burner,
uint256 _maxStorageRootItems
)
EIP20Gateway(
_valueToken,
_baseToken,
_stateRootProvider,
_bounty,
_organization,
_burner
_burner,
_maxStorageRootItems
)
public
{
Expand Down
8 changes: 6 additions & 2 deletions contracts/test/gateway/TestGatewayBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ contract TestGatewayBase is GatewayBase {
* @param _bounty The amount that facilitator will stakes to initiate the
* message transfers.
* @param _organization Address of a contract that manages workers.
* @param _maxStorageRootItems Defines how many storage roots should be
* stored in circular buffer.
*/
constructor(
StateRootInterface _stateRootProvider,
uint256 _bounty,
OrganizationInterface _organization
OrganizationInterface _organization,
uint256 _maxStorageRootItems
)
public
GatewayBase(
_stateRootProvider,
_bounty,
_organization
_organization,
_maxStorageRootItems
)
{}

Expand Down
3 changes: 3 additions & 0 deletions proof_generation/deployer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async function deployer(web3Provider, accounts) {
const bounty = new BN(100);
const organization = await MockOrganization.new(owner, worker);
const burner = Utils.NULL_ADDRESS;
const maxStorageRootItems = new BN(50);

const anchor = await Anchor.new(
remoteChainId,
Expand Down Expand Up @@ -75,6 +76,7 @@ async function deployer(web3Provider, accounts) {
bounty,
organization.address,
burner,
maxStorageRootItems,
);

const coGateway = await CoGateway.new(
Expand All @@ -85,6 +87,7 @@ async function deployer(web3Provider, accounts) {
organization.address,
gateway.address,
burner,
maxStorageRootItems,
);

await mockUtilityToken.setCoGateway(coGateway.address, { from: owner });
Expand Down
71 changes: 71 additions & 0 deletions proof_generation/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,74 @@ contract('Redeem and Unstake ', (accounts) => {
}
});
});

contract('Reward based on gas consumption', (accounts) => {
let registeredContracts;
let redeemParams;
let rpcEndpointOrigin;
let proofUtils;
let web3Provider;
let proofGenerationUtils;

before(async () => {
({ rpcEndpointOrigin } = await docker());
web3Provider = new Web3(rpcEndpointOrigin);
proofUtils = new ProofUtils(web3Provider, web3Provider);
});

beforeEach(async () => {
registeredContracts = await deployer(web3Provider, accounts);
redeemParams = {
amount: new BN(1000000000000000),
gasPrice: new BN(10000000),
gasLimit: new BN(10000000),
nonce: new BN(0),
redeemer: accounts[0],
beneficiary: accounts[2],
};
proofGenerationUtils = new ProofGenerationUtils(
registeredContracts,
null,
redeemParams,
proofUtils,
);
});

it('Generates proof data for "Redeem progressed"', async () => {
const numberOfProofs = 1;

for (let i = 0; i < numberOfProofs; i++) {
proofGenerationUtils.resetProofData();
await proofGenerationUtils.initializeProofData();

const {
redeemProofData,
} = await proofGenerationUtils.populateRedeemProofData();

const {
confirmRedeemIntentResult,
} = await proofGenerationUtils.populateConfirmRedeemIntentProofData(
redeemProofData,
);

const {
progressRedeemParams,
} = await proofGenerationUtils.populateProgressRedeemProofData(
confirmRedeemIntentResult,
);

await proofGenerationUtils.populateProgressUnstakeProofData(
progressRedeemParams,
);

// Write the proof data in to the files.
writeToFile(
`${PROOF_GENERATED_PATH}redeem_progressed_reward_based_on_gas_consumption.json`,
JSON.stringify(proofGenerationUtils.proofData),
);

// proof data is generated starting from nonce 0.
redeemParams.nonce = redeemParams.nonce.addn(1);
}
});
});
9 changes: 9 additions & 0 deletions proof_generation/helper/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ class Gateway {
burner,
};
}

/**
* Returns the message box offset
*
* @returns {BN} message box offset.
*/
async getMessageBoxOffset() {
return await this.gateway.MESSAGE_BOX_OFFSET.call();
}
}

module.exports = Gateway;
Loading