From e98d8b8b65d86296014cd310c0e438043ebbac22 Mon Sep 17 00:00:00 2001 From: itsdevbear Date: Wed, 31 Jan 2024 00:22:59 -0500 Subject: [PATCH 1/3] ugh --- cosmos/runtime/txpool/ante.go | 1 - cosmos/runtime/txpool/comet.go | 31 +++++++++---------------------- cosmos/runtime/txpool/mempool.go | 4 ---- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/cosmos/runtime/txpool/ante.go b/cosmos/runtime/txpool/ante.go index 18ea5905c..00215f04b 100644 --- a/cosmos/runtime/txpool/ante.go +++ b/cosmos/runtime/txpool/ante.go @@ -53,7 +53,6 @@ func (m *Mempool) AnteHandle( if shouldEject := m.shouldEjectFromCometMempool( ctx.BlockTime().Unix(), ethTx, ); shouldEject { - m.crc.DropRemoteTx(ethTx.Hash()) telemetry.IncrCounter(float32(1), MetricKeyAnteEjectedTxs) return ctx, errors.New("eject from comet mempool") } diff --git a/cosmos/runtime/txpool/comet.go b/cosmos/runtime/txpool/comet.go index c40809db9..82d3c68d6 100644 --- a/cosmos/runtime/txpool/comet.go +++ b/cosmos/runtime/txpool/comet.go @@ -21,14 +21,14 @@ package txpool import ( - "sync" "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/lru" ) const ( - defaultCacheSize = 4096 + defaultCacheSize = 100000 ) // CometRemoteCache is used to mark which txs are added to our Polaris node remotely from @@ -37,43 +37,30 @@ type CometRemoteCache interface { IsRemoteTx(txHash common.Hash) bool MarkRemoteSeen(txHash common.Hash) TimeFirstSeen(txHash common.Hash) int64 // Unix timestamp - DropRemoteTx(txHash common.Hash) } // Thread-safe implementation of CometRemoteCache. type cometRemoteCache struct { - timeInserted map[common.Hash]int64 - timeInsertedMu sync.RWMutex + timeInserted *lru.Cache[common.Hash, int64] } func newCometRemoteCache() *cometRemoteCache { return &cometRemoteCache{ - timeInserted: make(map[common.Hash]int64, defaultCacheSize), + + timeInserted: lru.NewCache[common.Hash, int64](defaultCacheSize), } } func (crc *cometRemoteCache) IsRemoteTx(txHash common.Hash) bool { - crc.timeInsertedMu.RLock() - defer crc.timeInsertedMu.RUnlock() - _, ok := crc.timeInserted[txHash] - return ok + return crc.timeInserted.Contains(txHash) } // Record the time the tx was inserted from Comet successfully. func (crc *cometRemoteCache) MarkRemoteSeen(txHash common.Hash) { - crc.timeInsertedMu.Lock() - crc.timeInserted[txHash] = time.Now().Unix() - crc.timeInsertedMu.Unlock() + crc.timeInserted.Add(txHash, time.Now().Unix()) } func (crc *cometRemoteCache) TimeFirstSeen(txHash common.Hash) int64 { - crc.timeInsertedMu.RLock() - defer crc.timeInsertedMu.RUnlock() - return crc.timeInserted[txHash] -} - -func (crc *cometRemoteCache) DropRemoteTx(txHash common.Hash) { - crc.timeInsertedMu.Lock() - delete(crc.timeInserted, txHash) - crc.timeInsertedMu.Unlock() + i, _ := crc.timeInserted.Get(txHash) + return i } diff --git a/cosmos/runtime/txpool/mempool.go b/cosmos/runtime/txpool/mempool.go index cb63403fe..4a043d516 100644 --- a/cosmos/runtime/txpool/mempool.go +++ b/cosmos/runtime/txpool/mempool.go @@ -172,10 +172,6 @@ func (m *Mempool) Remove(tx sdk.Tx) error { if err := ethTx.UnmarshalBinary(txBz); err != nil { continue } - txHash := ethTx.Hash() - - // Remove the eth tx from comet seen tx cache. - m.crc.DropRemoteTx(txHash) } } return nil From fdfada924b88c6cad4b9be49a699823e3f305fd7 Mon Sep 17 00:00:00 2001 From: itsdevbear Date: Wed, 31 Jan 2024 00:27:46 -0500 Subject: [PATCH 2/3] boom --- cosmos/runtime/txpool/mempool.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/cosmos/runtime/txpool/mempool.go b/cosmos/runtime/txpool/mempool.go index 4a043d516..db7f9bad0 100644 --- a/cosmos/runtime/txpool/mempool.go +++ b/cosmos/runtime/txpool/mempool.go @@ -158,21 +158,5 @@ func (m *Mempool) Select(context.Context, [][]byte) mempool.Iterator { // Remove is an intentional no-op as the eth txpool handles removals. func (m *Mempool) Remove(tx sdk.Tx) error { - // Get the Eth payload envelope from the Cosmos transaction. - msgs := tx.GetMsgs() - if len(msgs) == 1 { - env, ok := utils.GetAs[*types.WrappedPayloadEnvelope](msgs[0]) - if !ok { - return nil - } - - // Unwrap the payload to unpack the individual eth transactions to remove from the txpool. - for _, txBz := range env.UnwrapPayload().ExecutionPayload.Transactions { - ethTx := new(ethtypes.Transaction) - if err := ethTx.UnmarshalBinary(txBz); err != nil { - continue - } - } - } return nil } From 7a0f5e9248eefbcba76ccf395b5c8bb2cff7caa9 Mon Sep 17 00:00:00 2001 From: itsdevbear Date: Wed, 31 Jan 2024 00:31:56 -0500 Subject: [PATCH 3/3] mark seen --- cosmos/runtime/txpool/comet.go | 10 +++++++--- cosmos/runtime/txpool/mempool.go | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cosmos/runtime/txpool/comet.go b/cosmos/runtime/txpool/comet.go index 82d3c68d6..4e5c5c68d 100644 --- a/cosmos/runtime/txpool/comet.go +++ b/cosmos/runtime/txpool/comet.go @@ -35,7 +35,7 @@ const ( // Comet CheckTX and when. type CometRemoteCache interface { IsRemoteTx(txHash common.Hash) bool - MarkRemoteSeen(txHash common.Hash) + MarkRemoteSeen(txHash common.Hash) bool TimeFirstSeen(txHash common.Hash) int64 // Unix timestamp } @@ -56,8 +56,12 @@ func (crc *cometRemoteCache) IsRemoteTx(txHash common.Hash) bool { } // Record the time the tx was inserted from Comet successfully. -func (crc *cometRemoteCache) MarkRemoteSeen(txHash common.Hash) { - crc.timeInserted.Add(txHash, time.Now().Unix()) +func (crc *cometRemoteCache) MarkRemoteSeen(txHash common.Hash) bool { + if !crc.timeInserted.Contains(txHash) { + crc.timeInserted.Add(txHash, time.Now().Unix()) + return true + } + return false } func (crc *cometRemoteCache) TimeFirstSeen(txHash common.Hash) int64 { diff --git a/cosmos/runtime/txpool/mempool.go b/cosmos/runtime/txpool/mempool.go index db7f9bad0..0d58c8b2a 100644 --- a/cosmos/runtime/txpool/mempool.go +++ b/cosmos/runtime/txpool/mempool.go @@ -140,7 +140,7 @@ func (m *Mempool) Insert(ctx context.Context, sdkTx sdk.Tx) error { } // Add the eth tx to the remote cache. - m.crc.MarkRemoteSeen(ethTx.Hash()) + _ = m.crc.MarkRemoteSeen(ethTx.Hash()) return nil } @@ -157,6 +157,6 @@ func (m *Mempool) Select(context.Context, [][]byte) mempool.Iterator { } // Remove is an intentional no-op as the eth txpool handles removals. -func (m *Mempool) Remove(tx sdk.Tx) error { +func (m *Mempool) Remove(_ sdk.Tx) error { return nil }