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

View File

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