Skip to content

Commit

Permalink
added capella against deneb test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidu28 committed Feb 9, 2024
1 parent 2b957c3 commit b89458c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 32 deletions.
24 changes: 2 additions & 22 deletions merkle_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func setupSuite() {
fmt.Println("error with oracle block header", err)
}

block, err = ExtractBlock(bodyFile)
block, err = ExtractBlockDeneb(bodyFile)
if err != nil {
fmt.Println("error with block body", err)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestProveWithdrawals(t *testing.T) {
historicalSummaryStateBlockRoots := historicalSummaryState.BlockRoots
ParseDenebBeaconStateFromJSON(*historicalSummaryStateJSON, &historicalSummaryState)

withdrawalBlock, err := ExtractBlock("data/deneb_goerli_block_7421951.json")
withdrawalBlock, err := ExtractBlockDeneb("data/deneb_goerli_block_7421951.json")
if err != nil {
fmt.Println("block.UnmarshalJSON error", err)
}
Expand Down Expand Up @@ -801,25 +801,6 @@ func ParseJSONFile(filePath string) (*beaconStateJSONDeneb, error) {
return &actualData, nil
}

func ParseJSONFileCapella(filePath string) (*beaconStateJSONCapella, error) {
data, err := os.ReadFile(filePath)

if err != nil {
fmt.Println("error with reading file")
return nil, err
}

var beaconState beaconStateVersionCapella
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 common.Proof) bool {
root, err := oracleBlockHeader.HashTreeRoot()
if err != nil {
Expand All @@ -835,7 +816,6 @@ func verifyStateRootAgainstBlockHeaderProof(oracleBlockHeader phase0.BeaconBlock
fmt.Println("this error")
}
return flag

}

func verifyValidatorAgainstBeaconState(oracleState *deneb.BeaconState, proof common.Proof, validatorIndex uint64) bool {
Expand Down
88 changes: 81 additions & 7 deletions onchain_tests/onchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
beacon "github.com/Layr-Labs/eigenpod-proofs-generation/beacon"
contractBeaconChainProofs "github.com/Layr-Labs/eigenpod-proofs-generation/bindings"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -58,15 +59,15 @@ func TestMain(m *testing.M) {
}

func setupSuite() {
RPC := "https://rpc.ankr.com/eth_goerli"
PrivateKey := os.Getenv("PRIVATE_KEY")
rpc := "https://rpc.ankr.com/eth_goerli"
privateKey := os.Getenv("PRIVATE_KEY")

ethClient, err := ethclient.Dial(RPC)
ethClient, err := ethclient.Dial(rpc)
if err != nil {
log.Panicf("failed to connect to the Ethereum client: %s", err)
}

chainClient, err = eigenpodproofs.NewChainClient(ethClient, PrivateKey)
chainClient, err = eigenpodproofs.NewChainClient(ethClient, privateKey)
if err != nil {
log.Panicf("failed to create chain client: %s", err)
}
Expand Down Expand Up @@ -99,7 +100,7 @@ func setupSuite() {
fmt.Println("error with oracle block header", err)
}

block, err = eigenpodproofs.ExtractBlock(bodyFile)
block, err = eigenpodproofs.ExtractBlockDeneb(bodyFile)
if err != nil {
fmt.Println("error with block body", err)
}
Expand Down Expand Up @@ -170,7 +171,7 @@ func TestValidatorContainersProofOnChain(t *testing.T) {
assert.Nil(t, err)
}

func TestWithdrawalProofOnChain(t *testing.T) {
func TestProvingDenebWithdrawalAgainstDenebStateOnChain(t *testing.T) {

versionedOracleState, err := beacon.CreateVersionedState(&oracleState)
if err != nil {
Expand All @@ -186,7 +187,80 @@ func TestWithdrawalProofOnChain(t *testing.T) {
historicalSummaryStateBlockRoots := historicalSummaryState.BlockRoots
eigenpodproofs.ParseDenebBeaconStateFromJSON(*historicalSummaryStateJSON, &historicalSummaryState)

withdrawalBlock, err := eigenpodproofs.ExtractBlock("../data/deneb_goerli_block_7421951.json")
withdrawalBlock, err := eigenpodproofs.ExtractBlockDeneb("../data/deneb_goerli_block_7421951.json")
if err != nil {
fmt.Println("block.UnmarshalJSON error", err)
}

versionedWithdrawalBlock, err := beacon.CreateVersionedSignedBlock(withdrawalBlock)
if err != nil {
fmt.Println("error", err)
return
}

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)
}

var withdrawalFields [][32]byte
for _, field := range verifyAndProcessWithdrawalCallParams.WithdrawalFields[0] {
withdrawalFields = append(withdrawalFields, field)
}

withdrawalProof := contractBeaconChainProofs.BeaconChainProofsWithdrawalProof{
WithdrawalProof: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].WithdrawalProof.ToByteSlice(),
SlotProof: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].SlotProof.ToByteSlice(),
ExecutionPayloadProof: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].ExecutionPayloadProof.ToByteSlice(),
TimestampProof: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].TimestampProof.ToByteSlice(),
HistoricalSummaryBlockRootProof: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].HistoricalSummaryBlockRootProof.ToByteSlice(),
BlockRootIndex: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].BlockRootIndex,
HistoricalSummaryIndex: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].HistoricalSummaryIndex,
WithdrawalIndex: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].WithdrawalIndex,
BlockRoot: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].BlockRoot,
SlotRoot: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].SlotRoot,
TimestampRoot: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].TimestampRoot,
ExecutionPayloadRoot: verifyAndProcessWithdrawalCallParams.WithdrawalProofs[0].ExecutionPayloadRoot,
}

