-
Notifications
You must be signed in to change notification settings - Fork 8
/
ethereum.go
428 lines (367 loc) · 10.7 KB
/
ethereum.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// xk6 build --with github.com/distribworks/xk6-ethereum=.
package ethereum
import (
"encoding/hex"
"fmt"
"math/big"
"strconv"
"sync"
"time"
"github.com/grafana/sobek"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/abi"
"github.com/umbracle/ethgo/contract"
"github.com/umbracle/ethgo/jsonrpc"
"github.com/umbracle/ethgo/wallet"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/metrics"
)
type Transaction struct {
From string
To string
Input []byte
GasPrice uint64
GasFeeCap uint64
GasTipCap uint64
Gas uint64
Value int64
Nonce uint64
// eip-2930 values
ChainId int64
}
type Client struct {
w *wallet.Key
client *jsonrpc.Client
chainID *big.Int
vu modules.VU
metrics ethMetrics
opts *options
}
func (c *Client) Exports() modules.Exports {
return modules.Exports{}
}
func (c *Client) Call(method string, params ...interface{}) (interface{}, error) {
t := time.Now()
var out interface{}
err := c.client.Call(method, &out, params...)
c.reportMetricsFromStats(method, time.Since(t))
return out, err
}
func (c *Client) GasPrice() (uint64, error) {
t := time.Now()
g, err := c.client.Eth().GasPrice()
c.reportMetricsFromStats("gas_price", time.Since(t))
return g, err
}
func (c *Client) GetBalance(address string, blockNumber ethgo.BlockNumber) (uint64, error) {
b, err := c.client.Eth().GetBalance(ethgo.HexToAddress(address), blockNumber)
return b.Uint64(), err
}
// BlockNumber returns the current block number.
func (c *Client) BlockNumber() (uint64, error) {
return c.client.Eth().BlockNumber()
}
// GetBlockByNumber returns the block with the given block number.
func (c *Client) GetBlockByNumber(number ethgo.BlockNumber, full bool) (*ethgo.Block, error) {
return c.client.Eth().GetBlockByNumber(number, full)
}
// GetNonce returns the nonce for the given address.
func (c *Client) GetNonce(address string) (uint64, error) {
return c.client.Eth().GetNonce(ethgo.HexToAddress(address), ethgo.Pending)
}
// EstimateGas returns the estimated gas for the given transaction.
func (c *Client) EstimateGas(tx Transaction) (uint64, error) {
to := ethgo.HexToAddress(tx.To)
msg := ðgo.CallMsg{
From: c.w.Address(),
To: &to,
Value: big.NewInt(tx.Value),
Data: tx.Input,
GasPrice: tx.GasPrice,
}
gas, err := c.client.Eth().EstimateGas(msg)
if err != nil {
return 0, fmt.Errorf("failed to estimate gas: %e", err)
}
return gas, nil
}
// SendTransaction sends a transaction to the network.
func (c *Client) SendTransaction(tx Transaction) (string, error) {
to := ethgo.HexToAddress(tx.To)
if tx.Gas == 0 {
tx.Gas = 21000
}
if tx.GasPrice == 0 && tx.GasFeeCap == 0 && tx.GasTipCap == 0 {
tx.GasPrice = 5242880
}
t := ðgo.Transaction{
Type: ethgo.TransactionLegacy,
From: ethgo.HexToAddress(tx.From),
To: &to,
Value: big.NewInt(tx.Value),
Gas: tx.Gas,
GasPrice: tx.GasPrice,
}
if tx.GasFeeCap > 0 || tx.GasTipCap > 0 {
t.Type = ethgo.TransactionDynamicFee
t.GasPrice = 0
t.MaxFeePerGas = big.NewInt(0).SetUint64(tx.GasFeeCap)
t.MaxPriorityFeePerGas = big.NewInt(0).SetUint64(tx.GasTipCap)
}
h, err := c.client.Eth().SendTransaction(t)
return h.String(), err
}
// SendRawTransaction signs and sends transaction to the network.
func (c *Client) SendRawTransaction(tx Transaction) (string, error) {
to := ethgo.HexToAddress(tx.To)
gas, err := c.EstimateGas(tx)
if err != nil {
return "", err
}
t := ðgo.Transaction{
Type: ethgo.TransactionLegacy,
From: c.w.Address(),
To: &to,
Value: big.NewInt(tx.Value),
Gas: gas,
GasPrice: tx.GasPrice,
Nonce: tx.Nonce,
Input: tx.Input,
ChainID: c.chainID,
}
if tx.GasFeeCap > 0 || tx.GasTipCap > 0 {
t.Type = ethgo.TransactionDynamicFee
t.GasPrice = 0
t.MaxFeePerGas = big.NewInt(0).SetUint64(tx.GasFeeCap)
t.MaxPriorityFeePerGas = big.NewInt(0).SetUint64(tx.GasTipCap)
}
s := wallet.NewEIP155Signer(t.ChainID.Uint64())
st, err := s.SignTx(t, c.w)
if err != nil {
return "", err
}
trlp, err := st.MarshalRLPTo(nil)
if err != nil {
return "", fmt.Errorf("failed to marshal tx: %e", err)
}
h, err := c.client.Eth().SendRawTransaction(trlp)
return h.String(), err
}
// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
func (c *Client) GetTransactionReceipt(hash string) (*ethgo.Receipt, error) {
r, err := c.client.Eth().GetTransactionReceipt(ethgo.HexToHash(hash))
if err != nil {
return nil, err
}
if r != nil {
return r, nil
}
return nil, fmt.Errorf("not found")
}
// WaitForTransactionReceipt waits for the transaction receipt for the given transaction hash.
func (c *Client) WaitForTransactionReceipt(hash string) *sobek.Promise {
promise, resolve, reject := c.makeHandledPromise()
now := time.Now()
go func() {
for {
receipt, err := c.GetTransactionReceipt(hash)
if err != nil {
if err.Error() != "not found" {
reject(err)
return
}
}
if receipt != nil {
// If we are testing vu is nil
if c.vu != nil {
// Report metrics
metrics.PushIfNotDone(c.vu.Context(), c.vu.State().Samples, metrics.Sample{
TimeSeries: metrics.TimeSeries{
Metric: c.metrics.TimeToMine,
Tags: metrics.NewRegistry().RootTagSet(),
},
Value: float64(time.Since(now) / time.Millisecond),
Time: time.Now(),
})
}
resolve(receipt)
break
}
time.Sleep(100 * time.Millisecond)
}
}()
return promise
}
// Accounts returns a list of addresses owned by client. This endpoint is not enabled in infrastructure providers.
func (c *Client) Accounts() ([]string, error) {
accounts, err := c.client.Eth().Accounts()
if err != nil {
return nil, err
}
addresses := make([]string, len(accounts))
for i, a := range accounts {
addresses[i] = a.String()
}
return addresses, nil
}
// NewContract creates a new contract instance with the given ABI.
func (c *Client) NewContract(address string, abistr string) (*Contract, error) {
contractABI, err := abi.NewABI(abistr)
if err != nil {
return nil, fmt.Errorf("failed to parse abi: %w", err)
}
opts := []contract.ContractOption{
contract.WithJsonRPC(c.client.Eth()),
contract.WithSender(c.w),
}
contract := contract.NewContract(ethgo.HexToAddress(address), contractABI, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create contract: %w", err)
}
return &Contract{
Contract: contract,
client: c,
}, nil
}
// DeployContract deploys a contract to the blockchain.
func (c *Client) DeployContract(abistr string, bytecode string, args ...interface{}) (*ethgo.Receipt, error) {
// Parse ABI
contractABI, err := abi.NewABI(abistr)
if err != nil {
return nil, fmt.Errorf("failed to parse abi: %w", err)
}
// Parse bytecode
contractBytecode, err := hex.DecodeString(bytecode)
if err != nil {
return nil, fmt.Errorf("failed to decode bytecode: %w", err)
}
opts := []contract.ContractOption{
contract.WithJsonRPC(c.client.Eth()),
contract.WithSender(c.w),
}
// Deploy contract
txn, err := contract.DeployContract(contractABI, contractBytecode, args, opts...)
if err != nil {
return nil, fmt.Errorf("failed to deploy contract: %w", err)
}
txn.WithOpts(&contract.TxnOpts{
GasLimit: 1500000,
})
err = txn.Do()
if err != nil {
return nil, fmt.Errorf("failed to deploy contract: %w", err)
}
receipt, err := txn.Wait()
if err != nil {
return nil, fmt.Errorf("failed waiting to deploy contract: %w", err)
}
return receipt, nil
}
// makeHandledPromise will create a promise and return its resolve and reject methods,
// wrapped in such a way that it will block the eventloop from exiting before they are
// called even if the promise isn't resolved by the time the current script ends executing.
func (c *Client) makeHandledPromise() (*sobek.Promise, func(interface{}), func(interface{})) {
runtime := c.vu.Runtime()
callback := c.vu.RegisterCallback()
p, resolve, reject := runtime.NewPromise()
return p, func(i interface{}) {
// more stuff
callback(func() error {
resolve(i)
return nil
})
}, func(i interface{}) {
// more stuff
callback(func() error {
reject(i)
return nil
})
}
}
var blocks sync.Map
// PollBlocks polls for new blocks and emits a "block" metric.
func (c *Client) pollForBlocks() {
var lastBlockNumber uint64
var prevBlock *ethgo.Block
now := time.Now()
for range time.Tick(500 * time.Millisecond) {
blockNumber, err := c.BlockNumber()
if err != nil {
panic(err)
}
if blockNumber > lastBlockNumber {
// compute precise block time
blockTime := time.Since(now)
now = time.Now()
block, err := c.GetBlockByNumber(ethgo.BlockNumber(blockNumber), false)
if err != nil {
panic(err)
}
if block == nil {
// We're not going to continue past this point if we don't have a block
continue
}
lastBlockNumber = blockNumber
var blockTimestampDiff time.Duration
var tps float64
if prevBlock != nil {
// compute block time
blockTimestampDiff = time.Unix(int64(block.Timestamp), 0).Sub(time.Unix(int64(prevBlock.Timestamp), 0))
// Compute TPS
tps = float64(len(block.TransactionsHashes)) / float64(blockTimestampDiff.Seconds())
}
prevBlock = block
rootTS := metrics.NewRegistry().RootTagSet()
if c.vu != nil || c.vu.State() != nil || rootTS != nil {
if _, loaded := blocks.LoadOrStore(c.opts.URL+strconv.FormatUint(blockNumber, 10), true); loaded {
// We already have a block number for this client, so we can skip this
continue
}
metrics.PushIfNotDone(c.vu.Context(), c.vu.State().Samples, metrics.ConnectedSamples{
Samples: []metrics.Sample{
{
TimeSeries: metrics.TimeSeries{
Metric: c.metrics.Block,
Tags: rootTS.WithTagsFromMap(map[string]string{
"transactions": strconv.Itoa(len(block.TransactionsHashes)),
"gas_used": strconv.Itoa(int(block.GasUsed)),
"gas_limit": strconv.Itoa(int(block.GasLimit)),
}),
},
Value: float64(blockNumber),
Time: time.Now(),
},
{
TimeSeries: metrics.TimeSeries{
Metric: c.metrics.GasUsed,
Tags: rootTS.WithTagsFromMap(map[string]string{
"block": strconv.Itoa(int(blockNumber)),
}),
},
Value: float64(block.GasUsed),
Time: time.Now(),
},
{
TimeSeries: metrics.TimeSeries{
Metric: c.metrics.TPS,
Tags: rootTS,
},
Value: tps,
Time: time.Now(),
},
{
TimeSeries: metrics.TimeSeries{
Metric: c.metrics.BlockTime,
Tags: rootTS.WithTagsFromMap(map[string]string{
"block_timestamp_diff": blockTimestampDiff.String(),
}),
},
Value: float64(blockTime.Milliseconds()),
Time: time.Now(),
},
},
})
}
}
}
}