-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generic rpcclient example
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,4 @@ retract ( | |
v0.13.0-beta | ||
) | ||
|
||
go 1.17 | ||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Unit race
|
||
f := FutureGetDifficulty(resp) | ||
|
||
return &f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 { | ||
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 GitHub Actions / Unit race
|
||
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 | ||
} |