@@ -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.
5455func 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
6572func (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+
131144func 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.
143162func 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
175194func (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