Skip to content

Commit e8ba29a

Browse files
core/beacon: eth/catalyst: updated engine api to new version
1 parent cbb85e0 commit e8ba29a

File tree

6 files changed

+60
-47
lines changed

6 files changed

+60
-47
lines changed

core/beacon/errors.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ package beacon
1919
import "github.com/ethereum/go-ethereum/rpc"
2020

2121
var (
22-
VALID = GenericStringResponse{"VALID"}
23-
SUCCESS = GenericStringResponse{"SUCCESS"}
24-
INVALID = ForkChoiceResponse{Status: "INVALID", PayloadID: nil}
25-
SYNCING = ForkChoiceResponse{Status: "SYNCING", PayloadID: nil}
22+
VALID = "VALID"
23+
INVALIDBLOCKHASH = "INVALID_BLOCK_HASH"
24+
ACCEPTED = "ACCEPTED"
25+
INVALID = "INVALID"
26+
SYNCING = "SYNCING"
27+
STATUS_SUCCESS = ForkChoiceResponse{PayloadStatus: PayloadStatusV1{Status: VALID}, PayloadID: nil}
28+
STATUS_INVALID = ForkChoiceResponse{PayloadStatus: PayloadStatusV1{Status: INVALID}, PayloadID: nil}
29+
STATUS_SYNCING = ForkChoiceResponse{PayloadStatus: PayloadStatusV1{Status: SYNCING}, PayloadID: nil}
2630
GenericServerError = rpc.CustomError{Code: -32000, ValidationError: "Server error"}
2731
UnknownPayload = rpc.CustomError{Code: -32001, ValidationError: "Unknown payload"}
2832
InvalidTB = rpc.CustomError{Code: -32002, ValidationError: "Invalid terminal block"}

core/beacon/types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type ExecutableDataV1 struct {
4747
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
4848
FeeRecipient common.Address `json:"feeRecipient" gencodec:"required"`
4949
StateRoot common.Hash `json:"stateRoot" gencodec:"required"`
50-
ReceiptsRoot common.Hash `json:"receiptsRoot" gencodec:"required"`
50+
ReceiptsRoot common.Hash `json:"receiptsRoot" gencodec:"required"`
5151
LogsBloom []byte `json:"logsBloom" gencodec:"required"`
5252
Random common.Hash `json:"random" gencodec:"required"`
5353
Number uint64 `json:"blockNumber" gencodec:"required"`
@@ -84,9 +84,10 @@ type GenericStringResponse struct {
8484
Status string `json:"status"`
8585
}
8686

87-
type ExecutePayloadResponse struct {
87+
type PayloadStatusV1 struct {
8888
Status string `json:"status"`
8989
LatestValidHash common.Hash `json:"latestValidHash"`
90+
ValidationError string `json:"validationError"`
9091
}
9192

9293
type ConsensusValidatedParams struct {
@@ -114,8 +115,8 @@ func (b *PayloadID) UnmarshalText(input []byte) error {
114115
}
115116

116117
type ForkChoiceResponse struct {
117-
Status string `json:"status"`
118-
PayloadID *PayloadID `json:"payloadId"`
118+
PayloadStatus PayloadStatusV1 `json:"payloadStatus"`
119+
PayloadID *PayloadID `json:"payloadId"`
119120
}
120121

121122
type ForkchoiceStateV1 struct {

eth/catalyst/api.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/sha256"
2222
"encoding/binary"
2323
"fmt"
24+
2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/core/beacon"
2627
"github.com/ethereum/go-ethereum/core/types"
@@ -74,38 +75,42 @@ func NewConsensusAPI(eth *eth.Ethereum) *ConsensusAPI {
7475
func (api *ConsensusAPI) ForkchoiceUpdatedV1(heads beacon.ForkchoiceStateV1, payloadAttributes *beacon.PayloadAttributesV1) (beacon.ForkChoiceResponse, error) {
7576
log.Trace("Engine API request received", "method", "ForkChoiceUpdated", "head", heads.HeadBlockHash, "finalized", heads.FinalizedBlockHash, "safe", heads.SafeBlockHash)
7677
if heads.HeadBlockHash == (common.Hash{}) {
77-
return beacon.ForkChoiceResponse{Status: beacon.SUCCESS.Status, PayloadID: nil}, nil
78+
return beacon.STATUS_SUCCESS, nil
7879
}
7980
if err := api.checkTerminalTotalDifficulty(heads.HeadBlockHash); err != nil {
8081
if block := api.eth.BlockChain().GetBlockByHash(heads.HeadBlockHash); block == nil {
8182
// TODO (MariusVanDerWijden) trigger sync
82-
return beacon.SYNCING, nil
83+
return beacon.STATUS_SYNCING, nil
8384
}
84-
return beacon.INVALID, err
85+
return beacon.ForkChoiceResponse{PayloadStatus: api.invalid(err)}, nil
8586
}
8687
// If the finalized block is set, check if it is in our blockchain
8788
if heads.FinalizedBlockHash != (common.Hash{}) {
8889
if block := api.eth.BlockChain().GetBlockByHash(heads.FinalizedBlockHash); block == nil {
8990
// TODO (MariusVanDerWijden) trigger sync
90-
return beacon.SYNCING, nil
91+
return beacon.STATUS_SYNCING, nil
9192
}
9293
}
94+
if block := api.eth.BlockChain().GetBlockByHash(heads.HeadBlockHash); block == nil {
95+
// TODO (MariusVanDerWijden) trigger sync
96+
return beacon.STATUS_SYNCING, nil
97+
}
9398
// SetHead
9499
if err := api.setHead(heads.HeadBlockHash); err != nil {
95-
return beacon.INVALID, err
100+
return beacon.ForkChoiceResponse{PayloadStatus: api.invalid(err)}, nil
96101
}
97102
// Assemble block (if needed). It only works for full node.
98103
if payloadAttributes != nil {
99104
data, err := api.assembleBlock(heads.HeadBlockHash, payloadAttributes)
100105
if err != nil {
101-
return beacon.INVALID, err
106+
return beacon.STATUS_INVALID, err
102107
}
103108
id := computePayloadId(heads.HeadBlockHash, payloadAttributes)
104109
api.preparedBlocks.put(id, data)
105110
log.Info("Created payload", "payloadID", id)
106-
return beacon.ForkChoiceResponse{Status: beacon.SUCCESS.Status, PayloadID: &id}, nil
111+
return beacon.ForkChoiceResponse{PayloadStatus: beacon.PayloadStatusV1{Status: beacon.VALID}, PayloadID: &id}, nil
107112
}
108-
return beacon.ForkChoiceResponse{Status: beacon.SUCCESS.Status, PayloadID: nil}, nil
113+
return beacon.STATUS_SUCCESS, nil
109114
}
110115

111116
// GetPayloadV1 returns a cached payload by id.
@@ -118,38 +123,39 @@ func (api *ConsensusAPI) GetPayloadV1(payloadID beacon.PayloadID) (*beacon.Execu
118123
return data, nil
119124
}
120125

121-
// ExecutePayloadV1 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
122-
func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableDataV1) (beacon.ExecutePayloadResponse, error) {
126+
// NewPayloadV1 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
127+
func (api *ConsensusAPI) NewPayloadV1(params beacon.ExecutableDataV1) (beacon.PayloadStatusV1, error) {
123128
log.Trace("Engine API request received", "method", "ExecutePayload", params.BlockHash, "number", params.Number)
124129
block, err := beacon.ExecutableDataToBlock(params)
125130
if err != nil {
126-
return api.invalid(), err
131+
return beacon.PayloadStatusV1{Status: beacon.INVALIDBLOCKHASH}, nil
127132
}
128133
if !api.eth.BlockChain().HasBlock(block.ParentHash(), block.NumberU64()-1) {
129134
/*
130-
TODO (MariusVanDerWijden) reenable once sync is merged
135+
TODO (MariusVanDerWijden) maybe reenable once sync is merged
131136
if err := api.eth.Downloader().BeaconSync(api.eth.SyncMode(), block.Header()); err != nil {
132137
return SYNCING, err
133138
}
134139
*/
135140
// TODO (MariusVanDerWijden) we should return nil here not empty hash
136-
return beacon.ExecutePayloadResponse{Status: beacon.SYNCING.Status, LatestValidHash: common.Hash{}}, nil
141+
return beacon.PayloadStatusV1{Status: beacon.ACCEPTED}, nil
137142
}
138143
parent := api.eth.BlockChain().GetBlockByHash(params.ParentHash)
139144
td := api.eth.BlockChain().GetTd(parent.Hash(), block.NumberU64()-1)
140145
ttd := api.eth.BlockChain().Config().TerminalTotalDifficulty
141146
if td.Cmp(ttd) < 0 {
142-
return api.invalid(), fmt.Errorf("can not execute payload on top of block with low td got: %v threshold %v", td, ttd)
147+
err := fmt.Errorf("can not execute payload on top of block with low td got: %v threshold %v", td, ttd)
148+
return api.invalid(err), nil
143149
}
144150
log.Trace("Inserting block without head", "hash", block.Hash(), "number", block.Number)
145151
if err := api.eth.BlockChain().InsertBlockWithoutSetHead(block); err != nil {
146-
return api.invalid(), err
152+
return api.invalid(err), nil
147153
}
148154

149155
if merger := api.eth.Merger(); !merger.TDDReached() {
150156
merger.ReachTTD()
151157
}
152-
return beacon.ExecutePayloadResponse{Status: beacon.VALID.Status, LatestValidHash: block.Hash()}, nil
158+
return beacon.PayloadStatusV1{Status: beacon.VALID, LatestValidHash: block.Hash()}, nil
153159
}
154160

155161
// computePayloadId computes a pseudo-random payloadid, based on the parameters.
@@ -166,8 +172,8 @@ func computePayloadId(headBlockHash common.Hash, params *beacon.PayloadAttribute
166172
}
167173

168174
// invalid returns a response "INVALID" with the latest valid hash set to the current head.
169-
func (api *ConsensusAPI) invalid() beacon.ExecutePayloadResponse {
170-
return beacon.ExecutePayloadResponse{Status: beacon.INVALID.Status, LatestValidHash: api.eth.BlockChain().CurrentHeader().Hash()}
175+
func (api *ConsensusAPI) invalid(err error) beacon.PayloadStatusV1 {
176+
return beacon.PayloadStatusV1{Status: beacon.INVALID, LatestValidHash: api.eth.BlockChain().CurrentHeader().Hash(), ValidationError: err.Error()}
171177
}
172178

173179
// assembleBlock creates a new block and returns the "execution

eth/catalyst/api_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ func TestSetHeadBeforeTotalDifficulty(t *testing.T) {
130130
SafeBlockHash: common.Hash{},
131131
FinalizedBlockHash: common.Hash{},
132132
}
133-
if _, err := api.ForkchoiceUpdatedV1(fcState, nil); err == nil {
134-
t.Errorf("fork choice updated before total terminal difficulty should fail")
133+
if resp, err := api.ForkchoiceUpdatedV1(fcState, nil); err != nil {
134+
t.Errorf("fork choice updated should not error")
135+
} else if resp.PayloadStatus.Status != beacon.INVALID {
136+
t.Errorf("fork choice updated before total terminal difficulty should be INVALID")
135137
}
136138
}
137139

@@ -271,7 +273,7 @@ func TestEth2NewBlock(t *testing.T) {
271273
if err != nil {
272274
t.Fatalf("Failed to convert executable data to block %v", err)
273275
}
274-
newResp, err := api.ExecutePayloadV1(*execData)
276+
newResp, err := api.NewPayloadV1(*execData)
275277
if err != nil || newResp.Status != "VALID" {
276278
t.Fatalf("Failed to insert block: %v", err)
277279
}
@@ -311,7 +313,7 @@ func TestEth2NewBlock(t *testing.T) {
311313
if err != nil {
312314
t.Fatalf("Failed to convert executable data to block %v", err)
313315
}
314-
newResp, err := api.ExecutePayloadV1(*execData)
316+
newResp, err := api.NewPayloadV1(*execData)
315317
if err != nil || newResp.Status != "VALID" {
316318
t.Fatalf("Failed to insert block: %v", err)
317319
}
@@ -438,19 +440,19 @@ func TestFullAPI(t *testing.T) {
438440
if err != nil {
439441
t.Fatalf("error preparing payload, err=%v", err)
440442
}
441-
if resp.Status != beacon.SUCCESS.Status {
442-
t.Fatalf("error preparing payload, invalid status: %v", resp.Status)
443+
if resp.PayloadStatus.Status != beacon.SUCCESS {
444+
t.Fatalf("error preparing payload, invalid status: %v", resp.PayloadStatus.Status)
443445
}
444446
payloadID := computePayloadId(parent.Hash(), &params)
445447
payload, err := api.GetPayloadV1(payloadID)
446448
if err != nil {
447449
t.Fatalf("can't get payload: %v", err)
448450
}
449-
execResp, err := api.ExecutePayloadV1(*payload)
451+
execResp, err := api.NewPayloadV1(*payload)
450452
if err != nil {
451453
t.Fatalf("can't execute payload: %v", err)
452454
}
453-
if execResp.Status != beacon.VALID.Status {
455+
if execResp.Status != beacon.VALID {
454456
t.Fatalf("invalid status: %v", execResp.Status)
455457
}
456458
fcState = beacon.ForkchoiceStateV1{

les/catalyst/api.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,30 @@ func NewConsensusAPI(les *les.LightEthereum) *ConsensusAPI {
6868
// we return an error since block creation is not supported in les mode
6969
func (api *ConsensusAPI) ForkchoiceUpdatedV1(heads beacon.ForkchoiceStateV1, payloadAttributes *beacon.PayloadAttributesV1) (beacon.ForkChoiceResponse, error) {
7070
if heads.HeadBlockHash == (common.Hash{}) {
71-
return beacon.ForkChoiceResponse{Status: beacon.SUCCESS.Status, PayloadID: nil}, nil
71+
return beacon.STATUS_SUCCESS, nil
7272
}
7373
if err := api.checkTerminalTotalDifficulty(heads.HeadBlockHash); err != nil {
7474
if header := api.les.BlockChain().GetHeaderByHash(heads.HeadBlockHash); header == nil {
7575
// TODO (MariusVanDerWijden) trigger sync
76-
return beacon.SYNCING, nil
76+
return beacon.STATUS_SYNCING, nil
7777
}
78-
return beacon.INVALID, err
78+
return beacon.STATUS_INVALID, err
7979
}
8080
// If the finalized block is set, check if it is in our blockchain
8181
if heads.FinalizedBlockHash != (common.Hash{}) {
8282
if header := api.les.BlockChain().GetHeaderByHash(heads.FinalizedBlockHash); header == nil {
8383
// TODO (MariusVanDerWijden) trigger sync
84-
return beacon.SYNCING, nil
84+
return beacon.STATUS_SYNCING, nil
8585
}
8686
}
8787
// SetHead
8888
if err := api.setHead(heads.HeadBlockHash); err != nil {
89-
return beacon.INVALID, err
89+
return beacon.STATUS_INVALID, err
9090
}
9191
if payloadAttributes != nil {
92-
return beacon.INVALID, errors.New("not supported")
92+
return beacon.STATUS_INVALID, errors.New("not supported")
9393
}
94-
return beacon.ForkChoiceResponse{Status: beacon.SUCCESS.Status, PayloadID: nil}, nil
94+
return beacon.STATUS_SUCCESS, nil
9595
}
9696

9797
// GetPayloadV1 returns a cached payload by id. It's not supported in les mode.
@@ -100,7 +100,7 @@ func (api *ConsensusAPI) GetPayloadV1(payloadID beacon.PayloadID) (*beacon.Execu
100100
}
101101

102102
// ExecutePayloadV1 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
103-
func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableDataV1) (beacon.ExecutePayloadResponse, error) {
103+
func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableDataV1) (beacon.PayloadStatusV1, error) {
104104
block, err := beacon.ExecutableDataToBlock(params)
105105
if err != nil {
106106
return api.invalid(), err
@@ -113,7 +113,7 @@ func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableDataV1) (beaco
113113
}
114114
*/
115115
// TODO (MariusVanDerWijden) we should return nil here not empty hash
116-
return beacon.ExecutePayloadResponse{Status: beacon.SYNCING.Status, LatestValidHash: common.Hash{}}, nil
116+
return beacon.PayloadStatusV1{Status: beacon.SYNCING, LatestValidHash: common.Hash{}}, nil
117117
}
118118
parent := api.les.BlockChain().GetHeaderByHash(params.ParentHash)
119119
if parent == nil {
@@ -130,12 +130,12 @@ func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableDataV1) (beaco
130130
if merger := api.les.Merger(); !merger.TDDReached() {
131131
merger.ReachTTD()
132132
}
133-
return beacon.ExecutePayloadResponse{Status: beacon.VALID.Status, LatestValidHash: block.Hash()}, nil
133+
return beacon.PayloadStatusV1{Status: beacon.VALID, LatestValidHash: block.Hash()}, nil
134134
}
135135

136136
// invalid returns a response "INVALID" with the latest valid hash set to the current head.
137-
func (api *ConsensusAPI) invalid() beacon.ExecutePayloadResponse {
138-
return beacon.ExecutePayloadResponse{Status: beacon.INVALID.Status, LatestValidHash: api.les.BlockChain().CurrentHeader().Hash()}
137+
func (api *ConsensusAPI) invalid() beacon.PayloadStatusV1 {
138+
return beacon.PayloadStatusV1{Status: beacon.INVALID, LatestValidHash: api.les.BlockChain().CurrentHeader().Hash()}
139139
}
140140

141141
func (api *ConsensusAPI) checkTerminalTotalDifficulty(head common.Hash) error {

miner/stress/beacon/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (n *ethNode) insertBlock(eb beacon.ExecutableDataV1) error {
173173
}
174174
switch n.typ {
175175
case eth2NormalNode, eth2MiningNode:
176-
newResp, err := n.api.ExecutePayloadV1(eb)
176+
newResp, err := n.api.NewPayloadV1(eb)
177177
if err != nil {
178178
return err
179179
} else if newResp.Status != "VALID" {

0 commit comments

Comments
 (0)