-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
1077 lines (951 loc) · 31.4 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
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package optimism
import (
"context"
"encoding/json"
"fmt"
"log"
"math/big"
"net/http"
"strings"
"time"
RosettaTypes "github.com/coinbase/rosetta-sdk-go/types"
ethereum "github.com/ethereum-optimism/optimism/l2geth"
"github.com/ethereum-optimism/optimism/l2geth/core/types"
"github.com/ethereum-optimism/optimism/l2geth/eth"
"github.com/ethereum-optimism/optimism/l2geth/params"
"github.com/ethereum-optimism/optimism/l2geth/rpc"
types2 "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/sync/semaphore"
)
const (
defaultHTTPTimeout = 240 * time.Second
defaultTraceCacheSize = 100
defaultMaxTraceConcurrency = int64(1) // nolint:gomnd
semaphoreTraceWeight = int64(1) // nolint:gomnd
burnSelector = "0x9dc29fac" // keccak(burn(address,uint256))
mintSelector = "0x40c10f19" // keccak(mint(address,uint256))
erc20TransferSelector = "0xa9059cbb" // keccak(transfer(address,uint256))
fnSelectorLen = 10
sequencerFeeVaultAddr = "0x4200000000000000000000000000000000000011"
zeroAddr = "0x0000000000000000000000000000000000000000"
erc20TransferEventLogTopics = "Transfer(address,address,uint256)"
// While parsing ERC20 ops, we will ignore any event logs that we think are an ERC20 tansfer
// that do not contain 3 topics and who's 'data' field is not a single 32 byte hex string representing the amount of the transfer
numTopicsERC20Transfer = 3
// Fees on L2 weren't enforced on Goerli prior to this block height. This meant that transactions with a zero gas price were accepted without paying any fees
goerliRollupFeeEnforcementBlockHeight = 962297
)
var (
ovmEthAddr = common.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0000")
gasPriceOracleAddr = common.HexToAddress("0x420000000000000000000000000000000000000f")
// TODO: load the gpo owner from a config
gasPriceOracleOwnerMainnet = common.HexToAddress("0x7107142636C85c549690b1Aca12Bdb8052d26Ae6")
gasPriceOracleOwnerKovan = common.HexToAddress("0x84f70449f90300997840eCb0918873745Ede7aE6")
gasPriceOracleOwnerGoerli = common.HexToAddress("0xa693B8f8207FF043F6bbC2E2120bbE4C2251Efe9")
// The following mainnet block hashes have transaction (hashes) that are also present in succeeding blocks.
// This occurred due to a bug in contract whitelisting. Unfortunately eth_getTransactionByX now returns the succeeding block rather than the original.
// This is only an issue when reconciling account balances of the affected contracts.
// We fix this by hardcoding the original tx fees rather using the computed fees in the succeeding block (which had different block fee parameters at the time).
originalFeeAmountInDupTx = map[string]string{
"0x09b353fbfa414ff7765e9af807f488110775d55cfeee7df9ef3ee47e2aa0e9b9": "0x1c749d072a0258", // block height 985
"0x5471d82d53ccbddaf43c0fe223d97b125a80fc10ef006eb0fdf6ba3ec326ff39": "0x208e99382fa09c", // 19022
"0x5572ca94f6ef220f754ee486190a15c43aadcdfb2371ed3be1cd2d20f6edd96f": "0x19d4bf57ba1d5c", // 45036
"0x14a46bae4ae839106d7b45c6110dcf3935b38ed9b5701eb51c0450317b4abd2e": "0xab05782f225c1", // 123322
"0x5a24e459391c24d364497d914024edb75823547dc44573ee3ae965cb613fca16": "0xae5ab279b6d9f", // 123542
"0x336c8e5427d7049b0469aa23a61f52cecedcb5f41bde3a1684ba84136c6068e3": "0x1faece70f1455b", // 1133328
"0x1b3207bf43acb6a72e188edd91bced8abea2dd0edc47587db3e142ba10b7e001": "0x1f25f9be77ec1a", // 1135391
"0x638a3a797476c8a9b9ed5d6aa88e2e59e1b562fb4853f253c7c08753a7a285fb": "0x14c4434ea24463", // 1144468
"0x8fb9a287ccbe52b9ad39b47c95837ef257c3b684028e3ce2185bdf41d18646fa": "0x2f0664e5df8938", // 1244152
"0x2c987042c2af3e009ef8d2cebcdf659faaa694089e0f9231202f3efdcb6562b8": "0x3f69ca816a72d6", // 1272994
}
opTokenContractAddress = common.HexToAddress("0x4200000000000000000000000000000000000042")
// This contract accidentally triggered an Optimism bug on mainnet whereby its self destruction failed to relinquish its ETH
// See https://www.saurik.com/optimism.html for the details
opBugAccidentalTriggerContract = common.HexToAddress("0x40C539BBe076b91FdF681E6B4B84bd1Fe1F148d9")
opBugAccidentalTriggerTx = "0x3ff079ba4ea0745401e9661d623550d24c9412ea9ad578bfbb0d441dadcce9bc"
goerliChainID = big.NewInt(420)
)
// Client allows for querying a set of specific Ethereum endpoints in an
// idempotent manner. Client relies on the eth_*, debug_*, and admin_*
// methods and on the graphql endpoint.
//
// Client borrows HEAVILY from https://github.com/ethereum/go-ethereum/tree/master/ethclient.
type Client struct {
p *params.ChainConfig
tc *eth.TraceConfig
traceCache TraceCache
c JSONRPC
g GraphQL
currencyFetcher CurrencyFetcher
traceSemaphore *semaphore.Weighted
filterTokens bool
supportedTokens map[string]bool
supportsSyncing bool
skipAdminCalls bool
supportsPeering bool
bedrockBlock *big.Int
customBedrockTracer bool
traceByBlock bool
}
type ClientOptions struct {
HTTPTimeout time.Duration
MaxTraceConcurrency int64
EnableTraceCache bool
EnableGethTracer bool
FilterTokens bool
SupportedTokens map[string]bool
BedrockBlock *big.Int
SuportsSyncing bool
SkipAdminCalls bool
SupportsPeering bool
EnableCustomBedrockTracer bool
TraceByBlock bool
TraceCacheSize int
}
// NewClient creates a Client that from the provided url and params.
func NewClient(url string, params *params.ChainConfig, opts ClientOptions) (*Client, error) {
if opts.HTTPTimeout == 0 {
opts.HTTPTimeout = defaultHTTPTimeout
}
c, err := rpc.DialHTTPWithClient(url, &http.Client{
Timeout: opts.HTTPTimeout,
})
if err != nil {
return nil, fmt.Errorf("%w: unable to dial node", err)
}
tspec := tracerSpec{
TracerPath: defaultTracerPath,
UseGethTracer: opts.EnableGethTracer,
}
log.Printf("tracer spec: %#v", tspec)
tc, err := loadTraceConfig(tspec, opts.HTTPTimeout)
if err != nil {
return nil, fmt.Errorf("%w: unable to load trace config", err)
}
g, err := newGraphQLClient(url, opts.HTTPTimeout)
if err != nil {
return nil, fmt.Errorf("%w: unable to create GraphQL client", err)
}
currencyFetcher, err := newERC20CurrencyFetcher(c)
if err != nil {
return nil, fmt.Errorf("%w: unable to create CurrencyFetcher", err)
}
if opts.MaxTraceConcurrency == 0 {
opts.MaxTraceConcurrency = defaultMaxTraceConcurrency
}
log.Printf("max trace concurrency is %d", opts.MaxTraceConcurrency)
var traceCache TraceCache
if opts.EnableTraceCache {
traceCacheSize := opts.TraceCacheSize
if traceCacheSize == 0 {
traceCacheSize = defaultCacheSize
}
log.Printf("using trace cache. cache_size=%d", traceCacheSize)
if traceCache, err = NewTraceCache(c, tspec, opts.HTTPTimeout, traceCacheSize); err != nil {
return nil, fmt.Errorf("%w: unable to create trace cache", err)
}
}
return &Client{
p: params,
tc: tc,
c: c,
g: g,
currencyFetcher: currencyFetcher,
traceSemaphore: semaphore.NewWeighted(opts.MaxTraceConcurrency),
traceCache: traceCache,
filterTokens: opts.FilterTokens,
supportedTokens: opts.SupportedTokens,
supportsSyncing: opts.SuportsSyncing,
skipAdminCalls: opts.SkipAdminCalls,
supportsPeering: opts.SupportsPeering,
bedrockBlock: opts.BedrockBlock,
customBedrockTracer: opts.EnableCustomBedrockTracer,
traceByBlock: opts.TraceByBlock,
}, nil
}
// Close shuts down the RPC client connection.
func (ec *Client) Close() {
ec.c.Close()
}
// PendingNonceAt returns the account nonce of the given account in the pending state.
// This is the nonce that should be used for the next transaction.
func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
var result hexutil.Uint64
err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, "pending")
return uint64(result), err
}
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
// execution of a transaction.
func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
var hex hexutil.Big
if err := ec.c.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
return nil, err
}
return (*big.Int)(&hex), nil
}
// SendTransaction injects a signed transaction into the pending pool for execution.
//
// If the transaction was a contract creation use the TransactionReceipt method to get the
// contract address after the transaction has been mined.
func (ec *Client) SendTransaction(ctx context.Context, tx *types2.Transaction) error {
data, err := tx.MarshalBinary()
if err != nil {
return err
}
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data))
}
func (ec *Client) getTransactionTraces(
ctx context.Context,
txs []rpcTransaction,
) ([]*Call, error) {
if err := ec.traceSemaphore.Acquire(ctx, semaphoreTraceWeight); err != nil {
return nil, err
}
defer ec.traceSemaphore.Release(semaphoreTraceWeight)
traces := make([]*Call, len(txs))
if len(txs) == 0 {
return traces, nil
}
if ec.traceCache != nil {
for i := range txs {
result, err := ec.traceCache.FetchTransaction(ctx, txs[i].tx.Hash())
if err != nil {
return nil, err
}
traces[i] = result
}
return traces, nil
}
// Fetch traces sequentially to avoid DoS'ing the backend
for i := range txs {
if err := ec.c.CallContext(ctx, &traces[i], "debug_traceTransaction", txs[i].tx.Hash().Hex(), ec.tc); err != nil {
return nil, err
}
if traces[i] == nil {
return nil, fmt.Errorf("got empty trace for %x", txs[i].tx.Hash().Hex())
}
}
return traces, nil
}
func (ec *Client) getBlockReceipts(
ctx context.Context,
blockHash common.Hash,
txs []rpcTransaction,
) ([]*types.Receipt, error) {
receipts := make([]*types.Receipt, len(txs))
if len(txs) == 0 {
return receipts, nil
}
reqs := make([]rpc.BatchElem, len(txs))
for i := range reqs {
reqs[i] = rpc.BatchElem{
Method: EthGetTransactionReceipt,
Args: []interface{}{txs[i].tx.Hash().Hex()},
Result: &receipts[i],
}
}
if err := ec.c.BatchCallContext(ctx, reqs); err != nil {
return nil, err
}
for i := range reqs {
if reqs[i].Error != nil {
return nil, reqs[i].Error
}
if receipts[i] == nil {
return nil, fmt.Errorf("got empty receipt for %x", txs[i].tx.Hash().Hex())
}
if receipts[i].BlockHash.Hex() != blockHash.Hex() && !blockContainsDuplicateTransaction(blockHash) {
return nil, fmt.Errorf(
"%w: expected block hash %s for transaction but got %s",
ErrBlockOrphaned,
blockHash.Hex(),
receipts[i].BlockHash.Hex(),
)
}
}
return receipts, nil
}
//nolint:gocognit
func (ec *Client) erc20TokenOps(
ctx context.Context,
block *types.Block,
tx *legacyTransaction,
startIndex int,
) ([]*RosettaTypes.Operation, error) {
receipt := tx.Receipt
ops := []*RosettaTypes.Operation{}
var status string
if receipt.Status == 1 {
status = SuccessStatus
} else {
status = FailureStatus
}
keccak := crypto.Keccak256([]byte(erc20TransferEventLogTopics))
encodedTransferMethod := hexutil.Encode(keccak)
// To handle cases such as out-of-gas errors, where no logs are emitted
if status == FailureStatus && len(receipt.Logs) == 0 {
input := strings.ToLower(tx.Trace.Input)
// special case for failed ERC20 token transfers
if strings.HasPrefix(input, erc20TransferSelector) {
if toAddress, amount, err := decodeAddressUint256(input[fnSelectorLen:]); err == nil {
contractAddress := tx.Trace.To.String()
fromAddress := tx.Trace.From.String()
currency, err := ec.currencyFetcher.FetchCurrency(ctx, block.Number().Uint64(), contractAddress)
// If an error is encountered while fetching currency details, return a default value and let the client handle it.
if err != nil {
log.Printf("error while fetching currency details for currency: %s: %v", contractAddress, err)
currency = &RosettaTypes.Currency{
Symbol: defaultERC20Symbol,
Decimals: defaultERC20Decimals,
Metadata: map[string]interface{}{
ContractAddressKey: contractAddress,
},
}
}
ops = appendERC20Operations(ops, fromAddress, toAddress.String(), amount, currency, startIndex, status)
}
}
}
for _, receiptLog := range receipt.Logs {
// If this isn't an ERC20 transfer, skip
if !containsTopic(receiptLog, encodedTransferMethod) {
continue
}
if len(receiptLog.Topics) != numTopicsERC20Transfer {
continue
}
value := new(big.Int).SetBytes(receiptLog.Data)
// If value <= 0, skip to the next receiptLog. Otherwise, proceed to generate the debit + credit operations.
if value.Cmp(big.NewInt(0)) < 1 {
continue
}
contractAddress := receiptLog.Address.String()
_, ok := ChecksumAddress(contractAddress)
if !ok {
return nil, fmt.Errorf("%s is not a valid address", contractAddress)
}
// If it's a deposit tx, skip
if contractAddress == ovmEthAddr.String() {
continue
}
if !ec.supportsToken(contractAddress) {
continue
}
fromAddress := common.HexToAddress(receiptLog.Topics[1].Hex()).String()
_, ok = ChecksumAddress(fromAddress)
if !ok {
return nil, fmt.Errorf("%s is not a valid address", fromAddress)
}
toAddress := common.HexToAddress(receiptLog.Topics[2].Hex()).String()
_, ok = ChecksumAddress(toAddress)
if !ok {
return nil, fmt.Errorf("%s is not a valid address", toAddress)
}
currency, err := ec.currencyFetcher.FetchCurrency(ctx, block.Number().Uint64(), contractAddress)
// If an error is encountered while fetching currency details, return a default value and let the client handle it.
if err != nil {
log.Printf("error while fetching currency details for currency: %s: %v", contractAddress, err)
currency = &RosettaTypes.Currency{
Symbol: defaultERC20Symbol,
Decimals: defaultERC20Decimals,
Metadata: map[string]interface{}{
ContractAddressKey: contractAddress,
},
}
}
ops = appendERC20Operations(ops, fromAddress, toAddress, value, currency, startIndex, status)
}
return ops, nil
}
func appendERC20Operations(ops []*RosettaTypes.Operation,
fromAddress string,
toAddress string,
value *big.Int,
currency *RosettaTypes.Currency,
startIndex int,
status string) []*RosettaTypes.Operation {
if fromAddress == zeroAddr {
mintOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + startIndex),
},
Type: ERC20MintOpType,
Status: RosettaTypes.String(status),
Account: &RosettaTypes.AccountIdentifier{
Address: toAddress,
},
Amount: &RosettaTypes.Amount{
Value: value.String(),
Currency: currency,
},
}
ops = append(ops, mintOp)
return ops
}
if toAddress == zeroAddr {
burnOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + startIndex),
},
Type: ERC20BurnOpType,
Status: RosettaTypes.String(status),
Account: &RosettaTypes.AccountIdentifier{
Address: fromAddress,
},
Amount: &RosettaTypes.Amount{
Value: new(big.Int).Neg(value).String(),
Currency: currency,
},
}
ops = append(ops, burnOp)
return ops
}
fromOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + startIndex),
},
Type: PaymentOpType,
Status: RosettaTypes.String(status),
Account: &RosettaTypes.AccountIdentifier{
Address: fromAddress,
},
Amount: &RosettaTypes.Amount{
Value: new(big.Int).Neg(value).String(),
Currency: currency,
},
}
ops = append(ops, fromOp)
lastOpIndex := ops[len(ops)-1].OperationIdentifier.Index
toOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: lastOpIndex + 1,
},
RelatedOperations: []*RosettaTypes.OperationIdentifier{
{
Index: lastOpIndex,
},
},
Type: PaymentOpType,
Status: RosettaTypes.String(status),
Account: &RosettaTypes.AccountIdentifier{
Address: toAddress,
},
Amount: &RosettaTypes.Amount{
Value: value.String(),
Currency: currency,
},
}
ops = append(ops, toOp)
return ops
}
func blockContainsDuplicateTransaction(blockHash common.Hash) bool {
return originalFeeAmountInDupTx[blockHash.Hex()] != ""
}
func containsTopic(log *types.Log, topic string) bool {
for _, t := range log.Topics {
hex := t.Hex()
if hex == topic {
return true
}
}
return false
}
// traceOps returns all *RosettaTypes.Operation for a given
// array of flattened traces.
//
//nolint:gocyclo,gocognit
func traceOps(block *types.Block, calls []*FlatCall, startIndex int) []*RosettaTypes.Operation {
var ops []*RosettaTypes.Operation
if len(calls) == 0 {
return ops
}
destroyedAccounts := map[string]*big.Int{}
for _, trace := range calls {
// Rejected transactions do not produce traces (ex: attempts to deploy contracts that aren't in the Optimism whitelist)
if trace.Type == "" {
continue
}
// Handle partial transaction success
metadata := map[string]interface{}{}
opStatus := SuccessStatus
if trace.Revert {
opStatus = FailureStatus
metadata["error"] = trace.ErrorMessage
}
var zeroValue bool
if trace.Value.Sign() == 0 {
zeroValue = true
}
// Skip all 0 value CallType operations (TODO: make optional to include)
//
// We can't continue here because we may need to adjust our destroyed
// accounts map if a CallTYpe operation resurrects an account.
shouldAdd := true
if zeroValue && CallType(trace.Type) {
shouldAdd = false
}
var (
burnCall bool
mintCall bool
burnMintAddr common.Address
burnMintAmt *big.Int
)
// either we're burning or minting OVM_ETH
if trace.Type == CallOpType && trace.To.Hex() == ovmEthAddr.Hex() {
burnCall = strings.HasPrefix(trace.Input, burnSelector)
mintCall = strings.HasPrefix(trace.Input, mintSelector)
if burnCall || mintCall {
var err error
burnMintAddr, burnMintAmt, err = decodeAddressUint256(trace.Input[fnSelectorLen:])
if err != nil {
burnMintAddr, burnMintAmt = common.Address{}, nil
}
}
}
// Checksum addresses
from := MustChecksum(trace.From.String())
to := MustChecksum(trace.To.String())
if shouldAdd {
value := new(big.Int).Neg(trace.Value).String()
// The OP bug here means that the ETH balance of the self-destructed contract remains unchanged
// TODO(inphi): Bedrock fixes this
if block.Transactions()[0].Hash().String() == opBugAccidentalTriggerTx &&
opStatus == SuccessStatus &&
trace.Type == SelfDestructOpType &&
from == opBugAccidentalTriggerContract.String() {
value = new(big.Int).String()
}
fromOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + startIndex),
},
Type: trace.Type,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: from,
},
Amount: &RosettaTypes.Amount{
Value: value,
Currency: Currency,
},
Metadata: metadata,
}
if zeroValue {
fromOp.Amount = nil
} else {
_, destroyed := destroyedAccounts[from]
if destroyed && opStatus == SuccessStatus {
destroyedAccounts[from] = new(big.Int).Sub(destroyedAccounts[from], trace.Value)
}
}
ops = append(ops, fromOp)
}
if burnCall {
// if we are handling a burn of ovmEth, `shouldAdd` is disabled for the entirety of the iteration for this trace call
burnDebitOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + startIndex),
},
Type: trace.Type,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: burnMintAddr.String(),
},
Amount: &RosettaTypes.Amount{
Value: new(big.Int).Neg(burnMintAmt).String(),
Currency: Currency,
},
Metadata: metadata,
}
ops = append(ops, burnDebitOp)
lastOpIndex := ops[len(ops)-1].OperationIdentifier.Index
burnCreditOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + startIndex),
},
RelatedOperations: []*RosettaTypes.OperationIdentifier{
{
Index: lastOpIndex,
},
},
Type: trace.Type,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: zeroAddr,
},
Amount: &RosettaTypes.Amount{
Value: burnMintAmt.String(),
Currency: Currency,
},
Metadata: metadata,
}
ops = append(ops, burnCreditOp)
}
if mintCall {
mintOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + startIndex),
},
Type: trace.Type,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: burnMintAddr.String(),
},
Amount: &RosettaTypes.Amount{
Value: burnMintAmt.String(),
Currency: Currency,
},
Metadata: metadata,
}
ops = append(ops, mintOp)
}
// Add to destroyed accounts if SELFDESTRUCT
// and overwrite existing balance.
// OVM hack: The OVM models SELFDESTRUCT as a couple of Add and Sub balance operations. See https://github.com/ethereum-optimism/optimism/issues/2604
// for context.
// We do the same here by permitting the shouldAdd check work on both sides of the Value transfer. Otherwise, the destroyed account will seem to
// have a negative balance.
// TODO(inphi): Bedrock fixes this. Uncomment this once Bedrock is up
/*
if trace.Type == SelfDestructOpType {
destroyedAccounts[from] = new(big.Int)
// If destination of of SELFDESTRUCT is self,
// we should skip. In the EVM, the balance is reset
// after the balance is increased on the destination
// so this is a no-op.
if from == to {
continue
}
}
*/
// Skip empty to addresses (this may not
// actually occur but leaving it as a
// sanity check)
if len(trace.To.String()) == 0 {
continue
}
// If the account is resurrected, we remove it from
// the destroyed accounts map.
if CreateType(trace.Type) {
delete(destroyedAccounts, to)
}
if shouldAdd {
lastOpIndex := ops[len(ops)-1].OperationIdentifier.Index
toOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: lastOpIndex + 1,
},
RelatedOperations: []*RosettaTypes.OperationIdentifier{
{
Index: lastOpIndex,
},
},
Type: trace.Type,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: to,
},
Amount: &RosettaTypes.Amount{
Value: trace.Value.String(),
Currency: Currency,
},
Metadata: metadata,
}
if zeroValue {
toOp.Amount = nil
} else {
_, destroyed := destroyedAccounts[to]
if destroyed && opStatus == SuccessStatus {
destroyedAccounts[to] = new(big.Int).Add(destroyedAccounts[to], trace.Value)
}
}
ops = append(ops, toOp)
}
}
// Zero-out all destroyed accounts that are removed
// during transaction finalization.
for acct, val := range destroyedAccounts {
if val.Sign() == 0 {
continue
}
if val.Sign() < 0 {
log.Fatalf("negative balance for suicided account %s: %s\n", acct, val.String())
}
ops = append(ops, &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: ops[len(ops)-1].OperationIdentifier.Index + 1,
},
Type: DestructOpType,
Status: RosettaTypes.String(SuccessStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: acct,
},
Amount: &RosettaTypes.Amount{
Value: new(big.Int).Neg(val).String(),
Currency: Currency,
},
})
}
return ops
}
func decodeAddressUint256(hex string) (common.Address, *big.Int, error) {
if len(hex) != 128 {
return common.Address{}, nil, fmt.Errorf("invalid hex string length")
}
addrB := hex[:64]
addr := common.HexToAddress(addrB)
uint256Str := strings.TrimLeft(hex[64:], "0")
if uint256Str == "" {
uint256Str = "0"
}
bigHex := fmt.Sprintf("0x%s", uint256Str)
uint256, err := hexutil.DecodeBig(bigHex)
if err != nil {
return common.Address{}, nil, err
}
return addr, uint256, nil
}
// transactionReceipt returns the receipt of a transaction by transaction hash.
// Note that the receipt is not available for pending transactions.
func (ec *Client) transactionReceipt(
ctx context.Context,
txHash common.Hash,
) (*types.Receipt, error) {
var r *types.Receipt
err := ec.c.CallContext(ctx, &r, EthGetTransactionReceipt, txHash)
if err == nil {
if r == nil {
return nil, ethereum.NotFound
}
}
return r, err
}
// contractCall returns the data specified by the given contract method
func (ec *Client) contractCall(
ctx context.Context,
params map[string]interface{},
) (map[string]interface{}, error) {
// validate call input
input, err := validateCallInput(params)
if err != nil {
return nil, err
}
// default query
blockQuery := "latest"
// if block number or hash, override blockQuery
if input.BlockIndex > int64(0) {
blockQuery = toBlockNumArg(big.NewInt(input.BlockIndex))
} else if len(input.BlockHash) > 0 {
blockQuery = input.BlockHash
}
// ensure valid contract address
_, ok := ChecksumAddress(input.To)
if !ok {
return nil, ErrCallParametersInvalid
}
// parameters for eth_call
callParams := map[string]string{
"to": input.To,
"data": input.Data,
}
var resp string
if err := ec.c.CallContext(ctx, &resp, "eth_call", callParams, blockQuery); err != nil {
return nil, err
}
return map[string]interface{}{
"data": resp,
}, nil
}
// estimateGas returns the data specified by the given contract method
func (ec *Client) estimateGas(
ctx context.Context,
params map[string]interface{},
) (map[string]interface{}, error) {
// validate call input
input, err := validateCallInput(params)
if err != nil {
return nil, err
}
// ensure valid contract address
_, ok := ChecksumAddress(input.To)
if !ok {
return nil, ErrCallParametersInvalid
}
// ensure valid from address
_, ok = ChecksumAddress(input.From)
if !ok {
return nil, ErrCallParametersInvalid
}
// parameters for eth_estimateGas
estimateGasParams := map[string]string{
"from": input.From,
"to": input.To,
"data": input.Data,
}
var resp string
if err := ec.c.CallContext(ctx, &resp, "eth_estimateGas", estimateGasParams); err != nil {
return nil, err
}
return map[string]interface{}{
"data": resp,
}, nil
}
func validateCallInput(params map[string]interface{}) (*GetCallInput, error) {
var input GetCallInput
if err := RosettaTypes.UnmarshalMap(params, &input); err != nil {
return nil, fmt.Errorf("%w: %s", ErrCallParametersInvalid, err.Error())
}
// to address is required for call requests
if len(input.To) == 0 {
return nil, fmt.Errorf("%w:to address is missing from parameters", ErrCallParametersInvalid)
}
if len(input.Data) == 0 {
return nil, fmt.Errorf("%w:data is missing from parameters", ErrCallParametersInvalid)
}
return &input, nil
}
func convertTime(time uint64) int64 {
return int64(time) * 1000
}
type rpcProgress struct {
StartingBlock hexutil.Uint64
CurrentBlock hexutil.Uint64
HighestBlock hexutil.Uint64
PulledStates hexutil.Uint64
KnownStates hexutil.Uint64
}
//nolint:unused
type graphqlBalance struct {
Errors []struct {
Message string `json:"message"`
Path []string `json:"path"`
} `json:"errors"`
Data struct {
Block struct {
Hash string `json:"hash"`
Number int64 `json:"number"`
Account struct {
Balance string `json:"balance"`
Nonce string `json:"transactionCount"`
Code string `json:"code"`
} `json:"account"`
} `json:"block"`
} `json:"data"`
}
// decodeHexData accepts a fully formed hex string (including the 0x prefix) and returns a big.Int
func decodeHexData(data string) (*big.Int, error) {
decoded, ok := new(big.Int).SetString(data[2:], 16)
if !ok {
return nil, fmt.Errorf("could not extract data from %s", data)
}
return decoded, nil
}
// GetBlockByNumberInput is the input to the call
// method "eth_getBlockByNumber".
type GetBlockByNumberInput struct {
Index *int64 `json:"index,omitempty"`
ShowTxDetails bool `json:"show_transaction_details"`
}
// GetTransactionReceiptInput is the input to the call
// method "eth_getTransactionReceipt".
type GetTransactionReceiptInput struct {
TxHash string `json:"tx_hash"`
}
// GetCallInput is the input to the call
// method "eth_call", "eth_estimateGas".
type GetCallInput struct {
BlockIndex int64 `json:"index,omitempty"`
BlockHash string `json:"hash,omitempty"`
From string `json:"from"`
To string `json:"to"`
Gas int64 `json:"gas"`
GasPrice int64 `json:"gas_price"`
Value int64 `json:"value"`
Data string `json:"data"`
}
// Call handles calls to the /call endpoint.
func (ec *Client) Call(
ctx context.Context,
request *RosettaTypes.CallRequest,