From 93a5c376962b3435ade7654c8057270d208d8864 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 16 Dec 2020 13:14:30 +0300 Subject: [PATCH] core: fix bug with mempool.verifiedMap Transaction is added to verifiedMap before OOM check, so we may have a case when OOM occurs during tx1 pooling, but mp.containsKey(tx1) returns `true` after this. Fixed. --- pkg/core/mempool/mem_pool.go | 2 +- pkg/core/mempool/mem_pool_test.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/core/mempool/mem_pool.go b/pkg/core/mempool/mem_pool.go index 15be93292..112752f95 100644 --- a/pkg/core/mempool/mem_pool.go +++ b/pkg/core/mempool/mem_pool.go @@ -216,7 +216,6 @@ func (mp *Pool) Add(t *transaction.Transaction, fee Feer, data ...interface{}) e mp.oracleResp[id] = t.Hash() } - mp.verifiedMap[t.Hash()] = t if fee.P2PSigExtensionsEnabled() { // Remove conflicting transactions. for _, conflictingTx := range conflictsToBeRemoved { @@ -254,6 +253,7 @@ func (mp *Pool) Add(t *transaction.Transaction, fee Feer, data ...interface{}) e copy(mp.verifiedTxes[n+1:], mp.verifiedTxes[n:]) mp.verifiedTxes[n] = pItem } + mp.verifiedMap[t.Hash()] = t if fee.P2PSigExtensionsEnabled() { // Add conflicting hashes to the mp.conflicts list. for _, attr := range t.GetAttributes(transaction.ConflictsT) { diff --git a/pkg/core/mempool/mem_pool_test.go b/pkg/core/mempool/mem_pool_test.go index 6dc3d2a6b..64b9669ad 100644 --- a/pkg/core/mempool/mem_pool_test.go +++ b/pkg/core/mempool/mem_pool_test.go @@ -148,6 +148,9 @@ func TestOverCapacity(t *testing.T) { txcnt++ require.Error(t, mp.Add(tx, fs)) require.Equal(t, mempoolSize, mp.Count()) + require.Equal(t, mempoolSize, len(mp.verifiedMap)) + require.Equal(t, mempoolSize, len(mp.verifiedTxes)) + require.False(t, mp.containsKey(tx.Hash())) require.Equal(t, true, sort.IsSorted(sort.Reverse(mp.verifiedTxes))) // Low net fee, but higher per-byte fee is still a better combination.