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

feat: add datalayer connection #91

Merged
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 @@ -19,6 +19,7 @@ type ChiaConfig struct {
Harvester HarvesterConfig `yaml:"harvester"`
Wallet WalletConfig `yaml:"wallet"`
Seeder SeederConfig `yaml:"seeder"`
DataLayer DataLayerConfig `yaml:"data_layer"`
SelectedNetwork string `yaml:"selected_network"`
}

Expand Down Expand Up @@ -60,6 +61,12 @@ type CrawlerConfig struct {
SSL SSLConfig `yaml:"ssl"`
}

// DataLayerConfig datalayer configuration section
type DataLayerConfig 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 @@ -46,6 +46,10 @@ type HTTPClient struct {
crawlerPort uint16
crawlerKeyPair *tls.Certificate
crawlerClient *http.Client

datalayerPort uint16
datalayerKeyPair *tls.Certificate
datalayerClient *http.Client
}

// NewHTTPClient returns a new HTTP client that satisfies the rpcinterface.Client interface
Expand All @@ -60,6 +64,7 @@ func NewHTTPClient(cfg *config.ChiaConfig, options ...rpcinterface.ClientOptionF
harvesterPort: cfg.Harvester.RPCPort,
walletPort: cfg.Wallet.RPCPort,
crawlerPort: cfg.Seeder.CrawlerConfig.RPCPort,
datalayerPort: cfg.DataLayer.RPCPort,
}

// Sets the default host. Can be overridden by client options
Expand Down Expand Up @@ -226,6 +231,14 @@ func (c *HTTPClient) generateHTTPClientForService(service rpcinterface.ServiceTy
}
}
keyPair = c.crawlerKeyPair
case rpcinterface.ServiceDataLayer:
if c.datalayerKeyPair == nil {
c.datalayerKeyPair, err = c.config.DataLayer.SSL.LoadPrivateKeyPair(c.config.ChiaRoot)
if err != nil {
return nil, err
}
}
keyPair = c.datalayerKeyPair
default:
return nil, fmt.Errorf("unknown service")
}
Expand Down Expand Up @@ -266,6 +279,8 @@ func (c *HTTPClient) portForService(service rpcinterface.ServiceType) uint16 {
port = c.walletPort
case rpcinterface.ServiceCrawler:
port = c.crawlerPort
case rpcinterface.ServiceDataLayer:
port = c.datalayerPort
}

return port
Expand Down Expand Up @@ -319,6 +334,14 @@ func (c *HTTPClient) httpClientForService(service rpcinterface.ServiceType) (*ht
}
}
client = c.crawlerClient
case rpcinterface.ServiceDataLayer:
if c.datalayerClient == nil {
c.datalayerClient, err = c.generateHTTPClientForService(rpcinterface.ServiceDataLayer)
if err != nil {
return nil, err
}
}
client = c.datalayerClient
}

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 @@ -23,6 +23,7 @@ type Client struct {
FarmerService *FarmerService
HarvesterService *HarvesterService
CrawlerService *CrawlerService
DataLayerService *DataLayerService

websocketHandlers []rpcinterface.WebsocketResponseHandler
}
Expand Down Expand Up @@ -67,6 +68,7 @@ func NewClient(connectionMode ConnectionMode, configOption rpcinterface.ConfigOp
c.FarmerService = &FarmerService{client: c}
c.HarvesterService = &HarvesterService{client: c}
c.CrawlerService = &CrawlerService{client: c}
c.DataLayerService = &DataLayerService{client: c}

return c, nil
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func setup(t *testing.T) (*http.ServeMux, *httptest.Server, *Client) {
ChiaRoot: tmpDir,
DaemonPort: portConf.RPCPort,
DaemonSSL: sslConf,
FullNode: config.FullNodeConfig{
Farmer: config.FarmerConfig{
PortConfig: portConf,
SSL: sslConf,
},
Farmer: config.FarmerConfig{
FullNode: config.FullNodeConfig{
PortConfig: portConf,
SSL: sslConf,
},
Expand All @@ -86,6 +86,10 @@ func setup(t *testing.T) (*http.ServeMux, *httptest.Server, *Client) {
SSL: sslConf,
},
},
DataLayer: config.DataLayerConfig{
PortConfig: portConf,
SSL: sslConf,
},
}))
if err != nil {
t.Fatal(err)
Expand Down
22 changes: 22 additions & 0 deletions pkg/rpc/datalayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package rpc

import (
"net/http"

"github.com/chia-network/go-chia-libs/pkg/rpcinterface"
)

// DataLayerService encapsulates data layer RPC methods
type DataLayerService struct {
client *Client
}

// NewRequest returns a new request specific to the wallet service
func (s *DataLayerService) NewRequest(rpcEndpoint rpcinterface.Endpoint, opt interface{}) (*rpcinterface.Request, error) {
return s.client.NewRequest(rpcinterface.ServiceDataLayer, rpcEndpoint, opt)
}

// Do is just a shortcut to the client's Do method
func (s *DataLayerService) Do(req *rpcinterface.Request, v interface{}) (*http.Response, error) {
return s.client.Do(req, v)
}
3 changes: 3 additions & 0 deletions pkg/rpcinterface/servicetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ const (

// ServiceCrawler crawler service
ServiceCrawler

// ServiceDataLayer datalayer service
ServiceDataLayer
)