Skip to content

Commit

Permalink
ethmonitor: fetchBlockByNumber not protected by mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav committed Aug 10, 2023
1 parent e178e46 commit 1889f41
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 43 deletions.
63 changes: 28 additions & 35 deletions ethmonitor/ethmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ func NewMonitor(provider ethrpc.Interface, options ...Options) (*Monitor, error)
err error
)
if opts.CacheBackend != nil {
blockCache, err = cachestorectl.Open[*types.Block](opts.CacheBackend)
// TODO: think about lock expiry time
blockCache, err = cachestorectl.Open[*types.Block](opts.CacheBackend, cachestore.WithLockExpiry(2*time.Second))
if err != nil {
return nil, fmt.Errorf("ethmonitor: open block cache: %w", err)
}
Expand Down Expand Up @@ -507,48 +508,40 @@ func (m *Monitor) fetchNextBlock(ctx context.Context) (*types.Block, error) {
}

func (m *Monitor) fetchBlockByNumber(ctx context.Context, num *big.Int) (*types.Block, error) {
getter := func(ctx context.Context, _ string) (*types.Block, error) {
m.log.Debugf("ethmonitor: fetchBlockByNumber is calling origin for number %s", num)
maxErrAttempts, errAttempts := 3, 0 // quick retry in case of short-term node connection failures
m.log.Debugf("ethmonitor: fetchBlockByNumber is calling origin for number %s", num)
maxErrAttempts, errAttempts := 3, 0 // quick retry in case of short-term node connection failures

var block *types.Block
var err error
var block *types.Block
var err error

for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}

if errAttempts >= maxErrAttempts {
m.log.Warnf("ethmonitor: fetchBlockByNumber hit maxErrAttempts after %d tries for block num %v due to %v", errAttempts, num, err)
return nil, superr.New(ErrMaxAttempts, err)
}
if errAttempts >= maxErrAttempts {
m.log.Warnf("ethmonitor: fetchBlockByNumber hit maxErrAttempts after %d tries for block num %v due to %v", errAttempts, num, err)
return nil, superr.New(ErrMaxAttempts, err)
}

tctx, cancel := context.WithTimeout(ctx, m.options.Timeout)
defer cancel()
tctx, cancel := context.WithTimeout(ctx, m.options.Timeout)
defer cancel()

block, err = m.provider.BlockByNumber(tctx, num)
if err != nil {
if errors.Is(err, ethereum.NotFound) {
return nil, ethereum.NotFound
} else {
m.log.Warnf("ethmonitor: fetchBlockByNumber failed due to: %v", err)
errAttempts++
time.Sleep(time.Duration(errAttempts) * time.Second)
continue
}
block, err = m.provider.BlockByNumber(tctx, num)
if err != nil {
if errors.Is(err, ethereum.NotFound) {
return nil, ethereum.NotFound
} else {
m.log.Warnf("ethmonitor: fetchBlockByNumber failed due to: %v", err)
errAttempts++
time.Sleep(time.Duration(errAttempts) * time.Second)
continue
}
return block, nil
}
return block, nil
}

if m.blockCache != nil {
key := "BlockByNumber:" + num.String()
return m.blockCache.GetOrSetWithLockEx(ctx, key, getter, m.options.CacheExpiry)
}
return getter(ctx, "")
}

func (m *Monitor) fetchBlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/google/uuid v1.2.0
github.com/gorilla/websocket v1.5.0
github.com/goware/breaker v0.1.2
github.com/goware/cachestore v0.7.1-0.20230809150046-334a913b9e91
github.com/goware/cachestore v0.7.1-0.20230810101011-d24347595586
github.com/goware/calc v0.2.0
github.com/goware/channel v0.2.4
github.com/goware/logger v0.1.0
Expand Down Expand Up @@ -47,7 +47,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/redis/go-redis/v9 v9.0.3 // indirect
github.com/redis/go-redis/v9 v9.0.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/term v0.5.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/goware/breaker v0.1.2 h1:er7Jo7OAUdKyN0iXBQGI2x18MiXjTKNaE4P+ceimNzE=
github.com/goware/breaker v0.1.2/go.mod h1:ijCEfXAa0j6w7IoHA4v6Sox2W6U9HUbI/t+5x0zGaug=
github.com/goware/cachestore v0.7.1-0.20230809121138-8f8d4a99716f h1:efXZHCg00EvAgiJsWTdbHfnHhX1jn63ULP1/YZg7vKs=
github.com/goware/cachestore v0.7.1-0.20230809121138-8f8d4a99716f/go.mod h1:n9v3qNfCRlXAAF4TCN/s1VUxEOVGh+w1ua0m+QD0ngo=
github.com/goware/cachestore v0.7.1-0.20230809150046-334a913b9e91 h1:vIpyOSR3PqiaPKJAqIeR0u9ryvV0wPQDroHf3C6gPPs=
github.com/goware/cachestore v0.7.1-0.20230809150046-334a913b9e91/go.mod h1:n9v3qNfCRlXAAF4TCN/s1VUxEOVGh+w1ua0m+QD0ngo=
github.com/goware/cachestore v0.7.1-0.20230810101011-d24347595586 h1:nY1CMEjjz8p1GOK6PZc+VSoq8wIbkkgrsIJ1jP2IJV8=
github.com/goware/cachestore v0.7.1-0.20230810101011-d24347595586/go.mod h1:n9v3qNfCRlXAAF4TCN/s1VUxEOVGh+w1ua0m+QD0ngo=
github.com/goware/calc v0.2.0 h1:3B9qjXYpE0kgS4LhyklbM6X/0cOvZLdUZG7sdAuVCb4=
github.com/goware/calc v0.2.0/go.mod h1:BSQUbfS6ICW9RvSV9SikDY+t6/HQKI+CUxIpjE3VD28=
github.com/goware/channel v0.2.4 h1:ifU+wT0INGf0kdQpCnQRLAThOFUz98T0POHXmKsMat4=
Expand Down Expand Up @@ -112,8 +110,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o=
github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
Expand Down

0 comments on commit 1889f41

Please sign in to comment.