diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index fa6447ec..1aea35f8 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -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); }); /**