From 7e98d2d516cc58e605ef9cc169bf634193d0e68c Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Thu, 14 Mar 2024 22:15:17 -0500 Subject: [PATCH 1/6] Add get_network_info to all services + add timelord service --- pkg/config/config.go | 7 ++++ pkg/httpclient/httpclient.go | 23 +++++++++++ pkg/rpc/client.go | 2 + pkg/rpc/farmer.go | 17 ++++++++ pkg/rpc/harvester.go | 17 ++++++++ pkg/rpc/timelord.go | 55 ++++++++++++++++++++++++++ pkg/websocketclient/websocketclient.go | 6 ++- 7 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 pkg/rpc/timelord.go diff --git a/pkg/config/config.go b/pkg/config/config.go index ac8334a..1290883 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..5245c38 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 full node +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..365597a 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 full node +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..02f5c3c --- /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 crawler 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.ServiceCrawler, 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") } From cdda2437e33ebe884d7cb0f5ae45fc6dd0c7b92f Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Thu, 14 Mar 2024 22:19:42 -0500 Subject: [PATCH 2/6] Fix yaml tag --- pkg/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 1290883..9b80ed0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -20,7 +20,7 @@ type ChiaConfig struct { Wallet WalletConfig `yaml:"wallet"` Seeder SeederConfig `yaml:"seeder"` DataLayer DataLayerConfig `yaml:"data_layer"` - Timelord TimelordConfig `yaml:timelord` + Timelord TimelordConfig `yaml:"timelord"` SelectedNetwork string `yaml:"selected_network"` } From 8e41730871e70b12eb651c420a402bcf2a7ebbe9 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Fri, 15 Mar 2024 18:37:47 -0500 Subject: [PATCH 3/6] Update pkg/rpc/timelord.go Co-authored-by: StartToaster --- pkg/rpc/timelord.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/rpc/timelord.go b/pkg/rpc/timelord.go index 02f5c3c..8af4673 100644 --- a/pkg/rpc/timelord.go +++ b/pkg/rpc/timelord.go @@ -13,7 +13,7 @@ type TimelordService struct { // 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.ServiceCrawler, rpcEndpoint, opt) + return s.client.NewRequest(rpcinterface.ServiceTimelord, rpcEndpoint, opt) } // Do is just a shortcut to the client's Do method From aa2ed677c231e6399a3d7f3c3579b916a5ce39c9 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Fri, 15 Mar 2024 18:37:52 -0500 Subject: [PATCH 4/6] Update pkg/rpc/timelord.go Co-authored-by: StartToaster --- pkg/rpc/timelord.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/rpc/timelord.go b/pkg/rpc/timelord.go index 8af4673..9ca4b93 100644 --- a/pkg/rpc/timelord.go +++ b/pkg/rpc/timelord.go @@ -6,7 +6,7 @@ import ( "github.com/chia-network/go-chia-libs/pkg/rpcinterface" ) -// TimelordService encapsulates crawler RPC methods +// TimelordService encapsulates timelord RPC methods type TimelordService struct { client *Client } From a7fcbc35dfa4658a3754135e34b72b936d3f2040 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Fri, 15 Mar 2024 18:37:58 -0500 Subject: [PATCH 5/6] Update pkg/rpc/harvester.go Co-authored-by: StartToaster --- pkg/rpc/harvester.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/rpc/harvester.go b/pkg/rpc/harvester.go index 365597a..556c349 100644 --- a/pkg/rpc/harvester.go +++ b/pkg/rpc/harvester.go @@ -40,7 +40,7 @@ func (s *HarvesterService) GetConnections(opts *GetConnectionsOptions) (*GetConn return c, resp, nil } -// GetNetworkInfo gets the network name and prefix from the full node +// 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 { From eb19a900d74edd690b73873e8eb42f9580bfdc9b Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Fri, 15 Mar 2024 18:38:04 -0500 Subject: [PATCH 6/6] Update pkg/rpc/farmer.go Co-authored-by: StartToaster --- pkg/rpc/farmer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/rpc/farmer.go b/pkg/rpc/farmer.go index 5245c38..15abd5e 100644 --- a/pkg/rpc/farmer.go +++ b/pkg/rpc/farmer.go @@ -41,7 +41,7 @@ func (s *FarmerService) GetConnections(opts *GetConnectionsOptions) (*GetConnect return c, resp, nil } -// GetNetworkInfo gets the network name and prefix from the full node +// 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 {