Skip to content

Commit

Permalink
import constants from beacon pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
mpjunior92 committed Jan 23, 2024
1 parent 1ae8fd9 commit 7d8a141
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 55 deletions.
4 changes: 2 additions & 2 deletions beacon/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func ProveBlockBodyAgainstBlockHeader(blockHeader *phase0.BeaconBlockHeader) (co
return nil, err
}

return common.GetProof(blockHeaderContainerRoots, beaconBlockBodyRootIndex, blockHeaderMerkleSubtreeNumLayers)
return common.GetProof(blockHeaderContainerRoots, BeaconBlockBodyRootIndex, blockHeaderMerkleSubtreeNumLayers)
}

// refer to this: https://github.com/attestantio/go-eth2-client/blob/654ac05b4c534d96562329f988655e49e5743ff5/spec/phase0/beaconblockheader_encoding.go
Expand All @@ -33,7 +33,7 @@ func ProveSlotAgainstBlockHeader(blockHeader *phase0.BeaconBlockHeader) (common.
return nil, err
}

return common.GetProof(blockHeaderContainerRoots, slotIndex, blockHeaderMerkleSubtreeNumLayers)
return common.GetProof(blockHeaderContainerRoots, SlotIndex, blockHeaderMerkleSubtreeNumLayers)
}

func GetBlockHeaderFieldRoots(blockHeader *phase0.BeaconBlockHeader) ([]phase0.Root, error) {
Expand Down
4 changes: 2 additions & 2 deletions beacon/capella.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ func ProveExecutionPayloadAgainstBlockBodyCapella(beaconBlockBody *capella.Beaco
hh.Reset()
}

proof, err := common.GetProof(beaconBlockBodyContainerRoots, executionPayloadIndex, blockBodyMerkleSubtreeNumLayers)
proof, err := common.GetProof(beaconBlockBodyContainerRoots, ExecutionPayloadIndex, BlockBodyMerkleSubtreeNumLayers)

return proof, beaconBlockBodyContainerRoots[executionPayloadIndex], err
return proof, beaconBlockBodyContainerRoots[ExecutionPayloadIndex], err
}

