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

Initial support for electra #41

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 117 additions & 66 deletions eth2/beacon/common/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,50 @@ type Hash32 = Root

const Hash32Type = RootType

const (
MAX_STEMS = 1 << 16
MAX_COMMITMENTS_PER_STEM = 33 // = 31 for inner nodes + 2 (C1/C2)
VERKLE_WIDTH = 256
IPA_PROOF_DEPTH = 8 // = log2(VERKLE_WIDTH)
)

var StemType RootMeta = 31

var SuffixStateDiffType = ContainerType("SuffixStateDiff", []FieldDef{
{"suffix", ByteType},
{"current_value", UnionType([]TypeDef{nil, Bytes32Type})},
// Uncomment this for all testnets after Kaustinen
// {"new_value", UnionType([]TypeDef{nil, Bytes32Type})},
})

var StemStateDiffType = ContainerType("StemStateDiff", []FieldDef{
{"stem", StemType},
{"suffix_diffs", ListType(SuffixStateDiffType, VERKLE_WIDTH)},
})

var IpaProofType = ContainerType("IpaProof", []FieldDef{
{"C_L", VectorType(Bytes32Type, IPA_PROOF_DEPTH)},
{"C_R", VectorType(Bytes32Type, IPA_PROOF_DEPTH)},
{"final_evaluation", Bytes32Type},
})

var VerkleProofFields = []FieldDef{
{"other_stems", ListType(StemType, MAX_STEMS)},
{"depth_extension_present", ListType(Uint8Type, MAX_STEMS)},
{"commitments_by_path", ListType(Bytes32Type, MAX_STEMS*MAX_COMMITMENTS_PER_STEM)},
{"D", Bytes32Type},
{"ipa_poof", IpaProofType},
}

var VerkleProofType = ContainerType("VerkleProof", VerkleProofFields)

var ExecutionWitnessFields = []FieldDef{
{"state_diff", ListType(StemStateDiffType, MAX_STEMS)},
{"verkle_proof", VerkleProofType},
}

var ExecutionWitnessType = ContainerType("ExecutionWitness", ExecutionWitnessFields)

