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

Stake and mint integration test #634

Merged
merged 14 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 32 additions & 0 deletions test_integration/01_deployment/01_deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

const chai = require('chai');
const Web3 = require('web3');
const BN = require('bn.js');
const docker = require('../docker');
const shared = require('../shared');
const {
Expand All @@ -31,6 +32,9 @@ const {

const { assert } = chai;

// Max ost prime supply.
const TOKENS_MAX = new BN('800000000000000000000000000');

describe('Deploy', async () => {
let rpcEndpointOrigin;
let web3Origin;
Expand All @@ -56,6 +60,8 @@ describe('Deploy', async () => {
shared.auxiliary.web3 = web3Auxiliary;
shared.origin.deployerAddress = deployerAddressOrigin;
shared.auxiliary.deployerAddress = deployerAddressAuxiliary;
shared.origin.accounts = accountsOrigin;
shared.auxiliary.accounts = accountsAuxiliary;

// FIXME: #623
shared.origin.organizationAddress = deployerAddressOrigin;
Expand All @@ -70,6 +76,7 @@ describe('Deploy', async () => {

await shared.auxiliary.addContract('EIP20CoGateway');
await shared.auxiliary.addContract('Anchor');
await shared.auxiliary.addContract('OSTPrime');
});

let tokenAddressOrigin;
Expand Down Expand Up @@ -139,4 +146,29 @@ describe('Deploy', async () => {
...auxiliaryAddresses,
};
});

it('active gateway ', async () => {
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
await shared.origin.addContract('EIP20Gateway');
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
const gateway = shared.origin.contracts.EIP20Gateway;

await gateway.activateGateway(
shared.auxiliary.contractAddresses.EIP20CoGateway,
{ from: deployerAddressOrigin },
);
});

it('initialize and set co-gateway in ost prime', async () => {
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
await shared.auxiliary.addContract('OSTPrime');
const ostPrime = shared.auxiliary.contracts.OSTPrime;

await ostPrime.initialize({
from: deployerAddressAuxiliary,
value: TOKENS_MAX,
});

await ostPrime.setCoGateway(
shared.auxiliary.contractAddresses.EIP20CoGateway,
{ from: deployerAddressAuxiliary },
);
});
});
42 changes: 0 additions & 42 deletions test_integration/02_stake_and_mint/01_stake.js

This file was deleted.

203 changes: 203 additions & 0 deletions test_integration/02_stake_and_mint/01_stake_and_mint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// Copyright 2018 OpenST Ltd.
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
//
// 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 BN = require('bn.js');
const shared = require('../shared');
const Utils = require('../../test/test_lib/utils');
const EventDecoder = require('../../test/test_lib/event_decoder');

const AssertStake = require('./utils/assert_stake');
const AssertProveGateway = require('./utils/assert_prove_gateway');
const AssertConfirmStakeIntent = require('./utils/assert_confirm_stake_intent');
const AssertProgressStake = require('./utils/assert_progress_stake');
const AssertProgressMint = require('./utils/assert_progress_mint');
const ProofUtils = require('./utils/proof_utils');
const Anchor = require('./utils/anchor');

describe('Stake and mint', async () => {
let assertStake;
let assertProgressStake;
let assertProgressMint;
let proofUtils;

let originAccounts;
let auxiliaryAccounts;
let originWeb3;
let auxiliaryWeb3;

let gateway;
let cogateway;
let token;
let baseToken;
let auxiliaryAnchor;
let ostPrime;
let stakeRequest;

before(async () => {
originWeb3 = shared.origin.web3;
auxiliaryWeb3 = shared.auxiliary.web3;
originAccounts = shared.origin.accounts;
auxiliaryAccounts = shared.origin.accounts;
token = shared.origin.contracts.Token;
baseToken = shared.origin.contracts.BaseToken;
gateway = shared.origin.contracts.EIP20Gateway;
cogateway = shared.auxiliary.contracts.EIP20CoGateway;
ostPrime = shared.auxiliary.contracts.OSTPrime;

const hasher = Utils.generateHashLock();
stakeRequest = {
amount: new BN(200),
gasPrice: new BN(1),
gasLimit: new BN(100),
staker: originAccounts[0],
bounty: await gateway.bounty.call(),
nonce: await gateway.getNonce.call(originAccounts[0]),
beneficiary: auxiliaryAccounts[2],
hashLock: hasher.l,
unlockSecret: hasher.s,
};

assertStake = new AssertStake(gateway, token, baseToken);
assertProgressStake = new AssertProgressStake(gateway, token, baseToken);
assertProgressMint = new AssertProgressMint(auxiliaryWeb3, cogateway, ostPrime);
proofUtils = new ProofUtils(originWeb3, auxiliaryWeb3);
auxiliaryAnchor = new Anchor(
originWeb3,
shared.auxiliary.contracts.Anchor,
);
});

it('stake', async () => {
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
// Capture initial token and base token balance of staker and gateway.
const initialBalances = await assertStake.captureBalances(stakeRequest.staker);
// Approve gateway for stake amount.
await token.approve(
gateway.address,
stakeRequest.amount,
{ from: stakeRequest.staker },
);

// Approve gateway for bounty.
await baseToken.approve(
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
gateway.address,
stakeRequest.bounty,
{ from: stakeRequest.staker },
);
const tx = await gateway.stake(
stakeRequest.amount,
stakeRequest.beneficiary,
stakeRequest.gasPrice,
stakeRequest.gasLimit,
stakeRequest.nonce,
stakeRequest.hashLock,
{ from: stakeRequest.staker },
);

const event = EventDecoder.getEvents(tx, gateway);
// Assert event and balances.
await assertStake.verify(event, stakeRequest, initialBalances);

stakeRequest.messageHash = event.StakeIntentDeclared._messageHash;
});

it('confirm stake', async () => {
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
// Anchor state root.
const blockNumber = await auxiliaryAnchor.anchorStateRoot(
'latest',
auxiliaryAccounts[0],
);
// Generate outbox proof for block height for which state root is
// anchored.
const outboxProof = await proofUtils.getOutboxProof(
gateway.address,
[stakeRequest.messageHash],
originWeb3.utils.toHex(blockNumber),
);

stakeRequest.blockHeight = new BN(blockNumber);
// Prove gateway.
let tx = await cogateway.proveGateway(
stakeRequest.blockHeight,
outboxProof.encodedAccountValue,
outboxProof.serializedAccountProof,
{ from: auxiliaryAccounts[0] },
);

let event = EventDecoder.getEvents(tx, cogateway);
AssertProveGateway.verify(
event,
stakeRequest.blockHeight,
outboxProof.storageHash,
gateway.address,
);

tx = await cogateway.confirmStakeIntent(
stakeRequest.staker,
stakeRequest.nonce,
stakeRequest.beneficiary,
stakeRequest.amount,
stakeRequest.gasPrice,
stakeRequest.gasLimit,
stakeRequest.hashLock,
stakeRequest.blockHeight,
outboxProof.storageProof[0].serializedProof,
{ from: auxiliaryAccounts[0] },
);

event = EventDecoder.getEvents(tx, cogateway);
// Assert event.
AssertConfirmStakeIntent.verify(event, stakeRequest);
});

it('progress stake', async () => {
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
// Capture initial token and base token balance of staker and gateway.
const initialBalancesBeforeProgress = await assertProgressStake.captureBalances(
stakeRequest.staker,
);

const tx = await gateway.progressStake(
stakeRequest.messageHash,
stakeRequest.unlockSecret,
{ from: originAccounts[0] },
);

const event = EventDecoder.getEvents(tx, gateway);
// Assert event and balances.
await assertProgressStake.verify(event, stakeRequest, initialBalancesBeforeProgress);
});

it('progress mint', async () => {
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
// Capture initial OST prime ERC20 and base token balance of
// beneficiary, OST prime contract address and gateway.
const initialBalancesBeforeMint = await assertProgressMint.captureBalances(
stakeRequest.beneficiary,
);

const tx = await cogateway.progressMint(
stakeRequest.messageHash,
stakeRequest.unlockSecret,
{ from: auxiliaryAccounts[0] },
);
const event = EventDecoder.getEvents(tx, cogateway);

// Assert event and balances.
await assertProgressMint.verify(event, stakeRequest, initialBalancesBeforeMint);
});
});
69 changes: 69 additions & 0 deletions test_integration/02_stake_and_mint/utils/anchor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2019 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 BN = require('bn.js');
const AssertAnchorStateRoot = require('./assert_anchor_stateroot');
const EventDecoder = require('../../../test/test_lib/event_decoder');

/**
* This class represents anchor contract on a chain.
*/
class Anchor {
/**
*
* @param sourceWeb3 Web3 of source chain for which this anchor tracks
* state root.
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
* @param anchor Contract instance of anchor contract.
*/
constructor(sourceWeb3, anchor) {
this.sourceWeb3 = sourceWeb3;
this.anchor = anchor;
}

/**
*
* @param atBlock Block at which state root needs to anchor. Default
* value is latest.
* @param owner Address of owner, reponsible for anchor state root.
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
* @return {Promise<blockHeight>} returns blockHeight at which state
* root is anchored.
*/
async anchorStateRoot(atBlock = 'latest', owner) {
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
const block = await this.sourceWeb3.eth.getBlock(atBlock);
const blockHeight = new BN(block.number);
const stateRoot = block.stateRoot;

const tx = await this.anchor.anchorStateRoot(
blockHeight,
stateRoot,
{ from: owner },
);

const event = EventDecoder.getEvents(tx, this.anchor);
AssertAnchorStateRoot.verify(
event,
blockHeight,
stateRoot,
);
return block.number;
}
}

module.exports = Anchor;
Loading