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

btcjson, rpcclient: revamp package and eliminate duplication #2014

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ retract (
v0.13.0-beta
)

go 1.17
go 1.18
47 changes: 47 additions & 0 deletions rpcclientv2/blochchaincmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package rpcclientv2

import (
"encoding/json"
)

type GetDifficultyRpcCmd struct{}

func (u GetDifficultyRpcCmd) Name() string {
return "getdifficulty"
}

func (u GetDifficultyRpcCmd) Marshal() (json.RawMessage, error) {
return json.Marshal(u)
}

type FutureGetDifficulty chan *Response

type GetDifficultyResult float64

func (r FutureGetDifficulty) Receive() (GetDifficultyResult, error) {
res, err := ReceiveFuture(r)
if err != nil {
return 0, err
}

// Unmarshal the result as a float64.
var difficulty float64
err = json.Unmarshal(res, &difficulty)
if err != nil {
return 0, err
}
return GetDifficultyResult(difficulty), nil
}

type GetDifficultyRpcCall struct {
}

func (u *GetDifficultyRpcCall) MapReq(arg string) GetDifficultyRpcCmd {
return GetDifficultyRpcCmd{}
}

func (u *GetDifficultyRpcCall) MapResp(resp chan *Response) MessageFuture[GetDifficultyResult] {

Check failure on line 43 in rpcclientv2/blochchaincmd.go

View workflow job for this annotation

GitHub Actions / Unit race

missing function body

Check failure on line 43 in rpcclientv2/blochchaincmd.go

View workflow job for this annotation

GitHub Actions / Unit race

syntax error: unexpected [ after top level declaration
f := FutureGetDifficulty(resp)

return &f
}
59 changes: 59 additions & 0 deletions rpcclientv2/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package rpcclientv2

import (
"encoding/json"
)

type Response struct {
Resp json.RawMessage
Err error
}

type Command interface {
Name() string
Marshal() (json.RawMessage, error)
}

type Client interface {
SendCmd(cmd Command) chan *Response
}

type MessageFuture[M any] interface {

Check failure on line 21 in rpcclientv2/request.go

View workflow job for this annotation

GitHub Actions / Unit race

syntax error: unexpected any, expecting ]
Receive() (M, error)
}

func ReceiveFuture(f chan *Response) (json.RawMessage, error) {
// Wait for a response on the returned channel.
r := <-f
return r.Resp, r.Err
}

type RpcCall[Args any, Cmd Command, Resp any] interface {

Check failure on line 31 in rpcclientv2/request.go

View workflow job for this annotation

GitHub Actions / Unit race

syntax error: unexpected any, expecting ]
MapReq(arg Args) Cmd

MapResp(resp chan *Response) MessageFuture[Resp]
}

func AsyncRequest[Args any, Cmd Command, Resp any](

Check failure on line 37 in rpcclientv2/request.go

View workflow job for this annotation

GitHub Actions / Unit race

missing function body

Check failure on line 37 in rpcclientv2/request.go

View workflow job for this annotation

GitHub Actions / Unit race

syntax error: unexpected [, expecting (
client Client, cmdReq RpcCall[Args, Cmd, Resp], args Args) MessageFuture[Resp] {

jsonCmd := cmdReq.MapReq(args)

resp := client.SendCmd(jsonCmd)

return cmdReq.MapResp(resp)
}

type FakeClient struct {
}

func (m FakeClient) SendCmd(cmd Command) chan *Response {
responseChan := make(chan *Response, 1)

result := Response{
Resp: []byte("561651.5"),
Err: nil,
}
responseChan <- &result
return responseChan
}