commit
510cc22812
|
@ -37,21 +37,21 @@ module.exports.broadcastTx = function(tx) {
|
|||
|
||||
t.valueOut = parseInt(valueOut) / util.COIN;
|
||||
}
|
||||
ios.sockets. in ('inv').emit('tx', t);
|
||||
ios.sockets.in('inv').emit('tx', t);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.broadcastBlock = function(block) {
|
||||
if (ios) ios.sockets. in ('inv').emit('block', block);
|
||||
if (ios)
|
||||
ios.sockets.in('inv').emit('block', block);
|
||||
};
|
||||
|
||||
module.exports.broadcastAddressTx = function(address, tx) {
|
||||
if (ios) ios.sockets. in (address).emit(address, tx);
|
||||
if (ios)
|
||||
ios.sockets.in(address).emit(address, tx);
|
||||
};
|
||||
|
||||
module.exports.broadcastSyncInfo = function(historicSync) {
|
||||
|
||||
if (ios) {
|
||||
ios.sockets. in ('sync').emit('status', historicSync);
|
||||
}
|
||||
if (ios)
|
||||
ios.sockets.in('sync').emit('status', historicSync);
|
||||
};
|
||||
|
|
|
@ -22,6 +22,7 @@ var BAD_GEN_ERROR = 'Bad genesis block. Network mismatch between Insight and bit
|
|||
var BAD_GEN_ERROR_DB = 'Bad genesis block. Network mismatch between Insight and levelDB? Insight is configured for:';
|
||||
function HistoricSync(opts) {
|
||||
opts = opts || {};
|
||||
this.shouldBroadcast = opts.shouldBroadcastSync;
|
||||
|
||||
this.network = config.network === 'testnet' ? networks.testnet: networks.livenet;
|
||||
|
||||
|
@ -31,7 +32,6 @@ function HistoricSync(opts) {
|
|||
this.genesis = genesisHashReversed.toString('hex');
|
||||
|
||||
this.rpc = new RpcClient(config.bitcoind);
|
||||
this.shouldBroadcast = opts.shouldBroadcastSync;
|
||||
this.sync = new Sync(opts);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,10 +5,13 @@ var Sync = require('./Sync');
|
|||
var Peer = require('bitcore/Peer');
|
||||
var config = require('../config/config');
|
||||
var networks = require('bitcore/networks');
|
||||
var sockets = require('../app/controllers/socket.js');
|
||||
|
||||
var peerdb_fn = 'peerdb.json';
|
||||
|
||||
function PeerSync(opts) {
|
||||
opts = opts|| {};
|
||||
this.shouldBroadcast = opts.shouldBroadcast;
|
||||
this.connected = false;
|
||||
this.peerdb = undefined;
|
||||
this.allowReorgs = false;
|
||||
|
@ -43,6 +46,7 @@ PeerSync.prototype.handleInv = function(info) {
|
|||
};
|
||||
|
||||
PeerSync.prototype.handleTx = function(info) {
|
||||
var self =this;
|
||||
var tx = info.message.tx.getStandardizedObject();
|
||||
tx.outs = info.message.tx.outs;
|
||||
tx.ins = info.message.tx.ins;
|
||||
|
@ -53,9 +57,20 @@ PeerSync.prototype.handleTx = function(info) {
|
|||
if (err) {
|
||||
console.log('[p2p_sync] Error in handle TX: ' + JSON.stringify(err));
|
||||
}
|
||||
else {
|
||||
if (self.shouldBroadcast) {
|
||||
sockets.broadcastTx(tx);
|
||||
if (tx.addrsToEmit) {
|
||||
tx.addrsToEmit.forEach(function(a) {
|
||||
sockets.broadcastAddressTx(a, tx.txid);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
PeerSync.prototype.handleBlock = function(info) {
|
||||
var self = this;
|
||||
var block = info.message.block;
|
||||
|
@ -67,7 +82,7 @@ PeerSync.prototype.handleBlock = function(info) {
|
|||
return bitcoreUtil.formatHashFull(tx.hash);
|
||||
});
|
||||
|
||||
this.sync.storeTipBlock({
|
||||
self.sync.storeTipBlock({
|
||||
'hash': blockHash,
|
||||
'tx': tx_hashes,
|
||||
'previousblockhash': bitcoreUtil.formatHashFull(block.prev_hash),
|
||||
|
@ -81,10 +96,15 @@ PeerSync.prototype.handleBlock = function(info) {
|
|||
else if (err) {
|
||||
console.log('[p2p_sync] Error in handle Block: ' + err);
|
||||
}
|
||||
else {
|
||||
if (self.shouldBroadcast) {
|
||||
sockets.broadcastBlock(blockHash);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
PeerSync.prototype.handle_connected = function(data) {
|
||||
PeerSync.prototype.handleConnected = function(data) {
|
||||
var peerman = data.pm;
|
||||
var peers_n = peerman.peers.length;
|
||||
console.log('[p2p_sync] Connected to ' + peers_n + ' peer' + (peers_n !== 1 ? 's' : ''));
|
||||
|
@ -104,7 +124,7 @@ PeerSync.prototype.run = function() {
|
|||
conn.on('block', self.handleBlock.bind(self));
|
||||
conn.on('tx', self.handleTx.bind(self));
|
||||
});
|
||||
this.peerman.on('connect', self.handle_connected.bind(self));
|
||||
this.peerman.on('connect', self.handleConnected.bind(self));
|
||||
|
||||
this.peerman.on('netDisconnected', function() {
|
||||
self.connected = false;
|
||||
|
|
24
lib/Sync.js
24
lib/Sync.js
|
@ -1,20 +1,18 @@
|
|||
'use strict';
|
||||
|
||||
var imports = require('soop').imports();
|
||||
var sockets = require('../app/controllers/socket.js');
|
||||
|
||||
var config = imports.config || require('../config/config');
|
||||
var networks = require('bitcore/networks');
|
||||
var async = require('async');
|
||||
|
||||
var syncId = 0;
|
||||
|
||||
function Sync(opts) {
|
||||
this.id = syncId++;
|
||||
this.opts = opts || {};
|
||||
this.bDb = require('./BlockDb').default();
|
||||
this.txDb = require('./TransactionDb').default();
|
||||
this.txDb.on('tx_for_address', this.handleTxForAddress.bind(this));
|
||||
this.txDb.on('new_tx', this.handleNewTx.bind(this));
|
||||
this.bDb.on('new_block', this.handleNewBlock.bind(this));
|
||||
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
|
||||
}
|
||||
|
||||
|
@ -252,24 +250,6 @@ Sync.prototype.setBranchConnectedBackwards = function(fromHash, cb) {
|
|||
};
|
||||
|
||||
|
||||
Sync.prototype.handleTxForAddress = function(data) {
|
||||
if (this.opts.shouldBroadcast) {
|
||||
sockets.broadcastAddressTx(data.address, data.txid);
|
||||
}
|
||||
};
|
||||
|
||||
Sync.prototype.handleNewTx = function(data) {
|
||||
if (this.opts.shouldBroadcast) {
|
||||
sockets.broadcastTx(data.tx);
|
||||
}
|
||||
};
|
||||
|
||||
Sync.prototype.handleNewBlock = function(data) {
|
||||
if (this.opts.shouldBroadcast) {
|
||||
sockets.broadcastBlock(data.blockid);
|
||||
}
|
||||
};
|
||||
|
||||
Sync.prototype.storeTxs = function(txs, cb) {
|
||||
var self = this;
|
||||
self.txDb.createFromArray(txs, null, function(err) {
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
var imports = require('soop').imports();
|
||||
|
||||
var ThisParent = imports.parent || require('events').EventEmitter;
|
||||
// blockHash -> txid mapping
|
||||
var IN_BLK_PREFIX = 'txb-'; //txb-<txid>-<block> => 1/0 (connected or not)
|
||||
|
||||
|
@ -51,7 +50,6 @@ var TransactionDb = function() {
|
|||
TransactionDb.super(this, arguments);
|
||||
this.network = config.network === 'testnet' ? networks.testnet : networks.livenet;
|
||||
};
|
||||
TransactionDb.parent = ThisParent;
|
||||
|
||||
TransactionDb.prototype.close = function(cb) {
|
||||
db.close(cb);
|
||||
|
@ -479,8 +477,10 @@ TransactionDb.prototype.getAddrStr = function(s) {
|
|||
case Script.TX_MULTISIG:
|
||||
var chunks = s.capture();
|
||||
chunks.forEach(function(chunk) {
|
||||
if (chunk && Buffer.isBuffer(chunk)) {
|
||||
var a = new Address(self.network.addressPubkey, bitutil.sha256ripe160(chunk));
|
||||
addrStrs.push(a.toString());
|
||||
}
|
||||
});
|
||||
break;
|
||||
case Script.TX_UNKNOWN:
|
||||
|
@ -615,17 +615,8 @@ TransactionDb.prototype.add = function(tx, blockhash, cb) {
|
|||
], function(err) {
|
||||
if (addrs.length > 0 && !blockhash) {
|
||||
// only emit if we are processing a single tx (not from a block)
|
||||
addrs.forEach(function(addr) {
|
||||
self.emit('tx_for_address', {
|
||||
address: addr,
|
||||
txid: tx.txid
|
||||
});
|
||||
});
|
||||
tx.addrsToEmit=addrs;
|
||||
}
|
||||
self.emit('new_tx', {
|
||||
tx: tx
|
||||
});
|
||||
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "insight-bitcore-api",
|
||||
"description": "An open-source bitcoin blockchain API. The Insight API provides you with a convenient, powerful and simple way to query and broadcast data on the bitcoin network and build your own services with it.",
|
||||
"version": "0.1.4",
|
||||
"version": "0.1.5",
|
||||
"author": {
|
||||
"name": "Ryan X Charles",
|
||||
"email": "ryan@bitpay.com"
|
||||
|
@ -50,7 +50,7 @@
|
|||
"start": "node node_modules/grunt-cli/bin/grunt"
|
||||
},
|
||||
"dependencies": {
|
||||
"bitcore": "git://github.com/bitpay/bitcore.git",
|
||||
"bitcore": "~0.1.8",
|
||||
"base58-native": "0.1.2",
|
||||
"async": "*",
|
||||
"leveldown": "*",
|
||||
|
|
Loading…
Reference in New Issue