-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
156 lines (128 loc) · 3.87 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package jsonrpchttp
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/Azure/go-autorest/autorest"
"github.com/sirupsen/logrus"
kilnhttp "github.com/kilnfi/go-utils/net/http"
httppreparer "github.com/kilnfi/go-utils/net/http/preparer"
"github.com/kilnfi/go-utils/net/jsonrpc"
)
// Client allows to connect to a JSON-RPC server
type Client struct {
client autorest.Sender
logger logrus.FieldLogger
}
// NewClient creates a new client connected to a JSON-RPC server
func NewClientFromClient(s autorest.Sender) *Client {
c := &Client{
client: s,
}
c.SetLogger(logrus.StandardLogger())
return c
}
// NewClient creates a client connecting to an Ethereum 2.0 Beacon chain node at given addr
func NewClient(cfg *Config) (*Client, error) {
httpc, err := kilnhttp.NewClient(cfg.HTTP)
if err != nil {
return nil, err
}
return NewClientFromClient(
autorest.Client{
Sender: httpc,
RequestInspector: httppreparer.WithBaseURL(cfg.Address),
},
), nil
}
func (c *Client) Logger() logrus.FieldLogger {
return c.logger
}
func (c *Client) SetLogger(logger logrus.FieldLogger) {
c.logger = logger.WithField("component", "jsonrpc.http-client")
}
func (c *Client) Call(ctx context.Context, r *jsonrpc.Request, res interface{}) error {
err := c.call(ctx, r, res)
if err != nil {
c.logger.
WithField("req.method", r.Method).
WithField("req.version", r.Version).
WithField("req.params", r.Params).
WithField("req.id", r.ID).
WithError(err).Errorf("jsonrpc call failed")
}
return err
}
// Call performs JSON-RPC call
func (c *Client) call(ctx context.Context, r *jsonrpc.Request, res interface{}) error {
req, err := newCallRequest(ctx, r)
if err != nil {
return autorest.NewErrorWithError(err, "jsonrpchttp.Client", "Call", nil, "Request")
}
resp, err := c.client.Do(req)
if err != nil {
msg, _ := json.Marshal(r)
return autorest.NewErrorWithError(err, "jsonrpchttp.Client", fmt.Sprintf("Call(%v)", string(msg)), resp, "Do")
}
err = inspectCallResponse(resp, res)
if err != nil {
msg, _ := json.Marshal(r)
return autorest.NewErrorWithError(err, "jsonrpchttp.Client", fmt.Sprintf("Call(%v)", string(msg)), resp, "Response")
}
return nil
}
// ByUnmarshallingResponse marshall JSON-RPC request message into http.Request body
func newCallRequest(ctx context.Context, req *jsonrpc.Request) (*http.Request, error) {
return autorest.CreatePreparer(
autorest.AsPost(),
autorest.WithPath("/"),
autorest.AsJSON(),
autorest.WithJSON(req),
).Prepare(newRequest(ctx))
}
func newRequest(ctx context.Context) *http.Request {
req, _ := http.NewRequestWithContext(ctx, "", "", http.NoBody)
return req
}
// responseMsg is a struct allowing to encode/decode a JSON-RPC response body
type responseMsg struct {
Version string `json:"jsonrpc"`
Result *json.RawMessage `json:"result,omitempty"`
Error *json.RawMessage `json:"error,omitempty"`
ID *json.RawMessage `json:"id,omitempty"`
}
func inspectCallResponseMsg(msg *responseMsg, res interface{}) error {
if msg.Error == nil && msg.Result == nil {
return fmt.Errorf("invalid JSON-RPC response missing both result and error")
}
if msg.Error != nil {
errMsg := new(jsonrpc.ErrorMsg)
err := json.Unmarshal(*msg.Error, errMsg)
if err != nil {
return fmt.Errorf("invalid JSON-RPC error message %v", string(*msg.Error))
}
return errMsg
}
if msg.Result != nil && res != nil {
err := json.Unmarshal(*msg.Result, res)
if err != nil {
return fmt.Errorf("failed to unmarshal JSON-RPC result %v into %T (%v)", string(*msg.Result), res, err)
}
return nil
}
return nil
}
func inspectCallResponse(resp *http.Response, res interface{}) error {
msg := new(responseMsg)
err := autorest.Respond(
resp,
autorest.WithErrorUnlessOK(),
autorest.ByUnmarshallingJSON(msg),
autorest.ByClosing(),
)
if err != nil {
return err
}
return inspectCallResponseMsg(msg, res)
}