Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy value and deferred chain id in ethmonitor #102

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions ethmonitor/ethmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync/atomic"
"time"

"github.com/0xsequence/ethkit"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/go-ethereum"
"github.com/0xsequence/ethkit/go-ethereum/common"
Expand Down Expand Up @@ -94,7 +95,7 @@ type Monitor struct {
provider ethrpc.Interface

chain *Chain
chainID *big.Int
chainID *ethkit.Lazy[big.Int]
nextBlockNumber *big.Int
blockCache cachestore.Store[*types.Block]
logCache cachestore.Store[[]types.Log]
Expand Down Expand Up @@ -133,14 +134,18 @@ func NewMonitor(provider ethrpc.Interface, options ...Options) (*Monitor, error)
}
}

chainID, err := getChainID(provider)
if err != nil {
return nil, err
}
chainID := ethkit.NewLazy(func() *big.Int {
chainId, err := getChainID(provider)
if err != nil {
return nil
}
return chainId
})

var (
blockCache cachestore.Store[*types.Block]
logCache cachestore.Store[[]types.Log]
err error
)
if opts.CacheBackend != nil {
blockCache, err = cachestorectl.Open[*types.Block](opts.CacheBackend, cachestore.WithLockExpiry(4*time.Second))
Expand Down Expand Up @@ -468,7 +473,7 @@ func (m *Monitor) filterLogs(ctx context.Context, blockHash common.Hash, topics
topicsDigest.Write([]byte{'\n'})
}

key := fmt.Sprintf("ethmonitor:%s:Logs:hash=%s;topics=%d", m.chainID.String(), blockHash.String(), topicsDigest.Sum64())
key := fmt.Sprintf("ethmonitor:%s:Logs:hash=%s;topics=%d", m.getChainID().String(), blockHash.String(), topicsDigest.Sum64())
return m.logCache.GetOrSetWithLockEx(ctx, key, getter, m.options.CacheExpiry)
}

Expand Down Expand Up @@ -531,7 +536,7 @@ func (m *Monitor) fetchNextBlock(ctx context.Context) (*types.Block, bool, error
}

if m.blockCache != nil {
key := fmt.Sprintf("ethmonitor:%s:BlockNum:%s", m.chainID.String(), m.nextBlockNumber.String())
key := fmt.Sprintf("ethmonitor:%s:BlockNum:%s", m.getChainID().String(), m.nextBlockNumber.String())
nextBlock, err := m.blockCache.GetOrSetWithLockEx(ctx, key, getter, m.options.CacheExpiry)
return nextBlock, miss, err
}
Expand Down Expand Up @@ -620,7 +625,7 @@ func (m *Monitor) fetchBlockByHash(ctx context.Context, hash common.Hash) (*type
}

if m.blockCache != nil {
key := fmt.Sprintf("ethmonitor:%s:BlockHash:%s", m.chainID.String(), hash.String())
key := fmt.Sprintf("ethmonitor:%s:BlockHash:%s", m.getChainID().String(), hash.String())
return m.blockCache.GetOrSetWithLockEx(ctx, key, getter, m.options.CacheExpiry)
}
return getter(ctx, "")
Expand Down Expand Up @@ -770,6 +775,14 @@ func (m *Monitor) PurgeHistory() {
}
}

func (m *Monitor) getChainID() *big.Int {
if val := m.chainID.Get(); val == nil {
return big.NewInt(-1)
} else {
return val
}
}

func getChainID(provider ethrpc.Interface) (*big.Int, error) {
var chainID *big.Int
err := breaker.Do(context.Background(), func() error {
Expand Down
16 changes: 16 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ func ToSliceValues[T any](in []*T) []T {
}
return out
}

type Lazy[T any] struct {
once func() *T
val *T
}

func NewLazy[T any](once func() *T) *Lazy[T] {
return &Lazy[T]{once: once}
}

func (l *Lazy[T]) Get() *T {
if l.val == nil {
l.val = l.once()
}
return l.val
}
Loading