Repository navigation
Expand file tree
/
Copy pathrequest.go
More file actions
1311 lines (1095 loc) · 42.5 KB
/
Copy pathrequest.go
File metadata and controls
1311 lines (1095 loc) · 42.5 KB
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
package query
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"strings"
"github.com/certusone/wormhole/node/pkg/common"
gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
ethCommon "github.com/ethereum/go-ethereum/common"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
solana "github.com/gagliardetto/solana-go"
)
// MSG_VERSION is the current version of the CCQ message protocol.
const MSG_VERSION uint8 = 1
// QueryRequest defines a cross chain query request to be submitted to the guardians.
// It is the payload of the SignedQueryRequest gossip message.
type QueryRequest struct {
Nonce uint32
PerChainQueries []*PerChainQueryRequest
}
// PerChainQueryRequest represents a query request for a single chain.
type PerChainQueryRequest struct {
// ChainId indicates which chain this query is destine for.
ChainId vaa.ChainID
// Query is the chain specific query data.
Query ChainSpecificQuery
}
// ChainSpecificQuery is the interface that must be implemented by a chain specific query.
type ChainSpecificQuery interface {
Type() ChainSpecificQueryType
Marshal() ([]byte, error)
Unmarshal(data []byte) error
UnmarshalFromReader(reader *bytes.Reader) error
Validate() error
}
// ChainSpecificQueryType is used to interpret the data in a per chain query request.
type ChainSpecificQueryType uint8
// EthCallQueryRequestType is the type of an EVM eth_call query request.
const EthCallQueryRequestType ChainSpecificQueryType = 1
// EthCallQueryRequest implements ChainSpecificQuery for an EVM eth_call query request.
type EthCallQueryRequest struct {
// BlockId identifies the block to be queried. It must be a hex string starting with 0x. It may be a block number or a block hash.
BlockId string
// CallData is an array of specific queries to be performed on the specified block, in a single RPC call.
CallData []*EthCallData
}
func (ecr *EthCallQueryRequest) CallDataList() []*EthCallData {
return ecr.CallData
}
// EthCallByTimestampQueryRequestType is the type of an EVM eth_call_by_timestamp query request.
const EthCallByTimestampQueryRequestType ChainSpecificQueryType = 2
// EthCallByTimestampQueryRequest implements ChainSpecificQuery for an EVM eth_call_by_timestamp query request.
type EthCallByTimestampQueryRequest struct {
// TargetTimeInUs specifies the desired timestamp in microseconds.
TargetTimestamp uint64
// TargetBlockIdHint is optional. If specified, it identifies the block prior to the desired timestamp. It must be a hex string starting with 0x. It may be a block number or a block hash.
TargetBlockIdHint string
// FollowingBlockIdHint is optional. If specified, it identifies the block immediately following the desired timestamp. It must be a hex string starting with 0x. It may be a block number or a block hash.
FollowingBlockIdHint string
// CallData is an array of specific queries to be performed on the specified block, in a single RPC call.
CallData []*EthCallData
}
func (ecr *EthCallByTimestampQueryRequest) CallDataList() []*EthCallData {
return ecr.CallData
}
// EthCallWithFinalityQueryRequestType is the type of an EVM eth_call_with_finality query request.
const EthCallWithFinalityQueryRequestType ChainSpecificQueryType = 3
// EthCallWithFinalityQueryRequest implements ChainSpecificQuery for an EVM eth_call_with_finality query request.
type EthCallWithFinalityQueryRequest struct {
// BlockId identifies the block to be queried. It must be a hex string starting with 0x. It may be a block number or a block hash.
BlockId string
// Finality is required. It identifies the level of finality the block must reach before the query is performed. Valid values are "finalized" and "safe".
Finality string
// CallData is an array of specific queries to be performed on the specified block, in a single RPC call.
CallData []*EthCallData
}
func (ecr *EthCallWithFinalityQueryRequest) CallDataList() []*EthCallData {
return ecr.CallData
}
// EthCallData specifies the parameters to a single EVM eth_call request.
type EthCallData struct {
// To specifies the contract address to be queried.
To []byte
// Data is the ABI encoded parameters to the query.
Data []byte
}
const EvmContractAddressLength = 20
////////////////////////////////// Solana Queries ////////////////////////////////////////////////
// SolanaAccountQueryRequestType is the type of a Solana sol_account query request.
const SolanaAccountQueryRequestType ChainSpecificQueryType = 4
// SolanaAccountQueryRequest implements ChainSpecificQuery for a Solana sol_account query request.
type SolanaAccountQueryRequest struct {
// Commitment identifies the commitment level to be used in the queried. Currently it may only "finalized".
// Before we can support "confirmed", we need a way to read the account data and the block information atomically.
// We would also need to deal with the fact that queries are only handled in the finalized watcher and it does not
// have access to the latest confirmed slot needed for MinContextSlot retries.
Commitment string
// The minimum slot that the request can be evaluated at. Zero means unused.
MinContextSlot uint64
// The offset of the start of data to be returned. Unused if DataSliceLength is zero.
DataSliceOffset uint64
// The length of the data to be returned. Zero means all data is returned.
DataSliceLength uint64
// Accounts is an array of accounts to be queried.
Accounts [][SolanaPublicKeyLength]byte
}
// Solana public keys are fixed length.
const SolanaPublicKeyLength = solana.PublicKeyLength
// According to the Solana spec, the longest comment string is nine characters. Allow a few more, just in case.
// https://pkg.go.dev/github.com/gagliardetto/solana-go/rpc#CommitmentType
const SolanaMaxCommitmentLength = 12
// According to the spec, the query only supports up to 100 accounts.
// https://github.com/solana-labs/solana/blob/9d132441fdc6282a8be4bff0bc77d6a2fefe8b59/rpc-client-api/src/request.rs#L204
const SolanaMaxAccountsPerQuery = 100
func (saq *SolanaAccountQueryRequest) AccountList() [][SolanaPublicKeyLength]byte {
return saq.Accounts
}
// SolanaPdaQueryRequestType is the type of a Solana sol_pda query request.
const SolanaPdaQueryRequestType ChainSpecificQueryType = 5
// SolanaPdaQueryRequest implements ChainSpecificQuery for a Solana sol_pda query request.
type SolanaPdaQueryRequest struct {
// Commitment identifies the commitment level to be used in the queried. Currently it may only "finalized".
// Before we can support "confirmed", we need a way to read the account data and the block information atomically.
// We would also need to deal with the fact that queries are only handled in the finalized watcher and it does not
// have access to the latest confirmed slot needed for MinContextSlot retries.
Commitment string
// The minimum slot that the request can be evaluated at. Zero means unused.
MinContextSlot uint64
// The offset of the start of data to be returned. Unused if DataSliceLength is zero.
DataSliceOffset uint64
// The length of the data to be returned. Zero means all data is returned.
DataSliceLength uint64
// PDAs is an array of PDAs to be queried.
PDAs []SolanaPDAEntry
}
// SolanaPDAEntry defines a single Solana Program derived address (PDA).
type SolanaPDAEntry struct {
ProgramAddress [SolanaPublicKeyLength]byte
Seeds [][]byte
}
// According to the spec, there may be at most 16 seeds.
// https://github.com/gagliardetto/solana-go/blob/6fe3aea02e3660d620433444df033fc3fe6e64c1/keys.go#L559
const SolanaMaxSeeds = solana.MaxSeeds
// According to the spec, a seed may be at most 32 bytes.
// https://github.com/gagliardetto/solana-go/blob/6fe3aea02e3660d620433444df033fc3fe6e64c1/keys.go#L557
const SolanaMaxSeedLen = solana.MaxSeedLength
func (spda *SolanaPdaQueryRequest) PDAList() []SolanaPDAEntry {
return spda.PDAs
}
// PerChainQueryInternal is an internal representation of a query request that is passed to the watcher.
type PerChainQueryInternal struct {
RequestID string
RequestIdx int
Request *PerChainQueryRequest
}
func (pcqi *PerChainQueryInternal) ID() string {
return fmt.Sprintf("%s:%d", pcqi.RequestID, pcqi.RequestIdx)
}
// QueryRequestDigest returns the query signing prefix based on the environment.
func QueryRequestDigest(env common.Environment, b []byte) ethCommon.Hash {
var queryRequestPrefix []byte
if env == common.MainNet {
queryRequestPrefix = []byte("mainnet_query_request_000000000000|")
} else if env == common.TestNet {
queryRequestPrefix = []byte("testnet_query_request_000000000000|")
} else {
queryRequestPrefix = []byte("devnet_query_request_0000000000000|")
}
return ethCrypto.Keccak256Hash(append(queryRequestPrefix, b...))
}
// PostSignedQueryRequest posts a signed query request to the specified channel.
func PostSignedQueryRequest(signedQueryReqSendC chan<- *gossipv1.SignedQueryRequest, req *gossipv1.SignedQueryRequest) error {
select {
case signedQueryReqSendC <- req:
return nil
default:
return common.ErrChanFull
}
}
func SignedQueryRequestEqual(left *gossipv1.SignedQueryRequest, right *gossipv1.SignedQueryRequest) bool {
if !bytes.Equal(left.QueryRequest, right.QueryRequest) {
return false
}
if !bytes.Equal(left.Signature, right.Signature) {
return false
}
return true
}
//
// Implementation of QueryRequest.
//
// Marshal serializes the binary representation of a query request.
// This method calls Validate() and relies on it to range checks lengths, etc.
func (queryRequest *QueryRequest) Marshal() ([]byte, error) {
if err := queryRequest.Validate(); err != nil {
return nil, err
}
buf := new(bytes.Buffer)
vaa.MustWrite(buf, binary.BigEndian, MSG_VERSION) // version
vaa.MustWrite(buf, binary.BigEndian, queryRequest.Nonce) // uint32
vaa.MustWrite(buf, binary.BigEndian, uint8(len(queryRequest.PerChainQueries)))
for _, perChainQuery := range queryRequest.PerChainQueries {
pcqBuf, err := perChainQuery.Marshal()
if err != nil {
return nil, fmt.Errorf("failed to marshal per chain query: %w", err)
}
buf.Write(pcqBuf)
}
return buf.Bytes(), nil
}
// Unmarshal deserializes the binary representation of a query request from a byte array
func (queryRequest *QueryRequest) Unmarshal(data []byte) error {
reader := bytes.NewReader(data[:])
return queryRequest.UnmarshalFromReader(reader)
}
// UnmarshalFromReader deserializes the binary representation of a query request from an existing reader
func (queryRequest *QueryRequest) UnmarshalFromReader(reader *bytes.Reader) error {
var version uint8
if err := binary.Read(reader, binary.BigEndian, &version); err != nil {
return fmt.Errorf("failed to read message version: %w", err)
}
if version != MSG_VERSION {
return fmt.Errorf("unsupported message version: %d", version)
}
if err := binary.Read(reader, binary.BigEndian, &queryRequest.Nonce); err != nil {
return fmt.Errorf("failed to read request nonce: %w", err)
}
numPerChainQueries := uint8(0)
if err := binary.Read(reader, binary.BigEndian, &numPerChainQueries); err != nil {
return fmt.Errorf("failed to read number of per chain queries: %w", err)
}
for count := 0; count < int(numPerChainQueries); count++ {
perChainQuery := PerChainQueryRequest{}
err := perChainQuery.UnmarshalFromReader(reader)
if err != nil {
return fmt.Errorf("failed to Unmarshal per chain query: %w", err)
}
queryRequest.PerChainQueries = append(queryRequest.PerChainQueries, &perChainQuery)
}
if reader.Len() != 0 {
return fmt.Errorf("excess bytes in unmarshal")
}
if err := queryRequest.Validate(); err != nil {
return fmt.Errorf("unmarshaled request failed validation: %w", err)
}
return nil
}
// Validate does basic validation on a received query request.
func (queryRequest *QueryRequest) Validate() error {
// Nothing to validate on the Nonce.
if len(queryRequest.PerChainQueries) <= 0 {
return fmt.Errorf("request does not contain any per chain queries")
}
if len(queryRequest.PerChainQueries) > math.MaxUint8 {
return fmt.Errorf("too many per chain queries")
}
for idx, perChainQuery := range queryRequest.PerChainQueries {
if err := perChainQuery.Validate(); err != nil {
return fmt.Errorf("failed to validate per chain query %d: %w", idx, err)
}
}
return nil
}
// Equal verifies that two query requests are equal.
func (left *QueryRequest) Equal(right *QueryRequest) bool {
if left.Nonce != right.Nonce {
return false
}
if len(left.PerChainQueries) != len(right.PerChainQueries) {
return false
}
for idx := range left.PerChainQueries {
if !left.PerChainQueries[idx].Equal(right.PerChainQueries[idx]) {
return false
}
}
return true
}
//
// Implementation of PerChainQueryRequest.
//
// Marshal serializes the binary representation of a per chain query request.
// This method calls Validate() and relies on it to range checks lengths, etc.
func (perChainQuery *PerChainQueryRequest) Marshal() ([]byte, error) {
if err := perChainQuery.Validate(); err != nil {
return nil, err
}
buf := new(bytes.Buffer)
vaa.MustWrite(buf, binary.BigEndian, perChainQuery.ChainId)
vaa.MustWrite(buf, binary.BigEndian, perChainQuery.Query.Type())
queryBuf, err := perChainQuery.Query.Marshal()
if err != nil {
return nil, err
}
// Write the length of the query to facilitate on-chain parsing.
if len(queryBuf) > math.MaxUint32 {
return nil, fmt.Errorf("query too long")
}
vaa.MustWrite(buf, binary.BigEndian, uint32(len(queryBuf)))
buf.Write(queryBuf)
return buf.Bytes(), nil
}
// Unmarshal deserializes the binary representation of a per chain query request from a byte array
func (perChainQuery *PerChainQueryRequest) Unmarshal(data []byte) error {
reader := bytes.NewReader(data[:])
return perChainQuery.UnmarshalFromReader(reader)
}
// UnmarshalFromReader deserializes the binary representation of a per chain query request from an existing reader
func (perChainQuery *PerChainQueryRequest) UnmarshalFromReader(reader *bytes.Reader) error {
if err := binary.Read(reader, binary.BigEndian, &perChainQuery.ChainId); err != nil {
return fmt.Errorf("failed to read request chain: %w", err)
}
qt := uint8(0)
if err := binary.Read(reader, binary.BigEndian, &qt); err != nil {
return fmt.Errorf("failed to read request type: %w", err)
}
queryType := ChainSpecificQueryType(qt)
if err := ValidatePerChainQueryRequestType(queryType); err != nil {
return err
}
// Skip the query length.
var queryLength uint32
if err := binary.Read(reader, binary.BigEndian, &queryLength); err != nil {
return fmt.Errorf("failed to read query length: %w", err)
}
switch queryType {
case EthCallQueryRequestType:
q := EthCallQueryRequest{}
if err := q.UnmarshalFromReader(reader); err != nil {
return fmt.Errorf("failed to unmarshal eth call request: %w", err)
}
perChainQuery.Query = &q
case EthCallByTimestampQueryRequestType:
q := EthCallByTimestampQueryRequest{}
if err := q.UnmarshalFromReader(reader); err != nil {
return fmt.Errorf("failed to unmarshal eth call by timestamp request: %w", err)
}
perChainQuery.Query = &q
case EthCallWithFinalityQueryRequestType:
q := EthCallWithFinalityQueryRequest{}
if err := q.UnmarshalFromReader(reader); err != nil {
return fmt.Errorf("failed to unmarshal eth call with finality request: %w", err)
}
perChainQuery.Query = &q
case SolanaAccountQueryRequestType:
q := SolanaAccountQueryRequest{}
if err := q.UnmarshalFromReader(reader); err != nil {
return fmt.Errorf("failed to unmarshal solana account query request: %w", err)
}
perChainQuery.Query = &q
case SolanaPdaQueryRequestType:
q := SolanaPdaQueryRequest{}
if err := q.UnmarshalFromReader(reader); err != nil {
return fmt.Errorf("failed to unmarshal solana PDA query request: %w", err)
}
perChainQuery.Query = &q
default:
return fmt.Errorf("unsupported query type: %d", queryType)
}
return nil
}
// Validate does basic validation on a per chain query request.
func (perChainQuery *PerChainQueryRequest) Validate() error {
str := perChainQuery.ChainId.String()
if _, err := vaa.ChainIDFromString(str); err != nil {
return fmt.Errorf("invalid chainID: %d", uint16(perChainQuery.ChainId))
}
if perChainQuery.Query == nil {
return fmt.Errorf("query is nil")
}
if err := ValidatePerChainQueryRequestType(perChainQuery.Query.Type()); err != nil {
return err
}
if err := perChainQuery.Query.Validate(); err != nil {
return fmt.Errorf("chain specific query is invalid: %w", err)
}
return nil
}
func ValidatePerChainQueryRequestType(qt ChainSpecificQueryType) error {
if qt != EthCallQueryRequestType && qt != EthCallByTimestampQueryRequestType && qt != EthCallWithFinalityQueryRequestType &&
qt != SolanaAccountQueryRequestType && qt != SolanaPdaQueryRequestType {
return fmt.Errorf("invalid query request type: %d", qt)
}
return nil
}
// Equal verifies that two query requests are equal.
func (left *PerChainQueryRequest) Equal(right *PerChainQueryRequest) bool {
if left.ChainId != right.ChainId {
return false
}
if left.Query == nil && right.Query == nil {
return true
}
if left.Query == nil || right.Query == nil {
return false
}
if left.Query.Type() != right.Query.Type() {
return false
}
switch leftQuery := left.Query.(type) {
case *EthCallQueryRequest:
switch rightQuery := right.Query.(type) {
case *EthCallQueryRequest:
return leftQuery.Equal(rightQuery)
default:
panic("unsupported query type on right, must be eth_call")
}
case *EthCallByTimestampQueryRequest:
switch rightQuery := right.Query.(type) {
case *EthCallByTimestampQueryRequest:
return leftQuery.Equal(rightQuery)
default:
panic("unsupported query type on right, must be eth_call_by_timestamp")
}
case *EthCallWithFinalityQueryRequest:
switch rightQuery := right.Query.(type) {
case *EthCallWithFinalityQueryRequest:
return leftQuery.Equal(rightQuery)
default:
panic("unsupported query type on right, must be eth_call_with_finality")
}
case *SolanaAccountQueryRequest:
switch rightQuery := right.Query.(type) {
case *SolanaAccountQueryRequest:
return leftQuery.Equal(rightQuery)
default:
panic("unsupported query type on right, must be sol_account")
}
case *SolanaPdaQueryRequest:
switch rightQuery := right.Query.(type) {
case *SolanaPdaQueryRequest:
return leftQuery.Equal(rightQuery)
default:
panic("unsupported query type on right, must be sol_pda")
}
default:
panic("unsupported query type on left")
}
}
//
// Implementation of EthCallQueryRequest, which implements the ChainSpecificQuery interface.
//
func (e *EthCallQueryRequest) Type() ChainSpecificQueryType {
return EthCallQueryRequestType
}
// Marshal serializes the binary representation of an EVM eth_call request.
// This method calls Validate() and relies on it to range checks lengths, etc.
func (ecd *EthCallQueryRequest) Marshal() ([]byte, error) {
if err := ecd.Validate(); err != nil {
return nil, err
}
buf := new(bytes.Buffer)
vaa.MustWrite(buf, binary.BigEndian, uint32(len(ecd.BlockId)))
buf.Write([]byte(ecd.BlockId))
vaa.MustWrite(buf, binary.BigEndian, uint8(len(ecd.CallData)))
for _, callData := range ecd.CallData {
buf.Write(callData.To)
vaa.MustWrite(buf, binary.BigEndian, uint32(len(callData.Data)))
buf.Write(callData.Data)
}
return buf.Bytes(), nil
}
// Unmarshal deserializes an EVM eth_call query from a byte array
func (ecd *EthCallQueryRequest) Unmarshal(data []byte) error {
reader := bytes.NewReader(data[:])
return ecd.UnmarshalFromReader(reader)
}
// UnmarshalFromReader deserializes an EVM eth_call query from a byte array
func (ecd *EthCallQueryRequest) UnmarshalFromReader(reader *bytes.Reader) error {
blockIdLen := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &blockIdLen); err != nil {
return fmt.Errorf("failed to read block id len: %w", err)
}
blockId := make([]byte, blockIdLen)
if n, err := reader.Read(blockId[:]); err != nil || n != int(blockIdLen) {
return fmt.Errorf("failed to read block id [%d]: %w", n, err)
}
ecd.BlockId = string(blockId[:])
numCallData := uint8(0)
if err := binary.Read(reader, binary.BigEndian, &numCallData); err != nil {
return fmt.Errorf("failed to read number of call data entries: %w", err)
}
for count := 0; count < int(numCallData); count++ {
to := [EvmContractAddressLength]byte{}
if n, err := reader.Read(to[:]); err != nil || n != EvmContractAddressLength {
return fmt.Errorf("failed to read call To [%d]: %w", n, err)
}
dataLen := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &dataLen); err != nil {
return fmt.Errorf("failed to read call Data len: %w", err)
}
data := make([]byte, dataLen)
if n, err := reader.Read(data[:]); err != nil || n != int(dataLen) {
return fmt.Errorf("failed to read call data [%d]: %w", n, err)
}
callData := &EthCallData{
To: to[:],
Data: data[:],
}
ecd.CallData = append(ecd.CallData, callData)
}
return nil
}
// Validate does basic validation on an EVM eth_call query.
func (ecd *EthCallQueryRequest) Validate() error {
if len(ecd.BlockId) > math.MaxUint32 {
return fmt.Errorf("block id too long")
}
if !strings.HasPrefix(ecd.BlockId, "0x") {
return fmt.Errorf("block id must be a hex number or hash starting with 0x")
}
if len(ecd.CallData) <= 0 {
return fmt.Errorf("does not contain any call data")
}
if len(ecd.CallData) > math.MaxUint8 {
return fmt.Errorf("too many call data entries")
}
for _, callData := range ecd.CallData {
if callData.To == nil || len(callData.To) <= 0 {
return fmt.Errorf("no call data to")
}
if len(callData.To) != EvmContractAddressLength {
return fmt.Errorf("invalid length for To contract")
}
if callData.Data == nil || len(callData.Data) <= 0 {
return fmt.Errorf("no call data data")
}
if len(callData.Data) > math.MaxUint32 {
return fmt.Errorf("call data data too long")
}
}
return nil
}
// Equal verifies that two EVM eth_call queries are equal.
func (left *EthCallQueryRequest) Equal(right *EthCallQueryRequest) bool {
if left.BlockId != right.BlockId {
return false
}
if len(left.CallData) != len(right.CallData) {
return false
}
for idx := range left.CallData {
if !bytes.Equal(left.CallData[idx].To, right.CallData[idx].To) {
return false
}
if !bytes.Equal(left.CallData[idx].Data, right.CallData[idx].Data) {
return false
}
}
return true
}
//
// Implementation of EthCallByTimestampQueryRequest, which implements the ChainSpecificQuery interface.
//
func (e *EthCallByTimestampQueryRequest) Type() ChainSpecificQueryType {
return EthCallByTimestampQueryRequestType
}
// Marshal serializes the binary representation of an EVM eth_call_by_timestamp request.
// This method calls Validate() and relies on it to range checks lengths, etc.
func (ecd *EthCallByTimestampQueryRequest) Marshal() ([]byte, error) {
if err := ecd.Validate(); err != nil {
return nil, err
}
buf := new(bytes.Buffer)
vaa.MustWrite(buf, binary.BigEndian, ecd.TargetTimestamp)
vaa.MustWrite(buf, binary.BigEndian, uint32(len(ecd.TargetBlockIdHint)))
buf.Write([]byte(ecd.TargetBlockIdHint))
vaa.MustWrite(buf, binary.BigEndian, uint32(len(ecd.FollowingBlockIdHint)))
buf.Write([]byte(ecd.FollowingBlockIdHint))
vaa.MustWrite(buf, binary.BigEndian, uint8(len(ecd.CallData)))
for _, callData := range ecd.CallData {
buf.Write(callData.To)
vaa.MustWrite(buf, binary.BigEndian, uint32(len(callData.Data)))
buf.Write(callData.Data)
}
return buf.Bytes(), nil
}
// Unmarshal deserializes an EVM eth_call_by_timestamp query from a byte array
func (ecd *EthCallByTimestampQueryRequest) Unmarshal(data []byte) error {
reader := bytes.NewReader(data[:])
return ecd.UnmarshalFromReader(reader)
}
// UnmarshalFromReader deserializes an EVM eth_call_by_timestamp query from a byte array
func (ecd *EthCallByTimestampQueryRequest) UnmarshalFromReader(reader *bytes.Reader) error {
if err := binary.Read(reader, binary.BigEndian, &ecd.TargetTimestamp); err != nil {
return fmt.Errorf("failed to read timestamp: %w", err)
}
blockIdHintLen := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &blockIdHintLen); err != nil {
return fmt.Errorf("failed to read target block id hint len: %w", err)
}
targetBlockIdHint := make([]byte, blockIdHintLen)
if n, err := reader.Read(targetBlockIdHint[:]); err != nil || n != int(blockIdHintLen) {
return fmt.Errorf("failed to read target block id hint [%d]: %w", n, err)
}
ecd.TargetBlockIdHint = string(targetBlockIdHint[:])
blockIdHintLen = uint32(0)
if err := binary.Read(reader, binary.BigEndian, &blockIdHintLen); err != nil {
return fmt.Errorf("failed to read following block id hint len: %w", err)
}
followingBlockIdHint := make([]byte, blockIdHintLen)
if n, err := reader.Read(followingBlockIdHint[:]); err != nil || n != int(blockIdHintLen) {
return fmt.Errorf("failed to read following block id hint [%d]: %w", n, err)
}
ecd.FollowingBlockIdHint = string(followingBlockIdHint[:])
numCallData := uint8(0)
if err := binary.Read(reader, binary.BigEndian, &numCallData); err != nil {
return fmt.Errorf("failed to read number of call data entries: %w", err)
}
for count := 0; count < int(numCallData); count++ {
to := [EvmContractAddressLength]byte{}
if n, err := reader.Read(to[:]); err != nil || n != EvmContractAddressLength {
return fmt.Errorf("failed to read call To [%d]: %w", n, err)
}
dataLen := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &dataLen); err != nil {
return fmt.Errorf("failed to read call Data len: %w", err)
}
data := make([]byte, dataLen)
if n, err := reader.Read(data[:]); err != nil || n != int(dataLen) {
return fmt.Errorf("failed to read call data [%d]: %w", n, err)
}
callData := &EthCallData{
To: to[:],
Data: data[:],
}
ecd.CallData = append(ecd.CallData, callData)
}
return nil
}
// Validate does basic validation on an EVM eth_call_by_timestamp query.
func (ecd *EthCallByTimestampQueryRequest) Validate() error {
if ecd.TargetTimestamp == 0 {
return fmt.Errorf("target timestamp may not be zero")
}
if len(ecd.TargetBlockIdHint) > math.MaxUint32 {
return fmt.Errorf("target block id hint too long")
}
if (ecd.TargetBlockIdHint == "") != (ecd.FollowingBlockIdHint == "") {
return fmt.Errorf("if either the target or following block id is unset, they both must be unset")
}
if ecd.TargetBlockIdHint != "" && !strings.HasPrefix(ecd.TargetBlockIdHint, "0x") {
return fmt.Errorf("target block id must be a hex number or hash starting with 0x")
}
if len(ecd.FollowingBlockIdHint) > math.MaxUint32 {
return fmt.Errorf("following block id hint too long")
}
if ecd.FollowingBlockIdHint != "" && !strings.HasPrefix(ecd.FollowingBlockIdHint, "0x") {
return fmt.Errorf("following block id must be a hex number or hash starting with 0x")
}
if len(ecd.CallData) <= 0 {
return fmt.Errorf("does not contain any call data")
}
if len(ecd.CallData) > math.MaxUint8 {
return fmt.Errorf("too many call data entries")
}
for _, callData := range ecd.CallData {
if callData.To == nil || len(callData.To) <= 0 {
return fmt.Errorf("no call data to")
}
if len(callData.To) != EvmContractAddressLength {
return fmt.Errorf("invalid length for To contract")
}
if callData.Data == nil || len(callData.Data) <= 0 {
return fmt.Errorf("no call data data")
}
if len(callData.Data) > math.MaxUint32 {
return fmt.Errorf("call data data too long")
}
}
return nil
}
// Equal verifies that two EVM eth_call_by_timestamp queries are equal.
func (left *EthCallByTimestampQueryRequest) Equal(right *EthCallByTimestampQueryRequest) bool {
if left.TargetTimestamp != right.TargetTimestamp {
return false
}
if left.TargetBlockIdHint != right.TargetBlockIdHint {
return false
}
if left.FollowingBlockIdHint != right.FollowingBlockIdHint {
return false
}
if len(left.CallData) != len(right.CallData) {
return false
}
for idx := range left.CallData {
if !bytes.Equal(left.CallData[idx].To, right.CallData[idx].To) {
return false
}
if !bytes.Equal(left.CallData[idx].Data, right.CallData[idx].Data) {
return false
}
}
return true
}
//
// Implementation of EthCallWithFinalityQueryRequest, which implements the ChainSpecificQuery interface.
//
func (e *EthCallWithFinalityQueryRequest) Type() ChainSpecificQueryType {
return EthCallWithFinalityQueryRequestType
}
// Marshal serializes the binary representation of an EVM eth_call_with_finality request.
// This method calls Validate() and relies on it to range checks lengths, etc.
func (ecd *EthCallWithFinalityQueryRequest) Marshal() ([]byte, error) {
if err := ecd.Validate(); err != nil {
return nil, err
}
buf := new(bytes.Buffer)
vaa.MustWrite(buf, binary.BigEndian, uint32(len(ecd.BlockId)))
buf.Write([]byte(ecd.BlockId))
vaa.MustWrite(buf, binary.BigEndian, uint32(len(ecd.Finality)))
buf.Write([]byte(ecd.Finality))
vaa.MustWrite(buf, binary.BigEndian, uint8(len(ecd.CallData)))
for _, callData := range ecd.CallData {
buf.Write(callData.To)
vaa.MustWrite(buf, binary.BigEndian, uint32(len(callData.Data)))
buf.Write(callData.Data)
}
return buf.Bytes(), nil
}
// Unmarshal deserializes an EVM eth_call_with_finality query from a byte array
func (ecd *EthCallWithFinalityQueryRequest) Unmarshal(data []byte) error {
reader := bytes.NewReader(data[:])
return ecd.UnmarshalFromReader(reader)
}
// UnmarshalFromReader deserializes an EVM eth_call_with_finality query from a byte array
func (ecd *EthCallWithFinalityQueryRequest) UnmarshalFromReader(reader *bytes.Reader) error {
blockIdLen := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &blockIdLen); err != nil {
return fmt.Errorf("failed to read target block id len: %w", err)
}
blockId := make([]byte, blockIdLen)
if n, err := reader.Read(blockId[:]); err != nil || n != int(blockIdLen) {
return fmt.Errorf("failed to read target block id [%d]: %w", n, err)
}
ecd.BlockId = string(blockId[:])
finalityLen := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &finalityLen); err != nil {
return fmt.Errorf("failed to read finality len: %w", err)
}
finality := make([]byte, finalityLen)
if n, err := reader.Read(finality[:]); err != nil || n != int(finalityLen) {
return fmt.Errorf("failed to read finality [%d]: %w", n, err)
}
ecd.Finality = string(finality[:])
numCallData := uint8(0)
if err := binary.Read(reader, binary.BigEndian, &numCallData); err != nil {
return fmt.Errorf("failed to read number of call data entries: %w", err)
}
for count := 0; count < int(numCallData); count++ {
to := [EvmContractAddressLength]byte{}
if n, err := reader.Read(to[:]); err != nil || n != EvmContractAddressLength {
return fmt.Errorf("failed to read call To [%d]: %w", n, err)
}
dataLen := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &dataLen); err != nil {
return fmt.Errorf("failed to read call Data len: %w", err)
}
data := make([]byte, dataLen)
if n, err := reader.Read(data[:]); err != nil || n != int(dataLen) {
return fmt.Errorf("failed to read call data [%d]: %w", n, err)
}
callData := &EthCallData{
To: to[:],
Data: data[:],
}
ecd.CallData = append(ecd.CallData, callData)
}
return nil
}
// Validate does basic validation on an EVM eth_call_with_finality query.
func (ecd *EthCallWithFinalityQueryRequest) Validate() error {
if len(ecd.BlockId) > math.MaxUint32 {
return fmt.Errorf("block id too long")
}
if ecd.BlockId == "" {
return fmt.Errorf("block id is required")
}
if !strings.HasPrefix(ecd.BlockId, "0x") {
return fmt.Errorf("block id must be a hex number or hash starting with 0x")
}
if len(ecd.Finality) > math.MaxUint32 {
return fmt.Errorf("finality too long")
}
if ecd.Finality == "" {
return fmt.Errorf("finality is required")
}
if ecd.Finality != "finalized" && ecd.Finality != "safe" {
return fmt.Errorf(`finality must be "finalized" or "safe", is "%s"`, ecd.Finality)
}
if len(ecd.CallData) <= 0 {
return fmt.Errorf("does not contain any call data")
}
if len(ecd.CallData) > math.MaxUint8 {
return fmt.Errorf("too many call data entries")
}
for _, callData := range ecd.CallData {
if callData.To == nil || len(callData.To) <= 0 {
return fmt.Errorf("no call data to")
}
if len(callData.To) != EvmContractAddressLength {
return fmt.Errorf("invalid length for To contract")
}
if callData.Data == nil || len(callData.Data) <= 0 {
return fmt.Errorf("no call data data")
}
if len(callData.Data) > math.MaxUint32 {
return fmt.Errorf("call data data too long")
}
}
return nil
}
// Equal verifies that two EVM eth_call_with_finality queries are equal.
func (left *EthCallWithFinalityQueryRequest) Equal(right *EthCallWithFinalityQueryRequest) bool {
if left.BlockId != right.BlockId {
return false
}
if left.Finality != right.Finality {
return false
}
if len(left.CallData) != len(right.CallData) {
return false
}
for idx := range left.CallData {
if !bytes.Equal(left.CallData[idx].To, right.CallData[idx].To) {
return false
}
if !bytes.Equal(left.CallData[idx].Data, right.CallData[idx].Data) {
return false
}
}
return true
}
//
// Implementation of SolanaAccountQueryRequest, which implements the ChainSpecificQuery interface.
//
func (e *SolanaAccountQueryRequest) Type() ChainSpecificQueryType {