Insert genesis block if needed on db load.

Previously, the genesis block was only inserted when the database was
created, but it's possible due to rollback that the database is created
and the genesis block insert gets rolled back if the app is existed too
quickly.  This commit modifies the logic to test the need for the genesis
block any time the database is loaded and insert it if necessary.
This commit is contained in:
Dave Collins 2013-08-08 00:47:48 -05:00
parent aad69df74b
commit eab9c1f0c1

View File

@ -425,17 +425,6 @@ func loadBlockDB() (btcdb.Db, error) {
if err != nil {
return nil, err
}
// Insert the appropriate genesis block for the bitcoin network
// being connected to.
genesis := btcutil.NewBlock(activeNetParams.genesisBlock)
_, err := db.InsertBlock(genesis)
if err != nil {
db.Close()
return nil, err
}
log.Infof("[BMGR] Inserted genesis block %v",
activeNetParams.genesisHash)
}
// Get the latest block height from the database.
@ -444,6 +433,21 @@ func loadBlockDB() (btcdb.Db, error) {
db.Close()
return nil, err
}
// Insert the appropriate genesis block for the bitcoin network being
// connected to if needed.
if height == -1 {
genesis := btcutil.NewBlock(activeNetParams.genesisBlock)
_, err := db.InsertBlock(genesis)
if err != nil {
db.Close()
return nil, err
}
log.Infof("[BMGR] Inserted genesis block %v",
activeNetParams.genesisHash)
height = 0
}
log.Infof("[BMGR] Block database loaded with block height %d", height)
return db, nil
}