Skip to content

Commit

Permalink
Merge pull request #16 from Chia-Network/harvester-get-plots
Browse files Browse the repository at this point in the history
Harvester get plots
  • Loading branch information
cmmarslender authored Jun 21, 2022
2 parents ac08e12 + 0d8a07d commit aa79063
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 23 deletions.
20 changes: 17 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ $(BIN)/%: | $(BIN) ; $(info $(M) building $(PACKAGE)…)
GOLINT = $(BIN)/golint
$(BIN)/golint: PACKAGE=golang.org/x/lint/golint

STATICCHECK = $(BIN)/staticcheck
$(BIN)/staticcheck: PACKAGE=honnef.co/go/tools/cmd/staticcheck

ERRCHECK = $(BIN)/errcheck
$(BIN)/errcheck: PACKAGE=github.com/kisielk/errcheck

GOCOV = $(BIN)/gocov
$(BIN)/gocov: PACKAGE=github.com/axw/gocov/...

Expand All @@ -54,10 +60,10 @@ test-verbose: ARGS=-v ## Run tests in verbose mode with coverage repo
test-race: ARGS=-race ## Run tests with race detector
$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%)
$(TEST_TARGETS): test
check test tests: fmt lint vet; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests
check test tests: fmt lint vet staticcheck errcheck; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests
$Q $(GO) test -timeout $(TIMEOUT)s $(ARGS) $(TESTPKGS)

