added tests to Block and Peer, and migrated buffertools usage

This commit is contained in:
Manuel Araoz 2014-02-18 12:47:15 -03:00
parent cdf53d1fc5
commit 3d901a12f2
5 changed files with 64 additions and 4 deletions

View File

@ -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');
}

View File

@ -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('.');

View File

@ -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') {

28
test/test.Block.js Normal file
View File

@ -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);
});
});

28
test/test.Peer.js Normal file
View File

@ -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);
});
});