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 0000000..207abda Binary files /dev/null and b/test/data/blk86756-testnet.dat differ 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; +