chain: refactor open.

This commit is contained in:
Christopher Jeffrey 2016-09-24 00:23:24 -07:00
parent 6589cdc95b
commit b7f82990c3
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -218,23 +218,26 @@ ChainDB.layout = layout;
*/
ChainDB.prototype._open = co(function* open() {
var result, genesis, block;
var state, block, entry;
this.logger.info('Starting chain load.');
yield this.db.open();
result = yield this.db.has(layout.e(this.network.genesis.hash));
yield this.db.checkVersion('V', 1);
if (result) {
yield this.initState();
state = yield this.db.get(layout.R);
if (state) {
// Grab the chainstate if we have one.
this.state = ChainState.fromRaw(state);
} else {
// Otherwise write the genesis block.
// (We assume this database is fresh).
block = bcoin.block.fromRaw(this.network.genesisBlock, 'hex');
block.setHeight(0);
genesis = bcoin.chainentry.fromBlock(this.chain, block);
yield this.save(genesis, block, new CoinView());
entry = bcoin.chainentry.fromBlock(this.chain, block);
yield this.save(entry, block, new CoinView());
}
this.logger.info('Chain successfully loaded.');
@ -245,8 +248,6 @@ ChainDB.prototype._open = co(function* open() {
this.state.tx,
this.state.coin,
utils.btc(this.state.value));
yield this.db.checkVersion('V', 1);
});
/**