2021-11-30 22:42:12 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/p2p"
|
2022-06-10 15:18:43 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
2021-11-30 22:42:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AdminAPI the interface for the admin_* RPC commands.
|
|
|
|
type AdminAPI interface {
|
|
|
|
// NodeInfo returns a collection of metadata known about the host.
|
|
|
|
NodeInfo(ctx context.Context) (*p2p.NodeInfo, error)
|
2022-04-25 13:40:04 +00:00
|
|
|
|
|
|
|
// Peers returns information about the connected remote nodes.
|
|
|
|
// https://geth.ethereum.org/docs/rpc/ns-admin#admin_peers
|
|
|
|
Peers(ctx context.Context) ([]*p2p.PeerInfo, error)
|
2021-11-30 22:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AdminAPIImpl data structure to store things needed for admin_* commands.
|
|
|
|
type AdminAPIImpl struct {
|
2022-06-10 15:18:43 +00:00
|
|
|
ethBackend rpchelper.ApiBackend
|
2021-11-30 22:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdminAPI returns AdminAPIImpl instance.
|
2022-06-10 15:18:43 +00:00
|
|
|
func NewAdminAPI(eth rpchelper.ApiBackend) *AdminAPIImpl {
|
2021-11-30 22:42:12 +00:00
|
|
|
return &AdminAPIImpl{
|
|
|
|
ethBackend: eth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *AdminAPIImpl) NodeInfo(ctx context.Context) (*p2p.NodeInfo, error) {
|
|
|
|
nodes, err := api.ethBackend.NodeInfo(ctx, 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("node info request error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return nil, errors.New("empty nodesInfo response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &nodes[0], nil
|
|
|
|
}
|
2022-04-25 13:40:04 +00:00
|
|
|
|
|
|
|
func (api *AdminAPIImpl) Peers(ctx context.Context) ([]*p2p.PeerInfo, error) {
|
|
|
|
return api.ethBackend.Peers(ctx)
|
|
|
|
}
|