-
Notifications
You must be signed in to change notification settings - Fork 79
/
client.go
212 lines (190 loc) · 5.62 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"time"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
"github.com/nspcc-dev/neo-go/pkg/util"
)
const (
defaultDialTimeout = 4 * time.Second
defaultRequestTimeout = 4 * time.Second
// Number of blocks after which cache is expired.
cacheTimeout = 100
)
// Client represents the middleman for executing JSON RPC calls
// to remote NEO RPC nodes.
type Client struct {
cli *http.Client
endpoint *url.URL
network netmode.Magic
stateRootInHeader bool
initDone bool
ctx context.Context
opts Options
requestF func(*request.Raw) (*response.Raw, error)
cache cache
}
// Options defines options for the RPC client.
// All values are optional. If any duration is not specified
// a default of 4 seconds will be used.
type Options struct {
// Cert is a client-side certificate, it doesn't work at the moment along
// with the other two options below.
Cert string
Key string
CACert string
DialTimeout time.Duration
RequestTimeout time.Duration
// Limit total number of connections per host. No limit by default.
MaxConnsPerHost int
}
// cache stores cache values for the RPC client methods.
type cache struct {
calculateValidUntilBlock calculateValidUntilBlockCache
nativeHashes map[string]util.Uint160
}
// calculateValidUntilBlockCache stores cached number of validators and
// cache expiration value in blocks.
type calculateValidUntilBlockCache struct {
validatorsCount uint32
expiresAt uint32
}
// New returns a new Client ready to use. You should call Init method to
// initialize network magic the client is operating on.
func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
url, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
if opts.DialTimeout <= 0 {
opts.DialTimeout = defaultDialTimeout
}
if opts.RequestTimeout <= 0 {
opts.RequestTimeout = defaultRequestTimeout
}
httpClient := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: opts.DialTimeout,
}).DialContext,
MaxConnsPerHost: opts.MaxConnsPerHost,
},
Timeout: opts.RequestTimeout,
}
// TODO(@antdm): Enable SSL.
// if opts.Cert != "" && opts.Key != "" {
// }
cl := &Client{
ctx: ctx,
cli: httpClient,
endpoint: url,
cache: cache{
nativeHashes: make(map[string]util.Uint160),
},
}
cl.opts = opts
cl.requestF = cl.makeHTTPRequest
return cl, nil
}
// Init sets magic of the network client connected to, stateRootInHeader option
// and native NEO, GAS and Policy contracts scripthashes. This method should be
// called before any transaction-, header- or block-related requests in order to
// deserialize responses properly.
func (c *Client) Init() error {
version, err := c.GetVersion()
if err != nil {
return fmt.Errorf("failed to get network magic: %w", err)
}
c.network = version.Protocol.Network
c.stateRootInHeader = version.Protocol.StateRootInHeader
if version.Protocol.MillisecondsPerBlock == 0 {
c.network = version.Magic
c.stateRootInHeader = version.StateRootInHeader
}
neoContractHash, err := c.GetContractStateByAddressOrName(nativenames.Neo)
if err != nil {
return fmt.Errorf("failed to get NEO contract scripthash: %w", err)
}
c.cache.nativeHashes[nativenames.Neo] = neoContractHash.Hash
gasContractHash, err := c.GetContractStateByAddressOrName(nativenames.Gas)
if err != nil {
return fmt.Errorf("failed to get GAS contract scripthash: %w", err)
}
c.cache.nativeHashes[nativenames.Gas] = gasContractHash.Hash
policyContractHash, err := c.GetContractStateByAddressOrName(nativenames.Policy)
if err != nil {
return fmt.Errorf("failed to get Policy contract scripthash: %w", err)
}
c.cache.nativeHashes[nativenames.Policy] = policyContractHash.Hash
c.initDone = true
return nil
}
func (c *Client) performRequest(method string, p request.RawParams, v interface{}) error {
var r = request.Raw{
JSONRPC: request.JSONRPCVersion,
Method: method,
RawParams: p.Values,
ID: 1,
}
raw, err := c.requestF(&r)
if raw != nil && raw.Error != nil {
return raw.Error
} else if err != nil {
return err
} else if raw == nil || raw.Result == nil {
return errors.New("no result returned")
}
return json.Unmarshal(raw.Result, v)
}
func (c *Client) makeHTTPRequest(r *request.Raw) (*response.Raw, error) {
var (
buf = new(bytes.Buffer)
raw = new(response.Raw)
)
if err := json.NewEncoder(buf).Encode(r); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", c.endpoint.String(), buf)
if err != nil {
return nil, err
}
resp, err := c.cli.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// The node might send us proper JSON anyway, so look there first and if
// it parses, then it has more relevant data than HTTP error code.
err = json.NewDecoder(resp.Body).Decode(raw)
if err != nil {
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("HTTP %d/%s", resp.StatusCode, http.StatusText(resp.StatusCode))
} else {
err = fmt.Errorf("JSON decoding: %w", err)
}
}
if err != nil {
return nil, err
}
return raw, nil
}
// Ping attempts to create a connection to the endpoint.
// and returns an error if there is one.
func (c *Client) Ping() error {
conn, err := net.DialTimeout("tcp", c.endpoint.Host, defaultDialTimeout)
if err != nil {
return err
}
_ = conn.Close()
return nil
}