Skip to content

Commit

Permalink
eth, catalyst: fix flaky tests (ethereum#26153)
Browse files Browse the repository at this point in the history
* eth/catalyst: fix time-dependent (flaky) test

* eth: increase timeout on TestTransactionPropagation
  • Loading branch information
holiman authored and shekhirin committed Jun 6, 2023
1 parent 632443e commit 4ac1a54
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
74 changes: 44 additions & 30 deletions eth/catalyst/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,41 +567,55 @@ func TestNewPayloadOnInvalidChain(t *testing.T) {
var (
api = NewConsensusAPI(ethservice)
parent = ethservice.BlockChain().CurrentBlock()
signer = types.LatestSigner(ethservice.BlockChain().Config())
// This EVM code generates a log when the contract is created.
logCode = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
)
for i := 0; i < 10; i++ {
statedb, _ := ethservice.BlockChain().StateAt(parent.Root())
nonce := statedb.GetNonce(testAddr)
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)

params := beacon.PayloadAttributesV1{
Timestamp: parent.Time() + 1,
Random: crypto.Keccak256Hash([]byte{byte(i)}),
SuggestedFeeRecipient: parent.Coinbase(),
}

fcState := beacon.ForkchoiceStateV1{
HeadBlockHash: parent.Hash(),
SafeBlockHash: common.Hash{},
FinalizedBlockHash: common.Hash{},
}
resp, err := api.ForkchoiceUpdatedV1(fcState, &params)
if err != nil {
t.Fatalf("error preparing payload, err=%v", err)
}
if resp.PayloadStatus.Status != beacon.VALID {
t.Fatalf("error preparing payload, invalid status: %v", resp.PayloadStatus.Status)
}
// give the payload some time to be built
time.Sleep(100 * time.Millisecond)
payload, err := api.GetPayloadV1(*resp.PayloadID)
if err != nil {
t.Fatalf("can't get payload: %v", err)
}
if len(payload.Transactions) == 0 {
t.Fatalf("payload should not be empty")
tx := types.MustSignNewTx(testKey, signer, &types.LegacyTx{
Nonce: statedb.GetNonce(testAddr),
Value: new(big.Int),
Gas: 1000000,
GasPrice: big.NewInt(2 * params.InitialBaseFee),
Data: logCode,
})
ethservice.TxPool().AddRemotesSync([]*types.Transaction{tx})
var (
params = beacon.PayloadAttributesV1{
Timestamp: parent.Time() + 1,
Random: crypto.Keccak256Hash([]byte{byte(i)}),
SuggestedFeeRecipient: parent.Coinbase(),
}
fcState = beacon.ForkchoiceStateV1{
HeadBlockHash: parent.Hash(),
SafeBlockHash: common.Hash{},
FinalizedBlockHash: common.Hash{},
}
payload *beacon.ExecutableDataV1
resp beacon.ForkChoiceResponse
err error
)
for i := 0; ; i++ {
if resp, err = api.ForkchoiceUpdatedV1(fcState, &params); err != nil {
t.Fatalf("error preparing payload, err=%v", err)
}
if resp.PayloadStatus.Status != beacon.VALID {
t.Fatalf("error preparing payload, invalid status: %v", resp.PayloadStatus.Status)
}
// give the payload some time to be built
time.Sleep(50 * time.Millisecond)
if payload, err = api.GetPayloadV1(*resp.PayloadID); err != nil {
t.Fatalf("can't get payload: %v", err)
}
if len(payload.Transactions) > 0 {
break
}
// No luck this time we need to update the params and try again.
params.Timestamp = params.Timestamp + 1
if i > 10 {
t.Fatalf("payload should not be empty")
}
}
execResp, err := api.NewPayloadV1(*payload)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion eth/handler_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func testTransactionPropagation(t *testing.T, protocol uint) {
select {
case event := <-txChs[i]:
arrived += len(event.Txs)
case <-time.After(time.Second):
case <-time.After(2 * time.Second):
t.Errorf("sink %d: transaction propagation timed out: have %d, want %d", i, arrived, len(txs))
timeout = true
}
Expand Down

0 comments on commit 4ac1a54

Please sign in to comment.