@@ -21,16 +21,13 @@ import (
2121 "crypto/sha256"
2222 "encoding/binary"
2323 "fmt"
24- "math/big"
25-
2624 "github.com/ethereum/go-ethereum/common"
2725 "github.com/ethereum/go-ethereum/core/beacon"
2826 "github.com/ethereum/go-ethereum/core/types"
2927 "github.com/ethereum/go-ethereum/eth"
3028 "github.com/ethereum/go-ethereum/log"
3129 "github.com/ethereum/go-ethereum/node"
3230 "github.com/ethereum/go-ethereum/rpc"
33- "github.com/ethereum/go-ethereum/trie"
3431)
3532
3633// Register adds catalyst APIs to the full node.
@@ -124,7 +121,7 @@ func (api *ConsensusAPI) GetPayloadV1(payloadID beacon.PayloadID) (*beacon.Execu
124121// ExecutePayloadV1 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
125122func (api * ConsensusAPI ) ExecutePayloadV1 (params beacon.ExecutableDataV1 ) (beacon.ExecutePayloadResponse , error ) {
126123 log .Trace ("Engine API request received" , "method" , "ExecutePayload" , params .BlockHash , "number" , params .Number )
127- block , err := ExecutableDataToBlock (params )
124+ block , err := beacon . ExecutableDataToBlock (params )
128125 if err != nil {
129126 return api .invalid (), err
130127 }
@@ -181,86 +178,7 @@ func (api *ConsensusAPI) assembleBlock(parentHash common.Hash, params *beacon.Pa
181178 if err != nil {
182179 return nil , err
183180 }
184- return BlockToExecutableData (block ), nil
185- }
186-
187- func encodeTransactions (txs []* types.Transaction ) [][]byte {
188- var enc = make ([][]byte , len (txs ))
189- for i , tx := range txs {
190- enc [i ], _ = tx .MarshalBinary ()
191- }
192- return enc
193- }
194-
195- func decodeTransactions (enc [][]byte ) ([]* types.Transaction , error ) {
196- var txs = make ([]* types.Transaction , len (enc ))
197- for i , encTx := range enc {
198- var tx types.Transaction
199- if err := tx .UnmarshalBinary (encTx ); err != nil {
200- return nil , fmt .Errorf ("invalid transaction %d: %v" , i , err )
201- }
202- txs [i ] = & tx
203- }
204- return txs , nil
205- }
206-
207- // ExecutableDataToBlock constructs a block from executable data.
208- // It verifies that the following fields:
209- // len(extraData) <= 32
210- // uncleHash = emptyUncleHash
211- // difficulty = 0
212- // and that the blockhash of the constructed block matches the parameters.
213- func ExecutableDataToBlock (params beacon.ExecutableDataV1 ) (* types.Block , error ) {
214- txs , err := decodeTransactions (params .Transactions )
215- if err != nil {
216- return nil , err
217- }
218- if len (params .ExtraData ) > 32 {
219- return nil , fmt .Errorf ("invalid extradata length: %v" , len (params .ExtraData ))
220- }
221- header := & types.Header {
222- ParentHash : params .ParentHash ,
223- UncleHash : types .EmptyUncleHash ,
224- Coinbase : params .FeeRecipient ,
225- Root : params .StateRoot ,
226- TxHash : types .DeriveSha (types .Transactions (txs ), trie .NewStackTrie (nil )),
227- ReceiptHash : params .ReceiptsRoot ,
228- Bloom : types .BytesToBloom (params .LogsBloom ),
229- Difficulty : common .Big0 ,
230- Number : new (big.Int ).SetUint64 (params .Number ),
231- GasLimit : params .GasLimit ,
232- GasUsed : params .GasUsed ,
233- Time : params .Timestamp ,
234- BaseFee : params .BaseFeePerGas ,
235- Extra : params .ExtraData ,
236- MixDigest : params .Random ,
237- }
238- block := types .NewBlockWithHeader (header ).WithBody (txs , nil /* uncles */ )
239- if block .Hash () != params .BlockHash {
240- return nil , fmt .Errorf ("blockhash mismatch, want %x, got %x" , params .BlockHash , block .Hash ())
241- }
242- return block , nil
243- }
244-
245- // BlockToExecutableData constructs the executableDataV1 structure by filling the
246- // fields from the given block. It assumes the given block is post-merge block.
247- func BlockToExecutableData (block * types.Block ) * beacon.ExecutableDataV1 {
248- return & beacon.ExecutableDataV1 {
249- BlockHash : block .Hash (),
250- ParentHash : block .ParentHash (),
251- FeeRecipient : block .Coinbase (),
252- StateRoot : block .Root (),
253- Number : block .NumberU64 (),
254- GasLimit : block .GasLimit (),
255- GasUsed : block .GasUsed (),
256- BaseFeePerGas : block .BaseFee (),
257- Timestamp : block .Time (),
258- ReceiptsRoot : block .ReceiptHash (),
259- LogsBloom : block .Bloom ().Bytes (),
260- Transactions : encodeTransactions (block .Transactions ()),
261- Random : block .MixDigest (),
262- ExtraData : block .Extra (),
263- }
181+ return beacon .BlockToExecutableData (block ), nil
264182}
265183
266184// Used in tests to add a the list of transactions from a block to the tx pool.
0 commit comments