Skip to content

Commit

Permalink
Rename Serenity consensus engine to Merge (#7475)
Browse files Browse the repository at this point in the history
[EIP-2982](https://eips.ethereum.org/EIPS/eip-2982) "Serenity Phase 0"
was superseded by [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675)
"Upgrade consensus to Proof-of-Stake"
  • Loading branch information
yperbasis authored May 9, 2023
1 parent 42e8db3 commit 771c6fc
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 76 deletions.
6 changes: 3 additions & 3 deletions cl/cltypes/eth1_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ledgerwatch/erigon/cl/cltypes/ssz"
"github.com/ledgerwatch/erigon/cl/merkle_tree"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/consensus/merge"
"github.com/ledgerwatch/erigon/core/types"
)

Expand Down Expand Up @@ -359,14 +359,14 @@ func (b *Eth1Block) RlpHeader() (*types.Header, error) {
TxHash: types.DeriveSha(types.BinaryTransactions(b.Transactions)),
ReceiptHash: b.ReceiptsRoot,
Bloom: b.LogsBloom,
Difficulty: serenity.SerenityDifficulty,
Difficulty: merge.ProofOfStakeDifficulty,
Number: big.NewInt(int64(b.BlockNumber)),
GasLimit: b.GasLimit,
GasUsed: b.GasUsed,
Time: b.Time,
Extra: b.Extra,
MixDigest: b.PrevRandao,
Nonce: serenity.SerenityNonce,
Nonce: merge.ProofOfStakeNonce,
BaseFee: baseFee,
WithdrawalsHash: withdrawalsHash,
ExcessDataGas: excessDataGas,
Expand Down
6 changes: 3 additions & 3 deletions cmd/erigon-el/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import (
"github.com/ledgerwatch/erigon/consensus/bor"
"github.com/ledgerwatch/erigon/consensus/clique"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/consensus/merge"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state/historyv2read"
Expand Down Expand Up @@ -706,7 +706,7 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, mining *stagedsy
var clq *clique.Clique
if c, ok := s.engine.(*clique.Clique); ok {
clq = c
} else if cl, ok := s.engine.(*serenity.Serenity); ok {
} else if cl, ok := s.engine.(*merge.Merge); ok {
if c, ok := cl.InnerEngine().(*clique.Clique); ok {
clq = c
}
Expand All @@ -725,7 +725,7 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, mining *stagedsy
var borcfg *bor.Bor
if b, ok := s.engine.(*bor.Bor); ok {
borcfg = b
} else if br, ok := s.engine.(*serenity.Serenity); ok {
} else if br, ok := s.engine.(*merge.Merge); ok {
if b, ok := br.InnerEngine().(*bor.Bor); ok {
borcfg = b
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/consensus/merge"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
Expand Down Expand Up @@ -303,9 +303,9 @@ func Main(ctx *cli.Context) error {
defer tx.Rollback()

reader, writer := MakePreState(chainConfig.Rules(0, 0), tx, prestate.Pre)
// serenity engine can be used for pre-merge blocks as well, as it
// Merge engine can be used for pre-merge blocks as well, as it
// redirects to the ethash engine based on the block number
engine := serenity.New(&ethash.FakeEthash{})
engine := merge.New(&ethash.FakeEthash{})

result, err := core.ExecuteBlockEphemerally(chainConfig, &vmConfig, getHash, engine, block, reader, writer, nil, getTracer)

Expand Down
74 changes: 36 additions & 38 deletions consensus/serenity/serenity.go → consensus/merge/merge.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package serenity
package merge

import (
"bytes"
Expand All @@ -7,6 +7,7 @@ import (
"math/big"

"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"

Expand All @@ -19,11 +20,10 @@ import (
"github.com/ledgerwatch/erigon/rpc"
)

// Constants for Serenity as specified into https://eips.ethereum.org/EIPS/eip-2982
// Constants for The Merge as specified by EIP-3675: Upgrade consensus to Proof-of-Stake
var (
SerenityDifficulty = libcommon.Big0 // Serenity block's difficulty is always 0.
SerenityNonce = types.BlockNonce{} // Serenity chain's nonces are 0.
RewardSerenity = big.NewInt(300000000000000000)
ProofOfStakeDifficulty = libcommon.Big0 // PoS block's difficulty is always 0
ProofOfStakeNonce = types.BlockNonce{} // PoS block's have all-zero nonces
)

var (
Expand All @@ -39,47 +39,45 @@ var (
errOlderBlockTime = errors.New("timestamp older than parent")
)

// Serenity Consensus Engine for the Execution Layer.
// Serenity is a consensus engine that combines the eth1 consensus and proof-of-stake
// Merge Consensus Engine for the Execution Layer.
// Merge is a consensus engine that combines the eth1 consensus and proof-of-stake
// algorithm. The transition rule is described in the eth1/2 merge spec:
// https://eips.ethereum.org/EIPS/eip-3675
//
// Note: After the Merge the work is mostly done on the Consensus Layer, so nothing much is to be added on this side.
type Serenity struct {
type Merge struct {
eth1Engine consensus.Engine // Original consensus engine used in eth1, e.g. ethash or clique
}

// New creates a new instance of the Serenity Engine with the given embedded eth1 engine.
func New(eth1Engine consensus.Engine) *Serenity {
if _, ok := eth1Engine.(*Serenity); ok {
// New creates a new instance of the Merge Engine with the given embedded eth1 engine.
func New(eth1Engine consensus.Engine) *Merge {
if _, ok := eth1Engine.(*Merge); ok {
panic("nested consensus engine")
}
return &Serenity{eth1Engine: eth1Engine}
return &Merge{eth1Engine: eth1Engine}
}

// InnerEngine returns the embedded eth1 consensus engine.
func (s *Serenity) InnerEngine() consensus.Engine {
func (s *Merge) InnerEngine() consensus.Engine {
return s.eth1Engine
}

// Type returns the type of the underlying consensus engine.
func (s *Serenity) Type() chain.ConsensusName {
func (s *Merge) Type() chain.ConsensusName {
return s.eth1Engine.Type()
}

// Author implements consensus.Engine, returning the header's coinbase as the
// proof-of-stake verified author of the block.
// This is thread-safe (only access the header.Coinbase or the underlying engine's thread-safe method)
func (s *Serenity) Author(header *types.Header) (libcommon.Address, error) {
func (s *Merge) Author(header *types.Header) (libcommon.Address, error) {
if !IsPoSHeader(header) {
return s.eth1Engine.Author(header)
}
return header.Coinbase, nil
}

// VerifyHeader checks whether a header conforms to the consensus rules of the
// stock Ethereum serenity engine.
func (s *Serenity) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error {
func (s *Merge) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error {
reached, err := IsTTDReached(chain, header.ParentHash, header.Number.Uint64()-1)
if err != nil {
return err
Expand All @@ -99,7 +97,7 @@ func (s *Serenity) VerifyHeader(chain consensus.ChainHeaderReader, header *types

// VerifyUncles implements consensus.Engine, always returning an error for any
// uncles as this consensus mechanism doesn't permit uncles.
func (s *Serenity) VerifyUncles(chain consensus.ChainReader, header *types.Header, uncles []*types.Header) error {
func (s *Merge) VerifyUncles(chain consensus.ChainReader, header *types.Header, uncles []*types.Header) error {
if !IsPoSHeader(header) {
return s.eth1Engine.VerifyUncles(chain, header, uncles)
}
Expand All @@ -110,20 +108,20 @@ func (s *Serenity) VerifyUncles(chain consensus.ChainReader, header *types.Heade
}

// Prepare makes sure difficulty and nonce are correct
func (s *Serenity) Prepare(chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState) error {
func (s *Merge) Prepare(chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState) error {
reached, err := IsTTDReached(chain, header.ParentHash, header.Number.Uint64()-1)
if err != nil {
return err
}
if !reached {
return s.eth1Engine.Prepare(chain, header, state)
}
header.Difficulty = SerenityDifficulty
header.Nonce = SerenityNonce
header.Difficulty = ProofOfStakeDifficulty
header.Nonce = ProofOfStakeNonce
return nil
}

func (s *Serenity) CalculateRewards(config *chain.Config, header *types.Header, uncles []*types.Header, syscall consensus.SystemCall,
func (s *Merge) CalculateRewards(config *chain.Config, header *types.Header, uncles []*types.Header, syscall consensus.SystemCall,
) ([]consensus.Reward, error) {
_, isAura := s.eth1Engine.(*aura.AuRa)
if !IsPoSHeader(header) || isAura {
Expand All @@ -132,7 +130,7 @@ func (s *Serenity) CalculateRewards(config *chain.Config, header *types.Header,
return []consensus.Reward{}, nil
}

func (s *Serenity) Finalize(config *chain.Config, header *types.Header, state *state.IntraBlockState,
func (s *Merge) Finalize(config *chain.Config, header *types.Header, state *state.IntraBlockState,
txs types.Transactions, uncles []*types.Header, r types.Receipts, withdrawals []*types.Withdrawal,
chain consensus.ChainHeaderReader, syscall consensus.SystemCall,
) (types.Transactions, types.Receipts, error) {
Expand Down Expand Up @@ -172,7 +170,7 @@ func (s *Serenity) Finalize(config *chain.Config, header *types.Header, state *s
return txs, r, nil
}

func (s *Serenity) FinalizeAndAssemble(config *chain.Config, header *types.Header, state *state.IntraBlockState,
func (s *Merge) FinalizeAndAssemble(config *chain.Config, header *types.Header, state *state.IntraBlockState,
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal,
chain consensus.ChainHeaderReader, syscall consensus.SystemCall, call consensus.Call,
) (*types.Block, types.Transactions, types.Receipts, error) {
Expand All @@ -186,24 +184,24 @@ func (s *Serenity) FinalizeAndAssemble(config *chain.Config, header *types.Heade
return types.NewBlock(header, outTxs, uncles, outReceipts, withdrawals), outTxs, outReceipts, nil
}

func (s *Serenity) SealHash(header *types.Header) (hash libcommon.Hash) {
func (s *Merge) SealHash(header *types.Header) (hash libcommon.Hash) {
return s.eth1Engine.SealHash(header)
}

func (s *Serenity) CalcDifficulty(chain consensus.ChainHeaderReader, time, parentTime uint64, parentDifficulty *big.Int, parentNumber uint64, parentHash, parentUncleHash libcommon.Hash, parentAuRaStep uint64) *big.Int {
func (s *Merge) CalcDifficulty(chain consensus.ChainHeaderReader, time, parentTime uint64, parentDifficulty *big.Int, parentNumber uint64, parentHash, parentUncleHash libcommon.Hash, parentAuRaStep uint64) *big.Int {
reached, err := IsTTDReached(chain, parentHash, parentNumber)
if err != nil {
return nil
}
if !reached {
return s.eth1Engine.CalcDifficulty(chain, time, parentTime, parentDifficulty, parentNumber, parentHash, parentUncleHash, parentAuRaStep)
}
return SerenityDifficulty
return ProofOfStakeDifficulty
}

// verifyHeader checks whether a Proof-of-Stake header conforms to the consensus rules of the
// stock Ethereum consensus engine with EIP-3675 modifications.
func (s *Serenity) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header) error {
func (s *Merge) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header) error {

if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
return fmt.Errorf("extra-data longer than %d bytes (%d)", params.MaximumExtraDataSize, len(header.Extra))
Expand All @@ -213,11 +211,11 @@ func (s *Serenity) verifyHeader(chain consensus.ChainHeaderReader, header, paren
return errOlderBlockTime
}

if header.Difficulty.Cmp(SerenityDifficulty) != 0 {
if header.Difficulty.Cmp(ProofOfStakeDifficulty) != 0 {
return errInvalidDifficulty
}

if !bytes.Equal(header.Nonce[:], SerenityNonce[:]) {
if !bytes.Equal(header.Nonce[:], ProofOfStakeNonce[:]) {
return errInvalidNonce
}

Expand Down Expand Up @@ -263,30 +261,30 @@ func (s *Serenity) verifyHeader(chain consensus.ChainHeaderReader, header, paren
return nil
}

func (s *Serenity) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
func (s *Merge) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
if !IsPoSHeader(block.Header()) {
return s.eth1Engine.Seal(chain, block, results, stop)
}
return nil
}

func (s *Serenity) GenerateSeal(chain consensus.ChainHeaderReader, currnt, parent *types.Header, call consensus.Call) []byte {
func (s *Merge) GenerateSeal(chain consensus.ChainHeaderReader, currnt, parent *types.Header, call consensus.Call) []byte {
return nil
}

func (s *Serenity) IsServiceTransaction(sender libcommon.Address, syscall consensus.SystemCall) bool {
func (s *Merge) IsServiceTransaction(sender libcommon.Address, syscall consensus.SystemCall) bool {
return s.eth1Engine.IsServiceTransaction(sender, syscall)
}

func (s *Serenity) Initialize(config *chain.Config, chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, syscall consensus.SystemCall) {
func (s *Merge) Initialize(config *chain.Config, chain consensus.ChainHeaderReader, header *types.Header, state *state.IntraBlockState, txs []types.Transaction, uncles []*types.Header, syscall consensus.SystemCall) {
s.eth1Engine.Initialize(config, chain, header, state, txs, uncles, syscall)
}

func (s *Serenity) APIs(chain consensus.ChainHeaderReader) []rpc.API {
func (s *Merge) APIs(chain consensus.ChainHeaderReader) []rpc.API {
return s.eth1Engine.APIs(chain)
}

func (s *Serenity) Close() error {
func (s *Merge) Close() error {
return s.eth1Engine.Close()
}

Expand All @@ -297,7 +295,7 @@ func IsPoSHeader(header *types.Header) bool {
if header.Difficulty == nil {
panic("IsPoSHeader called with invalid difficulty")
}
return header.Difficulty.Cmp(SerenityDifficulty) == 0
return header.Difficulty.Cmp(ProofOfStakeDifficulty) == 0
}

// IsTTDReached checks if the TotalTerminalDifficulty has been surpassed on the `parentHash` block.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package serenity
package merge

import (
"math/big"
Expand Down Expand Up @@ -48,14 +48,14 @@ func TestVerifyHeaderDifficulty(t *testing.T) {
parent := &types.Header{}

var eth1Engine consensus.Engine
serenity := New(eth1Engine)
mergeEngine := New(eth1Engine)

err := serenity.verifyHeader(readerMock{}, header, parent)
err := mergeEngine.verifyHeader(readerMock{}, header, parent)
if err != errInvalidDifficulty {
if err != nil {
t.Fatalf("Serenity should not accept non-zero difficulty, got %s", err.Error())
t.Fatalf("Merge engine should not accept non-zero difficulty, got %s", err.Error())
} else {
t.Fatalf("Serenity should not accept non-zero difficulty")
t.Fatalf("Merge engine should not accept non-zero difficulty")
}
}
}
Expand All @@ -70,14 +70,14 @@ func TestVerifyHeaderNonce(t *testing.T) {
parent := &types.Header{}

var eth1Engine consensus.Engine
serenity := New(eth1Engine)
mergeEngine := New(eth1Engine)

err := serenity.verifyHeader(readerMock{}, header, parent)
err := mergeEngine.verifyHeader(readerMock{}, header, parent)
if err != errInvalidNonce {
if err != nil {
t.Fatalf("Serenity should not accept non-zero difficulty, got %s", err.Error())
t.Fatalf("Merge engine should not accept non-zero difficulty, got %s", err.Error())
} else {
t.Fatalf("Serenity should not accept non-zero difficulty")
t.Fatalf("Merge engine should not accept non-zero difficulty")
}
}
}
7 changes: 3 additions & 4 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ import (
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/core/systemcontracts"
"github.com/ledgerwatch/erigon/eth/ethconfig"

"github.com/ledgerwatch/erigon-lib/kv"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/merge"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
Expand Down Expand Up @@ -277,7 +276,7 @@ func (cp *ChainPack) Copy() *ChainPack {

func (cp *ChainPack) NumberOfPoWBlocks() int {
for i, header := range cp.Headers {
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
if header.Difficulty.Cmp(merge.ProofOfStakeDifficulty) == 0 {
return i
}
}
Expand Down
7 changes: 4 additions & 3 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"math/big"

"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/serenity"
"github.com/ledgerwatch/erigon/consensus/merge"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
)
Expand All @@ -50,8 +51,8 @@ func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) libco
}

var prevRandDao *libcommon.Hash
if header.Difficulty.Cmp(serenity.SerenityDifficulty) == 0 {
// EIP-4399. We use SerenityDifficulty (i.e. 0) as a telltale of Proof-of-Stake blocks.
if header.Difficulty.Cmp(merge.ProofOfStakeDifficulty) == 0 {
// EIP-4399. We use ProofOfStakeDifficulty (i.e. 0) as a telltale of Proof-of-Stake blocks.
prevRandDao = &header.MixDigest
}

Expand Down
Loading

0 comments on commit 771c6fc

Please sign in to comment.