var ExecutionPayloadHeaderType = ContainerType("ExecutionPayloadHeader", []FieldDef{
{"parent_hash", Hash32Type},
{"fee_recipient", Eth1AddressType},
Expand All @@ -32,6 +76,7 @@ var ExecutionPayloadHeaderType = ContainerType("ExecutionPayloadHeader", []Field
{"base_fee_per_gas", Uint256Type},
{"block_hash", Hash32Type},
{"transactions_root", RootType},
{"execution_witness", RootType},
})

type ExecutionPayloadHeaderView struct {
Expand Down Expand Up @@ -60,6 +105,7 @@ func (v *ExecutionPayloadHeaderView) Raw() (*ExecutionPayloadHeader, error) {
baseFeePerGas, err := AsUint256(values[11], err)
blockHash, err := AsRoot(values[12], err)
transactionsRoot, err := AsRoot(values[13], err)
executionWitnessRoot, err := AsRoot(values[14], err)
if err != nil {
return nil, err
}
Expand All @@ -72,20 +118,21 @@ func (v *ExecutionPayloadHeaderView) Raw() (*ExecutionPayloadHeader, error) {
return nil, err
}
return &ExecutionPayloadHeader{
ParentHash: parentHash,
FeeRecipient: feeRecipient,
StateRoot: stateRoot,
ReceiptsRoot: receiptsRoot,
LogsBloom: *logsBloom,
PrevRandao: prevRandao,
BlockNumber: blockNumber,
GasLimit: gasLimit,
GasUsed: gasUsed,
Timestamp: timestamp,
ExtraData: extraData,
BaseFeePerGas: baseFeePerGas,
BlockHash: blockHash,
TransactionsRoot: transactionsRoot,
ParentHash: parentHash,
FeeRecipient: feeRecipient,
StateRoot: stateRoot,
ReceiptsRoot: receiptsRoot,
LogsBloom: *logsBloom,
PrevRandao: prevRandao,
BlockNumber: blockNumber,
GasLimit: gasLimit,
GasUsed: gasUsed,
Timestamp: timestamp,
ExtraData: extraData,
BaseFeePerGas: baseFeePerGas,
BlockHash: blockHash,
TransactionsRoot: transactionsRoot,
ExecutionWitnessRoot: executionWitnessRoot,
}, nil
}

Expand Down Expand Up @@ -151,20 +198,21 @@ func AsExecutionPayloadHeader(v View, err error) (*ExecutionPayloadHeaderView, e
}

type ExecutionPayloadHeader struct {
ParentHash Hash32 `json:"parent_hash" yaml:"parent_hash"`
FeeRecipient Eth1Address `json:"fee_recipient" yaml:"fee_recipient"`
StateRoot Bytes32 `json:"state_root" yaml:"state_root"`
ReceiptsRoot Bytes32 `json:"receipts_root" yaml:"receipts_root"`
LogsBloom LogsBloom `json:"logs_bloom" yaml:"logs_bloom"`
PrevRandao Bytes32 `json:"prev_randao" yaml:"prev_randao"`
BlockNumber Uint64View `json:"block_number" yaml:"block_number"`
GasLimit Uint64View `json:"gas_limit" yaml:"gas_limit"`
GasUsed Uint64View `json:"gas_used" yaml:"gas_used"`
Timestamp Timestamp `json:"timestamp" yaml:"timestamp"`
ExtraData ExtraData `json:"extra_data" yaml:"extra_data"`
BaseFeePerGas Uint256View `json:"base_fee_per_gas" yaml:"base_fee_per_gas"`
BlockHash Hash32 `json:"block_hash" yaml:"block_hash"`
TransactionsRoot Root `json:"transactions_root" yaml:"transactions_root"`
ParentHash Hash32 `json:"parent_hash" yaml:"parent_hash"`
FeeRecipient Eth1Address `json:"fee_recipient" yaml:"fee_recipient"`
StateRoot Bytes32 `json:"state_root" yaml:"state_root"`
ReceiptsRoot Bytes32 `json:"receipts_root" yaml:"receipts_root"`
LogsBloom LogsBloom `json:"logs_bloom" yaml:"logs_bloom"`
PrevRandao Bytes32 `json:"prev_randao" yaml:"prev_randao"`
BlockNumber Uint64View `json:"block_number" yaml:"block_number"`
GasLimit Uint64View `json:"gas_limit" yaml:"gas_limit"`
GasUsed Uint64View `json:"gas_used" yaml:"gas_used"`
Timestamp Timestamp `json:"timestamp" yaml:"timestamp"`
ExtraData ExtraData `json:"extra_data" yaml:"extra_data"`
BaseFeePerGas Uint256View `json:"base_fee_per_gas" yaml:"base_fee_per_gas"`
BlockHash Hash32 `json:"block_hash" yaml:"block_hash"`
TransactionsRoot Root `json:"transactions_root" yaml:"transactions_root"`
ExecutionWitnessRoot Root `json:"execution_witness_root" yaml:"execution_witness_root"`
}

func (s *ExecutionPayloadHeader) View() *ExecutionPayloadHeaderView {
Expand All @@ -175,8 +223,8 @@ func (s *ExecutionPayloadHeader) View() *ExecutionPayloadHeaderView {
pr, cb, sr, rr := (*RootView)(&s.ParentHash), s.FeeRecipient.View(), (*RootView)(&s.StateRoot), (*RootView)(&s.ReceiptsRoot)
lb, rng, nr, gl, gu := s.LogsBloom.View(), (*RootView)(&s.PrevRandao), s.BlockNumber, s.GasLimit, s.GasUsed
ts, bf, bh, tr := Uint64View(s.Timestamp), &s.BaseFeePerGas, (*RootView)(&s.BlockHash), (*RootView)(&s.TransactionsRoot)

v, err := AsExecutionPayloadHeader(ExecutionPayloadHeaderType.FromFields(pr, cb, sr, rr, lb, rng, nr, gl, gu, ts, ed, bf, bh, tr))
ew := (*RootView)(&s.ExecutionWitnessRoot)
v, err := AsExecutionPayloadHeader(ExecutionPayloadHeaderType.FromFields(pr, cb, sr, rr, lb, rng, nr, gl, gu, ts, ed, bf, bh, tr, ew))
if err != nil {
panic(err)
}
Expand All @@ -186,19 +234,19 @@ func (s *ExecutionPayloadHeader) View() *ExecutionPayloadHeaderView {
func (s *ExecutionPayloadHeader) Deserialize(dr *codec.DecodingReader) error {
return dr.Container(&s.ParentHash, &s.FeeRecipient, &s.StateRoot,
&s.ReceiptsRoot, &s.LogsBloom, &s.PrevRandao, &s.BlockNumber, &s.GasLimit,
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, &s.TransactionsRoot)
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, &s.TransactionsRoot, &s.ExecutionWitnessRoot)
}

func (s *ExecutionPayloadHeader) Serialize(w *codec.EncodingWriter) error {
return w.Container(&s.ParentHash, &s.FeeRecipient, &s.StateRoot,
&s.ReceiptsRoot, &s.LogsBloom, &s.PrevRandao, &s.BlockNumber, &s.GasLimit,
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, &s.TransactionsRoot)
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, &s.TransactionsRoot, &s.ExecutionWitnessRoot)
}

func (s *ExecutionPayloadHeader) ByteLength() uint64 {
return codec.ContainerLength(&s.ParentHash, &s.FeeRecipient, &s.StateRoot,
&s.ReceiptsRoot, &s.LogsBloom, &s.PrevRandao, &s.BlockNumber, &s.GasLimit,
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, &s.TransactionsRoot)
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, &s.TransactionsRoot, &s.ExecutionWitnessRoot)
}

func (b *ExecutionPayloadHeader) FixedLength() uint64 {
Expand All @@ -208,7 +256,7 @@ func (b *ExecutionPayloadHeader) FixedLength() uint64 {
func (s *ExecutionPayloadHeader) HashTreeRoot(hFn tree.HashFn) Root {
return hFn.HashTreeRoot(&s.ParentHash, &s.FeeRecipient, &s.StateRoot,
&s.ReceiptsRoot, &s.LogsBloom, &s.PrevRandao, &s.BlockNumber, &s.GasLimit,
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, &s.TransactionsRoot)
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, &s.TransactionsRoot, &s.ExecutionWitnessRoot)
}

func ExecutionPayloadType(spec *Spec) *ContainerTypeDef {
Expand All @@ -227,6 +275,7 @@ func ExecutionPayloadType(spec *Spec) *ContainerTypeDef {
{"base_fee_per_gas", Uint256Type},
{"block_hash", Hash32Type},
{"transactions", PayloadTransactionsType(spec)},
{"execution_witness", ExecutionWitnessType},
})
}

Expand Down Expand Up @@ -303,38 +352,39 @@ func (v *ExtraDataView) Raw() (ExtraData, error) {
}

type ExecutionPayload struct {
ParentHash Hash32 `json:"parent_hash" yaml:"parent_hash"`
FeeRecipient Eth1Address `json:"fee_recipient" yaml:"fee_recipient"`
StateRoot Bytes32 `json:"state_root" yaml:"state_root"`
ReceiptsRoot Bytes32 `json:"receipts_root" yaml:"receipts_root"`
LogsBloom LogsBloom `json:"logs_bloom" yaml:"logs_bloom"`
PrevRandao Bytes32 `json:"prev_randao" yaml:"prev_randao"`
BlockNumber Uint64View `json:"block_number" yaml:"block_number"`
GasLimit Uint64View `json:"gas_limit" yaml:"gas_limit"`
GasUsed Uint64View `json:"gas_used" yaml:"gas_used"`
Timestamp Timestamp `json:"timestamp" yaml:"timestamp"`
ExtraData ExtraData `json:"extra_data" yaml:"extra_data"`
BaseFeePerGas Uint256View `json:"base_fee_per_gas" yaml:"base_fee_per_gas"`
BlockHash Hash32 `json:"block_hash" yaml:"block_hash"`
Transactions PayloadTransactions `json:"transactions" yaml:"transactions"`
ParentHash Hash32 `json:"parent_hash" yaml:"parent_hash"`
FeeRecipient Eth1Address `json:"fee_recipient" yaml:"fee_recipient"`
StateRoot Bytes32 `json:"state_root" yaml:"state_root"`
ReceiptsRoot Bytes32 `json:"receipts_root" yaml:"receipts_root"`
LogsBloom LogsBloom `json:"logs_bloom" yaml:"logs_bloom"`
PrevRandao Bytes32 `json:"prev_randao" yaml:"prev_randao"`
BlockNumber Uint64View `json:"block_number" yaml:"block_number"`
GasLimit Uint64View `json:"gas_limit" yaml:"gas_limit"`
GasUsed Uint64View `json:"gas_used" yaml:"gas_used"`
Timestamp Timestamp `json:"timestamp" yaml:"timestamp"`
ExtraData ExtraData `json:"extra_data" yaml:"extra_data"`
BaseFeePerGas Uint256View `json:"base_fee_per_gas" yaml:"base_fee_per_gas"`
BlockHash Hash32 `json:"block_hash" yaml:"block_hash"`
Transactions PayloadTransactions `json:"transactions" yaml:"transactions"`
ExecutionWitness ExecutionWitness `json:"execution_witness", yaml:"execution_witness"`
}

func (s *ExecutionPayload) Deserialize(spec *Spec, dr *codec.DecodingReader) error {
return dr.Container(&s.ParentHash, &s.FeeRecipient, &s.StateRoot,
&s.ReceiptsRoot, &s.LogsBloom, &s.PrevRandao, &s.BlockNumber, &s.GasLimit,
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, spec.Wrap(&s.Transactions))
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, spec.Wrap(&s.Transactions), &s.ExecutionWitness)
}

func (s *ExecutionPayload) Serialize(spec *Spec, w *codec.EncodingWriter) error {
return w.Container(&s.ParentHash, &s.FeeRecipient, &s.StateRoot,
&s.ReceiptsRoot, &s.LogsBloom, &s.PrevRandao, &s.BlockNumber, &s.GasLimit,
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, spec.Wrap(&s.Transactions))
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, spec.Wrap(&s.Transactions), &s.ExecutionWitness)
}

