Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
87af6f0d
Unverified
Commit
87af6f0d
authored
Sep 12, 2024
by
Sebastian Stammler
Committed by
GitHub
Sep 12, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
txmgr: improve code sharing between Send and SendAsync (#11876)
parent
849680b8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
56 deletions
+17
-56
txmgr.go
op-service/txmgr/txmgr.go
+15
-46
txmgr_test.go
op-service/txmgr/txmgr_test.go
+2
-10
No files found.
op-service/txmgr/txmgr.go
View file @
87af6f0d
...
@@ -232,9 +232,14 @@ type TxCandidate struct {
...
@@ -232,9 +232,14 @@ type TxCandidate struct {
//
//
// NOTE: Send can be called concurrently, the nonce will be managed internally.
// NOTE: Send can be called concurrently, the nonce will be managed internally.
func
(
m
*
SimpleTxManager
)
Send
(
ctx
context
.
Context
,
candidate
TxCandidate
)
(
*
types
.
Receipt
,
error
)
{
func
(
m
*
SimpleTxManager
)
Send
(
ctx
context
.
Context
,
candidate
TxCandidate
)
(
*
types
.
Receipt
,
error
)
{
_
,
r
,
err
:=
m
.
send
(
ctx
,
candidate
)
return
r
,
err
}
func
(
m
*
SimpleTxManager
)
send
(
ctx
context
.
Context
,
candidate
TxCandidate
)
(
*
types
.
Transaction
,
*
types
.
Receipt
,
error
)
{
// refuse new requests if the tx manager is closed
// refuse new requests if the tx manager is closed
if
m
.
closed
.
Load
()
{
if
m
.
closed
.
Load
()
{
return
nil
,
ErrClosed
return
nil
,
nil
,
ErrClosed
}
}
m
.
metr
.
RecordPendingTx
(
m
.
pending
.
Add
(
1
))
m
.
metr
.
RecordPendingTx
(
m
.
pending
.
Add
(
1
))
...
@@ -251,63 +256,27 @@ func (m *SimpleTxManager) Send(ctx context.Context, candidate TxCandidate) (*typ
...
@@ -251,63 +256,27 @@ func (m *SimpleTxManager) Send(ctx context.Context, candidate TxCandidate) (*typ
tx
,
err
:=
m
.
prepare
(
ctx
,
candidate
)
tx
,
err
:=
m
.
prepare
(
ctx
,
candidate
)
if
err
!=
nil
{
if
err
!=
nil
{
m
.
resetNonce
()
m
.
resetNonce
()
return
nil
,
err
return
tx
,
nil
,
err
}
}
receipt
,
err
:=
m
.
sendTx
(
ctx
,
tx
)
receipt
,
err
:=
m
.
sendTx
(
ctx
,
tx
)
if
err
!=
nil
{
if
err
!=
nil
{
m
.
resetNonce
()
m
.
resetNonce
()
return
nil
,
err
return
nil
,
nil
,
err
}
}
return
receipt
,
err
return
tx
,
receipt
,
err
}
}
func
(
m
*
SimpleTxManager
)
SendAsync
(
ctx
context
.
Context
,
candidate
TxCandidate
,
ch
chan
SendResponse
)
{
func
(
m
*
SimpleTxManager
)
SendAsync
(
ctx
context
.
Context
,
candidate
TxCandidate
,
ch
chan
SendResponse
)
{
if
cap
(
ch
)
==
0
{
panic
(
"SendAsync: channel must be buffered"
)
}
// refuse new requests if the tx manager is closed
if
m
.
closed
.
Load
()
{
ch
<-
SendResponse
{
Receipt
:
nil
,
Err
:
ErrClosed
,
}
return
}
m
.
metr
.
RecordPendingTx
(
m
.
pending
.
Add
(
1
))
var
cancel
context
.
CancelFunc
if
m
.
cfg
.
TxSendTimeout
==
0
{
ctx
,
cancel
=
context
.
WithCancel
(
ctx
)
}
else
{
ctx
,
cancel
=
context
.
WithTimeout
(
ctx
,
m
.
cfg
.
TxSendTimeout
)
}
tx
,
err
:=
m
.
prepare
(
ctx
,
candidate
)
if
err
!=
nil
{
m
.
resetNonce
()
cancel
()
m
.
metr
.
RecordPendingTx
(
m
.
pending
.
Add
(
-
1
))
ch
<-
SendResponse
{
Receipt
:
nil
,
Err
:
err
,
}
return
}
go
func
()
{
go
func
()
{
defer
m
.
metr
.
RecordPendingTx
(
m
.
pending
.
Add
(
-
1
))
tx
,
receipt
,
err
:=
m
.
send
(
ctx
,
candidate
)
defer
cancel
()
r
:=
SendResponse
{
receipt
,
err
:=
m
.
sendTx
(
ctx
,
tx
)
if
err
!=
nil
{
m
.
resetNonce
()
}
ch
<-
SendResponse
{
Receipt
:
receipt
,
Receipt
:
receipt
,
Nonce
:
tx
.
Nonce
(),
Err
:
err
,
Err
:
err
,
}
}
if
tx
!=
nil
{
r
.
Nonce
=
tx
.
Nonce
()
}
ch
<-
r
}()
}()
}
}
...
...
op-service/txmgr/txmgr_test.go
View file @
87af6f0d
...
@@ -351,7 +351,8 @@ func testSendVariants(t *testing.T, testFn func(t *testing.T, send testSendVaria
...
@@ -351,7 +351,8 @@ func testSendVariants(t *testing.T, testFn func(t *testing.T, send testSendVaria
t
.
Run
(
"SendAsync"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"SendAsync"
,
func
(
t
*
testing
.
T
)
{
testFn
(
t
,
func
(
ctx
context
.
Context
,
h
*
testHarness
,
tx
TxCandidate
)
(
*
types
.
Receipt
,
error
)
{
testFn
(
t
,
func
(
ctx
context
.
Context
,
h
*
testHarness
,
tx
TxCandidate
)
(
*
types
.
Receipt
,
error
)
{
ch
:=
make
(
chan
SendResponse
,
1
)
// unbuffered is ok, will be written to from a goroutine spawned inside SendAsync
ch
:=
make
(
chan
SendResponse
)
h
.
mgr
.
SendAsync
(
ctx
,
tx
,
ch
)
h
.
mgr
.
SendAsync
(
ctx
,
tx
,
ch
)
res
:=
<-
ch
res
:=
<-
ch
return
res
.
Receipt
,
res
.
Err
return
res
.
Receipt
,
res
.
Err
...
@@ -1588,12 +1589,3 @@ func TestMakeSidecar(t *testing.T) {
...
@@ -1588,12 +1589,3 @@ func TestMakeSidecar(t *testing.T) {
require
.
Equal
(
t
,
hashes
[
i
],
eth
.
KZGToVersionedHash
(
commit
))
require
.
Equal
(
t
,
hashes
[
i
],
eth
.
KZGToVersionedHash
(
commit
))
}
}
}
}
func
TestSendAsyncUnbufferedChan
(
t
*
testing
.
T
)
{
conf
:=
configWithNumConfs
(
2
)
h
:=
newTestHarnessWithConfig
(
t
,
conf
)
require
.
Panics
(
t
,
func
()
{
h
.
mgr
.
SendAsync
(
context
.
Background
(),
TxCandidate
{},
make
(
chan
SendResponse
))
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment