Skip to content

Commit

Permalink
[R4R]fix:Shift panic for zero length of heads (bnb-chain#870)
Browse files Browse the repository at this point in the history
* fix:Shift panic for zero length of heads

* fix: make sure peek before shift

* refactor and update ut

* refactor
  • Loading branch information
qinglin89 authored and j75689 committed May 7, 2022
1 parent 0755c4e commit 2c8bace
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
18 changes: 8 additions & 10 deletions core/state_prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,21 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce,
go func(txset *types.TransactionsByPriceAndNonce) {
count := 0
for {
tx := txset.Peek()
if tx == nil {
return
}
select {
case <-interruptCh:
return
default:
}
if count++; count%checkInterval == 0 {
if *txCurr == nil {
if count++; count%checkInterval == 0 {
txset.Forward(*txCurr)
}
tx := txset.Peek()
if tx == nil {
return
}
txset.Forward(*txCurr)
txCh <- tx
txset.Shift()

}
txCh <- tx
txset.Shift()
}
}(txs)
}
Expand Down
4 changes: 3 additions & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,9 @@ 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]
if len(t.heads) > 0 {
t.heads = t.heads[0:0]
}
return
}
//check whether target tx exists in t.heads
Expand Down
5 changes: 4 additions & 1 deletion core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,17 @@ 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
for ; i < j; i++ {
txset.Shift()
}
tx := txset.Peek()
if tx == nil {
continue
}
txsetCpy.Forward(tx)
txCpy := txsetCpy.Peek()
if txCpy == nil {
Expand Down

0 comments on commit 2c8bace

Please sign in to comment.