func (s *ExecutionPayload) ByteLength(spec *Spec) uint64 {
return codec.ContainerLength(&s.ParentHash, &s.FeeRecipient, &s.StateRoot,
&s.ReceiptsRoot, &s.LogsBloom, &s.PrevRandao, &s.BlockNumber, &s.GasLimit,
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, spec.Wrap(&s.Transactions))
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, spec.Wrap(&s.Transactions), &s.ExecutionWitness)
}

func (a *ExecutionPayload) FixedLength(*Spec) uint64 {
Expand All @@ -345,25 +395,26 @@ func (a *ExecutionPayload) FixedLength(*Spec) uint64 {
func (s *ExecutionPayload) HashTreeRoot(spec *Spec, hFn tree.HashFn) Root {
return hFn.HashTreeRoot(&s.ParentHash, &s.FeeRecipient, &s.StateRoot,
&s.ReceiptsRoot, &s.LogsBloom, &s.PrevRandao, &s.BlockNumber, &s.GasLimit,
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, spec.Wrap(&s.Transactions))
&s.GasUsed, &s.Timestamp, &s.ExtraData, &s.BaseFeePerGas, &s.BlockHash, spec.Wrap(&s.Transactions), &s.ExecutionWitness)
}

func (ep *ExecutionPayload) Header(spec *Spec) *ExecutionPayloadHeader {
return &ExecutionPayloadHeader{
ParentHash: ep.ParentHash,
FeeRecipient: ep.FeeRecipient,
StateRoot: ep.StateRoot,
ReceiptsRoot: ep.ReceiptsRoot,
LogsBloom: ep.LogsBloom,
PrevRandao: ep.PrevRandao,
BlockNumber: ep.BlockNumber,
GasLimit: ep.GasLimit,
GasUsed: ep.GasUsed,
Timestamp: ep.Timestamp,
ExtraData: ep.ExtraData,
BaseFeePerGas: ep.BaseFeePerGas,
BlockHash: ep.BlockHash,
TransactionsRoot: ep.Transactions.HashTreeRoot(spec, tree.GetHashFn()),
ParentHash: ep.ParentHash,
FeeRecipient: ep.FeeRecipient,
StateRoot: ep.StateRoot,
ReceiptsRoot: ep.ReceiptsRoot,
LogsBloom: ep.LogsBloom,
PrevRandao: ep.PrevRandao,
BlockNumber: ep.BlockNumber,
GasLimit: ep.GasLimit,
GasUsed: ep.GasUsed,
Timestamp: ep.Timestamp,
ExtraData: ep.ExtraData,
BaseFeePerGas: ep.BaseFeePerGas,
BlockHash: ep.BlockHash,
TransactionsRoot: ep.Transactions.HashTreeRoot(spec, tree.GetHashFn()),
ExecutionWitnessRoot: ep.ExecutionWitness.HashTreeRoot(tree.GetHashFn()),
}
}

Expand Down
Loading