Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deneb cleanup #22

Merged
merged 15 commits into from
Jan 25, 2024
63 changes: 0 additions & 63 deletions beacon/beacon_constants.go

This file was deleted.

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
68 changes: 68 additions & 0 deletions beacon/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package beacon

const (
SlotsPerHistoricalRoot = uint64(8192)

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

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

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

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

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

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

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

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

//
//
// **************Number of Layers in Various Subtrees**************
//
//

const (
// Number of layers for various merkle subtrees
blockHeaderMerkleSubtreeNumLayers = uint64(3)

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)

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

// Number of layers for the merkleization of the Withdrawal List in the Exection Payload
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)

// **************Number of fields of various containers**************
beaconBlockHeaderNumFields = uint64(5)
)
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is capella in deneb.go?

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