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) {
for _, tx := range block.Transactions() {
from := *(tx.From())
if tx.To() != nil && *tx.To() == common.TradingStateAddrBinary && from == author {
if len(tx.Data()) >= 32 {
return common.BytesToHash(tx.Data()[:32]), nil
to := tx.To()
if to != nil && *to == common.TradingStateAddrBinary && *tx.From() == author {
data := tx.Data()
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) {
for _, tx := range block.Transactions() {
from := *(tx.From())
if tx.To() != nil && *tx.To() == common.TradingStateAddrBinary && from == author {
if len(tx.Data()) >= 64 {
return common.BytesToHash(tx.Data()[32:]), nil
to := tx.To()
if to != nil && *to == common.TradingStateAddrBinary && *tx.From() == author {
data := tx.Data()
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,
// 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) {
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)
}
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)
}
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)
}
if tx.IsTradingTransaction() && config.IsTIPXDCXReceiver(header.Number) {
@ -233,8 +234,8 @@ func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
}
var balanceFee *big.Int
if tx.To() != nil {
if value, ok := tokensFee[*tx.To()]; ok {
if to != nil {
if value, ok := tokensFee[*to]; ok {
balanceFee = value
}
}
@ -441,7 +442,7 @@ func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
receipt.BlockNumber = header.Number
receipt.TransactionIndex = uint(statedb.TxIndex())
if balanceFee != nil && failed {
state.PayFeeWithTRC21TxFail(statedb, msg.From(), *tx.To())
state.PayFeeWithTRC21TxFail(statedb, msg.From(), *to)
}
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
// first priority for special Txs
for _, tx := range specialTxs {
to := tx.To()
//HF number for black-list
if (env.header.Number.Uint64() >= common.BlackListHFNumber) && !common.IsTestnet {
from := tx.From()
// check if sender is in black list
if tx.From() != nil && common.Blacklist[*tx.From()] {
log.Debug("Skipping transaction with sender in black-list", "sender", tx.From().Hex())
if from != nil && common.Blacklist[*from] {
log.Debug("Skipping transaction with sender in black-list", "sender", from.Hex())
continue
}
// check if receiver is in black list
if tx.To() != nil && common.Blacklist[*tx.To()] {
log.Debug("Skipping transaction with receiver in black-list", "receiver", tx.To().Hex())
if to != nil && common.Blacklist[*to] {
log.Debug("Skipping transaction with receiver in black-list", "receiver", to.Hex())
continue
}
}
data := tx.Data()
// validate minFee slot for XDCZ
if tx.IsXDCZApplyTransaction() {
copyState, _ := bc.State()
if err := core.ValidateXDCZApplyTransaction(bc, nil, copyState, common.BytesToAddress(tx.Data()[4:])); err != nil {
log.Debug("XDCZApply: invalid token", "token", common.BytesToAddress(tx.Data()[4:]).Hex())
if err := core.ValidateXDCZApplyTransaction(bc, nil, copyState, common.BytesToAddress(data[4:])); err != nil {
log.Debug("XDCZApply: invalid token", "token", common.BytesToAddress(data[4:]).Hex())
txs.Pop()
continue
}
@ -835,8 +836,8 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
// validate balance slot, token decimal for XDCX
if tx.IsXDCXApplyTransaction() {
copyState, _ := bc.State()
if err := core.ValidateXDCXApplyTransaction(bc, nil, copyState, common.BytesToAddress(tx.Data()[4:])); err != nil {
log.Debug("XDCXApply: invalid token", "token", common.BytesToAddress(tx.Data()[4:]).Hex())
if err := core.ValidateXDCXApplyTransaction(bc, nil, copyState, common.BytesToAddress(data[4:])); err != nil {
log.Debug("XDCXApply: invalid token", "token", common.BytesToAddress(data[4:]).Hex())
txs.Pop()
continue
}
@ -853,38 +854,39 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
from, _ := types.Sender(env.signer, tx)
// Check whether the tx is replay protected. If we're not in the EIP155 hf
// phase, start ignoring the sender until we do.
hash := tx.Hash()
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
}
if *tx.To() == common.BlockSignersBinary {
if len(tx.Data()) < 68 {
log.Trace("Data special transaction invalid length", "hash", tx.Hash(), "data", len(tx.Data()))
if *to == common.BlockSignersBinary {
if len(data) < 68 {
log.Trace("Data special transaction invalid length", "hash", hash, "data", len(data))
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 {
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
}
}
// 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)
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
}
err, logs, tokenFeeUsed, gas := env.commitTransaction(balanceFee, tx, bc, coinbase, gp)
switch err {
case core.ErrNonceTooLow:
// 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:
// 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:
// Everything ok, collect the logs and shift in the next transaction from the same account
coalescedLogs = append(coalescedLogs, logs...)
@ -893,12 +895,12 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
default:
// Strange error, discard the transaction and get the next in line (note, the
// 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 {
fee := common.GetGasFee(env.header.Number.Uint64(), gas)
balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee)
balanceUpdated[*tx.To()] = balanceFee[*tx.To()]
balanceFee[*to] = new(big.Int).Sub(balanceFee[*to], fee)
balanceUpdated[*to] = balanceFee[*to]
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
to := tx.To()
if (env.header.Number.Uint64() >= common.BlackListHFNumber) && !common.IsTestnet {
from := tx.From()
// check if sender is in black list
if tx.From() != nil && common.Blacklist[*tx.From()] {
log.Debug("Skipping transaction with sender in black-list", "sender", tx.From().Hex())
if from != nil && common.Blacklist[*from] {
log.Debug("Skipping transaction with sender in black-list", "sender", from.Hex())
txs.Pop()
continue
}
// check if receiver is in black list
if tx.To() != nil && common.Blacklist[*tx.To()] {
log.Debug("Skipping transaction with receiver in black-list", "receiver", tx.To().Hex())
if to != nil && common.Blacklist[*to] {
log.Debug("Skipping transaction with receiver in black-list", "receiver", to.Hex())
txs.Shift()
continue
}
}
data := tx.Data()
// validate minFee slot for XDCZ
if tx.IsXDCZApplyTransaction() {
copyState, _ := bc.State()
if err := core.ValidateXDCZApplyTransaction(bc, nil, copyState, common.BytesToAddress(tx.Data()[4:])); err != nil {
log.Debug("XDCZApply: invalid token", "token", common.BytesToAddress(tx.Data()[4:]).Hex())
if err := core.ValidateXDCZApplyTransaction(bc, nil, copyState, common.BytesToAddress(data[4:])); err != nil {
log.Debug("XDCZApply: invalid token", "token", common.BytesToAddress(data[4:]).Hex())
txs.Pop()
continue
}
@ -947,8 +951,8 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
// validate balance slot, token decimal for XDCX
if tx.IsXDCXApplyTransaction() {
copyState, _ := bc.State()
if err := core.ValidateXDCXApplyTransaction(bc, nil, copyState, common.BytesToAddress(tx.Data()[4:])); err != nil {
log.Debug("XDCXApply: invalid token", "token", common.BytesToAddress(tx.Data()[4:]).Hex())
if err := core.ValidateXDCXApplyTransaction(bc, nil, copyState, common.BytesToAddress(data[4:])); err != nil {
log.Debug("XDCXApply: invalid token", "token", common.BytesToAddress(data[4:]).Hex())
txs.Pop()
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.
from, _ := types.Sender(env.signer, tx)
hash := tx.Hash()
// Check whether the tx is replay protected. If we're not in the EIP155 hf
// phase, start ignoring the sender until we do.
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()
continue
}
// 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)
if nonce > tx.Nonce() {
// 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:
// Strange error, discard the transaction and get the next in line (note, the
// 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()
}
if tokenFeeUsed {
fee := common.GetGasFee(env.header.Number.Uint64(), gas)
balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee)
balanceUpdated[*tx.To()] = balanceFee[*tx.To()]
balanceFee[*to] = new(big.Int).Sub(balanceFee[*to], fee)
balanceUpdated[*to] = balanceFee[*to]
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)
}
}