more chain methods.
This commit is contained in:
parent
4c1a3761af
commit
a4084d8252
@ -154,7 +154,7 @@ Chain.prototype._addIndex = function _addIndex(hash, ts, height) {
|
||||
return new Error('Already added.');
|
||||
|
||||
var pos = utils.binaryInsert(this.index.ts, ts, compareTs, true);
|
||||
var checkpoint, obj;
|
||||
var checkpoint;
|
||||
|
||||
// Avoid duplicates
|
||||
if (this.index.hashes[pos] === hash
|
||||
@ -178,14 +178,9 @@ Chain.prototype._addIndex = function _addIndex(hash, ts, height) {
|
||||
this.index.heights.splice(pos, 0, height);
|
||||
this.index.bloom.add(hash, 'hex');
|
||||
|
||||
if (!this.storage)
|
||||
return;
|
||||
|
||||
obj = { ts: ts, height: height };
|
||||
|
||||
this.storage.put(this.prefix + hash, obj, function(err) {
|
||||
if (err)
|
||||
self.emit('error', err);
|
||||
this._save(hash, {
|
||||
ts: ts,
|
||||
height: height
|
||||
});
|
||||
};
|
||||
|
||||
@ -256,13 +251,7 @@ Chain.prototype._killFork = function _killFork(probe) {
|
||||
this.index.heights.splice(index, 1);
|
||||
|
||||
// Delete both blocks, let's see what others will choose
|
||||
if (!this.storage)
|
||||
return true;
|
||||
|
||||
this.storage.del(this.prefix + hash, function(err) {
|
||||
if (err)
|
||||
self.emit('error', err);
|
||||
});
|
||||
this._delete(hash);
|
||||
|
||||
return true;
|
||||
};
|
||||
@ -404,7 +393,7 @@ Chain.prototype.byHeight = function byHeight(height) {
|
||||
var index = this.index.heights.indexOf(height);
|
||||
|
||||
if (index === -1)
|
||||
return -1;
|
||||
return null;
|
||||
|
||||
return {
|
||||
index: index,
|
||||
@ -414,6 +403,16 @@ Chain.prototype.byHeight = function byHeight(height) {
|
||||
};
|
||||
};
|
||||
|
||||
Chain.prototype.getTip = function() {
|
||||
var index = this.index.hashes.length - 1;
|
||||
return {
|
||||
index: index,
|
||||
hash: this.index.hashes[index],
|
||||
ts: this.index.ts[index],
|
||||
height: this.index.heights[index]
|
||||
};
|
||||
};
|
||||
|
||||
Chain.prototype.byHash = function byHash(hash) {
|
||||
if (this.loading)
|
||||
return null;
|
||||
@ -426,7 +425,7 @@ Chain.prototype.byHash = function byHash(hash) {
|
||||
var index = this.index.hashes.indexOf(hash);
|
||||
|
||||
if (index === -1)
|
||||
return -1;
|
||||
return null;
|
||||
|
||||
return {
|
||||
index: index,
|
||||
@ -603,6 +602,34 @@ Chain.prototype.size = function size() {
|
||||
return this.index.hashes.length;
|
||||
};
|
||||
|
||||
Chain.prototype.height = function height() {
|
||||
return this.getTip().height;
|
||||
};
|
||||
|
||||
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 keep = 1000;
|
||||
|
||||
|
||||
@ -159,11 +159,8 @@ Chain.prototype.resetHeight = function resetHeight(height) {
|
||||
|
||||
this.index.lastTs = this.index.entries[this.index.entries.length - 1].ts;
|
||||
|
||||
if (!this.storage)
|
||||
return;
|
||||
|
||||
forked.forEach(function(entry) {
|
||||
self._del(entry);
|
||||
self._delete(entry);
|
||||
});
|
||||
};
|
||||
|
||||
@ -279,7 +276,7 @@ Chain.prototype.byHeight = function byHeight(height) {
|
||||
if (this.loading)
|
||||
return null;
|
||||
|
||||
return this.index.entries[height];
|
||||
return this.index.entries[height] || null;
|
||||
};
|
||||
|
||||
Chain.prototype.byHash = function byHash(hash) {
|
||||
@ -294,6 +291,13 @@ Chain.prototype.byHash = function byHash(hash) {
|
||||
return this.byHeight(this.index.heights[hash]);
|
||||
};
|
||||
|
||||
Chain.prototype.getTip = function getTip() {
|
||||
if (this.loading)
|
||||
return null;
|
||||
|
||||
return this.index.entries[this.index.entries.length - 1];
|
||||
};
|
||||
|
||||
Chain.prototype.hasBlock = function hasBlock(hash) {
|
||||
if (this.loading)
|
||||
return false;
|
||||
@ -435,25 +439,29 @@ Chain.prototype.size = function size() {
|
||||
return this.index.entries.length;
|
||||
};
|
||||
|
||||
Chain.prototype._save = function(entry) {
|
||||
Chain.prototype.height = function height() {
|
||||
return this.getTip().height;
|
||||
};
|
||||
|
||||
Chain.prototype._save = function(hash, obj) {
|
||||
var self = this;
|
||||
|
||||
if (!this.storage)
|
||||
return;
|
||||
|
||||
this.storage.put(this.prefix + entry.hash, entry.toJSON(), function(err) {
|
||||
this.storage.put(this.prefix + hash, obj.toJSON(), function(err) {
|
||||
if (err)
|
||||
self.emit('error', err);
|
||||
});
|
||||
};
|
||||
|
||||
Chain.prototype._del = function(entry) {
|
||||
Chain.prototype._delete = function(hash) {
|
||||
var self = this;
|
||||
|
||||
if (!this.storage)
|
||||
return;
|
||||
|
||||
this.storage.del(this.prefix + entry.hash, function(err) {
|
||||
this.storage.del(this.prefix + hash, function(err) {
|
||||
if (err)
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
@ -852,10 +852,10 @@ TX.prototype.getConfirmations = function getConfirmations(chain) {
|
||||
if (!chain)
|
||||
return 0;
|
||||
|
||||
top = chain.getHeight(chain.index.hashes[chain.index.hashes.length - 1]);
|
||||
top = chain.height();
|
||||
height = this.getHeight(chain);
|
||||
|
||||
if (top === -1 || height === -1)
|
||||
if (height === -1)
|
||||
return 0;
|
||||
|
||||
return top - height + 1;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user