Skip to content

Commit fe4890f

Browse files
authored
Revert "txmgr: improve code sharing between Send and SendAsync (ethereum-optimism#11876)" (ethereum-optimism#11883)
This reverts commit 87af6f0.
1 parent 87af6f0 commit fe4890f

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

op-service/txmgr/txmgr.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,9 @@ 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) {
240235
// refuse new requests if the tx manager is closed
241236
if m.closed.Load() {
242-
return nil, nil, ErrClosed
237+
return nil, ErrClosed
243238
}
244239

245240
m.metr.RecordPendingTx(m.pending.Add(1))
@@ -256,27 +251,63 @@ func (m *SimpleTxManager) send(ctx context.Context, candidate TxCandidate) (*typ
256251
tx, err := m.prepare(ctx, candidate)
257252
if err != nil {
258253
m.resetNonce()
259-
return tx, nil, err
254+
return nil, err
260255
}
261256
receipt, err := m.sendTx(ctx, tx)
262257
if err != nil {
263258
m.resetNonce()
264-
return nil, nil, err
259+
return nil, err
265260
}
266-
return tx, receipt, err
261+
return receipt, err
267262
}
268263

269264
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+
270299
go func() {
271-
tx, receipt, err := m.send(ctx, candidate)
272-
r := SendResponse{
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{
273307
Receipt: receipt,
308+
Nonce: tx.Nonce(),
274309
Err: err,
275310
}
276-
if tx != nil {
277-
r.Nonce = tx.Nonce()
278-
}
279-
ch <- r
280311
}()
281312
}
282313

op-service/txmgr/txmgr_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ 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-
// unbuffered is ok, will be written to from a goroutine spawned inside SendAsync
355-
ch := make(chan SendResponse)
354+
ch := make(chan SendResponse, 1)
356355
h.mgr.SendAsync(ctx, tx, ch)
357356
res := <-ch
358357
return res.Receipt, res.Err
@@ -1589,3 +1588,12 @@ func TestMakeSidecar(t *testing.T) {
15891588
require.Equal(t, hashes[i], eth.KZGToVersionedHash(commit))
15901589
}
15911590
}
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)