From eab9c1f0c1f592d56ccb1398a45e3a08ce40c0d2 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 8 Aug 2013 00:47:48 -0500 Subject: [PATCH] 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. --- blockmanager.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/blockmanager.go b/blockmanager.go index 30ccfef1..b7330ae8 100644 --- a/blockmanager.go +++ b/blockmanager.go @@ -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 }