forked from Layr-Labs/eigenpod-proofs-generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkle_util_test.go
1065 lines (846 loc) · 35.2 KB
/
merkle_util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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 eigenpodproofs
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"log"
"os"
"testing"
"github.com/ethereum/go-ethereum/ethclient"
ssz "github.com/ferranbt/fastssz"
"github.com/stretchr/testify/assert"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
beacon "github.com/Layr-Labs/eigenpod-proofs-generation/beacon"
contractBeaconChainProofs "github.com/Layr-Labs/eigenpod-proofs-generation/bindings"
epgcommon "github.com/Layr-Labs/eigenpod-proofs-generation/common"
common "github.com/ethereum/go-ethereum/common"
)
var (
ctx context.Context
oracleState deneb.BeaconState
oracleBlockHeader phase0.BeaconBlockHeader
blockHeader phase0.BeaconBlockHeader
blockHeaderIndex uint64
block deneb.BeaconBlock
validatorIndex phase0.ValidatorIndex
beaconBlockHeaderToVerifyIndex uint64
executionPayload deneb.ExecutionPayload
epp *EigenPodProofs
executionPayloadFieldRoots []phase0.Root
chainClient *ChainClient
beaconChainProofs *contractBeaconChainProofs.BeaconChainProofsTest
)
// var VALIDATOR_INDEX uint64 = 61068 //this is the index of a validator that has a partial withdrawal
var VALIDATOR_INDEX uint64 = 61336 //this is the index of a validator that has a full withdrawal.
var REPOINTED_VALIDATOR_INDEX uint64 = 61511 //this is the index of a validator that we use for the withdrawal credential proofs
// this needs to be hand crafted. If you want the root of the header at the slot x,
// then look for entry in (x)%slotsPerHistoricalRoot in the block_roots.
// var BEACON_BLOCK_HEADER_TO_VERIFY_INDEX uint64 = 656
var BEACON_BLOCK_HEADER_TO_VERIFY_INDEX uint64 = 2262
const DENEB_FORK_TIMESTAMP_GOERLI = uint64(1705473120)
var GOERLI_CHAIN_ID uint64 = 5
func TestMain(m *testing.M) {
// Setup
log.Println("Setting up suite")
setupSuite()
// Run tests
code := m.Run()
// Teardown
log.Println("Tearing down suite")
teardownSuite()
// Exit with test result code
os.Exit(code)
}
func setupSuite() {
log.Println("Setting up suite")
rpc := "https://rpc.ankr.com/eth_goerli"
privateKey := os.Getenv("PRIVATE_KEY")
ethClient, err := ethclient.Dial(rpc)
if err != nil {
log.Panicf("failed to connect to the Ethereum client: %s", err)
}
chainClient, err = NewChainClient(ethClient, privateKey)
if err != nil {
log.Panicf("failed to create chain client: %s", err)
}
//BeaconChainProofs.sol deployment: https://goerli.etherscan.io/address/0xd132dD701d3980bb5d66A21e2340f263765e4a19#code
contractAddress := common.HexToAddress("0xd132dD701d3980bb5d66A21e2340f263765e4a19")
beaconChainProofs, err = contractBeaconChainProofs.NewBeaconChainProofsTest(contractAddress, chainClient)
if err != nil {
log.Panicf("failed to create beacon chain proofs contract: %s", err)
}
stateFile := "data/deneb_goerli_slot_7413760.json"
oracleHeaderFile := "data/deneb_goerli_block_header_7413760.json"
headerFile := "data/deneb_goerli_block_header_7426113.json"
bodyFile := "data/deneb_goerli_block_7426113.json"
//ParseCapellaBeaconState(stateFile)
stateJSON, err := ParseJSONFile(stateFile)
if err != nil {
fmt.Println("error with JSON parsing beacon state")
}
ParseDenebBeaconStateFromJSON(*stateJSON, &oracleState)
blockHeader, err = ExtractBlockHeader(headerFile)
if err != nil {
fmt.Println("error with block header", err)
}
oracleBlockHeader, err = ExtractBlockHeader(oracleHeaderFile)
if err != nil {
fmt.Println("error with oracle block header", err)
}
block, err = ExtractBlockDeneb(bodyFile)
if err != nil {
fmt.Println("error with block body", err)
}
executionPayload = *block.Body.ExecutionPayload
blockHeaderIndex = uint64(blockHeader.Slot) % beacon.SlotsPerHistoricalRoot
epp, err = NewEigenPodProofs(GOERLI_CHAIN_ID, 1000)
if err != nil {
fmt.Println("error in NewEigenPodProofs", err)
}
epp.ComputeBeaconStateTopLevelRoots(&spec.VersionedBeaconState{Deneb: &oracleState})
epp.ComputeBeaconStateRoot(&oracleState)
executionPayloadFieldRoots, _ = beacon.ComputeExecutionPayloadFieldRootsDeneb(block.Body.ExecutionPayload)
}
func teardownSuite() {
// Any cleanup you want to perform should go here
fmt.Println("all done!")
}
// verifies that the "ProveValidatorContainers" call, which the backend calls, returns valid proofs
func TestProveValidatorContainers(t *testing.T) {
versionedOracleState, err := beacon.CreateVersionedState(&oracleState)
if err != nil {
fmt.Println("error", err)
return
}
verifyValidatorFieldsCallParams, err := epp.ProveValidatorContainers(&oracleBlockHeader, &versionedOracleState, []uint64{VALIDATOR_INDEX})
if err != nil {
fmt.Println("error", err)
}
stateRootProof := verifyValidatorFieldsCallParams.StateRootProof
validatorFieldsProofs := verifyValidatorFieldsCallParams.ValidatorFieldsProofs
validatorIndices := verifyValidatorFieldsCallParams.ValidatorIndices
flag := verifyStateRootAgainstBlockHeaderProof(oracleBlockHeader, oracleState, stateRootProof.StateRootProof)
assert.True(t, flag, "State Root Proof %v failed")
flag = verifyValidatorAgainstBeaconState(&oracleState, validatorFieldsProofs[0], validatorIndices[0])
assert.True(t, flag, "State Root Proof %v failed")
}
func TestProveWithdrawals(t *testing.T) {
oracleStateFile := "data/deneb_goerli_slot_7431952.json"
oracleHeaderFile := "data/deneb_goerli_block_header_7431952.json"
oracleStateJSON, err := ParseJSONFile(oracleStateFile)
if err != nil {
fmt.Println("error with JSON parsing beacon state")
}
oracleBlockHeader, err = ExtractBlockHeader(oracleHeaderFile)
if err != nil {
fmt.Println("error with block header", err)
}
ParseDenebBeaconStateFromJSON(*oracleStateJSON, &oracleState)
versionedOracleState, err := beacon.CreateVersionedState(&oracleState)
if err != nil {
fmt.Println("error creating versioned state", err)
return
}
historicalSummaryStateJSON, err := ParseJSONFile("data/deneb_goerli_slot_7421952.json")
if err != nil {
fmt.Println("error parsing historicalSummaryState JSON")
}
var historicalSummaryState deneb.BeaconState
ParseDenebBeaconStateFromJSON(*historicalSummaryStateJSON, &historicalSummaryState)
historicalSummaryStateBlockRoots := historicalSummaryState.BlockRoots
withdrawalBlock, err := ExtractBlockDeneb("data/deneb_goerli_block_7421951.json")
if err != nil {
fmt.Println("block.UnmarshalJSON error", err)
}
withdrawalBlockHeader, err := ExtractBlockHeader("data/deneb_goerli_block_header_7421951.json")
if err != nil {
fmt.Println("blockHeader.UnmarshalJSON error", err)
}
versionedWithdrawalBlock, err := beacon.CreateVersionedSignedBlock(withdrawalBlock)
if err != nil {
fmt.Println("error", err)
return
}
withdrawalIndex := uint64(0)
withdrawalValidatorIndex := uint64(627559) //this is the index of the validator with the first withdrawal in the withdrawalBlock 7421951
verifyAndProcessWithdrawalCallParams, err := epp.ProveWithdrawals(
&oracleBlockHeader,
&versionedOracleState,
[][]phase0.Root{historicalSummaryStateBlockRoots},
[]*spec.VersionedSignedBeaconBlock{&versionedWithdrawalBlock},
[]uint64{withdrawalValidatorIndex},
)
if err != nil {
fmt.Println("error", err)
}
executionPayloadRoot, err := withdrawalBlock.Body.ExecutionPayload.HashTreeRoot()
if err != nil {
fmt.Println("error", err)
}
oracleStateRoot, err := oracleState.HashTreeRoot()
if err != nil {
fmt.Println("error", err)
return
}
flag := verifyStateRootAgainstBlockHeaderProof(oracleBlockHeader, oracleState, verifyAndProcessWithdrawalCallParams.StateRootProof.StateRootProof)
assert.True(t, flag, "State Root Proof %v failed")
flag = verifyValidatorAgainstBeaconState(&oracleState, verifyAndProcessWithdrawalCallParams.ValidatorFieldsProofs[0], withdrawalValidatorIndex)
assert.True(t, flag, "Validator Fields Proof %v failed")
flag = verifyWithdrawalAgainstExecutionPayload(executionPayloadRoot, verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].WithdrawalProof, withdrawalIndex, withdrawalBlock.Body.ExecutionPayload.Withdrawals[0])
assert.True(t, flag, "Withdrawal Proof %v failed")
flag = verifyTimestampAgainstExecutionPayload(executionPayloadRoot, verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].TimestampProof, withdrawalBlock.Body.ExecutionPayload.Timestamp)
assert.True(t, flag, "Timestamp Proof %v failed")
flag = verifyBlockRootAgainstBeaconStateViaHistoricalSummaries(
oracleStateRoot,
verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].HistoricalSummaryBlockRootProof,
withdrawalBlockHeader,
verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].BlockRootIndex,
verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].HistoricalSummaryIndex,
)
assert.True(t, flag, "Historical Summary Block Root Proof %v failed")
}
func TestUnmarshalSSZVersionedBeaconStateDeneb(t *testing.T) {
oracleStateBytes, err := oracleState.MarshalSSZ()
if err != nil {
fmt.Println("error", err)
}
versionedBeaconState, err := beacon.UnmarshalSSZVersionedBeaconState(oracleStateBytes)
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedBeaconState.Version, spec.DataVersionDeneb, "Version %v failed")
versionedBeaconStateBytes, err := versionedBeaconState.Deneb.MarshalSSZ()
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedBeaconStateBytes, oracleStateBytes, "Version %v failed")
assert.Nil(t, err, "Error %v failed")
}
func TestUnmarshalSSZVersionedBeaconStateCapella(t *testing.T) {
var capellaState capella.BeaconState
capellaStateJSON, err := ParseJSONFileCapella("data/goerli_slot_6409723.json")
if err != nil {
fmt.Println("error", err)
}
ParseCapellaBeaconStateFromJSON(*capellaStateJSON, &capellaState)
capellaStateBytes, err := capellaState.MarshalSSZ()
if err != nil {
fmt.Println("error", err)
}
versionedBeaconState, err := beacon.UnmarshalSSZVersionedBeaconState(capellaStateBytes)
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedBeaconState.Version, spec.DataVersionCapella, "Version %v failed")
versionedBeaconStateBytes, err := versionedBeaconState.Capella.MarshalSSZ()
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedBeaconStateBytes, capellaStateBytes, "Version %v failed")
assert.Nil(t, err, "Error %v failed")
}
func TestMarshalSSZVersionedBeaconStateDeneb(t *testing.T) {
oracleStateBytes, err := oracleState.MarshalSSZ()
if err != nil {
fmt.Println("error", err)
}
versionedBeaconState, err := beacon.CreateVersionedState(&oracleState)
if err != nil {
fmt.Println("error", err)
}
versionedBeaconStateBytes, err := beacon.MarshalSSZVersionedBeaconState(versionedBeaconState)
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedBeaconStateBytes, oracleStateBytes, "Version %v failed")
assert.Nil(t, err, "Error %v failed")
}
func TestMarshalSSZVersionedBeaconStateCapella(t *testing.T) {
var capellaState capella.BeaconState
capellaStateJSON, err := ParseJSONFileCapella("data/goerli_slot_6409723.json")
if err != nil {
fmt.Println("error", err)
}
ParseCapellaBeaconStateFromJSON(*capellaStateJSON, &capellaState)
capellaStateBytes, err := capellaState.MarshalSSZ()
if err != nil {
fmt.Println("error", err)
}
versionedBeaconState, err := beacon.CreateVersionedState(&capellaState)
if err != nil {
fmt.Println("error", err)
}
versionedBeaconStateBytes, err := beacon.MarshalSSZVersionedBeaconState(versionedBeaconState)
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedBeaconStateBytes, capellaStateBytes, "Version %v failed")
assert.Nil(t, err, "Error %v failed")
}
func TestGenerateWithdrawalCredentialsProof(t *testing.T) {
// picking up one random validator index
validatorIndex := phase0.ValidatorIndex(REPOINTED_VALIDATOR_INDEX)
beaconStateTopLevelRoots, err := beacon.ComputeBeaconStateTopLevelRootsDeneb(&oracleState)
if err != nil {
fmt.Println("error reading beaconStateTopLevelRoots")
}
proof, err := epp.ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots, oracleState.Slot, oracleState.Validators, uint64(validatorIndex))
if err != nil {
fmt.Println(err)
}
flag := verifyValidatorAgainstBeaconState(&oracleState, proof, uint64(validatorIndex))
assert.True(t, flag, "Proof %v failed")
}
func TestCreateVersionedSignedBlockDeneb(t *testing.T) {
block := deneb.BeaconBlock{}
versionedBlock, err := beacon.CreateVersionedSignedBlock(block)
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedBlock.Version, spec.DataVersionDeneb, "Version %v failed")
assert.Nil(t, err, "Error %v failed")
}
func TestCreateVersionedSignedBlockCapella(t *testing.T) {
block := capella.BeaconBlock{}
versionedBlock, err := beacon.CreateVersionedSignedBlock(block)
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedBlock.Version, spec.DataVersionCapella, "Version %v failed")
assert.Nil(t, err, "Error %v failed")
}
func TestCreateVersionedSignedBlockAltair(t *testing.T) {
block := altair.BeaconBlock{}
_, err := beacon.CreateVersionedSignedBlock(block)
if err != nil {
fmt.Println("error", err)
}
assert.NotNil(t, err, "error %v was nil")
}
func TestCreateVersionedBeaconStateDeneb(t *testing.T) {
oracleState := deneb.BeaconState{}
versionedState, err := beacon.CreateVersionedState(&oracleState)
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedState.Version, spec.DataVersionDeneb, "Version %v failed")
assert.Nil(t, err, "Error %v failed")
}
func TestCreateVersionedBeaconStateCapella(t *testing.T) {
state := capella.BeaconState{}
versionedState, err := beacon.CreateVersionedState(&state)
if err != nil {
fmt.Println("error", err)
}
assert.Equal(t, versionedState.Version, spec.DataVersionCapella, "Version %v failed")
assert.Nil(t, err, "Error %v failed")
}
func TestCreateVersionedBeaconStateAltair(t *testing.T) {
state := altair.BeaconState{}
_, err := beacon.CreateVersionedState(&state)
if err != nil {
fmt.Println("error", err)
}
assert.NotNil(t, err, "error %v was nil")
}
func TestProveValidatorBalanceAgainstValidatorBalanceList(t *testing.T) {
validatorIndex := phase0.ValidatorIndex(REPOINTED_VALIDATOR_INDEX)
proof, _ := beacon.ProveValidatorBalanceAgainstValidatorBalanceList(oracleState.Balances, uint64(validatorIndex))
beaconStateTopLevelRoots, _ := beacon.ComputeBeaconStateTopLevelRootsDeneb(&oracleState)
root := beaconStateTopLevelRoots.BalancesRoot
balanceRootList, err := beacon.GetBalanceRoots(oracleState.Balances)
if err != nil {
fmt.Println("error", err)
}
balanceIndex := validatorIndex / 4
leaf := balanceRootList[balanceIndex]
flag := epgcommon.ValidateProof(*root, proof, leaf, uint64(balanceIndex))
if flag != true {
fmt.Println("balance proof failed")
}
assert.True(t, flag, "Proof %v failed")
}
func TestProveBeaconTopLevelRootAgainstBeaconState(t *testing.T) {
// get the oracle state root for a merkle tree with top level roots as the leaves
beaconStateTopLevelRoots, err := beacon.ComputeBeaconStateTopLevelRootsDeneb(&oracleState)
if err != nil {
fmt.Println("error")
}
// compute the Merkle proof for the inclusion of Validators Root as a leaf
validatorsRootProof, err := beacon.ProveBeaconTopLevelRootAgainstBeaconState(beaconStateTopLevelRoots, beacon.ValidatorListIndex)
if err != nil {
fmt.Println("error")
}
// getting Merkle root of the BeaconStateRoot Merkle tree from attestation's code
beaconStateRoot, err := oracleState.HashTreeRoot()
if err != nil {
fmt.Println("error")
}
// validation of the proof
// get the leaf denoting the validatorsRoot in the BeaconStateRoot Merkle tree
leaf := beaconStateTopLevelRoots.ValidatorsRoot
flag := epgcommon.ValidateProof(beaconStateRoot, validatorsRootProof, *leaf, beacon.ValidatorListIndex)
if flag != true {
fmt.Println("error")
}
// fmt.Println("flag", flag)
assert.True(t, flag, "Proof %v failed\n")
}
func TestGetHistoricalSummariesBlockRootsProofProof(t *testing.T) {
//curl -H "Accept: application/json" https://data.spiceai.io/goerli/beacon/eth/v2/debug/beacon/states/7431952 -o deneb_goerli_slot_7431952.json --header 'X-API-Key: 343035|8b6ddd9b31f54c07b3fc18282b30f61c'
currentBeaconStateJSON, err := ParseJSONFile("data/deneb_goerli_slot_7431952.json")
if err != nil {
fmt.Println("error parsing currentBeaconStateJSON")
}
//this is not the beacon state of the slot containing the old withdrawal we want to prove but rather
// its the state that was merkleized to create a historical summary containing the slot that has that withdrawal
//, ie, 7421952 mod 8192 = 0 and 7421952 - 7421951 < 8192
oldBeaconStateJSON, err := ParseJSONFile("data/deneb_goerli_slot_7421952.json")
if err != nil {
fmt.Println("error parsing oldBeaconStateJSON")
}
var blockHeader phase0.BeaconBlockHeader
blockHeader, err = ExtractBlockHeader("data/deneb_goerli_block_header_7421951.json")
if err != nil {
fmt.Println("blockHeader.UnmarshalJSON error", err)
}
var currentBeaconState deneb.BeaconState
var oldBeaconState deneb.BeaconState
ParseDenebBeaconStateFromJSON(*currentBeaconStateJSON, ¤tBeaconState)
ParseDenebBeaconStateFromJSON(*oldBeaconStateJSON, &oldBeaconState)
currentBeaconStateTopLevelRoots, _ := beacon.ComputeBeaconStateTopLevelRootsDeneb(¤tBeaconState)
if err != nil {
fmt.Println("error")
}
historicalSummaryIndex := uint64(271) //7421951 - FIRST_CAPELLA_SLOT_GOERLI // 8192
beaconBlockHeaderToVerifyIndex = 8191 //(7421951 mod 8192)
if err != nil {
fmt.Println("error", err)
}
oldBlockRoots := oldBeaconState.BlockRoots
historicalSummaryBlockHeaderProof, err := beacon.ProveBlockRootAgainstBeaconStateViaHistoricalSummaries(
currentBeaconStateTopLevelRoots,
currentBeaconState.HistoricalSummaries,
oldBlockRoots,
historicalSummaryIndex,
beaconBlockHeaderToVerifyIndex,
)
if err != nil {
fmt.Println("error")
}
currentBeaconStateRoot, _ := currentBeaconState.HashTreeRoot()
flag := verifyBlockRootAgainstBeaconStateViaHistoricalSummaries(currentBeaconStateRoot, historicalSummaryBlockHeaderProof, blockHeader, beaconBlockHeaderToVerifyIndex, historicalSummaryIndex)
assert.True(t, flag, "Proof %v failed\n")
}
func TestGetHistoricalSummariesBlockRootsProofProofCapellaAgainstDeneb(t *testing.T) {
//curl -H "Accept: application/json" https://data.spiceai.io/goerli/beacon/eth/v2/debug/beacon/states/7431952 -o deneb_goerli_slot_7431952.json --header 'X-API-Key: 343035|8b6ddd9b31f54c07b3fc18282b30f61c'
currentBeaconStateJSON, err := ParseJSONFile("data/deneb_goerli_slot_7431952.json")
if err != nil {
fmt.Println("error parsing currentBeaconStateJSON")
}
//this is not the beacon state of the slot containing the old withdrawal we want to proof but rather
// its the state that was merklized to create a historical summary containing the slot that has that withdrawal, ie, 7421952 mod 8192 = 0
oldBeaconStateJSON, err := ParseJSONFileCapella("data/goerli_slot_6397952.json")
if err != nil {
fmt.Println("error parsing oldBeaconStateJSON", err)
}
var blockHeader phase0.BeaconBlockHeader
//blockHeader, err = ExtractBlockHeader("data/goerli_block_header_6397852.json")
blockHeader, err = ExtractBlockHeader("data/goerli_block_header_6397852.json")
if err != nil {
fmt.Println("blockHeader.UnmarshalJSON error", err)
}
var currentBeaconState deneb.BeaconState
var oldBeaconState capella.BeaconState
ParseDenebBeaconStateFromJSON(*currentBeaconStateJSON, ¤tBeaconState)
ParseCapellaBeaconStateFromJSON(*oldBeaconStateJSON, &oldBeaconState)
currentBeaconStateTopLevelRoots, _ := beacon.ComputeBeaconStateTopLevelRootsDeneb(¤tBeaconState)
//oldBeaconStateTopLevelRoots, _ := ComputeBeaconStateTopLevelRoots(&oldBeaconState)
if err != nil {
fmt.Println("error")
}
historicalSummaryIndex := uint64(146)
beaconBlockHeaderToVerifyIndex = 8092 //(7421951 mod 8192)
if err != nil {
fmt.Println("error", err)
}
oldBlockRoots := oldBeaconState.BlockRoots
historicalSummaryBlockHeaderProof, err := beacon.ProveBlockRootAgainstBeaconStateViaHistoricalSummaries(
currentBeaconStateTopLevelRoots,
currentBeaconState.HistoricalSummaries,
oldBlockRoots,
historicalSummaryIndex,
beaconBlockHeaderToVerifyIndex,
)
if err != nil {
fmt.Println("error")
}
currentBeaconStateRoot, _ := currentBeaconState.HashTreeRoot()
flag := verifyBlockRootAgainstBeaconStateViaHistoricalSummaries(currentBeaconStateRoot, historicalSummaryBlockHeaderProof, blockHeader, beaconBlockHeaderToVerifyIndex, historicalSummaryIndex)
assert.True(t, flag, "Proof %v failed\n")
}
func TestProveValidatorAgainstValidatorList(t *testing.T) {
epp.ComputeValidatorTree(oracleState.Slot, oracleState.Validators)
// picking up one random validator index
validatorIndex := phase0.ValidatorIndex(10000)
// get the validators field
validators := oracleState.Validators
// get the Merkle proof for inclusion
validatorProof, err := epp.ProveValidatorAgainstValidatorList(0, validators, uint64(validatorIndex))
if err != nil {
fmt.Println("error")
}
// verify the proof
// get the leaf corresponding to validatorIndex
leaf, err := validators[validatorIndex].HashTreeRoot()
if err != nil {
fmt.Println("error")
}
// get the oracle state root for a merkle tree with top level roots as the leaves
beaconStateTopLevelRoots, err := beacon.ComputeBeaconStateTopLevelRootsDeneb(&oracleState)
if err != nil {
fmt.Println("error")
}
// calling the proof verification func
flag := epgcommon.ValidateProof(*beaconStateTopLevelRoots.ValidatorsRoot, validatorProof, leaf, uint64(validatorIndex))
if flag != true {
fmt.Println("error")
}
// fmt.Println("flag", flag)
assert.True(t, flag, "Proof %v failed\n")
}
func TestComputeBlockSlotProof(t *testing.T) {
// get the proof for slot in the block header
blockHeaderSlotProof, err := beacon.ProveSlotAgainstBlockHeader(&blockHeader)
if err != nil {
fmt.Println("error", err)
}
// get the hash of the slot - this will be the leaf of the merkle tree
var slotHashRoot phase0.Root
hh := ssz.NewHasher()
hh.PutUint64(uint64(blockHeader.Slot))
copy(slotHashRoot[:], hh.Hash())
// get the block header root which will be used as a root of the Merkle tree
// Note that the blockHeader was obtained from the actual Block header
beaconBlockHeaderRoot, err := blockHeader.HashTreeRoot()
if err != nil {
fmt.Println("error:", err)
}
// calling the proof verification function
flag := epgcommon.ValidateProof(beaconBlockHeaderRoot, blockHeaderSlotProof, slotHashRoot, beacon.SlotIndex)
if flag != true {
fmt.Println("error")
}
assert.True(t, flag, "Proof %v failed\n")
}
func TestProveBlockBodyAgainstBlockHeader(t *testing.T) {
// get the proof for block body in the block header
blockHeaderBlockBodyProof, err := beacon.ProveBlockBodyAgainstBlockHeader(&blockHeader)
if err != nil {
fmt.Println("error", err)
}
// get the hash of the block body root - this will be the leaf of the merkle tree
var blockBodyHashRoot phase0.Root
hh := ssz.NewHasher()
hh.PutBytes(blockHeader.BodyRoot[:])
copy(blockBodyHashRoot[:], hh.Hash())
// get the block header root which will be used as a root of the Merkle tree
// Note that the blockHeader was obtained from the actual Block header
beaconBlockHeaderRoot, err := blockHeader.HashTreeRoot()
if err != nil {
fmt.Println("error:", err)
}
// calling the proof verification function
flag := epgcommon.ValidateProof(beaconBlockHeaderRoot, blockHeaderBlockBodyProof, blockBodyHashRoot, beacon.BeaconBlockBodyRootIndex)
if flag != true {
fmt.Println("error")
}
assert.True(t, flag, "Proof %v failed\n")
}
func TestComputeExecutionPayloadHeader(t *testing.T) {
// get the proof for execution payload in the block body
beaconBlockBodyProof, _, err := beacon.ProveExecutionPayloadAgainstBlockBodyDeneb(block.Body)
if err != nil {
fmt.Println("error", err)
}
// get the hash root of the actual execution payload
var executionPayloadHashRoot phase0.Root
hh := ssz.NewHasher()
{
if err = block.Body.ExecutionPayload.HashTreeRootWith(hh); err != nil {
fmt.Println("error", err)
}
copy(executionPayloadHashRoot[:], hh.Hash())
}
// get the body root in the beacon block header - will be used as the Merkle root
blockHeaderBodyRoot := blockHeader.BodyRoot
// calling the proof verification function
flag := epgcommon.ValidateProof(blockHeaderBodyRoot, beaconBlockBodyProof, executionPayloadHashRoot, beacon.ExecutionPayloadIndex)
if flag != true {
fmt.Println("error")
}
assert.True(t, flag, "Proof %v failed\n")
}
func TestStateRootAgainstLatestBlockHeaderProof(t *testing.T) {
// this is the state where the latest block header from the oracle was taken. This is the next slot after
// the state we want to prove things about (remember latestBlockHeader.state_root = previous slot's state root)
// oracleStateJSON, err := ParseJSONFile("data/historical_summary_proof/goerli_slot_6399999.json")
// var oracleState deneb.BeaconState
// ParseCapellaBeaconStateFromJSON(*oracleStateJSON, &oracleState)
var blockHeader phase0.BeaconBlockHeader
blockHeader, err := ExtractBlockHeader("data/deneb_goerli_block_header_7413760.json")
if err != nil {
fmt.Println("error with block header", err)
}
//the state from the prev slot which contains shit we wanna prove about
stateToProveJSON, err := ParseJSONFile("data/deneb_goerli_slot_7413760.json")
if err != nil {
fmt.Println("error with parsing JSON state file", err)
}
var stateToProve deneb.BeaconState
ParseDenebBeaconStateFromJSON(*stateToProveJSON, &stateToProve)
proof, err := beacon.ProveStateRootAgainstBlockHeader(&blockHeader)
if err != nil {
fmt.Println("Error in generating proof", err)
}
flag := verifyStateRootAgainstBlockHeaderProof(blockHeader, stateToProve, proof)
assert.True(t, flag, "Proof %v failed")
}
func TestGetExecutionPayloadProof(t *testing.T) {
// get the proof for execution payload in the block body
executionPayloadProof, _, _ := beacon.ProveExecutionPayloadAgainstBlockHeaderDeneb(&blockHeader, block.Body)
// get the hash root of the actual execution payload
var executionPayloadHashRoot, _ = block.Body.ExecutionPayload.HashTreeRoot()
// get the body root in the beacon block header - will be used as the Merkle root
root, _ := blockHeader.HashTreeRoot()
index := beacon.BeaconBlockBodyRootIndex<<(beacon.BlockBodyMerkleSubtreeNumLayers) | beacon.ExecutionPayloadIndex
// calling the proof verification function
flag := epgcommon.ValidateProof(root, executionPayloadProof, executionPayloadHashRoot, index)
if flag != true {
fmt.Println("error")
}
assert.True(t, flag, "Proof %v failed")
}
func TestComputeWithdrawalsListProof(t *testing.T) {
withdrawalsListProof, err := beacon.ProveWithdrawalListAgainstExecutionPayload(executionPayloadFieldRoots)
if err != nil {
fmt.Println("error!", err)
}
var withdrawalsHashRoot phase0.Root
hh := ssz.NewHasher()
{
subIndx := hh.Index()
num := uint64(len(block.Body.ExecutionPayload.Withdrawals))
if num > 16 {
err := ssz.ErrIncorrectListSize
fmt.Println("error!", err)
}
for _, elem := range block.Body.ExecutionPayload.Withdrawals {
if err = elem.HashTreeRootWith(hh); err != nil {
fmt.Println("error 4", err)
}
}
hh.MerkleizeWithMixin(subIndx, num, 16)
copy(withdrawalsHashRoot[:], hh.Hash())
hh.Reset()
}
var executionPayloadHashRoot phase0.Root
{
if err = block.Body.ExecutionPayload.HashTreeRootWith(hh); err != nil {
fmt.Println("error hel", err)
}
copy(executionPayloadHashRoot[:], hh.Hash())
}
flag := epgcommon.ValidateProof(executionPayloadHashRoot, withdrawalsListProof, withdrawalsHashRoot, beacon.WithdrawalsIndex)
if flag != true {
fmt.Println("Proof Failed")
}
assert.True(t, flag, "Proof %v failed\n")
}
func TestComputeIndividualWithdrawalProof(t *testing.T) {
// picking up one random validator index
withdrawalIndex := uint8(0)
// get the validators field
withdrawals := block.Body.ExecutionPayload.Withdrawals
// get the Merkle proof for inclusion
withdrawalProof, err := beacon.ProveWithdrawalAgainstWithdrawalList(withdrawals, withdrawalIndex)
if err != nil {
fmt.Println("error")
}
// verify the proof
// get the leaf corresponding to validatorIndex
leaf, err := withdrawals[withdrawalIndex].HashTreeRoot()
if err != nil {
fmt.Println("error")
}
var withdrawalsHashRoot phase0.Root
hh := ssz.NewHasher()
{
subIndx := hh.Index()
num := uint64(len(block.Body.ExecutionPayload.Withdrawals))
if num > 16 {
err := ssz.ErrIncorrectListSize
fmt.Println("error", err)
}
for _, elem := range block.Body.ExecutionPayload.Withdrawals {
if err = elem.HashTreeRootWith(hh); err != nil {
fmt.Println("error", err)
}
}
hh.MerkleizeWithMixin(subIndx, num, 16)
copy(withdrawalsHashRoot[:], hh.Hash())
hh.Reset()
}
// calling the proof verification func
flag := epgcommon.ValidateProof(withdrawalsHashRoot, withdrawalProof, leaf, uint64(withdrawalIndex))
if flag != true {
fmt.Println("error")
}
assert.True(t, flag, "Proof %v failed\n")
}
func TestGetWithdrawalProof(t *testing.T) {
// picking up one random validator index
withdrawalIndex := 0
withdrawalProof, _ := beacon.ProveWithdrawalAgainstExecutionPayload(executionPayloadFieldRoots, block.Body.ExecutionPayload.Withdrawals, uint8(withdrawalIndex))
executionPayloadRoot, _ := block.Body.ExecutionPayload.HashTreeRoot()
// calling the proof verification func
flag := verifyWithdrawalAgainstExecutionPayload(executionPayloadRoot, withdrawalProof, uint64(withdrawalIndex), block.Body.ExecutionPayload.Withdrawals[withdrawalIndex])
assert.True(t, flag, "Proof %v failed\n")
}
func TestGetTimestampProof(t *testing.T) {
// get the block number
executionPayloadFields := block.Body.ExecutionPayload
// get the Merkle proof for inclusion
timestampProof, _ := beacon.ProveTimestampAgainstExecutionPayload(executionPayloadFieldRoots)
root, err := block.Body.ExecutionPayload.HashTreeRoot()
if err != nil {
fmt.Println("error")
}
flag := verifyTimestampAgainstExecutionPayload(root, timestampProof, executionPayloadFields.Timestamp)
assert.True(t, flag, "Proof %v failed")
}
func TestGetValidatorProof(t *testing.T) {
// picking up one random validator index
validatorIndex := uint64(VALIDATOR_INDEX)
beaconStateTopLevelRoots, err := beacon.ComputeBeaconStateTopLevelRootsDeneb(&oracleState)
if err != nil {
fmt.Println("error reading beaconStateTopLevelRoots")
}
validatorProof, _ := epp.ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots, oracleState.Slot, oracleState.Validators, uint64(validatorIndex))
flag := verifyValidatorAgainstBeaconState(&oracleState, validatorProof, validatorIndex)
assert.True(t, flag, "Proof %v failed\n")
}
func TestGetSlotProof(t *testing.T) {
// picking up one random validator index
slot := blockHeader.Slot
buf := make([]byte, 32)
binary.LittleEndian.PutUint64(buf, uint64(slot))
var bytes32 [32]byte
copy(bytes32[:], buf[:32])
proof, _ := beacon.ProveSlotAgainstBlockHeader(&blockHeader)
root, _ := blockHeader.HashTreeRoot()
hh := ssz.NewHasher()
hh.PutUint64(uint64(slot))
leaf := ConvertTo32ByteArray(hh.Hash())
flag := epgcommon.ValidateProof(root, proof, leaf, 0)
if flag != true {
fmt.Println("error")
}
assert.True(t, flag, "Proof %v failed\n")
}
type Proofs struct {
Slot uint64 `json:"slot"`
ValidatorIndex uint64 `json:"validatorIndex"`
WithdrawalIndex uint64 `json:"withdrawalIndex"`
BlockHeaderRootIndex uint64 `json:"blockHeaderRootIndex"`
BeaconStateRoot string `json:"beaconStateRoot"`
SlotRoot string `json:"slotRoot"`
BlockNumberRoot string `json:"blockNumberRoot"`
BlockHeaderRoot string `json:"blockHeaderRoot"`
BlockBodyRoot string `json:"blockBodyRoot"`
ExecutionPayloadRoot string `json:"executionPayloadRoot"`
BlockHeaderProof []string `json:"BlockHeaderProof"`
SlotProof []string `json:"SlotProof"`
WithdrawalProof []string `json:"WithdrawalProof"`
ValidatorProof []string `json:"ValidatorProof"`
BlockNumberProof []string `json:"BlockNumberProof"`
ExecutionPayloadProof []string `json:"ExecutionPayloadProof"`
ValidatorFields []string `json:"ValidatorFields"`
WithdrawalFields []string `json:"WithdrawalFields"`
}
func ParseJSONFile(filePath string) (*beaconStateJSONDeneb, error) {
data, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("error with reading file")
return nil, err
}
var beaconState beaconStateVersionDeneb
err = json.Unmarshal(data, &beaconState)
if err != nil {
fmt.Println("error with beaconState JSON unmarshalling")
return nil, err
}
actualData := beaconState.Data
return &actualData, nil
}
func verifyStateRootAgainstBlockHeaderProof(oracleBlockHeader phase0.BeaconBlockHeader, oracleState deneb.BeaconState, proof epgcommon.Proof) bool {
root, err := oracleBlockHeader.HashTreeRoot()
if err != nil {
fmt.Println("this error", err)