Skip to content

Commit

Permalink
Implement and add changes for EIPs 7002, 6110, 7685 (#12159)
Browse files Browse the repository at this point in the history
Cherry pick #10533

---------

Co-authored-by: Somnath <snb895@outlook.com>
  • Loading branch information
yperbasis and somnathb1 authored Oct 1, 2024
1 parent 52685b4 commit 9892c85
Show file tree
Hide file tree
Showing 50 changed files with 45,529 additions and 424 deletions.
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type stEnv struct {
UncleHash libcommon.Hash `json:"uncleHash,omitempty"`
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
WithdrawalsHash *libcommon.Hash `json:"withdrawalsRoot,omitempty"`
Requests []*types.Request `json:"requests,omitempty"`
Requests types.Requests `json:"requests,omitempty"`
RequestsRoot *libcommon.Hash `json:"requestsRoot,omitempty"`
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/evm/internal/t8ntool/gen_stenv.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ func Main(ctx *cli.Context) error {
t8logger := log.New("t8ntool")
chainReader := stagedsync.NewChainReaderImpl(chainConfig, tx, nil, t8logger)
result, err := core.ExecuteBlockEphemerally(chainConfig, &vmConfig, getHash, engine, block, reader, writer, chainReader, getTracer, t8logger)

if hashError != nil {
return NewError(ErrorMissingBlockhash, fmt.Errorf("blockhash error: %v", err))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ func stageLogIndex(db kv.RwDB, ctx context.Context, logger log.Logger) error {
logger.Info("Stage exec", "progress", execAt)
logger.Info("Stage", "name", s.ID, "progress", s.BlockNumber)

cfg := stagedsync.StageLogIndexCfg(db, pm, dirs.Tmp, chainConfig.DepositContract)
cfg := stagedsync.StageLogIndexCfg(db, pm, dirs.Tmp, &chainConfig.DepositContract)
if unwind > 0 {
u := sync.NewUnwindState(stages.LogIndex, s.BlockNumber-unwind, s.BlockNumber)
err = stagedsync.UnwindLogIndex(u, s, tx, cfg, ctx)
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ func (e *remoteConsensusEngine) Prepare(_ consensus.ChainHeaderReader, _ *types.
panic("remoteConsensusEngine.Prepare not supported")
}

func (e *remoteConsensusEngine) Finalize(_ *chain.Config, _ *types.Header, _ *state.IntraBlockState, _ types.Transactions, _ []*types.Header, _ types.Receipts, _ []*types.Withdrawal, _ consensus.ChainReader, _ consensus.SystemCall, _ log.Logger) (types.Transactions, types.Receipts, error) {
func (e *remoteConsensusEngine) Finalize(_ *chain.Config, _ *types.Header, _ *state.IntraBlockState, _ types.Transactions, _ []*types.Header, _ types.Receipts, _ []*types.Withdrawal, _ consensus.ChainReader, _ consensus.SystemCall, _ log.Logger) (types.Transactions, types.Receipts, types.Requests, error) {
panic("remoteConsensusEngine.Finalize not supported")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/state/exec3/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (rw *Worker) RunTxTaskNoLock(txTask *exec22.TxTask) {
return core.SysCallContract(contract, data, rw.chainConfig, ibs, header, rw.engine, false /* constCall */)
}

if _, _, err := rw.engine.Finalize(rw.chainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, nil, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, logger); err != nil {
if _, _, _, err := rw.engine.Finalize(rw.chainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, nil, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, logger); err != nil {
//fmt.Printf("error=%v\n", err)
txTask.Error = err
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/exec3/state_recon.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (rw *ReconWorker) runTxTask(txTask *exec22.TxTask) error {
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
return core.SysCallContract(contract, data, rw.chainConfig, ibs, txTask.Header, rw.engine, false /* constCall */)
}
if _, _, err := rw.engine.Finalize(rw.chainConfig, types.CopyHeader(txTask.Header), ibs, txTask.Txs, txTask.Uncles, nil, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, logger); err != nil {
if _, _, _, err := rw.engine.Finalize(rw.chainConfig, types.CopyHeader(txTask.Header), ibs, txTask.Txs, txTask.Uncles, nil, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, logger); err != nil {
if _, readError := rw.stateReader.ReadError(); !readError {
return fmt.Errorf("finalize of block %d failed: %w", txTask.BlockNum, err)
}
Expand Down
20 changes: 10 additions & 10 deletions consensus/aura/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,11 +699,11 @@ func (c *AuRa) applyRewards(header *types.Header, state *state.IntraBlockState,

// word `signal epoch` == word `pending epoch`
func (c *AuRa) Finalize(config *chain.Config, header *types.Header, state *state.IntraBlockState, txs types.Transactions,
uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests []*types.Request,
uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests,
chain consensus.ChainReader, syscall consensus.SystemCall, logger log.Logger,
) (types.Transactions, types.Receipts, error) {
) (types.Transactions, types.Receipts, types.Requests, error) {
if err := c.applyRewards(header, state, syscall); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

// check_and_lock_block -> check_epoch_end_signal (after enact)
Expand All @@ -712,14 +712,14 @@ func (c *AuRa) Finalize(config *chain.Config, header *types.Header, state *state
}
pendingTransitionProof, err := c.cfg.Validators.signalEpochEnd(header.Number.Uint64() == 0, header, receipts)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
if pendingTransitionProof != nil {
if header.Number.Uint64() >= DEBUG_LOG_FROM {
fmt.Printf("insert_pending_transition: %d,receipts=%d, lenProof=%d\n", header.Number.Uint64(), len(receipts), len(pendingTransitionProof))
}
if err = c.e.PutPendingEpoch(header.Hash(), header.Number.Uint64(), pendingTransitionProof); err != nil {
return nil, nil, err
return nil, nil, nil, err
}
}
// check_and_lock_block -> check_epoch_end_signal END
Expand All @@ -728,17 +728,17 @@ func (c *AuRa) Finalize(config *chain.Config, header *types.Header, state *state
c.EpochManager.finalityChecker.print(header.Number.Uint64())
epochEndProof, err := isEpochEnd(chain, c.e, finalized, header)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
if epochEndProof != nil {
c.EpochManager.noteNewEpoch()
logger.Info("[aura] epoch transition", "block_num", header.Number.Uint64())
if err := c.e.PutEpoch(header.Hash(), header.Number.Uint64(), epochEndProof); err != nil {
return nil, nil, err
return nil, nil, nil, err
}
}

return txs, receipts, nil
return txs, receipts, nil, nil
}

func buildFinality(e *EpochManager, chain consensus.ChainHeaderReader, er *NonTransactionalEpochReader, validators ValidatorSet, header *types.Header, syscall consensus.SystemCall) []unAssembledHeader {
Expand Down Expand Up @@ -838,8 +838,8 @@ func allHeadersUntil(chain consensus.ChainHeaderReader, from *types.Header, to l
//}

// FinalizeAndAssemble implements consensus.Engine
func (c *AuRa) FinalizeAndAssemble(config *chain.Config, header *types.Header, state *state.IntraBlockState, txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests []*types.Request, chain consensus.ChainReader, syscall consensus.SystemCall, call consensus.Call, logger log.Logger) (*types.Block, types.Transactions, types.Receipts, error) {
outTxs, outReceipts, err := c.Finalize(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, logger)
func (c *AuRa) FinalizeAndAssemble(config *chain.Config, header *types.Header, state *state.IntraBlockState, txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests, chain consensus.ChainReader, syscall consensus.SystemCall, call consensus.Call, logger log.Logger) (*types.Block, types.Transactions, types.Receipts, error) {
outTxs, outReceipts, _, err := c.Finalize(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, logger)
if err != nil {
return nil, nil, nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,18 @@ func (c *Clique) CalculateRewards(config *chain.Config, header *types.Header, un
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
func (c *Clique) Finalize(config *chain.Config, header *types.Header, state *state.IntraBlockState,
txs types.Transactions, uncles []*types.Header, r types.Receipts, withdrawals []*types.Withdrawal, requests []*types.Request,
txs types.Transactions, uncles []*types.Header, r types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests,
chain consensus.ChainReader, syscall consensus.SystemCall, logger log.Logger,
) (types.Transactions, types.Receipts, error) {
) (types.Transactions, types.Receipts, types.Requests, error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.UncleHash = types.CalcUncleHash(nil)
return txs, r, nil
return txs, r, nil, nil
}

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (c *Clique) FinalizeAndAssemble(chainConfig *chain.Config, header *types.Header, state *state.IntraBlockState,
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests []*types.Request, chain consensus.ChainReader, syscall consensus.SystemCall, call consensus.Call, logger log.Logger,
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests, chain consensus.ChainReader, syscall consensus.SystemCall, call consensus.Call, logger log.Logger,
) (*types.Block, types.Transactions, types.Receipts, error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.UncleHash = types.CalcUncleHash(nil)
Expand Down
6 changes: 3 additions & 3 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ type EngineWriter interface {
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
Finalize(config *chain.Config, header *types.Header, state *state.IntraBlockState,
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests []*types.Request, chain ChainReader, syscall SystemCall, logger log.Logger,
) (types.Transactions, types.Receipts, error)
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests, chain ChainReader, syscall SystemCall, logger log.Logger,
) (types.Transactions, types.Receipts, types.Requests, error)

// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
// rewards) and assembles the final block.
//
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
FinalizeAndAssemble(config *chain.Config, header *types.Header, state *state.IntraBlockState,
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests []*types.Request, chain ChainReader, syscall SystemCall, call Call, logger log.Logger,
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests, chain ChainReader, syscall SystemCall, call Call, logger log.Logger,
) (*types.Block, types.Transactions, types.Receipts, error)

// Seal generates a new sealing request for the given input block and pushes
Expand Down
10 changes: 5 additions & 5 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,23 +563,23 @@ func (ethash *Ethash) Initialize(config *chain.Config, chain consensus.ChainHead
// Finalize implements consensus.Engine, accumulating the block and uncle rewards,
// setting the final state on the header
func (ethash *Ethash) Finalize(config *chain.Config, header *types.Header, state *state.IntraBlockState,
txs types.Transactions, uncles []*types.Header, r types.Receipts, withdrawals []*types.Withdrawal, requests []*types.Request,
txs types.Transactions, uncles []*types.Header, r types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests,
chain consensus.ChainReader, syscall consensus.SystemCall, logger log.Logger,
) (types.Transactions, types.Receipts, error) {
) (types.Transactions, types.Receipts, types.Requests, error) {
// Accumulate any block and uncle rewards and commit the final state root
accumulateRewards(config, state, header, uncles)
return txs, r, nil
return txs, r, nil, nil
}

// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
// uncle rewards, setting the final state and assembling the block.
func (ethash *Ethash) FinalizeAndAssemble(chainConfig *chain.Config, header *types.Header, state *state.IntraBlockState,
txs types.Transactions, uncles []*types.Header, r types.Receipts, withdrawals []*types.Withdrawal, requests []*types.Request,
txs types.Transactions, uncles []*types.Header, r types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests,
chain consensus.ChainReader, syscall consensus.SystemCall, call consensus.Call, logger log.Logger,
) (*types.Block, types.Transactions, types.Receipts, error) {

// Finalize block
outTxs, outR, err := ethash.Finalize(chainConfig, header, state, txs, uncles, r, withdrawals, requests, chain, syscall, logger)
outTxs, outR, _, err := ethash.Finalize(chainConfig, header, state, txs, uncles, r, withdrawals, requests, chain, syscall, logger)
if err != nil {
return nil, nil, nil, err
}
Expand Down
53 changes: 44 additions & 9 deletions consensus/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"reflect"

"github.com/holiman/uint256"

Expand Down Expand Up @@ -131,16 +132,16 @@ func (s *Merge) CalculateRewards(config *chain.Config, header *types.Header, unc
}

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, requests []*types.Request,
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests,
chain consensus.ChainReader, syscall consensus.SystemCall, logger log.Logger,
) (types.Transactions, types.Receipts, error) {
) (types.Transactions, types.Receipts, types.Requests, error) {
if !misc.IsPoSHeader(header) {
return s.eth1Engine.Finalize(config, header, state, txs, uncles, r, withdrawals, requests, chain, syscall, logger)
return s.eth1Engine.Finalize(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, logger)
}

rewards, err := s.CalculateRewards(config, header, uncles, syscall)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
for _, r := range rewards {
state.AddBalance(r.Beneficiary, &r.Amount)
Expand All @@ -149,7 +150,7 @@ func (s *Merge) Finalize(config *chain.Config, header *types.Header, state *stat
if withdrawals != nil {
if auraEngine, ok := s.eth1Engine.(*aura.AuRa); ok {
if err := auraEngine.ExecuteSystemWithdrawals(withdrawals, syscall); err != nil {
return nil, nil, err
return nil, nil, nil, err
}
} else {
for _, w := range withdrawals {
Expand All @@ -159,20 +160,54 @@ func (s *Merge) Finalize(config *chain.Config, header *types.Header, state *stat
}
}

return txs, r, nil
var rs types.Requests
if config.IsPrague(header.Time) {
rs = make(types.Requests, 0)
allLogs := types.Logs{}
for _, rec := range receipts {
allLogs = append(allLogs, rec.Logs...)
}
ds, err := types.ParseDepositLogs(allLogs, config.DepositContract)
rs = append(rs, ds...)
if err != nil {
return nil, nil, nil, fmt.Errorf("error: could not parse requests logs: %v", err)
}

rs = append(rs, misc.DequeueWithdrawalRequests7002(syscall)...)
if requests != nil || header.RequestsRoot != nil {
rh := types.DeriveSha(rs)
if *header.RequestsRoot != rh {
return nil, nil, nil, fmt.Errorf("error: invalid requests root hash in header, expected: %v, got :%v", header.RequestsRoot, rh)
}
sds := requests.Deposits()
if !reflect.DeepEqual(sds, ds.Deposits()) {
return nil, nil, nil, fmt.Errorf("error: invalid deposits in block")
}
//TODO @somnathb1 add DeepEqual check for WithdrawaRequests too, because in future there could be other types of requests
}
}

return txs, receipts, rs, nil
}

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, requests []*types.Request, chain consensus.ChainReader, syscall consensus.SystemCall, call consensus.Call, logger log.Logger,
txs types.Transactions, uncles []*types.Header, receipts types.Receipts, withdrawals []*types.Withdrawal, requests types.Requests, chain consensus.ChainReader, syscall consensus.SystemCall, call consensus.Call, logger log.Logger,
) (*types.Block, types.Transactions, types.Receipts, error) {

if !misc.IsPoSHeader(header) {
return s.eth1Engine.FinalizeAndAssemble(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, call, logger)
}
outTxs, outReceipts, err := s.Finalize(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, logger)
header.RequestsRoot = nil
outTxs, outReceipts, rs, err := s.Finalize(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, logger)
if err != nil {
return nil, nil, nil, err
}
return types.NewBlock(header, outTxs, uncles, outReceipts, withdrawals, requests), outTxs, outReceipts, nil
if config.IsPrague(header.Time) {
if rs == nil {
rs = make(types.Requests, 0)
}
}
return types.NewBlock(header, outTxs, uncles, outReceipts, withdrawals, rs), outTxs, outReceipts, nil
}

func (s *Merge) SealHash(header *types.Header) (hash libcommon.Hash) {
Expand Down
Loading

0 comments on commit 9892c85

Please sign in to comment.