Skip to content

Commit

Permalink
Optimize estimatefee database
Browse files Browse the repository at this point in the history
  • Loading branch information
lochjin committed Sep 9, 2023
1 parent 8c87803 commit 6961ecf
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 44 deletions.
7 changes: 0 additions & 7 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/Qitmeer/qng/core/blockchain"
"github.com/Qitmeer/qng/core/event"
"github.com/Qitmeer/qng/core/protocol"
"github.com/Qitmeer/qng/database/legacychaindb"
"github.com/Qitmeer/qng/database/legacydb"
"github.com/Qitmeer/qng/engine/txscript"
"github.com/Qitmeer/qng/log"
"github.com/Qitmeer/qng/meerevm/amana"
Expand Down Expand Up @@ -71,11 +69,6 @@ func (s *consensus) DatabaseContext() model.DataBase {
return s.databaseContext
}

// TODO: Will be deleted in the future, will be completely replaced by DatabaseContext in the future
func (s *consensus) LegacyDB() legacydb.DB {
return s.databaseContext.(*legacychaindb.LegacyChainDB).DB()
}

func (s *consensus) Config() *config.Config {
return s.cfg
}
Expand Down
2 changes: 0 additions & 2 deletions consensus/model/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/config"
"github.com/Qitmeer/qng/core/event"
"github.com/Qitmeer/qng/database/legacydb"
"github.com/Qitmeer/qng/engine/txscript"
"github.com/Qitmeer/qng/node/service"
"github.com/Qitmeer/qng/params"
Expand All @@ -16,7 +15,6 @@ type Consensus interface {
GenesisHash() *hash.Hash
Config() *config.Config
DatabaseContext() DataBase
LegacyDB() legacydb.DB //TODO: Will be deleted in the future
BlockChain() BlockChain
IndexManager() IndexManager
Events() *event.Feed
Expand Down
3 changes: 3 additions & 0 deletions consensus/model/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,7 @@ type DataBase interface {
CleanAddrIdx(finish bool) error
IsLegacy() bool
TryUpgrade(di *common.DatabaseInfo, interrupt <-chan struct{}) error
GetEstimateFee() ([]byte, error)
PutEstimateFee(data []byte) error
DeleteEstimateFee() error
}
12 changes: 12 additions & 0 deletions database/chaindb/chaindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,18 @@ func (cdb *ChainDB) IsLegacy() bool {
return false
}

func (cdb *ChainDB) GetEstimateFee() ([]byte, error) {
return rawdb.ReadEstimateFee(cdb.db), nil
}

func (cdb *ChainDB) PutEstimateFee(data []byte) error {
return rawdb.WriteEstimateFee(cdb.db, data)
}

func (cdb *ChainDB) DeleteEstimateFee() error {
return rawdb.DeleteEstimateFee(cdb.db)
}

func (cdb *ChainDB) TryUpgrade(di *common.DatabaseInfo, interrupt <-chan struct{}) error {
return nil
}
Expand Down
25 changes: 25 additions & 0 deletions database/legacychaindb/legacychaindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Qitmeer/qng/core/dbnamespace"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/database/legacydb"
"github.com/Qitmeer/qng/database/rawdb"
"github.com/Qitmeer/qng/meerdag"
"github.com/Qitmeer/qng/params"
"math"
Expand Down Expand Up @@ -624,6 +625,30 @@ func (cdb *LegacyChainDB) IsLegacy() bool {
return true
}

func (cdb *LegacyChainDB) GetEstimateFee() ([]byte, error) {
feeEstimationData := []byte{}
err := cdb.db.View(func(dbTx legacydb.Tx) error {
metadata := dbTx.Metadata()
feeEstimationData = metadata.Get(rawdb.EstimateFeeDatabaseKey)
return nil
})
return feeEstimationData, err
}

func (cdb *LegacyChainDB) PutEstimateFee(data []byte) error {
return cdb.db.Update(func(dbTx legacydb.Tx) error {
metadata := dbTx.Metadata()
return metadata.Put(rawdb.EstimateFeeDatabaseKey, data)
})
}

func (cdb *LegacyChainDB) DeleteEstimateFee() error {
return cdb.db.Update(func(dbTx legacydb.Tx) error {
metadata := dbTx.Metadata()
return metadata.Delete(rawdb.EstimateFeeDatabaseKey)
})
}

func New(cfg *config.Config, interrupt <-chan struct{}) (*LegacyChainDB, error) {
// Load the block database.
db, err := LoadBlockDB(cfg)
Expand Down
23 changes: 22 additions & 1 deletion database/rawdb/accessors_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore) {
// best chain state
func ReadBestChainState(db ethdb.Reader) []byte {
data, err := db.Get(bestChainStateKey)
if len(data) == 0 {
if err != nil {
log.Error(err.Error())
return nil
}
Expand All @@ -215,3 +215,24 @@ func WriteBestChainState(db ethdb.KeyValueWriter, data []byte) error {
}
return db.Put(bestChainStateKey, data)
}

// estimatefee
func ReadEstimateFee(db ethdb.Reader) []byte {
data, err := db.Get(EstimateFeeDatabaseKey)
if err != nil {
log.Error(err.Error())
return nil
}
return data
}

func WriteEstimateFee(db ethdb.KeyValueWriter, data []byte) error {
if len(data) <= 0 {
return nil
}
return db.Put(EstimateFeeDatabaseKey, data)
}

func DeleteEstimateFee(db ethdb.KeyValueWriter) error {
return db.Delete(EstimateFeeDatabaseKey)
}
4 changes: 4 additions & 0 deletions database/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ var (
// snapshot
SnapshotBlockOrderPrefix = []byte("o") // SnapshotBlockOrderPrefix + block order -> block id
SnapshotBlockStatusPrefix = []byte("s") // SnapshotBlockStatusPrefix + block id -> block status

// EstimateFeeDatabaseKey is the key that we use to
// store the fee estimator in the database.
EstimateFeeDatabaseKey = []byte("estimatefee")
)

// encodeBlockID encodes a block id as big endian uint64
Expand Down
6 changes: 0 additions & 6 deletions services/mempool/estimatefee.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ const (
UnminedHeight = 0x7fffffff
)

var (
// EstimateFeeDatabaseKey is the key that we use to
// store the fee estimator in the database.
EstimateFeeDatabaseKey = []byte("estimatefee")
)

type QitPerByte float64

type MeerPerKilobyte float64
Expand Down
43 changes: 15 additions & 28 deletions services/tx/txmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Qitmeer/qng/consensus/model"
"github.com/Qitmeer/qng/core/blockchain"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/database/legacydb"
"github.com/Qitmeer/qng/engine/txscript"
"github.com/Qitmeer/qng/meerdag"
"github.com/Qitmeer/qng/node/service"
Expand All @@ -25,9 +24,6 @@ type TxManager struct {
// notify
ntmgr model.Notify

// db
db legacydb.DB

//invalidTx hash->block hash
invalidTx map[hash.Hash]*meerdag.HashSet

Expand Down Expand Up @@ -62,25 +58,22 @@ func (tm *TxManager) initFeeEstimator() error {
}
// Search for a FeeEstimator state in the database. If none can be found
// or if it cannot be loaded, create a new one.
tm.db.Update(func(tx legacydb.Tx) error {
metadata := tx.Metadata()
feeEstimationData := metadata.Get(mempool.EstimateFeeDatabaseKey)
if feeEstimationData != nil {
// delete it from the database so that we don't try to restore the
// same thing again somehow.
metadata.Delete(mempool.EstimateFeeDatabaseKey)

// If there is an error, log it and make a new fee estimator.
var err error
tm.feeEstimator, err = mempool.RestoreFeeEstimator(feeEstimationData)
feeEstimationData, err := tm.consensus.DatabaseContext().GetEstimateFee()
if err != nil {
return err
}
if feeEstimationData != nil {
// delete it from the database so that we don't try to restore the
// same thing again somehow.
tm.consensus.DatabaseContext().DeleteEstimateFee()
// If there is an error, log it and make a new fee estimator.
var err error
tm.feeEstimator, err = mempool.RestoreFeeEstimator(feeEstimationData)

if err != nil {
log.Error(fmt.Sprintf("Failed to restore fee estimator %v", err))
}
if err != nil {
log.Error(fmt.Sprintf("Failed to restore fee estimator %v", err))
}

return nil
})
}

// If no feeEstimator has been found, or if the one that has been found
// is behind somehow, create a new one and start over.
Expand Down Expand Up @@ -110,12 +103,7 @@ func (tm *TxManager) Stop() error {

if tm.feeEstimator != nil {
// Save fee estimator state in the database.
tm.db.Update(func(tx legacydb.Tx) error {
metadata := tx.Metadata()
metadata.Put(mempool.EstimateFeeDatabaseKey, tm.feeEstimator.Save())

return nil
})
return tm.consensus.DatabaseContext().PutEstimateFee(tm.feeEstimator.Save())
}

return nil
Expand Down Expand Up @@ -240,7 +228,6 @@ func NewTxManager(consensus model.Consensus, ntmgr model.Notify) (*TxManager, er
indexManager: consensus.IndexManager().(*index.Manager),
txMemPool: txMemPool,
ntmgr: ntmgr,
db: consensus.LegacyDB(),
invalidTx: invalidTx,
enableFeeEst: cfg.Estimatefee}
consensus.BlockChain().(*blockchain.BlockChain).Subscribe(tm.handleNotifyMsg)
Expand Down

0 comments on commit 6961ecf

Please sign in to comment.