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

WIP: Add challenge exit tests #83

Open
wants to merge 86 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
34ec19c
challengeExit test 01
Sprimage Mar 1, 2020
124ebee
fixed string bug
Sprimage Mar 1, 2020
0d856b7
syntax error
Sprimage Mar 1, 2020
17ac7f0
syntax error
Sprimage Mar 2, 2020
e11e0c6
syntax error
Sprimage Mar 2, 2020
b30cd20
syntax error
Sprimage Mar 2, 2020
c8b0cc1
syntax error
Sprimage Mar 2, 2020
cd6a076
syntax error
Sprimage Mar 2, 2020
716176a
syntax error
Sprimage Mar 2, 2020
905af88
syntax error
Sprimage Mar 2, 2020
38ac1d8
syntax error
Sprimage Mar 2, 2020
1d286b4
syntax error
Sprimage Mar 2, 2020
426a1fb
syntax error
Sprimage Mar 2, 2020
62a1c19
added helpers
Sprimage Mar 2, 2020
68a4791
added helpers
Sprimage Mar 2, 2020
2552493
added helpers
Sprimage Mar 2, 2020
4c949d1
Update 2_challengeExit.js
Sprimage Mar 2, 2020
830b031
exit below
Sprimage Mar 3, 2020
0878ce9
Merge branch 'master' of https://github.com/Sprimage/leap-sandbox
Sprimage Mar 3, 2020
680e204
Update 2_challengeExit.js
Sprimage Mar 3, 2020
cd6898b
exit below
Sprimage Mar 3, 2020
5da508e
Update 2_challengeExit.js
Sprimage Mar 3, 2020
635b484
Update 2_challengeExit.js
Sprimage Mar 3, 2020
c4166ea
Update 2_challengeExit.js
Sprimage Mar 3, 2020
ec95365
check payload
Sprimage Mar 4, 2020
819387c
check payload
Sprimage Mar 4, 2020
c97e2c8
check payload
Sprimage Mar 4, 2020
6d53687
check payload
Sprimage Mar 4, 2020
b06b1f5
check payload
Sprimage Mar 4, 2020
1cf5c09
check payload
Sprimage Mar 4, 2020
a3849af
check payload
Sprimage Mar 4, 2020
d037a89
check payload
Sprimage Mar 4, 2020
13ce483
exit mapping
Sprimage Mar 5, 2020
83c71bf
exit mapping
Sprimage Mar 5, 2020
dfde038
exit mapping
Sprimage Mar 5, 2020
c80d4d7
Update 2_challengeExit.js
Sprimage Mar 5, 2020
11c7ff6
Update 2_challengeExit.js
Sprimage Mar 5, 2020
e515e34
Update 2_challengeExit.js
Sprimage Mar 10, 2020
669d51f
Update 2_challengeExit.js
Sprimage Mar 14, 2020
e65bcfc
Update 2_challengeExit.js
Sprimage Mar 14, 2020
9ed258c
Update 2_challengeExit.js
Sprimage Mar 14, 2020
9b618e5
Update 2_challengeExit.js
Sprimage Mar 14, 2020
60317a2
Update 2_challengeExit.js
Sprimage Mar 16, 2020
adea67b
Update 2_challengeExit.js
Sprimage Mar 17, 2020
8f42f86
Update 2_challengeExit.js
Sprimage Mar 17, 2020
84fa46a
Update 2_challengeExit.js
Sprimage Mar 17, 2020
829c15f
Update 2_challengeExit.js
Sprimage Mar 17, 2020
a73da0b
Update 2_challengeExit.js
Sprimage Mar 17, 2020
f194142
Update 2_challengeExit.js
Sprimage Mar 17, 2020
2efbd44
Update 2_challengeExit.js
Sprimage Mar 19, 2020
53e6e51
Update 2_challengeExit.js
Sprimage Mar 19, 2020
4253a87
Update 2_challengeExit.js
Sprimage Mar 19, 2020
0d7926e
Update 2_challengeExit.js
Sprimage Mar 19, 2020
8535016
Update 2_challengeExit.js
Sprimage Mar 19, 2020
fd6f767
Update 2_challengeExit.js
Sprimage Mar 19, 2020
9fcb879
Update 2_challengeExit.js
Sprimage Mar 21, 2020
cb4f0a9
Update 2_challengeExit.js
Sprimage Mar 21, 2020
7379ac3
Update 2_challengeExit.js
Sprimage Mar 21, 2020
e2440d6
Update 2_challengeExit.js
Sprimage Mar 21, 2020
c36bd66
Update 2_challengeExit.js
Sprimage Mar 21, 2020
d94503a
Update 2_challengeExit.js
Sprimage Mar 21, 2020
ed92b8f
Update 2_challengeExit.js
Sprimage Mar 21, 2020
2a6686f
Update 2_challengeExit.js
Sprimage Mar 21, 2020
dffeef6
Update 2_challengeExit.js
Sprimage Mar 21, 2020
da08f6b
Update 2_challengeExit.js
Sprimage Mar 21, 2020
66d8ed0
Update 2_challengeExit.js
Sprimage Mar 21, 2020
9f5f547
Update 2_challengeExit.js
Sprimage Mar 21, 2020
64c9125
Update 2_challengeExit.js
Sprimage Mar 21, 2020
494271d
Update 2_challengeExit.js
Sprimage Mar 21, 2020
2c1b19a
Update 2_challengeExit.js
Sprimage Mar 21, 2020
e3e3557
Update 2_challengeExit.js
Sprimage Mar 21, 2020
8d3a633
Update 2_challengeExit.js
Sprimage Mar 21, 2020
8b45c0f
Update 2_challengeExit.js
Sprimage Mar 21, 2020
eb01923
Update 2_challengeExit.js
Sprimage Mar 21, 2020
a894801
Update 2_challengeExit.js
Sprimage Mar 21, 2020
5c1acbc
Update 2_challengeExit.js
Sprimage Mar 21, 2020
a2c96e0
Update 2_challengeExit.js
Sprimage Mar 21, 2020
e88b074
Update 2_challengeExit.js
Sprimage Mar 21, 2020
12bc59e
Update 2_challengeExit.js
Sprimage Mar 24, 2020
74db7b1
Update 2_challengeExit.js
Sprimage Mar 24, 2020
ffa749e
Update 2_challengeExit.js
Sprimage Mar 24, 2020
0262ac8
Update 2_challengeExit.js
Sprimage Mar 24, 2020
73ced57
Update 2_challengeExit.js
Sprimage Mar 24, 2020
a85b37a
Update 2_challengeExit.js
Sprimage Mar 24, 2020
12f7ec0
Update 2_challengeExit.js
Sprimage Mar 27, 2020
490eb1e
Update 2_challengeExit.js
Sprimage Mar 27, 2020
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
144 changes: 144 additions & 0 deletions tests/2_challengeExit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
const debug = require('debug');
const ethers = require('ethers');
const mintAndDeposit = require('./actions/mintAndDeposit');
const { transfer } = require('./actions/transfer');
const exitUnspent = require('./actions/exitUnspent');
const minePeriod = require('./actions/minePeriod');
const { helpers, Tx, Util, Input, Output, Outpoint } = require('leap-core');
const { bufferToHex } = require('ethereumjs-util');

