Node: Rename Arbitrum connector (#2432)

* Node: Rename Arbitrum connector

Change-Id: I3cb2212f923f260ae92f6bd97b747f9839495744

* Rename the class

Change-Id: I9f8dd5efd3d5e058c1ed70750215144908dbafef
This commit is contained in:
bruce-riley 2023-03-01 11:14:25 -05:00 committed by GitHub
parent 73841556ba
commit 26da76077e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 48 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)