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
365d40ea
Commit
365d40ea
authored
May 26, 2023
by
Andreas Bigger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Copy logs for thread safety.
parent
12ee9df2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
14 deletions
+17
-14
log_store.go
op-challenger/challenger/log_store.go
+11
-8
log_store_test.go
op-challenger/challenger/log_store_test.go
+6
-6
No files found.
op-challenger/challenger/log_store.go
View file @
365d40ea
...
...
@@ -65,25 +65,29 @@ func (l *logStore) Client() ethereum.LogFilterer {
func
(
l
*
logStore
)
GetLogs
()
[]
types
.
Log
{
l
.
mu
.
Lock
()
defer
l
.
mu
.
Unlock
()
return
l
.
logList
logs
:=
make
([]
types
.
Log
,
len
(
l
.
logList
))
copy
(
logs
,
l
.
logList
)
return
logs
}
// GetLogByBlockHash returns all logs in the log store for a given block hash.
func
(
l
*
logStore
)
GetLogByBlockHash
(
blockHash
common
.
Hash
)
[]
types
.
Log
{
l
.
mu
.
Lock
()
defer
l
.
mu
.
Unlock
()
return
l
.
logMap
[
blockHash
]
logs
:=
make
([]
types
.
Log
,
len
(
l
.
logMap
[
blockHash
]))
copy
(
logs
,
l
.
logMap
[
blockHash
])
return
logs
}
// Subscribe starts the subscription.
// This function spawns a new goroutine.
func
(
l
*
logStore
)
Subscribe
()
error
{
func
(
l
*
logStore
)
Subscribe
(
ctx
context
.
Context
)
error
{
err
:=
l
.
subscription
.
Subscribe
()
if
err
!=
nil
{
l
.
log
.
Error
(
"failed to subscribe"
,
"err"
,
err
)
return
err
}
go
l
.
dispatchLogs
()
go
l
.
dispatchLogs
(
ctx
)
return
nil
}
...
...
@@ -103,9 +107,8 @@ func (l *logStore) buildBackoffStrategy() backoff.Strategy {
// resubscribe attempts to re-establish the log store internal
// subscription with a backoff strategy.
func
(
l
*
logStore
)
resubscribe
()
error
{
func
(
l
*
logStore
)
resubscribe
(
ctx
context
.
Context
)
error
{
l
.
log
.
Info
(
"resubscribing"
)
ctx
:=
context
.
Background
()
backoffStrategy
:=
l
.
buildBackoffStrategy
()
return
backoff
.
DoCtx
(
ctx
,
10
,
backoffStrategy
,
func
()
error
{
if
l
.
subscription
==
nil
{
...
...
@@ -130,13 +133,13 @@ func (l *logStore) insertLog(log types.Log) {
// dispatchLogs dispatches logs to the log store.
// This function is intended to be run as a goroutine.
func
(
l
*
logStore
)
dispatchLogs
()
{
func
(
l
*
logStore
)
dispatchLogs
(
ctx
context
.
Context
)
{
for
{
select
{
case
err
:=
<-
l
.
subscription
.
sub
.
Err
()
:
l
.
log
.
Error
(
"log subscription error"
,
"err"
,
err
)
for
{
err
=
l
.
resubscribe
()
err
=
l
.
resubscribe
(
ctx
)
if
err
==
nil
{
break
}
...
...
op-challenger/challenger/log_store_test.go
View file @
365d40ea
...
...
@@ -95,7 +95,7 @@ func TestLogStore_Subscribe_EstablishesSubscription(t *testing.T) {
defer
logStore
.
Quit
()
require
.
Equal
(
t
,
0
,
client
.
subcount
)
require
.
False
(
t
,
logStore
.
Subscribed
())
require
.
NoError
(
t
,
logStore
.
Subscribe
())
require
.
NoError
(
t
,
logStore
.
Subscribe
(
context
.
Background
()
))
require
.
True
(
t
,
logStore
.
Subscribed
())
require
.
Equal
(
t
,
1
,
client
.
subcount
)
}
...
...
@@ -103,7 +103,7 @@ func TestLogStore_Subscribe_EstablishesSubscription(t *testing.T) {
func
TestLogStore_Subscribe_ReceivesLogs
(
t
*
testing
.
T
)
{
logStore
,
client
:=
newLogStore
(
t
)
defer
logStore
.
Quit
()
require
.
NoError
(
t
,
logStore
.
Subscribe
())
require
.
NoError
(
t
,
logStore
.
Subscribe
(
context
.
Background
()
))
mockLog
:=
types
.
Log
{
BlockHash
:
common
.
HexToHash
(
"0x1"
),
...
...
@@ -122,7 +122,7 @@ func TestLogStore_Subscribe_ReceivesLogs(t *testing.T) {
func
TestLogStore_Subscribe_SubscriptionErrors
(
t
*
testing
.
T
)
{
logStore
,
client
:=
newLogStore
(
t
)
defer
logStore
.
Quit
()
require
.
NoError
(
t
,
logStore
.
Subscribe
())
require
.
NoError
(
t
,
logStore
.
Subscribe
(
context
.
Background
()
))
client
.
sub
.
errorChan
<-
ErrTestError
...
...
@@ -143,19 +143,19 @@ func TestLogStore_Subscribe_NoClient_Panics(t *testing.T) {
}
}()
logStore
,
_
:=
newErrorLogStore
(
t
,
nil
)
require
.
NoError
(
t
,
logStore
.
Subscribe
())
require
.
NoError
(
t
,
logStore
.
Subscribe
(
context
.
Background
()
))
}
func
TestLogStore_Subscribe_ErrorSubscribing
(
t
*
testing
.
T
)
{
logStore
,
_
:=
newErrorLogStore
(
t
,
&
errLogStoreClient
{})
require
.
False
(
t
,
logStore
.
Subscribed
())
require
.
EqualError
(
t
,
logStore
.
Subscribe
(),
ErrTestError
.
Error
())
require
.
EqualError
(
t
,
logStore
.
Subscribe
(
context
.
Background
()
),
ErrTestError
.
Error
())
}
func
TestLogStore_Quit_ResetsSubscription
(
t
*
testing
.
T
)
{
logStore
,
_
:=
newLogStore
(
t
)
require
.
False
(
t
,
logStore
.
Subscribed
())
require
.
NoError
(
t
,
logStore
.
Subscribe
())
require
.
NoError
(
t
,
logStore
.
Subscribe
(
context
.
Background
()
))
require
.
True
(
t
,
logStore
.
Subscribed
())
logStore
.
Quit
()
require
.
False
(
t
,
logStore
.
Subscribed
())
...
...
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