Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_network_info to all services + add timelord service #112

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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"`
Expand Down
23 changes: 23 additions & 0 deletions pkg/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Client struct {
HarvesterService *HarvesterService
CrawlerService *CrawlerService
DataLayerService *DataLayerService
TimelordService *TimelordService

websocketHandlers []rpcinterface.WebsocketResponseHandler
}
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/rpc/farmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
17 changes: 17 additions & 0 deletions pkg/rpc/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions pkg/rpc/timelord.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions pkg/websocketclient/websocketclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down