eth/catalyst: make tests less time-sensitive (#26201)

This makes a couple of sometimes-failing tests less brittle.
pull/26245/head
Martin Holst Swende 2 years ago committed by GitHub
parent c2e0abce2e
commit 6975f09998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 66
      eth/catalyst/api_test.go

@ -93,22 +93,28 @@ func TestEth2AssembleBlock(t *testing.T) {
blockParams := beacon.PayloadAttributesV1{ blockParams := beacon.PayloadAttributesV1{
Timestamp: blocks[9].Time() + 5, Timestamp: blocks[9].Time() + 5,
} }
// This test is a bit time-sensitive, the miner needs to pick up on the // The miner needs to pick up on the txs in the pool, so a few retries might be
// txs in the pool. Therefore, we retry once if it fails on the first attempt. // needed.
var testErr error if _, testErr := assembleWithTransactions(api, blocks[9].Hash(), &blockParams, 1); testErr != nil {
for retries := 2; retries > 0; retries-- { t.Fatal(testErr)
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
} }
}
// assembleWithTransactions tries to assemble a block, retrying until it has 'want',
// number of transactions in it, or it has retried three times.
func assembleWithTransactions(api *ConsensusAPI, parentHash common.Hash, params *beacon.PayloadAttributesV1, want int) (execData *beacon.ExecutableDataV1, err error) {
for retries := 3; retries > 0; retries-- {
execData, err = assembleBlock(api, parentHash, params)
if err != nil {
return nil, err
} }
if testErr != nil { if have, want := len(execData.Transactions), want; have != want {
t.Fatal(testErr) err = fmt.Errorf("invalid number of transactions, have %d want %d", have, want)
continue
}
return execData, nil
} }
return nil, err
} }
func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) { func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) {
@ -123,21 +129,10 @@ func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) {
blockParams := beacon.PayloadAttributesV1{ blockParams := beacon.PayloadAttributesV1{
Timestamp: blocks[8].Time() + 5, Timestamp: blocks[8].Time() + 5,
} }
// This test is a bit time-sensitive, the miner needs to pick up on the // The miner needs to pick up on the txs in the pool, so a few retries might be
// txs in the pool. Therefore, we retry once if it fails on the first attempt. // needed.
var testErr error if _, err := assembleWithTransactions(api, blocks[8].Hash(), &blockParams, blocks[9].Transactions().Len()); err != nil {
for retries := 2; retries > 0; retries-- { t.Fatal(err)
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 testErr != nil {
t.Fatal(testErr)
} }
} }
@ -293,9 +288,9 @@ func TestEth2NewBlock(t *testing.T) {
tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey)
ethservice.TxPool().AddLocal(tx) ethservice.TxPool().AddLocal(tx)
execData, err := assembleBlock(api, parent.Hash(), &beacon.PayloadAttributesV1{ execData, err := assembleWithTransactions(api, parent.Hash(), &beacon.PayloadAttributesV1{
Timestamp: parent.Time() + 5, Timestamp: parent.Time() + 5,
}) }, 1)
if err != nil { if err != nil {
t.Fatalf("Failed to create the executable data %v", err) t.Fatalf("Failed to create the executable data %v", err)
} }
@ -915,17 +910,6 @@ func TestSimultaneousNewBlock(t *testing.T) {
parent = preMergeBlocks[len(preMergeBlocks)-1] parent = preMergeBlocks[len(preMergeBlocks)-1]
) )
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
statedb, _ := ethservice.BlockChain().StateAt(parent.Root())
ethservice.TxPool().AddLocal(types.MustSignNewTx(testKey, types.LatestSigner(ethservice.BlockChain().Config()),
&types.DynamicFeeTx{
Nonce: statedb.GetNonce(testAddr),
Value: big.NewInt(0),
GasFeeCap: big.NewInt(2 * params.InitialBaseFee),
GasTipCap: big.NewInt(2 * params.InitialBaseFee),
ChainID: genesis.Config.ChainID,
Gas: 1000000,
To: &common.Address{99},
}))
execData, err := assembleBlock(api, parent.Hash(), &beacon.PayloadAttributesV1{ execData, err := assembleBlock(api, parent.Hash(), &beacon.PayloadAttributesV1{
Timestamp: parent.Time() + 5, Timestamp: parent.Time() + 5,
}) })

Loading…
Cancel
Save