Skip to content

Commit

Permalink
accounts/abi/bind: check SendTransaction error in tests (ethereum#30349)
Browse files Browse the repository at this point in the history
In few tests the returned error from `SendTransaction` is not being
checked. This PR checks the returned err in tests.

Returning errors also revealed tx in `TestCommitReturnValue` is not
actually being sent, and returns err ` only replay-protected (EIP-155)
transactions allowed over RPC`. Fixed the transaction by using the
`testTx` function.
  • Loading branch information
gzliudan committed Jan 16, 2025
1 parent 89c17b3 commit ec9caee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
20 changes: 16 additions & 4 deletions accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1376,29 +1376,38 @@ func TestForkResendTx(t *testing.T) {
defer sim.Close()
// 1.
parent := sim.blockchain.CurrentBlock()

// 2.
head, _ := sim.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))

_tx := types.NewTransaction(0, testAddr, big.NewInt(1000), params.TxGas, gasPrice, nil)
tx, _ := types.SignTx(_tx, types.HomesteadSigner{}, testKey)
sim.SendTransaction(context.Background(), tx)
tx, err := types.SignTx(_tx, types.HomesteadSigner{}, testKey)
if err != nil {
t.Fatalf("could not sign transaction: %v", err)
}
if err = sim.SendTransaction(context.Background(), tx); err != nil {
t.Fatalf("sending transaction: %v", err)
}
sim.Commit()

// 3.
receipt, _ := sim.TransactionReceipt(context.Background(), tx.Hash())
if h := receipt.BlockNumber.Uint64(); h != 1 {
t.Errorf("TX included in wrong block: %d", h)
}

// 4.
if err := sim.Fork(context.Background(), parent.Hash()); err != nil {
t.Errorf("forking: %v", err)
}

// 5.
sim.Commit()
if err := sim.SendTransaction(context.Background(), tx); err != nil {
t.Errorf("sending transaction: %v", err)
}
sim.Commit()

// 6.
receipt, _ = sim.TransactionReceipt(context.Background(), tx.Hash())
if h := receipt.BlockNumber.Uint64(); h != 2 {
Expand All @@ -1425,7 +1434,10 @@ func TestCommitReturnValue(t *testing.T) {
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
_tx := types.NewTransaction(0, testAddr, big.NewInt(1000), params.TxGas, gasPrice, nil)
tx, _ := types.SignTx(_tx, types.HomesteadSigner{}, testKey)
sim.SendTransaction(context.Background(), tx)
if err := sim.SendTransaction(context.Background(), tx); err != nil {
t.Errorf("sending transaction: %v", err)
}

h2 := sim.Commit()

// Create another block in the original chain
Expand Down
12 changes: 9 additions & 3 deletions accounts/abi/bind/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func TestWaitDeployed(t *testing.T) {
}()

// Send and mine the transaction.
backend.SendTransaction(ctx, tx)
if err := backend.SendTransaction(ctx, tx); err != nil {
t.Errorf("test %q: failed to send transaction: %v", name, err)
}
backend.Commit()

select {
Expand Down Expand Up @@ -125,7 +127,9 @@ func TestWaitDeployedCornerCases(t *testing.T) {
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
backend.SendTransaction(ctx, tx)
if err := backend.SendTransaction(ctx, tx); err != nil {
t.Errorf("failed to send transaction: %q", err)
}
backend.Commit()
notContractCreation := errors.New("tx is not contract creation")
if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != notContractCreation.Error() {
Expand All @@ -143,6 +147,8 @@ func TestWaitDeployedCornerCases(t *testing.T) {
}
}()

backend.SendTransaction(ctx, tx)
if err := backend.SendTransaction(ctx, tx); err != nil {
t.Errorf("failed to send transaction: %q", err)
}
cancel()
}

0 comments on commit ec9caee

Please sign in to comment.