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

Electra upgrade #1283

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion relayer/cmd/import_beacon_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func importBeaconState(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("read finalized state data from file: %w", err)
}

afterDenebFork := (conf.Source.Beacon.Spec.DenebForkEpoch + 1) * 32
afterDenebFork := (conf.Source.Beacon.Spec.ForkVersions.Deneb + 1) * 32

attestedState, err := syncer.UnmarshalBeaconState(afterDenebFork, attestedData)
if err != nil {
Expand Down
147 changes: 1 addition & 146 deletions relayer/contracts/gateway.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion relayer/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Build() {
}

func BuildMain() error {
err := sh.Run("sszgen", "--path", "relays/beacon/state", "--objs", "BeaconStateCapellaMainnet,BlockRootsContainerMainnet,TransactionsRootContainer,BeaconBlockCapellaMainnet,WithdrawalsRootContainerMainnet,BeaconStateDenebMainnet,BeaconBlockDenebMainnet")
err := sh.Run("sszgen", "--path", "relays/beacon/state", "--objs", "BeaconStateCapellaMainnet,BlockRootsContainerMainnet,TransactionsRootContainer,BeaconBlockCapellaMainnet,WithdrawalsRootContainerMainnet,BeaconStateDenebMainnet,BeaconBlockDenebMainnet,BeaconStateElectra,BeaconBlockElectra")
if err != nil {
return err
}
Expand Down
13 changes: 9 additions & 4 deletions relayer/relays/beacon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ type Config struct {
}

type SpecSettings struct {
SyncCommitteeSize uint64 `mapstructure:"syncCommitteeSize"`
SlotsInEpoch uint64 `mapstructure:"slotsInEpoch"`
EpochsPerSyncCommitteePeriod uint64 `mapstructure:"epochsPerSyncCommitteePeriod"`
DenebForkEpoch uint64 `mapstructure:"denebForkedEpoch"`
SyncCommitteeSize uint64 `mapstructure:"syncCommitteeSize"`
SlotsInEpoch uint64 `mapstructure:"slotsInEpoch"`
EpochsPerSyncCommitteePeriod uint64 `mapstructure:"epochsPerSyncCommitteePeriod"`
ForkVersions ForkVersions `mapstructure:"forkVersions"`
}

type ForkVersions struct {
Deneb uint64 `mapstructure:"deneb"`
Electra uint64 `mapstructure:"electra"`
}

type SourceConfig struct {
Expand Down
48 changes: 0 additions & 48 deletions relayer/relays/beacon/header/syncer/api/api_deneb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package api
import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
beaconjson "github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/json"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale"
"github.com/snowfork/snowbridge/relayer/relays/beacon/state"
"github.com/snowfork/snowbridge/relayer/relays/util"
Expand Down Expand Up @@ -55,49 +53,3 @@ func DenebExecutionPayloadToScale(e *state.ExecutionPayloadDeneb) (scale.Executi
}, nil
}

func DenebJsonExecutionPayloadHeaderToScale(e *beaconjson.FullExecutionPayloadHeaderJson) (scale.ExecutionPayloadHeaderDeneb, 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.

Unused method.

var executionPayloadHeader scale.ExecutionPayloadHeaderDeneb
var baseFeePerGas big.Int
baseFeePerGasU64, err := util.ToUint64(e.BaseFeePerGas)
if err != nil {
return executionPayloadHeader, err
}
blockNumber, err := util.ToUint64(e.BlockNumber)
if err != nil {
return executionPayloadHeader, err
}
baseFeePerGas.SetUint64(baseFeePerGasU64)
gasLimit, err := util.ToUint64(e.GasLimit)
if err != nil {
return executionPayloadHeader, err
}
gasUsed, err := util.ToUint64(e.GasUsed)
if err != nil {
return executionPayloadHeader, err
}
timestamp, err := util.ToUint64(e.Timestamp)
if err != nil {
return executionPayloadHeader, err
}
blobGasUsed, _ := util.ToUint64(e.BlobGasUsed)
excessBlobGas, _ := util.ToUint64(e.ExcessBlobGas)
return scale.ExecutionPayloadHeaderDeneb{
ParentHash: types.NewH256(common.HexToHash(e.ParentHash).Bytes()),
FeeRecipient: types.NewH160(common.HexToAddress(e.FeeRecipient).Bytes()),
StateRoot: types.NewH256(common.HexToHash(e.StateRoot).Bytes()),
ReceiptsRoot: types.NewH256(common.HexToHash(e.ReceiptsRoot).Bytes()),
LogsBloom: common.FromHex(e.LogsBloom),
PrevRandao: types.NewH256(common.HexToHash(e.PrevRandao).Bytes()),
BlockNumber: types.NewU64(blockNumber),
GasLimit: types.NewU64(gasLimit),
GasUsed: types.NewU64(gasUsed),
Timestamp: types.NewU64(timestamp),
ExtraData: common.FromHex(e.ExtraData),
BaseFeePerGas: types.NewU256(baseFeePerGas),
BlockHash: types.NewH256(common.HexToHash(e.BlockHash).Bytes()),
TransactionsRoot: types.NewH256(common.HexToHash(e.TransactionsRoot).Bytes()),
WithdrawalsRoot: types.NewH256(common.HexToHash(e.WithdrawalsRoot).Bytes()),
BlobGasUsed: types.NewU64(blobGasUsed),
ExcessBlobGas: types.NewU64(excessBlobGas),
}, nil
}
128 changes: 128 additions & 0 deletions relayer/relays/beacon/header/syncer/api/api_electra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package api

import (
"math/big"

"github.com/snowfork/go-substrate-rpc-client/v4/types"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale"
"github.com/snowfork/snowbridge/relayer/relays/beacon/state"
"github.com/snowfork/snowbridge/relayer/relays/util"
)

func ElectraExecutionPayloadToScale(e *state.ExecutionPayloadElectra) (scale.ExecutionPayloadHeaderDeneb, error) {
var payloadHeader scale.ExecutionPayloadHeaderDeneb
transactionsContainer := state.TransactionsRootContainer{}
transactionsContainer.Transactions = e.Transactions

transactionsRoot, err := transactionsContainer.HashTreeRoot()
if err != nil {
return payloadHeader, err
}

var withdrawalRoot types.H256

withdrawalContainer := state.WithdrawalsRootContainerMainnet{}
withdrawalContainer.Withdrawals = e.Withdrawals
withdrawalRoot, err = withdrawalContainer.HashTreeRoot()
if err != nil {
return payloadHeader, err
}

baseFeePerGas := big.Int{}
// Change BaseFeePerGas back from little-endian to big-endian
baseFeePerGas.SetBytes(util.ChangeByteOrder(e.BaseFeePerGas[:]))

return scale.ExecutionPayloadHeaderDeneb{
ParentHash: types.NewH256(e.ParentHash[:]),
FeeRecipient: e.FeeRecipient,
StateRoot: types.NewH256(e.StateRoot[:]),
ReceiptsRoot: types.NewH256(e.ReceiptsRoot[:]),
LogsBloom: e.LogsBloom[:],
PrevRandao: types.NewH256(e.PrevRandao[:]),
BlockNumber: types.NewU64(e.BlockNumber),
GasLimit: types.NewU64(e.GasLimit),
GasUsed: types.NewU64(e.GasUsed),
Timestamp: types.NewU64(e.Timestamp),
ExtraData: e.ExtraData,
BaseFeePerGas: types.NewU256(baseFeePerGas),
BlockHash: types.NewH256(e.BlockHash[:]),
TransactionsRoot: transactionsRoot,
WithdrawalsRoot: withdrawalRoot,
BlobGasUsed: types.NewU64(e.BlobGasUsed),
ExcessBlobGas: types.NewU64(e.ExcessBlobGas),
}, nil
}

func (a AttesterSlashingResponse) ToFastSSZElectra() (*state.AttesterSlashingElectra, error) {
attestation1, err := a.Attestation1.ToFastSSZElectra()
if err != nil {
return nil, err
}

attestation2, err := a.Attestation2.ToFastSSZElectra()
if err != nil {
return nil, err
}

return &state.AttesterSlashingElectra{
Attestation1: attestation1,
Attestation2: attestation2,
}, nil
}

func (i IndexedAttestationResponse) ToFastSSZElectra() (*state.IndexedAttestationElectra, error) {
data, err := i.Data.ToFastSSZ()
if err != nil {
return nil, err
}

attestationIndexes := []uint64{}
for _, index := range i.AttestingIndices {
indexInt, err := util.ToUint64(index)
if err != nil {
return nil, err
}

attestationIndexes = append(attestationIndexes, indexInt)
}

signature, err := util.HexStringToByteArray(i.Signature)
if err != nil {
return nil, err
}

return &state.IndexedAttestationElectra{
AttestationIndices: attestationIndexes,
Data: data,
Signature: signature,
}, nil
}

func (a AttestationResponse) ToFastSSZElectra() (*state.AttestationElectra, error) {
data, err := a.Data.ToFastSSZ()
if err != nil {
return nil, err
}

aggregationBits, err := util.HexStringToByteArray(a.AggregationBits)
if err != nil {
return nil, err
}

signature, err := util.HexStringTo96Bytes(a.Signature)
if err != nil {
return nil, err
}

committeeBits, err := util.HexStringToByteArray(a.CommitteeBits)
if err != nil {
return nil, err
}

return &state.AttestationElectra{
AggregationBits: aggregationBits,
Data: data,
Signature: signature,
CommitteeBits: committeeBits,
}, nil
}
115 changes: 93 additions & 22 deletions relayer/relays/beacon/header/syncer/api/api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"github.com/snowfork/snowbridge/relayer/relays/beacon/protocol"
"math/big"
"strconv"

Expand Down Expand Up @@ -150,6 +151,7 @@ type AttestationResponse struct {
AggregationBits string `json:"aggregation_bits"`
Data AttestationDataResponse `json:"data"`
Signature string `json:"signature"`
CommitteeBits string `json:"committee_bits,omitempty"`
}

type SignedVoluntaryExitResponse struct {
Expand Down Expand Up @@ -461,7 +463,7 @@ func (s SyncAggregateResponse) ToScale() (scale.SyncAggregate, error) {
// Because it only returns JSON, we need this interim step where we convert the block JSON to the data
// types that the FastSSZ lib expects. When Lodestar supports SSZ block response, we can remove all these
// and directly unmarshal SSZ bytes to state.BeaconBlock.
func (b BeaconBlockResponse) ToFastSSZ(isDeneb bool) (state.BeaconBlock, error) {
func (b BeaconBlockResponse) ToFastSSZ(forkVersion protocol.ForkVersion) (state.BeaconBlock, error) {
data := b.Data.Message

slot, err := util.ToUint64(data.Slot)
Expand Down Expand Up @@ -521,26 +523,6 @@ func (b BeaconBlockResponse) ToFastSSZ(isDeneb bool) (state.BeaconBlock, error)
proposerSlashings = append(proposerSlashings, proposerSlashingSSZ)
}

attesterSlashings := []*state.AttesterSlashing{}
for _, attesterSlashing := range body.AttesterSlashings {
attesterSlashingSSZ, err := attesterSlashing.ToFastSSZ()
if err != nil {
return nil, err
}

attesterSlashings = append(attesterSlashings, attesterSlashingSSZ)
}

attestations := []*state.Attestation{}
for _, attestation := range body.Attestations {
attestationSSZ, err := attestation.ToFastSSZ()
if err != nil {
return nil, err
}

attestations = append(attestations, attestationSSZ)
}

deposits := []*state.Deposit{}
for _, deposit := range body.Deposits {
depositScale, err := deposit.ToFastSSZ()
Expand Down Expand Up @@ -691,7 +673,96 @@ func (b BeaconBlockResponse) ToFastSSZ(isDeneb bool) (state.BeaconBlock, error)
kzgCommitments = append(kzgCommitments, kzgCommitmentSSZ)
}

if isDeneb {
if forkVersion == protocol.Electra {

attesterSlashings := []*state.AttesterSlashingElectra{}
for _, attesterSlashing := range body.AttesterSlashings {
attesterSlashingSSZ, err := attesterSlashing.ToFastSSZElectra()
if err != nil {
return nil, err
}

attesterSlashings = append(attesterSlashings, attesterSlashingSSZ)
}

attestations := []*state.AttestationElectra{}
for _, attestation := range body.Attestations {
attestationSSZ, err := attestation.ToFastSSZElectra()
if err != nil {
return nil, err
}

attestations = append(attestations, attestationSSZ)
}

return &state.BeaconBlockElectra{
Slot: slot,
ProposerIndex: proposerIndex,
ParentRoot: parentRoot,
StateRoot: stateRoot,
Body: &state.BeaconBlockBodyElectra{
RandaoReveal: randaoReveal,
Eth1Data: &state.Eth1Data{
DepositRoot: eth1DepositRoot,
DepositCount: eth1DepositCount,
BlockHash: eth1BlockHash,
},
Graffiti: graffiti,
ProposerSlashings: proposerSlashings,
AttesterSlashings: attesterSlashings,
Attestations: attestations,
Deposits: deposits,
VoluntaryExits: voluntaryExits,
SyncAggregate: &state.SyncAggregateMainnet{
SyncCommitteeBits: syncCommitteeBits,
SyncCommitteeSignature: syncCommitteeSignature,
},
ExecutionPayload: &state.ExecutionPayloadElectra{
ParentHash: parentHash,
FeeRecipient: feeRecipient,
StateRoot: executionStateRoot,
ReceiptsRoot: receiptsRoot,
LogsBloom: logsBloom,
PrevRandao: prevRando,
BlockNumber: blockNumber,
GasLimit: gasLimit,
GasUsed: gasUsed,
Timestamp: timestamp,
ExtraData: extraData,
BaseFeePerGas: baseFeePerGasBytes,
BlockHash: blockHash,
Transactions: transactions,
Withdrawals: withdrawals,
BlobGasUsed: blobGasUsed,
ExcessBlobGas: excessBlobGas,
},
BlsToExecutionChanges: blsExecutionChanges,
BlobKzgCommitments: kzgCommitments,
},
}, nil
}

attesterSlashings := []*state.AttesterSlashing{}
for _, attesterSlashing := range body.AttesterSlashings {
attesterSlashingSSZ, err := attesterSlashing.ToFastSSZ()
if err != nil {
return nil, err
}

attesterSlashings = append(attesterSlashings, attesterSlashingSSZ)
}

attestations := []*state.Attestation{}
for _, attestation := range body.Attestations {
attestationSSZ, err := attestation.ToFastSSZ()
if err != nil {
return nil, err
}

attestations = append(attestations, attestationSSZ)
}

if forkVersion == protocol.Deneb {
return &state.BeaconBlockDenebMainnet{
Slot: slot,
ProposerIndex: proposerIndex,
Expand Down
Loading
Loading