Commit ef9c2090 authored by Adrian Sutton's avatar Adrian Sutton

op-node: Add `admin_sequencerActive` RPC method

Returns true if the node is actively sequencing, otherwise false. This is essentially a parallel with the `eth_mining` API from execution clients.
parent 2e64d046
...@@ -121,6 +121,10 @@ func (s *l2VerifierBackend) StopSequencer(ctx context.Context) (common.Hash, err ...@@ -121,6 +121,10 @@ func (s *l2VerifierBackend) StopSequencer(ctx context.Context) (common.Hash, err
return common.Hash{}, errors.New("stopping the L2Verifier sequencer is not supported") return common.Hash{}, errors.New("stopping the L2Verifier sequencer is not supported")
} }
func (s *l2VerifierBackend) SequencerActive(ctx context.Context) (bool, error) {
return false, nil
}
func (s *L2Verifier) L2Finalized() eth.L2BlockRef { func (s *L2Verifier) L2Finalized() eth.L2BlockRef {
return s.derivation.Finalized() return s.derivation.Finalized()
} }
......
...@@ -26,18 +26,30 @@ func TestStopStartSequencer(t *testing.T) { ...@@ -26,18 +26,30 @@ func TestStopStartSequencer(t *testing.T) {
nodeRPC, err := rpc.DialContext(context.Background(), rollupNode.HTTPEndpoint()) nodeRPC, err := rpc.DialContext(context.Background(), rollupNode.HTTPEndpoint())
require.Nil(t, err, "Error dialing node") require.Nil(t, err, "Error dialing node")
rollupClient := sources.NewRollupClient(client.NewBaseRPCClient(nodeRPC))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
active, err := rollupClient.SequencerActive(ctx)
require.NoError(t, err)
require.True(t, active, "sequencer should be active")
blockBefore := latestBlock(t, l2Seq) blockBefore := latestBlock(t, l2Seq)
time.Sleep(time.Duration(cfg.DeployConfig.L2BlockTime+1) * time.Second) time.Sleep(time.Duration(cfg.DeployConfig.L2BlockTime+1) * time.Second)
blockAfter := latestBlock(t, l2Seq) blockAfter := latestBlock(t, l2Seq)
require.Greaterf(t, blockAfter, blockBefore, "Chain did not advance") require.Greaterf(t, blockAfter, blockBefore, "Chain did not advance")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
blockHash := common.Hash{} blockHash, err := rollupClient.StopSequencer(ctx)
err = nodeRPC.CallContext(ctx, &blockHash, "admin_stopSequencer")
require.Nil(t, err, "Error stopping sequencer") require.Nil(t, err, "Error stopping sequencer")
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
active, err = rollupClient.SequencerActive(ctx)
require.NoError(t, err)
require.False(t, active, "sequencer should be inactive")
blockBefore = latestBlock(t, l2Seq) blockBefore = latestBlock(t, l2Seq)
time.Sleep(time.Duration(cfg.DeployConfig.L2BlockTime+1) * time.Second) time.Sleep(time.Duration(cfg.DeployConfig.L2BlockTime+1) * time.Second)
blockAfter = latestBlock(t, l2Seq) blockAfter = latestBlock(t, l2Seq)
...@@ -45,9 +57,15 @@ func TestStopStartSequencer(t *testing.T) { ...@@ -45,9 +57,15 @@ func TestStopStartSequencer(t *testing.T) {
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
err = nodeRPC.CallContext(ctx, nil, "admin_startSequencer", blockHash) err = rollupClient.StartSequencer(ctx, blockHash)
require.Nil(t, err, "Error starting sequencer") require.Nil(t, err, "Error starting sequencer")
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
active, err = rollupClient.SequencerActive(ctx)
require.NoError(t, err)
require.True(t, active, "sequencer should be active again")
blockBefore = latestBlock(t, l2Seq) blockBefore = latestBlock(t, l2Seq)
time.Sleep(time.Duration(cfg.DeployConfig.L2BlockTime+1) * time.Second) time.Sleep(time.Duration(cfg.DeployConfig.L2BlockTime+1) * time.Second)
blockAfter = latestBlock(t, l2Seq) blockAfter = latestBlock(t, l2Seq)
......
...@@ -28,6 +28,7 @@ type driverClient interface { ...@@ -28,6 +28,7 @@ type driverClient interface {
ResetDerivationPipeline(context.Context) error ResetDerivationPipeline(context.Context) error
StartSequencer(ctx context.Context, blockHash common.Hash) error StartSequencer(ctx context.Context, blockHash common.Hash) error
StopSequencer(context.Context) (common.Hash, error) StopSequencer(context.Context) (common.Hash, error)
SequencerActive(context.Context) (bool, error)
} }
type rpcMetrics interface { type rpcMetrics interface {
...@@ -65,6 +66,12 @@ func (n *adminAPI) StopSequencer(ctx context.Context) (common.Hash, error) { ...@@ -65,6 +66,12 @@ func (n *adminAPI) StopSequencer(ctx context.Context) (common.Hash, error) {
return n.dr.StopSequencer(ctx) return n.dr.StopSequencer(ctx)
} }
func (n *adminAPI) SequencerActive(ctx context.Context) (bool, error) {
recordDur := n.m.RecordRPCServerRequest("admin_sequencerActive")
defer recordDur()
return n.dr.SequencerActive(ctx)
}
type nodeAPI struct { type nodeAPI struct {
config *rollup.Config config *rollup.Config
client l2EthClient client l2EthClient
......
...@@ -227,3 +227,7 @@ func (c *mockDriverClient) StartSequencer(ctx context.Context, blockHash common. ...@@ -227,3 +227,7 @@ func (c *mockDriverClient) StartSequencer(ctx context.Context, blockHash common.
func (c *mockDriverClient) StopSequencer(ctx context.Context) (common.Hash, error) { func (c *mockDriverClient) StopSequencer(ctx context.Context) (common.Hash, error) {
return c.Mock.MethodCalled("StopSequencer").Get(0).(common.Hash), nil return c.Mock.MethodCalled("StopSequencer").Get(0).(common.Hash), nil
} }
func (c *mockDriverClient) SequencerActive(ctx context.Context) (bool, error) {
return c.Mock.MethodCalled("SequencerActive").Get(0).(bool), nil
}
...@@ -127,6 +127,7 @@ func NewDriver(driverCfg *Config, cfg *rollup.Config, l2 L2Chain, l1 L1Chain, al ...@@ -127,6 +127,7 @@ func NewDriver(driverCfg *Config, cfg *rollup.Config, l2 L2Chain, l1 L1Chain, al
forceReset: make(chan chan struct{}, 10), forceReset: make(chan chan struct{}, 10),
startSequencer: make(chan hashAndErrorChannel, 10), startSequencer: make(chan hashAndErrorChannel, 10),
stopSequencer: make(chan chan hashAndError, 10), stopSequencer: make(chan chan hashAndError, 10),
sequencerActive: make(chan chan bool, 10),
sequencerNotifs: sequencerStateListener, sequencerNotifs: sequencerStateListener,
config: cfg, config: cfg,
driverConfig: driverCfg, driverConfig: driverCfg,
......
...@@ -47,6 +47,11 @@ type Driver struct { ...@@ -47,6 +47,11 @@ type Driver struct {
// It tells the caller that the sequencer stopped by returning the latest sequenced L2 block hash. // It tells the caller that the sequencer stopped by returning the latest sequenced L2 block hash.
stopSequencer chan chan hashAndError stopSequencer chan chan hashAndError
// Upon receiving a channel in this channel, the current sequencer status is queried.
// It tells the caller the status by outputting a boolean to the provided channel:
// true when the sequencer is active, false when it is not.
sequencerActive chan chan bool
// sequencerNotifs is notified when the sequencer is started or stopped // sequencerNotifs is notified when the sequencer is started or stopped
sequencerNotifs SequencerStateListener sequencerNotifs SequencerStateListener
...@@ -373,6 +378,8 @@ func (s *Driver) eventLoop() { ...@@ -373,6 +378,8 @@ func (s *Driver) eventLoop() {
s.driverConfig.SequencerStopped = true s.driverConfig.SequencerStopped = true
respCh <- hashAndError{hash: s.derivation.UnsafeL2Head().Hash} respCh <- hashAndError{hash: s.derivation.UnsafeL2Head().Hash}
} }
case respCh := <-s.sequencerActive:
respCh <- !s.driverConfig.SequencerStopped
case <-s.done: case <-s.done:
return return
} }
...@@ -436,6 +443,24 @@ func (s *Driver) StopSequencer(ctx context.Context) (common.Hash, error) { ...@@ -436,6 +443,24 @@ func (s *Driver) StopSequencer(ctx context.Context) (common.Hash, error) {
} }
} }
func (s *Driver) SequencerActive(ctx context.Context) (bool, error) {
if !s.driverConfig.SequencerEnabled {
return false, nil
}
respCh := make(chan bool, 1)
select {
case <-ctx.Done():
return false, ctx.Err()
case s.sequencerActive <- respCh:
select {
case <-ctx.Done():
return false, ctx.Err()
case active := <-respCh:
return active, nil
}
}
}
// syncStatus returns the current sync status, and should only be called synchronously with // syncStatus returns the current sync status, and should only be called synchronously with
// the driver event loop to avoid retrieval of an inconsistent status. // the driver event loop to avoid retrieval of an inconsistent status.
func (s *Driver) syncStatus() *eth.SyncStatus { func (s *Driver) syncStatus() *eth.SyncStatus {
......
...@@ -52,3 +52,9 @@ func (r *RollupClient) StopSequencer(ctx context.Context) (common.Hash, error) { ...@@ -52,3 +52,9 @@ func (r *RollupClient) StopSequencer(ctx context.Context) (common.Hash, error) {
err := r.rpc.CallContext(ctx, &result, "admin_stopSequencer") err := r.rpc.CallContext(ctx, &result, "admin_stopSequencer")
return result, err return result, err
} }
func (r *RollupClient) SequencerActive(ctx context.Context) (bool, error) {
var result bool
err := r.rpc.CallContext(ctx, &result, "admin_sequencerActive")
return result, err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment