Skip to content

Commit

Permalink
Merge pull request #91 from hashgreen/feature/add-datalayer-connection
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Jun 14, 2023
2 parents cc6688c + ba778a3 commit a3e2fad
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
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
)

0 comments on commit a3e2fad

Please sign in to comment.