added tests to Block and Peer, and migrated buffertools usage
This commit is contained in:
parent
cdf53d1fc5
commit
3d901a12f2
7
Block.js
7
Block.js
|
@ -7,6 +7,7 @@ function spec(b) {
|
||||||
var Bignum = b.Bignum || require('bignum');
|
var Bignum = b.Bignum || require('bignum');
|
||||||
var Binary = b.Binary || require('binary');
|
var Binary = b.Binary || require('binary');
|
||||||
var Step = b.Step || require('step');
|
var Step = b.Step || require('step');
|
||||||
|
var buffertools = b.buffertools || require('buffertools');
|
||||||
var Transaction = b.Transaction || require('./Transaction').class();
|
var Transaction = b.Transaction || require('./Transaction').class();
|
||||||
var TransactionIn = Transaction.In;
|
var TransactionIn = Transaction.In;
|
||||||
var TransactionOut = Transaction.Out;
|
var TransactionOut = Transaction.Out;
|
||||||
|
@ -79,7 +80,7 @@ function spec(b) {
|
||||||
|
|
||||||
Block.prototype.checkHash = function checkHash() {
|
Block.prototype.checkHash = function checkHash() {
|
||||||
if (!this.hash || !this.hash.length) return false;
|
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() {
|
Block.prototype.getHash = function getHash() {
|
||||||
|
@ -95,7 +96,7 @@ function spec(b) {
|
||||||
// endian so we don't have to reverse both buffers before comparing.
|
// endian so we don't have to reverse both buffers before comparing.
|
||||||
this.hash.reverse();
|
this.hash.reverse();
|
||||||
|
|
||||||
if (this.hash.compare(target) > 0) {
|
if (buffertools.compare(this.hash, target) > 0) {
|
||||||
throw new VerificationError('Difficulty target not met');
|
throw new VerificationError('Difficulty target not met');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +200,7 @@ function spec(b) {
|
||||||
throw new VerificationError('No merkle root');
|
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');
|
throw new VerificationError('Merkle root incorrect');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
Peer.js
3
Peer.js
|
@ -3,6 +3,7 @@ require('classtool');
|
||||||
function spec(b) {
|
function spec(b) {
|
||||||
var Net = b.Net || require('net');
|
var Net = b.Net || require('net');
|
||||||
var Binary = b.Binary || require('binary');
|
var Binary = b.Binary || require('binary');
|
||||||
|
var buffertools = b.buffertools || require('buffertools');
|
||||||
|
|
||||||
function Peer(host, port, services) {
|
function Peer(host, port, services) {
|
||||||
if ("string" === typeof host) {
|
if ("string" === typeof host) {
|
||||||
|
@ -17,7 +18,7 @@ function spec(b) {
|
||||||
this.host = host.host;
|
this.host = host.host;
|
||||||
this.port = host.port;
|
this.port = host.port;
|
||||||
} else if (Buffer.isBuffer(host)) {
|
} 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.');
|
throw new Error('IPV6 not supported yet! Cannot instantiate host.');
|
||||||
}
|
}
|
||||||
this.host = Array.prototype.slice.apply(host.slice(12)).join('.');
|
this.host = Array.prototype.slice.apply(host.slice(12)).join('.');
|
||||||
|
|
|
@ -15,6 +15,8 @@ module.exports.util = require('./util/util');
|
||||||
module.exports.Script = require('./Script');
|
module.exports.Script = require('./Script');
|
||||||
module.exports.SINKey = require('./SINKey');
|
module.exports.SINKey = require('./SINKey');
|
||||||
module.exports.Transaction = require('./Transaction');
|
module.exports.Transaction = require('./Transaction');
|
||||||
|
module.exports.Peer = require('./Peer');
|
||||||
|
module.exports.Block = require('./Block');
|
||||||
|
|
||||||
|
|
||||||
if (typeof process.versions === 'undefined') {
|
if (typeof process.versions === 'undefined') {
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue