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

Add arb_checkPublisherHealth method #898

Merged
merged 4 commits into from
Aug 4, 2022
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
26 changes: 19 additions & 7 deletions arbnode/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,25 @@ import (
)

type BlockValidatorAPI struct {
val *validator.BlockValidator
}

func (a *BlockValidatorAPI) LatestValidatedBlock(ctx context.Context) (hexutil.Uint64, error) {
block := a.val.LastBlockValidated()
return hexutil.Uint64(block), nil
}

func (a *BlockValidatorAPI) LatestValidatedBlockHash(ctx context.Context) (common.Hash, error) {
_, hash, _ := a.val.LastBlockValidatedAndHash()
return hash, nil
}

type BlockValidatorDebugAPI struct {
val *validator.BlockValidator
blockchain *core.BlockChain
}

func (a *BlockValidatorAPI) RevalidateBlock(ctx context.Context, blockNum rpc.BlockNumberOrHash, moduleRootOptional *common.Hash) (bool, error) {
func (a *BlockValidatorDebugAPI) RevalidateBlock(ctx context.Context, blockNum rpc.BlockNumberOrHash, moduleRootOptional *common.Hash) (bool, error) {
header, err := arbitrum.HeaderByNumberOrHash(a.blockchain, blockNum)
if err != nil {
return false, err
Expand All @@ -47,14 +61,12 @@ func (a *BlockValidatorAPI) RevalidateBlock(ctx context.Context, blockNum rpc.Bl
return a.val.ValidateBlock(ctx, header, moduleRoot)
}

func (a *BlockValidatorAPI) LatestValidatedBlock(ctx context.Context) (hexutil.Uint64, error) {
block := a.val.LastBlockValidated()
return hexutil.Uint64(block), nil
type ArbAPI struct {
txPublisher TransactionPublisher
}

func (a *BlockValidatorAPI) LatestValidatedBlockHash(ctx context.Context) (common.Hash, error) {
_, hash, _ := a.val.LastBlockValidatedAndHash()
return hash, nil
func (a *ArbAPI) CheckPublisherHealth(ctx context.Context) error {
return a.txPublisher.CheckHealth(ctx)
}

type ArbDebugAPI struct {
Expand Down
1 change: 1 addition & 0 deletions arbnode/arb_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type TransactionPublisher interface {
PublishTransaction(ctx context.Context, tx *types.Transaction) error
CheckHealth(ctx context.Context) error
Initialize(context.Context) error
Start(context.Context) error
StopAndWait()
Expand Down
55 changes: 46 additions & 9 deletions arbnode/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ package arbnode

import (
"context"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/pkg/errors"
)

type TxForwarder struct {
enabled int32
target string
timeout time.Duration
client *ethclient.Client
enabled int32
target string
timeout time.Duration
rpcClient *rpc.Client
ethClient *ethclient.Client

healthMutex sync.Mutex
healthErr error
healthChecked time.Time
}

func NewForwarder(target string, timeout time.Duration) *TxForwarder {
Expand All @@ -40,22 +47,46 @@ func (f *TxForwarder) PublishTransaction(inctx context.Context, tx *types.Transa
}
ctx, cancelFunc := f.ctxWithTimeout(inctx)
defer cancelFunc()
return f.client.SendTransaction(ctx, tx)
return f.ethClient.SendTransaction(ctx, tx)
}

const cacheUpstreamHealth = 2 * time.Second
const maxHealthTimeout = 10 * time.Second

func (f *TxForwarder) CheckHealth(inctx context.Context) error {
if atomic.LoadInt32(&f.enabled) == 0 {
return ErrNoSequencer
}
f.healthMutex.Lock()
defer f.healthMutex.Unlock()
if time.Since(f.healthChecked) > cacheUpstreamHealth {
timeout := f.timeout
if timeout == time.Duration(0) || timeout >= maxHealthTimeout {
timeout = maxHealthTimeout
}
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
f.healthErr = f.rpcClient.CallContext(ctx, nil, "arb_checkPublisherHealth")
f.healthChecked = time.Now()
}
return f.healthErr
}

func (f *TxForwarder) Initialize(inctx context.Context) error {
if f.target == "" {
f.client = nil
f.rpcClient = nil
f.ethClient = nil
f.enabled = 0
return nil
}
ctx, cancelFunc := f.ctxWithTimeout(inctx)
defer cancelFunc()
client, err := ethclient.DialContext(ctx, f.target)
rpcClient, err := rpc.DialContext(ctx, f.target)
if err != nil {
return err
}
f.client = client
f.rpcClient = rpcClient
f.ethClient = ethclient.NewClient(rpcClient)
f.enabled = 1
return nil
}
Expand All @@ -77,8 +108,14 @@ func NewTxDropper() *TxDropper {
return &TxDropper{}
}

var txDropperErr = errors.New("publishing transactions not supported by this endpoint")

func (f *TxDropper) PublishTransaction(ctx context.Context, tx *types.Transaction) error {
return errors.New("transactions not supported by this endpoint")
return txDropperErr
}

func (f *TxDropper) CheckHealth(ctx context.Context) error {
return txDropperErr
}

func (f *TxDropper) Initialize(ctx context.Context) error { return nil }
Expand Down
14 changes: 13 additions & 1 deletion arbnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,11 +1056,23 @@ func CreateNode(
apis = append(apis, rpc.API{
Namespace: "arb",
Version: "1.0",
Service: &BlockValidatorAPI{val: currentNode.BlockValidator, blockchain: l2BlockChain},
Service: &BlockValidatorAPI{val: currentNode.BlockValidator},
Public: false,
})
apis = append(apis, rpc.API{
Namespace: "arbdebug",
Version: "1.0",
Service: &BlockValidatorDebugAPI{val: currentNode.BlockValidator, blockchain: l2BlockChain},
Public: false,
})
}

apis = append(apis, rpc.API{
Namespace: "arb",
Version: "1.0",
Service: &ArbAPI{currentNode.TxPublisher},
Public: false,
})
apis = append(apis, rpc.API{
Namespace: "arbdebug",
Version: "1.0",
Expand Down
14 changes: 14 additions & 0 deletions arbnode/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ func (s *Sequencer) postTxFilter(state *arbosState.ArbosState, tx *types.Transac
return nil
}

func (s *Sequencer) CheckHealth(ctx context.Context) error {
s.forwarderMutex.Lock()
forwarder := s.forwarder
s.forwarderMutex.Unlock()
if forwarder != nil {
return forwarder.CheckHealth(ctx)
}

if s.txStreamer.coordinator != nil && !s.txStreamer.coordinator.CurrentlyChosen() {
return ErrNoSequencer
}
return nil
}

func (s *Sequencer) ForwardTarget() string {
s.forwarderMutex.Lock()
defer s.forwarderMutex.Unlock()
Expand Down
4 changes: 4 additions & 0 deletions arbnode/tx_pre_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,7 @@ func (c *TxPreChecker) PublishTransaction(ctx context.Context, tx *types.Transac
}
return c.publisher.PublishTransaction(ctx, tx)
}

func (c *TxPreChecker) CheckHealth(ctx context.Context) error {
return c.publisher.CheckHealth(ctx)
}
4 changes: 2 additions & 2 deletions cmd/genericconf/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type HTTPConfig struct {
var HTTPConfigDefault = HTTPConfig{
Addr: node.DefaultConfig.HTTPHost,
Port: 8547,
API: append(node.DefaultConfig.HTTPModules, "eth"),
API: append(node.DefaultConfig.HTTPModules, "eth", "arb"),
RPCPrefix: node.DefaultConfig.HTTPPathPrefix,
CORSDomain: node.DefaultConfig.HTTPCors,
VHosts: node.DefaultConfig.HTTPVirtualHosts,
Expand Down Expand Up @@ -89,7 +89,7 @@ type WSConfig struct {
var WSConfigDefault = WSConfig{
Addr: node.DefaultConfig.WSHost,
Port: 8548,
API: append(node.DefaultConfig.WSModules, "eth"),
API: append(node.DefaultConfig.WSModules, "eth", "arb"),
RPCPrefix: node.DefaultConfig.WSPathPrefix,
Origins: node.DefaultConfig.WSOrigins,
ExposeAll: node.DefaultConfig.WSExposeAll,
Expand Down