Skip to content

Commit cbb85e0

Browse files
committed
core, eth, les, miner: move common function to beacon package
1 parent 0f6ae20 commit cbb85e0

File tree

5 files changed

+88
-142
lines changed

5 files changed

+88
-142
lines changed

core/beacon/types.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/common/hexutil"
25+
"github.com/ethereum/go-ethereum/core/types"
26+
"github.com/ethereum/go-ethereum/trie"
2527
)
2628

2729
//go:generate go run github.com/fjl/gencodec -type PayloadAttributesV1 -field-override payloadAttributesMarshaling -out gen_blockparams.go
@@ -121,3 +123,82 @@ type ForkchoiceStateV1 struct {
121123
SafeBlockHash common.Hash `json:"safeBlockHash"`
122124
FinalizedBlockHash common.Hash `json:"finalizedBlockHash"`
123125
}
126+
127+
func encodeTransactions(txs []*types.Transaction) [][]byte {
128+
var enc = make([][]byte, len(txs))
129+
for i, tx := range txs {
130+
enc[i], _ = tx.MarshalBinary()
131+
}
132+
return enc
133+
}
134+
135+
func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) {
136+
var txs = make([]*types.Transaction, len(enc))
137+
for i, encTx := range enc {
138+
var tx types.Transaction
139+
if err := tx.UnmarshalBinary(encTx); err != nil {
140+
return nil, fmt.Errorf("invalid transaction %d: %v", i, err)
141+
}
142+
txs[i] = &tx
143+
}
144+
return txs, nil
145+
}
146+
147+
// ExecutableDataToBlock constructs a block from executable data.
148+
// It verifies that the following fields:
149+
// len(extraData) <= 32
150+
// uncleHash = emptyUncleHash
151+
// difficulty = 0
152+
// and that the blockhash of the constructed block matches the parameters.
153+
func ExecutableDataToBlock(params ExecutableDataV1) (*types.Block, error) {
154+
txs, err := decodeTransactions(params.Transactions)
155+
if err != nil {
156+
return nil, err
157+
}
158+
if len(params.ExtraData) > 32 {
159+
return nil, fmt.Errorf("invalid extradata length: %v", len(params.ExtraData))
160+
}
161+
header := &types.Header{
162+
ParentHash: params.ParentHash,
163+
UncleHash: types.EmptyUncleHash,
164+
Coinbase: params.FeeRecipient,
165+
Root: params.StateRoot,
166+
TxHash: types.DeriveSha(types.Transactions(txs), trie.NewStackTrie(nil)),
167+
ReceiptHash: params.ReceiptsRoot,
168+
Bloom: types.BytesToBloom(params.LogsBloom),
169+
Difficulty: common.Big0,
170+
Number: new(big.Int).SetUint64(params.Number),
171+
GasLimit: params.GasLimit,
172+
GasUsed: params.GasUsed,
173+
Time: params.Timestamp,
174+
BaseFee: params.BaseFeePerGas,
175+
Extra: params.ExtraData,
176+
MixDigest: params.Random,
177+
}
178+
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
179+
if block.Hash() != params.BlockHash {
180+
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
181+
}
182+
return block, nil
183+
}
184+
185+
// BlockToExecutableData constructs the executableDataV1 structure by filling the
186+
// fields from the given block. It assumes the given block is post-merge block.
187+
func BlockToExecutableData(block *types.Block) *ExecutableDataV1 {
188+
return &ExecutableDataV1{
189+
BlockHash: block.Hash(),
190+
ParentHash: block.ParentHash(),
191+
FeeRecipient: block.Coinbase(),
192+
StateRoot: block.Root(),
193+
Number: block.NumberU64(),
194+
GasLimit: block.GasLimit(),
195+
GasUsed: block.GasUsed(),
196+
BaseFeePerGas: block.BaseFee(),
197+
Timestamp: block.Time(),
198+
ReceiptsRoot: block.ReceiptHash(),
199+
LogsBloom: block.Bloom().Bytes(),
200+
Transactions: encodeTransactions(block.Transactions()),
201+
Random: block.MixDigest(),
202+
ExtraData: block.Extra(),
203+
}
204+
}

eth/catalyst/api.go

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
125122
func (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.

eth/catalyst/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func TestEth2NewBlock(t *testing.T) {
267267
if err != nil {
268268
t.Fatalf("Failed to create the executable data %v", err)
269269
}
270-
block, err := ExecutableDataToBlock(*execData)
270+
block, err := beacon.ExecutableDataToBlock(*execData)
271271
if err != nil {
272272
t.Fatalf("Failed to convert executable data to block %v", err)
273273
}
@@ -307,7 +307,7 @@ func TestEth2NewBlock(t *testing.T) {
307307
if err != nil {
308308
t.Fatalf("Failed to create the executable data %v", err)
309309
}
310-
block, err := ExecutableDataToBlock(*execData)
310+
block, err := beacon.ExecutableDataToBlock(*execData)
311311
if err != nil {
312312
t.Fatalf("Failed to convert executable data to block %v", err)
313313
}

les/catalyst/api.go

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ package catalyst
2020
import (
2121
"errors"
2222
"fmt"
23-
"math/big"
2423

2524
"github.com/ethereum/go-ethereum/common"
2625
"github.com/ethereum/go-ethereum/core/beacon"
27-
"github.com/ethereum/go-ethereum/core/types"
2826
"github.com/ethereum/go-ethereum/les"
2927
"github.com/ethereum/go-ethereum/log"
3028
"github.com/ethereum/go-ethereum/node"
3129
"github.com/ethereum/go-ethereum/rpc"
32-
"github.com/ethereum/go-ethereum/trie"
3330
)
3431

3532
// Register adds catalyst APIs to the light client.
@@ -104,7 +101,7 @@ func (api *ConsensusAPI) GetPayloadV1(payloadID beacon.PayloadID) (*beacon.Execu
104101

105102
// ExecutePayloadV1 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
106103
func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableDataV1) (beacon.ExecutePayloadResponse, error) {
107-
block, err := ExecutableDataToBlock(params)
104+
block, err := beacon.ExecutableDataToBlock(params)
108105
if err != nil {
109106
return api.invalid(), err
110107
}
@@ -141,56 +138,6 @@ func (api *ConsensusAPI) invalid() beacon.ExecutePayloadResponse {
141138
return beacon.ExecutePayloadResponse{Status: beacon.INVALID.Status, LatestValidHash: api.les.BlockChain().CurrentHeader().Hash()}
142139
}
143140

144-
func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) {
145-
var txs = make([]*types.Transaction, len(enc))
146-
for i, encTx := range enc {
147-
var tx types.Transaction
148-
if err := tx.UnmarshalBinary(encTx); err != nil {
149-
return nil, fmt.Errorf("invalid transaction %d: %v", i, err)
150-
}
151-
txs[i] = &tx
152-
}
153-
return txs, nil
154-
}
155-
156-
// ExecutableDataToBlock constructs a block from executable data.
157-
// It verifies that the following fields:
158-
// len(extraData) <= 32
159-
// uncleHash = emptyUncleHash
160-
// difficulty = 0
161-
// and that the blockhash of the constructed block matches the parameters.
162-
func ExecutableDataToBlock(params beacon.ExecutableDataV1) (*types.Block, error) {
163-
txs, err := decodeTransactions(params.Transactions)
164-
if err != nil {
165-
return nil, err
166-
}
167-
if len(params.ExtraData) > 32 {
168-
return nil, fmt.Errorf("invalid extradata length: %v", len(params.ExtraData))
169-
}
170-
header := &types.Header{
171-
ParentHash: params.ParentHash,
172-
UncleHash: types.EmptyUncleHash,
173-
Coinbase: params.FeeRecipient,
174-
Root: params.StateRoot,
175-
TxHash: types.DeriveSha(types.Transactions(txs), trie.NewStackTrie(nil)),
176-
ReceiptHash: params.ReceiptsRoot,
177-
Bloom: types.BytesToBloom(params.LogsBloom),
178-
Difficulty: common.Big0,
179-
Number: new(big.Int).SetUint64(params.Number),
180-
GasLimit: params.GasLimit,
181-
GasUsed: params.GasUsed,
182-
Time: params.Timestamp,
183-
BaseFee: params.BaseFeePerGas,
184-
Extra: params.ExtraData,
185-
MixDigest: params.Random,
186-
}
187-
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
188-
if block.Hash() != params.BlockHash {
189-
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
190-
}
191-
return block, nil
192-
}
193-
194141
func (api *ConsensusAPI) checkTerminalTotalDifficulty(head common.Hash) error {
195142
// shortcut if we entered PoS already
196143
if api.les.Merger().PoSFinalized() {

miner/stress/beacon/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (n *ethNode) insertBlockAndSetHead(parent *types.Header, ed beacon.Executab
200200
if err := n.insertBlock(ed); err != nil {
201201
return err
202202
}
203-
block, err := fcatalyst.ExecutableDataToBlock(ed)
203+
block, err := beacon.ExecutableDataToBlock(ed)
204204
if err != nil {
205205
return err
206206
}
@@ -364,7 +364,7 @@ func (mgr *nodeManager) run() {
364364
log.Error("Failed to assemble the block", "err", err)
365365
continue
366366
}
367-
block, _ := fcatalyst.ExecutableDataToBlock(*ed)
367+
block, _ := beacon.ExecutableDataToBlock(*ed)
368368

369369
nodes := mgr.getNodes(eth2MiningNode)
370370
nodes = append(nodes, mgr.getNodes(eth2NormalNode)...)

0 commit comments

Comments
 (0)