From 3b8c13841fe8d1abdf633189ec67993f7e798f30 Mon Sep 17 00:00:00 2001 From: SiddyJ Date: Mon, 5 Feb 2024 12:07:25 -0800 Subject: [PATCH] responded to comments --- beacon/versioned_beacon_state.go | 11 +++++ generation/generate_balance_update_proof.go | 12 ++++- generation/generate_validator_proof.go | 13 ++++-- .../generate_withdrawal_fields_proof.go | 23 ++++++++-- generation/main.go | 8 ++-- generation/proof_utils.go | 4 +- merkle_util_test.go | 4 +- prove_validator.go | 11 +++-- prove_withdrawal.go | 35 ++++++++------ solidityProofGen/main.go | 46 +++++++++++++------ solidityProofGen/utils.go | 4 +- utils.go | 14 +++--- 12 files changed, 128 insertions(+), 57 deletions(-) diff --git a/beacon/versioned_beacon_state.go b/beacon/versioned_beacon_state.go index a9f869d2..18753ef9 100644 --- a/beacon/versioned_beacon_state.go +++ b/beacon/versioned_beacon_state.go @@ -17,3 +17,14 @@ func HistoricalSummaries(state *spec.VersionedBeaconState) ([]*capella.Historica return nil, errors.New("unsupported beacon state version") } } + +func GenesisTime(state *spec.VersionedBeaconState) (uint64, error) { + switch state.Version { + case spec.DataVersionCapella: + return state.Capella.GenesisTime, nil + case spec.DataVersionDeneb: + return state.Deneb.GenesisTime, nil + default: + return 0, errors.New("unsupported beacon state version") + } +} diff --git a/generation/generate_balance_update_proof.go b/generation/generate_balance_update_proof.go index 103d369f..dfaacd08 100644 --- a/generation/generate_balance_update_proof.go +++ b/generation/generate_balance_update_proof.go @@ -11,36 +11,41 @@ import ( "github.com/rs/zerolog/log" ) -func GenerateBalanceUpdateProof(oracleBlockHeaderFile string, stateFile string, validatorIndex uint64, chainID uint64, output string) { +func GenerateBalanceUpdateProof(oracleBlockHeaderFile string, stateFile string, validatorIndex uint64, chainID uint64, output string) error { var state deneb.BeaconState var oracleBeaconBlockHeader phase0.BeaconBlockHeader stateJSON, err := ParseDenebStateJSONFile(stateFile) if err != nil { log.Debug().AnErr("GenerateBalanceUpdateProof: error with JSON parsing", err) + return err } ParseDenebBeaconStateFromJSON(*stateJSON, &state) oracleBeaconBlockHeader, err = ExtractBlockHeader(oracleBlockHeaderFile) if err != nil { log.Debug().AnErr("Error with parsing header file", err) + return err } beaconStateRoot, err := state.HashTreeRoot() if err != nil { log.Debug().AnErr("Error with HashTreeRoot of state", err) + return err } epp, err := eigenpodproofs.NewEigenPodProofs(chainID, 1000) if err != nil { log.Debug().AnErr("Error creating EPP object", err) + return err } - versionedState := CreateVersionedState(state) + versionedState := createVersionedState(state) stateRootProof, validatorFieldsProof, err := epp.ProveValidatorFields(&oracleBeaconBlockHeader, &versionedState, uint64(validatorIndex)) if err != nil { log.Debug().AnErr("Error with ProveValidatorFields", err) + return err } proofs := BalanceUpdateProofs{ ValidatorIndex: uint64(validatorIndex), @@ -53,8 +58,11 @@ func GenerateBalanceUpdateProof(oracleBlockHeaderFile string, stateFile string, proofData, err := json.Marshal(proofs) if err != nil { log.Debug().AnErr("JSON marshal error: ", err) + return err } _ = os.WriteFile(output, proofData, 0644) + return nil + } diff --git a/generation/generate_validator_proof.go b/generation/generate_validator_proof.go index 56e59367..4eebb815 100644 --- a/generation/generate_validator_proof.go +++ b/generation/generate_validator_proof.go @@ -11,39 +11,42 @@ import ( "github.com/rs/zerolog/log" ) -func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string, validatorIndex uint64, chainID uint64, output string) { +func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string, validatorIndex uint64, chainID uint64, output string) error { var state deneb.BeaconState var oracleBeaconBlockHeader phase0.BeaconBlockHeader stateJSON, err := ParseDenebStateJSONFile(stateFile) if err != nil { log.Debug().Msg("GenerateValidatorFieldsProof: error with JSON parsing") + return err } ParseDenebBeaconStateFromJSON(*stateJSON, &state) oracleBeaconBlockHeader, err = ExtractBlockHeader(oracleBlockHeaderFile) if err != nil { log.Debug().AnErr("Error with parsing header file", err) - + return err } beaconStateRoot, err := state.HashTreeRoot() if err != nil { log.Debug().AnErr("Error with HashTreeRoot of state", err) + return err } epp, err := eigenpodproofs.NewEigenPodProofs(chainID, 1000) if err != nil { log.Debug().AnErr("Error creating EPP object", err) - + return err } - versionedState := CreateVersionedState(state) + versionedState := createVersionedState(state) stateRootProof, validatorFieldsProof, err := epp.ProveValidatorFields(&oracleBeaconBlockHeader, &versionedState, uint64(validatorIndex)) if err != nil { log.Debug().AnErr("Error with ProveValidatorFields", err) + return err } proofs := WithdrawalCredentialProofs{ @@ -57,8 +60,10 @@ func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string proofData, err := json.Marshal(proofs) if err != nil { log.Debug().AnErr("JSON marshal error: ", err) + return err } _ = os.WriteFile(output, proofData, 0644) + return nil } diff --git a/generation/generate_withdrawal_fields_proof.go b/generation/generate_withdrawal_fields_proof.go index 9d9f54b8..292adde9 100644 --- a/generation/generate_withdrawal_fields_proof.go +++ b/generation/generate_withdrawal_fields_proof.go @@ -28,7 +28,7 @@ func GenerateWithdrawalFieldsProof( blockHeaderIndex, chainID uint64, outputFile string, -) { +) error { //this is the oracle provided state var oracleBeaconBlockHeader phase0.BeaconBlockHeader @@ -46,28 +46,33 @@ func GenerateWithdrawalFieldsProof( if err != nil { log.Debug().AnErr("Error with parsing header file", err) + return err } stateJSON, err := ParseDenebStateJSONFile(stateFile) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with JSON parsing state file", err) + return err } ParseDenebBeaconStateFromJSON(*stateJSON, &state) historicalSummaryJSON, err := ParseDenebStateJSONFile(historicalSummaryStateFile) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with JSON parsing historical summary state file", err) + return err } ParseDenebBeaconStateFromJSON(*historicalSummaryJSON, &historicalSummaryState) withdrawalBlockHeader, err = ExtractBlockHeader(blockHeaderFile) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with parsing header file", err) + return err } withdrawalBlock, err = ExtractBlock(blockBodyFile) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with parsing body file", err) + return err } hh := ssz.NewHasher() @@ -78,6 +83,7 @@ func GenerateWithdrawalFieldsProof( beaconStateRoot, err := state.HashTreeRoot() if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with HashTreeRoot of state", err) + return err } slot := withdrawalBlockHeader.Slot @@ -91,39 +97,47 @@ func GenerateWithdrawalFieldsProof( blockHeaderRoot, err := withdrawalBlockHeader.HashTreeRoot() if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with HashTreeRoot of blockHeader", err) + return err } executionPayloadRoot, err := withdrawalBlock.Body.ExecutionPayload.HashTreeRoot() if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with HashTreeRoot of executionPayload", err) + return err } epp, err := eigenpodproofs.NewEigenPodProofs(chainID, 1000) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error creating EPP object", err) + return err } - versionedState = CreateVersionedState(state) + versionedState = createVersionedState(state) oracleBeaconStateTopLevelRoots, err := epp.ComputeBeaconStateTopLevelRoots(&versionedState) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with ComputeBeaconStateTopLevelRoots", err) + return err } - versionedBlock := CreateVersionedBlock(withdrawalBlock) + versionedBlock := createVersionedBlock(withdrawalBlock) withdrawalProof, _, err := epp.ProveWithdrawal(&oracleBeaconBlockHeader, &versionedState, oracleBeaconStateTopLevelRoots, historicalSummaryState.BlockRoots, &versionedBlock, uint64(validatorIndex)) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with ProveWithdrawal", err) + return err } stateRootProofAgainstBlockHeader, err := beacon.ProveStateRootAgainstBlockHeader(&oracleBeaconBlockHeader) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with ProveStateRootAgainstBlockHeader", err) + return err } slotProofAgainstBlockHeader, err := beacon.ProveSlotAgainstBlockHeader(&oracleBeaconBlockHeader) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with ProveSlotAgainstBlockHeader", err) + return err } - validatorProof, err := epp.ProveValidatorAgainstBeaconState(state.Slot, state.Validators, oracleBeaconStateTopLevelRoots, uint64(validatorIndex)) + validatorProof, err := epp.ProveValidatorAgainstBeaconState(oracleBeaconStateTopLevelRoots, state.Slot, state.Validators, uint64(validatorIndex)) if err != nil { log.Debug().AnErr("GenerateWithdrawalFieldsProof: error with ProveValidatorAgainstBeaconState", err) + return err } proofs := WithdrawalProofs{ StateRootAgainstLatestBlockHeaderProof: ConvertBytesToStrings(stateRootProofAgainstBlockHeader), @@ -153,4 +167,5 @@ func GenerateWithdrawalFieldsProof( _ = os.WriteFile(outputFile, proofData, 0644) + return nil } diff --git a/generation/main.go b/generation/main.go index dc33655f..899df69f 100644 --- a/generation/main.go +++ b/generation/main.go @@ -39,18 +39,20 @@ func main() { return } + var err error // Handling commands based on the 'command' flag switch *command { case "ValidatorFieldsProof": - GenerateValidatorFieldsProof(*oracleBlockHeaderFile, *stateFile, *validatorIndex, *chainID, *outputFile) + err = GenerateValidatorFieldsProof(*oracleBlockHeaderFile, *stateFile, *validatorIndex, *chainID, *outputFile) case "WithdrawalFieldsProof": - GenerateWithdrawalFieldsProof(*oracleBlockHeaderFile, *stateFile, *historicalSummaryStateFile, *blockHeaderFile, *blockBodyFile, *validatorIndex, *withdrawalIndex, *historicalSummariesIndex, *blockHeaderIndex, *chainID, *outputFile) + err = GenerateWithdrawalFieldsProof(*oracleBlockHeaderFile, *stateFile, *historicalSummaryStateFile, *blockHeaderFile, *blockBodyFile, *validatorIndex, *withdrawalIndex, *historicalSummariesIndex, *blockHeaderIndex, *chainID, *outputFile) case "BalanceUpdateProof": - GenerateBalanceUpdateProof(*oracleBlockHeaderFile, *stateFile, *validatorIndex, *chainID, *outputFile) + err = GenerateBalanceUpdateProof(*oracleBlockHeaderFile, *stateFile, *validatorIndex, *chainID, *outputFile) default: log.Debug().Str("Unknown command:", *command) } + log.Debug().AnErr("Error: ", err) } diff --git a/generation/proof_utils.go b/generation/proof_utils.go index 9dac0f7c..f0b82774 100644 --- a/generation/proof_utils.go +++ b/generation/proof_utils.go @@ -154,7 +154,7 @@ func ConvertBytesToStrings(b [][32]byte) []string { return s } -func CreateVersionedState(state interface{}) spec.VersionedBeaconState { +func createVersionedState(state interface{}) spec.VersionedBeaconState { var versionedState spec.VersionedBeaconState switch s := state.(type) { @@ -168,7 +168,7 @@ func CreateVersionedState(state interface{}) spec.VersionedBeaconState { return versionedState } -func CreateVersionedBlock(block interface{}) spec.VersionedBeaconBlock { +func createVersionedBlock(block interface{}) spec.VersionedBeaconBlock { var versionedBlock spec.VersionedBeaconBlock switch s := block.(type) { case *deneb.BeaconBlock: diff --git a/merkle_util_test.go b/merkle_util_test.go index 6828881a..56507f50 100644 --- a/merkle_util_test.go +++ b/merkle_util_test.go @@ -110,7 +110,7 @@ func TestGenerateWithdrawalCredentialsProof(t *testing.T) { fmt.Println("error reading beaconStateTopLevelRoots") } - proof, err := epp.ProveValidatorAgainstBeaconState(b.Slot, b.Validators, beaconStateTopLevelRoots, uint64(validatorIndex)) + proof, err := epp.ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots, b.Slot, b.Validators, uint64(validatorIndex)) if err != nil { fmt.Println(err) } @@ -685,7 +685,7 @@ func TestGetValidatorProof(t *testing.T) { beaconStateTopLevelRoots, err := beacon.ComputeBeaconStateTopLevelRootsDeneb(&b) - validatorProof, _ := epp.ProveValidatorAgainstBeaconState(b.Slot, b.Validators, beaconStateTopLevelRoots, uint64(validatorIndex)) + validatorProof, _ := epp.ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots, b.Slot, b.Validators, uint64(validatorIndex)) // verify the proof // get the leaf corresponding to validatorIndex diff --git a/prove_validator.go b/prove_validator.go index ba2e4698..0bde3b19 100644 --- a/prove_validator.go +++ b/prove_validator.go @@ -48,14 +48,17 @@ func (epp *EigenPodProofs) ProveValidatorContainers(oracleBlockHeader *phase0.Be return nil, err } - VerifyValidatorFieldsCallParams.OracleTimestamp = GetSlotTimestamp(oracleBeaconState, oracleBlockHeader) + VerifyValidatorFieldsCallParams.OracleTimestamp, err = GetSlotTimestamp(oracleBeaconState, oracleBlockHeader) + if err != nil { + return nil, err + } VerifyValidatorFieldsCallParams.ValidatorIndices = make([]uint64, len(validatorIndices)) VerifyValidatorFieldsCallParams.ValidatorFieldsProofs = make([]common.Proof, len(validatorIndices)) VerifyValidatorFieldsCallParams.ValidatorFields = make([][]Bytes32, len(validatorIndices)) for i, validatorIndex := range validatorIndices { VerifyValidatorFieldsCallParams.ValidatorIndices[i] = validatorIndex // prove the validator fields against the beacon state - VerifyValidatorFieldsCallParams.ValidatorFieldsProofs[i], err = epp.ProveValidatorAgainstBeaconState(oracleBeaconStateSlot, oracleBeaconStateValidators, beaconStateTopLevelRoots, validatorIndex) + VerifyValidatorFieldsCallParams.ValidatorFieldsProofs[i], err = epp.ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots, oracleBeaconStateSlot, oracleBeaconStateValidators, validatorIndex) if err != nil { return nil, err } @@ -95,7 +98,7 @@ func (epp *EigenPodProofs) ProveValidatorFields(oracleBlockHeader *phase0.Beacon return nil, nil, err } - validatorFieldsProof, err := epp.ProveValidatorAgainstBeaconState(oracleBeaconStateSlot, oracleBeaconStateValidators, beaconStateTopLevelRoots, validatorIndex) + validatorFieldsProof, err := epp.ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots, oracleBeaconStateSlot, oracleBeaconStateValidators, validatorIndex) if err != nil { return nil, nil, err @@ -104,7 +107,7 @@ func (epp *EigenPodProofs) ProveValidatorFields(oracleBlockHeader *phase0.Beacon return stateRootProof, validatorFieldsProof, nil } -func (epp *EigenPodProofs) ProveValidatorAgainstBeaconState(oracleBeaconStateSlot phase0.Slot, oracleBeaconStateValidators []*phase0.Validator, beaconStateTopLevelRoots *beacon.BeaconStateTopLevelRoots, validatorIndex uint64) (common.Proof, error) { +func (epp *EigenPodProofs) ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots *beacon.BeaconStateTopLevelRoots, oracleBeaconStateSlot phase0.Slot, oracleBeaconStateValidators []*phase0.Validator, validatorIndex uint64) (common.Proof, error) { // prove the validator list against the beacon state validatorListProof, err := beacon.ProveBeaconTopLevelRootAgainstBeaconState(beaconStateTopLevelRoots, beacon.ValidatorListIndex) if err != nil { diff --git a/prove_withdrawal.go b/prove_withdrawal.go index 8cd7ba34..dd213aba 100644 --- a/prove_withdrawal.go +++ b/prove_withdrawal.go @@ -122,7 +122,10 @@ func (epp *EigenPodProofs) ProveWithdrawals( return nil, err } - verifyAndProcessWithdrawalCallParams.OracleTimestamp = GetSlotTimestamp(oracleBeaconState, oracleBlockHeader) + verifyAndProcessWithdrawalCallParams.OracleTimestamp, err = GetSlotTimestamp(oracleBeaconState, oracleBlockHeader) + if err != nil { + return nil, err + } numWithdrawals := len(withdrawalBlocks) @@ -139,16 +142,16 @@ func (epp *EigenPodProofs) ProveWithdrawals( if err != nil { return nil, err } - log.Info().Msgf("time to prove withdrawal: %s", time.Since(start)) + log.Debug().Msgf("time to prove withdrawal: %s", time.Since(start)) start = time.Now() // prove validator - verifyAndProcessWithdrawalCallParams.ValidatorFieldsProofs[i], err = epp.ProveValidatorAgainstBeaconState(oracleBeaconStateSlot, oracleBeaconStateValidators, oracleBeaconStateTopLevelRoots, validatorIndices[i]) + verifyAndProcessWithdrawalCallParams.ValidatorFieldsProofs[i], err = epp.ProveValidatorAgainstBeaconState(oracleBeaconStateTopLevelRoots, oracleBeaconStateSlot, oracleBeaconStateValidators, validatorIndices[i]) if err != nil { return nil, err } verifyAndProcessWithdrawalCallParams.ValidatorFields[i] = ConvertValidatorToValidatorFields(oracleBeaconStateValidators[validatorIndices[i]]) - log.Info().Msgf("time to prove validator: %s", time.Since(start)) + log.Debug().Msgf("time to prove validator: %s", time.Since(start)) } return verifyAndProcessWithdrawalCallParams, nil @@ -174,7 +177,7 @@ func (epp *EigenPodProofs) ProveWithdrawal( if err != nil { return nil, nil, err } - log.Info().Msgf("time to compute block body root: %s", time.Since(start)) + log.Debug().Msgf("time to compute block body root: %s", time.Since(start)) slot, err := withdrawalBlock.Slot() if err != nil { @@ -217,13 +220,16 @@ func (epp *EigenPodProofs) ProveWithdrawal( if err != nil { return nil, nil, err } - log.Info().Msgf("time to prove execution payload against block header: %s", time.Since(start)) + log.Debug().Msgf("time to prove execution payload against block header: %s", time.Since(start)) + start = time.Now() // calculate execution payload field roots withdrawalExecutionPayloadFieldRoots, err = beacon.ComputeExecutionPayloadFieldRootsDeneb(withdrawalBlock.Deneb.Body.ExecutionPayload) if err != nil { return nil, nil, err } + log.Debug().Msgf("time to compute execution payload field roots: %s", time.Since(start)) + withdrawals = withdrawalBlock.Deneb.Body.ExecutionPayload.Withdrawals withdrawalIndex = GetWithdrawalIndex(validatorIndex, withdrawals) withdrawalFields = ConvertWithdrawalToWithdrawalFields(withdrawalBlock.Deneb.Body.ExecutionPayload.Withdrawals[withdrawalIndex]) @@ -235,13 +241,16 @@ func (epp *EigenPodProofs) ProveWithdrawal( if err != nil { return nil, nil, err } - log.Info().Msgf("time to prove execution payload against block header: %s", time.Since(start)) + log.Debug().Msgf("time to prove execution payload against block header: %s", time.Since(start)) + start = time.Now() // calculate execution payload field roots withdrawalExecutionPayloadFieldRoots, err = beacon.ComputeExecutionPayloadFieldRootsCapella(withdrawalBlock.Capella.Body.ExecutionPayload) if err != nil { return nil, nil, err } + log.Debug().Msgf("time to compute execution payload field roots: %s", time.Since(start)) + withdrawals = withdrawalBlock.Capella.Body.ExecutionPayload.Withdrawals withdrawalIndex = GetWithdrawalIndex(validatorIndex, withdrawals) withdrawalFields = ConvertWithdrawalToWithdrawalFields(withdrawalBlock.Capella.Body.ExecutionPayload.Withdrawals[withdrawalIndex]) @@ -272,7 +281,7 @@ func (epp *EigenPodProofs) ProveWithdrawal( return nil, nil, err } - log.Info().Msgf("time to prove withdrawal: %s", time.Since(start)) + log.Debug().Msgf("time to prove withdrawal: %s", time.Since(start)) return withdrawalProof, withdrawalFields, nil } @@ -317,7 +326,7 @@ func (epp *EigenPodProofs) proveWithdrawal( } // log the time it takes to compute each proof - log.Info().Msg("computing withdrawal proof") + log.Debug().Msg("computing withdrawal proof") var err error start := time.Now() @@ -330,7 +339,7 @@ func (epp *EigenPodProofs) proveWithdrawal( if err != nil { return err } - log.Info().Msgf("time to prove withdrawal against execution payload: %s", time.Since(start)) + log.Debug().Msgf("time to prove withdrawal against execution payload: %s", time.Since(start)) start = time.Now() // prove the slot against the withdrawal block header @@ -338,7 +347,7 @@ func (epp *EigenPodProofs) proveWithdrawal( if err != nil { return err } - log.Info().Msgf("time to prove slot against block header: %s", time.Since(start)) + log.Debug().Msgf("time to prove slot against block header: %s", time.Since(start)) withdrawalProof.SlotRoot = ConvertUint64ToRoot(uint64(withdrawalBlockHeader.Slot)) start = time.Now() @@ -348,7 +357,7 @@ func (epp *EigenPodProofs) proveWithdrawal( return err } withdrawalProof.TimestampRoot = ConvertUint64ToRoot(withdrawalTimestamp) - log.Info().Msgf("time to prove timestamp against execution payload: %s", time.Since(start)) + log.Debug().Msgf("time to prove timestamp against execution payload: %s", time.Since(start)) start = time.Now() // prove the withdrawal block root against the oracle state root @@ -356,7 +365,7 @@ func (epp *EigenPodProofs) proveWithdrawal( if err != nil { return err } - log.Info().Msgf("time to prove block root against beacon state via historical summaries: %s", time.Since(start)) + log.Debug().Msgf("time to prove block root against beacon state via historical summaries: %s", time.Since(start)) return nil } diff --git a/solidityProofGen/main.go b/solidityProofGen/main.go index c78425d6..f764d650 100644 --- a/solidityProofGen/main.go +++ b/solidityProofGen/main.go @@ -115,11 +115,11 @@ func main() { GenerateValidatorFieldsProof(oracleStateFile, stateFile, index, changeBalance, uint64(newBalance), outputFile) default: fmt.Println("Unknown command:", args[0]) + os.Exit(0) } } -// TODO: Get this working -func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string, index uint64, changeBalance bool, newBalance uint64, output string) { +func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string, index uint64, changeBalance bool, newBalance uint64, output string) error { var state deneb.BeaconState var oracleBeaconBlockHeader phase0.BeaconBlockHeader SetupValidatorProof(oracleBlockHeaderFile, stateFile, index, changeBalance, newBalance, 0, &state, &oracleBeaconBlockHeader) @@ -131,12 +131,13 @@ func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string latestBlockHeaderRoot, err := oracleBeaconBlockHeader.HashTreeRoot() if err != nil { fmt.Println("Error with HashTreeRoot of latestBlockHeader", err) + return err } epp, err := eigenpodproofs.NewEigenPodProofs(GOERLI_CHAIN_ID, 1000) if err != nil { fmt.Println("Error creating EPP object", err) - + return err } var versionedState spec.VersionedBeaconState @@ -156,13 +157,15 @@ func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string proofData, err := json.Marshal(proofs) if err != nil { fmt.Println("error") + return err } _ = ioutil.WriteFile(output, proofData, 0644) + return nil } -func GenerateWithdrawalFieldsProof(index, historicalSummariesIndex, blockHeaderIndex uint64, oracleBlockHeaderFile, stateFile, historicalSummaryStateFile, headerFile, bodyFile, outputFile string, modifyStateToIncludeFullWithdrawal bool, partialWithdrawalProof bool, advanceSlotOfWithdrawal bool) { +func GenerateWithdrawalFieldsProof(index, historicalSummariesIndex, blockHeaderIndex uint64, oracleBlockHeaderFile, stateFile, historicalSummaryStateFile, headerFile, bodyFile, outputFile string, modifyStateToIncludeFullWithdrawal bool, partialWithdrawalProof bool, advanceSlotOfWithdrawal bool) error { //this is the oracle provided state var oracleBeaconBlockHeader phase0.BeaconBlockHeader @@ -181,13 +184,17 @@ func GenerateWithdrawalFieldsProof(index, historicalSummariesIndex, blockHeaderI beaconBlockHeaderToVerifyIndex := blockHeaderIndex - versionedOracleState := CreateVersionedState(spec.DataVersionDeneb) + versionedOracleState := createVersionedState(spec.DataVersionDeneb) versionedOracleState.Deneb = &oracleState - versionedWithdrawalBlock := CreateVersionedBlock(spec.DataVersionDeneb) + versionedWithdrawalBlock := createVersionedBlock(spec.DataVersionDeneb) versionedWithdrawalBlock.Deneb = &withdrawalBlock validatorIndex := phase0.ValidatorIndex(index) - beaconStateRoot, _ := oracleState.HashTreeRoot() + beaconStateRoot, err := oracleState.HashTreeRoot() + if err != nil { + fmt.Println("Error with HashTreeRoot of oracleState", err) + return err + } fmt.Println("beaconStateRoot", hex.EncodeToString(beaconStateRoot[:])) @@ -198,6 +205,7 @@ func GenerateWithdrawalFieldsProof(index, historicalSummariesIndex, blockHeaderI latestBlockHeaderRoot, err := oracleBeaconBlockHeader.HashTreeRoot() if err != nil { fmt.Println("Error with HashTreeRoot of latestBlockHeader", err) + return err } timestamp := withdrawalBlock.Body.ExecutionPayload.Timestamp @@ -211,24 +219,29 @@ func GenerateWithdrawalFieldsProof(index, historicalSummariesIndex, blockHeaderI epp, err := eigenpodproofs.NewEigenPodProofs(GOERLI_CHAIN_ID, 1000) if err != nil { fmt.Println("Error creating EPP object", err) + return err } oracleBeaconStateTopLevelRoots, err := epp.ComputeBeaconStateTopLevelRoots(&versionedOracleState) if err != nil { fmt.Println("Error computing beacon state top level roots", err) + return err } //blockHeaderProof, slotProof, withdrawalProof, validatorProof, timestampProof, executionPayloadProof, stateRootAgainstLatestBlockHeaderProof, historicalSummaryProof, err := // withdrawalProof, stateRootProof, validatorProof, err := epp.ProveWithdrawal(&oracleBeaconBlockHeader, &oracleState, historicalSummaryState.BlockRoots, &withdrawalBlock, validatorIndex) withdrawalProof, _, err := epp.ProveWithdrawal(&oracleBeaconBlockHeader, &versionedOracleState, oracleBeaconStateTopLevelRoots, historicalSummaryState.BlockRoots, &versionedWithdrawalBlock, uint64(validatorIndex)) if err != nil { fmt.Println("ProveWithdrawal error", err) + return err } stateRootProof, err := beacon.ProveStateRootAgainstBlockHeader(&oracleBeaconBlockHeader) if err != nil { fmt.Println("ProveStateRootAgainstBlockHeader error", err) + return err } - validatorProof, err := epp.ProveValidatorAgainstBeaconState(oracleState.Slot, oracleState.Validators, oracleBeaconStateTopLevelRoots, uint64(validatorIndex)) + validatorProof, err := epp.ProveValidatorAgainstBeaconState(oracleBeaconStateTopLevelRoots, oracleState.Slot, oracleState.Validators, uint64(validatorIndex)) if err != nil { fmt.Println("ProveValidatorAgainstBeaconState error", err) + return err } proofs := WithdrawalProofs{ @@ -257,13 +270,14 @@ func GenerateWithdrawalFieldsProof(index, historicalSummariesIndex, blockHeaderI proofData, err := json.Marshal(proofs) if err != nil { fmt.Println("error") + return err } _ = os.WriteFile(outputFile, proofData, 0644) - + return nil } -func GenerateWithdrawalFieldsProofCapella(index, historicalSummariesIndex, blockHeaderIndex uint64, oracleBlockHeaderFile, stateFile, historicalSummaryStateFile, headerFile, bodyFile, outputFile string, modifyStateToIncludeFullWithdrawal bool, partialWithdrawalProof bool, advanceSlotOfWithdrawal bool) { +func GenerateWithdrawalFieldsProofCapella(index, historicalSummariesIndex, blockHeaderIndex uint64, oracleBlockHeaderFile, stateFile, historicalSummaryStateFile, headerFile, bodyFile, outputFile string, modifyStateToIncludeFullWithdrawal bool, partialWithdrawalProof bool, advanceSlotOfWithdrawal bool) error { //this is the oracle provided state var oracleBeaconBlockHeader phase0.BeaconBlockHeader @@ -283,9 +297,9 @@ func GenerateWithdrawalFieldsProofCapella(index, historicalSummariesIndex, block beaconBlockHeaderToVerifyIndex := blockHeaderIndex - versionedOracleState := CreateVersionedState(spec.DataVersionCapella) + versionedOracleState := createVersionedState(spec.DataVersionCapella) versionedOracleState.Deneb = &oracleState - versionedWithdrawalBlock := CreateVersionedBlock(spec.DataVersionCapella) + versionedWithdrawalBlock := createVersionedBlock(spec.DataVersionCapella) versionedWithdrawalBlock.Capella = &withdrawalBlock validatorIndex := phase0.ValidatorIndex(index) @@ -300,6 +314,7 @@ func GenerateWithdrawalFieldsProofCapella(index, historicalSummariesIndex, block latestBlockHeaderRoot, err := oracleBeaconBlockHeader.HashTreeRoot() if err != nil { fmt.Println("Error with HashTreeRoot of latestBlockHeader", err) + return err } timestamp := withdrawalBlock.Body.ExecutionPayload.Timestamp @@ -313,6 +328,7 @@ func GenerateWithdrawalFieldsProofCapella(index, historicalSummariesIndex, block epp, err := eigenpodproofs.NewEigenPodProofs(GOERLI_CHAIN_ID, 1000) if err != nil { fmt.Println("Error creating EPP object", err) + return err } oracleBeaconStateTopLevelRoots, err := epp.ComputeBeaconStateTopLevelRoots(&versionedOracleState) //blockHeaderProof, slotProof, withdrawalProof, validatorProof, timestampProof, executionPayloadProof, stateRootAgainstLatestBlockHeaderProof, historicalSummaryProof, err := @@ -320,14 +336,17 @@ func GenerateWithdrawalFieldsProofCapella(index, historicalSummariesIndex, block withdrawalProof, _, err := epp.ProveWithdrawal(&oracleBeaconBlockHeader, &versionedOracleState, oracleBeaconStateTopLevelRoots, historicalSummaryState.BlockRoots, &versionedWithdrawalBlock, uint64(validatorIndex)) if err != nil { fmt.Println("ProveWithdrawal error", err) + return err } stateRootProof, err := beacon.ProveStateRootAgainstBlockHeader(&oracleBeaconBlockHeader) if err != nil { fmt.Println("ProveStateRootAgainstBlockHeader error", err) + return err } - validatorProof, err := epp.ProveValidatorAgainstBeaconState(oracleState.Slot, oracleState.Validators, oracleBeaconStateTopLevelRoots, uint64(validatorIndex)) + validatorProof, err := epp.ProveValidatorAgainstBeaconState(oracleBeaconStateTopLevelRoots, oracleState.Slot, oracleState.Validators, uint64(validatorIndex)) if err != nil { fmt.Println("ProveValidatorAgainstBeaconState error", err) + return err } proofs := WithdrawalProofs{ Slot: uint64(slot), @@ -360,4 +379,5 @@ func GenerateWithdrawalFieldsProofCapella(index, historicalSummariesIndex, block _ = os.WriteFile(outputFile, proofData, 0644) + return nil } diff --git a/solidityProofGen/utils.go b/solidityProofGen/utils.go index 9e334f3b..f74330bd 100644 --- a/solidityProofGen/utils.go +++ b/solidityProofGen/utils.go @@ -483,7 +483,7 @@ func GetWithdrawalFields(w *capella.Withdrawal) []string { return withdrawalFields } -func CreateVersionedState(version spec.DataVersion) spec.VersionedBeaconState { +func createVersionedState(version spec.DataVersion) spec.VersionedBeaconState { var versionedState spec.VersionedBeaconState switch version { case spec.DataVersionDeneb: @@ -497,7 +497,7 @@ func CreateVersionedState(version spec.DataVersion) spec.VersionedBeaconState { return versionedState } -func CreateVersionedBlock(version spec.DataVersion) spec.VersionedBeaconBlock { +func createVersionedBlock(version spec.DataVersion) spec.VersionedBeaconBlock { var versionedBlock spec.VersionedBeaconBlock switch version { case spec.DataVersionDeneb: diff --git a/utils.go b/utils.go index 04b57eae..1d72dd16 100644 --- a/utils.go +++ b/utils.go @@ -6,6 +6,7 @@ import ( "math/big" "math/bits" + beacon "github.com/Layr-Labs/eigenpod-proofs-generation/beacon" "github.com/attestantio/go-eth2-client/spec" "github.com/attestantio/go-eth2-client/spec/capella" "github.com/attestantio/go-eth2-client/spec/phase0" @@ -90,15 +91,12 @@ func NextPowerOfTwo(v uint64) uint { return uint(v) } -func GetSlotTimestamp(beaconState *spec.VersionedBeaconState, blockHeader *phase0.BeaconBlockHeader) uint64 { - var genesisTime uint64 - switch beaconState.Version { - case spec.DataVersionDeneb: - genesisTime = beaconState.Deneb.GenesisTime - case spec.DataVersionCapella: - genesisTime = beaconState.Capella.GenesisTime +func GetSlotTimestamp(beaconState *spec.VersionedBeaconState, blockHeader *phase0.BeaconBlockHeader) (uint64, error) { + genesisTime, err := beacon.GenesisTime(beaconState) + if err != nil { + return 0, err } - return genesisTime + uint64(blockHeader.Slot)*12 + return genesisTime + uint64(blockHeader.Slot)*12, nil } func ConvertValidatorToValidatorFields(v *phase0.Validator) []Bytes32 {