Commit 9dd1b384 authored by Seungju Lee's avatar Seungju Lee

refactor: standardize admin api into op-service

parent f94019c1
......@@ -2,6 +2,8 @@ package rpc
import (
"context"
"github.com/ethereum-optimism/optimism/op-service/rpc"
)
type batcherClient interface {
......@@ -10,6 +12,7 @@ type batcherClient interface {
}
type adminAPI struct {
*rpc.CommonAdminAPI
b batcherClient
}
......
......@@ -11,7 +11,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/version"
"github.com/ethereum-optimism/optimism/op-service/eth"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/rpc"
)
type l2EthClient interface {
......@@ -31,79 +31,51 @@ type driverClient interface {
SequencerActive(context.Context) (bool, error)
}
type rpcMetrics interface {
// RecordRPCServerRequest returns a function that records the duration of serving the given RPC method
RecordRPCServerRequest(method string) func()
}
type adminAPI struct {
*rpc.CommonAdminAPI
dr driverClient
m rpcMetrics
log log.Logger
}
func NewAdminAPI(dr driverClient, m rpcMetrics, log log.Logger) *adminAPI {
func NewAdminAPI(dr driverClient, m rpc.RpcMetrics, log log.Logger) *adminAPI {
return &adminAPI{
dr: dr,
m: m,
log: log,
CommonAdminAPI: rpc.NewCommonAdminAPI(m, log),
}
}
func (n *adminAPI) ResetDerivationPipeline(ctx context.Context) error {
recordDur := n.m.RecordRPCServerRequest("admin_resetDerivationPipeline")
recordDur := n.M.RecordRPCServerRequest("admin_resetDerivationPipeline")
defer recordDur()
return n.dr.ResetDerivationPipeline(ctx)
}
func (n *adminAPI) StartSequencer(ctx context.Context, blockHash common.Hash) error {
recordDur := n.m.RecordRPCServerRequest("admin_startSequencer")
recordDur := n.M.RecordRPCServerRequest("admin_startSequencer")
defer recordDur()
return n.dr.StartSequencer(ctx, blockHash)
}
func (n *adminAPI) StopSequencer(ctx context.Context) (common.Hash, error) {
recordDur := n.m.RecordRPCServerRequest("admin_stopSequencer")
recordDur := n.M.RecordRPCServerRequest("admin_stopSequencer")
defer recordDur()
return n.dr.StopSequencer(ctx)
}
func (n *adminAPI) SequencerActive(ctx context.Context) (bool, error) {
recordDur := n.m.RecordRPCServerRequest("admin_sequencerActive")
recordDur := n.M.RecordRPCServerRequest("admin_sequencerActive")
defer recordDur()
return n.dr.SequencerActive(ctx)
}
func (n *adminAPI) SetLogLevel(ctx context.Context, lvlStr string) error {
recordDur := n.m.RecordRPCServerRequest("admin_setLogLevel")
defer recordDur()
h := n.log.GetHandler()
lvl, err := log.LvlFromString(lvlStr)
if err != nil {
return err
}
// We set the log level, and do not wrap the handler with an additional filter handler,
// as the underlying handler would otherwise also still filter with the previous log level.
lvlSetter, ok := h.(oplog.LvlSetter)
if !ok {
return fmt.Errorf("log handler type %T cannot change log level", h)
}
lvlSetter.SetLogLevel(lvl)
return nil
}
type nodeAPI struct {
config *rollup.Config
client l2EthClient
dr driverClient
log log.Logger
m rpcMetrics
m rpc.RpcMetrics
}
func NewNodeAPI(config *rollup.Config, l2Client l2EthClient, dr driverClient, log log.Logger, m rpcMetrics) *nodeAPI {
func NewNodeAPI(config *rollup.Config, l2Client l2EthClient, dr driverClient, log log.Logger, m rpc.RpcMetrics) *nodeAPI {
return &nodeAPI{
config: config,
client: l2Client,
......
package rpc
import (
"context"
"fmt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/log"
)
type RpcMetrics interface {
// RecordRPCServerRequest returns a function that records the duration of serving the given RPC method
RecordRPCServerRequest(method string) func()
}
type CommonAdminAPI struct {
M RpcMetrics
log log.Logger
}
func NewCommonAdminAPI(m RpcMetrics, log log.Logger) *CommonAdminAPI {
return &CommonAdminAPI{
M: m,
log: log,
}
}
func (n *CommonAdminAPI) SetLogLevel(ctx context.Context, lvlStr string) error {
recordDur := n.M.RecordRPCServerRequest("admin_setLogLevel")
defer recordDur()
h := n.log.GetHandler()
lvl, err := log.LvlFromString(lvlStr)
if err != nil {
return err
}
// We set the log level, and do not wrap the handler with an additional filter handler,
// as the underlying handler would otherwise also still filter with the previous log level.
lvlSetter, ok := h.(oplog.LvlSetter)
if !ok {
return fmt.Errorf("log handler type %T cannot change log level", h)
}
lvlSetter.SetLogLevel(lvl)
return nil
}
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