better system for avoiding duplicate txs.

This commit is contained in:
Christopher Jeffrey 2016-01-29 16:08:33 -08:00
parent 3959b9cf5d
commit 41516b4866

View File

@ -134,7 +134,7 @@ function Pool(options) {
};
this.tx = {
have: {},
state: {},
count: 0
};
@ -815,12 +815,12 @@ Pool.prototype._createPeer = function _createPeer(backoff, priority) {
});
peer.on('tx', function(tx) {
var requested, added;
var state = self.tx.state[tx.hash('hex')];
requested = self._response(tx);
added = self._addTX(tx);
self._response(tx);
self._addTX(tx, 1);
if (added || tx.block)
if (state !== 1 || tx.block)
self.emit('tx', tx, peer);
if (!self.options.fullNode && tx.block)
@ -840,7 +840,7 @@ Pool.prototype._createPeer = function _createPeer(backoff, priority) {
self.emit('txs', txs, peer);
txs.forEach(function(hash) {
hash = utils.toHex(hash);
if (self._addTX(hash))
if (self._addTX(hash, 0))
self._request('tx', hash, peer);
});
self._scheduleRequests();
@ -935,19 +935,21 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
});
};
Pool.prototype._addTX = function(hash) {
Pool.prototype._addTX = function(hash, state) {
if (utils.isBuffer(hash))
hash = utils.toHex(hash);
else if (hash.hash)
hash = hash.hash('hex');
if (this.tx.count >= 10000) {
this.tx.have = {};
if (this.tx.count >= 5000) {
this.tx.state = {};
this.tx.count = 0;
}
if (!this.tx.have[hash]) {
this.tx.have[hash] = true;
if (this.tx.state[hash] == null) {
if (state == null)
state = 1;
this.tx.state[hash] = state;
this.tx.count++;
return true;
}