Skip to content

Commit 74caa26

Browse files
committed
ethclient/simulated: change test to explicitly specify nonces
1 parent 8bdbf7f commit 74caa26

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

ethclient/simulated/backend_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func simTestBackend(testAddr common.Address) *Backend {
5252
)
5353
}
5454

55-
func newBlobTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error) {
55+
func newBlobTx(sim *Backend, key *ecdsa.PrivateKey, nonce uint64) (*types.Transaction, error) {
5656
client := sim.Client()
5757

5858
testBlob := &kzg4844.Blob{0x00}
@@ -67,12 +67,8 @@ func newBlobTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error)
6767

6868
addr := crypto.PubkeyToAddress(key.PublicKey)
6969
chainid, _ := client.ChainID(context.Background())
70-
nonce, err := client.PendingNonceAt(context.Background(), addr)
71-
if err != nil {
72-
return nil, err
73-
}
74-
7570
chainidU256, _ := uint256.FromBig(chainid)
71+
7672
tx := types.NewTx(&types.BlobTx{
7773
ChainID: chainidU256,
7874
GasTipCap: gasTipCapU256,
@@ -88,18 +84,15 @@ func newBlobTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error)
8884
return types.SignTx(tx, types.LatestSignerForChainID(chainid), key)
8985
}
9086

91-
func newTx(sim *Backend, key *ecdsa.PrivateKey) (*types.Transaction, error) {
87+
func newTx(sim *Backend, key *ecdsa.PrivateKey, nonce uint64) (*types.Transaction, error) {
9288
client := sim.Client()
9389

9490
// create a signed transaction to send
9591
head, _ := client.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
9692
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(params.GWei))
9793
addr := crypto.PubkeyToAddress(key.PublicKey)
9894
chainid, _ := client.ChainID(context.Background())
99-
nonce, err := client.PendingNonceAt(context.Background(), addr)
100-
if err != nil {
101-
return nil, err
102-
}
95+
10396
tx := types.NewTx(&types.DynamicFeeTx{
10497
ChainID: chainid,
10598
Nonce: nonce,
@@ -161,7 +154,7 @@ func TestSendTransaction(t *testing.T) {
161154
client := sim.Client()
162155
ctx := context.Background()
163156

164-
signedTx, err := newTx(sim, testKey)
157+
signedTx, err := newTx(sim, testKey, 0)
165158
if err != nil {
166159
t.Errorf("could not create transaction: %v", err)
167160
}
@@ -252,7 +245,7 @@ func TestForkResendTx(t *testing.T) {
252245
parent, _ := client.HeaderByNumber(ctx, nil)
253246

254247
// 2.
255-
tx, err := newTx(sim, testKey)
248+
tx, err := newTx(sim, testKey, 0)
256249
if err != nil {
257250
t.Fatalf("could not create transaction: %v", err)
258251
}
@@ -297,7 +290,7 @@ func TestCommitReturnValue(t *testing.T) {
297290
}
298291

299292
// Create a block in the original chain (containing a transaction to force different block hashes)
300-
tx, _ := newTx(sim, testKey)
293+
tx, _ := newTx(sim, testKey, 0)
301294
if err := client.SendTransaction(ctx, tx); err != nil {
302295
t.Errorf("sending transaction: %v", err)
303296
}

ethclient/simulated/rollback_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,19 @@ func TestTransactionRollbackBehavior(t *testing.T) {
3838
defer sim.Close()
3939
client := sim.Client()
4040

41-
btx0 := testSendSignedTx(t, testKey, sim, true)
42-
tx0 := testSendSignedTx(t, testKey2, sim, false)
43-
time.Sleep(200 * time.Millisecond) // Wait to avoid nonce race condition
44-
tx1 := testSendSignedTx(t, testKey2, sim, false)
41+
btx0 := testSendSignedTx(t, testKey, sim, true, 0)
42+
tx0 := testSendSignedTx(t, testKey2, sim, false, 0)
43+
tx1 := testSendSignedTx(t, testKey2, sim, false, 1)
4544

4645
sim.Rollback()
4746

4847
if pendingStateHasTx(client, btx0) || pendingStateHasTx(client, tx0) || pendingStateHasTx(client, tx1) {
4948
t.Fatalf("all transactions were not rolled back")
5049
}
5150

52-
btx2 := testSendSignedTx(t, testKey, sim, true)
53-
tx2 := testSendSignedTx(t, testKey2, sim, false)
54-
time.Sleep(200 * time.Millisecond) // Wait to avoid nonce race condition
55-
tx3 := testSendSignedTx(t, testKey2, sim, false)
51+
btx2 := testSendSignedTx(t, testKey, sim, true, 0)
52+
tx2 := testSendSignedTx(t, testKey2, sim, false, 0)
53+
tx3 := testSendSignedTx(t, testKey2, sim, false, 1)
5654

5755
sim.Commit()
5856

@@ -63,7 +61,7 @@ func TestTransactionRollbackBehavior(t *testing.T) {
6361

6462
// testSendSignedTx sends a signed transaction to the simulated backend.
6563
// It does not commit the block.
66-
func testSendSignedTx(t *testing.T, key *ecdsa.PrivateKey, sim *Backend, isBlobTx bool) *types.Transaction {
64+
func testSendSignedTx(t *testing.T, key *ecdsa.PrivateKey, sim *Backend, isBlobTx bool, nonce uint64) *types.Transaction {
6765
t.Helper()
6866
client := sim.Client()
6967
ctx := context.Background()
@@ -73,9 +71,9 @@ func testSendSignedTx(t *testing.T, key *ecdsa.PrivateKey, sim *Backend, isBlobT
7371
signedTx *types.Transaction
7472
)
7573
if isBlobTx {
76-
signedTx, err = newBlobTx(sim, key)
74+
signedTx, err = newBlobTx(sim, key, nonce)
7775
} else {
78-
signedTx, err = newTx(sim, key)
76+
signedTx, err = newTx(sim, key, nonce)
7977
}
8078
if err != nil {
8179
t.Fatalf("failed to create transaction: %v", err)

0 commit comments

Comments
 (0)