Merge pull request #5 from maraoz/feature/prototype-p2p
p2p handling of inv for blocks and transactions
This commit is contained in:
commit
e1f958345e
12
HeaderDB.js
12
HeaderDB.js
@ -38,7 +38,7 @@ function ClassSpec(b) {
|
|||||||
assert.equal(this.byHeight[0].toString(),
|
assert.equal(this.byHeight[0].toString(),
|
||||||
this.network.genesisBlock.hash.toString());
|
this.network.genesisBlock.hash.toString());
|
||||||
loc.push(this.byHeight[0]);
|
loc.push(this.byHeight[0]);
|
||||||
console.log("Requesting more headers. Current height: " + block.height );
|
//console.log('Requesting more headers. Current height: ' + block.height );
|
||||||
return loc;
|
return loc;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ function ClassSpec(b) {
|
|||||||
|
|
||||||
if (hash in this.blocks) {
|
if (hash in this.blocks) {
|
||||||
var old = this.blocks[hash];
|
var old = this.blocks[hash];
|
||||||
throw new Error("duplicate block (was at height " + old.height + ")");
|
throw new Error('duplicate block (was at height ' + old.height + ')');
|
||||||
}
|
}
|
||||||
|
|
||||||
var bestChain = false;
|
var bestChain = false;
|
||||||
@ -63,7 +63,7 @@ function ClassSpec(b) {
|
|||||||
if (this.cached_size == 0) {
|
if (this.cached_size == 0) {
|
||||||
if (this.network.genesisBlock.hash.toString() !=
|
if (this.network.genesisBlock.hash.toString() !=
|
||||||
hash.toString())
|
hash.toString())
|
||||||
throw new Error("Invalid genesis block");
|
throw new Error('Invalid genesis block');
|
||||||
|
|
||||||
block.height = 0;
|
block.height = 0;
|
||||||
block.work = curWork;
|
block.work = curWork;
|
||||||
@ -72,7 +72,7 @@ function ClassSpec(b) {
|
|||||||
} else {
|
} else {
|
||||||
var prevBlock = this.blocks[block.prev_hash];
|
var prevBlock = this.blocks[block.prev_hash];
|
||||||
if (!prevBlock)
|
if (!prevBlock)
|
||||||
throw new Error("orphan block; prev not found");
|
throw new Error('orphan block; prev not found');
|
||||||
|
|
||||||
block.height = prevBlock.height + 1;
|
block.height = prevBlock.height + 1;
|
||||||
block.work = prevBlock.work + curWork;
|
block.work = prevBlock.work + curWork;
|
||||||
@ -169,7 +169,7 @@ function ClassSpec(b) {
|
|||||||
var fd = fs.openSync(filename, 'r');
|
var fd = fs.openSync(filename, 'r');
|
||||||
var stats = fs.fstatSync(fd);
|
var stats = fs.fstatSync(fd);
|
||||||
if (stats.size % 80 != 0)
|
if (stats.size % 80 != 0)
|
||||||
throw new Error("Corrupted header db");
|
throw new Error('Corrupted header db');
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
var buf = new Buffer(80);
|
var buf = new Buffer(80);
|
||||||
@ -180,7 +180,7 @@ function ClassSpec(b) {
|
|||||||
this.addBuf(buf);
|
this.addBuf(buf);
|
||||||
|
|
||||||
if ( ! ( this.cached_size % 1000 )) {
|
if ( ! ( this.cached_size % 1000 )) {
|
||||||
console.log("\tblock..." + this.cached_size ) ;
|
console.log('\tblock...' + this.cached_size ) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
49
p2p.js
49
p2p.js
@ -1,3 +1,5 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var HeaderDB = require('./HeaderDB').class();
|
var HeaderDB = require('./HeaderDB').class();
|
||||||
var Block = require('bitcore/Block').class();
|
var Block = require('bitcore/Block').class();
|
||||||
@ -19,8 +21,6 @@ var PeerManager = require('bitcore/PeerManager').createClass({
|
|||||||
});
|
});
|
||||||
var Peer = require('bitcore/Peer').class();
|
var Peer = require('bitcore/Peer').class();
|
||||||
|
|
||||||
var syncState = 'init';
|
|
||||||
|
|
||||||
function peerdb_load() {
|
function peerdb_load() {
|
||||||
try {
|
try {
|
||||||
peerdb = JSON.parse(fs.readFileSync(peerdb_fn));
|
peerdb = JSON.parse(fs.readFileSync(peerdb_fn));
|
||||||
@ -57,8 +57,7 @@ function add_header(info, block) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handle_headers(info) {
|
function handle_headers(info) {
|
||||||
console.log("handle headers");
|
console.log('handle headers');
|
||||||
var conn = info.conn;
|
|
||||||
var headers = info.message.headers;
|
var headers = info.message.headers;
|
||||||
|
|
||||||
headers.forEach(function(hdr) {
|
headers.forEach(function(hdr) {
|
||||||
@ -72,16 +71,6 @@ function handle_headers(info) {
|
|||||||
get_more_headers(info);
|
get_more_headers(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle_block(info) {
|
|
||||||
console.log("handle block")
|
|
||||||
var block = info.message.block;
|
|
||||||
add_header(info, block);
|
|
||||||
|
|
||||||
if (syncState === 'init') {
|
|
||||||
syncState = 'headers';
|
|
||||||
get_more_headers(info);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handle_verack(info) {
|
function handle_verack(info) {
|
||||||
var inv = {
|
var inv = {
|
||||||
@ -89,17 +78,41 @@ function handle_verack(info) {
|
|||||||
hash : network.genesisBlock.hash,
|
hash : network.genesisBlock.hash,
|
||||||
};
|
};
|
||||||
var invs = [ inv ];
|
var invs = [ inv ];
|
||||||
console.log('p2psync: Asking for the genesis block');
|
|
||||||
|
|
||||||
// Asks for the genesis block
|
// Asks for the genesis block
|
||||||
|
// console.log('p2psync: Asking for the genesis block');
|
||||||
|
// info.conn.sendGetData(invs);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_inv(info) {
|
||||||
|
console.log('handle inv');
|
||||||
|
// TODO: should limit the invs to objects we haven't seen yet
|
||||||
|
var invs = info.message.invs;
|
||||||
|
invs.forEach(function(inv) {
|
||||||
|
console.log('Received inv for a '+CoinConst.MSG.to_str(inv.type));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
console.log('requesting getData');
|
||||||
info.conn.sendGetData(invs);
|
info.conn.sendGetData(invs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handle_tx(info) {
|
||||||
|
var tx = info.message.tx.getStandardizedObject();
|
||||||
|
console.log('handle tx: '+JSON.stringify(tx));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_block(info) {
|
||||||
|
console.log('handle block');
|
||||||
|
var block = info.message.block;
|
||||||
|
add_header(info, block);
|
||||||
|
}
|
||||||
|
|
||||||
function handle_connected(data) {
|
function handle_connected(data) {
|
||||||
var peerman = data.pm;
|
var peerman = data.pm;
|
||||||
var peers_n = peerman.peers.length;
|
var peers_n = peerman.peers.length;
|
||||||
console.log('p2psync: Connected to ' + peers_n + ' peer'
|
console.log('p2psync: Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's' : ''));
|
||||||
+ (peers_n != 1 ? 's' : ''));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function p2psync() {
|
function p2psync() {
|
||||||
@ -114,6 +127,8 @@ function p2psync() {
|
|||||||
conn.on('verack', handle_verack);
|
conn.on('verack', handle_verack);
|
||||||
conn.on('block', handle_block);
|
conn.on('block', handle_block);
|
||||||
conn.on('headers', handle_headers);
|
conn.on('headers', handle_headers);
|
||||||
|
conn.on('inv', handle_inv);
|
||||||
|
conn.on('tx', handle_tx);
|
||||||
});
|
});
|
||||||
peerman.on('connect', handle_connected);
|
peerman.on('connect', handle_connected);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user