From d014dce1ba1a8c94970bfc2dccf5b7d40064bd45 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Thu, 20 Jun 2024 17:47:18 -0500 Subject: [PATCH 1/5] first commit --- ethergo/listener/listener.go | 24 ++++++++++++++++++++++++ ethergo/listener/options.go | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/ethergo/listener/listener.go b/ethergo/listener/listener.go index 8e8befd582..8c02dba98f 100644 --- a/ethergo/listener/listener.go +++ b/ethergo/listener/listener.go @@ -12,6 +12,7 @@ import ( "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" @@ -48,6 +49,8 @@ type chainListener struct { 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 ( @@ -66,6 +69,8 @@ func NewChainListener(omnirpcClient client.EVM, store listenerDB.ChainListenerDB client: omnirpcClient, backoff: newBackoffConfig(), pollIntervalSetting: time.Millisecond * 50, + finalityMode: FinalityModeLatest, + blockWait: 0, } for _, option := range options { @@ -183,6 +188,25 @@ func (c *chainListener) doPoll(parentCtx context.Context, handler HandleLog) (er return nil } +func (c chainListener) getBlockNumber(ctx context.Context) (uint64, error) { + var block *types.Block + var err error + + block, err = c.client.BlockByNumber(ctx, big.NewInt(c.finalityMode.Int64())) + + if err != nil { + return 0, err + } + + blockNumber := block.Number() + + if c.blockWait > 0 { + blockNumber.Sub(blockNumber, big.NewInt(int64(c.blockWait))) + } + + 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") diff --git a/ethergo/listener/options.go b/ethergo/listener/options.go index 89c45c759d..c0e976efcf 100644 --- a/ethergo/listener/options.go +++ b/ethergo/listener/options.go @@ -3,6 +3,8 @@ package listener import ( "context" "time" + + "github.com/ethereum/go-ethereum/rpc" ) // Option is a functional option for chainListener. @@ -24,3 +26,25 @@ func WithPollInterval(interval time.Duration) Option { c.pollIntervalSetting = interval } } + +// FinalityMode represents the finality mode for block queries. + +const ( + FinalityModeSafe rpc.BlockNumber = rpc.SafeBlockNumber + FinalityModeFinalized rpc.BlockNumber = rpc.FinalizedBlockNumber + FinalityModeLatest rpc.BlockNumber = rpc.FinalizedBlockNumber +) + +// WithFinalityMode sets the finality mode. +func WithFinalityMode(mode rpc.BlockNumber) Option { + return func(c *chainListener) { + c.finalityMode = mode + } +} + +// WithBlockWait sets the block wait. +func WithBlockWait(wait uint64) Option { + return func(c *chainListener) { + c.blockWait = wait + } +} From b2b0ea0eb5164b2971b81b5d1f27b95c1db77155 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Thu, 20 Jun 2024 17:55:34 -0500 Subject: [PATCH 2/5] fixing --- ethergo/listener/listener.go | 8 ++------ ethergo/listener/options.go | 10 +++++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/ethergo/listener/listener.go b/ethergo/listener/listener.go index 8c02dba98f..ccd5ca7a05 100644 --- a/ethergo/listener/listener.go +++ b/ethergo/listener/listener.go @@ -137,7 +137,7 @@ func (c *chainListener) doPoll(parentCtx context.Context, handler HandleLog) (er }() 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) } @@ -189,11 +189,7 @@ func (c *chainListener) doPoll(parentCtx context.Context, handler HandleLog) (er } func (c chainListener) getBlockNumber(ctx context.Context) (uint64, error) { - var block *types.Block - var err error - - block, err = c.client.BlockByNumber(ctx, big.NewInt(c.finalityMode.Int64())) - + block, err := c.client.BlockByNumber(ctx, big.NewInt(c.finalityMode.Int64())) if err != nil { return 0, err } diff --git a/ethergo/listener/options.go b/ethergo/listener/options.go index c0e976efcf..b71ffb7c3a 100644 --- a/ethergo/listener/options.go +++ b/ethergo/listener/options.go @@ -27,12 +27,12 @@ func WithPollInterval(interval time.Duration) Option { } } -// FinalityMode represents the finality mode for block queries. - const ( - FinalityModeSafe rpc.BlockNumber = rpc.SafeBlockNumber - FinalityModeFinalized rpc.BlockNumber = rpc.FinalizedBlockNumber - FinalityModeLatest rpc.BlockNumber = rpc.FinalizedBlockNumber + FinalityModeSafe rpc.BlockNumber = rpc.SafeBlockNumber + FinalityModeFinalized rpc.BlockNumber = rpc.FinalizedBlockNumber + FinalityModeLatest rpc.BlockNumber = rpc.LatestBlockNumber + FinalityModePending rpc.BlockNumber = rpc.PendingBlockNumber + FinalityModeEarliestBlockNumber rpc.BlockNumber = rpc.EarliestBlockNumber ) // WithFinalityMode sets the finality mode. From 32ce20c43e9e00a190a0a6f3956ac4cfc2686ebc Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Thu, 20 Jun 2024 18:18:24 -0500 Subject: [PATCH 3/5] cleanup --- ethergo/listener/listener.go | 2 +- ethergo/listener/options.go | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ethergo/listener/listener.go b/ethergo/listener/listener.go index ccd5ca7a05..9f59c1b0d5 100644 --- a/ethergo/listener/listener.go +++ b/ethergo/listener/listener.go @@ -69,7 +69,7 @@ func NewChainListener(omnirpcClient client.EVM, store listenerDB.ChainListenerDB client: omnirpcClient, backoff: newBackoffConfig(), pollIntervalSetting: time.Millisecond * 50, - finalityMode: FinalityModeLatest, + finalityMode: rpc.LatestBlockNumber, blockWait: 0, } diff --git a/ethergo/listener/options.go b/ethergo/listener/options.go index b71ffb7c3a..97f87b20ac 100644 --- a/ethergo/listener/options.go +++ b/ethergo/listener/options.go @@ -27,18 +27,21 @@ func WithPollInterval(interval time.Duration) Option { } } -const ( - FinalityModeSafe rpc.BlockNumber = rpc.SafeBlockNumber - FinalityModeFinalized rpc.BlockNumber = rpc.FinalizedBlockNumber - FinalityModeLatest rpc.BlockNumber = rpc.LatestBlockNumber - FinalityModePending rpc.BlockNumber = rpc.PendingBlockNumber - FinalityModeEarliestBlockNumber rpc.BlockNumber = rpc.EarliestBlockNumber -) - // WithFinalityMode sets the finality mode. -func WithFinalityMode(mode rpc.BlockNumber) Option { +func WithFinalityMode(mode string) Option { return func(c *chainListener) { - c.finalityMode = mode + switch mode { + case "latest": + c.finalityMode = rpc.LatestBlockNumber + case "earliest": + c.finalityMode = rpc.EarliestBlockNumber + case "pending": + c.finalityMode = rpc.PendingBlockNumber + case "safe": + c.finalityMode = rpc.SafeBlockNumber + case "finalized": + c.finalityMode = rpc.FinalizedBlockNumber + } } } From 1ec0562b1e7da6596df7c463160b9c65119cfefd Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Thu, 20 Jun 2024 18:59:16 -0500 Subject: [PATCH 4/5] test --- ethergo/listener/listener_test.go | 78 +++++++++++++++++++++++++++---- ethergo/listener/options.go | 4 -- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/ethergo/listener/listener_test.go b/ethergo/listener/listener_test.go index 633a5cfcef..7feae7f5d4 100644 --- a/ethergo/listener/listener_test.go +++ b/ethergo/listener/listener_test.go @@ -3,10 +3,11 @@ 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() { @@ -14,13 +15,10 @@ func (l *ListenerTestSuite) TestListenForEvents() { 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) @@ -32,7 +30,7 @@ func (l *ListenerTestSuite) TestListenForEvents() { l.NoError(err) l.NotNil(bridgeResponseTX) l.backend.WaitForConfirmation(l.GetTestContext(), bridgeResponseTX) - }(i) + }() } wg.Wait() @@ -40,10 +38,47 @@ func (l *ListenerTestSuite) TestListenForEvents() { 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 @@ -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()) + } diff --git a/ethergo/listener/options.go b/ethergo/listener/options.go index 97f87b20ac..9413ab315e 100644 --- a/ethergo/listener/options.go +++ b/ethergo/listener/options.go @@ -33,10 +33,6 @@ func WithFinalityMode(mode string) Option { switch mode { case "latest": c.finalityMode = rpc.LatestBlockNumber - case "earliest": - c.finalityMode = rpc.EarliestBlockNumber - case "pending": - c.finalityMode = rpc.PendingBlockNumber case "safe": c.finalityMode = rpc.SafeBlockNumber case "finalized": From 888792fb80aa71ecb8aa064ffc12ec928d5ae7d5 Mon Sep 17 00:00:00 2001 From: golangisfun123 Date: Thu, 20 Jun 2024 19:05:06 -0500 Subject: [PATCH 5/5] wrapped error lint --- ethergo/listener/listener.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethergo/listener/listener.go b/ethergo/listener/listener.go index 9f59c1b0d5..e55a997b09 100644 --- a/ethergo/listener/listener.go +++ b/ethergo/listener/listener.go @@ -191,7 +191,7 @@ func (c *chainListener) doPoll(parentCtx context.Context, handler HandleLog) (er 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, err + return 0, fmt.Errorf("could not get block by number: %w", err) } blockNumber := block.Number()