Skip to content

Commit

Permalink
reworked first test case into async
Browse files Browse the repository at this point in the history
  • Loading branch information
abbbe committed Jan 28, 2018
1 parent 327d9fc commit 34a540b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 35 deletions.
84 changes: 49 additions & 35 deletions test/test_splitter_contract.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Promise = require("bluebird");
Promise.promisifyAll(web3.eth, { suffix: "Promise" });

web3.eth.getTransactionReceiptMined = require("./getTransactionReceiptMined.js");

var Splitter = artifacts.require("./Splitter.sol");
Expand All @@ -10,22 +13,19 @@ contract('Splitter', function (accounts) {
const emma = accounts[5];
var splitter;

// returns an array containing account balances (as BigNumber, in wei)
// returns promise of an array containing account balances (as BigNumber, in wei)
function getBalances() {
return [splitter.address, alice, bob, carol, dave, emma].map(function (acc) { return web3.eth.getBalance(acc) });
}

// returns an array of BigNumbers, containing differences between current and "before" account balances
function getBalancesDiff(balancesBefore) {
var balancesAfter = getBalances();
return balancesBefore.map(function (_, i) { return balancesAfter[i].minus(balancesBefore[i]) });
return Promise.all([splitter.address, alice, bob, carol, dave, emma]
.map(acc => web3.eth.getBalancePromise(acc)));
}

// compares balances "before" (array of BigNumbers as returned by getBalances()) with expected ones (simple numbers)
function assertBalancesDiffEqual(balancesBefore, expectedDiffNumbers) {
var actualDiffs = getBalancesDiff(balancesBefore).map(function (n) { return n.toString(10) });
var expectedDiffs = expectedDiffNumbers.map(function (n) { return n.toString(); });
assert.deepEqual(actualDiffs, expectedDiffs);
return getBalances().then(balancesAfter => {
var actualDiffStr = balancesBefore.map((_, i) => balancesAfter[i].minus(balancesBefore[i]).toString(10));
var expectedDiffStr = expectedDiffNumbers.map(n => n.toString());
assert.deepEqual(actualDiffStr, expectedDiffStr);
});
}

before(async function () {
Expand All @@ -45,33 +45,47 @@ contract('Splitter', function (accounts) {
assert.equal(emma, accounts[5]);
});

it("funds sent by Alice to fallback should be claimable by Bob and Carol", async function () {
it("funds sent by Alice to fallback should be claimable by Bob and Carol", function (done) {

This comment has been minimized.

Copy link
@xavierlepretre

xavierlepretre Jan 30, 2018

Massive test. Can you split (no pun intended) it into digestible its?

This comment has been minimized.

Copy link
@abbbe

abbbe Jan 30, 2018

Author Owner

Split in two 343d2cb.

// calculate expected amounts to be debited and credited
var amount = 1000000;
var halfAmount1 = Math.floor(amount / 2);
var halfAmount2 = amount - halfAmount1;

// send some amount to Splitter on behalf of Alice
var balancesBefore = getBalances();
var txHash = web3.eth.sendTransaction({ from: alice, to: splitter.address, value: amount });
var tx = web3.eth.getTransaction(txHash);
var txReceipt = await web3.eth.getTransactionReceiptMined(txHash);

// got receipt for the transaction - make sure funds are with splitter
var txCost = txReceipt.gasUsed * tx.gasPrice;
assertBalancesDiffEqual(balancesBefore, [amount, -txCost - amount, 0, 0, 0, 0]);

// claim as bob
var txBobInfo = await splitter.withdraw({ from: bob });
var txBob = await web3.eth.getTransaction(txBobInfo.tx);
var txCostBob = txBobInfo.receipt.gasUsed * txBob.gasPrice;

// claim as carol
var txCarolInfo = await splitter.withdraw({ from: carol });
var txCarol = await web3.eth.getTransaction(txCarolInfo.tx);
var txCostCarol = txCarolInfo.receipt.gasUsed * txCarol.gasPrice;

assertBalancesDiffEqual(balancesBefore, [0, -txCost - amount, halfAmount1 - txCostBob, halfAmount2 - txCostCarol, 0, 0]);
var balancesBefore, txHash, tx, txCost;
var txBobInfo, txCostBob, txCarolInfo;
getBalances().then(_balancesBefore => {
balancesBefore = _balancesBefore;
return web3.eth.sendTransactionPromise({ from: alice, to: splitter.address, value: amount });
}).then(_txHash => {
txHash = _txHash;
return web3.eth.getTransactionPromise(txHash);
}).then(_tx => {
tx = _tx;
return web3.eth.getTransactionReceiptMined(txHash);
}).then(txReceipt => {
// got receipt for the transaction - make sure funds are with splitter
txCost = txReceipt.gasUsed * tx.gasPrice;
return assertBalancesDiffEqual(balancesBefore, [amount, -txCost - amount, 0, 0, 0, 0]);

This comment has been minimized.

Copy link
@xavierlepretre

xavierlepretre Jan 29, 2018

It is not very expressive. Perhaps use objects { alice: aliceBalance, ...} instead of array?

This comment has been minimized.

Copy link
@abbbe

abbbe Jan 30, 2018

Author Owner

#28

}).then(() => {
// claim as bob
return splitter.withdraw({ from: bob });
}).then(_txBobInfo => {
txBobInfo = _txBobInfo;
return web3.eth.getTransactionPromise(txBobInfo.tx);
}).then(txBob => {
txCostBob = txBobInfo.receipt.gasUsed * txBob.gasPrice;
// claim as carol
return splitter.withdraw({ from: carol });
}).then(_txCarolInfo => {
txCarolInfo = _txCarolInfo;
return web3.eth.getTransactionPromise(txCarolInfo.tx);
}).then(txCarol => {
var txCostCarol = txCarolInfo.receipt.gasUsed * txCarol.gasPrice;
return assertBalancesDiffEqual(balancesBefore, [0, -txCost - amount, halfAmount1 - txCostBob, halfAmount2 - txCostCarol, 0, 0]);
}).then(() => {
done();
});
});

function _assertRevert(error, tag) {
Expand Down Expand Up @@ -131,7 +145,7 @@ contract('Splitter', function (accounts) {
// assertBalancesDiffEqual(balancesBefore, [0, 0, 0, halfAmount2, -txCost - amount, halfAmount1]);
// });

it("funds sent by Dave to split(emma, carol) should be claimable by Emma and Carol, events should fire", async function () {
it.skip("funds sent by Dave to split(emma, carol) should be claimable by Emma and Carol, events should fire", async function () {
// calculate expected amounts to be debited and credited
var amount = 1000000;
var halfAmount1 = Math.floor(amount / 2);
Expand All @@ -140,7 +154,7 @@ contract('Splitter', function (accounts) {
// send some amount to Splitter on behalf of Dave
var balancesBefore = getBalances();
var txInfo = await splitter.split(emma, carol, { from: dave, to: splitter.address, value: amount });

assert.equal(txInfo.logs.length, 1);
assert.equal(txInfo.logs[0].event, 'LogSplit');
assert.equal(txInfo.logs[0].args.party0, dave);
Expand All @@ -160,7 +174,7 @@ contract('Splitter', function (accounts) {
assert.equal(txEmmaInfo.logs.length, 1);
assert.equal(txEmmaInfo.logs[0].event, 'LogWithdraw');
assert.equal(txEmmaInfo.logs[0].args.party, emma);
assert.equal(txEmmaInfo.logs[0].args.amount, halfAmount1);
assert.equal(txEmmaInfo.logs[0].args.amount, halfAmount1);

This comment has been minimized.

Copy link
@xavierlepretre

xavierlepretre Jan 30, 2018

Use assert.strictEqual(.

This comment has been minimized.

Copy link
@abbbe

abbbe Jan 30, 2018

Author Owner

May I ask why? I understand assert.equal('0x1', 1) passes, but .strictEqual will fail. How is it relevant here?

This comment has been minimized.

Copy link
@xavierlepretre

xavierlepretre Jan 30, 2018

Because the types are different and you were lucky there were some magic conversion happening. Can you .strictEqual the .toString(10) of both?

This comment has been minimized.

Copy link
@abbbe

abbbe Jan 31, 2018

Author Owner

Should one always use strictEqual or there is something special about this particular line of code? #36

This comment has been minimized.

Copy link
@xavierlepretre

xavierlepretre Jan 31, 2018

I tend to use .strictEqual because it has more chance of failing, so more chance of catching errors. Especially when you know that Javascript has plenty of weird equivalences.

var txEmma = await web3.eth.getTransaction(txEmmaInfo.tx);
var txCostEmma = txEmmaInfo.receipt.gasUsed * txEmma.gasPrice;

Expand Down
11 changes: 11 additions & 0 deletions truffle.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
var web3 = require('web3');
var net = require('net');
var ipcPath = '/Users/abb/Documents/dvp/net24601/datadir/geth.ipc';

module.exports = {
networks: {
development: {
Expand All @@ -12,6 +16,13 @@ module.exports = {
network_id: "24601"
},

net24601_ipc: {
provider: function () {
return new web3.providers.IpcProvider(ipcPath, net);
},
network_id: "24601"
},

parity_dvp: {
host: "127.0.0.1",
port: 48545,
Expand Down

0 comments on commit 34a540b

Please sign in to comment.