Skip to content

Commit 9da25c5

Browse files
rjl493456442MariusVanDerWijdenkaralabe
authored
all: separate catalyst package (#24280)
* all: seperate catalyst package * eth/catalyst: moved some methods, added docs * eth/catalyst, les/catalyst: add method docs * core, eth, les, miner: move common function to beacon package * eth/catalyst: goimported * cmd/utils, miner/stress/beacon: naming nitpicks Co-authored-by: Marius van der Wijden <[email protected]> Co-authored-by: Péter Szilágyi <[email protected]>
1 parent a5c0cfb commit 9da25c5

File tree

11 files changed

+701
-320
lines changed

11 files changed

+701
-320
lines changed

cmd/utils/flags.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import (
4545
"github.com/ethereum/go-ethereum/core/vm"
4646
"github.com/ethereum/go-ethereum/crypto"
4747
"github.com/ethereum/go-ethereum/eth"
48-
"github.com/ethereum/go-ethereum/eth/catalyst"
48+
ethcatalyst "github.com/ethereum/go-ethereum/eth/catalyst"
4949
"github.com/ethereum/go-ethereum/eth/downloader"
5050
"github.com/ethereum/go-ethereum/eth/ethconfig"
5151
"github.com/ethereum/go-ethereum/eth/gasprice"
@@ -56,6 +56,7 @@ import (
5656
"github.com/ethereum/go-ethereum/internal/ethapi"
5757
"github.com/ethereum/go-ethereum/internal/flags"
5858
"github.com/ethereum/go-ethereum/les"
59+
lescatalyst "github.com/ethereum/go-ethereum/les/catalyst"
5960
"github.com/ethereum/go-ethereum/log"
6061
"github.com/ethereum/go-ethereum/metrics"
6162
"github.com/ethereum/go-ethereum/metrics/exp"
@@ -1724,7 +1725,7 @@ func RegisterEthService(stack *node.Node, cfg *ethconfig.Config, isCatalyst bool
17241725
}
17251726
stack.RegisterAPIs(tracers.APIs(backend.ApiBackend))
17261727
if isCatalyst {
1727-
if err := catalyst.RegisterLight(stack, backend); err != nil {
1728+
if err := lescatalyst.Register(stack, backend); err != nil {
17281729
Fatalf("Failed to register the catalyst service: %v", err)
17291730
}
17301731
}
@@ -1741,7 +1742,7 @@ func RegisterEthService(stack *node.Node, cfg *ethconfig.Config, isCatalyst bool
17411742
}
17421743
}
17431744
if isCatalyst {
1744-
if err := catalyst.Register(stack, backend); err != nil {
1745+
if err := ethcatalyst.Register(stack, backend); err != nil {
17451746
Fatalf("Failed to register the catalyst service: %v", err)
17461747
}
17471748
}

core/beacon/errors.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>
16+
17+
package beacon
18+
19+
import "github.com/ethereum/go-ethereum/rpc"
20+
21+
var (
22+
VALID = GenericStringResponse{"VALID"}
23+
SUCCESS = GenericStringResponse{"SUCCESS"}
24+
INVALID = ForkChoiceResponse{Status: "INVALID", PayloadID: nil}
25+
SYNCING = ForkChoiceResponse{Status: "SYNCING", PayloadID: nil}
26+
GenericServerError = rpc.CustomError{Code: -32000, ValidationError: "Server error"}
27+
UnknownPayload = rpc.CustomError{Code: -32001, ValidationError: "Unknown payload"}
28+
InvalidTB = rpc.CustomError{Code: -32002, ValidationError: "Invalid terminal block"}
29+
)

eth/catalyst/gen_blockparams.go renamed to core/beacon/gen_blockparams.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eth/catalyst/gen_ed.go renamed to core/beacon/gen_ed.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eth/catalyst/api_types.go renamed to core/beacon/types.go

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 The go-ethereum Authors
1+
// Copyright 2022 The go-ethereum Authors
22
// This file is part of the go-ethereum library.
33
//
44
// The go-ethereum library is free software: you can redistribute it and/or modify
@@ -14,14 +14,16 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package catalyst
17+
package beacon
1818

1919
import (
2020
"fmt"
2121
"math/big"
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+
}

0 commit comments

Comments
 (0)