Skip to content

Commit 14e0f86

Browse files
committed
NonceAt -> PendingNonceAt
1 parent 87703b3 commit 14e0f86

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

sender/eth_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (c *ethClient) clientID(addr common.Address) int {
112112
}
113113

114114
func (c *ethClient) Nonce(ctx context.Context, addr common.Address) (uint64, error) {
115-
return c.clients[c.clientID(addr)].NonceAt(ctx, addr, nil)
115+
return c.clients[c.clientID(addr)].PendingNonceAt(ctx, addr)
116116
}
117117

118118
func (c *ethClient) Send(ctx context.Context, tx *types.LoadTx) (_err error) {

sender/eth_client_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ func TestEthClientSendTx_DryRunWithoutEndpoints(t *testing.T) {
101101
require.NoError(t, client.Send(t.Context(), tx))
102102
}
103103

104+
func TestEthClientNonceUsesPending(t *testing.T) {
105+
api := newMockEthAPI()
106+
srv := rpc.NewServer()
107+
require.NoError(t, srv.RegisterName("eth", api))
108+
109+
ts := httptest.NewServer(srv)
110+
defer ts.Close()
111+
112+
client, err := newEthClient(t.Context(), &ethClientConfig{
113+
ChainID: "test-chain",
114+
Endpoints: []string{ts.URL},
115+
ConnsPerEndpoint: 1,
116+
Collector: stats.NewCollector(),
117+
})
118+
require.NoError(t, err)
119+
defer client.Close()
120+
121+
nonce, err := client.Nonce(t.Context(), common.HexToAddress("0x0000000000000000000000000000000000000123"))
122+
require.NoError(t, err)
123+
require.EqualValues(t, 7, nonce)
124+
require.Equal(t, []rpc.BlockNumber{rpc.PendingBlockNumber}, api.TransactionCountRequests())
125+
}
126+
104127
func TestNewEthClientRejectsZeroConnsPerEndpoint(t *testing.T) {
105128
client, err := newEthClient(t.Context(), &ethClientConfig{
106129
ChainID: "test-chain",
@@ -113,12 +136,17 @@ func TestNewEthClientRejectsZeroConnsPerEndpoint(t *testing.T) {
113136
}
114137

115138
type mockEthAPI struct {
116-
rawTxs utils.Mutex[*[][]byte]
139+
rawTxs utils.Mutex[*[][]byte]
140+
transactionCountReqs utils.Mutex[*[]rpc.BlockNumber]
117141
}
118142

119143
func newMockEthAPI() *mockEthAPI {
120144
rawTxs := [][]byte{}
121-
return &mockEthAPI{rawTxs: utils.NewMutex(&rawTxs)}
145+
transactionCountReqs := []rpc.BlockNumber{}
146+
return &mockEthAPI{
147+
rawTxs: utils.NewMutex(&rawTxs),
148+
transactionCountReqs: utils.NewMutex(&transactionCountReqs),
149+
}
122150
}
123151

124152
func (m *mockEthAPI) SendRawTransaction(_ context.Context, rawTx hexutil.Bytes) (common.Hash, error) {
@@ -136,6 +164,20 @@ func (m *mockEthAPI) RawTransactions() [][]byte {
136164
panic("unreachable")
137165
}
138166

167+
func (m *mockEthAPI) GetTransactionCount(_ context.Context, _ common.Address, block rpc.BlockNumber) (hexutil.Uint64, error) {
168+
for reqs := range m.transactionCountReqs.Lock() {
169+
*reqs = append(*reqs, block)
170+
}
171+
return hexutil.Uint64(7), nil
172+
}
173+
174+
func (m *mockEthAPI) TransactionCountRequests() []rpc.BlockNumber {
175+
for reqs := range m.transactionCountReqs.Lock() {
176+
return slices.Clone(*reqs)
177+
}
178+
panic("unreachable")
179+
}
180+
139181
func testLoadTx(t *testing.T) *types.LoadTx {
140182
t.Helper()
141183

0 commit comments

Comments
 (0)