Skip to content

Commit

Permalink
responded to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidu28 committed Feb 5, 2024
1 parent 849c488 commit 3b8c138
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 57 deletions.
11 changes: 11 additions & 0 deletions beacon/versioned_beacon_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
12 changes: 10 additions & 2 deletions generation/generate_balance_update_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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

}
13 changes: 9 additions & 4 deletions generation/generate_validator_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
23 changes: 19 additions & 4 deletions generation/generate_withdrawal_fields_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GenerateWithdrawalFieldsProof(
blockHeaderIndex,
chainID uint64,
outputFile string,
) {
) error {

//this is the oracle provided state
var oracleBeaconBlockHeader phase0.BeaconBlockHeader
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -153,4 +167,5 @@ func GenerateWithdrawalFieldsProof(

_ = os.WriteFile(outputFile, proofData, 0644)

return nil
}
8 changes: 5 additions & 3 deletions generation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions generation/proof_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions merkle_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions prove_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 3b8c138

Please sign in to comment.