2020-08-17 15:27:29 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-26 21:01:11 +00:00
|
|
|
"fmt"
|
2020-09-15 16:44:28 +00:00
|
|
|
"strconv"
|
|
|
|
|
2020-08-29 07:24:50 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
2021-03-23 09:00:07 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
2020-08-17 15:27:29 +00:00
|
|
|
)
|
|
|
|
|
2020-09-26 21:01:11 +00:00
|
|
|
// NetAPI the interface for the net_ RPC commands
|
2020-08-17 15:27:29 +00:00
|
|
|
type NetAPI interface {
|
2020-09-15 16:44:28 +00:00
|
|
|
Listening(_ context.Context) (bool, error)
|
|
|
|
Version(_ context.Context) (string, error)
|
|
|
|
PeerCount(_ context.Context) (hexutil.Uint, error)
|
2020-08-17 15:27:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 21:01:11 +00:00
|
|
|
// NetAPIImpl data structure to store things needed for net_ commands
|
2020-08-17 15:27:29 +00:00
|
|
|
type NetAPIImpl struct {
|
2021-03-23 09:00:07 +00:00
|
|
|
ethBackend core.ApiBackend
|
2020-08-17 15:27:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 21:01:11 +00:00
|
|
|
// NewNetAPIImpl returns NetAPIImplImpl instance
|
2021-03-23 09:00:07 +00:00
|
|
|
func NewNetAPIImpl(eth core.ApiBackend) *NetAPIImpl {
|
2020-08-17 15:27:29 +00:00
|
|
|
return &NetAPIImpl{
|
|
|
|
ethBackend: eth,
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 07:24:50 +00:00
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// Listening implements net_listening. Returns true if client is actively listening for network connections.
|
|
|
|
// TODO: Remove hard coded value
|
2020-09-15 16:44:28 +00:00
|
|
|
func (api *NetAPIImpl) Listening(_ context.Context) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// Version implements net_version. Returns the current network id.
|
2021-03-23 09:00:07 +00:00
|
|
|
func (api *NetAPIImpl) Version(ctx context.Context) (string, error) {
|
2020-09-26 21:01:11 +00:00
|
|
|
if api.ethBackend == nil {
|
|
|
|
// We're running in --chaindata mode or otherwise cannot get the backend
|
|
|
|
return "", fmt.Errorf(NotAvailableChainData, "net_version")
|
|
|
|
}
|
|
|
|
|
2021-03-23 09:00:07 +00:00
|
|
|
res, err := api.ethBackend.NetVersion(ctx)
|
2020-09-15 16:44:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-09-26 21:01:11 +00:00
|
|
|
|
2020-09-15 16:44:28 +00:00
|
|
|
return strconv.FormatUint(res, 10), nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// PeerCount implements net_peerCount. Returns number of peers currently connected to the client.
|
|
|
|
// TODO: This routine currently returns a hard coded value of '25'
|
2020-08-29 07:24:50 +00:00
|
|
|
func (api *NetAPIImpl) PeerCount(_ context.Context) (hexutil.Uint, error) {
|
|
|
|
return hexutil.Uint(25), nil
|
|
|
|
}
|