core, ethclient/gethclient: improve flaky tests (#25918)
* ethclient/gethclient: improve time-sensitive flaky test * eth/catalyst: fix (?) flaky test * core: stop blockchains in tests after use * core: fix dangling blockchain instances * core: rm whitespace * eth/gasprice, eth/tracers, consensus/clique: stop dangling blockchains in tests * all: address review concerns * core: goimports * eth/catalyst: fix another time-sensitive test * consensus/clique: add snapshot test run function * core: rename stop() to stopWithoutSaving() Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
co-authored by
Felix Lange
parent
deead99731
commit
067bac3f24
+28
-10
@@ -92,12 +92,21 @@ func TestEth2AssembleBlock(t *testing.T) {
|
||||
blockParams := beacon.PayloadAttributesV1{
|
||||
Timestamp: blocks[9].Time() + 5,
|
||||
}
|
||||
execData, err := assembleBlock(api, blocks[9].Hash(), &blockParams)
|
||||
if err != nil {
|
||||
t.Fatalf("error producing block, err=%v", err)
|
||||
// This test is a bit time-sensitive, the miner needs to pick up on the
|
||||
// txs in the pool. Therefore, we retry once if it fails on the first attempt.
|
||||
var testErr error
|
||||
for retries := 2; retries > 0; retries-- {
|
||||
if execData, err := assembleBlock(api, blocks[9].Hash(), &blockParams); err != nil {
|
||||
t.Fatalf("error producing block, err=%v", err)
|
||||
} else if have, want := len(execData.Transactions), 1; have != want {
|
||||
testErr = fmt.Errorf("invalid number of transactions, have %d want %d", have, want)
|
||||
} else {
|
||||
testErr = nil
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(execData.Transactions) != 1 {
|
||||
t.Fatalf("invalid number of transactions %d != 1", len(execData.Transactions))
|
||||
if testErr != nil {
|
||||
t.Fatal(testErr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,12 +122,21 @@ func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) {
|
||||
blockParams := beacon.PayloadAttributesV1{
|
||||
Timestamp: blocks[8].Time() + 5,
|
||||
}
|
||||
execData, err := assembleBlock(api, blocks[8].Hash(), &blockParams)
|
||||
if err != nil {
|
||||
t.Fatalf("error producing block, err=%v", err)
|
||||
// This test is a bit time-sensitive, the miner needs to pick up on the
|
||||
// txs in the pool. Therefore, we retry once if it fails on the first attempt.
|
||||
var testErr error
|
||||
for retries := 2; retries > 0; retries-- {
|
||||
if execData, err := assembleBlock(api, blocks[8].Hash(), &blockParams); err != nil {
|
||||
t.Fatalf("error producing block, err=%v", err)
|
||||
} else if have, want := len(execData.Transactions), blocks[9].Transactions().Len(); have != want {
|
||||
testErr = fmt.Errorf("invalid number of transactions, have %d want %d", have, want)
|
||||
} else {
|
||||
testErr = nil
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(execData.Transactions) != blocks[9].Transactions().Len() {
|
||||
t.Fatalf("invalid number of transactions %d != 1", len(execData.Transactions))
|
||||
if testErr != nil {
|
||||
t.Fatal(testErr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ func TestFeeHistory(t *testing.T) {
|
||||
oracle := NewOracle(backend, config)
|
||||
|
||||
first, reward, baseFee, ratio, err := oracle.FeeHistory(context.Background(), c.count, c.last, c.percent)
|
||||
|
||||
backend.teardown()
|
||||
expReward := c.expCount
|
||||
if len(c.percent) == 0 {
|
||||
expReward = 0
|
||||
|
||||
@@ -113,6 +113,12 @@ func (b *testBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) eve
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *testBackend) teardown() {
|
||||
b.chain.Stop()
|
||||
}
|
||||
|
||||
// newTestBackend creates a test backend. OBS: don't forget to invoke tearDown
|
||||
// after use, otherwise the blockchain instance will mem-leak via goroutines.
|
||||
func newTestBackend(t *testing.T, londonBlock *big.Int, pending bool) *testBackend {
|
||||
var (
|
||||
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||
@@ -198,6 +204,7 @@ func TestSuggestTipCap(t *testing.T) {
|
||||
|
||||
// The gas price sampled is: 32G, 31G, 30G, 29G, 28G, 27G
|
||||
got, err := oracle.SuggestTipCap(context.Background())
|
||||
backend.teardown()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to retrieve recommended gas price: %v", err)
|
||||
}
|
||||
|
||||
+23
-8
@@ -63,6 +63,8 @@ type testBackend struct {
|
||||
relHook func() // Hook is invoked when the requested state is released
|
||||
}
|
||||
|
||||
// testBackend creates a new test backend. OBS: After test is done, teardown must be
|
||||
// invoked in order to release associated resources.
|
||||
func newTestBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i int, b *core.BlockGen)) *testBackend {
|
||||
backend := &testBackend{
|
||||
chainConfig: gspec.Config,
|
||||
@@ -137,6 +139,11 @@ func (b *testBackend) ChainDb() ethdb.Database {
|
||||
return b.chaindb
|
||||
}
|
||||
|
||||
// teardown releases the associated resources.
|
||||
func (b *testBackend) teardown() {
|
||||
b.chain.Stop()
|
||||
}
|
||||
|
||||
func (b *testBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, StateReleaseFunc, error) {
|
||||
statedb, err := b.chain.StateAt(block.Root())
|
||||
if err != nil {
|
||||
@@ -198,13 +205,15 @@ func TestTraceCall(t *testing.T) {
|
||||
}
|
||||
genBlocks := 10
|
||||
signer := types.HomesteadSigner{}
|
||||
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
|
||||
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
|
||||
// Transfer from account[0] to account[1]
|
||||
// value: 1000 wei
|
||||
// fee: 0 wei
|
||||
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
}))
|
||||
})
|
||||
defer backend.teardown()
|
||||
api := NewAPI(backend)
|
||||
var testSuite = []struct {
|
||||
blockNumber rpc.BlockNumber
|
||||
call ethapi.TransactionArgs
|
||||
@@ -330,14 +339,16 @@ func TestTraceTransaction(t *testing.T) {
|
||||
}
|
||||
target := common.Hash{}
|
||||
signer := types.HomesteadSigner{}
|
||||
api := NewAPI(newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) {
|
||||
backend := newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) {
|
||||
// Transfer from account[0] to account[1]
|
||||
// value: 1000 wei
|
||||
// fee: 0 wei
|
||||
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
target = tx.Hash()
|
||||
}))
|
||||
})
|
||||
defer backend.chain.Stop()
|
||||
api := NewAPI(backend)
|
||||
result, err := api.TraceTransaction(context.Background(), target, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to trace transaction %v", err)
|
||||
@@ -371,13 +382,15 @@ func TestTraceBlock(t *testing.T) {
|
||||
}
|
||||
genBlocks := 10
|
||||
signer := types.HomesteadSigner{}
|
||||
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
|
||||
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
|
||||
// Transfer from account[0] to account[1]
|
||||
// value: 1000 wei
|
||||
// fee: 0 wei
|
||||
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
}))
|
||||
})
|
||||
defer backend.chain.Stop()
|
||||
api := NewAPI(backend)
|
||||
|
||||
var testSuite = []struct {
|
||||
blockNumber rpc.BlockNumber
|
||||
@@ -449,13 +462,15 @@ func TestTracingWithOverrides(t *testing.T) {
|
||||
}
|
||||
genBlocks := 10
|
||||
signer := types.HomesteadSigner{}
|
||||
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
|
||||
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
|
||||
// Transfer from account[0] to account[1]
|
||||
// value: 1000 wei
|
||||
// fee: 0 wei
|
||||
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
}))
|
||||
})
|
||||
defer backend.chain.Stop()
|
||||
api := NewAPI(backend)
|
||||
randomAccounts := newAccounts(3)
|
||||
type res struct {
|
||||
Gas int
|
||||
|
||||
Reference in New Issue
Block a user