Skip to content

Instantly share code, notes, and snippets.

@kallewoof
Created January 11, 2017 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kallewoof/b7fbd05da026ea2cf717cb7bd62786f4 to your computer and use it in GitHub Desktop.
Save kallewoof/b7fbd05da026ea2cf717cb7bd62786f4 to your computer and use it in GitHub Desktop.
Unit test for listsinceblock bug where a reorg results in an invalid depth, losing transactions made on alternate (now main) chain.
if (process.argv.length !== 3) {
console.log("syntax: <bitcoin/src path>");
process.exit(1);
}
const bitcoin = process.argv[2];
const { BitcoinNet, BitcoinGraph } = require('bitcointest');
const net = new BitcoinNet(bitcoin, '/tmp/bitcointest', 23001, 23002);
try {
console.log('starting nodes');
const graph = new BitcoinGraph(net);
const nodes = net.launchBatchS(4);
const [n1,n2,n3,n4] = nodes;
net.waitForNodesS(nodes, 50000);
console.log('generating initial blocks');
net.mergeS(nodes);
n3.generateBlocksS(100);
net.syncS(nodes);
n1.generateBlocksS(10);
net.syncS(nodes);
console.log('waiting for balance');
n3.waitForBalanceChangeS(0);
if (n3.getBalanceS() < 1) throw "n3 balance invalid";
addr = n1.getNewAddressS();
console.log(`splitting network and making payment to ${addr} (left) from right side`);
const [g1,g2] = net.partitionS(nodes, 2);
graph.printBlockChainsS(g1,g2);
txid = n3.sendToNodeS(n1, 1);
blocks = n1.generateBlocksS(6);
lastblock = blocks[5];
console.log(`known last block = ${lastblock}`);
n3.generateBlocksS(6);
net.mergeS(nodes);
n3.generateBlocksS(1);
// a node on n1 last checked when lastblock was as above, but a reorg has now occured
net.syncS(nodes);
n1.client.listSinceBlock(lastblock, (e,i) => {
if (e) throw e.message;
r = i.result;
let found = false;
for (const tx of r.transactions) {
if (tx.txid === txid) {
found = true;
break;
}
}
net.shutdownS();
if (!found) throw "did not find tx";
console.log('found tx');
process.exit(0);
});
} catch (e) {
net.shutdownS();
throw typeof(e) == 'string' ? e : JSON.stringify(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment