Skip to content

Commit

Permalink
#28 prevent distribute from running more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
swswsw committed Sep 18, 2018
1 parent ca4f3af commit 3f8ed9a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ function txDistributeHandler(state, tx, chainInfo) {

let localContractStorage = new LocalContractStorage(state, marketId);

// check that distribute hasn't been completed already
let checkValue = localContractStorage.getValue("payouts");
if (typeof checkValue !== "undefined") {
let msg = "distribute cannot be invoked again";
console.log(msg);
throw new Error(msg);
}

// do final calculation and distribute the tokens accordingly.

// finaloutcome is assumed to be oracleoutcome, unless there is a vote.
Expand Down
118 changes: 118 additions & 0 deletions test/testDistribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,123 @@ test('test distribute for situation with vote', function(t) {
t.equals(state.balances.carol, 10003.33333333333333333333);


t.end();
});

test('test calling distribute again. distribute can only be called once.', function(t) {
let txDistributeHandler = mainapp.__get__('txDistributeHandler');

// storage.payouts is in the state,
// which indicates that distribute is called.
let state =
{
"market": {
"m1537251525926": {
"id": "m1537251525926",
"phaseTime": {
"challengeEnd": 10811,
"challengeStart": 7211,
"distributeEnd": 18013,
"distributeStart": 14413,
"marketEnd": 3609,
"marketStart": 9,
"oracleEnd": 7210,
"oracleStart": 3610,
"voteEnd": 14412,
"voteStart": 10812
},
"bets": [
{
"amount": 10,
"marketId": "m1537251525926",
"outcome": 1,
"type": "bet",
"user": "5wvwWgKP3Qfw1akQoXWg4NtKmzx5v4dTj"
},
{
"amount": 10,
"marketId": "m1537251525926",
"outcome": 2,
"type": "bet",
"user": "9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL"
}
],
"oracles": [

],
"oracleOutcome": 2,
"voteRecords": [

],
"payoutRatio": 1.5,
"storage": {
"distribution": {
"betPoolTotal": "20",
"winnerPoolTotal": "10"
},
"payouts": [
{
"user": "9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL",
"betAmount": "10",
"payoutAmount": "20"
}
]
},
"startInfo": {
"oracle": [
"9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL",
"5wvwWgKP3Qfw1akQoXWg4NtKmzx5v4dTj"
],
"oracleMeta": "http:\/\/data.com\/oracleinfo",
"outcomes": [
"england",
"italy",
"brazil",
"germany"
],
"phaseTime": {
"challengeEnd": 10811,
"challengeStart": 7211,
"distributeEnd": 18013,
"distributeStart": 14413,
"marketEnd": 3609,
"marketStart": 9,
"oracleEnd": 7210,
"oracleStart": 3610,
"voteEnd": 14412,
"voteStart": 10812
},
"question": "Who will win FIFA 2018?"
}
}
},
"balances": {
"5wvwWgKP3Qfw1akQoXWg4NtKmzx5v4dTj": 9990,
"9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL": 10010,
"t6bUAUxNh8xoZzt6YZdQn27J4DSiR2oH": 10000
},
"seq": {
"5wvwWgKP3Qfw1akQoXWg4NtKmzx5v4dTj": 1,
"9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL": 3
}
}
;

let tx = {"type": "distribute", "marketId": "m1537251525926"};
let privAlice = Buffer.from("ebca1fcfba3e09e865613a87a3814813ab932936885c1b0495f1c05c7e21b1fc", "hex"); // alice's private key
let sequence = 0;
signTx(tx, privAlice, sequence); // need to add signature to tx
let chainInfo = {height: 0}; // not used when we are in test mode that does not check for phase

txDistributeHandler(state, tx, chainInfo);

// the state contains storage.payouts. which means distribute has already completed.
// so calling distribute again should receive an error.
// the balance should be the same as before.
t.equals(state.balances["5wvwWgKP3Qfw1akQoXWg4NtKmzx5v4dTj"], 9990, "alice balance");
t.equals(state.balances["9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL"], 10010, "bob balance");
t.equals(state.balances["t6bUAUxNh8xoZzt6YZdQn27J4DSiR2oH"], 10000, "carol balance");


t.end();
});
1 change: 1 addition & 0 deletions test/testIntegrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ async function main() {
await oracle();
await oracle2();
await distribute();
await distribute(); // calling it twice should not cause problem. second distribute should be ignored.

state = await getState();
console.log("state: ", state);
Expand Down

0 comments on commit 3f8ed9a

Please sign in to comment.