-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathclient_test.go
278 lines (225 loc) · 8.97 KB
/
client_test.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package client_test
import (
"context"
"github.com/brianvoe/gofakeit/v6"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/w3types"
. "github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/core/metrics/instrumentation/httpcapture"
"github.com/synapsecns/sanguine/ethergo/client"
"github.com/synapsecns/sanguine/ethergo/mocks"
"github.com/synapsecns/sanguine/ethergo/parser/rpc"
_ "go.opentelemetry.io/otel/sdk/trace/tracetest"
"io"
"math/big"
"net/http"
"net/http/httptest"
)
// TestEVM is an EVM that can be used for testing.
type TestEVM interface {
client.EVM
GetMetrics() metrics.Handler
}
func (c *ClientSuite) TestParseCalls() {
calls := make([]w3types.Caller, 4)
chainID := new(uint64)
calls[0] = eth.ChainID().Returns(chainID)
maxHeight := new(big.Int)
calls[1] = eth.BlockNumber().Returns(maxHeight)
var mockHash common.Hash
calls[2] = eth.StorageAt(mocks.MockAddress(), mocks.NewMockHash(c.T()), big.NewInt(1)).Returns(&mockHash)
filter := ethereum.FilterQuery{
Addresses: []common.Address{mocks.MockAddress()},
}
calls[3] = eth.Logs(filter).Returns(nil)
res := client.ParseCalls(calls)
Equal(c.T(), res.Value.AsStringSlice(), []string{"eth_chainId", "eth_blockNumber", "eth_getStorageAt", "eth_getLogs"})
}
// checkRequest is a helper method for checking requests.
func (c *ClientSuite) checkRequest(makeReq func(client TestEVM)) {
ctx, cancel := context.WithCancel(c.GetTestContext())
defer cancel()
doneChan := make(chan bool)
mockTracer := metrics.NewTestTracer(ctx, c.T())
var rpcRequest rpc.Requests
var body []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
body, err = io.ReadAll(r.Body)
c.Require().NoError(err, "failed to read request body")
rpcRequest, err = rpc.ParseRPCPayload(body)
c.Require().NoError(err, "failed to parse rpc payload")
go func() {
doneChan <- true
}()
}))
defer server.Close()
evmClient, err := client.DialBackend(ctx, server.URL, mockTracer, client.Capture(true))
c.Require().NoError(err)
castClient, ok := evmClient.(TestEVM)
c.Require().True(ok, "failed to cast client to TestEVM")
makeReq(castClient)
<-doneChan
spans := mockTracer.GetSpansByName(rpcRequest.Method())
requestSpans := mockTracer.GetSpansByName(httpcapture.RequestSpanName)
// make sure we got at most 1 span
c.Require().Equal(len(spans), 1, "expected 1 span, got %d", len(spans))
span := spans[0]
c.Require().Equal(len(requestSpans), 1, "expected 1 request span, got %d", len(spans))
requestSpan := requestSpans[0]
// make sure the span has an exception
c.Require().True(metrics.SpanHasException(span), "expected exception event, got none")
Equal(c.T(), metrics.SpanAttributeByName(span, "endpoint").AsString(), server.URL)
Equal(c.T(), metrics.SpanEventByName(requestSpan, httpcapture.RequestEventName).AsString(), string(body))
}
// Test parsing.
//
//nolint:maintidx
func (c *ClientSuite) TestParseRPCPayload() {
/*
CHECK BLOCKS
*/
// check latest block, should not be confirmable since
// rpcs might be on different latest heights
c.checkRequest(func(client TestEVM) {
_, _ = client.BlockByNumber(c.GetTestContext(), nil)
})
// check non-latest block, should be confirmable
c.checkRequest(func(client TestEVM) {
_, _ = client.BlockByNumber(c.GetTestContext(), new(big.Int).SetUint64(gofakeit.Uint64()))
})
/*
CHECK HEADERS
*/
// check non-latest block, should be confirmable
c.checkRequest(func(client TestEVM) {
_, _ = client.HeaderByNumber(c.GetTestContext(), new(big.Int).SetUint64(gofakeit.Uint64()))
})
// eth_blockNumber should not be confirmable, can differ based on rpc
c.checkRequest(func(client TestEVM) {
_, _ = client.BlockNumber(c.GetTestContext())
})
/*
CHECK Transaction Methods
*/
c.checkRequest(func(client TestEVM) {
_, _, _ = client.TransactionByHash(c.GetTestContext(), common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64())))
})
c.checkRequest(func(client TestEVM) {
_, _ = client.TransactionCount(c.GetTestContext(), common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64())))
})
c.checkRequest(func(client TestEVM) {
_, _ = client.PendingTransactionCount(c.GetTestContext())
})
c.checkRequest(func(client TestEVM) {
_, _ = client.TransactionInBlock(c.GetTestContext(), common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64())), 1)
})
c.checkRequest(func(client TestEVM) {
_, _ = client.TransactionReceipt(c.GetTestContext(), common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64())))
})
/*
Sync Methods
*/
c.checkRequest(func(client TestEVM) {
_, _ = client.SyncProgress(c.GetTestContext())
})
c.checkRequest(func(client TestEVM) {
_, _ = client.NetworkID(c.GetTestContext())
})
/*
Accessor Methods
*/
// latest block, should not be confirmable
c.checkRequest(func(client TestEVM) {
_, _ = client.BalanceAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), nil)
})
// pending
c.checkRequest(func(client TestEVM) {
_, _ = client.PendingBalanceAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())))
})
// non-latest block
c.checkRequest(func(client TestEVM) {
_, _ = client.BalanceAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), new(big.Int).SetUint64(gofakeit.Uint64()))
})
// latest block, should not be confirmable
c.checkRequest(func(client TestEVM) {
_, _ = client.StorageAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64())), nil)
})
c.checkRequest(func(client TestEVM) {
_, _ = client.PendingStorageAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64())))
})
// non-latest block
c.checkRequest(func(client TestEVM) {
_, _ = client.StorageAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64())), new(big.Int).SetUint64(gofakeit.Uint64()))
})
c.checkRequest(func(client TestEVM) {
_, _ = client.CodeAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), nil)
})
c.checkRequest(func(client TestEVM) {
_, _ = client.PendingCodeAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())))
})
// non-latest block
c.checkRequest(func(client TestEVM) {
_, _ = client.CodeAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), new(big.Int).SetUint64(gofakeit.Uint64()))
})
// storage:
c.checkRequest(func(client TestEVM) {
_, _ = client.NonceAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), nil)
})
c.checkRequest(func(client TestEVM) {
_, _ = client.PendingNonceAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())))
})
// non-latest block
c.checkRequest(func(client TestEVM) {
_, _ = client.NonceAt(c.GetTestContext(), common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64())), new(big.Int).SetUint64(gofakeit.Uint64()))
})
c.checkRequest(func(client TestEVM) {
_, _ = client.FilterLogs(c.GetTestContext(), ethereum.FilterQuery{
FromBlock: nil,
ToBlock: nil,
Addresses: []common.Address{common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64()))},
Topics: [][]common.Hash{{common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64()))}},
})
})
// set only block hash
c.checkRequest(func(client TestEVM) {
bhash := common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64()))
_, _ = client.FilterLogs(c.GetTestContext(), ethereum.FilterQuery{
BlockHash: &bhash,
Addresses: []common.Address{common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64()))},
Topics: [][]common.Hash{{common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64()))}},
})
})
/*
Call Contract
*/
c.checkRequest(func(client TestEVM) {
_, _ = client.CallContract(c.GetTestContext(), ethereum.CallMsg{}, nil)
})
c.checkRequest(func(client TestEVM) {
_, _ = client.CallContract(c.GetTestContext(), ethereum.CallMsg{}, big.NewInt(1))
})
c.checkRequest(func(client TestEVM) {
_, _ = client.PendingCallContract(c.GetTestContext(), ethereum.CallMsg{})
})
/*
Gas Pricing
*/
c.checkRequest(func(client TestEVM) {
_, _ = client.SuggestGasPrice(c.GetTestContext())
})
c.checkRequest(func(client TestEVM) {
_, _ = client.SuggestGasTipCap(c.GetTestContext())
})
c.checkRequest(func(client TestEVM) {
_, _ = client.EstimateGas(c.GetTestContext(), ethereum.CallMsg{})
})
// note: single batch requests will fail checkRequest since it is a helper function that expects more than one call, this could be checked manually
// if so desired
c.checkRequest(func(client TestEVM) {
_ = client.BatchWithContext(c.GetTestContext(), eth.ChainID().Returns(nil), eth.ChainID().Returns(nil))
})
}