-
Notifications
You must be signed in to change notification settings - Fork 266
test(e2e): add preconf state consistency test #3082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fridrik01
wants to merge
1
commit into
preconf-dev
Choose a base branch
from
add-preconf-state-consistency-test
base: preconf-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+269
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| // | ||
| // Copyright (C) 2025, Berachain Foundation. All rights reserved. | ||
| // Use of this software is governed by the Business Source License included | ||
| // in the LICENSE file of this repository and at www.mariadb.com/bsl11. | ||
| // | ||
| // ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY | ||
| // TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER | ||
| // VERSIONS OF THE LICENSED WORK. | ||
| // | ||
| // THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF | ||
| // LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF | ||
| // LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). | ||
| // | ||
| // TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON | ||
| // AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, | ||
| // EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF | ||
| // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND | ||
| // TITLE. | ||
|
|
||
| //go:build e2e | ||
|
|
||
| package preconf_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rand" | ||
| "fmt" | ||
| "math/big" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/berachain/beacon-kit/testing/e2e/suite" | ||
| "github.com/berachain/beacon-kit/testing/e2e/suite/types" | ||
| "github.com/ethereum/go-ethereum/common" | ||
| ) | ||
|
|
||
| const ( | ||
| stateConsistencyTxsPerWorker = 20 | ||
| stateConsistencyPollInterval = 25 * time.Millisecond | ||
| stateConsistencyPerTxTimeout = 30 * time.Second | ||
| ) | ||
|
|
||
| type workerResult struct { | ||
| pendingBalMismatches int | ||
| pendingNonceMismatches int | ||
| inclusionBalMismatches int | ||
| inclusionNonceMismatches int | ||
| err error | ||
| } | ||
|
|
||
| // TestFlashblockStateConsistency verifies that the preconf RPC exposes *exactly correct* recipient balance | ||
| // and sender nonce at both the "pending" tag and at the inclusion block after each individual transaction. | ||
| func (s *PreconfE2ESuite) TestFlashblockStateConsistency() { | ||
| // Run test with single sender, fresh recipient, N sequential TXs. | ||
| s.Run("Serial", s.runStateConsistencySerial) | ||
| // Run test with one worker per test account, each with its own recipient. | ||
| s.Run("Parallel", s.runStateConsistencyParallel) | ||
| } | ||
|
|
||
| func (s *PreconfE2ESuite) runStateConsistencySerial() { | ||
| ctx := s.Ctx() | ||
| preconf := s.PreconfRPCClients(0) | ||
| gasTipCap, gasFeeCap := s.suggestGasCaps(preconf.Client) | ||
|
|
||
| r := s.runStateConsistencyWorker( | ||
| ctx, preconf, s.TestAccounts()[0], newRandomAddress(), | ||
| stateConsistencyTxsPerWorker, gasTipCap, gasFeeCap, | ||
| ) | ||
| s.Require().NoError(r.err) | ||
| s.Require().Zero(r.pendingBalMismatches) | ||
| s.Require().Zero(r.pendingNonceMismatches) | ||
| s.Require().Zero(r.inclusionBalMismatches) | ||
| s.Require().Zero(r.inclusionNonceMismatches) | ||
| } | ||
|
|
||
| func (s *PreconfE2ESuite) runStateConsistencyParallel() { | ||
| ctx := s.Ctx() | ||
| preconf := s.PreconfRPCClients(0) | ||
| gasTipCap, gasFeeCap := s.suggestGasCaps(preconf.Client) | ||
| senders := s.TestAccounts() | ||
|
|
||
| results := make([]workerResult, len(senders)) | ||
| var wg sync.WaitGroup | ||
| for i := range senders { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| results[i] = s.runStateConsistencyWorker( | ||
| ctx, preconf, senders[i], newRandomAddress(), | ||
| stateConsistencyTxsPerWorker, gasTipCap, gasFeeCap, | ||
| ) | ||
| }() | ||
| } | ||
| wg.Wait() | ||
|
|
||
| for i, r := range results { | ||
| s.Require().NoError(r.err, "worker %d fatal error", i) | ||
| s.Require().Zero(r.pendingBalMismatches) | ||
| s.Require().Zero(r.pendingNonceMismatches) | ||
| s.Require().Zero(r.inclusionBalMismatches) | ||
| s.Require().Zero(r.inclusionNonceMismatches) | ||
| } | ||
| } | ||
|
|
||
| // runStateConsistencyWorker sends numTxs ETH transfers and, for each TX, asserts exact recipient | ||
| // balance and sender nonce on the preconf RPC at both the "pending" tag and the inclusion block. | ||
| func (s *PreconfE2ESuite) runStateConsistencyWorker( | ||
| ctx context.Context, | ||
| preconf *types.ExecutionClient, | ||
| sender *types.EthAccount, | ||
| recipient common.Address, | ||
| numTxs int, | ||
| gasTipCap, gasFeeCap *big.Int, | ||
| ) workerResult { | ||
| var r workerResult | ||
| txValue := new(big.Int).SetUint64(suite.Ether / 1000) //nolint:mnd // 0.001 ETH | ||
| senderAddr := sender.Address() | ||
|
|
||
| startNonce, err := preconf.PendingNonceAt(ctx, senderAddr) | ||
| if err != nil { | ||
| r.err = fmt.Errorf("get initial pending nonce for %s: %w", senderAddr, err) | ||
| return r | ||
| } | ||
| lastScanned, err := preconf.BlockNumber(ctx) | ||
| if err != nil { | ||
| r.err = fmt.Errorf("get initial block number: %w", err) | ||
| return r | ||
| } | ||
|
|
||
| var firstBlock, lastBlock uint64 | ||
| for i := range numTxs { | ||
| expectedBal := new(big.Int).Mul(txValue, big.NewInt(int64(i+1))) | ||
| expectedNonce := startNonce + uint64(i+1) | ||
|
|
||
| tx, sErr := s.sendETHTransfer(transferParams{ | ||
| client: preconf.Client, | ||
| sender: sender, | ||
| to: recipient, | ||
| nonce: startNonce + uint64(i), | ||
| amount: txValue, | ||
| gasTipCap: gasTipCap, | ||
| gasFeeCap: gasFeeCap, | ||
| }) | ||
| if sErr != nil { | ||
| r.err = fmt.Errorf("send tx %d from %s: %w", i+1, senderAddr, sErr) | ||
| return r | ||
| } | ||
|
|
||
| if !waitForTxInPending(ctx, preconf, tx.Hash()) { | ||
| r.err = fmt.Errorf("tx %s from %s timed out waiting for pending inclusion", tx.Hash().Hex(), senderAddr) | ||
| return r | ||
| } | ||
| pendingBal, bErr := preconf.PendingBalanceAt(ctx, recipient) | ||
| if bErr != nil { | ||
| r.err = fmt.Errorf("get pending balance for %s: %w", recipient, bErr) | ||
| return r | ||
| } | ||
| if pendingBal.Cmp(expectedBal) != 0 { | ||
| r.pendingBalMismatches++ | ||
| s.T().Errorf("pending balance mismatch: tx=%d recipient=%s got=%s want=%s", i+1, recipient, pendingBal, expectedBal) | ||
| } | ||
| pendingNonce, nErr := preconf.PendingNonceAt(ctx, senderAddr) | ||
| if nErr != nil { | ||
| r.err = fmt.Errorf("get pending nonce for %s: %w", senderAddr, nErr) | ||
| return r | ||
| } | ||
| if pendingNonce != expectedNonce { | ||
| r.pendingNonceMismatches++ | ||
| s.T().Errorf("pending nonce mismatch: tx=%d sender=%s got=%d want=%d", i+1, senderAddr, pendingNonce, expectedNonce) | ||
| } | ||
|
|
||
| inclBlock := waitForTxInclusion(ctx, preconf, tx.Hash(), lastScanned) | ||
| if inclBlock == 0 { | ||
| r.err = fmt.Errorf("tx %s from %s timed out waiting for inclusion", tx.Hash().Hex(), senderAddr) | ||
| return r | ||
| } | ||
| if firstBlock == 0 { | ||
| firstBlock = inclBlock | ||
| } | ||
| lastBlock = inclBlock | ||
| lastScanned = inclBlock | ||
|
|
||
| inclBlockBig := new(big.Int).SetUint64(inclBlock) | ||
| inclBal, bErr := preconf.BalanceAt(ctx, recipient, inclBlockBig) | ||
| if bErr != nil { | ||
| r.err = fmt.Errorf("get balance for %s at block %d: %w", recipient, inclBlock, bErr) | ||
| return r | ||
| } | ||
| if inclBal.Cmp(expectedBal) != 0 { | ||
| r.inclusionBalMismatches++ | ||
| s.T().Errorf("inclusion balance mismatch: tx=%d recipient=%s block=%d got=%s want=%s", | ||
| i+1, recipient, inclBlock, inclBal, expectedBal) | ||
| } | ||
| inclNonce, nErr := preconf.NonceAt(ctx, senderAddr, inclBlockBig) | ||
| if nErr != nil { | ||
| r.err = fmt.Errorf("get nonce for %s at block %d: %w", senderAddr, inclBlock, nErr) | ||
| return r | ||
| } | ||
| if inclNonce != expectedNonce { | ||
| r.inclusionNonceMismatches++ | ||
| s.T().Errorf("inclusion nonce mismatch: tx=%d sender=%s block=%d got=%d want=%d", | ||
| i+1, senderAddr, inclBlock, inclNonce, expectedNonce) | ||
| } | ||
| } | ||
|
|
||
| s.T().Logf("verified %d txs from %s to %s in blocks %d-%d", | ||
| numTxs, senderAddr.Hex()[:10], recipient.Hex()[:10], firstBlock, lastBlock) | ||
| return r | ||
| } | ||
|
|
||
| // waitForTxInPending polls the preconf pending block until hash appears (or times out) | ||
| func waitForTxInPending(ctx context.Context, preconf *types.ExecutionClient, hash common.Hash) bool { | ||
| ctx, cancel := context.WithTimeout(ctx, stateConsistencyPerTxTimeout) | ||
| defer cancel() | ||
| tick := time.NewTicker(stateConsistencyPollInterval) | ||
| defer tick.Stop() | ||
| for { | ||
| pb, err := preconf.BlockByNumber(ctx, big.NewInt(-1)) | ||
| if err == nil && pb != nil { | ||
| for _, tx := range pb.Transactions() { | ||
| if tx.Hash() == hash { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| select { | ||
| case <-ctx.Done(): | ||
| return false | ||
| case <-tick.C: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // waitForTxInclusion scans newly-committed blocks (from lastScanned+1) on the given client until the tx is found. | ||
| func waitForTxInclusion(ctx context.Context, client *types.ExecutionClient, hash common.Hash, lastScanned uint64) uint64 { | ||
| ctx, cancel := context.WithTimeout(ctx, stateConsistencyPerTxTimeout) | ||
| defer cancel() | ||
| tick := time.NewTicker(stateConsistencyPollInterval) | ||
| defer tick.Stop() | ||
| scanFrom := lastScanned + 1 | ||
|
fridrik01 marked this conversation as resolved.
|
||
| for { | ||
| block, err := client.BlockByNumber(ctx, new(big.Int).SetUint64(scanFrom)) | ||
| if err != nil || block == nil { | ||
| // Block not yet committed (or transient RPC error); wait then retry. | ||
| select { | ||
| case <-ctx.Done(): | ||
| return 0 | ||
| case <-tick.C: | ||
| } | ||
| continue | ||
| } | ||
| for _, tx := range block.Transactions() { | ||
| if tx.Hash() == hash { | ||
| return scanFrom | ||
| } | ||
| } | ||
| scanFrom++ | ||
| } | ||
| } | ||
|
|
||
| // newRandomAddress returns a random 20-byte address. | ||
| func newRandomAddress() common.Address { | ||
| var b [common.AddressLength]byte | ||
| if _, err := rand.Read(b[:]); err != nil { | ||
| panic(fmt.Errorf("crypto/rand.Read: %w", err)) | ||
| } | ||
| return common.Address(b) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.