ethclient/simulated: change test to explicitly specify nonces

This commit is contained in:
kashitaka 2025-07-28 11:57:48 +09:00
parent 8bdbf7f3eb
commit 74caa26c1c
2 changed files with 16 additions and 25 deletions

View file

@ -52,7 +52,7 @@ func simTestBackend(testAddr common.Address) *Backend {
)
}
func newBlobTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error) {
func newBlobTx(sim *Backend, key *ecdsa.PrivateKey, nonce uint64) (*types.Transaction, error) {
client := sim.Client()
testBlob := &kzg4844.Blob{0x00}
@ -67,12 +67,8 @@ func newBlobTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error)
addr := crypto.PubkeyToAddress(key.PublicKey)
chainid, _ := client.ChainID(context.Background())
nonce, err := client.PendingNonceAt(context.Background(), addr)
if err != nil {
return nil, err
}
chainidU256, _ := uint256.FromBig(chainid)
tx := types.NewTx(&types.BlobTx{
ChainID: chainidU256,
GasTipCap: gasTipCapU256,
@ -88,7 +84,7 @@ func newBlobTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error)
return types.SignTx(tx, types.LatestSignerForChainID(chainid), key)
}
func newTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error) {
func newTx(sim *Backend, key *ecdsa.PrivateKey, nonce uint64) (*types.Transaction, error) {
client := sim.Client()
// create a signed transaction to send
@ -96,10 +92,7 @@ func newTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error) {
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(params.GWei))
addr := crypto.PubkeyToAddress(key.PublicKey)
chainid, _ := client.ChainID(context.Background())
nonce, err := client.PendingNonceAt(context.Background(), addr)
if err != nil {
return nil, err
}
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainid,
Nonce: nonce,
@ -161,7 +154,7 @@ func TestSendTransaction(t *testing.T) {
client := sim.Client()
ctx := context.Background()
signedTx, err := newTx(sim, testKey)
signedTx, err := newTx(sim, testKey, 0)
if err != nil {
t.Errorf("could not create transaction: %v", err)
}
@ -252,7 +245,7 @@ func TestForkResendTx(t *testing.T) {
parent, _ := client.HeaderByNumber(ctx, nil)
// 2.
tx, err := newTx(sim, testKey)
tx, err := newTx(sim, testKey, 0)
if err != nil {
t.Fatalf("could not create transaction: %v", err)
}
@ -297,7 +290,7 @@ func TestCommitReturnValue(t *testing.T) {
}
// Create a block in the original chain (containing a transaction to force different block hashes)
tx, _ := newTx(sim, testKey)
tx, _ := newTx(sim, testKey, 0)
if err := client.SendTransaction(ctx, tx); err != nil {
t.Errorf("sending transaction: %v", err)
}

View file

@ -38,10 +38,9 @@ func TestTransactionRollbackBehavior(t *testing.T) {
defer sim.Close()
client := sim.Client()
btx0 := testSendSignedTx(t, testKey, sim, true)
tx0 := testSendSignedTx(t, testKey2, sim, false)
time.Sleep(200 * time.Millisecond) // Wait to avoid nonce race condition
tx1 := testSendSignedTx(t, testKey2, sim, false)
btx0 := testSendSignedTx(t, testKey, sim, true, 0)
tx0 := testSendSignedTx(t, testKey2, sim, false, 0)
tx1 := testSendSignedTx(t, testKey2, sim, false, 1)
sim.Rollback()
@ -49,10 +48,9 @@ func TestTransactionRollbackBehavior(t *testing.T) {
t.Fatalf("all transactions were not rolled back")
}
btx2 := testSendSignedTx(t, testKey, sim, true)
tx2 := testSendSignedTx(t, testKey2, sim, false)
time.Sleep(200 * time.Millisecond) // Wait to avoid nonce race condition
tx3 := testSendSignedTx(t, testKey2, sim, false)
btx2 := testSendSignedTx(t, testKey, sim, true, 0)
tx2 := testSendSignedTx(t, testKey2, sim, false, 0)
tx3 := testSendSignedTx(t, testKey2, sim, false, 1)
sim.Commit()
@ -63,7 +61,7 @@ func TestTransactionRollbackBehavior(t *testing.T) {
// testSendSignedTx sends a signed transaction to the simulated backend.
// It does not commit the block.
func testSendSignedTx(t *testing.T, key *ecdsa.PrivateKey, sim *Backend, isBlobTx bool) *types.Transaction {
func testSendSignedTx(t *testing.T, key *ecdsa.PrivateKey, sim *Backend, isBlobTx bool, nonce uint64) *types.Transaction {
t.Helper()
client := sim.Client()
ctx := context.Background()
@ -73,9 +71,9 @@ func testSendSignedTx(t *testing.T, key *ecdsa.PrivateKey, sim *Backend, isBlobT
signedTx *types.Transaction
)
if isBlobTx {
signedTx, err = newBlobTx(sim, key)
signedTx, err = newBlobTx(sim, key, nonce)
} else {
signedTx, err = newTx(sim, key)
signedTx, err = newTx(sim, key, nonce)
}
if err != nil {
t.Fatalf("failed to create transaction: %v", err)