chain: misc refactoring.

This commit is contained in:
Christopher Jeffrey 2016-09-30 23:44:38 -07:00
parent 79e70d7bbb
commit d4778d21b2
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 32 additions and 49 deletions

View File

@ -796,8 +796,7 @@ Chain.prototype.reorganize = co(function* reorganize(entry, block) {
*/
Chain.prototype.disconnect = co(function* disconnect(entry) {
var items = yield this.db.disconnect(entry);
var block = items[1];
var block = yield this.db.disconnect(entry);
var prev = yield entry.getPrevious();
assert(prev);
@ -1026,18 +1025,15 @@ Chain.prototype.add = co(function* add(block) {
*/
Chain.prototype._add = co(function* add(block) {
var ret, initial, hash, prevBlock;
var height, checkpoint, orphan, entry;
var existing, prev;
assert(this.loaded);
ret = new VerifyResult();
initial = true;
var ret = new VerifyResult();
var initial = true;
var hash, prevBlock, height, checkpoint;
var orphan, entry, existing, prev;
while (block) {
hash = block.hash('hex');
prevBlock = block.prevBlock;
height = -1;
// Mark the start time.
this.mark();
@ -1096,7 +1092,8 @@ Chain.prototype._add = co(function* add(block) {
// Find the previous block height/index.
prev = yield this.db.get(prevBlock);
height = !prev ? -1 : prev.height + 1;
if (prev)
height = prev.height + 1;
if (height > this.bestHeight) {
this.bestHeight = height;
@ -1479,17 +1476,19 @@ Chain.prototype.isFull = function isFull() {
*/
Chain.prototype.isInitial = function isInitial() {
if (!this.tip)
return true;
if (this.synced)
return false;
if (this.height < this.network.checkpoints.lastHeight)
return true;
return this.height < this.bestHeight - 24 * 6
|| this.tip.ts < utils.now() - this.network.block.maxTipAge;
if (this.height < this.bestHeight - 24 * 6)
return true;
if (this.tip.ts < utils.now() - this.network.block.maxTipAge)
return true;
return false;
};
/**
@ -1498,15 +1497,9 @@ Chain.prototype.isInitial = function isInitial() {
*/
Chain.prototype.getProgress = function getProgress() {
var start, current, end;
if (!this.tip)
return 0;
start = this.network.genesis.ts;
current = this.tip.ts - start;
end = utils.now() - start - 40 * 60;
var start = this.network.genesis.ts;
var current = this.tip.ts - start;
var end = utils.now() - start - 40 * 60;
return Math.min(1, current / end);
};
@ -1621,8 +1614,6 @@ Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) {
*/
Chain.prototype.getCurrentTarget = co(function* getCurrentTarget() {
if (!this.tip)
return this.network.pow.bits;
return yield this.getTargetAsync(null, this.tip);
});
@ -1921,12 +1912,8 @@ Chain.prototype.computeBlockVersion = co(function* computeBlockVersion(prev) {
*/
Chain.prototype.getDeploymentState = co(function* getDeploymentState() {
var prev, ancestors;
if (!this.tip)
return this.state;
prev = yield this.tip.getPrevious();
var prev = yield this.tip.getPrevious();
var ancestors;
if (!prev)
return this.state;

View File

@ -628,7 +628,7 @@ ChainDB.prototype.reconnect = co(function* reconnect(entry, block, view) {
yield this.commit();
return [entry, block];
return block;
});
/**
@ -676,7 +676,7 @@ ChainDB.prototype.disconnect = co(function* disconnect(entry) {
yield this.commit();
return [entry, block];
return block;
});
/**
@ -772,16 +772,16 @@ ChainDB.prototype.reset = co(function* reset(block) {
* Test whether the chain contains a block in the
* main chain or an alternate chain. Alternate chains will only
* be tested if the lookup is done by hash.
* @param {Hash|Number} height - Hash or height.
* @param {Hash|Number} block - Hash or height.
* @returns {Promise} - Returns Boolean.
*/
ChainDB.prototype.has = co(function* has(height) {
ChainDB.prototype.has = co(function* has(block) {
var items, hash;
checkHash(height);
checkHash(block);
items = yield this.getBoth(height);
items = yield this.getBoth(block);
hash = items[0];
return hash != null;
@ -1463,12 +1463,12 @@ ChainDB.prototype.getBlock = co(function* getBlock(hash) {
var items = yield this.getBoth(hash);
var height, data, block;
if (!items)
return;
hash = items[0];
height = items[1];
if (!hash)
return;
data = yield this.db.get(layout.b(hash));
if (!data)
@ -1486,16 +1486,12 @@ ChainDB.prototype.getBlock = co(function* getBlock(hash) {
* @returns {Promise} - Returns {@link Block}.
*/
ChainDB.prototype.getRawBlock = co(function* getRawBlock(hash) {
var items = yield this.getBoth(hash);
var height;
ChainDB.prototype.getRawBlock = co(function* getRawBlock(block) {
var hash = yield this.getHash(block);
if (!items)
if (!hash)
return;
hash = items[0];
height = items[1];
return yield this.db.get(layout.b(hash));
});