require('chai').should();

const log = debug('challengeExit');

module.exports = async function(env, addr, color) {
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;
const bob = accounts[6].addr;
const bobPriv = accounts[6].privKey;
const charlie = accounts[4].addr;
const charliePriv = accounts[6].privKey;
const amount = 10000000;

await minePeriod(env);

const unspents = await node.getUnspent(bob, color);
const latestBlockNumber = (await node.getBlock('latest')).number;
log("Latest Block number: ", latestBlockNumber);
const latestSubmittedBlock = latestBlockNumber - latestBlockNumber % 32;
log("Latest submitted block number: ", latestSubmittedBlock);
if (latestSubmittedBlock === 0) {
throw new Error("Can't exit, no periods were submitted yet");
};

const getIndex = async (unspents, lastBlock) =>{
for(let i=0; i<unspents.length; i++) {
txHash = unspents[i].outpoint.hash;
txData = await node.getTransaction(bufferToHex(txHash));
log("Unspent", i, "blocknumber:", txData.blockNumber);
log("Is submitted?", txData.blockNumber < lastBlock);
if (txData.blockNumber < lastBlock) return i;
}

return -1;
};


const unspentIndex = await getIndex(unspents, latestSubmittedBlock);

if (unspentIndex === -1) {
throw new Error("Can't exit, no unspents are in submitted periods found");
};

const unspent = unspents[unspentIndex];


console.log("╔══════════════════════════════════════════╗");
console.log("║ Test: Challenge exit after Transfer ║");
console.log("║Steps: ║");
console.log("║1. Deposit to Alice ║");
console.log("║2. Trasfer from Alice to Bob ║");
console.log("║3. Exit Alice ║");
console.log("║4. Challenge Alice exit ║");
console.log("╚══════════════════════════════════════════╝");

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

// Alice makes a transfer to Bob
const t1 = await transfer(alice, alicePriv, bob, '1000', node);
await minePeriod(env);



const transfer1 = await node.getTransaction(bufferToHex(t1.hash()));
const proofOfTransfer1 = await helpers.getProof(
plasmaWallet.provider,
transfer1,
{excludePrevHashFromProof: true }
);

const youngestInput = await helpers.getYoungestInputTx(node, Tx.fromRaw(transfer1.raw));
const youngestInputProof = await helpers.getProof(
plasmaWallet.provider,
youngestInput.tx,
{excludePrevHashFromProof: true }
);

await minePeriod(env);



//

// console.log('youngest input', youngestInput);
// console.log('transfer 1' , transfer1);
// console.log('proofOfTransfer1', proofOfTransfer1);

// Now:
// 1. Bob spends the utxo he got from Alice in transfer1.
// 2. Bob starts an exit with the utxo he just sent to alice, using contract.exitHandler.startExit
// 3. But we know the unspent-transaction-output (utxo) he is trying to exit is NOT unspent (he sent it to Alice)
// 4. We use the proof that he spent the utxo in contract.exitHandler.challengeExit
// 5. In the end, we make sure the Exit struct in the exitHandler contract was deleted (this means the challenge was successful)

const t2 = await transfer(bob, bobPriv, charlie, '200', node);
await minePeriod(env);

const transfer2 = await node.getTransaction(bufferToHex(t2.hash()));
const proofOfTransfer2 = await helpers.getProof(
plasmaWallet.provider,
transfer2,
{excludePrevHashFromProof: true }
);
await minePeriod(env);

console.log(unspent.outpoint.index)
console.log(youngestInput.index)

let startExitResult =
await contracts.exitHandler.connect(wallet.provider.getSigner(addr)).startExit(
[],
proofOfTransfer1,
unspent.outpoint.index,
youngestInput.index,
{ value: ethers.utils.parseEther('1'), gasLimit: 2000000 }
);
console.log(await startExitResult.wait());


//const utxoId = exitUtxoId(event);
//console.log('transfer 1', transfer1);
//assert.equal(utxoId, spendTx.inputs[0].prevout.getUtxoId());

//assert.equal((await contracts.exitHandler.exits(utxoId))[2], bob);

// await contracts.exitHandler.challengeExit(proofOfTransfer1, proofOfTransfer1, 0, 0, alice);

// check exit was evicted from PriorityQueue
//assert.equal((await contracts.exitHandler.tokens(0))[1], 0);
}