diff --git a/node/pkg/watchers/evm/connectors/arbitrum.go b/node/pkg/watchers/evm/connectors/arbitrum.go deleted file mode 100644 index 791f73f42..000000000 --- a/node/pkg/watchers/evm/connectors/arbitrum.go +++ /dev/null @@ -1,42 +0,0 @@ -// On Arbitrum we are unable to get blocks by transaction hash using the go-ethereum library -// because it fails with "transaction type not supported". However, calling the underlying -// eth_getBlockByHash directly works. The sole function of this connector is to implement -// TimeOfBlockByHash using the raw connection. - -package connectors - -import ( - "context" - "fmt" - "strconv" - - ethCommon "github.com/ethereum/go-ethereum/common" -) - -type ArbitrumConnector struct { - Connector -} - -func NewArbitrumConnector(ctx context.Context, baseConnector Connector) (*ArbitrumConnector, error) { - connector := &ArbitrumConnector{Connector: baseConnector} - return connector, nil -} - -func (a *ArbitrumConnector) TimeOfBlockByHash(ctx context.Context, hash ethCommon.Hash) (uint64, error) { - type Marshaller struct { - Time string `json:"timestamp" gencodec:"required"` - } - - var m *Marshaller - err := a.RawCallContext(ctx, &m, "eth_getBlockByHash", hash, false) - if err != nil { - return 0, fmt.Errorf("failed to get block %s: %w", hash.String(), err) - } - - num, err := strconv.ParseUint(m.Time[2:], 16, 64) - if err != nil { - return 0, fmt.Errorf("failed to parse time %s: %w", m.Time, err) - } - - return num, nil -} diff --git a/node/pkg/watchers/evm/connectors/get_time_of_block.go b/node/pkg/watchers/evm/connectors/get_time_of_block.go new file mode 100644 index 000000000..01b789fa0 --- /dev/null +++ b/node/pkg/watchers/evm/connectors/get_time_of_block.go @@ -0,0 +1,40 @@ +package connectors + +import ( + "context" + "fmt" + "strconv" + + ethCommon "github.com/ethereum/go-ethereum/common" +) + +// ConnectorWithGetTimeOfBlockOverride is used to override the implementation of TimeOfBlockByHash() as defined in +// the go-ethereum library because on some chains it fails with "transaction type not supported". Calling the underlying +// eth_getBlockByHash directly works, so the sole function of this connector is to implement TimeOfBlockByHash() using the raw connection. +type ConnectorWithGetTimeOfBlockOverride struct { + Connector +} + +func NewConnectorWithGetTimeOfBlockOverride(ctx context.Context, baseConnector Connector) (*ConnectorWithGetTimeOfBlockOverride, error) { + connector := &ConnectorWithGetTimeOfBlockOverride{Connector: baseConnector} + return connector, nil +} + +func (a *ConnectorWithGetTimeOfBlockOverride) TimeOfBlockByHash(ctx context.Context, hash ethCommon.Hash) (uint64, error) { + type Marshaller struct { + Time string `json:"timestamp" gencodec:"required"` + } + + var m *Marshaller + err := a.RawCallContext(ctx, &m, "eth_getBlockByHash", hash, false) + if err != nil { + return 0, fmt.Errorf("failed to get block %s: %w", hash.String(), err) + } + + num, err := strconv.ParseUint(m.Time[2:], 16, 64) + if err != nil { + return 0, fmt.Errorf("failed to parse time %s: %w", m.Time, err) + } + + return num, nil +} diff --git a/node/pkg/watchers/evm/watcher.go b/node/pkg/watchers/evm/watcher.go index b3ed71dd8..6973dc893 100644 --- a/node/pkg/watchers/evm/watcher.go +++ b/node/pkg/watchers/evm/watcher.go @@ -272,7 +272,8 @@ func (w *Watcher) Run(ctx context.Context) error { p2p.DefaultRegistry.AddErrorCount(w.chainID, 1) return fmt.Errorf("creating block poll connector failed: %w", err) } - w.ethConn, err = connectors.NewArbitrumConnector(ctx, pollConnector) + // The standard geth BlockByHash() does not work on Arbitrum. + w.ethConn, err = connectors.NewConnectorWithGetTimeOfBlockOverride(ctx, pollConnector) if err != nil { ethConnectionErrors.WithLabelValues(w.networkName, "dial_error").Inc() p2p.DefaultRegistry.AddErrorCount(w.chainID, 1) @@ -318,9 +319,8 @@ func (w *Watcher) Run(ctx context.Context) error { p2p.DefaultRegistry.AddErrorCount(w.chainID, 1) return fmt.Errorf("creating block poll connector failed: %w", err) } - // I know this says Arbitrum. That's just what the type is called. - // But we need it the TimeOfBlockByHash() implementation. - w.ethConn, err = connectors.NewArbitrumConnector(ctx, pollConnector) + // The standard geth BlockByHash() does not work on Optimism Bedrock. + w.ethConn, err = connectors.NewConnectorWithGetTimeOfBlockOverride(ctx, pollConnector) if err != nil { ethConnectionErrors.WithLabelValues(w.networkName, "dial_error").Inc() p2p.DefaultRegistry.AddErrorCount(w.chainID, 1) @@ -357,8 +357,8 @@ func (w *Watcher) Run(ctx context.Context) error { p2p.DefaultRegistry.AddErrorCount(w.chainID, 1) return fmt.Errorf("creating block poll connector failed: %w", err) } - // Use the Arbitrum connector to get the TimeOfBlockByHash() implementation. - w.ethConn, err = connectors.NewArbitrumConnector(ctx, pollConnector) + // The standard geth BlockByHash() does not work on Base. + w.ethConn, err = connectors.NewConnectorWithGetTimeOfBlockOverride(ctx, pollConnector) if err != nil { ethConnectionErrors.WithLabelValues(w.networkName, "dial_error").Inc() p2p.DefaultRegistry.AddErrorCount(w.chainID, 1)