Skip to content

Commit

Permalink
(parlia): clean up applying of internal transactions (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel authored Apr 26, 2024
1 parent 62ae021 commit 36ae78d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 105 deletions.
31 changes: 14 additions & 17 deletions consensus/parlia/feynmanfork.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package parlia
import (
"container/heap"
"fmt"
"math/big"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon/common/u256"
Expand All @@ -11,7 +13,6 @@ import (
"github.com/ledgerwatch/erigon/core/systemcontracts"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/log/v3"
"math/big"
)

const SecondsPerDay uint64 = 86400
Expand All @@ -27,8 +28,8 @@ func isBreatheBlock(lastBlockTime, blockTime uint64) bool {

// initializeFeynmanContract initialize new contracts of Feynman fork
func (p *Parlia) initializeFeynmanContract(state *state.IntraBlockState, header *types.Header,
txs types.Transactions, receipts types.Receipts, systemTxs types.Transactions, usedGas *uint64, mining bool,
) (types.Transactions, types.Transactions, types.Receipts, error) {
txs *types.Transactions, receipts *types.Receipts, systemTxs *types.Transactions, usedGas *uint64, mining bool,
) error {
// method
method := "initialize"

Expand All @@ -44,20 +45,16 @@ func (p *Parlia) initializeFeynmanContract(state *state.IntraBlockState, header
data, err := p.stakeHubABI.Pack(method)
if err != nil {
log.Error("Unable to pack tx for initialize feynman contracts", "error", err)
return nil, nil, nil, err
return err
}
for _, c := range contracts {
// apply message
log.Info("initialize feynman contract", "block number", header.Number.Uint64(), "contract", c)
var tx types.Transaction
var receipt *types.Receipt
if systemTxs, tx, receipt, err = p.applyTransaction(header.Coinbase, c, u256.Num0, data, state, header, len(txs), systemTxs, usedGas, mining); err != nil {
return nil, nil, nil, err
if err := p.applyTransaction(header.Coinbase, c, u256.Num0, data, state, header, txs, receipts, systemTxs, usedGas, mining); err != nil {
return err
}
txs = append(txs, tx)
receipts = append(receipts, receipt)
}
return txs, systemTxs, receipts, nil
return nil
}

type ValidatorItem struct {
Expand Down Expand Up @@ -95,17 +92,17 @@ func (h *ValidatorHeap) Pop() interface{} {
}

func (p *Parlia) updateValidatorSetV2(chain consensus.ChainHeaderReader, state *state.IntraBlockState, header *types.Header,
txs types.Transactions, systemTxs types.Transactions, usedGas *uint64, mining bool,
) (types.Transactions, types.Transaction, *types.Receipt, error) {
txs *types.Transactions, receipts *types.Receipts, systemTxs *types.Transactions, usedGas *uint64, mining bool,
) error {
// 1. get all validators and its voting header.Nu power
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
validatorItems, err := p.getValidatorElectionInfo(parent, state)
if err != nil {
return nil, nil, nil, err
return err
}
maxElectedValidators, err := p.getMaxElectedValidators(parent, state)
if err != nil {
return nil, nil, nil, err
return err
}

// 2. sort by voting power
Expand All @@ -116,11 +113,11 @@ func (p *Parlia) updateValidatorSetV2(chain consensus.ChainHeaderReader, state *
data, err := p.validatorSetABI.Pack(method, eValidators, eVotingPowers, eVoteAddrs)
if err != nil {
log.Error("Unable to pack tx for updateValidatorSetV2", "error", err)
return nil, nil, nil, err
return err
}

// apply message
return p.applyTransaction(header.Coinbase, systemcontracts.ValidatorContract, u256.Num0, data, state, header, len(txs), systemTxs, usedGas, mining)
return p.applyTransaction(header.Coinbase, systemcontracts.ValidatorContract, u256.Num0, data, state, header, txs, receipts, systemTxs, usedGas, mining)
}

func (p *Parlia) getValidatorElectionInfo(header *types.Header, ibs *state.IntraBlockState) ([]ValidatorItem, error) {
Expand Down
Loading

0 comments on commit 36ae78d

Please sign in to comment.