Skip to content

Commit

Permalink
Merge pull request bnb-chain#33 from bnb-chain/issue_870
Browse files Browse the repository at this point in the history
[R4R]fix:Shift panic for zero length of heads (bnb-chain#870)
  • Loading branch information
unclezoro committed May 7, 2022
2 parents 0755c4e + 2c8bace commit 1e67a33
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 1e67a33

Please sign in to comment.