From e464e16a334510b07fe2500434af6ce13f3eb39c Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Wed, 20 Dec 2023 12:43:50 +0100 Subject: [PATCH] Stop notifications of duplicate insertions into ethereum type mempool --- bchain/coins/eth/ethrpc.go | 6 ++++-- bchain/mempool_ethereum_type.go | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/bchain/coins/eth/ethrpc.go b/bchain/coins/eth/ethrpc.go index 3ed83f5d..edf69ea4 100644 --- a/bchain/coins/eth/ethrpc.go +++ b/bchain/coins/eth/ethrpc.go @@ -238,8 +238,10 @@ func (b *EthereumRPC) subscribeEvents() error { if glog.V(2) { glog.Info("rpc: new tx ", hex) } - b.Mempool.AddTransactionToMempool(hex) - b.PushHandler(bchain.NotificationNewTx) + added := b.Mempool.AddTransactionToMempool(hex) + if added { + b.PushHandler(bchain.NotificationNewTx) + } } }() diff --git a/bchain/mempool_ethereum_type.go b/bchain/mempool_ethereum_type.go index dc90e4f7..aa1fbb53 100644 --- a/bchain/mempool_ethereum_type.go +++ b/bchain/mempool_ethereum_type.go @@ -132,8 +132,8 @@ func (m *MempoolEthereumType) Resync() (int, error) { return entries, nil } -// AddTransactionToMempool adds transactions to mempool -func (m *MempoolEthereumType) AddTransactionToMempool(txid string) { +// AddTransactionToMempool adds transactions to mempool, returns true if tx added to mempool, false if not added (for example duplicate call) +func (m *MempoolEthereumType) AddTransactionToMempool(txid string) bool { m.mux.Lock() _, exists := m.txEntries[txid] m.mux.Unlock() @@ -143,7 +143,7 @@ func (m *MempoolEthereumType) AddTransactionToMempool(txid string) { if !exists { entry, ok := m.createTxEntry(txid, uint32(time.Now().Unix())) if !ok { - return + return false } m.mux.Lock() m.txEntries[txid] = entry @@ -152,6 +152,7 @@ func (m *MempoolEthereumType) AddTransactionToMempool(txid string) { } m.mux.Unlock() } + return !exists } // RemoveTransactionFromMempool removes transaction from mempool