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
12ee9df2
Commit
12ee9df2
authored
May 26, 2023
by
Andreas Bigger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix tests to use public log store interface.
parent
9e0702db
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
161 additions
and
224 deletions
+161
-224
log_store.go
op-challenger/challenger/log_store.go
+35
-30
log_store_test.go
op-challenger/challenger/log_store_test.go
+94
-147
subscription.go
op-challenger/challenger/subscription.go
+13
-17
subscription_test.go
op-challenger/challenger/subscription_test.go
+19
-30
No files found.
op-challenger/challenger/log_store.go
View file @
12ee9df2
...
...
@@ -23,7 +23,7 @@ type logStore struct {
logList
[]
types
.
Log
logMap
map
[
common
.
Hash
][]
types
.
Log
// Log subscription
s
// Log subscription
subscription
*
Subscription
// Client to query for logs
...
...
@@ -46,32 +46,50 @@ func NewLogStore(query ethereum.FilterQuery, client ethereum.LogFilterer, log lo
}
}
// Subscribed returns true if the subscription has started.
func
(
l
*
logStore
)
Subscribed
()
bool
{
return
l
.
subscription
.
Started
()
}
// Query returns the log filter query.
func
(
l
*
logStore
)
Query
()
ethereum
.
FilterQuery
{
return
l
.
query
}
// Client returns the log filter client.
func
(
l
*
logStore
)
Client
()
ethereum
.
LogFilterer
{
return
l
.
client
}
// GetLogs returns all logs in the log store.
func
(
l
*
logStore
)
GetLogs
()
[]
types
.
Log
{
l
.
mu
.
Lock
()
defer
l
.
mu
.
Unlock
()
return
l
.
logList
}
// 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
]
}
// Subscribe starts the subscription.
// This function spawns a new goroutine.
func
(
l
*
logStore
)
Subscribe
()
error
{
if
l
.
subscription
==
nil
{
l
.
log
.
Error
(
"subscription zeroed out"
)
return
nil
}
err
:=
l
.
subscription
.
Subscribe
()
if
err
!=
nil
{
l
.
log
.
Error
(
"failed to subscribe"
,
"err"
,
err
)
return
err
}
return
nil
}
// Start starts the log store.
// This function spawns a new goroutine.
func
(
l
*
logStore
)
Start
()
{
go
l
.
dispatchLogs
()
return
nil
}
// Quit stops all log store asynchronous tasks.
func
(
l
*
logStore
)
Quit
()
{
if
l
.
subscription
!=
nil
{
l
.
subscription
.
Quit
()
}
l
.
subscription
.
Quit
()
}
// buildBackoffStrategy builds a [backoff.Strategy].
...
...
@@ -83,9 +101,10 @@ func (l *logStore) buildBackoffStrategy() backoff.Strategy {
}
}
// resubscribe resubscribes to the log store subscription with a backoff.
// resubscribe attempts to re-establish the log store internal
// subscription with a backoff strategy.
func
(
l
*
logStore
)
resubscribe
()
error
{
l
.
log
.
Info
(
"resubscribing
to subscription"
,
"id"
,
l
.
subscription
.
ID
()
)
l
.
log
.
Info
(
"resubscribing
"
)
ctx
:=
context
.
Background
()
backoffStrategy
:=
l
.
buildBackoffStrategy
()
return
backoff
.
DoCtx
(
ctx
,
10
,
backoffStrategy
,
func
()
error
{
...
...
@@ -109,20 +128,6 @@ func (l *logStore) insertLog(log types.Log) {
l
.
mu
.
Unlock
()
}
// GetLogs returns all logs in the log store.
func
(
l
*
logStore
)
GetLogs
()
[]
types
.
Log
{
l
.
mu
.
Lock
()
defer
l
.
mu
.
Unlock
()
return
l
.
logList
}
// 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
]
}
// dispatchLogs dispatches logs to the log store.
// This function is intended to be run as a goroutine.
func
(
l
*
logStore
)
dispatchLogs
()
{
...
...
op-challenger/challenger/log_store_test.go
View file @
12ee9df2
This diff is collapsed.
Click to expand it.
op-challenger/challenger/subscription.go
View file @
12ee9df2
...
...
@@ -2,17 +2,12 @@ package challenger
import
(
"context"
"errors"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)
var
(
ErrMissingClient
=
errors
.
New
(
"missing client"
)
)
// SubscriptionId is a unique subscription ID.
type
SubscriptionId
uint64
...
...
@@ -28,6 +23,8 @@ type Subscription struct {
id
SubscriptionId
// The current subscription
sub
ethereum
.
Subscription
// If the subscription is started
started
bool
// The query used to create the subscription
query
ethereum
.
FilterQuery
// The log channel
...
...
@@ -43,13 +40,14 @@ type Subscription struct {
// NewSubscription creates a new subscription.
func
NewSubscription
(
query
ethereum
.
FilterQuery
,
client
ethereum
.
LogFilterer
,
log
log
.
Logger
)
*
Subscription
{
return
&
Subscription
{
id
:
SubscriptionId
(
0
),
sub
:
nil
,
query
:
query
,
logs
:
make
(
chan
types
.
Log
),
quit
:
make
(
chan
struct
{}),
client
:
client
,
log
:
log
,
id
:
SubscriptionId
(
0
),
sub
:
nil
,
started
:
false
,
query
:
query
,
logs
:
make
(
chan
types
.
Log
),
quit
:
make
(
chan
struct
{}),
client
:
client
,
log
:
log
,
}
}
...
...
@@ -60,22 +58,19 @@ func (s *Subscription) ID() SubscriptionId {
// Started returns true if the subscription has started.
func
(
s
*
Subscription
)
Started
()
bool
{
return
s
.
s
ub
!=
nil
return
s
.
s
tarted
}
// Subscribe constructs the subscription.
func
(
s
*
Subscription
)
Subscribe
()
error
{
s
.
log
.
Info
(
"Subscribing to"
,
"query"
,
s
.
query
.
Topics
,
"id"
,
s
.
id
)
if
s
.
client
==
nil
{
s
.
log
.
Error
(
"missing client"
)
return
ErrMissingClient
}
sub
,
err
:=
s
.
client
.
SubscribeFilterLogs
(
context
.
Background
(),
s
.
query
,
s
.
logs
)
if
err
!=
nil
{
s
.
log
.
Error
(
"failed to subscribe to logs"
,
"err"
,
err
)
return
err
}
s
.
sub
=
sub
s
.
started
=
true
return
nil
}
...
...
@@ -84,5 +79,6 @@ func (s *Subscription) Quit() {
s
.
log
.
Info
(
"Quitting subscription"
,
"id"
,
s
.
id
)
s
.
sub
.
Unsubscribe
()
s
.
quit
<-
struct
{}{}
s
.
started
=
false
s
.
log
.
Info
(
"Quit subscription"
,
"id"
,
s
.
id
)
}
op-challenger/challenger/subscription_test.go
View file @
12ee9df2
...
...
@@ -15,7 +15,6 @@ import (
"github.com/stretchr/testify/require"
)
// mockLogFilterClient implements the [ethereum.LogFilter] interface for testing.
type
mockLogFilterClient
struct
{}
func
(
m
mockLogFilterClient
)
FilterLogs
(
context
.
Context
,
ethereum
.
FilterQuery
)
([]
types
.
Log
,
error
)
{
...
...
@@ -26,7 +25,12 @@ func (m mockLogFilterClient) SubscribeFilterLogs(context.Context, ethereum.Filte
return
nil
,
nil
}
// FuzzSubscriptionId_Increment tests the Increment method on a [SubscriptionId].
func
newSubscription
(
t
*
testing
.
T
,
client
*
mockLogFilterClient
)
(
*
Subscription
,
*
mockLogFilterClient
)
{
query
:=
ethereum
.
FilterQuery
{}
log
:=
testlog
.
Logger
(
t
,
log
.
LvlError
)
return
NewSubscription
(
query
,
client
,
log
),
client
}
func
FuzzSubscriptionId_Increment
(
f
*
testing
.
F
)
{
maxUint64
:=
uint64
(
math
.
MaxUint64
)
f
.
Fuzz
(
func
(
t
*
testing
.
T
,
id
uint64
)
{
...
...
@@ -39,31 +43,20 @@ func FuzzSubscriptionId_Increment(f *testing.F) {
})
}
// TestSubscription_Subscribe_MissingClient tests the Subscribe
// method on a [Subscription] fails when the client is missing.
func
TestSubscription_Subscribe_MissingClient
(
t
*
testing
.
T
)
{
query
:=
ethereum
.
FilterQuery
{}
log
:=
testlog
.
Logger
(
t
,
log
.
LvlError
)
subscription
:=
Subscription
{
query
:
query
,
log
:
log
,
}
err
:=
subscription
.
Subscribe
()
require
.
EqualError
(
t
,
err
,
ErrMissingClient
.
Error
())
func
TestSubscription_Subscribe_NilClient_Panics
(
t
*
testing
.
T
)
{
defer
func
()
{
if
recover
()
==
nil
{
t
.
Error
(
"expected nil client to panic"
)
}
}()
subscription
,
_
:=
newSubscription
(
t
,
nil
)
require
.
NoError
(
t
,
subscription
.
Subscribe
())
}
// TestSubscription_Subscribe tests the Subscribe method on a [Subscription].
func
TestSubscription_Subscribe
(
t
*
testing
.
T
)
{
query
:=
ethereum
.
FilterQuery
{}
log
:=
testlog
.
Logger
(
t
,
log
.
LvlError
)
subscription
:=
Subscription
{
query
:
query
,
client
:
mockLogFilterClient
{},
log
:
log
,
}
require
.
Nil
(
t
,
subscription
.
logs
)
err
:=
subscription
.
Subscribe
()
require
.
NoError
(
t
,
err
)
subscription
,
_
:=
newSubscription
(
t
,
&
mockLogFilterClient
{})
require
.
NoError
(
t
,
subscription
.
Subscribe
())
require
.
True
(
t
,
subscription
.
Started
())
}
var
ErrSubscriptionFailed
=
errors
.
New
(
"failed to subscribe to logs"
)
...
...
@@ -78,10 +71,7 @@ func (m errLogFilterClient) SubscribeFilterLogs(context.Context, ethereum.Filter
return
nil
,
ErrSubscriptionFailed
}
// TestSubscription_Subscribe_Errors tests the Subscribe
// method on a [Subscription] errors if the LogFilter client
// returns an error.
func
TestSubscription_Subscribe_Errors
(
t
*
testing
.
T
)
{
func
TestSubscription_Subscribe_SubscriptionErrors
(
t
*
testing
.
T
)
{
query
:=
ethereum
.
FilterQuery
{}
log
:=
testlog
.
Logger
(
t
,
log
.
LvlError
)
subscription
:=
Subscription
{
...
...
@@ -89,6 +79,5 @@ func TestSubscription_Subscribe_Errors(t *testing.T) {
client
:
errLogFilterClient
{},
log
:
log
,
}
err
:=
subscription
.
Subscribe
()
require
.
EqualError
(
t
,
err
,
ErrSubscriptionFailed
.
Error
())
require
.
EqualError
(
t
,
subscription
.
Subscribe
(),
ErrSubscriptionFailed
.
Error
())
}
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