Skip to content

Commit

Permalink
feat: generic rpcclient example
Browse files Browse the repository at this point in the history
feat: generic rpcclient example
  • Loading branch information
Eoous committed Aug 15, 2023
1 parent f7bffa7 commit 5f65bd7
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
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
}

0 comments on commit 5f65bd7

Please sign in to comment.