From 525d3f2cfdd54e4283d230648c0d1c9fc90621d3 Mon Sep 17 00:00:00 2001 From: Jin Date: Wed, 15 Nov 2023 09:25:45 +0800 Subject: [PATCH] BUG:Missing index info for genesis transactions --- core/blockchain/blockchain.go | 5 ++++ database/chaindb/chaindb.go | 4 ---- database/chaindb/chaindb_upgrade.go | 24 +++++++++++++++++++ .../legacychaindb/legacychaindb_upgrade.go | 11 +++++++++ services/index/index_manager.go | 10 -------- 5 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 database/chaindb/chaindb_upgrade.go diff --git a/core/blockchain/blockchain.go b/core/blockchain/blockchain.go index 8378885c..69c0ee17 100644 --- a/core/blockchain/blockchain.go +++ b/core/blockchain/blockchain.go @@ -330,6 +330,11 @@ func (b *BlockChain) createChainState() error { if err != nil { return err } + // genesis tx index + err = b.DB().PutTxIdxEntrys(genesisBlock, ib) + if err != nil { + return err + } return b.bd.Commit() } diff --git a/database/chaindb/chaindb.go b/database/chaindb/chaindb.go index f29e0e09..b55c26f3 100644 --- a/database/chaindb/chaindb.go +++ b/database/chaindb/chaindb.go @@ -607,10 +607,6 @@ func (cdb *ChainDB) DeleteEstimateFee() error { return rawdb.DeleteEstimateFee(cdb.db) } -func (cdb *ChainDB) TryUpgrade(di *common.DatabaseInfo, interrupt <-chan struct{}) error { - return nil -} - func (cdb *ChainDB) StartTrack(info string) error { if cdb.diff != nil { return nil diff --git a/database/chaindb/chaindb_upgrade.go b/database/chaindb/chaindb_upgrade.go new file mode 100644 index 00000000..ed1996fa --- /dev/null +++ b/database/chaindb/chaindb_upgrade.go @@ -0,0 +1,24 @@ +package chaindb + +import ( + "github.com/Qitmeer/qng/database/common" + "github.com/Qitmeer/qng/database/rawdb" + "github.com/Qitmeer/qng/params" +) + +func (cdb *ChainDB) TryUpgrade(di *common.DatabaseInfo, interrupt <-chan struct{}) error { + if di.Version() == common.CurrentDatabaseVersion { + // To fix old data, re index old genesis transaction data. + txId := params.ActiveNetParams.GenesisBlock.Transactions()[0].Hash() + _, blockHash, err := cdb.GetTxIdxEntry(txId, false) + if err != nil { + return err + } + if blockHash == nil { + log.Info("Re index genesis transaction for database") + return rawdb.WriteTxLookupEntriesByBlock(cdb.db, params.ActiveNetParams.GenesisBlock, 0) + } + return nil + } + return nil +} diff --git a/database/legacychaindb/legacychaindb_upgrade.go b/database/legacychaindb/legacychaindb_upgrade.go index 9791c8cc..3937711f 100644 --- a/database/legacychaindb/legacychaindb_upgrade.go +++ b/database/legacychaindb/legacychaindb_upgrade.go @@ -11,12 +11,23 @@ import ( "github.com/Qitmeer/qng/database/legacydb" l "github.com/Qitmeer/qng/log" "github.com/Qitmeer/qng/meerdag" + "github.com/Qitmeer/qng/params" "github.com/schollz/progressbar/v3" "math" ) func (cdb *LegacyChainDB) TryUpgrade(di *common.DatabaseInfo, interrupt <-chan struct{}) error { if di.Version() == common.CurrentDatabaseVersion { + // To fix old data, re index old genesis transaction data. + txId := params.ActiveNetParams.GenesisBlock.Transactions()[0].Hash() + _, blockHash, err := cdb.GetTxIdxEntry(txId, false) + if err != nil { + return err + } + if blockHash == nil { + log.Info("Re index genesis transaction for legacy database") + return cdb.doPutTxIndexEntrys(params.ActiveNetParams.GenesisBlock, 0) + } return nil } else if di.Version() > 13 { return fmt.Errorf("The data is temporarily incompatible.") diff --git a/services/index/index_manager.go b/services/index/index_manager.go index 6619f37a..10edea43 100644 --- a/services/index/index_manager.go +++ b/services/index/index_manager.go @@ -73,16 +73,6 @@ func (m *Manager) Init() error { return err } } - if m.consensus.BlockChain().GetMainOrder() == 0 { - sblock, block, err := m.consensus.BlockChain().FetchBlockByOrder(0) - if err != nil { - return err - } - err = m.ConnectBlock(sblock, block, nil) - if err != nil { - return err - } - } return nil }