From 3d901a12f225f0f791ecc41652527177b8140d79 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 12:47:15 -0300 Subject: [PATCH] added tests to Block and Peer, and migrated buffertools usage --- Block.js | 7 ++++--- Peer.js | 3 ++- bitcore.js | 2 ++ test/test.Block.js | 28 ++++++++++++++++++++++++++++ test/test.Peer.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 test/test.Block.js create mode 100644 test/test.Peer.js diff --git a/Block.js b/Block.js index cfe976b..f0abb5e 100644 --- a/Block.js +++ b/Block.js @@ -7,6 +7,7 @@ function spec(b) { var Bignum = b.Bignum || require('bignum'); var Binary = b.Binary || require('binary'); var Step = b.Step || require('step'); + var buffertools = b.buffertools || require('buffertools'); var Transaction = b.Transaction || require('./Transaction').class(); var TransactionIn = Transaction.In; var TransactionOut = Transaction.Out; @@ -79,7 +80,7 @@ function spec(b) { Block.prototype.checkHash = function checkHash() { if (!this.hash || !this.hash.length) return false; - return this.calcHash().compare(this.hash) == 0; + return buffertools.compare(this.calcHash(), this.hash) == 0; }; Block.prototype.getHash = function getHash() { @@ -95,7 +96,7 @@ function spec(b) { // endian so we don't have to reverse both buffers before comparing. this.hash.reverse(); - if (this.hash.compare(target) > 0) { + if (buffertools.compare(this.hash, target) > 0) { throw new VerificationError('Difficulty target not met'); } @@ -199,7 +200,7 @@ function spec(b) { throw new VerificationError('No merkle root'); } - if (this.calcMerkleRoot().compare(this.merkle_root) == 0) { + if (buffertools.compare(this.calcMerkleRoot(), this.merkle_root) == 0) { throw new VerificationError('Merkle root incorrect'); } diff --git a/Peer.js b/Peer.js index b27a5e2..54d6635 100644 --- a/Peer.js +++ b/Peer.js @@ -3,6 +3,7 @@ require('classtool'); function spec(b) { var Net = b.Net || require('net'); var Binary = b.Binary || require('binary'); + var buffertools = b.buffertools || require('buffertools'); function Peer(host, port, services) { if ("string" === typeof host) { @@ -17,7 +18,7 @@ function spec(b) { this.host = host.host; this.port = host.port; } else if (Buffer.isBuffer(host)) { - if (host.slice(0, 12).compare(Peer.IPV6_IPV4_PADDING) != 0) { + if (buffertools.compare(Peer.IPV6_IPV4_PADDING, host.slice(0, 12)) != 0) { throw new Error('IPV6 not supported yet! Cannot instantiate host.'); } this.host = Array.prototype.slice.apply(host.slice(12)).join('.'); diff --git a/bitcore.js b/bitcore.js index d712bc4..e99952f 100644 --- a/bitcore.js +++ b/bitcore.js @@ -15,6 +15,8 @@ module.exports.util = require('./util/util'); module.exports.Script = require('./Script'); module.exports.SINKey = require('./SINKey'); module.exports.Transaction = require('./Transaction'); +module.exports.Peer = require('./Peer'); +module.exports.Block = require('./Block'); if (typeof process.versions === 'undefined') { diff --git a/test/test.Block.js b/test/test.Block.js new file mode 100644 index 0000000..8932d6b --- /dev/null +++ b/test/test.Block.js @@ -0,0 +1,28 @@ +'use strict'; + +var chai = require('chai'); +var bitcore = require('../bitcore'); + +var should = chai.should(); + +var BlockModule = bitcore.Block; +var Block; + +describe('Block', function() { + it('should initialze the main object', function() { + should.exist(BlockModule); + }); + it('should be able to create class', function() { + Block = BlockModule.class(); + should.exist(Block); + }); + it('should be able to create instance', function() { + var p = new Block("localhost", 8333); + should.exist(p); + }); +}); + + + + + diff --git a/test/test.Peer.js b/test/test.Peer.js new file mode 100644 index 0000000..a2ff648 --- /dev/null +++ b/test/test.Peer.js @@ -0,0 +1,28 @@ +'use strict'; + +var chai = require('chai'); +var bitcore = require('../bitcore'); + +var should = chai.should(); + +var PeerModule = bitcore.Peer; +var Peer; + +describe('Peer', function() { + it('should initialze the main object', function() { + should.exist(PeerModule); + }); + it('should be able to create class', function() { + Peer = PeerModule.class(); + should.exist(Peer); + }); + it('should be able to create instance', function() { + var p = new Peer("localhost", 8333); + should.exist(p); + }); +}); + + + + +