XDCx,core,XDCxlending,miner: reduce duplicate calls

This commit is contained in:
JukLee0ira 2024-06-04 18:56:56 +08:00
parent 9ffacc595d
commit 15bbb5d3d5
4 changed files with 59 additions and 51 deletions

View file

@ -595,10 +595,11 @@ func (XDCx *XDCX) GetTriegc() *prque.Prque {
func (XDCx *XDCX) GetTradingStateRoot(block *types.Block, author common.Address) (common.Hash, error) { func (XDCx *XDCX) GetTradingStateRoot(block *types.Block, author common.Address) (common.Hash, error) {
for _, tx := range block.Transactions() { for _, tx := range block.Transactions() {
from := *(tx.From()) to := tx.To()
if tx.To() != nil && *tx.To() == common.TradingStateAddrBinary && from == author { if to != nil && *to == common.TradingStateAddrBinary && *tx.From() == author {
if len(tx.Data()) >= 32 { data := tx.Data()
return common.BytesToHash(tx.Data()[:32]), nil if len(data) >= 32 {
return common.BytesToHash(data[:32]), nil
} }
} }
} }

View file

@ -696,10 +696,11 @@ func (l *Lending) GetTriegc() *prque.Prque {
func (l *Lending) GetLendingStateRoot(block *types.Block, author common.Address) (common.Hash, error) { func (l *Lending) GetLendingStateRoot(block *types.Block, author common.Address) (common.Hash, error) {
for _, tx := range block.Transactions() { for _, tx := range block.Transactions() {
from := *(tx.From()) to := tx.To()
if tx.To() != nil && *tx.To() == common.TradingStateAddrBinary && from == author { if to != nil && *to == common.TradingStateAddrBinary && *tx.From() == author {
if len(tx.Data()) >= 64 { data := tx.Data()
return common.BytesToHash(tx.Data()[32:]), nil if len(data) >= 64 {
return common.BytesToHash(data[32:]), nil
} }
} }
} }

View file

@ -215,13 +215,14 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated
// for the transaction, gas used and an error if the transaction failed, // for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid. // indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*big.Int, bc *BlockChain, author *common.Address, gp *GasPool, statedb *state.StateDB, XDCxState *tradingstate.TradingStateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error, bool) { func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*big.Int, bc *BlockChain, author *common.Address, gp *GasPool, statedb *state.StateDB, XDCxState *tradingstate.TradingStateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, uint64, error, bool) {
if tx.To() != nil && *tx.To() == common.BlockSignersBinary && config.IsTIPSigning(header.Number) { to := tx.To()
if to != nil && *to == common.BlockSignersBinary && config.IsTIPSigning(header.Number) {
return ApplySignTransaction(config, statedb, header, tx, usedGas) return ApplySignTransaction(config, statedb, header, tx, usedGas)
} }
if tx.To() != nil && *tx.To() == common.TradingStateAddrBinary && config.IsTIPXDCXReceiver(header.Number) { if to != nil && *to == common.TradingStateAddrBinary && config.IsTIPXDCXReceiver(header.Number) {
return ApplyEmptyTransaction(config, statedb, header, tx, usedGas) return ApplyEmptyTransaction(config, statedb, header, tx, usedGas)
} }
if tx.To() != nil && *tx.To() == common.XDCXLendingAddressBinary && config.IsTIPXDCXReceiver(header.Number) { if to != nil && *to == common.XDCXLendingAddressBinary && config.IsTIPXDCXReceiver(header.Number) {
return ApplyEmptyTransaction(config, statedb, header, tx, usedGas) return ApplyEmptyTransaction(config, statedb, header, tx, usedGas)
} }
if tx.IsTradingTransaction() && config.IsTIPXDCXReceiver(header.Number) { if tx.IsTradingTransaction() && config.IsTIPXDCXReceiver(header.Number) {
@ -233,8 +234,8 @@ func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
} }
var balanceFee *big.Int var balanceFee *big.Int
if tx.To() != nil { if to != nil {
if value, ok := tokensFee[*tx.To()]; ok { if value, ok := tokensFee[*to]; ok {
balanceFee = value balanceFee = value
} }
} }
@ -441,7 +442,7 @@ func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
receipt.BlockNumber = header.Number receipt.BlockNumber = header.Number
receipt.TransactionIndex = uint(statedb.TxIndex()) receipt.TransactionIndex = uint(statedb.TxIndex())
if balanceFee != nil && failed { if balanceFee != nil && failed {
state.PayFeeWithTRC21TxFail(statedb, msg.From(), *tx.To()) state.PayFeeWithTRC21TxFail(statedb, msg.From(), *to)
} }
return receipt, gas, err, balanceFee != nil return receipt, gas, err, balanceFee != nil
} }

View file

@ -808,26 +808,27 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
var coalescedLogs []*types.Log var coalescedLogs []*types.Log
// first priority for special Txs // first priority for special Txs
for _, tx := range specialTxs { for _, tx := range specialTxs {
to := tx.To()
//HF number for black-list //HF number for black-list
if (env.header.Number.Uint64() >= common.BlackListHFNumber) && !common.IsTestnet { if (env.header.Number.Uint64() >= common.BlackListHFNumber) && !common.IsTestnet {
from := tx.From()
// check if sender is in black list // check if sender is in black list
if tx.From() != nil && common.Blacklist[*tx.From()] { if from != nil && common.Blacklist[*from] {
log.Debug("Skipping transaction with sender in black-list", "sender", tx.From().Hex()) log.Debug("Skipping transaction with sender in black-list", "sender", from.Hex())
continue continue
} }
// check if receiver is in black list // check if receiver is in black list
if tx.To() != nil && common.Blacklist[*tx.To()] { if to != nil && common.Blacklist[*to] {
log.Debug("Skipping transaction with receiver in black-list", "receiver", tx.To().Hex()) log.Debug("Skipping transaction with receiver in black-list", "receiver", to.Hex())
continue continue
} }
} }
data := tx.Data()
// validate minFee slot for XDCZ // validate minFee slot for XDCZ
if tx.IsXDCZApplyTransaction() { if tx.IsXDCZApplyTransaction() {
copyState, _ := bc.State() copyState, _ := bc.State()
if err := core.ValidateXDCZApplyTransaction(bc, nil, copyState, common.BytesToAddress(tx.Data()[4:])); err != nil { if err := core.ValidateXDCZApplyTransaction(bc, nil, copyState, common.BytesToAddress(data[4:])); err != nil {
log.Debug("XDCZApply: invalid token", "token", common.BytesToAddress(tx.Data()[4:]).Hex()) log.Debug("XDCZApply: invalid token", "token", common.BytesToAddress(data[4:]).Hex())
txs.Pop() txs.Pop()
continue continue
} }
@ -835,8 +836,8 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
// validate balance slot, token decimal for XDCX // validate balance slot, token decimal for XDCX
if tx.IsXDCXApplyTransaction() { if tx.IsXDCXApplyTransaction() {
copyState, _ := bc.State() copyState, _ := bc.State()
if err := core.ValidateXDCXApplyTransaction(bc, nil, copyState, common.BytesToAddress(tx.Data()[4:])); err != nil { if err := core.ValidateXDCXApplyTransaction(bc, nil, copyState, common.BytesToAddress(data[4:])); err != nil {
log.Debug("XDCXApply: invalid token", "token", common.BytesToAddress(tx.Data()[4:]).Hex()) log.Debug("XDCXApply: invalid token", "token", common.BytesToAddress(data[4:]).Hex())
txs.Pop() txs.Pop()
continue continue
} }
@ -853,38 +854,39 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
from, _ := types.Sender(env.signer, tx) from, _ := types.Sender(env.signer, tx)
// Check whether the tx is replay protected. If we're not in the EIP155 hf // Check whether the tx is replay protected. If we're not in the EIP155 hf
// phase, start ignoring the sender until we do. // phase, start ignoring the sender until we do.
hash := tx.Hash()
if tx.Protected() && !env.config.IsEIP155(env.header.Number) { if tx.Protected() && !env.config.IsEIP155(env.header.Number) {
log.Trace("Ignoring reply protected special transaction", "hash", tx.Hash(), "eip155", env.config.EIP155Block) log.Trace("Ignoring reply protected special transaction", "hash", hash, "eip155", env.config.EIP155Block)
continue continue
} }
if *tx.To() == common.BlockSignersBinary { if *to == common.BlockSignersBinary {
if len(tx.Data()) < 68 { if len(data) < 68 {
log.Trace("Data special transaction invalid length", "hash", tx.Hash(), "data", len(tx.Data())) log.Trace("Data special transaction invalid length", "hash", hash, "data", len(data))
continue continue
} }
blkNumber := binary.BigEndian.Uint64(tx.Data()[8:40]) blkNumber := binary.BigEndian.Uint64(data[8:40])
if blkNumber >= env.header.Number.Uint64() || blkNumber <= env.header.Number.Uint64()-env.config.XDPoS.Epoch*2 { if blkNumber >= env.header.Number.Uint64() || blkNumber <= env.header.Number.Uint64()-env.config.XDPoS.Epoch*2 {
log.Trace("Data special transaction invalid number", "hash", tx.Hash(), "blkNumber", blkNumber, "miner", env.header.Number) log.Trace("Data special transaction invalid number", "hash", hash, "blkNumber", blkNumber, "miner", env.header.Number)
continue continue
} }
} }
// Start executing the transaction // Start executing the transaction
env.state.Prepare(tx.Hash(), common.Hash{}, env.tcount) env.state.Prepare(hash, common.Hash{}, env.tcount)
nonce := env.state.GetNonce(from) nonce := env.state.GetNonce(from)
if nonce != tx.Nonce() && !tx.IsSkipNonceTransaction() { if nonce != tx.Nonce() && !tx.IsSkipNonceTransaction() {
log.Trace("Skipping account with special transaction invalid nonce", "sender", from, "nonce", nonce, "tx nonce ", tx.Nonce(), "to", tx.To()) log.Trace("Skipping account with special transaction invalid nonce", "sender", from, "nonce", nonce, "tx nonce ", tx.Nonce(), "to", to)
continue continue
} }
err, logs, tokenFeeUsed, gas := env.commitTransaction(balanceFee, tx, bc, coinbase, gp) err, logs, tokenFeeUsed, gas := env.commitTransaction(balanceFee, tx, bc, coinbase, gp)
switch err { switch err {
case core.ErrNonceTooLow: case core.ErrNonceTooLow:
// New head notification data race between the transaction pool and miner, shift // New head notification data race between the transaction pool and miner, shift
log.Trace("Skipping special transaction with low nonce", "sender", from, "nonce", tx.Nonce(), "to", tx.To()) log.Trace("Skipping special transaction with low nonce", "sender", from, "nonce", tx.Nonce(), "to", to)
case core.ErrNonceTooHigh: case core.ErrNonceTooHigh:
// Reorg notification data race between the transaction pool and miner, skip account = // Reorg notification data race between the transaction pool and miner, skip account =
log.Trace("Skipping account with special transaction hight nonce", "sender", from, "nonce", tx.Nonce(), "to", tx.To()) log.Trace("Skipping account with special transaction hight nonce", "sender", from, "nonce", tx.Nonce(), "to", to)
case nil: case nil:
// Everything ok, collect the logs and shift in the next transaction from the same account // Everything ok, collect the logs and shift in the next transaction from the same account
coalescedLogs = append(coalescedLogs, logs...) coalescedLogs = append(coalescedLogs, logs...)
@ -893,12 +895,12 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
default: default:
// Strange error, discard the transaction and get the next in line (note, the // Strange error, discard the transaction and get the next in line (note, the
// nonce-too-high clause will prevent us from executing in vain). // nonce-too-high clause will prevent us from executing in vain).
log.Debug("Add Special Transaction failed, account skipped", "hash", tx.Hash(), "sender", from, "nonce", tx.Nonce(), "to", tx.To(), "err", err) log.Debug("Add Special Transaction failed, account skipped", "hash", hash, "sender", from, "nonce", tx.Nonce(), "to", to, "err", err)
} }
if tokenFeeUsed { if tokenFeeUsed {
fee := common.GetGasFee(env.header.Number.Uint64(), gas) fee := common.GetGasFee(env.header.Number.Uint64(), gas)
balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee) balanceFee[*to] = new(big.Int).Sub(balanceFee[*to], fee)
balanceUpdated[*tx.To()] = balanceFee[*tx.To()] balanceUpdated[*to] = balanceFee[*to]
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee) totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)
} }
} }
@ -920,26 +922,28 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
} }
//HF number for black-list //HF number for black-list
to := tx.To()
if (env.header.Number.Uint64() >= common.BlackListHFNumber) && !common.IsTestnet { if (env.header.Number.Uint64() >= common.BlackListHFNumber) && !common.IsTestnet {
from := tx.From()
// check if sender is in black list // check if sender is in black list
if tx.From() != nil && common.Blacklist[*tx.From()] { if from != nil && common.Blacklist[*from] {
log.Debug("Skipping transaction with sender in black-list", "sender", tx.From().Hex()) log.Debug("Skipping transaction with sender in black-list", "sender", from.Hex())
txs.Pop() txs.Pop()
continue continue
} }
// check if receiver is in black list // check if receiver is in black list
if tx.To() != nil && common.Blacklist[*tx.To()] { if to != nil && common.Blacklist[*to] {
log.Debug("Skipping transaction with receiver in black-list", "receiver", tx.To().Hex()) log.Debug("Skipping transaction with receiver in black-list", "receiver", to.Hex())
txs.Shift() txs.Shift()
continue continue
} }
} }
data := tx.Data()
// validate minFee slot for XDCZ // validate minFee slot for XDCZ
if tx.IsXDCZApplyTransaction() { if tx.IsXDCZApplyTransaction() {
copyState, _ := bc.State() copyState, _ := bc.State()
if err := core.ValidateXDCZApplyTransaction(bc, nil, copyState, common.BytesToAddress(tx.Data()[4:])); err != nil { if err := core.ValidateXDCZApplyTransaction(bc, nil, copyState, common.BytesToAddress(data[4:])); err != nil {
log.Debug("XDCZApply: invalid token", "token", common.BytesToAddress(tx.Data()[4:]).Hex()) log.Debug("XDCZApply: invalid token", "token", common.BytesToAddress(data[4:]).Hex())
txs.Pop() txs.Pop()
continue continue
} }
@ -947,8 +951,8 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
// validate balance slot, token decimal for XDCX // validate balance slot, token decimal for XDCX
if tx.IsXDCXApplyTransaction() { if tx.IsXDCXApplyTransaction() {
copyState, _ := bc.State() copyState, _ := bc.State()
if err := core.ValidateXDCXApplyTransaction(bc, nil, copyState, common.BytesToAddress(tx.Data()[4:])); err != nil { if err := core.ValidateXDCXApplyTransaction(bc, nil, copyState, common.BytesToAddress(data[4:])); err != nil {
log.Debug("XDCXApply: invalid token", "token", common.BytesToAddress(tx.Data()[4:]).Hex()) log.Debug("XDCXApply: invalid token", "token", common.BytesToAddress(data[4:]).Hex())
txs.Pop() txs.Pop()
continue continue
} }
@ -959,15 +963,16 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
// //
// We use the eip155 signer regardless of the current hf. // We use the eip155 signer regardless of the current hf.
from, _ := types.Sender(env.signer, tx) from, _ := types.Sender(env.signer, tx)
hash := tx.Hash()
// Check whether the tx is replay protected. If we're not in the EIP155 hf // Check whether the tx is replay protected. If we're not in the EIP155 hf
// phase, start ignoring the sender until we do. // phase, start ignoring the sender until we do.
if tx.Protected() && !env.config.IsEIP155(env.header.Number) { if tx.Protected() && !env.config.IsEIP155(env.header.Number) {
log.Trace("Ignoring reply protected transaction", "hash", tx.Hash(), "eip155", env.config.EIP155Block) log.Trace("Ignoring reply protected transaction", "hash", hash, "eip155", env.config.EIP155Block)
txs.Pop() txs.Pop()
continue continue
} }
// Start executing the transaction // Start executing the transaction
env.state.Prepare(tx.Hash(), common.Hash{}, env.tcount) env.state.Prepare(hash, common.Hash{}, env.tcount)
nonce := env.state.GetNonce(from) nonce := env.state.GetNonce(from)
if nonce > tx.Nonce() { if nonce > tx.Nonce() {
// New head notification data race between the transaction pool and miner, shift // New head notification data race between the transaction pool and miner, shift
@ -1012,13 +1017,13 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
default: default:
// Strange error, discard the transaction and get the next in line (note, the // Strange error, discard the transaction and get the next in line (note, the
// nonce-too-high clause will prevent us from executing in vain). // nonce-too-high clause will prevent us from executing in vain).
log.Debug("Transaction failed, account skipped", "hash", tx.Hash(), "err", err) log.Debug("Transaction failed, account skipped", "hash", hash, "err", err)
txs.Shift() txs.Shift()
} }
if tokenFeeUsed { if tokenFeeUsed {
fee := common.GetGasFee(env.header.Number.Uint64(), gas) fee := common.GetGasFee(env.header.Number.Uint64(), gas)
balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee) balanceFee[*to] = new(big.Int).Sub(balanceFee[*to], fee)
balanceUpdated[*tx.To()] = balanceFee[*tx.To()] balanceUpdated[*to] = balanceFee[*to]
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee) totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)
} }
} }