diff --git a/pkg/config/config.go b/pkg/config/config.go index ac8334a..9b80ed0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -20,6 +20,7 @@ type ChiaConfig struct { Wallet WalletConfig `yaml:"wallet"` Seeder SeederConfig `yaml:"seeder"` DataLayer DataLayerConfig `yaml:"data_layer"` + Timelord TimelordConfig `yaml:"timelord"` SelectedNetwork string `yaml:"selected_network"` } @@ -67,6 +68,12 @@ type DataLayerConfig struct { SSL SSLConfig `yaml:"ssl"` } +// TimelordConfig timelord configuration section +type TimelordConfig struct { + PortConfig `yaml:",inline"` + SSL SSLConfig `yaml:"ssl"` +} + // PortConfig common port settings found in many sections of the config type PortConfig struct { Port uint16 `yaml:"port"` diff --git a/pkg/httpclient/httpclient.go b/pkg/httpclient/httpclient.go index 964fae6..bf55a30 100644 --- a/pkg/httpclient/httpclient.go +++ b/pkg/httpclient/httpclient.go @@ -50,6 +50,10 @@ type HTTPClient struct { datalayerPort uint16 datalayerKeyPair *tls.Certificate datalayerClient *http.Client + + timelordPort uint16 + timelordKeyPair *tls.Certificate + timelordClient *http.Client } // NewHTTPClient returns a new HTTP client that satisfies the rpcinterface.Client interface @@ -65,6 +69,7 @@ func NewHTTPClient(cfg *config.ChiaConfig, options ...rpcinterface.ClientOptionF walletPort: cfg.Wallet.RPCPort, crawlerPort: cfg.Seeder.CrawlerConfig.RPCPort, datalayerPort: cfg.DataLayer.RPCPort, + timelordPort: cfg.Timelord.RPCPort, } // Sets the default host. Can be overridden by client options @@ -239,6 +244,14 @@ func (c *HTTPClient) generateHTTPClientForService(service rpcinterface.ServiceTy } } keyPair = c.datalayerKeyPair + case rpcinterface.ServiceTimelord: + if c.timelordKeyPair == nil { + c.timelordKeyPair, err = c.config.Timelord.SSL.LoadPrivateKeyPair(c.config.ChiaRoot) + if err != nil { + return nil, err + } + } + keyPair = c.timelordKeyPair default: return nil, fmt.Errorf("unknown service") } @@ -281,6 +294,8 @@ func (c *HTTPClient) portForService(service rpcinterface.ServiceType) uint16 { port = c.crawlerPort case rpcinterface.ServiceDataLayer: port = c.datalayerPort + case rpcinterface.ServiceTimelord: + port = c.timelordPort } return port @@ -342,6 +357,14 @@ func (c *HTTPClient) httpClientForService(service rpcinterface.ServiceType) (*ht } } client = c.datalayerClient + case rpcinterface.ServiceTimelord: + if c.timelordClient == nil { + c.timelordClient, err = c.generateHTTPClientForService(rpcinterface.ServiceTimelord) + if err != nil { + return nil, err + } + } + client = c.timelordClient } if client == nil { diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 52af01b..bf94a67 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -24,6 +24,7 @@ type Client struct { HarvesterService *HarvesterService CrawlerService *CrawlerService DataLayerService *DataLayerService + TimelordService *TimelordService websocketHandlers []rpcinterface.WebsocketResponseHandler } @@ -69,6 +70,7 @@ func NewClient(connectionMode ConnectionMode, configOption rpcinterface.ConfigOp c.HarvesterService = &HarvesterService{client: c} c.CrawlerService = &CrawlerService{client: c} c.DataLayerService = &DataLayerService{client: c} + c.TimelordService = &TimelordService{client: c} return c, nil } diff --git a/pkg/rpc/farmer.go b/pkg/rpc/farmer.go index 425c8c2..15abd5e 100644 --- a/pkg/rpc/farmer.go +++ b/pkg/rpc/farmer.go @@ -41,6 +41,23 @@ func (s *FarmerService) GetConnections(opts *GetConnectionsOptions) (*GetConnect return c, resp, nil } +// GetNetworkInfo gets the network name and prefix from the farmer +func (s *FarmerService) GetNetworkInfo(opts *GetNetworkInfoOptions) (*GetNetworkInfoResponse, *http.Response, error) { + request, err := s.NewRequest("get_network_info", opts) + if err != nil { + return nil, nil, err + } + + r := &GetNetworkInfoResponse{} + + resp, err := s.Do(request, r) + if err != nil { + return nil, resp, err + } + + return r, resp, nil +} + // FarmerGetHarvestersOptions optoins for get_harvesters endpoint. Currently, accepts no options type FarmerGetHarvestersOptions struct{} diff --git a/pkg/rpc/harvester.go b/pkg/rpc/harvester.go index 21559cc..556c349 100644 --- a/pkg/rpc/harvester.go +++ b/pkg/rpc/harvester.go @@ -40,6 +40,23 @@ func (s *HarvesterService) GetConnections(opts *GetConnectionsOptions) (*GetConn return c, resp, nil } +// GetNetworkInfo gets the network name and prefix from the harvester +func (s *HarvesterService) GetNetworkInfo(opts *GetNetworkInfoOptions) (*GetNetworkInfoResponse, *http.Response, error) { + request, err := s.NewRequest("get_network_info", opts) + if err != nil { + return nil, nil, err + } + + r := &GetNetworkInfoResponse{} + + resp, err := s.Do(request, r) + if err != nil { + return nil, resp, err + } + + return r, resp, nil +} + // HarvesterGetPlotsResponse get_plots response format type HarvesterGetPlotsResponse struct { Response diff --git a/pkg/rpc/timelord.go b/pkg/rpc/timelord.go new file mode 100644 index 0000000..9ca4b93 --- /dev/null +++ b/pkg/rpc/timelord.go @@ -0,0 +1,55 @@ +package rpc + +import ( + "net/http" + + "github.com/chia-network/go-chia-libs/pkg/rpcinterface" +) + +// TimelordService encapsulates timelord RPC methods +type TimelordService struct { + client *Client +} + +// NewRequest returns a new request specific to the crawler service +func (s *TimelordService) NewRequest(rpcEndpoint rpcinterface.Endpoint, opt interface{}) (*rpcinterface.Request, error) { + return s.client.NewRequest(rpcinterface.ServiceTimelord, rpcEndpoint, opt) +} + +// Do is just a shortcut to the client's Do method +func (s *TimelordService) Do(req *rpcinterface.Request, v interface{}) (*http.Response, error) { + return s.client.Do(req, v) +} + +// GetConnections returns connections +func (s *TimelordService) GetConnections(opts *GetConnectionsOptions) (*GetConnectionsResponse, *http.Response, error) { + request, err := s.NewRequest("get_connections", opts) + if err != nil { + return nil, nil, err + } + + c := &GetConnectionsResponse{} + resp, err := s.Do(request, c) + if err != nil { + return nil, resp, err + } + + return c, resp, nil +} + +// GetNetworkInfo gets the network name and prefix from the full node +func (s *TimelordService) GetNetworkInfo(opts *GetNetworkInfoOptions) (*GetNetworkInfoResponse, *http.Response, error) { + request, err := s.NewRequest("get_network_info", opts) + if err != nil { + return nil, nil, err + } + + r := &GetNetworkInfoResponse{} + + resp, err := s.Do(request, r) + if err != nil { + return nil, resp, err + } + + return r, resp, nil +} diff --git a/pkg/websocketclient/websocketclient.go b/pkg/websocketclient/websocketclient.go index a5e7a63..0c2a884 100644 --- a/pkg/websocketclient/websocketclient.go +++ b/pkg/websocketclient/websocketclient.go @@ -118,13 +118,15 @@ func (c *WebsocketClient) Do(req *rpcinterface.Request, v interface{}) (*http.Re case rpcinterface.ServiceFullNode: destination = "chia_full_node" case rpcinterface.ServiceFarmer: - destination = "chia_farmer" // @TODO validate the correct string for this + destination = "chia_farmer" case rpcinterface.ServiceHarvester: - destination = "chia_harvester" // @TODO validate the correct string for this + destination = "chia_harvester" case rpcinterface.ServiceWallet: destination = "chia_wallet" case rpcinterface.ServiceCrawler: destination = "chia_crawler" + case rpcinterface.ServiceTimelord: + destination = "chia_timelord" default: return nil, fmt.Errorf("unknown service") }