migrating buffertools compare method and test

This commit is contained in:
Manuel Araoz 2014-02-18 13:17:23 -03:00
parent d2e7c7fc19
commit 0096238004
9 changed files with 70 additions and 9 deletions

View File

@ -35,7 +35,7 @@ function spec(b) {
this.active = data.active || false; this.active = data.active || false;
this.chainWork = data.chainWork || util.EMPTY_BUFFER; this.chainWork = data.chainWork || util.EMPTY_BUFFER;
this.txs = data.txs || []; this.txs = data.txs || [];
}; }
Block.prototype.getHeader = function getHeader() { Block.prototype.getHeader = function getHeader() {
var buf = new Buffer(80); var buf = new Buffer(80);

View File

@ -17,6 +17,7 @@ function spec(b) {
var Transaction = require('./Transaction').class(); var Transaction = require('./Transaction').class();
var util = b.util || require('./util/util'); var util = b.util || require('./util/util');
var Parser = b.Parser || require('./util/BinaryParser').class(); var Parser = b.Parser || require('./util/BinaryParser').class();
var buffertools = b.buffertools || require('buffertools');
var doubleSha256 = b.doubleSha256 || util.twoSha256; var doubleSha256 = b.doubleSha256 || util.twoSha256;
var nonce = util.generateNonce(); var nonce = util.generateNonce();
@ -114,7 +115,7 @@ function spec(b) {
switch (message.command) { switch (message.command) {
case 'version': case 'version':
// Did we connect to ourself? // Did we connect to ourself?
if (nonce.compare(message.nonce) === 0) { if (buffertools.compare(nonce, message.nonce) === 0) {
this.socket.end(); this.socket.end();
return; return;
} }
@ -376,7 +377,7 @@ function spec(b) {
if (checksum !== null) { if (checksum !== null) {
var checksumConfirm = doubleSha256(payload).slice(0, 4); var checksumConfirm = doubleSha256(payload).slice(0, 4);
if (checksumConfirm.compare(checksum) !== 0) { if (buffertools.compare(checksumConfirm, checksum) !== 0) {
log.err('['+this.peer+'] '+ log.err('['+this.peer+'] '+
'Checksum failed', 'Checksum failed',
{ cmd: command, { cmd: command,

View File

@ -4,7 +4,8 @@ function spec(b) {
var config = b.config || require('./config'); var config = b.config || require('./config');
var log = b.log || require('./util/log'); var log = b.log || require('./util/log');
var Opcode = require('./Opcode').class(); var Opcode = b.Opcode || require('./Opcode').class();
var buffertools = b.buffertools || require('buffertools');
// Make opcodes available as pseudo-constants // Make opcodes available as pseudo-constants
for (var i in Opcode.map) { for (var i in Opcode.map) {
@ -396,7 +397,7 @@ function spec(b) {
if (Buffer.isBuffer(chunk)) { if (Buffer.isBuffer(chunk)) {
for (var i = 0, l = this.chunks.length; i < l; i++) { for (var i = 0, l = this.chunks.length; i < l; i++) {
if (Buffer.isBuffer(this.chunks[i]) && if (Buffer.isBuffer(this.chunks[i]) &&
this.chunks[i].compare(chunk) == 0) { buffertools.compare(this.chunks[i], chunk) === 0) {
this.chunks.splice(i, 1); this.chunks.splice(i, 1);
dirty = true; dirty = true;
} }

View File

@ -5,7 +5,8 @@ function spec(b) {
var config = b.config || require('./config'); var config = b.config || require('./config');
var log = b.log || require('./util/log'); var log = b.log || require('./util/log');
var Opcode = require('./Opcode').class(); var Opcode = b.Opcode || require('./Opcode').class();
var buffertools = b.buffertools || require('buffertools');
// Make opcodes available as pseudo-constants // Make opcodes available as pseudo-constants
for (var i in Opcode.map) { for (var i in Opcode.map) {
@ -385,7 +386,7 @@ function spec(b) {
// (x1 x2 - bool) // (x1 x2 - bool)
var v1 = this.stackTop(2); var v1 = this.stackTop(2);
var v2 = this.stackTop(1); var v2 = this.stackTop(1);
var value = v1.compare(v2) == 0; var value = buffertools.compare(v1, v2) === 0;
// OP_NOTEQUAL is disabled because it would be too easy to say // OP_NOTEQUAL is disabled because it would be too easy to say
// something like n != 1 and have some wiseguy pass in 1 with extra // something like n != 1 and have some wiseguy pass in 1 with extra

View File

@ -17,6 +17,7 @@ module.exports.SINKey = require('./SINKey');
module.exports.Transaction = require('./Transaction'); module.exports.Transaction = require('./Transaction');
module.exports.Peer = require('./Peer'); module.exports.Peer = require('./Peer');
module.exports.Block = require('./Block'); module.exports.Block = require('./Block');
module.exports.Connection = require('./Connection');
if (typeof process.versions === 'undefined') { if (typeof process.versions === 'undefined') {

View File

@ -17,7 +17,7 @@ describe('Block', function() {
should.exist(Block); should.exist(Block);
}); });
it('should be able to create instance', function() { it('should be able to create instance', function() {
var p = new Block("localhost", 8333); var p = new Block();
should.exist(p); should.exist(p);
}); });
}); });

29
test/test.Connection.js Normal file
View File

@ -0,0 +1,29 @@
'use strict';
var chai = require('chai');
var bitcore = require('../bitcore');
var should = chai.should();
var ConnectionModule = bitcore.Connection;
var Connection;
describe('Connection', function() {
it('should initialze the main object', function() {
should.exist(ConnectionModule);
});
it('should be able to create class', function() {
Connection = ConnectionModule.class();
should.exist(Connection);
});
it('should be able to create instance', function() {
var mSocket, mPeer;
var c = new Connection(mSocket, mPeer);
should.exist(c);
});
});

View File

@ -17,7 +17,7 @@ describe('Peer', function() {
should.exist(Peer); should.exist(Peer);
}); });
it('should be able to create instance', function() { it('should be able to create instance', function() {
var p = new Peer("localhost", 8333); var p = new Peer('localhost', 8333);
should.exist(p); should.exist(p);
}); });
}); });

View File

@ -0,0 +1,28 @@
'use strict';
var chai = require('chai');
var bitcore = require('../bitcore');
var should = chai.should();
var ScriptInterpreterModule = bitcore.ScriptInterpreter;
var ScriptInterpreter;
describe('ScriptInterpreter', function() {
it('should initialze the main object', function() {
should.exist(ScriptInterpreterModule);
});
it('should be able to create class', function() {
ScriptInterpreter = ScriptInterpreterModule.class();
should.exist(ScriptInterpreter);
});
it('should be able to create instance', function() {
var si = new ScriptInterpreter();
should.exist(si);
});
});