Skip to content

Commit 0f5a4c8

Browse files
authored
[R4R]fix:Shift panic for zero length of heads (#870)
* fix:Shift panic for zero length of heads * fix: make sure peek before shift * refactor and update ut * refactor
1 parent 74ecbf2 commit 0f5a4c8

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

core/state_prefetcher.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,21 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce,
127127
go func(txset *types.TransactionsByPriceAndNonce) {
128128
count := 0
129129
for {
130-
tx := txset.Peek()
131-
if tx == nil {
132-
return
133-
}
134130
select {
135131
case <-interruptCh:
136132
return
137133
default:
138-
}
139-
if count++; count%checkInterval == 0 {
140-
if *txCurr == nil {
134+
if count++; count%checkInterval == 0 {
135+
txset.Forward(*txCurr)
136+
}
137+
tx := txset.Peek()
138+
if tx == nil {
141139
return
142140
}
143-
txset.Forward(*txCurr)
141+
txCh <- tx
142+
txset.Shift()
143+
144144
}
145-
txCh <- tx
146-
txset.Shift()
147145
}
148146
}(txs)
149147
}

core/types/transaction.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,9 @@ func (t *TransactionsByPriceAndNonce) CurrentSize() int {
506506
//Forward moves current transaction to be the one which is one index after tx
507507
func (t *TransactionsByPriceAndNonce) Forward(tx *Transaction) {
508508
if tx == nil {
509-
t.heads = t.heads[0:0]
509+
if len(t.heads) > 0 {
510+
t.heads = t.heads[0:0]
511+
}
510512
return
511513
}
512514
//check whether target tx exists in t.heads

core/types/transaction_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,17 @@ func TestTransactionForward(t *testing.T) {
392392
}
393393

394394
tmp := txset.Copy()
395-
for j := 0; j < 10; j++ {
395+
for j := 0; j < 11; j++ {
396396
txset = tmp.Copy()
397397
txsetCpy = tmp.Copy()
398398
i := 0
399399
for ; i < j; i++ {
400400
txset.Shift()
401401
}
402402
tx := txset.Peek()
403+
if tx == nil {
404+
continue
405+
}
403406
txsetCpy.Forward(tx)
404407
txCpy := txsetCpy.Peek()
405408
if txCpy == nil {

0 commit comments

Comments
 (0)