Skip to content

Commit

Permalink
feat: support for CAS periods (#50)
Browse files Browse the repository at this point in the history
when mining period, wait for period to appear in a period store, so that you can
initiate exit (otherwise you lack CAS and slot data)
  • Loading branch information
troggy authored Aug 29, 2019
1 parent f58e507 commit 4567f5b
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const getTests = async () => {
};

async function run() {
const { contracts, nodes, accounts, wallet, plasmaWallet } = await startNetwork();
const env = await startNetwork();

const tests = await getTests();
for (const test of tests) {
console.log('Running: ', test);
await require('../../tests/' + test)(contracts, nodes, accounts, wallet, plasmaWallet);
await require('../../tests/' + test)(env);
}
if (process.env.CI || process.env.TRAVIS) {
process.exit(0);
Expand Down
4 changes: 3 additions & 1 deletion tests/0_spendingCondition.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const ethUtil = require('ethereumjs-util');
const spendingConditionABI = require('../src/spendingConditionABI');
const { Tx, Input, Output } = require('leap-core');

module.exports = async function(contracts, [node], accounts, wallet) {
module.exports = async function(env) {
const { contracts, nodes, accounts } = env;
const node = nodes[0];
const alice = accounts[0].addr;
const alicePriv = accounts[0].privKey;
const bob = accounts[1].addr;
Expand Down
10 changes: 6 additions & 4 deletions tests/1_depositTransferExit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const minePeriod = require('./actions/minePeriod');

require('chai').should();

module.exports = async function(contracts, [node], accounts, wallet, plasmaWallet) {
module.exports = async function(env) {
const { contracts, nodes, accounts, wallet, plasmaWallet } = env;
const node = nodes[0];
const minter = accounts[0].addr;
const alice = accounts[2].addr;
const alicePriv = accounts[2].privKey;
Expand Down Expand Up @@ -38,11 +40,11 @@ module.exports = async function(contracts, [node], accounts, wallet, plasmaWalle
(await node.getBalance(alice)).should.be.equal(balanceAlice - txAmount);
(await node.getBalance(bob)).should.be.equal(balanceBob + txAmount);
}
await minePeriod(node, accounts, contracts);
await minePeriod(env);
console.log("------Exit Alice------");
await exitUnspent(contracts, node, wallet, alice);
await exitUnspent(env, alice);
console.log("------Exit Bob------");
await exitUnspent(contracts, node, wallet, bob);
await exitUnspent(env, bob);

console.log("╔══════════════════════════════════════════╗");
console.log("║ Test: Deposit, trasfer, then exit ║");
Expand Down
8 changes: 5 additions & 3 deletions tests/3_depositExitTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const expect = chai.expect;

module.exports = async function(contracts, [node], accounts, wallet, plasmaWallet) {
module.exports = async function(env) {
const { contracts, nodes, accounts, wallet, plasmaWallet } = env;
const node = nodes[0];
const minter = accounts[0].addr;
const alice = accounts[6].addr;
const alicePriv = accounts[6].privKey;
Expand All @@ -24,10 +26,10 @@ module.exports = async function(contracts, [node], accounts, wallet, plasmaWalle

await mintAndDeposit(accounts[6], amount, minter, contracts.token, 0, contracts.exitHandler, node, wallet, plasmaWallet);

await minePeriod(node, accounts, contracts);
await minePeriod(env);

console.log("------Exit Alice------");
const utxo = await exitUnspent(contracts, node, wallet, alice);
const utxo = await exitUnspent(env, alice);
console.log("------Attemp to transfer exited utxo from Alice to Bob (should fail)------");
let plasmaBalanceBefore = await node.getBalance(alice);
const bobBalanceBefore = await node.getBalance(bob);
Expand Down
8 changes: 5 additions & 3 deletions tests/4_epochLengthExit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);

module.exports = async function(contracts, [node], accounts, wallet, plasmaWallet) {
module.exports = async function(env) {
const { contracts, nodes, accounts, wallet, plasmaWallet } = env;
const node = nodes[0];
const minter = accounts[0].addr;
const alice = accounts[7].addr;
const alicePriv = accounts[7].privKey;
Expand Down Expand Up @@ -41,9 +43,9 @@ module.exports = async function(contracts, [node], accounts, wallet, plasmaWalle
// 2 weeks waiting period ;)
await mine(gov.finalize({ gasLimit: 2000000 }));

await minePeriod(node, accounts, contracts);
await minePeriod(env);
console.log("------Exit Bob------");
await exitUnspent(contracts, node, wallet, bob);
await exitUnspent(env, bob);

console.log("╔══════════════════════════════════════════╗");
console.log("║ Test: Exit after epochLength change ║");
Expand Down
6 changes: 4 additions & 2 deletions tests/6_updateContracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const minePeriod = require('./actions/minePeriod');
const { mine } = require('../src/helpers');
const PosOperator = require('../build/contracts/build/contracts/PosOperator');

module.exports = async function(contracts, [node], accounts, wallet) {
module.exports = async function(env) {
const { contracts, nodes, accounts, wallet } = env;
const node = nodes[0];
const alice = accounts[0].addr;

console.log("╔═════════════════════════════════════╗");
Expand Down Expand Up @@ -40,7 +42,7 @@ module.exports = async function(contracts, [node], accounts, wallet) {
await mine(contracts.governance.finalize());

console.log("have some epochs pass by...");
await minePeriod(node, accounts, contracts);
await minePeriod(env);

console.log("╔══════════════════════════════════════╗");
console.log("║ Test: Upgrade contract ║");
Expand Down
6 changes: 4 additions & 2 deletions tests/7_registerTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const { assert } = chai;
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);

module.exports = async function(contracts, [node], accounts, wallet) {
const minter = accounts[0].addr;
module.exports = async function(env) {
const { contracts, nodes, accounts, wallet } = env;
const node = nodes[0];
const minter = accounts[0].addr;

console.log("Registering another token...");

Expand Down
9 changes: 6 additions & 3 deletions tests/8_breeding.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const TOKEN = require('../build/contracts/build/contracts/ERC1949.json');
const BreedingCondition =
'6080604052348015600f57600080fd5b5060043610602b5760e060020a6000350463689dcafc81146030575b600080fd5b606960048036036080811015604457600080fd5b50803590600160a060020a03602082013581169160408101359160609091013516606b565b005b6040805160e060020a63451da9f902815260048101869052600160a060020a038581166024830152604482018590529151839283169163451da9f991606480830192600092919082900301818387803b15801560c657600080fd5b505af115801560d9573d6000803e3d6000fd5b50505050505050505056fea165627a7a72305820ff50695e9e2f7357f76cac1b8adb0d5d43b7930e23a23bdae6c9f81c5fcddfcc0029';

module.exports = async function(contracts, [node], accounts, wallet) {
module.exports = async function(env) {
const { contracts, nodes, accounts, wallet } = env;
const node = nodes[0];

const minter = accounts[0].addr;
const minterPriv = accounts[0].privKey;

Expand Down Expand Up @@ -185,10 +188,10 @@ module.exports = async function(contracts, [node], accounts, wallet) {
);
transferTx.signAll(minterPriv);
await node.sendTx(transferTx);
await minePeriod(node, accounts, contracts);
await minePeriod(env);

unspents = (await node.provider.send('plasma_unspent', [minter]));
console.log(unspents);
const utxo = await exitUnspent(contracts, node, wallet, minter, unspents.length - 1);
const utxo = await exitUnspent(env, minter, unspents.length - 1);
console.log(utxo);
}
20 changes: 12 additions & 8 deletions tests/actions/exitUnspent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ const waitForBalanceChange = require('./waitForBalanceChange');
const ERC20_ERC721_TRANSFER_EVENT = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
const ERC1948_DATA_UPDATED_EVENT = '0x8ec06c2117d45dcb6bcb6ecf8918414a7ff1cb1ed07da8175e2cf638d0f4777f';

module.exports = async function(contracts, node, wallet, addr, uIndex) {
const log = getLog(false);
const slotId = 0;
const validatorAddr = (await node.getValidatorInfo()).ethAddress;

module.exports = async function(env, addr, uIndex) {
const { contracts, nodes, wallet, plasmaWallet } = env;
const node = nodes[0];

const log = getLog(false);

let txHash;
let txData;

Expand Down Expand Up @@ -42,7 +43,8 @@ module.exports = async function(contracts, node, wallet, addr, uIndex) {
return -1;
};

const unspentIndex = uIndex === undefined ? await getIndex(unspents, latestSubmittedBlock) : uIndex;
const unspentIndex = uIndex === undefined || uIndex === null ? await getIndex(unspents, latestSubmittedBlock) : uIndex;

if (unspentIndex === -1) {
throw new Error("Can't exit, no unspents are in submitted periods found");
};
Expand All @@ -61,7 +63,8 @@ module.exports = async function(contracts, node, wallet, addr, uIndex) {
const period = await Period.periodForTx(node, txData);
debug(period);
debug("------Proof------");
period.setValidatorData(slotId, validatorAddr);
const periodData = await plasmaWallet.provider.send('plasma_getPeriodByBlockHeight', [txData.blockNumber]);
period.setValidatorData(periodData[0].slotId, periodData[0].validatorAddress, periodData[0].casBitmap);
const proof = period.proof(Tx.fromRaw(txData.raw));
debug(proof);
debug("------Youngest Input------");
Expand All @@ -73,7 +76,8 @@ module.exports = async function(contracts, node, wallet, addr, uIndex) {
const youngestInputPeriod = await Period.periodForTx(node, youngestInput.tx);
debug(youngestInputPeriod);
debug("------Youngest Input Proof------");
youngestInputPeriod.setValidatorData(slotId, validatorAddr);
const periodData = await plasmaWallet.provider.send('plasma_getPeriodByBlockHeight', [youngestInput.tx.blockNumber]);
youngestInputPeriod.setValidatorData(periodData[0].slotId, periodData[0].validatorAddress, periodData[0].casBitmap);
youngestInputProof = youngestInputPeriod.proof(Tx.fromRaw(youngestInput.tx.raw));
debug(youngestInputProof);
} else {
Expand Down
19 changes: 14 additions & 5 deletions tests/actions/minePeriod.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const { helpers } = require('leap-core');
const { makeTransfer } = require('../../src/helpers');
const { makeTransfer, advanceBlocks } = require('../../src/helpers');

module.exports = async (node, [{ addr, privKey }], contracts) => {
module.exports = async (env) => {
const { contracts, nodes, accounts, wallet, plasmaWallet } = env;
const node = nodes[0];
const { addr, privKey } = accounts[0];
const currentBlock = Number((await node.getBlock('latest')).number);
const [, lastBlockInPeriod] = helpers.periodBlockRange(currentBlock);

Expand All @@ -10,7 +13,7 @@ module.exports = async (node, [{ addr, privKey }], contracts) => {
submissions.push(args);
});

for (let i = 0; i <= lastBlockInPeriod - currentBlock + 10; i++) {
for (let i = 0; i <= lastBlockInPeriod - currentBlock + 20; i++) {
const transfer = await makeTransfer(
node,
addr,
Expand All @@ -22,8 +25,14 @@ module.exports = async (node, [{ addr, privKey }], contracts) => {
await node.sendTx(transfer);
process.stdout.write(`\rMachinegunning till next period: ${currentBlock + i}/${lastBlockInPeriod + 1}`);
if (submissions.length > 0) {
console.log();
return;
await advanceBlocks(6, wallet);
}
const periodData = await plasmaWallet.provider.send('plasma_getPeriodByBlockHeight', [currentBlock]);
if (submissions.length > 0 && periodData) {
if (periodData) {
return;
}
break;
}
}
throw new Error('Period wasn\'t submitted on time');
Expand Down

0 comments on commit 4567f5b

Please sign in to comment.