-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathpayload.go
More file actions
161 lines (139 loc) · 5.57 KB
/
payload.go
File metadata and controls
161 lines (139 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
package genesis
import (
"fmt"
"github.com/berachain/beacon-kit/cli/context"
"github.com/berachain/beacon-kit/consensus-types/types"
engineprimitives "github.com/berachain/beacon-kit/engine-primitives/engine-primitives"
"github.com/berachain/beacon-kit/errors"
gethprimitives "github.com/berachain/beacon-kit/geth-primitives"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/constants"
"github.com/berachain/beacon-kit/primitives/encoding/json"
"github.com/berachain/beacon-kit/primitives/math"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
func AddExecutionPayloadCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "execution-payload [eth/genesis/file.json]",
Short: "adds the eth1 genesis execution payload to the genesis file",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return AddExecutionPayload(args[0], context.GetConfigFromCmd(cmd))
},
}
return cmd
}
func AddExecutionPayload(elGenesisPath string, config *cmtcfg.Config) error {
genesisBz, err := afero.ReadFile(afero.NewOsFs(), elGenesisPath)
if err != nil {
return errors.Wrap(err, "failed to read eth1 genesis file")
}
// Unmarshal the EL genesis file.
ethGenesis := &gethprimitives.Genesis{}
if err = ethGenesis.UnmarshalJSON(genesisBz); err != nil {
return errors.Wrap(err, "failed to unmarshal eth1 genesis")
}
genesisBlock := ethGenesis.ToBlock()
// Create the executable data from the EL genesis file.
payload := gethprimitives.BlockToExecutableData(
genesisBlock,
nil,
nil,
nil,
).ExecutionPayload
appGenesis, err := genutiltypes.AppGenesisFromFile(config.GenesisFile())
if err != nil {
return errors.Wrap(err, "failed to read genesis doc from file")
}
// Create the app state.
appGenesisState, err := genutiltypes.GenesisStateFromAppGenesis(appGenesis)
if err != nil {
return err
}
genesisInfo := &types.Genesis{}
if err = json.Unmarshal(appGenesisState["beacon"], genesisInfo); err != nil {
return errors.Wrap(err, "failed to unmarshal beacon state")
}
// Inject the execution payload from the executable data.
eph, err := executableDataToExecutionPayloadHeader(payload)
if err != nil {
return errors.Wrap(err, "failed to convert executable data to execution payload header")
}
genesisInfo.ExecutionPayloadHeader = eph
if appGenesisState["beacon"], err = json.Marshal(genesisInfo); err != nil {
return errors.Wrap(err, "failed to marshal beacon state")
}
if appGenesis.AppState, err = json.MarshalIndent(appGenesisState, "", " "); err != nil {
return err
}
return genutil.ExportGenesisFile(appGenesis, config.GenesisFile())
}
// Converts the eth executable data type to the beacon execution payload header.
func executableDataToExecutionPayloadHeader(
data *gethprimitives.ExecutableData,
) (*types.ExecutionPayloadHeader, error) {
eph := &types.ExecutionPayloadHeader{
ParentHash: common.ExecutionHash(data.ParentHash),
FeeRecipient: common.ExecutionAddress(data.FeeRecipient),
StateRoot: common.Bytes32(data.StateRoot),
ReceiptsRoot: common.Bytes32(data.ReceiptsRoot),
LogsBloom: [256]byte(data.LogsBloom),
Random: common.Bytes32(data.Random),
Number: math.U64(data.Number),
GasLimit: math.U64(data.GasLimit),
GasUsed: math.U64(data.GasUsed),
Timestamp: math.U64(data.Timestamp),
ExtraData: data.ExtraData,
BlockHash: common.ExecutionHash(data.BlockHash),
TransactionsRoot: engineprimitives.Transactions(data.Transactions).HashTreeRoot(),
}
withdrawals := make(engineprimitives.Withdrawals, len(data.Withdrawals))
for i, withdrawal := range data.Withdrawals {
withdrawals[i] = &engineprimitives.Withdrawal{
Index: math.U64(withdrawal.Index),
Validator: math.ValidatorIndex(withdrawal.Validator),
Address: common.ExecutionAddress(withdrawal.Address),
Amount: math.Gwei(withdrawal.Amount),
}
}
eph.WithdrawalsRoot = withdrawals.HashTreeRoot()
if len(data.ExtraData) > constants.ExtraDataLength {
data.ExtraData = data.ExtraData[:constants.ExtraDataLength]
}
eph.ExtraData = data.ExtraData
if data.BlobGasUsed != nil {
eph.BlobGasUsed = math.U64(*data.BlobGasUsed)
}
if data.ExcessBlobGas != nil {
eph.ExcessBlobGas = math.U64(*data.ExcessBlobGas)
}
baseFeePerGas, err := math.NewU256FromBigInt(data.BaseFeePerGas)
if err != nil {
return nil, fmt.Errorf("failed baseFeePerGas conversion: %w", err)
}
eph.BaseFeePerGas = baseFeePerGas
return eph, nil
}