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

feat(listener): listener safe, finalized, latest block numbers #2762

Merged
merged 5 commits into from
Jun 21, 2024
Merged
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
22 changes: 21 additions & 1 deletion ethergo/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ipfs/go-log"
"github.com/jpillora/backoff"
"github.com/synapsecns/sanguine/core/metrics"
Expand Down Expand Up @@ -48,6 +49,8 @@
pollInterval, pollIntervalSetting time.Duration
// newBlockHandler is an optional handler that is called when a new block is detected.
newBlockHandler NewBlockHandler
finalityMode rpc.BlockNumber
blockWait uint64
}

var (
Expand All @@ -66,6 +69,8 @@
client: omnirpcClient,
backoff: newBackoffConfig(),
pollIntervalSetting: time.Millisecond * 50,
finalityMode: rpc.LatestBlockNumber,
blockWait: 0,
}

for _, option := range options {
Expand Down Expand Up @@ -132,7 +137,7 @@
}()

oldLatestBlock := c.latestBlock
c.latestBlock, err = c.client.BlockNumber(ctx)
c.latestBlock, err = c.getBlockNumber(ctx)
if err != nil {
return fmt.Errorf("could not get block number: %w", err)
}
Expand Down Expand Up @@ -183,6 +188,21 @@
return nil
}

func (c chainListener) getBlockNumber(ctx context.Context) (uint64, error) {
block, err := c.client.BlockByNumber(ctx, big.NewInt(c.finalityMode.Int64()))
if err != nil {
return 0, fmt.Errorf("could not get block by number: %w", err)
}

Check warning on line 195 in ethergo/listener/listener.go

View check run for this annotation

Codecov / codecov/patch

ethergo/listener/listener.go#L194-L195

Added lines #L194 - L195 were not covered by tests

blockNumber := block.Number()

if c.blockWait > 0 {
blockNumber.Sub(blockNumber, big.NewInt(int64(c.blockWait)))
}

Check warning on line 201 in ethergo/listener/listener.go

View check run for this annotation

Codecov / codecov/patch

ethergo/listener/listener.go#L200-L201

Added lines #L200 - L201 were not covered by tests

return blockNumber.Uint64(), nil
}

func (c chainListener) getMetadata(parentCtx context.Context) (startBlock, chainID uint64, err error) {
var lastIndexed uint64
ctx, span := c.handler.Tracer().Start(parentCtx, "getMetadata")
Expand Down
78 changes: 68 additions & 10 deletions ethergo/listener/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ package listener_test
import (
"context"
"fmt"
"sync"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/synapsecns/sanguine/ethergo/listener"
"sync"
)

func (l *ListenerTestSuite) TestListenForEvents() {
_, handle := l.manager.GetCounter(l.GetTestContext(), l.backend)
var wg sync.WaitGroup
const iterations = 10
for i := 0; i < iterations; i++ {
i := i
wg.Add(1)
go func(_ int) {
go func() {
defer wg.Done()

auth := l.backend.GetTxContext(l.GetTestContext(), nil)

//nolint:typecheck
bridgeRequestTX, err := handle.IncrementCounter(auth.TransactOpts)
l.NoError(err)
Expand All @@ -32,18 +30,55 @@ func (l *ListenerTestSuite) TestListenForEvents() {
l.NoError(err)
l.NotNil(bridgeResponseTX)
l.backend.WaitForConfirmation(l.GetTestContext(), bridgeResponseTX)
}(i)
}()
}

wg.Wait()

startBlock, err := handle.DeployBlock(&bind.CallOpts{Context: l.GetTestContext()})
l.NoError(err)

cl, err := listener.NewChainListener(l.backend, l.store, handle.Address(), uint64(startBlock.Int64()), l.metrics, listener.WithNewBlockHandler(func(ctx context.Context, block uint64) error {
fmt.Println(block)
return nil
}))
cl, err := listener.NewChainListener(
l.backend,
l.store,
handle.Address(),
uint64(startBlock.Int64()),
l.metrics,
listener.WithNewBlockHandler(func(ctx context.Context, block uint64) error {
fmt.Println(block)
return nil
}),
)
l.NoError(err)

clSafe, err := listener.NewChainListener(
l.backend,
l.store,
handle.Address(),
uint64(startBlock.Int64()),
l.metrics,
listener.WithNewBlockHandler(func(ctx context.Context, block uint64) error {
fmt.Println(block)
return nil
}),
listener.WithFinalityMode("safe"),
listener.WithBlockWait(10),
)
l.NoError(err)

clFinalized, err := listener.NewChainListener(
l.backend,
l.store,
handle.Address(),
uint64(startBlock.Int64()),
l.metrics,
listener.WithNewBlockHandler(func(ctx context.Context, block uint64) error {
fmt.Println(block)
return nil
}),
listener.WithFinalityMode("finalized"),
listener.WithBlockWait(10),
)
l.NoError(err)

eventCount := 0
Expand All @@ -59,4 +94,27 @@ func (l *ListenerTestSuite) TestListenForEvents() {

return nil
})

_ = clSafe.Listen(listenCtx, func(ctx context.Context, log types.Log) error {
eventCount++

if eventCount == iterations*2 {
cancel()
}

return nil
})

_ = clFinalized.Listen(listenCtx, func(ctx context.Context, log types.Log) error {
eventCount++

if eventCount == iterations*2 {
cancel()
}

return nil
})

l.NotEqual(cl.LatestBlock(), clFinalized.LatestBlock(), clSafe.LatestBlock())

}
23 changes: 23 additions & 0 deletions ethergo/listener/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import (
"context"
"time"

"github.com/ethereum/go-ethereum/rpc"
)

// Option is a functional option for chainListener.
Expand All @@ -24,3 +26,24 @@
c.pollIntervalSetting = interval
}
}

// WithFinalityMode sets the finality mode.
func WithFinalityMode(mode string) Option {
return func(c *chainListener) {
switch mode {
case "latest":
c.finalityMode = rpc.LatestBlockNumber

Check warning on line 35 in ethergo/listener/options.go

View check run for this annotation

Codecov / codecov/patch

ethergo/listener/options.go#L34-L35

Added lines #L34 - L35 were not covered by tests
case "safe":
c.finalityMode = rpc.SafeBlockNumber
case "finalized":
c.finalityMode = rpc.FinalizedBlockNumber
}
}
}

// WithBlockWait sets the block wait.
func WithBlockWait(wait uint64) Option {
return func(c *chainListener) {
c.blockWait = wait
}
}
Loading