err = beaconChainProofs.VerifyWithdrawal(
&bind.CallOpts{},
verifyAndProcessWithdrawalCallParams.StateRootProof.BeaconStateRoot,
withdrawalFields,
withdrawalProof,
)
if err != nil {
fmt.Println("error", err)
}
assert.Nil(t, err)

}

func TestProvingCapellaWithdrawalAgainstDenebStateOnChain(t *testing.T) {

versionedOracleState, err := beacon.CreateVersionedState(&oracleState)
if err != nil {
fmt.Println("error creating versioned state", err)
return
}

historicalSummaryStateJSON, err := eigenpodproofs.ParseJSONFileCapella("../data/goerli_slot_6397952.json")
if err != nil {
fmt.Println("error parsing historicalSummaryState JSON")
}
var historicalSummaryState capella.BeaconState
historicalSummaryStateBlockRoots := historicalSummaryState.BlockRoots
eigenpodproofs.ParseCapellaBeaconStateFromJSON(*historicalSummaryStateJSON, &historicalSummaryState)

withdrawalBlock, err := eigenpodproofs.ExtractBlockCapella("../data/goerli_block_6397852.json")
if err != nil {
fmt.Println("block.UnmarshalJSON error", err)
}
Expand Down
51 changes: 48 additions & 3 deletions proof_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ type InputDataBlockHeader struct {
} `json:"data"`
}

type InputDataBlock struct {
type InputDataBlockDeneb struct {
Version string `json:"version"`
Data struct {
Message deneb.BeaconBlock `json:"message"`
Expand All @@ -146,6 +146,16 @@ type InputDataBlock struct {
Finalized bool `json:"finalized"`
}

type InputDataBlockCapella struct {
Version string `json:"version"`
Data struct {
Message capella.BeaconBlock `json:"message"`
Signature string `json:"signature"`
} `json:"data"`
Execution_optimistic bool `json:"execution_optimistic"`
Finalized bool `json:"finalized"`
}

func ParseJSONFileDeneb(filePath string) (*beaconStateJSONDeneb, error) {
data, err := os.ReadFile(filePath)

Expand All @@ -165,6 +175,25 @@ func ParseJSONFileDeneb(filePath string) (*beaconStateJSONDeneb, error) {
return &actualData, nil
}

func ParseJSONFileCapella(filePath string) (*beaconStateJSONCapella, error) {
data, err := os.ReadFile(filePath)

if err != nil {
fmt.Println("error with reading file")
return nil, err
}

var beaconState beaconStateVersionCapella
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 ConvertBytesToStrings(b [][32]byte) []string {
var s []string
for _, v := range b {
Expand Down Expand Up @@ -229,14 +258,14 @@ func ExtractBlockHeader(blockHeaderFile string) (phase0.BeaconBlockHeader, error
return inputData.Data.Header.Message, nil
}

func ExtractBlock(blockHeaderFile string) (deneb.BeaconBlock, error) {
func ExtractBlockDeneb(blockHeaderFile string) (deneb.BeaconBlock, error) {
fileBytes, err := os.ReadFile(blockHeaderFile)
if err != nil {
return deneb.BeaconBlock{}, err
}

// Decode JSON
var data InputDataBlock
var data InputDataBlockDeneb
if err := json.Unmarshal(fileBytes, &data); err != nil {
return deneb.BeaconBlock{}, err
}
Expand All @@ -245,6 +274,22 @@ func ExtractBlock(blockHeaderFile string) (deneb.BeaconBlock, error) {
return data.Data.Message, nil
}

func ExtractBlockCapella(blockHeaderFile string) (capella.BeaconBlock, error) {
fileBytes, err := os.ReadFile(blockHeaderFile)
if err != nil {
return capella.BeaconBlock{}, err
}

// Decode JSON
var data InputDataBlockCapella
if err := json.Unmarshal(fileBytes, &data); err != nil {
return capella.BeaconBlock{}, err
}

// Extract block body
return data.Data.Message, nil
}

func GetWithdrawalFields(w *capella.Withdrawal) []string {
var withdrawalFields []string
hh := ssz.NewHasher()
Expand Down

0 comments on commit b89458c

Please sign in to comment.