From 8e649e50388275a0a20a6bb9ef75d60a389d7ce3 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Mon, 21 Aug 2023 13:27:57 -0500 Subject: [PATCH] Add get_harvesters for the farmer, and consolidate the Plot definition to the protocol version --- pkg/protocols/harvester.go | 21 ++++++++++++++++ pkg/rpc/farmer.go | 51 ++++++++++++++++++++++++++++++++++++++ pkg/rpc/harvester.go | 4 +-- pkg/types/harvester.go | 27 ++++++++------------ 4 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 pkg/protocols/harvester.go diff --git a/pkg/protocols/harvester.go b/pkg/protocols/harvester.go new file mode 100644 index 0000000..47c00ce --- /dev/null +++ b/pkg/protocols/harvester.go @@ -0,0 +1,21 @@ +package protocols + +import ( + "github.com/samber/mo" + + "github.com/chia-network/go-chia-libs/pkg/types" +) + +// Plot is the plot definition in the harvester protocol +// https://github.com/Chia-Network/chia-blockchain/blob/main/chia/protocols/harvester_protocol.py#L78 +type Plot struct { + Filename string `json:"filename"` + Size uint8 `json:"size"` + PlotID types.Bytes32 `json:"plot_id"` + PoolPublicKey mo.Option[types.G1Element] `json:"pool_public_key"` + PoolContractPuzzleHash mo.Option[types.Bytes32] `json:"pool_contract_puzzle_hash"` + PlotPublicKey types.G1Element `json:"plot_public_key"` + FileSize uint64 `json:"file_size"` + TimeModified types.Timestamp `json:"time_modified"` + CompressionLevel mo.Option[uint8] `json:"compression_level"` +} diff --git a/pkg/rpc/farmer.go b/pkg/rpc/farmer.go index cc93fc4..425c8c2 100644 --- a/pkg/rpc/farmer.go +++ b/pkg/rpc/farmer.go @@ -3,7 +3,11 @@ package rpc import ( "net/http" + "github.com/samber/mo" + + "github.com/chia-network/go-chia-libs/pkg/protocols" "github.com/chia-network/go-chia-libs/pkg/rpcinterface" + "github.com/chia-network/go-chia-libs/pkg/types" ) // FarmerService encapsulates farmer RPC methods @@ -36,3 +40,50 @@ func (s *FarmerService) GetConnections(opts *GetConnectionsOptions) (*GetConnect return c, resp, nil } + +// FarmerGetHarvestersOptions optoins for get_harvesters endpoint. Currently, accepts no options +type FarmerGetHarvestersOptions struct{} + +// FarmerHarvester is a single harvester record returned by the farmer's get_harvesters endpoint +type FarmerHarvester struct { + Connection struct { + NodeID types.Bytes32 `json:"node_id"` + Host string `json:"host"` + Port uint16 `json:"port"` + } `json:"connection"` + Plots []protocols.Plot `json:"plots"` + FailedToOpenFilenames []string `json:"failed_to_open_filenames"` + NoKeyFilenames []string `json:"no_key_filenames"` + Duplicates []string `json:"duplicates"` + TotalPlotSize int `json:"total_plot_size"` + TotalEffectivePlotSize int `json:"total_effective_plot_size"` + Syncing mo.Option[struct { + Initial bool `json:"initial"` + PlotFilesProcessed uint32 `json:"plot_files_processed"` + PlotFilesTotal uint32 `json:"plot_files_total"` + }] `json:"syncing"` + LastSyncTime types.Timestamp `json:"last_sync_time"` + HarvestingMode mo.Option[types.HarvestingMode] `json:"harvesting_mode"` +} + +// FarmerGetHarvestersResponse get_harvesters response format +type FarmerGetHarvestersResponse struct { + Response + Harvesters []FarmerHarvester `json:"harvesters"` +} + +// GetHarvesters returns all harvester details for the farmer +func (s *FarmerService) GetHarvesters(opts *FarmerGetHarvestersOptions) (*FarmerGetHarvestersResponse, *http.Response, error) { + request, err := s.NewRequest("get_harvesters", opts) + if err != nil { + return nil, nil, err + } + + c := &FarmerGetHarvestersResponse{} + resp, err := s.Do(request, c) + if err != nil { + return nil, resp, err + } + + return c, resp, nil +} diff --git a/pkg/rpc/harvester.go b/pkg/rpc/harvester.go index 60af4ec..21559cc 100644 --- a/pkg/rpc/harvester.go +++ b/pkg/rpc/harvester.go @@ -5,8 +5,8 @@ import ( "github.com/samber/mo" + "github.com/chia-network/go-chia-libs/pkg/protocols" "github.com/chia-network/go-chia-libs/pkg/rpcinterface" - "github.com/chia-network/go-chia-libs/pkg/types" ) // HarvesterService encapsulates harvester RPC methods @@ -43,7 +43,7 @@ func (s *HarvesterService) GetConnections(opts *GetConnectionsOptions) (*GetConn // HarvesterGetPlotsResponse get_plots response format type HarvesterGetPlotsResponse struct { Response - Plots mo.Option[[]types.PlotInfo] `json:"plots"` + Plots mo.Option[[]protocols.Plot] `json:"plots"` FailedToOpenFilenames mo.Option[[]string] `json:"failed_to_open_filenames"` NotFoundFilenames mo.Option[[]string] `json:"not_found_filenames"` } diff --git a/pkg/types/harvester.go b/pkg/types/harvester.go index 4e6ac26..7edf496 100644 --- a/pkg/types/harvester.go +++ b/pkg/types/harvester.go @@ -1,9 +1,5 @@ package types -import ( - "github.com/samber/mo" -) - // EventHarvesterFarmingInfo is the event data for `farming_info` from the harvester // https://github.com/Chia-Network/chia-blockchain/blob/main/chia/harvester/harvester_api.py#L232 type EventHarvesterFarmingInfo struct { @@ -14,16 +10,13 @@ type EventHarvesterFarmingInfo struct { Time float64 `json:"time"` } -// PlotInfo contains information about a plot, as used in get_plots rpc -// There is also a PlotInfo type in chia, that is NOT used in the RPC, that has the first 5 fields as defined here -// https://github.com/Chia-Network/chia-blockchain/blob/main/chia/harvester/harvester.py#L139 -type PlotInfo struct { - PoolPublicKey mo.Option[G1Element] `json:"pool_public_key"` - PoolContractPuzzleHash mo.Option[Bytes32] `json:"pool_contract_puzzle_hash"` - PlotPublicKey G1Element `json:"plot_public_key"` - FileSize uint64 `json:"file_size"` - TimeModified Timestamp `json:"time_modified"` - Filename string `json:"filename"` - PlotID Bytes32 `json:"plot_id"` - Size uint8 `json:"size"` // https://github.com/Chia-Network/chiapos/blob/main/src/prover_disk.hpp#L181 -} +// HarvestingMode is the mode the harvester is using to harvest CPU or GPU +type HarvestingMode int + +const ( + // HarvestingModeCPU Using CPU to harvest + HarvestingModeCPU = HarvestingMode(1) + + // HarvestingModeGPU Using CPU to harvest + HarvestingModeGPU = HarvestingMode(2) +)