Skip to content

Commit

Permalink
Add requests to Finalize interface in consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
somnathb1 committed May 28, 2024
1 parent dbe4abc commit d91925a
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,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 @@ -206,7 +206,7 @@ func (rw *Worker) RunTxTaskNoLock(txTask *state.TxTask) {
return core.SysCallContract(contract, data, rw.chainConfig, ibs, header, rw.engine, false /* constCall */)
}

_, _, err := rw.engine.Finalize(rw.chainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, rw.logger)
_, _, _, err := rw.engine.Finalize(rw.chainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, rw.logger)
if err != nil {
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 @@ -308,7 +308,7 @@ func (rw *ReconWorker) runTxTask(txTask *state.TxTask) error {
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
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, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, rw.logger); err != nil {
if _, _, _, err := rw.engine.Finalize(rw.chainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, rw.logger); err != nil {
if _, readError := rw.stateReader.ReadError(); !readError {
return fmt.Errorf("finalize of block %d failed: %w", txTask.BlockNum, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/exec3/trace_worker2.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (rw *TraceWorker2) RunTxTask(txTask *state.TxTask) {
return core.SysCallContract(contract, data, rw.execArgs.ChainConfig, ibs, header, rw.execArgs.Engine, false /* constCall */)
}

_, _, err := rw.execArgs.Engine.Finalize(rw.execArgs.ChainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, rw.logger)
_, _, _, err := rw.execArgs.Engine.Finalize(rw.execArgs.ChainConfig, types.CopyHeader(header), ibs, txTask.Txs, txTask.Uncles, txTask.BlockReceipts, txTask.Withdrawals, txTask.Requests, rw.chain, syscall, rw.logger)
if err != nil {
txTask.Error = err
}
Expand Down
16 changes: 8 additions & 8 deletions consensus/aura/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@ func (c *AuRa) applyRewards(header *types.Header, state *state.IntraBlockState,
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.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 @@ -716,14 +716,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 @@ -732,17 +732,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 @@ -843,7 +843,7 @@ 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.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)
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
4 changes: 2 additions & 2 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ func (c *Clique) CalculateRewards(config *chain.Config, header *types.Header, un
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.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,
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type EngineWriter interface {
// 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.Requests, chain ChainReader, syscall SystemCall, logger log.Logger,
) (types.Transactions, types.Receipts, error)
) (types.Transactions, types.Receipts, types.Requests, error)

// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
// rewards) and assembles the final block.
Expand Down
6 changes: 3 additions & 3 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,10 @@ func (ethash *Ethash) Initialize(config *chain.Config, chain consensus.ChainHead
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.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
Expand All @@ -582,7 +582,7 @@ func (ethash *Ethash) FinalizeAndAssemble(chainConfig *chain.Config, header *typ
) (*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
15 changes: 9 additions & 6 deletions consensus/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ 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.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)
}

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 +149,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,7 +159,10 @@ func (s *Merge) Finalize(config *chain.Config, header *types.Header, state *stat
}
}

return txs, r, nil
requests = append(types.Requests{}, misc.DequeueWithdrawalRequests7002(syscall)...)


return txs, r, requests, nil
}

func (s *Merge) FinalizeAndAssemble(config *chain.Config, header *types.Header, state *state.IntraBlockState,
Expand All @@ -170,8 +173,8 @@ func (s *Merge) FinalizeAndAssemble(config *chain.Config, header *types.Header,
return s.eth1Engine.FinalizeAndAssemble(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, call, logger)
}
// get the deposits (TODO @somnathb1) and withdrawals and append it to requests
requests = append(types.Requests{}, misc.DequeueWithdrawalRequests7002(syscall)...)
outTxs, outReceipts, err := s.Finalize(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, logger)
// requests = append(types.Requests{}, misc.DequeueWithdrawalRequests7002(syscall)...)
outTxs, outReceipts, requests, err := s.Finalize(config, header, state, txs, uncles, receipts, withdrawals, requests, chain, syscall, logger)
if err != nil {
return nil, nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func FinalizeBlockExecution(
// newBlock.header

} else {
_, _, err = engine.Finalize(cc, header, ibs, txs, uncles, receipts, withdrawals, requests, chainReader, syscall, logger)
_, _, _, err = engine.Finalize(cc, header, ibs, txs, uncles, receipts, withdrawals, requests, chainReader, syscall, logger)
}
if err != nil {
return nil, nil, nil, err
Expand Down
14 changes: 7 additions & 7 deletions polygon/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,15 +976,15 @@ func (c *Bor) CalculateRewards(config *chain.Config, header *types.Header, uncle
func (c *Bor) Finalize(config *chain.Config, header *types.Header, state *state.IntraBlockState,
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) {
headerNumber := header.Number.Uint64()

if withdrawals != nil || header.WithdrawalsHash != nil {
return nil, nil, consensus.ErrUnexpectedWithdrawals
return nil, nil, nil, consensus.ErrUnexpectedWithdrawals
}

if requests != nil || header.RequestsRoot != nil {
return nil, nil, consensus.ErrUnexpectedRequests
return nil, nil, nil, consensus.ErrUnexpectedRequests
}

if isSprintStart(headerNumber, c.config.CalculateSprintLength(headerNumber)) {
Expand All @@ -995,20 +995,20 @@ func (c *Bor) Finalize(config *chain.Config, header *types.Header, state *state.
if err := c.checkAndCommitSpan(state, header, cx, syscall); err != nil {
err := fmt.Errorf("Finalize.checkAndCommitSpan: %w", err)
c.logger.Error("[bor] committing span", "err", err)
return nil, types.Receipts{}, err
return nil, types.Receipts{}, nil, err
}
// commit states
if err := c.CommitStates(state, header, cx, syscall); err != nil {
err := fmt.Errorf("Finalize.CommitStates: %w", err)
c.logger.Error("[bor] Error while committing states", "err", err)
return nil, types.Receipts{}, err
return nil, types.Receipts{}, nil, err
}
}
}

if err := c.changeContractCodeIfNeeded(headerNumber, state); err != nil {
c.logger.Error("[bor] Error changing contract code", "err", err)
return nil, types.Receipts{}, err
return nil, types.Receipts{}, nil, err
}

// No block rewards in PoA, so the state remains as is and uncles are dropped
Expand All @@ -1018,7 +1018,7 @@ func (c *Bor) Finalize(config *chain.Config, header *types.Header, state *state.
// Set state sync data to blockchain
// bc := chain.(*core.BlockChain)
// bc.SetStateSync(stateSyncData)
return nil, types.Receipts{}, nil
return nil, types.Receipts{}, nil, nil
}

func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.IntraBlockState) error {
Expand Down
2 changes: 1 addition & 1 deletion polygon/bor/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func NewFaker() *FakeBor {
func (f *FakeBor) Finalize(config *chain.Config, header *types.Header, state *state.IntraBlockState,
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) {
return f.FakeEthash.Finalize(config, header, state, txs, uncles, r, withdrawals, requests, chain, syscall, logger)
}

0 comments on commit d91925a

Please sign in to comment.