Skip to content

Commit

Permalink
add checkTxCommon for all tx type. and checkStartinfo to start tx type.
Browse files Browse the repository at this point in the history
  • Loading branch information
swswsw committed Sep 16, 2018
1 parent 8b1ce6f commit 635b09f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 6 deletions.
8 changes: 6 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function txVerifySigHandler(state, tx, chainInfo) {
function txHandler1(state, tx, chainInfo) {
console.log("tx: ", JSON.stringify(tx));
//console.log(chainInfo);
checkTxCommon(tx); // this is always called, so we check it here.
}

function txHandler2(state, tx, chainInfo) {
Expand All @@ -164,6 +165,8 @@ function txStartHandler(state, tx, chainInfo) {
console.log("start: ", JSON.stringify(cloned));
let marketId = cloned.marketId;
let startInfo = cloned.startInfo;

checkStartInfo(startInfo);

// check marketId does not exist yet
if (typeof (state.market[marketId]) === 'undefined') {
Expand Down Expand Up @@ -811,8 +814,8 @@ const PUBKEY_SIZE = 0; // todo
* 5.
*/
function checkTxCommon(tx) {
if (tx.type.length <= MAX_TYPE_LENGTH) { throw new Error("type too long"); }
if (tx.user.length <= MAX_USER_LENGTH) { throw new Error("user too long"); }
if (tx.type.length > MAX_TYPE_LENGTH) { throw new Error("type too long"); }
if (typeof tx.user !== "undefined" && tx.user.length > MAX_USER_LENGTH) { throw new Error("user too long"); }
}

const MAX_START_INFO_LENGTH = 1024;
Expand All @@ -829,6 +832,7 @@ const MAX_START_INFO_LENGTH = 1024;
function checkStartInfo(startInfo) {
let sStartInfo = JSON.stringify(startInfo);
if (sStartInfo > MAX_START_INFO_LENGTH) { throw new Error("startInfo too large"); }
if (typeof startInfo.oracle === "undefined") { throw new Error("startInfo.oracle is required"); }
if (!Array.isArray(startInfo.oracle)) { throw new Error("startInfo.oracle must be an array"); }
if (!startInfo.question) { throw new Error("startInfo.question is required"); }
if (!startInfo.outcomes) { throw new Error("startInfo.outcomes is required"); }
Expand Down
3 changes: 2 additions & 1 deletion test/testSend.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ async function main () {
console.log("signature hex: ", sigHex);

let res = await post('http://localhost:3000/txs', tx);
console.log("tx resp", res.data);
console.log("tx resp: ", res.data);
console.log("tx resp log: ", res.data.result.check_tx.log);

test("tx valid", function(t){
t.notEqual(res.data.result.height, 0, "response is valid");
Expand Down
95 changes: 95 additions & 0 deletions test/testStartInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* test startinfo validity
*/
let secp = require('secp256k1');
let test = require('tape');
let { sha256, addressHash } = require('../common.js');
let { complexSendTx, sendTx, signTx, getState } = require('./testCommon.js');

let privAlice = Buffer.from("ebca1fcfba3e09e865613a87a3814813ab932936885c1b0495f1c05c7e21b1fc", "hex"); // alice's private key
let pubAlice = secp.publicKeyCreate(privAlice); // create public key
let addrAlice = addressHash(pubAlice);

let marketId = "m" + new Date().getTime();

async function main() {

let tx = {
"type": "start",
"marketId": marketId,
"startInfo": {
"question": "Who will win FIFA 2018?",
"outcomes": [
"england",
"italy",
"brazil",
"germany"
],
"oracle": ["9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL","5wvwWgKP3Qfw1akQoXWg4NtKmzx5v4dTj"], // addresses of approved oracles
// meta data about oracle. eg. description
"oracleMeta": "http://data.com/oracleinfo",
"phaseTime":{
"marketStart":9,"marketEnd":3609,
"oracleStart":3610,"oracleEnd":7210,
"challengeStart":7211,"challengeEnd":10811,
"voteStart":10812,"voteEnd":14412,
"distributeStart":14413,"distributeEnd":18013
},
},
};

//
// test 1: no question
//
let cloned = Object.assign({}, tx);
delete cloned.startInfo.question;
let result = await complexSendTx(cloned, privAlice);
console.log("no question. result: ", result);
console.log("result.result.height should be 0: ", result.result.height);
// result.result.check_tx.log should state why it is rejected

//
// test 2: no outcomes
//
cloned = Object.assign({}, tx);
delete cloned.startInfo.outcomes;
result = await complexSendTx(cloned, privAlice);
console.log("no outcomes. result: ", result);
console.log("result.result.height should be 0: ", result.result.height);
// result.result.check_tx.log should state why it is rejected

//
// test 3: no oracle
//
cloned = Object.assign({}, tx);
delete cloned.startInfo.oracle;
result = await complexSendTx(cloned, privAlice);
console.log("no oracle. result: ", result);
console.log("result.result.height should be 0: ", result.result.height);
// result.result.check_tx.log should state why it is rejected

//
// test 4: oracle is not an array
//
cloned = Object.assign({}, tx);
cloned.startInfo.oracle = {"whatever": "whatever"};
result = await complexSendTx(cloned, privAlice);
console.log(". result: ", result);
console.log("result.result.height should be 0: ", result.result.height);
// result.result.check_tx.log should state why it is rejected

state = await getState();
console.log("state: ", state);
// note: cannot use await inside tape test function.
// also, when there is async function, we cannot declare test twice with tape.
// we will get "Error: test exited without ending" if we declare tape test twice
// with the async function.
// https://github.com/substack/tape/issues/160
test("test invalid startInfo: ", function(t) {
// market should not be created.
t.equal(typeof(state.market[marketId]), 'undefined');
t.end();
});
}
main();

7 changes: 4 additions & 3 deletions test/testVerifySig.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ async function main () {


let res = await post('http://localhost:3000/txs', tx);
console.log("tx resp", res.data);
console.log("tx resp: ", res.data);
console.log("tx resp log: ", res.data.result.check_tx.log);

test("tx valid", function(t){
t.notEqual(res.data.result.height, 0, "response is valid");
test("verify sig", function(t){
t.notEqual(res.data.result.height, 0, "check tx validity");
t.end();
});

Expand Down

0 comments on commit 635b09f

Please sign in to comment.