Skip to content
15 changes: 6 additions & 9 deletions lib/chain.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use script';
var EventEmitter = require('events').EventEmitter;
var bitcore = require('bitcore');
var bitcore = require('bitcore-lib');
var Block = bitcore.Block;
var BlockHeader = bitcore.BlockHeader;
var bufferUtil = bitcore.util.buffer;
Expand Down Expand Up @@ -40,7 +40,7 @@ function Chain(options) {
utils.inherits(Chain, EventEmitter);

Chain.prototype._load = function() {
this.fromJSON(data);
this.fromObject(data);
this.loaded = true;
this.emit('load');
}
Expand Down Expand Up @@ -147,10 +147,7 @@ Chain.prototype.toJSON = function() {
};
};

Chain.prototype.fromJSON = function fromJSON(json) {
if(typeof json === 'string') {
json = JSON.parse(json);
}
Chain.prototype.fromObject = function fromObject(json) {
utils.assert.equal(json.version, 1);
utils.assert.equal(json.type, 'chain');

Expand All @@ -159,7 +156,7 @@ Chain.prototype.fromJSON = function fromJSON(json) {
this.index.heights = json.heights.slice();

if (this.index.hashes.length === 0) {
var genesisBlock = BlockHeader.fromJSON(constants.GENESIS_BLOCK);
var genesisBlock = BlockHeader.fromObject(constants.GENESIS_BLOCK);
this.blocks.list.push(genesisBlock);
this.blocks.last = genesisBlock;
this.index.hashes.push(genesisBlock.id);
Expand All @@ -174,9 +171,9 @@ Chain.prototype.fromJSON = function fromJSON(json) {

};

Chain.fromJSON = function(json) {
Chain.fromObject = function(data) {
var c = new Chain();
c.fromJSON(json);
c.fromObject(data);
return c;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = {
nonce: 2083236893,
hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', // Extra
prevHash: '0000000000000000000000000000000000000000000000000000000000000000',
merkleRoot: '3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a'
merkleRoot: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'
}
};
30,840 changes: 27,912 additions & 2,928 deletions lib/data/index.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/pool.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
var P2P = require('bitcore-p2p');
var Peer = P2P.Peer;
var Messages = P2P.Messages;
var bitcore = require('bitcore');
var bitcore = require('bitcore-lib');
var BlockHeader = bitcore.BlockHeader;
var EventEmitter = require('events').EventEmitter;
var BloomFilter = P2P.BloomFilter;
Expand Down Expand Up @@ -93,7 +92,7 @@ Pool.prototype._setLoaderPeer = function(peer) {
this.peers.loader = peer;
var lastHashIdx = this.chain.index.hashes.length - 1;
// TODO: move this to an on('set-loader-peer') instead of being in here
peer.sendMessage(new Messages.GetHeaders([this.chain.index.hashes[lastHashIdx]]));
peer.sendMessage(new peer.messages.GetHeaders({starts: [this.chain.index.hashes[lastHashIdx]]}));
}

Pool.prototype._handlePeerConnect = function(peer) {
Expand All @@ -103,6 +102,7 @@ Pool.prototype._handlePeerConnect = function(peer) {
peer.disconnect();
},3000);
peer.on('ready', function() { clearTimeout(peerTimeout); });
peer.on('disconnect', function() { clearTimeout(peerTimeout); });
this.emit('peer-connect', peer)
}

Expand All @@ -115,7 +115,7 @@ Pool.prototype._handlePeerReady = function(peer, addr) {
// TODO: not ideal to stick this on the peer object
// But its the simplest for now
peer._pingTimer = (setInterval(function(){
peer.sendMessage(new Messages.Ping());
peer.sendMessage(peer.messages.Ping());
},this.ping.interval));

//TODO: Smarter loader peer choosing
Expand Down Expand Up @@ -160,7 +160,7 @@ Pool.prototype._handlePeerInv = function(peer, message) {
// If we got new blocks, GetHeaders
if(blockHashes.length) {
var lastHashIdx = this.chain.index.hashes.length - 1;
peer.sendMessage(new Messages.GetHeaders([this.chain.index.hashes[lastHashIdx]]));
peer.sendMessage(new peer.messages.GetHeaders({starts: [this.chain.index.hashes[lastHashIdx]]}));
}
}

Expand All @@ -175,7 +175,7 @@ Pool.prototype._handlePeerHeaders = function(peer, message) {
// If we got 2000 messages, assume we still have more to get
if(message.headers.length == 2000) {
var lastHeader = message.headers[message.headers.length - 1];
peer.sendMessage(new Messages.GetHeaders([lastHeader.id]));
peer.sendMessage(new peer.messages.GetHeaders({starts: [lastHeader.id]}));
} else {
this.emit('chain-full');
}
Expand Down
9 changes: 3 additions & 6 deletions lib/wallet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var bitcore = require('bitcore');
var bitcore = require('bitcore-lib');
var Mnemonic = require('bitcore-mnemonic');
var EventEmitter = require('events').EventEmitter;

Expand Down Expand Up @@ -43,13 +43,10 @@ Wallet.prototype.balance = function () {
Wallet.prototype.mnemonic = function () {
}

Wallet.prototype.fromJSON = function() {
Wallet.prototype.fromObject = function() {
}

Wallet.prototype.fromJSON = function(json) {
if(typeof json === 'string') {
json = JSON.parse(json);
}
Wallet.prototype.fromObject = function(json) {
utils.assert.equal(json.version, 1);
utils.assert.equal(json.type, 'wallet');
// TODO
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
},
"homepage": "https://github.com/throughnothing/bitcore-spv",
"dependencies": {
"bitcore": "^0.10.4",
"bitcore-p2p": "git://github.com/throughnothing/bitcore-p2p.git#4cad909a1f8902e8708825e75d1b6a4fc2243dcf",
"bitcore-mnemonic": "^0.10.0",
"bitcore-mnemonic": "^8.19.0",
"bitcore-p2p": "^8.19.0",
"bloom-filter": "^0.1.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/reversebuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* Useful for testing/debugging with data from block explorers, etc., that
* need to be reversed to use bitcoin protocol formatting.
*/
var BufferUtil = require('bitcore').util.buffer;
var BufferUtil = require('bitcore-lib').util.buffer;

console.log(BufferUtil.reverse(new Buffer(process.argv[2], 'hex')).toString('hex'));
28 changes: 14 additions & 14 deletions test/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var chai = require('chai');
var expect = chai.expect;
var should = chai.should();
var BlockHeader = require('bitcore').BlockHeader;
var BlockHeader = require('bitcore-lib').BlockHeader;

var Chain = require('..').Chain;
var constants = require('..').constants;
Expand All @@ -29,10 +29,10 @@ describe('Chain', function() {
});
});

describe('#fromJSON', function() {
describe('#fromObject', function() {
it('valid JSON string non-prototype',function () {
var json = JSON.stringify(shortChain);
var c = Chain.fromJSON(json);
var c = Chain.fromObject(JSON.parse(json));
c.index.hashes.should.deep.equal(shortChain.hashes);
c.index.ts.should.deep.equal(shortChain.ts);
c.index.heights.should.deep.equal(shortChain.heights);
Expand All @@ -42,48 +42,48 @@ describe('Chain', function() {
it('valid JSON string prototype',function () {
var json = JSON.stringify(shortChain);
var c = new Chain();
c.fromJSON(json);
c.fromObject(JSON.parse(json));
c.index.hashes.should.deep.equal(shortChain.hashes);
c.index.ts.should.deep.equal(shortChain.ts);
c.index.heights.should.deep.equal(shortChain.heights);
c.lastTsAtLoad.should.equal(shortChain.ts[shortChain.ts.length-1]);
});

it('valid JSON object non-prototype',function () {
var c = Chain.fromJSON(shortChain);
it('valid object non-prototype',function () {
var c = Chain.fromObject(shortChain);
c.index.hashes.should.deep.equal(shortChain.hashes);
c.index.ts.should.deep.equal(shortChain.ts);
c.index.heights.should.deep.equal(shortChain.heights);
c.lastTsAtLoad.should.equal(shortChain.ts[shortChain.ts.length-1]);
});

it('valid JSON object prototype',function () {
it('valid object prototype',function () {
var c = new Chain()
c.fromJSON(shortChain);
c.fromObject(shortChain);
c.index.hashes.should.deep.equal(shortChain.hashes);
c.index.ts.should.deep.equal(shortChain.ts);
c.index.heights.should.deep.equal(shortChain.heights);
c.lastTsAtLoad.should.equal(shortChain.ts[shortChain.ts.length-1]);
});

it('uses genesis block no valid empty JSON',function () {
var c = Chain.fromJSON({
it('uses genesis block no valid empty object',function () {
var c = Chain.fromObject({
version: 1, type: 'chain', hashes: [], heights: [], ts: [] });

var genesisBlock = BlockHeader.fromJSON(constants.GENESIS_BLOCK);
var genesisBlock = BlockHeader.fromObject(constants.GENESIS_BLOCK);
c.index.heights.should.deep.equal([0]);
c.index.hashes.should.deep.equal([genesisBlock.id]);
c.index.ts.should.deep.equal([genesisBlock.time]);
c.lastTsAtLoad.should.equal(genesisBlock.time);
});

it('invalid JSON',function () {
it('invalid object',function () {
shortChain.version = 2;
expect(function(){ Chain.fromJSON(shortChain); })
expect(function(){ Chain.fromObject(shortChain); })
.to.throw(/2 == 1/);
shortChain.version = 1;
shortChain.type = 'wallet';
expect(function(){ Chain.fromJSON(shortChain); })
expect(function(){ Chain.fromObject(shortChain); })
.to.throw(/wallet/);
shortChain.type = 'chain';
});
Expand Down
10 changes: 2 additions & 8 deletions test/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ describe('Wallet', function() {
});
});

describe('#fromJSON', function() {
it('valid JSON',function () {
// TODO
});

it('invalid JSON',function () {
// TODO
});
describe('#fromObject', function() {
// TODO
});

});