test-xml: fmt lint vet | $(GO2XUNIT) ; $(info $(M) running xUnit tests…) @ ## Run tests with xUnit output
test-xml: fmt lint vet staticcheck errcheck | $(GO2XUNIT) ; $(info $(M) running xUnit tests…) @ ## Run tests with xUnit output
$Q mkdir -p test
$Q 2>&1 $(GO) test -timeout $(TIMEOUT)s -v $(TESTPKGS) | tee test/tests.output
$(GO2XUNIT) -fail -input test/tests.output -output test/tests.xml
Expand All @@ -69,7 +75,7 @@ COVERAGE_HTML = $(COVERAGE_DIR)/index.html
.PHONY: test-coverage test-coverage-tools
test-coverage-tools: | $(GOCOV) $(GOCOVXML)
test-coverage: COVERAGE_DIR := $(CURDIR)/test/coverage
test-coverage: fmt lint vet test-coverage-tools ; $(info $(M) running coverage tests…) @ ## Run coverage tests
test-coverage: fmt lint vet staticcheck errcheck test-coverage-tools ; $(info $(M) running coverage tests…) @ ## Run coverage tests
$Q mkdir -p $(COVERAGE_DIR)
$Q $(GO) test \
-coverpkg=$$($(GO) list -f '{{ join .Deps "\n" }}' $(TESTPKGS) | \
Expand All @@ -92,6 +98,14 @@ fmt: ; $(info $(M) running gofmt…) @ ## Run gofmt on all source files
vet: ; $(info $(M) running go vet…) @ ## Run go vet on all source files
$Q $(GO) vet $(PKGS)

.PHONY: staticcheck
staticcheck: | $(STATICCHECK) ; $(info $(M) running staticcheck…) @
$Q $(STATICCHECK) $(PKGS)

.PHONY: errcheck
errcheck: | $(ERRCHECK) ; $(info $(M) running errcheck…) @
$Q $(ERRCHECK) $(PKGS)

# Misc

.PHONY: clean
Expand Down
2 changes: 1 addition & 1 deletion pkg/httpclient/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *CachedTransport) RoundTrip(r *http.Request) (*http.Response, error) {
cacheKey := c.key(r)

// If the response is cached, we can just respond with the cached version
if cached, found := c.cache.Get(cacheKey); found != false {
if cached, found := c.cache.Get(cacheKey); found {
cachedBytes := cached.([]byte)
return c.cachedResponse(cachedBytes, r)
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rpc

import (
"log"
"net/http"

"github.com/chia-network/go-chia-libs/pkg/config"
Expand All @@ -17,9 +18,10 @@ type Client struct {
activeClient rpcinterface.Client

// Services for the different chia services
FullNodeService *FullNodeService
WalletService *WalletService
CrawlerService *CrawlerService
FullNodeService *FullNodeService
WalletService *WalletService
HarvesterService *HarvesterService
CrawlerService *CrawlerService

websocketHandlers []rpcinterface.WebsocketResponseHandler
}
Expand Down Expand Up @@ -61,6 +63,7 @@ func NewClient(connectionMode ConnectionMode, options ...rpcinterface.ClientOpti
// Init Services
c.FullNodeService = &FullNodeService{client: c}
c.WalletService = &WalletService{client: c}
c.HarvesterService = &HarvesterService{client: c}
c.CrawlerService = &CrawlerService{client: c}

return c, nil
Expand Down Expand Up @@ -97,7 +100,12 @@ func (c *Client) Subscribe(service string) error {
func (c *Client) AddHandler(handler rpcinterface.WebsocketResponseHandler) error {
c.websocketHandlers = append(c.websocketHandlers, handler)

go c.ListenSync(c.handlerProxy)
go func() {
err := c.ListenSync(c.handlerProxy)
if err != nil {
log.Printf("Error calling ListenSync: %s\n", err.Error())
}
}()
return nil
}

Expand Down
47 changes: 47 additions & 0 deletions pkg/rpc/harvester.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package rpc

import (
"net/http"

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

// HarvesterService encapsulates harvester RPC methods
type HarvesterService struct {
client *Client
}

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

// Do is just a shortcut to the client's Do method
func (s *HarvesterService) Do(req *rpcinterface.Request, v interface{}) (*http.Response, error) {
return s.client.Do(req, v)
}

// HarvesterGetPlotsResponse get_plots response format
type HarvesterGetPlotsResponse struct {
Success bool `json:"success"`
Plots []*types.PlotInfo `json:"plots"`
FailedToOpenFilenames []string `json:"failed_to_open_filenames"`
NotFoundFilenames []string `json:"not_found_filenames"`
}

// GetPlots returns connections
func (s *HarvesterService) GetPlots() (*HarvesterGetPlotsResponse, *http.Response, error) {
request, err := s.NewRequest("get_plots", nil)
if err != nil {
return nil, nil, err
}

p := &HarvesterGetPlotsResponse{}
resp, err := s.Do(request, p)
if err != nil {
return nil, resp, err
}

return p, resp, nil
}
12 changes: 12 additions & 0 deletions pkg/types/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ type EventHarvesterFarmingInfo struct {
EligiblePlots uint64 `json:"eligible_plots"`
Time float64 `json:"time"`
}

// PlotInfo contains information about a plot, as used in get_plots rpc
type PlotInfo struct {
FileSize uint64 `json:"file_size"`
Filename string `json:"filename"`
PlotID string `json:"plot_id"`
PlotPublicKey string `json:"plot_public_key"`
PoolContractPuzzleHash string `json:"pool_contract_puzzle_hash"`
PoolPublicKey string `json:"pool_public_key"`
Size uint8 `json:"size"`
TimeModified int `json:"time_modified"`
}
9 changes: 4 additions & 5 deletions pkg/types/uint128.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (u Uint128) And(v Uint128) Uint128 {

// And64 returns u&v.
func (u Uint128) And64(v uint64) Uint128 {
//lint:ignore SA4016 Don't want to change too much in this library
return Uint128{u.Lo & v, u.Hi & 0}
}

Expand All @@ -113,6 +114,7 @@ func (u Uint128) Or(v Uint128) Uint128 {

// Or64 returns u|v.
func (u Uint128) Or64(v uint64) Uint128 {
//lint:ignore SA4016 Don't want to change too much in this library
return Uint128{u.Lo | v, u.Hi | 0}
}

Expand All @@ -123,6 +125,7 @@ func (u Uint128) Xor(v Uint128) Uint128 {

// Xor64 returns u^v.
func (u Uint128) Xor64(v uint64) Uint128 {
//lint:ignore SA4016 Don't want to change too much in this library
return Uint128{u.Lo ^ v, u.Hi ^ 0}
}

Expand Down Expand Up @@ -467,11 +470,7 @@ func (u *Uint128) MarshalJSON() ([]byte, error) {

// FitsInUint64 returns true if the value of the Uint128 will fit in Uint64
func (u *Uint128) FitsInUint64() bool {
if u.Hi == 0 {
return true
}

return false
return u.Hi == 0
}

// Uint64 returns the Uint64 value of the Int
Expand Down
31 changes: 23 additions & 8 deletions pkg/types/uint128_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ import (
"github.com/chia-network/go-chia-libs/pkg/types"
)

func randUint128() types.Uint128 {
func randUint128(t *testing.T) types.Uint128 {
randBuf := make([]byte, 16)
rand.Read(randBuf)
_, err := rand.Read(randBuf)
if err != nil {
t.Fatal("Error reading random data")
}
return types.Uint128FromBytes(randBuf)
}

func TestUint128(t *testing.T) {
// test non-arithmetic methods
for i := 0; i < 1000; i++ {
x, y := randUint128(), randUint128()
x, y := randUint128(t), randUint128(t)
if i%3 == 0 {
x = x.Rsh(64)
} else if i%7 == 0 {
Expand Down Expand Up @@ -101,7 +104,10 @@ func TestArithmetic(t *testing.T) {
// random values
randBuf := make([]byte, 17)
randUint128 := func() types.Uint128 {
rand.Read(randBuf)
_, err := rand.Read(randBuf)
if err != nil {
t.Fatal("Error reading random data")
}
var Lo, Hi uint64
if randBuf[16]&1 != 0 {
Lo = binary.LittleEndian.Uint64(randBuf[:8])
Expand Down Expand Up @@ -311,7 +317,7 @@ func TestLeadingZeros(t *testing.T) {

func TestString(t *testing.T) {
for i := 0; i < 1000; i++ {
x := randUint128()
x := randUint128(t)
if x.String() != x.Big().String() {
t.Fatalf("mismatch:\n%v !=\n%v", x.String(), x.Big().String())
}
Expand All @@ -329,7 +335,10 @@ func TestString(t *testing.T) {
func BenchmarkArithmetic(b *testing.B) {
randBuf := make([]byte, 17)
randUint128 := func() types.Uint128 {
rand.Read(randBuf)
_, err := rand.Read(randBuf)
if err != nil {
b.Fatal("Error reading random data")
}
var Lo, Hi uint64
if randBuf[16]&1 != 0 {
Lo = binary.LittleEndian.Uint64(randBuf[:8])
Expand Down Expand Up @@ -387,7 +396,10 @@ func BenchmarkArithmetic(b *testing.B) {
func BenchmarkDivision(b *testing.B) {
randBuf := make([]byte, 8)
randU64 := func() uint64 {
rand.Read(randBuf)
_, err := rand.Read(randBuf)
if err != nil {
b.Fatal("Error reading random data")
}
return binary.LittleEndian.Uint64(randBuf) | 3 // avoid divide-by-zero
}
x64 := types.Uint128From64(randU64())
Expand Down Expand Up @@ -452,7 +464,10 @@ func BenchmarkDivision(b *testing.B) {

func BenchmarkString(b *testing.B) {
buf := make([]byte, 16)
rand.Read(buf)
_, err := rand.Read(buf)
if err != nil {
b.Fatal("Error reading random data")
}
x := types.NewUint128(
binary.LittleEndian.Uint64(buf[:8]),
binary.LittleEndian.Uint64(buf[8:]),
Expand Down
7 changes: 5 additions & 2 deletions pkg/websocketclient/websocketclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (c *WebsocketClient) doSubscribe(service string) error {
// Errors returned by ReadMessage, or some other part of the websocket request/response will be
// passed to the handler to deal with
func (c *WebsocketClient) ListenSync(handler rpcinterface.WebsocketResponseHandler) error {
if c.listenSyncActive != true {
if !c.listenSyncActive {
c.listenSyncActive = true

for {
Expand Down Expand Up @@ -209,7 +209,10 @@ func (c *WebsocketClient) reconnectLoop() {
if err == nil {
log.Println("Reconnected!")
for _, topic := range c.subscriptions {
c.doSubscribe(topic)
err = c.doSubscribe(topic)
if err != nil {
log.Printf("Error subscribing to topic %s: %s\n", topic, err.Error())
}
}
return
}
Expand Down

0 comments on commit aa79063

Please sign in to comment.