[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
This commit is contained in:
Leon 2022-04-25 10:47:02 +08:00 committed by GitHub
parent 74ecbf271d
commit 0f5a4c87db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 12 deletions

@ -127,23 +127,21 @@ func (p *statePrefetcher) PrefetchMining(txs *types.TransactionsByPriceAndNonce,
go func(txset *types.TransactionsByPriceAndNonce) { go func(txset *types.TransactionsByPriceAndNonce) {
count := 0 count := 0
for { for {
tx := txset.Peek()
if tx == nil {
return
}
select { select {
case <-interruptCh: case <-interruptCh:
return return
default: default:
} if count++; count%checkInterval == 0 {
if count++; count%checkInterval == 0 { txset.Forward(*txCurr)
if *txCurr == nil { }
tx := txset.Peek()
if tx == nil {
return return
} }
txset.Forward(*txCurr) txCh <- tx
txset.Shift()
} }
txCh <- tx
txset.Shift()
} }
}(txs) }(txs)
} }

@ -506,7 +506,9 @@ func (t *TransactionsByPriceAndNonce) CurrentSize() int {
//Forward moves current transaction to be the one which is one index after tx //Forward moves current transaction to be the one which is one index after tx
func (t *TransactionsByPriceAndNonce) Forward(tx *Transaction) { func (t *TransactionsByPriceAndNonce) Forward(tx *Transaction) {
if tx == nil { if tx == nil {
t.heads = t.heads[0:0] if len(t.heads) > 0 {
t.heads = t.heads[0:0]
}
return return
} }
//check whether target tx exists in t.heads //check whether target tx exists in t.heads

@ -392,7 +392,7 @@ func TestTransactionForward(t *testing.T) {
} }
tmp := txset.Copy() tmp := txset.Copy()
for j := 0; j < 10; j++ { for j := 0; j < 11; j++ {
txset = tmp.Copy() txset = tmp.Copy()
txsetCpy = tmp.Copy() txsetCpy = tmp.Copy()
i := 0 i := 0
@ -400,6 +400,9 @@ func TestTransactionForward(t *testing.T) {
txset.Shift() txset.Shift()
} }
tx := txset.Peek() tx := txset.Peek()
if tx == nil {
continue
}
txsetCpy.Forward(tx) txsetCpy.Forward(tx)
txCpy := txsetCpy.Peek() txCpy := txsetCpy.Peek()
if txCpy == nil { if txCpy == nil {