From d5529996efcd8b1b0da2bcd96d0dd04cf020d207 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 17 Jun 2016 17:52:40 -0700 Subject: [PATCH] tests. --- lib/bcoin/block.js | 2 +- test/block-test.js | 26 +++++++++++++++++++++++--- test/script-test.js | 24 ++++++++++++++++++++++-- test/tx-test.js | 27 +++++++++++++++------------ 4 files changed, 61 insertions(+), 18 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index a3c36cae..6cfcba50 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -463,7 +463,7 @@ Block.prototype._verify = function _verify(ret) { } if (this.merkleRoot !== merkle) { - ret.reason = 'bad-txnmrkleroot'; + ret.reason = 'bad-txnmrklroot'; ret.score = 100; return false; } diff --git a/test/block-test.js b/test/block-test.js index 11ab6280..8a24a5db 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -136,12 +136,15 @@ describe('Block', function() { assert(block.verify()); assert(block.txs[0].isCoinbase()); assert(block.txs[0].isSane()); + assert(!block.hasWitness()); + assert.equal(block.getCost(), 1136924); var flags = constants.flags.VERIFY_P2SH | constants.flags.VERIFY_DERSIG; for (var i = 1; i < block.txs.length; i++) { var tx = block.txs[i]; assert(tx.isSane()); assert(tx.checkInputs(block.height)); assert(tx.verify(flags)); + assert(!tx.hasWitness()); } assert.equal(block.getReward(), 2507773345); assert.equal(block.getReward(), block.txs[0].outputs[0].value); @@ -149,9 +152,12 @@ describe('Block', function() { it('should fail with a bad merkle root', function() { var block2 = new bcoin.block(block); + block2.hash(); block2.merkleRoot = constants.NULL_HASH; delete block2._valid; - assert(!block2.verify()); + var ret = {}; + assert(!block2.verify(ret)); + assert.equal(ret.reason, 'bad-txnmrklroot'); delete block2._valid; delete block2._hash; block2.merkleRoot = block.merkleRoot; @@ -160,8 +166,11 @@ describe('Block', function() { it('should fail on merkle block with a bad merkle root', function() { var mblock2 = new bcoin.merkleblock(mblock); + mblock2.hash(); mblock2.merkleRoot = constants.NULL_HASH; - assert(!mblock2.verify()); + var ret = {}; + assert(!mblock2.verify(ret)); + assert.equal(ret.reason, 'bad-txnmrklroot'); delete mblock2._validPartial; delete mblock2._valid; delete mblock2._hash; @@ -171,14 +180,25 @@ describe('Block', function() { it('should fail with a low target', function() { var block2 = new bcoin.block(block); + block2.hash(); block2.bits = 403014710; - assert(!block2.verify()); + var ret = {}; + assert(!block2.verify(ret)); + assert.equal(ret.reason, 'high-hash'); delete block2._valid; delete block2._hash; block2.bits = block.bits; assert(block2.verify()); }); + it('should fail on duplicate txs', function() { + var block2 = new bcoin.block(block); + block2.txs.push(block2.txs[block2.txs.length - 1]); + var ret = {}; + assert(!block2.verify(ret)); + assert.equal(ret.reason, 'bad-txns-duplicate'); + }); + it('should verify with headers', function() { var headers = new bcoin.headers(block); assert(headers.verify()); diff --git a/test/script-test.js b/test/script-test.js index 14b75433..a69cf557 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -343,8 +343,28 @@ describe('Script', function() { locktime: 0 }); if (nocache) { - delete coin._raw; - delete tx._raw; + tx._raw = null; + tx._size = null; + tx._witnessSize = null; + tx._lastWitnessSize = 0; + tx._hash = null; + tx._inputValue = null; + tx._outputValue = null; + tx._hashPrevouts = null; + tx._hashSequence = null; + tx._hashOutputs = null; + + coin._raw = null; + coin._size = null; + coin._witnessSize = null; + coin._lastWitnessSize = 0; + coin._hash = null; + coin._inputValue = null; + coin._outputValue = null; + coin._hashPrevouts = null; + coin._hashSequence = null; + coin._hashOutputs = null; + delete input.redeem; delete input._address; delete output._address; diff --git a/test/tx-test.js b/test/tx-test.js index c5a11c3e..063b3d98 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -38,7 +38,7 @@ function clearCache(tx, nocache) { if (tx instanceof bcoin.script) { if (!nocache) return; - delete tx.redeem; + tx.redeem = null; return; } @@ -47,24 +47,27 @@ function clearCache(tx, nocache) { return; } - delete tx._raw; - delete tx._hash; - delete tx._inputValue; - delete tx._outputValue; - - tx._size = 0; - tx._witnessSize = 0; + tx._raw = null; + tx._size = null; + tx._witnessSize = null; + tx._lastWitnessSize = 0; + tx._hash = null; + tx._inputValue = null; + tx._outputValue = null; + tx._hashPrevouts = null; + tx._hashSequence = null; + tx._hashOutputs = null; for (i = 0; i < tx.inputs.length; i++) { input = tx.inputs[i]; - delete input._address; - delete input.script.redeem; - delete input.witness.redeem; + input._address = null; + input.script.redeem = null; + input.witness.redeem = null; } for (i = 0; i < tx.outputs.length; i++) { output = tx.outputs[i]; - delete output._address; + output._address = null; } }