add chain.compact. orphan failsafe.
This commit is contained in:
parent
35ba5a5de5
commit
fc45025131
@ -345,6 +345,17 @@ Chain.prototype.add = function add(block) {
|
|||||||
this.orphan.count--;
|
this.orphan.count--;
|
||||||
} while (true);
|
} while (true);
|
||||||
|
|
||||||
|
// Failsafe for large orphan chains
|
||||||
|
if (this.orphan.count > 10000) {
|
||||||
|
this.orphan.map = {};
|
||||||
|
this.orphan.bmap = {};
|
||||||
|
this.orphan.count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No need to have a huge chain
|
||||||
|
// if (this.size() > 100000)
|
||||||
|
// this.compact();
|
||||||
|
|
||||||
// Compress old blocks
|
// Compress old blocks
|
||||||
this._compress();
|
this._compress();
|
||||||
|
|
||||||
@ -652,36 +663,19 @@ Chain.prototype.retarget = function retarget(last, firstTs) {
|
|||||||
assert(false);
|
assert(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.compact = function compact() {
|
Chain.prototype.compact = function compact(keep) {
|
||||||
assert(false);
|
var index = this._compact(keep);
|
||||||
|
this.index.hashes = index.hashes;
|
||||||
|
this.index.ts = index.ts;
|
||||||
|
this.index.heights = index.heights;
|
||||||
|
this.index.bloom.reset();
|
||||||
|
this.index.hashes.forEach(function(hash) {
|
||||||
|
this.index.bloom.add(hash, 'hex');
|
||||||
|
}, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype._save = function(hash, obj) {
|
Chain.prototype._compact = function _compact(keep) {
|
||||||
var self = this;
|
keep = keep || 1000;
|
||||||
|
|
||||||
if (!this.storage)
|
|
||||||
return;
|
|
||||||
|
|
||||||
this.storage.put(this.prefix + hash, obj, function(err) {
|
|
||||||
if (err)
|
|
||||||
self.emit('error', err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Chain.prototype._delete = function(hash) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
if (!this.storage)
|
|
||||||
return;
|
|
||||||
|
|
||||||
this.storage.del(this.prefix + hash, function(err) {
|
|
||||||
if (err)
|
|
||||||
self.emit('error', err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Chain.prototype.toJSON = function toJSON() {
|
|
||||||
var keep = 1000;
|
|
||||||
|
|
||||||
// Keep only last 1000 consequent blocks, dilate others at:
|
// Keep only last 1000 consequent blocks, dilate others at:
|
||||||
// 7 day range for blocks before 2013
|
// 7 day range for blocks before 2013
|
||||||
@ -728,15 +722,48 @@ Chain.prototype.toJSON = function toJSON() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
v: 1,
|
|
||||||
type: 'chain',
|
|
||||||
network: network.type,
|
|
||||||
hashes: first.hashes.concat(last.hashes),
|
hashes: first.hashes.concat(last.hashes),
|
||||||
ts: first.ts.concat(last.ts),
|
ts: first.ts.concat(last.ts),
|
||||||
heights: first.heights.concat(last.heights)
|
heights: first.heights.concat(last.heights)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Chain.prototype._save = function(hash, obj) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
if (!this.storage)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.storage.put(this.prefix + hash, obj, function(err) {
|
||||||
|
if (err)
|
||||||
|
self.emit('error', err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Chain.prototype._delete = function(hash) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
if (!this.storage)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.storage.del(this.prefix + hash, function(err) {
|
||||||
|
if (err)
|
||||||
|
self.emit('error', err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Chain.prototype.toJSON = function toJSON() {
|
||||||
|
var index = this._compact();
|
||||||
|
return {
|
||||||
|
v: 1,
|
||||||
|
type: 'chain',
|
||||||
|
network: network.type,
|
||||||
|
hashes: index.hashes,
|
||||||
|
ts: index.ts,
|
||||||
|
heights: index.heights
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
Chain.prototype.fromJSON = function fromJSON(json) {
|
Chain.prototype.fromJSON = function fromJSON(json) {
|
||||||
var i;
|
var i;
|
||||||
|
|
||||||
|
|||||||
@ -260,6 +260,13 @@ Chain.prototype.add = function add(block) {
|
|||||||
this.orphan.count--;
|
this.orphan.count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Failsafe for large orphan chains
|
||||||
|
if (this.orphan.count > 10000) {
|
||||||
|
this.orphan.map = {};
|
||||||
|
this.orphan.bmap = {};
|
||||||
|
this.orphan.count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -537,7 +544,6 @@ Chain.prototype._delete = function(hash) {
|
|||||||
|
|
||||||
Chain.prototype.toJSON = function toJSON() {
|
Chain.prototype.toJSON = function toJSON() {
|
||||||
var entries = this.index.entries;
|
var entries = this.index.entries;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
v: 1,
|
v: 1,
|
||||||
type: 'chain',
|
type: 'chain',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user