Skip to content

Commit 0f6ae20

Browse files
committed
eth/catalyst, les/catalyst: add method docs
1 parent 4dddcc6 commit 0f6ae20

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

eth/catalyst/api.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"math/big"
2525

2626
"github.com/ethereum/go-ethereum/common"
27-
"github.com/ethereum/go-ethereum/consensus"
2827
"github.com/ethereum/go-ethereum/core/beacon"
2928
"github.com/ethereum/go-ethereum/core/types"
3029
"github.com/ethereum/go-ethereum/eth"
@@ -150,7 +149,7 @@ func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableDataV1) (beaco
150149
return api.invalid(), err
151150
}
152151

153-
if merger := api.merger(); !merger.TDDReached() {
152+
if merger := api.eth.Merger(); !merger.TDDReached() {
154153
merger.ReachTTD()
155154
}
156155
return beacon.ExecutePayloadResponse{Status: beacon.VALID.Status, LatestValidHash: block.Hash()}, nil
@@ -274,7 +273,7 @@ func (api *ConsensusAPI) insertTransactions(txs types.Transactions) error {
274273

275274
func (api *ConsensusAPI) checkTerminalTotalDifficulty(head common.Hash) error {
276275
// shortcut if we entered PoS already
277-
if api.merger().PoSFinalized() {
276+
if api.eth.Merger().PoSFinalized() {
278277
return nil
279278
}
280279
// make sure the parent has enough terminal total difficulty
@@ -304,15 +303,10 @@ func (api *ConsensusAPI) setHead(newHead common.Hash) error {
304303
return err
305304
}
306305
// Trigger the transition if it's the first `NewHead` event.
307-
if merger := api.merger(); !merger.PoSFinalized() {
306+
if merger := api.eth.Merger(); !merger.PoSFinalized() {
308307
merger.FinalizePoS()
309308
}
310309
// TODO (MariusVanDerWijden) are we really synced now?
311310
api.eth.SetSynced()
312311
return nil
313312
}
314-
315-
// Helper function, return the merger instance.
316-
func (api *ConsensusAPI) merger() *consensus.Merger {
317-
return api.eth.Merger()
318-
}

les/catalyst/api.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"math/big"
2424

2525
"github.com/ethereum/go-ethereum/common"
26-
"github.com/ethereum/go-ethereum/consensus"
2726
"github.com/ethereum/go-ethereum/core/beacon"
2827
"github.com/ethereum/go-ethereum/core/types"
2928
"github.com/ethereum/go-ethereum/les"
@@ -51,17 +50,25 @@ type ConsensusAPI struct {
5150
les *les.LightEthereum
5251
}
5352

53+
// NewConsensusAPI creates a new consensus api for the given backend.
54+
// The underlying blockchain needs to have a valid terminal total difficulty set.
5455
func NewConsensusAPI(les *les.LightEthereum) *ConsensusAPI {
5556
if les.BlockChain().Config().TerminalTotalDifficulty == nil {
5657
panic("Catalyst started without valid total difficulty")
5758
}
5859
return &ConsensusAPI{les: les}
5960
}
6061

61-
func (api *ConsensusAPI) GetPayloadV1(payloadID beacon.PayloadID) (*beacon.ExecutableDataV1, error) {
62-
return nil, &beacon.GenericServerError
63-
}
64-
62+
// ForkchoiceUpdatedV1 has several responsibilities:
63+
// If the method is called with an empty head block:
64+
// we return success, which can be used to check if the catalyst mode is enabled
65+
// If the total difficulty was not reached:
66+
// we return INVALID
67+
// If the finalizedBlockHash is set:
68+
// we check if we have the finalizedBlockHash in our db, if not we start a sync
69+
// We try to set our blockchain to the headBlock
70+
// If there are payloadAttributes:
71+
// we return an error since block creation is not supported in les mode
6572
func (api *ConsensusAPI) ForkchoiceUpdatedV1(heads beacon.ForkchoiceStateV1, payloadAttributes *beacon.PayloadAttributesV1) (beacon.ForkChoiceResponse, error) {
6673
if heads.HeadBlockHash == (common.Hash{}) {
6774
return beacon.ForkChoiceResponse{Status: beacon.SUCCESS.Status, PayloadID: nil}, nil
@@ -90,8 +97,9 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV1(heads beacon.ForkchoiceStateV1, pay
9097
return beacon.ForkChoiceResponse{Status: beacon.SUCCESS.Status, PayloadID: nil}, nil
9198
}
9299

93-
func (api *ConsensusAPI) invalid() beacon.ExecutePayloadResponse {
94-
return beacon.ExecutePayloadResponse{Status: beacon.INVALID.Status, LatestValidHash: api.les.BlockChain().CurrentHeader().Hash()}
100+
// GetPayloadV1 returns a cached payload by id. It's not supported in les mode.
101+
func (api *ConsensusAPI) GetPayloadV1(payloadID beacon.PayloadID) (*beacon.ExecutableDataV1, error) {
102+
return nil, &beacon.GenericServerError
95103
}
96104

97105
// ExecutePayloadV1 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
@@ -122,12 +130,17 @@ func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableDataV1) (beaco
122130
if err = api.les.BlockChain().InsertHeader(block.Header()); err != nil {
123131
return api.invalid(), err
124132
}
125-
if merger := api.merger(); !merger.TDDReached() {
133+
if merger := api.les.Merger(); !merger.TDDReached() {
126134
merger.ReachTTD()
127135
}
128136
return beacon.ExecutePayloadResponse{Status: beacon.VALID.Status, LatestValidHash: block.Hash()}, nil
129137
}
130138

139+
// invalid returns a response "INVALID" with the latest valid hash set to the current head.
140+
func (api *ConsensusAPI) invalid() beacon.ExecutePayloadResponse {
141+
return beacon.ExecutePayloadResponse{Status: beacon.INVALID.Status, LatestValidHash: api.les.BlockChain().CurrentHeader().Hash()}
142+
}
143+
131144
func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) {
132145
var txs = make([]*types.Transaction, len(enc))
133146
for i, encTx := range enc {
@@ -140,6 +153,12 @@ func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) {
140153
return txs, nil
141154
}
142155

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.
143162
func ExecutableDataToBlock(params beacon.ExecutableDataV1) (*types.Block, error) {
144163
txs, err := decodeTransactions(params.Transactions)
145164
if err != nil {
@@ -174,7 +193,7 @@ func ExecutableDataToBlock(params beacon.ExecutableDataV1) (*types.Block, error)
174193

175194
func (api *ConsensusAPI) checkTerminalTotalDifficulty(head common.Hash) error {
176195
// shortcut if we entered PoS already
177-
if api.merger().PoSFinalized() {
196+
if api.les.Merger().PoSFinalized() {
178197
return nil
179198
}
180199
// make sure the parent has enough terminal total difficulty
@@ -205,13 +224,8 @@ func (api *ConsensusAPI) setHead(newHead common.Hash) error {
205224
return err
206225
}
207226
// Trigger the transition if it's the first `NewHead` event.
208-
if merger := api.merger(); !merger.PoSFinalized() {
227+
if merger := api.les.Merger(); !merger.PoSFinalized() {
209228
merger.FinalizePoS()
210229
}
211230
return nil
212231
}
213-
214-
// Helper function, return the merger instance.
215-
func (api *ConsensusAPI) merger() *consensus.Merger {
216-
return api.les.Merger()
217-
}

0 commit comments

Comments
 (0)