From 3cecfcb4e1f588aa56fd113e047e83fcac1d79ef Mon Sep 17 00:00:00 2001 From: qinglin89 <316032931@qq.com> Date: Sun, 24 Apr 2022 15:32:33 +0800 Subject: [PATCH 1/4] fix:Shift panic for zero length of heads --- core/state_prefetcher.go | 5 +++-- core/types/transaction.go | 4 ---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go index bf0a6b80c6..a748587f2e 100644 --- a/core/state_prefetcher.go +++ b/core/state_prefetcher.go @@ -137,10 +137,11 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce, default: } if count++; count%checkInterval == 0 { - if *txCurr == nil { + txTmp := *txCurr + if txTmp == nil { return } - txset.Forward(*txCurr) + txset.Forward(txTmp) } txCh <- tx txset.Shift() diff --git a/core/types/transaction.go b/core/types/transaction.go index e95cec25a6..f3982de0e9 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -505,10 +505,6 @@ func (t *TransactionsByPriceAndNonce) CurrentSize() int { //Forward moves current transaction to be the one which is one index after tx func (t *TransactionsByPriceAndNonce) Forward(tx *Transaction) { - if tx == nil { - t.heads = t.heads[0:0] - return - } //check whether target tx exists in t.heads for _, head := range t.heads { if tx == head { From 925f94c65d9c68c7858984a35df288a47c80ed09 Mon Sep 17 00:00:00 2001 From: qinglin89 <316032931@qq.com> Date: Sun, 24 Apr 2022 15:51:18 +0800 Subject: [PATCH 2/4] fix: make sure peek before shift --- core/state_prefetcher.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go index a748587f2e..aa64a03ed8 100644 --- a/core/state_prefetcher.go +++ b/core/state_prefetcher.go @@ -127,6 +127,13 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce, go func(txset *types.TransactionsByPriceAndNonce) { count := 0 for { + if count++; count%checkInterval == 0 { + txTmp := *txCurr + if txTmp == nil { + return + } + txset.Forward(txTmp) + } tx := txset.Peek() if tx == nil { return @@ -136,13 +143,6 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce, return default: } - if count++; count%checkInterval == 0 { - txTmp := *txCurr - if txTmp == nil { - return - } - txset.Forward(txTmp) - } txCh <- tx txset.Shift() } From fa8fd6444a3e1efb66fe3541ea60f6c8b9caf70b Mon Sep 17 00:00:00 2001 From: qinglin89 <316032931@qq.com> Date: Sun, 24 Apr 2022 17:04:32 +0800 Subject: [PATCH 3/4] refactor and update ut --- core/state_prefetcher.go | 6 +----- core/types/transaction.go | 6 ++++++ core/types/transaction_test.go | 5 ++++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go index aa64a03ed8..74428022b0 100644 --- a/core/state_prefetcher.go +++ b/core/state_prefetcher.go @@ -128,11 +128,7 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce, count := 0 for { if count++; count%checkInterval == 0 { - txTmp := *txCurr - if txTmp == nil { - return - } - txset.Forward(txTmp) + txset.Forward(*txCurr) } tx := txset.Peek() if tx == nil { diff --git a/core/types/transaction.go b/core/types/transaction.go index f3982de0e9..5c8d04c010 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -505,6 +505,12 @@ func (t *TransactionsByPriceAndNonce) CurrentSize() int { //Forward moves current transaction to be the one which is one index after tx func (t *TransactionsByPriceAndNonce) Forward(tx *Transaction) { + if tx == nil { + if len(t.heads) > 0 { + t.heads = t.heads[0:0] + } + return + } //check whether target tx exists in t.heads for _, head := range t.heads { if tx == head { diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index c81b7b8647..a5fbb50928 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -392,7 +392,7 @@ func TestTransactionForward(t *testing.T) { } tmp := txset.Copy() - for j := 0; j < 10; j++ { + for j := 0; j < 11; j++ { txset = tmp.Copy() txsetCpy = tmp.Copy() i := 0 @@ -400,6 +400,9 @@ func TestTransactionForward(t *testing.T) { txset.Shift() } tx := txset.Peek() + if tx == nil { + continue + } txsetCpy.Forward(tx) txCpy := txsetCpy.Peek() if txCpy == nil { From 6fb12f6c18f06936cd61973963a4f03fc9b5a5a6 Mon Sep 17 00:00:00 2001 From: qinglin89 <316032931@qq.com> Date: Sun, 24 Apr 2022 18:05:55 +0800 Subject: [PATCH 4/4] refactor --- core/state_prefetcher.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go index 74428022b0..a2c2df16a4 100644 --- a/core/state_prefetcher.go +++ b/core/state_prefetcher.go @@ -127,20 +127,21 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce, go func(txset *types.TransactionsByPriceAndNonce) { count := 0 for { - if count++; count%checkInterval == 0 { - txset.Forward(*txCurr) - } - tx := txset.Peek() - if tx == nil { - return - } select { case <-interruptCh: return default: + if count++; count%checkInterval == 0 { + txset.Forward(*txCurr) + } + tx := txset.Peek() + if tx == nil { + return + } + txCh <- tx + txset.Shift() + } - txCh <- tx - txset.Shift() } }(txs) }