/
actions.go
666 lines (612 loc) · 20.9 KB
/
actions.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Package actions enables common chainlink interactions
package actions
import (
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
"go.uber.org/zap/zapcore"
"github.com/smartcontractkit/chainlink-testing-framework/blockchain"
ctfClient "github.com/smartcontractkit/chainlink-testing-framework/client"
"github.com/smartcontractkit/chainlink-testing-framework/k8s/environment"
"github.com/smartcontractkit/chainlink-testing-framework/logging"
"github.com/smartcontractkit/chainlink-testing-framework/testreporters"
"github.com/smartcontractkit/chainlink-testing-framework/utils/conversions"
"github.com/smartcontractkit/chainlink-testing-framework/utils/testcontext"
"github.com/smartcontractkit/chainlink/integration-tests/client"
"github.com/smartcontractkit/chainlink/integration-tests/contracts"
"github.com/smartcontractkit/chainlink/integration-tests/docker/test_env"
)
// ContractDeploymentInterval After how many contract actions to wait before starting any more
// Example: When deploying 1000 contracts, stop every ContractDeploymentInterval have been deployed to wait before continuing
var ContractDeploymentInterval = 200
// FundChainlinkNodes will fund all of the provided Chainlink nodes with a set amountCreateOCRv2Jobs of native currency
// Deprecated: we are moving away from blockchain.EVMClient, use actions_seth.FundChainlinkNodes
func FundChainlinkNodes(
nodes []*client.ChainlinkK8sClient,
client blockchain.EVMClient,
amount *big.Float,
) error {
for _, cl := range nodes {
toAddress, err := cl.PrimaryEthAddress()
if err != nil {
return err
}
recipient := common.HexToAddress(toAddress)
msg := ethereum.CallMsg{
From: common.HexToAddress(client.GetDefaultWallet().Address()),
To: &recipient,
Value: conversions.EtherToWei(amount),
}
gasEstimates, err := client.EstimateGas(msg)
if err != nil {
return err
}
err = client.Fund(toAddress, amount, gasEstimates)
if err != nil {
return err
}
}
return client.WaitForEvents()
}
// FundChainlinkNodesAddress will fund all of the provided Chainlink nodes address at given index with a set amount of native currency
func FundChainlinkNodesAddress(
nodes []*client.ChainlinkK8sClient,
client blockchain.EVMClient,
amount *big.Float,
keyIndex int,
) error {
for _, cl := range nodes {
toAddress, err := cl.EthAddresses()
if err != nil {
return err
}
toAddr := common.HexToAddress(toAddress[keyIndex])
gasEstimates, err := client.EstimateGas(ethereum.CallMsg{
To: &toAddr,
})
if err != nil {
return err
}
err = client.Fund(toAddress[keyIndex], amount, gasEstimates)
if err != nil {
return err
}
}
return client.WaitForEvents()
}
// FundChainlinkNodesAddress will fund all of the provided Chainlink nodes addresses with a set amount of native currency
func FundChainlinkNodesAddresses(
nodes []*client.ChainlinkClient,
client blockchain.EVMClient,
amount *big.Float,
) error {
for _, cl := range nodes {
toAddress, err := cl.EthAddressesForChain(client.GetChainID().String())
if err != nil {
return err
}
for _, addr := range toAddress {
toAddr := common.HexToAddress(addr)
gasEstimates, err := client.EstimateGas(ethereum.CallMsg{
To: &toAddr,
})
if err != nil {
return err
}
err = client.Fund(addr, amount, gasEstimates)
if err != nil {
return err
}
}
}
return client.WaitForEvents()
}
// FundChainlinkNodes will fund all of the provided Chainlink nodes with a set amount of native currency
func FundChainlinkNodesLink(
nodes []*client.ChainlinkK8sClient,
blockchain blockchain.EVMClient,
linkToken contracts.LinkToken,
linkAmount *big.Int,
) error {
for _, cl := range nodes {
toAddress, err := cl.PrimaryEthAddress()
if err != nil {
return err
}
err = linkToken.Transfer(toAddress, linkAmount)
if err != nil {
return err
}
}
return blockchain.WaitForEvents()
}
// ChainlinkNodeAddresses will return all the on-chain wallet addresses for a set of Chainlink nodes
func ChainlinkNodeAddresses(nodes []*client.ChainlinkK8sClient) ([]common.Address, error) {
addresses := make([]common.Address, 0)
for _, node := range nodes {
primaryAddress, err := node.PrimaryEthAddress()
if err != nil {
return nil, err
}
addresses = append(addresses, common.HexToAddress(primaryAddress))
}
return addresses, nil
}
// ChainlinkNodeAddressesAtIndex will return all the on-chain wallet addresses for a set of Chainlink nodes
func ChainlinkNodeAddressesAtIndex(nodes []*client.ChainlinkK8sClient, keyIndex int) ([]common.Address, error) {
addresses := make([]common.Address, 0)
for _, node := range nodes {
nodeAddresses, err := node.EthAddresses()
if err != nil {
return nil, err
}
addresses = append(addresses, common.HexToAddress(nodeAddresses[keyIndex]))
}
return addresses, nil
}
// SetChainlinkAPIPageSize specifies the page size from the Chainlink API, useful for high volume testing
func SetChainlinkAPIPageSize(nodes []*client.ChainlinkK8sClient, pageSize int) {
for _, n := range nodes {
n.SetPageSize(pageSize)
}
}
// ExtractRequestIDFromJobRun extracts RequestID from job runs response
func ExtractRequestIDFromJobRun(jobDecodeData client.RunsResponseData) ([]byte, error) {
var taskRun client.TaskRun
for _, tr := range jobDecodeData.Attributes.TaskRuns {
if tr.Type == "ethabidecodelog" {
taskRun = tr
}
}
var decodeLogTaskRun *client.DecodeLogTaskRun
if err := json.Unmarshal([]byte(taskRun.Output), &decodeLogTaskRun); err != nil {
return nil, err
}
rqInts := decodeLogTaskRun.RequestID
return rqInts, nil
}
// EncodeOnChainVRFProvingKey encodes uncompressed public VRF key to on-chain representation
func EncodeOnChainVRFProvingKey(vrfKey client.VRFKey) ([2]*big.Int, error) {
uncompressed := vrfKey.Data.Attributes.Uncompressed
provingKey := [2]*big.Int{}
var set1 bool
var set2 bool
// strip 0x to convert to int
provingKey[0], set1 = new(big.Int).SetString(uncompressed[2:66], 16)
if !set1 {
return [2]*big.Int{}, fmt.Errorf("can not convert VRF key to *big.Int")
}
provingKey[1], set2 = new(big.Int).SetString(uncompressed[66:], 16)
if !set2 {
return [2]*big.Int{}, fmt.Errorf("can not convert VRF key to *big.Int")
}
return provingKey, nil
}
// GetMockserverInitializerDataForOTPE creates mocked weiwatchers data needed for otpe
func GetMockserverInitializerDataForOTPE(
OCRInstances []contracts.OffchainAggregator,
chainlinkNodes []*client.ChainlinkK8sClient,
) (interface{}, error) {
var contractsInfo []ctfClient.ContractInfoJSON
for index, OCRInstance := range OCRInstances {
contractInfo := ctfClient.ContractInfoJSON{
ContractVersion: 4,
Path: fmt.Sprintf("contract_%d", index),
Status: "live",
ContractAddress: OCRInstance.Address(),
}
contractsInfo = append(contractsInfo, contractInfo)
}
contractsInitializer := ctfClient.HttpInitializer{
Request: ctfClient.HttpRequest{Path: "/contracts.json"},
Response: ctfClient.HttpResponse{Body: contractsInfo},
}
var nodesInfo []ctfClient.NodeInfoJSON
for _, chainlink := range chainlinkNodes {
ocrKeys, err := chainlink.MustReadOCRKeys()
if err != nil {
return nil, err
}
nodeInfo := ctfClient.NodeInfoJSON{
NodeAddress: []string{ocrKeys.Data[0].Attributes.OnChainSigningAddress},
ID: ocrKeys.Data[0].ID,
}
nodesInfo = append(nodesInfo, nodeInfo)
}
nodesInitializer := ctfClient.HttpInitializer{
Request: ctfClient.HttpRequest{Path: "/nodes.json"},
Response: ctfClient.HttpResponse{Body: nodesInfo},
}
initializers := []ctfClient.HttpInitializer{contractsInitializer, nodesInitializer}
return initializers, nil
}
// TeardownSuite tears down networks/clients and environment and creates a logs folder for failed tests in the
// specified path. Can also accept a testreporter (if one was used) to log further results
func TeardownSuite(
t *testing.T,
env *environment.Environment,
chainlinkNodes []*client.ChainlinkK8sClient,
optionalTestReporter testreporters.TestReporter, // Optionally pass in a test reporter to log further metrics
failingLogLevel zapcore.Level, // Examines logs after the test, and fails the test if any Chainlink logs are found at or above provided level
grafnaUrlProvider testreporters.GrafanaURLProvider,
clients ...blockchain.EVMClient,
) error {
l := logging.GetTestLogger(t)
if err := testreporters.WriteTeardownLogs(t, env, optionalTestReporter, failingLogLevel, grafnaUrlProvider); err != nil {
return fmt.Errorf("Error dumping environment logs, leaving environment running for manual retrieval, err: %w", err)
}
// Delete all jobs to stop depleting the funds
err := DeleteAllJobs(chainlinkNodes)
if err != nil {
l.Warn().Msgf("Error deleting jobs %+v", err)
}
for _, c := range clients {
if c != nil && chainlinkNodes != nil && len(chainlinkNodes) > 0 {
if err := ReturnFunds(chainlinkNodes, c); err != nil {
// This printed line is required for tests that use real funds to propagate the failure
// out to the system running the test. Do not remove
fmt.Println(environment.FAILED_FUND_RETURN)
l.Error().Err(err).Str("Namespace", env.Cfg.Namespace).
Msg("Error attempting to return funds from chainlink nodes to network's default wallet. " +
"Environment is left running so you can try manually!")
}
} else {
l.Info().Msg("Successfully returned funds from chainlink nodes to default network wallets")
}
// nolint
if c != nil {
err := c.Close()
if err != nil {
return err
}
}
}
return env.Shutdown()
}
// TeardownRemoteSuite is used when running a test within a remote-test-runner, like for long-running performance and
// soak tests
// Deprecated: we are moving away from blockchain.EVMClient, use actions_seth.TeardownRemoteSuite
func TeardownRemoteSuite(
t *testing.T,
namespace string,
chainlinkNodes []*client.ChainlinkK8sClient,
optionalTestReporter testreporters.TestReporter, // Optionally pass in a test reporter to log further metrics
grafnaUrlProvider testreporters.GrafanaURLProvider,
client blockchain.EVMClient,
) error {
l := logging.GetTestLogger(t)
var err error
if err = testreporters.SendReport(t, namespace, "./", optionalTestReporter, grafnaUrlProvider); err != nil {
l.Warn().Err(err).Msg("Error writing test report")
}
// Delete all jobs to stop depleting the funds
err = DeleteAllJobs(chainlinkNodes)
if err != nil {
l.Warn().Msgf("Error deleting jobs %+v", err)
}
if err = ReturnFunds(chainlinkNodes, client); err != nil {
l.Error().Err(err).Str("Namespace", namespace).
Msg("Error attempting to return funds from chainlink nodes to network's default wallet. " +
"Environment is left running so you can try manually!")
}
return err
}
func DeleteAllJobs(chainlinkNodes []*client.ChainlinkK8sClient) error {
for _, node := range chainlinkNodes {
if node == nil {
return fmt.Errorf("found a nil chainlink node in the list of chainlink nodes while tearing down: %v", chainlinkNodes)
}
jobs, _, err := node.ReadJobs()
if err != nil {
return fmt.Errorf("error reading jobs from chainlink node, err: %w", err)
}
for _, maps := range jobs.Data {
if _, ok := maps["id"]; !ok {
return fmt.Errorf("error reading job id from chainlink node's jobs %+v", jobs.Data)
}
id := maps["id"].(string)
_, err := node.DeleteJob(id)
if err != nil {
return fmt.Errorf("error deleting job from chainlink node, err: %w", err)
}
}
}
return nil
}
// ReturnFunds attempts to return all the funds from the chainlink nodes to the network's default address
// all from a remote, k8s style environment
func ReturnFunds(chainlinkNodes []*client.ChainlinkK8sClient, blockchainClient blockchain.EVMClient) error {
if blockchainClient == nil {
return fmt.Errorf("blockchain client is nil, unable to return funds from chainlink nodes")
}
log.Info().Msg("Attempting to return Chainlink node funds to default network wallets")
if blockchainClient.NetworkSimulated() {
log.Info().Str("Network Name", blockchainClient.GetNetworkName()).
Msg("Network is a simulated network. Skipping fund return.")
return nil
}
for _, chainlinkNode := range chainlinkNodes {
fundedKeys, err := chainlinkNode.ExportEVMKeysForChain(blockchainClient.GetChainID().String())
if err != nil {
return err
}
for _, key := range fundedKeys {
keyToDecrypt, err := json.Marshal(key)
if err != nil {
return err
}
// This can take up a good bit of RAM and time. When running on the remote-test-runner, this can lead to OOM
// issues. So we avoid running in parallel; slower, but safer.
decryptedKey, err := keystore.DecryptKey(keyToDecrypt, client.ChainlinkKeyPassword)
if err != nil {
return err
}
err = blockchainClient.ReturnFunds(decryptedKey.PrivateKey)
if err != nil {
log.Error().Err(err).Str("Address", fundedKeys[0].Address).Msg("Error returning funds from Chainlink node")
}
}
}
return blockchainClient.WaitForEvents()
}
// FundAddresses will fund a list of addresses with an amount of native currency
func FundAddresses(blockchain blockchain.EVMClient, amount *big.Float, addresses ...string) error {
for _, address := range addresses {
toAddr := common.HexToAddress(address)
gasEstimates, err := blockchain.EstimateGas(ethereum.CallMsg{
To: &toAddr,
})
if err != nil {
return err
}
if err := blockchain.Fund(address, amount, gasEstimates); err != nil {
return err
}
}
return blockchain.WaitForEvents()
}
// EncodeOnChainExternalJobID encodes external job uuid to on-chain representation
func EncodeOnChainExternalJobID(jobID uuid.UUID) [32]byte {
var ji [32]byte
copy(ji[:], strings.Replace(jobID.String(), "-", "", 4))
return ji
}
// UpgradeChainlinkNodeVersions upgrades all Chainlink nodes to a new version, and then runs the test environment
// to apply the upgrades
func UpgradeChainlinkNodeVersions(
testEnvironment *environment.Environment,
newImage, newVersion string,
nodes ...*client.ChainlinkK8sClient,
) error {
if newImage == "" || newVersion == "" {
return errors.New("New image and new version is needed to upgrade the node")
}
for _, node := range nodes {
if err := node.UpgradeVersion(testEnvironment, newImage, newVersion); err != nil {
return err
}
}
err := testEnvironment.RunUpdated(len(nodes))
if err != nil { // Run the new environment and wait for changes to show
return err
}
return client.ReconnectChainlinkNodes(testEnvironment, nodes)
}
func DeployLINKToken(cd contracts.ContractDeployer) (contracts.LinkToken, error) {
linkToken, err := cd.DeployLinkTokenContract()
if err != nil {
return nil, err
}
return linkToken, err
}
func DeployMockETHLinkFeed(cd contracts.ContractDeployer, answer *big.Int) (contracts.MockETHLINKFeed, error) {
mockETHLINKFeed, err := cd.DeployMockETHLINKFeed(answer)
if err != nil {
return nil, err
}
return mockETHLINKFeed, err
}
// todo - move to CTF
func GenerateWallet() (common.Address, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
return common.Address{}, err
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return common.Address{}, fmt.Errorf("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
return crypto.PubkeyToAddress(*publicKeyECDSA), nil
}
// todo - move to CTF
func FundAddress(client blockchain.EVMClient, sendingKey string, fundingToSendEth *big.Float) error {
address := common.HexToAddress(sendingKey)
gasEstimates, err := client.EstimateGas(ethereum.CallMsg{
To: &address,
})
if err != nil {
return err
}
err = client.Fund(sendingKey, fundingToSendEth, gasEstimates)
if err != nil {
return err
}
return nil
}
// todo - move to CTF
func GetTxFromAddress(tx *types.Transaction) (string, error) {
from, err := types.Sender(types.LatestSignerForChainID(tx.ChainId()), tx)
return from.String(), err
}
// todo - move to CTF
func GetTxByHash(ctx context.Context, client blockchain.EVMClient, hash common.Hash) (*types.Transaction, bool, error) {
return client.(*blockchain.EthereumMultinodeClient).
DefaultClient.(*blockchain.EthereumClient).
Client.
TransactionByHash(ctx, hash)
}
// todo - move to CTF
func DecodeTxInputData(abiString string, data []byte) (map[string]interface{}, error) {
jsonABI, err := abi.JSON(strings.NewReader(abiString))
if err != nil {
return nil, err
}
methodSigData := data[:4]
inputsSigData := data[4:]
method, err := jsonABI.MethodById(methodSigData)
if err != nil {
return nil, err
}
inputsMap := make(map[string]interface{})
if err := method.Inputs.UnpackIntoMap(inputsMap, inputsSigData); err != nil {
return nil, err
}
return inputsMap, nil
}
// todo - move to EVMClient
func WaitForBlockNumberToBe(
waitForBlockNumberToBe uint64,
client blockchain.EVMClient,
wg *sync.WaitGroup,
timeout time.Duration,
t testing.TB,
) (uint64, error) {
blockNumberChannel := make(chan uint64)
errorChannel := make(chan error)
testContext, testCancel := context.WithTimeout(context.Background(), timeout)
defer testCancel()
ticker := time.NewTicker(time.Second * 1)
var blockNumber uint64
for {
select {
case <-testContext.Done():
ticker.Stop()
wg.Done()
return blockNumber,
fmt.Errorf("timeout waiting for Block Number to be: %d. Last recorded block number was: %d",
waitForBlockNumberToBe, blockNumber)
case <-ticker.C:
go func() {
currentBlockNumber, err := client.LatestBlockNumber(testcontext.Get(t))
if err != nil {
errorChannel <- err
}
blockNumberChannel <- currentBlockNumber
}()
case blockNumber = <-blockNumberChannel:
if blockNumber == waitForBlockNumberToBe {
ticker.Stop()
wg.Done()
return blockNumber, nil
}
case err := <-errorChannel:
ticker.Stop()
wg.Done()
return 0, err
}
}
}
// todo - move to EVMClient
func RewindSimulatedChainToBlockNumber(
ctx context.Context,
evmClient blockchain.EVMClient,
rpcURL string,
rewindChainToBlockNumber uint64,
l zerolog.Logger,
) (uint64, error) {
latestBlockNumberBeforeReorg, err := evmClient.LatestBlockNumber(ctx)
if err != nil {
return 0, fmt.Errorf("error getting latest block number: %w", err)
}
l.Info().
Str("RPC URL", rpcURL).
Uint64("Latest Block Number before Reorg", latestBlockNumberBeforeReorg).
Uint64("Rewind Chain to Block Number", rewindChainToBlockNumber).
Msg("Performing Reorg on chain by rewinding chain to specific block number")
_, err = NewRPCRawClient(rpcURL).SetHeadForSimulatedChain(rewindChainToBlockNumber)
if err != nil {
return 0, fmt.Errorf("error making reorg: %w", err)
}
err = evmClient.WaitForEvents()
if err != nil {
return 0, fmt.Errorf("error waiting for events: %w", err)
}
latestBlockNumberAfterReorg, err := evmClient.LatestBlockNumber(ctx)
if err != nil {
return 0, fmt.Errorf("error getting latest block number: %w", err)
}
l.Info().
Uint64("Block Number", latestBlockNumberAfterReorg).
Msg("Latest Block Number after Reorg")
return latestBlockNumberAfterReorg, nil
}
func GetRPCUrl(env *test_env.CLClusterTestEnv, chainID int64) (string, error) {
provider, err := env.GetRpcProvider(chainID)
if err != nil {
return "", err
}
return provider.PublicHttpUrls()[0], nil
}
// RPCRawClient
// created separate client since method evmClient.RawJsonRPCCall fails on "invalid argument 0: json: cannot unmarshal non-string into Go value of type hexutil.Uint64"
type RPCRawClient struct {
resty *resty.Client
}
func NewRPCRawClient(url string) *RPCRawClient {
isDebug := os.Getenv("DEBUG_RESTY") == "true"
restyClient := resty.New().SetDebug(isDebug).SetBaseURL(url)
return &RPCRawClient{
resty: restyClient,
}
}
func (g *RPCRawClient) SetHeadForSimulatedChain(setHeadToBlockNumber uint64) (JsonRPCResponse, error) {
var responseObject JsonRPCResponse
postBody, _ := json.Marshal(map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "debug_setHead",
"params": []string{hexutil.EncodeUint64(setHeadToBlockNumber)},
})
resp, err := g.resty.R().
SetHeader("Content-Type", "application/json").
SetBody(postBody).
SetResult(&responseObject).
Post("")
if err != nil {
return JsonRPCResponse{}, fmt.Errorf("error making API request: %w", err)
}
statusCode := resp.StatusCode()
if statusCode != 200 && statusCode != 201 {
return JsonRPCResponse{}, fmt.Errorf("error invoking debug_setHead method, received unexpected status code %d: %s", statusCode, resp.String())
}
if responseObject.Error != "" {
return JsonRPCResponse{}, fmt.Errorf("received non-empty error field: %v", responseObject.Error)
}
return responseObject, nil
}
type JsonRPCResponse struct {
Version string `json:"jsonrpc"`
Id int `json:"id"`
Result string `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}