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
18524dbd
Commit
18524dbd
authored
Mar 30, 2023
by
Joshua Gutow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
txmgr: Fake sendState clock in tests
parent
cd2975c6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
12 deletions
+25
-12
send_state.go
op-service/txmgr/send_state.go
+11
-5
send_state_test.go
op-service/txmgr/send_state_test.go
+14
-7
No files found.
op-service/txmgr/send_state.go
View file @
18524dbd
...
...
@@ -17,6 +17,7 @@ import (
type
SendState
struct
{
minedTxs
map
[
common
.
Hash
]
struct
{}
mu
sync
.
RWMutex
now
func
()
time
.
Time
// Config
nonceTooLowCount
uint64
...
...
@@ -27,9 +28,8 @@ type SendState struct {
safeAbortNonceTooLowCount
uint64
// nonce too low error
}
// NewSendState parameterizes a new SendState from the passed
// safeAbortNonceTooLowCount.
func
NewSendState
(
safeAbortNonceTooLowCount
uint64
,
unableToSendTimeout
time
.
Duration
)
*
SendState
{
// NewSendStateWithNow creates a new send state with the provided clock.
func
NewSendStateWithNow
(
safeAbortNonceTooLowCount
uint64
,
unableToSendTimeout
time
.
Duration
,
now
func
()
time
.
Time
)
*
SendState
{
if
safeAbortNonceTooLowCount
==
0
{
panic
(
"txmgr: safeAbortNonceTooLowCount cannot be zero"
)
}
...
...
@@ -37,10 +37,16 @@ func NewSendState(safeAbortNonceTooLowCount uint64, unableToSendTimeout time.Dur
return
&
SendState
{
minedTxs
:
make
(
map
[
common
.
Hash
]
struct
{}),
safeAbortNonceTooLowCount
:
safeAbortNonceTooLowCount
,
txInMempoolDeadline
:
time
.
Now
()
.
Add
(
unableToSendTimeout
),
txInMempoolDeadline
:
now
()
.
Add
(
unableToSendTimeout
),
now
:
now
,
}
}
// NewSendState creates a new send state
func
NewSendState
(
safeAbortNonceTooLowCount
uint64
,
unableToSendTimeout
time
.
Duration
)
*
SendState
{
return
NewSendStateWithNow
(
safeAbortNonceTooLowCount
,
unableToSendTimeout
,
time
.
Now
)
}
// ProcessSendError should be invoked with the error returned for each
// publication. It is safe to call this method with nil or arbitrary errors.
func
(
s
*
SendState
)
ProcessSendError
(
err
error
)
{
...
...
@@ -100,7 +106,7 @@ func (s *SendState) ShouldAbortImmediately() bool {
// If we have exceeded the nonce too low count, abort
if
s
.
nonceTooLowCount
>=
s
.
safeAbortNonceTooLowCount
||
// If we have not published a transaction in the allotted time, abort
(
s
.
successFullPublishCount
==
0
&&
time
.
N
ow
()
.
After
(
s
.
txInMempoolDeadline
))
{
(
s
.
successFullPublishCount
==
0
&&
s
.
n
ow
()
.
After
(
s
.
txInMempoolDeadline
))
{
return
true
}
...
...
op-service/txmgr/send_state_test.go
View file @
18524dbd
...
...
@@ -19,11 +19,11 @@ var (
const
testSafeAbortNonceTooLowCount
=
3
func
newSendState
()
*
txmgr
.
SendState
{
return
newSendStateWithTimeout
(
time
.
Hour
)
return
newSendStateWithTimeout
(
time
.
Hour
,
time
.
Now
)
}
func
newSendStateWithTimeout
(
t
time
.
Duration
)
*
txmgr
.
SendState
{
return
txmgr
.
NewSendState
(
testSafeAbortNonceTooLowCount
,
t
)
func
newSendStateWithTimeout
(
t
time
.
Duration
,
now
func
()
time
.
Time
)
*
txmgr
.
SendState
{
return
txmgr
.
NewSendState
WithNow
(
testSafeAbortNonceTooLowCount
,
t
,
now
)
}
func
processNSendErrors
(
sendState
*
txmgr
.
SendState
,
err
error
,
n
int
)
{
...
...
@@ -166,19 +166,26 @@ func TestSendStateIsNotWaitingForConfirmationAfterTxUnmined(t *testing.T) {
require
.
False
(
t
,
sendState
.
IsWaitingForConfirmation
())
}
func
stepClock
(
step
time
.
Duration
)
func
()
time
.
Time
{
i
:=
0
return
func
()
time
.
Time
{
var
start
time
.
Time
i
+=
1
return
start
.
Add
(
time
.
Duration
(
i
)
*
step
)
}
}
// TestSendStateTimeoutAbort ensure that this will abort if it passes the tx pool timeout
// when no successful transactions have been recorded
func
TestSendStateTimeoutAbort
(
t
*
testing
.
T
)
{
sendState
:=
newSendStateWithTimeout
(
10
*
time
.
Millisecond
)
time
.
Sleep
(
20
*
time
.
Millisecond
)
sendState
:=
newSendStateWithTimeout
(
10
*
time
.
Millisecond
,
stepClock
(
20
*
time
.
Millisecond
))
require
.
True
(
t
,
sendState
.
ShouldAbortImmediately
(),
"Should abort after timing out"
)
}
// TestSendStateNoTimeoutAbortIfPublishedTx ensure that this will not abort if there is
// a successful transaction send.
func
TestSendStateNoTimeoutAbortIfPublishedTx
(
t
*
testing
.
T
)
{
sendState
:=
newSendStateWithTimeout
(
10
*
time
.
Millisecond
)
sendState
:=
newSendStateWithTimeout
(
10
*
time
.
Millisecond
,
stepClock
(
20
*
time
.
Millisecond
)
)
sendState
.
ProcessSendError
(
nil
)
time
.
Sleep
(
20
*
time
.
Millisecond
)
require
.
False
(
t
,
sendState
.
ShouldAbortImmediately
(),
"Should not abort if published transcation successfully"
)
}
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