func GetExecutionPayloadFieldRootsCapella(executionPayloadFields *capella.ExecutionPayload) ([]phase0.Root, error) {
Expand Down
29 changes: 15 additions & 14 deletions beacon/constants.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package beacon

const (
slotsPerHistoricalRoot = uint64(8192)
SlotsPerHistoricalRoot = uint64(8192)

// Index of the historical summaries in the beacon state
historicalSummaryListIndex = uint64(27)
HistoricalSummaryListIndex = uint64(27)

// Index of validator list in beacon state
validatorListIndex = uint64(11)
ValidatorListIndex = uint64(11)
balanceListIndex = uint64(12)

// Index of the beacon body root inside the beacon body header
beaconBlockBodyRootIndex = uint64(4)
BeaconBlockBodyRootIndex = uint64(4)

// Index of the execution payload in the BeaconBlockBody container
executionPayloadIndex = uint64(9)
ExecutionPayloadIndex = uint64(9)

// Index of the timestamp inside the execution payload
timestampIndex = uint64(9)
TimestampIndex = uint64(9)

// Index of the withdrawals inside the execution payload
withdrawalsIndex = uint64(14)
WithdrawalsIndex = uint64(14)

// Index of the slot in the beacon block header
slotIndex = uint64(0)
SlotIndex = uint64(0)
stateRootIndex = uint64(3)

// in the historical summary coontainer, the block root summary is at index 0
blockSummaryRootIndex = uint64(0)
BlockSummaryRootIndex = uint64(0)
)

//
Expand All @@ -40,27 +40,28 @@ const (
// Number of layers for various merkle subtrees
blockHeaderMerkleSubtreeNumLayers = uint64(3)

blockBodyMerkleSubtreeNumLayers = uint64(4)
BlockBodyMerkleSubtreeNumLayers = uint64(4)

// TODO unused; remove
// Number of layers for the merkelization of the Execution Payload
executionPayloadMerkleSubtreeNumLayersDeneb = uint64(5)

executionPayloadMerkleSubtreeNumLayersCapella = uint64(4)

// Number of layers for the merkleixation of the Validator List in the Beacon State
validatorListMerkleSubtreeNumLayers = uint64(40)
ValidatorListMerkleSubtreeNumLayers = uint64(40)

// Number of layers for the merkleixation of the Historical Summary List in the Beacon State
historicalSummaryListMerkleSubtreeNumLayers = uint64(24)
HistoricalSummaryListMerkleSubtreeNumLayers = uint64(24)

// Number of layers for the merkleization of the Withdrawal List in the Exection Payload
withdrawalListMerkleSubtreeNumLayers = uint64(4)
WithdrawalListMerkleSubtreeNumLayers = uint64(4)

// Number of layers for the merkleization of the Beacon State
beaconStateMerkleSubtreeNumLayers = uint64(5)

// Number of layers for the merkleization of the Block Roots in the Beacon State
blockRootsMerkleSubtreeNumLayers = uint64(13)
BlockRootsMerkleSubtreeNumLayers = uint64(13)

// **************Number of fields of various containers**************
beaconBlockHeaderNumFields = uint64(5)
Expand Down
137 changes: 135 additions & 2 deletions beacon/deneb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Layr-Labs/eigenpod-proofs-generation/common"
"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"
ssz "github.com/ferranbt/fastssz"
Expand Down Expand Up @@ -194,9 +195,9 @@ func ProveExecutionPayloadAgainstBlockBodyDeneb(beaconBlockBody *deneb.BeaconBlo
hh.Reset()
}

proof, err := common.GetProof(beaconBlockBodyContainerRoots, executionPayloadIndex, blockBodyMerkleSubtreeNumLayers)
proof, err := common.GetProof(beaconBlockBodyContainerRoots, ExecutionPayloadIndex, BlockBodyMerkleSubtreeNumLayers)

return proof, beaconBlockBodyContainerRoots[executionPayloadIndex], err
return proof, beaconBlockBodyContainerRoots[ExecutionPayloadIndex], err
}

// taken from https://github.com/attestantio/go-eth2-client/blob/21f7dd480fed933d8e0b1c88cee67da721c80eb2/spec/deneb/beaconstate_ssz.go#L640
Expand Down Expand Up @@ -743,3 +744,135 @@ func ComputeExecutionPayloadFieldRootsDeneb(executionPayloadFields *deneb.Execut

return executionPayloadFieldRoots, nil
}

func ComputeExecutionPayloadFieldRootsCapella(executionPayloadFields *capella.ExecutionPayload) ([]phase0.Root, error) {
executionPayloadFieldRoots := make([]phase0.Root, 15)
var err error

hh := ssz.NewHasher()

//Field 0: ParentHash
hh.PutBytes(executionPayloadFields.ParentHash[:])
copy(executionPayloadFieldRoots[0][:], hh.Hash())
hh.Reset()

//Field 1: FeeRecipient
hh.PutBytes(executionPayloadFields.FeeRecipient[:])
copy(executionPayloadFieldRoots[1][:], hh.Hash())
hh.Reset()

//Field 2: StateRoot
hh.PutBytes(executionPayloadFields.StateRoot[:])
copy(executionPayloadFieldRoots[2][:], hh.Hash())
hh.Reset()

//Field 3: ReceiptRoot
hh.PutBytes(executionPayloadFields.ReceiptsRoot[:])
copy(executionPayloadFieldRoots[3][:], hh.Hash())
hh.Reset()

//Field 4: LogsBloom
hh.PutBytes(executionPayloadFields.LogsBloom[:])
copy(executionPayloadFieldRoots[4][:], hh.Hash())
hh.Reset()

//Field 5: PrevRandao
hh.PutBytes(executionPayloadFields.PrevRandao[:])
copy(executionPayloadFieldRoots[5][:], hh.Hash())
hh.Reset()

//Field 6: BlockNumber
hh.PutUint64(executionPayloadFields.BlockNumber)
copy(executionPayloadFieldRoots[6][:], hh.Hash())
hh.Reset()

//Field 7: GasLimit
hh.PutUint64(executionPayloadFields.GasLimit)
copy(executionPayloadFieldRoots[7][:], hh.Hash())
hh.Reset()

//Field 8: GasUsed
hh.PutUint64(executionPayloadFields.GasUsed)
copy(executionPayloadFieldRoots[8][:], hh.Hash())
hh.Reset()

//Field 9: Timestamp
hh.PutUint64(executionPayloadFields.Timestamp)
copy(executionPayloadFieldRoots[9][:], hh.Hash())
hh.Reset()

//Field 10: ExtraData

// //If the field is empty, we set it to 0
// if len(executionPayloadFields.ExtraData) == 0 {
// executionPayloadFields.ExtraData = []byte{0}
// }

{
elemIndx := hh.Index()
byteLen := uint64(len(executionPayloadFields.ExtraData))
if byteLen > 32 {
err = ssz.ErrIncorrectListSize
fmt.Println(err)
}
hh.PutBytes(executionPayloadFields.ExtraData)
hh.MerkleizeWithMixin(elemIndx, byteLen, (32+31)/32)
copy(executionPayloadFieldRoots[10][:], hh.Hash())
hh.Reset()
}

//Field 11: BaseFeePerGas
hh.PutBytes(executionPayloadFields.BaseFeePerGas[:])
copy(executionPayloadFieldRoots[11][:], hh.Hash())
hh.Reset()

//Field 12: BlockHash
hh.PutBytes(executionPayloadFields.BlockHash[:])
copy(executionPayloadFieldRoots[12][:], hh.Hash())
hh.Reset()

//Field 13: Transactions
{
subIndx := hh.Index()
num := uint64(len(executionPayloadFields.Transactions))
if num > 1048576 {
err = ssz.ErrIncorrectListSize
fmt.Println(err)
}
for _, elem := range executionPayloadFields.Transactions {
{
elemIndx := hh.Index()
byteLen := uint64(len(elem))
if byteLen > 1073741824 {
err = ssz.ErrIncorrectListSize
fmt.Println(err)
}
hh.AppendBytes32(elem)
hh.MerkleizeWithMixin(elemIndx, byteLen, (1073741824+31)/32)
}
}
hh.MerkleizeWithMixin(subIndx, num, 1048576)
copy(executionPayloadFieldRoots[13][:], hh.Hash())
hh.Reset()
}

//Field 14: Withdrawals
{
subIndx := hh.Index()
num := uint64(len(executionPayloadFields.Withdrawals))
if num > 16 {
err := ssz.ErrIncorrectListSize
return nil, err
}
for _, elem := range executionPayloadFields.Withdrawals {
if err = elem.HashTreeRootWith(hh); err != nil {
return nil, err
}
}
hh.MerkleizeWithMixin(subIndx, num, 16)
copy(executionPayloadFieldRoots[14][:], hh.Hash())
hh.Reset()
}

return executionPayloadFieldRoots, nil
}
14 changes: 7 additions & 7 deletions beacon/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func GetBalanceRoots(balances []phase0.Gwei) ([]phase0.Root, error) {

func ProveBlockRootAgainstBeaconStateViaHistoricalSummaries(beaconStateTopLevelRoots *BeaconStateTopLevelRoots, historicalSummaries []*capella.HistoricalSummary, historicalBlockRoots []phase0.Root, historicalSummaryIndex uint64, blockRootIndex uint64) ([][32]byte, error) {
// prove the historical summaries against the beacon state
historicalSummariesListAgainstBeaconState, err := ProveBeaconTopLevelRootAgainstBeaconState(beaconStateTopLevelRoots, historicalSummaryListIndex)
historicalSummariesListAgainstBeaconState, err := ProveBeaconTopLevelRootAgainstBeaconState(beaconStateTopLevelRoots, HistoricalSummaryListIndex)
if err != nil {
return nil, err
}
Expand All @@ -98,7 +98,7 @@ func ProveBlockRootAgainstBeaconStateViaHistoricalSummaries(beaconStateTopLevelR
}

// historical block roots are incklude the really old block root you wanna prove
beaconBlockHeaderRootsProof, err := common.GetProof(historicalBlockRoots, blockRootIndex, blockRootsMerkleSubtreeNumLayers)
beaconBlockHeaderRootsProof, err := common.GetProof(historicalBlockRoots, blockRootIndex, BlockRootsMerkleSubtreeNumLayers)
if err != nil {
return nil, err
}
Expand All @@ -120,7 +120,7 @@ func ProveHistoricalSummaryAgainstHistoricalSummariesList(historicalSummaries []
historicalSummaryNodeList[i] = phase0.Root(historicalSummaryRoot)
}

proof, err := common.GetProof(historicalSummaryNodeList, historicalSummaryIndex, historicalSummaryListMerkleSubtreeNumLayers)
proof, err := common.GetProof(historicalSummaryNodeList, historicalSummaryIndex, HistoricalSummaryListMerkleSubtreeNumLayers)

if err != nil {
return nil, err
Expand All @@ -143,7 +143,7 @@ func ProveBlockRootListAgainstHistoricalSummary(historicalSummary *capella.Histo
}

func ProveBlockRootAgainstBlockRootsList(blockRoots []phase0.Root, blockRootIndex uint64) (common.Proof, error) {
proof, err := common.GetProof(blockRoots, blockRootIndex, blockRootsMerkleSubtreeNumLayers)
proof, err := common.GetProof(blockRoots, blockRootIndex, BlockRootsMerkleSubtreeNumLayers)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func ProveWithdrawalAgainstExecutionPayload(
}

func ProveWithdrawalListAgainstExecutionPayload(executionPayloadFieldRoots []phase0.Root) (common.Proof, error) {
return common.GetProof(executionPayloadFieldRoots, withdrawalsIndex, common.CeilLog2(len(executionPayloadFieldRoots)))
return common.GetProof(executionPayloadFieldRoots, WithdrawalsIndex, common.CeilLog2(len(executionPayloadFieldRoots)))
}

func ProveWithdrawalAgainstWithdrawalList(withdrawals []*capella.Withdrawal, withdrawalIndex uint8) (common.Proof, error) {
Expand All @@ -189,7 +189,7 @@ func ProveWithdrawalAgainstWithdrawalList(withdrawals []*capella.Withdrawal, wit
withdrawalNodeList[i] = phase0.Root(withdrawalRoot)
}

proof, err := common.GetProof(withdrawalNodeList, uint64(withdrawalIndex), withdrawalListMerkleSubtreeNumLayers)
proof, err := common.GetProof(withdrawalNodeList, uint64(withdrawalIndex), WithdrawalListMerkleSubtreeNumLayers)
if err != nil {
return nil, err
}
Expand All @@ -202,5 +202,5 @@ func ProveWithdrawalAgainstWithdrawalList(withdrawals []*capella.Withdrawal, wit
}

func ProveTimestampAgainstExecutionPayload(executionPayloadFieldRoots []phase0.Root) (common.Proof, error) {
return common.GetProof(executionPayloadFieldRoots, timestampIndex, common.CeilLog2(len(executionPayloadFieldRoots)))
return common.GetProof(executionPayloadFieldRoots, TimestampIndex, common.CeilLog2(len(executionPayloadFieldRoots)))
}
2 changes: 1 addition & 1 deletion eigen_pod_proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (epp *EigenPodProofs) ComputeValidatorTree(slot phase0.Slot, validators []*
}

// compute the validator tree
validatorTree, err := common.ComputeMerkleTreeFromLeaves(validatorLeaves, validatorListMerkleSubtreeNumLayers)
validatorTree, err := common.ComputeMerkleTreeFromLeaves(validatorLeaves, beacon.ValidatorListMerkleSubtreeNumLayers)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 7d8a141

Please sign in to comment.