From 5b95b0f0fdd8e0b00b5f9f3322a1267445190dd2 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 21 Mar 2014 12:50:19 -0300 Subject: [PATCH] Block.js tests WIP --- Block.js | 1 + bitcore.js | 1 + test/data/blk86756-testnet.dat | Bin 0 -> 9500 bytes test/test.Block.js | 101 ++++++++++++++++++++++++++++++++- test/testdata.js | 5 ++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 test/data/blk86756-testnet.dat diff --git a/Block.js b/Block.js index aeda5f9..64ae287 100644 --- a/Block.js +++ b/Block.js @@ -67,6 +67,7 @@ Block.prototype.parse = function parse(parser, headerOnly) { for (var i = 0; i < txCount; i++) { var tx = new Transaction(); tx.parse(parser); + tx.calcHash(); this.txs.push(tx); } }; diff --git a/bitcore.js b/bitcore.js index 3336406..79e8ffb 100644 --- a/bitcore.js +++ b/bitcore.js @@ -21,6 +21,7 @@ requireWhenAccessed('networks', './networks'); requireWhenAccessed('util', './util/util'); requireWhenAccessed('EncodedData', './util/EncodedData'); requireWhenAccessed('VersionedData', './util/VersionedData'); +requireWhenAccessed('BinaryParser', './util/BinaryParser'); requireWhenAccessed('Address', './Address'); requireWhenAccessed('Opcode', './Opcode'); requireWhenAccessed('Script', './Script'); diff --git a/test/data/blk86756-testnet.dat b/test/data/blk86756-testnet.dat new file mode 100644 index 0000000000000000000000000000000000000000..207abdac05c64e3bd7eac65736d13bd32f24b5b6 GIT binary patch literal 9500 zcmb7~byU>fy2fDu=>|b^Xb}aG?nb&n8l*v*fgz<^N>aK*Ku}P+Te=&hyIVl+40@E` zy=UFynYH-C#dr4mJRhF@?!CWTRBU8K5poz901OO_^!ow@cOJS(-}p_pCU8N*#_b>dU{Vnfdc89DN< z6UC%%qWzmpjwfRCd^`WDdh2X#K{7VUKw@_#!<{OJaAjrb-A06maHcOO)=~9=l4d{j z;a3t*#Q+pAZyip?!GeBV*=Q`gafT>?9`f`h0{SP-PgiK;fv6d2216?S*3PTH@;a(H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CTcNrG|u?Mf!RSYWx^dNq9HUCL#`Sh?pHE zBx|bX%nmx9#O+nEdIf>cm{brkeRr=QCXS}CWJ+l5G0-v7;i3-LSi-{t0JPhq2y32! zNhUz)dPdb5pO0&0&tyniK*ma%e;`{&8wv|G!j$cbtIg7=ojTXwW#Ja`uG}>43MbvV z@R=uD*!t&e8tH)nwolcytoGi}Pu6UAhB+ZW8!! z^*;0D3K!8=aAG^uIMi0Wg(2lP8o#ud2<=Gycw3S0epx(oUGZ`Fm;E9u-k~?(6QQCP z^|JVK=fQe;S~LuIDjw^Tdm?J^yYx%f&(^u3^Vj0iFhuveGRoGmAAVUi`vL%(Rc&~R+M_q-G;0$+3(k?b?Pis=O)8w-2M22 z$B$Ok2sm01iE3y?sh_v!IV3IL^DwAuU5>=Iw${GkOEnV}yU_}j>x(<*_;E(&LS|p} zwZRV!+NdbU74Gg$%>dzyK!mvod7d{D0rKntG4mp~B^3lz)Yq?s4`r4UxWL9z%g%bs z*425;z9=KMi-XsI+XKWetTzi5sv6x*pbPTy2j5YLWDFDUVGIwM&h9 zPp4wt8^Eo`hG%;@IDpN|*oc9qD0Z4*9SvPmrQ`bBikcW9wL(cU>k8;A`8QOo2KQ!X zMkto4VaFH2ee@$7#&@u`bqmS@C$1w0Q?_F%x}4%iBl3+AO97LZ2))~iCTd1C_mNcIOeQL>2xu;x^uuxY zR{FyFy|&I*kr(TGlou+>@4i0*C4Fx~fL!lw$vMsxH6Pk+eNsO1l z8tcpOcix_og3)JR48<$e7C(jxv3XO1o|==6u<&%8LdmbX2Wyoi>n2{|}kp BWlR77 literal 0 HcmV?d00001 diff --git a/test/test.Block.js b/test/test.Block.js index f159f43..7d366b1 100644 --- a/test/test.Block.js +++ b/test/test.Block.js @@ -4,9 +4,33 @@ var chai = chai || require('chai'); var bitcore = bitcore || require('../bitcore'); var should = chai.should(); +var testdata = testdata || require('./testdata'); var BlockModule = bitcore.Block; +var BinaryParser = bitcore.BinaryParser; var Block; + +var getBlock = function (onlyHeader) { + + var testnetMagic = bitcore.networks.testnet.magic.toString('hex'); + + var b = new Block(); + // this is block 86756 from testnet3 + var p = new BinaryParser(testdata.dataRawBlock); + + + var magic = p.buffer(4).toString('hex'); + + + if (magic !== testnetMagic ) + throw new Error('CRITICAL ERROR: Magic number mismatch: ' + + magic + ' : ' + testnetMagic); + + p.word32le(); + b.parse(p, onlyHeader); + return b; +}; + describe('Block', function() { it('should initialze the main object', function() { should.exist(BlockModule); @@ -16,9 +40,82 @@ describe('Block', function() { should.exist(Block); }); it('should be able to create instance', function() { - var p = new Block(); - should.exist(p); + var b = new Block(); + should.exist(b); }); + + it('should be able to parse a block from hex', function() { + var b = getBlock(); + should.exist(b); + should.exist(b.getHash()); + }); + + it('should be able to check block contents', function() { + var b = getBlock(); + should.exist(b.getHash()); + b.checkHash().should.equal(true); + b.checkProofOfWork().should.equal(true); + b.checkProofOfWork().should.equal(true); + b.getWork().toString().should.equal('17180131332'); + b.checkTimestamp().should.equal(true); + + }); + it('#checkBlock should be able to check block contents', function() { + var b = getBlock(); + should.exist(b.getHash()); + b.checkBlock().should.equal(true); + }); + + + it('should be able to check Transactions', function() { + var b = getBlock(); + + b.checkTransactions(b.txs).should.equal(true); + b.checkTransactions.bind([]).should.throw(); + + b.getMerkleTree(b.txs).length.should.equal(45); + bitcore.buffertools.toHex(b.calcMerkleRoot(b.txs)).should.equal(bitcore.buffertools.toHex(b.merkle_root)); + + var coinbase = b.txs.shift; + b.checkTransactions.bind(b.txs).should.throw(); + + b.txs.push(coinbase); + + b.checkTransactions.bind(b.txs).should.throw(); + + }); + + it('should be able to checkProofOfWork', function() { + var b = getBlock(); + + b.hash = bitcore.buffertools.reverse(new Buffer('000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11', 'hex')); + b.checkHash().should.equal(true); + b.checkProofOfWork().should.equal(true); + + // wrong hash hash, ok proof of work + b.hash = bitcore.buffertools.reverse(new Buffer('000000000000016390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11', 'hex')); + b.checkProofOfWork().should.equal(true); + b.checkHash().should.equal(false); + + + // wrong hash hash, wrong proof of work + b.hash = bitcore.buffertools.reverse(new Buffer('0000000bbb99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11', 'hex')); + b.checkHash().should.equal(false); + b.checkProofOfWork.bind().should.throw(); + }); + + it('should be able to get components from blocks', function() { + var b = getBlock(true); + + bitcore.util.formatHashFull(b.getHash()).should.equal('000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11'); + + bitcore.util.formatHashFull(b.getHeader()).should.equal('d6383bd51c3fffc051be10ce58e6d52d1eb00470ae1ab4d5a3375c0f51382c6f249fff84e9888286974cfc97000000003c35b5e70b13d5b938fef4e998a977c17bea978390273b7c50a9aa4b00000002'); + + bitcore.util.formatHashFull(b.merkle_root).should.equal('58e6d52d1eb00470ae1ab4d5a3375c0f51382c6f249fff84e9888286974cfc97'); + + }); + + }); diff --git a/test/testdata.js b/test/testdata.js index 3c7f3fd..a92fe41 100644 --- a/test/testdata.js +++ b/test/testdata.js @@ -30,3 +30,8 @@ module.exports.dataSigNonCanonical = dataSigNonCanonical; module.exports.dataBase58KeysValid = dataBase58KeysValid; module.exports.dataBase58KeysInvalid = dataBase58KeysInvalid; +var fd = fs.openSync('test/data/blk86756-testnet.dat', 'r'); +var buffer = new Buffer(9000); +fs.readSync(fd, buffer, 0, 9000, 0); +module.exports.dataRawBlock = buffer; +