Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding engine_ExchangeTransitionConfigurationV1 #3454

Merged
merged 24 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions cmd/rpcdaemon/commands/engine_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ type PayloadAttributes struct {
SuggestedFeeRecipient common.Address `json:"suggestedFeeRecipient" gencodec:"required"`
}

// TransitionConfiguration represents the correct configurations of the CL and the EL
type TransitionConfiguration struct {
TerminalTotalDifficulty *hexutil.Big `json:"terminalTotalDifficulty" gencodec:"required"`
TerminalBlockHash common.Hash `json:"terminalBlockHash" gencodec:"required"`
TerminalBlockNumber hexutil.Uint64 `json:"terminalBlockNumber" gencodec:"required"`
}

// EngineAPI Beacon chain communication endpoint
type EngineAPI interface {
ForkchoiceUpdatedV1(ctx context.Context, forkChoiceState *ForkChoiceState, payloadAttributes *PayloadAttributes) (map[string]interface{}, error)
NewPayloadV1(context.Context, *ExecutionPayload) (map[string]interface{}, error)
GetPayloadV1(ctx context.Context, payloadID hexutil.Bytes) (*ExecutionPayload, error)
GetPayloadBodiesV1(ctx context.Context, blockHashes []rpc.BlockNumberOrHash) (map[common.Hash]ExecutionPayload, error)
ExchangeTransitionConfigurationV1(ctx context.Context, transitionConfiguration TransitionConfiguration) (TransitionConfiguration, error)
}

// EngineImpl is implementation of the EngineAPI interface
Expand Down Expand Up @@ -246,6 +254,59 @@ func (e *EngineImpl) GetPayloadBodiesV1(ctx context.Context, blockHashes []rpc.B
return blockHashToBody, nil
}

// Gets a transistionConfiguration and pings the execution layer and checks if the execution layer has the correct configurations
func (e *EngineImpl) ExchangeTransitionConfigurationV1(ctx context.Context, transitionConfiguration TransitionConfiguration) (TransitionConfiguration, error) {
tx, err := e.db.BeginRo(ctx)

if err != nil {
return TransitionConfiguration{}, err
}

defer tx.Rollback()
// terminal block number must always be zero
if transitionConfiguration.TerminalBlockNumber != 0 {
return TransitionConfiguration{}, fmt.Errorf("received the wrong terminal block number. expected zero, but instead got: %d", transitionConfiguration.TerminalBlockNumber)
}

chainConfig, err := e.BaseAPI.chainConfig(tx)

if err != nil {
return TransitionConfiguration{}, err
}

terminalTotalDifficulty := chainConfig.TerminalTotalDifficulty
enriavil1 marked this conversation as resolved.
Show resolved Hide resolved

if terminalTotalDifficulty == nil {
return TransitionConfiguration{}, fmt.Errorf("the execution layer doesn't have the terminal total difficulty. expected: %v", transitionConfiguration.TerminalTotalDifficulty)
}

if terminalTotalDifficulty.Cmp((*big.Int)(transitionConfiguration.TerminalTotalDifficulty)) != 0 {
return TransitionConfiguration{}, fmt.Errorf("the execution layer has the wrong total terminal difficulty. expected %v, but instead got: %d", transitionConfiguration.TerminalTotalDifficulty, terminalTotalDifficulty)
}

if chainConfig.TerminalBlockHash == nil {
return TransitionConfiguration{}, fmt.Errorf("the execution layer doesn't have the terminal block hash. expected: %s", transitionConfiguration.TerminalBlockHash)
}

if *chainConfig.TerminalBlockHash == (common.Hash{}) {
return TransitionConfiguration{
TerminalTotalDifficulty: (*hexutil.Big)(terminalTotalDifficulty),
TerminalBlockHash: *chainConfig.TerminalBlockHash,
TerminalBlockNumber: 0,
}, nil
}

if chainConfig.TerminalBlockHash != nil && *chainConfig.TerminalBlockHash != transitionConfiguration.TerminalBlockHash {
return TransitionConfiguration{}, fmt.Errorf("the execution layer has the wrong block hash. expected %s, but instead got: %s", transitionConfiguration.TerminalBlockHash, *chainConfig.TerminalBlockHash)
}

return TransitionConfiguration{
TerminalTotalDifficulty: (*hexutil.Big)(terminalTotalDifficulty),
TerminalBlockHash: *chainConfig.TerminalBlockHash,
TerminalBlockNumber: 0,
}, nil
}

// NewEngineAPI returns EngineImpl instance
func NewEngineAPI(base *BaseAPI, db kv.RoDB, api services.ApiBackend) *EngineImpl {
return &EngineImpl{
Expand Down
5 changes: 4 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/common/paths"
"github.com/ledgerwatch/erigon/params/networkname"
)
Expand Down Expand Up @@ -615,7 +616,9 @@ type ChainConfig struct {
BrunoBlock *big.Int `json:"brunoBlock,omitempty" toml:",omitempty"` // brunoBlock switch block (nil = no fork, 0 = already activated)

// EIP-3675: Upgrade consensus to Proof-of-Stake
TerminalTotalDifficulty *big.Int `json:"terminalTotalDifficulty,omitempty"` // The merge happens when terminal total difficulty is reached
TerminalTotalDifficulty *big.Int `json:"terminalTotalDifficulty,omitempty"` // The merge happens when terminal total difficulty is reached
enriavil1 marked this conversation as resolved.
Show resolved Hide resolved
TerminalBlockHash *common.Hash `json:"terminalBlockHash,omitempty"` // The hash of the last POW block
TerminalBlockNumber *hexutil.Uint64 `json:"terminalBlockNumber,omitempty"` // The block number of the last POW block
// Various consensus engines
Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"`
Expand Down