From afb56489af4d2b151624994fce56ee8004e4903d Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 8 Apr 2024 09:44:08 +0800 Subject: [PATCH] small optimisations (#261) * mutex don't cover GetSigners don't clone header in context * Update CHANGELOG.md Signed-off-by: yihuang * update comment --------- Signed-off-by: yihuang --- types/context.go | 6 ++---- types/mempool/priority_nonce.go | 5 +++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/types/context.go b/types/context.go index 36a072b0ed0f..e07f4e7de160 100644 --- a/types/context.go +++ b/types/context.go @@ -7,7 +7,6 @@ import ( abci "github.com/cometbft/cometbft/abci/types" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" - "github.com/cosmos/gogoproto/proto" "cosmossdk.io/core/comet" "cosmossdk.io/core/header" @@ -111,10 +110,9 @@ func (c Context) TxCount() int { return c.txCou func (c Context) BlockGasUsed() uint64 { return c.blockGasUsed } func (c Context) BlockGasWanted() uint64 { return c.blockGasWanted } -// clone the header before returning +// BlockHeader returns the header by value (shallow copy). func (c Context) BlockHeader() cmtproto.Header { - msg := proto.Clone(&c.header).(*cmtproto.Header) - return *msg + return c.header } // HeaderHash returns a copy of the header hash obtained during abci.RequestBeginBlock diff --git a/types/mempool/priority_nonce.go b/types/mempool/priority_nonce.go index ba09f37cdbbe..81e06c7fe247 100644 --- a/types/mempool/priority_nonce.go +++ b/types/mempool/priority_nonce.go @@ -444,8 +444,6 @@ func (mp *PriorityNonceMempool[C]) CountTx() int { // Remove removes a transaction from the mempool in O(log n) time, returning an // error if unsuccessful. func (mp *PriorityNonceMempool[C]) Remove(tx sdk.Tx) error { - mp.mtx.Lock() - defer mp.mtx.Unlock() sigs, err := mp.cfg.SignerExtractor.GetSigners(tx) if err != nil { return err @@ -458,6 +456,9 @@ func (mp *PriorityNonceMempool[C]) Remove(tx sdk.Tx) error { sender := sig.Signer.String() nonce := sig.Sequence + mp.mtx.Lock() + defer mp.mtx.Unlock() + scoreKey := txMeta[C]{nonce: nonce, sender: sender} score, ok := mp.scores[scoreKey] if !ok {