Skip to content

Commit

Permalink
core: fix some flaky tests (#10)
Browse files Browse the repository at this point in the history
### Description

Merge upstream fix [1379](bnb-chain#1379).

The tests panic quite frequently.

### Rationale

The tests no longer panic. I manager to trigger the error (almost) every
time with this command:

```
seq 1 10 | parallel go test -count=1 ./eth/downloader -run "TestCanonicalSynchronisation66Full" -v
```

---------

Co-authored-by: 0xcb9ff9 <0xcb9ff9@proton.me>
Co-authored-by: Larry <92799281+brilliant-lx@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 20, 2023
1 parent d68e36f commit ff17138
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
8 changes: 7 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1546,14 +1546,20 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
}
}
// Garbage collect anything below our required write retention
wg2 := sync.WaitGroup{}
for !bc.triegc.Empty() {
root, number := bc.triegc.Pop()
if uint64(-number) > chosen {
bc.triegc.Push(root, number)
break
}
go triedb.Dereference(root.(common.Hash))
wg2.Add(1)
go func() {
triedb.Dereference(root.(common.Hash))
wg2.Done()
}()
}
wg2.Wait()
}
}
return nil
Expand Down
1 change: 1 addition & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (s *StateDB) StopPrefetcher() {
s.prefetcherLock.Lock()
if s.prefetcher != nil {
s.prefetcher.close()
s.prefetcher = nil
}
s.prefetcherLock.Unlock()
}
Expand Down
11 changes: 7 additions & 4 deletions eth/protocols/diff/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package diff

import (
"crypto/rand"
"math/big"
"math/rand"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -134,10 +134,13 @@ func testGetDiffLayers(t *testing.T, protocol uint) {
missDiffPackets := make([]FullDiffLayersPacket, 0)

for i := 0; i < 100; i++ {
number := uint64(rand.Int63n(1024))
if number == 0 {
continue
// Find a non 0 random number (1 ~ 1023)
n, err := rand.Int(rand.Reader, big.NewInt(1023))
if err != nil {
t.Fatalf("Failed to generate random number %v", err)
}

number := n.Uint64() + 1
foundHash := backend.chain.GetCanonicalHash(number + 1024)
missHash := backend.chain.GetCanonicalHash(number)
foundRlp := backend.chain.GetDiffLayerRLP(foundHash)
Expand Down

0 comments on commit ff17138

Please sign in to comment.