Skip to content

Commit 87af6f0

Browse files
authored
txmgr: improve code sharing between Send and SendAsync (ethereum-optimism#11876)
1 parent 849680b commit 87af6f0

File tree

2 files changed

+17
-56
lines changed

2 files changed

+17
-56
lines changed

op-service/txmgr/txmgr.go

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,14 @@ type TxCandidate struct {
232232
//
233233
// NOTE: Send can be called concurrently, the nonce will be managed internally.
234234
func (m *SimpleTxManager) Send(ctx context.Context, candidate TxCandidate) (*types.Receipt, error) {
235+
_, r, err := m.send(ctx, candidate)
236+
return r, err
237+
}
238+
239+
func (m *SimpleTxManager) send(ctx context.Context, candidate TxCandidate) (*types.Transaction, *types.Receipt, error) {
235240
// refuse new requests if the tx manager is closed
236241
if m.closed.Load() {
237-
return nil, ErrClosed
242+
return nil, nil, ErrClosed
238243
}
239244

240245
m.metr.RecordPendingTx(m.pending.Add(1))
@@ -251,63 +256,27 @@ func (m *SimpleTxManager) Send(ctx context.Context, candidate TxCandidate) (*typ
251256
tx, err := m.prepare(ctx, candidate)
252257
if err != nil {
253258
m.resetNonce()
254-
return nil, err
259+
return tx, nil, err
255260
}
256261
receipt, err := m.sendTx(ctx, tx)
257262
if err != nil {
258263
m.resetNonce()
259-
return nil, err
264+
return nil, nil, err
260265
}
261-
return receipt, err
266+
return tx, receipt, err
262267
}
263268

264269
func (m *SimpleTxManager) SendAsync(ctx context.Context, candidate TxCandidate, ch chan SendResponse) {
265-
if cap(ch) == 0 {
266-
panic("SendAsync: channel must be buffered")
267-
}
268-
269-
// refuse new requests if the tx manager is closed
270-
if m.closed.Load() {
271-
ch <- SendResponse{
272-
Receipt: nil,
273-
Err: ErrClosed,
274-
}
275-
return
276-
}
277-
278-
m.metr.RecordPendingTx(m.pending.Add(1))
279-
280-
var cancel context.CancelFunc
281-
if m.cfg.TxSendTimeout == 0 {
282-
ctx, cancel = context.WithCancel(ctx)
283-
} else {
284-
ctx, cancel = context.WithTimeout(ctx, m.cfg.TxSendTimeout)
285-
}
286-
287-
tx, err := m.prepare(ctx, candidate)
288-
if err != nil {
289-
m.resetNonce()
290-
cancel()
291-
m.metr.RecordPendingTx(m.pending.Add(-1))
292-
ch <- SendResponse{
293-
Receipt: nil,
294-
Err: err,
295-
}
296-
return
297-
}
298-
299270
go func() {
300-
defer m.metr.RecordPendingTx(m.pending.Add(-1))
301-
defer cancel()
302-
receipt, err := m.sendTx(ctx, tx)
303-
if err != nil {
304-
m.resetNonce()
305-
}
306-
ch <- SendResponse{
271+
tx, receipt, err := m.send(ctx, candidate)
272+
r := SendResponse{
307273
Receipt: receipt,
308-
Nonce: tx.Nonce(),
309274
Err: err,
310275
}
276+
if tx != nil {
277+
r.Nonce = tx.Nonce()
278+
}
279+
ch <- r
311280
}()
312281
}
313282

op-service/txmgr/txmgr_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ func testSendVariants(t *testing.T, testFn func(t *testing.T, send testSendVaria
351351

352352
t.Run("SendAsync", func(t *testing.T) {
353353
testFn(t, func(ctx context.Context, h *testHarness, tx TxCandidate) (*types.Receipt, error) {
354-
ch := make(chan SendResponse, 1)
354+
// unbuffered is ok, will be written to from a goroutine spawned inside SendAsync
355+
ch := make(chan SendResponse)
355356
h.mgr.SendAsync(ctx, tx, ch)
356357
res := <-ch
357358
return res.Receipt, res.Err
@@ -1588,12 +1589,3 @@ func TestMakeSidecar(t *testing.T) {
15881589
require.Equal(t, hashes[i], eth.KZGToVersionedHash(commit))
15891590
}
15901591
}
1591-
1592-
func TestSendAsyncUnbufferedChan(t *testing.T) {
1593-
conf := configWithNumConfs(2)
1594-
h := newTestHarnessWithConfig(t, conf)
1595-
1596-
require.Panics(t, func() {
1597-
h.mgr.SendAsync(context.Background(), TxCandidate{}, make(chan SendResponse))
1598-
})
1599-
}

0 commit comments

Comments
 (0)