diff --git a/core/state_processor.go b/core/state_processor.go index 8a9f5ea179..05e4f35416 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -1255,7 +1255,7 @@ func (p *StateProcessor) Apply(batch ethdb.Batch, block *types.WorkObject) ([]*t // and uses the input parameters for its environment. It returns the receipt // for the transaction, gas used and an error if the transaction failed, // indicating the block was invalid. -func ApplyTransaction(config *params.ChainConfig, parent *types.WorkObject, bc ChainContext, author *common.Address, gp *types.GasPool, statedb *state.StateDB, header *types.WorkObject, tx *types.Transaction, usedGas *uint64, cfg vm.Config, etxRLimit, etxPLimit *int, logger *log.Logger) (*types.Receipt, *big.Int, error) { +func ApplyTransaction(config *params.ChainConfig, parent *types.WorkObject, parentOrder int, bc ChainContext, author *common.Address, gp *types.GasPool, statedb *state.StateDB, header *types.WorkObject, tx *types.Transaction, usedGas *uint64, cfg vm.Config, etxRLimit, etxPLimit *int, logger *log.Logger) (*types.Receipt, *big.Int, error) { nodeCtx := config.Location.Context() msg, err := tx.AsMessage(types.MakeSigner(config, header.Number(nodeCtx)), header.BaseFee()) if err != nil { @@ -1263,9 +1263,14 @@ func ApplyTransaction(config *params.ChainConfig, parent *types.WorkObject, bc C } if tx.Type() == types.ExternalTxType && tx.ETXSender().Location().Equal(*tx.To().Location()) { // Qi->Quai Conversion msg.SetLock(new(big.Int).Add(header.Number(nodeCtx), big.NewInt(params.ConversionLockPeriod))) - primeTerminus := bc.GetHeaderByHash(header.PrimeTerminus()) - if primeTerminus == nil { - return nil, nil, fmt.Errorf("could not find prime terminus header %032x", header.PrimeTerminus()) + var primeTerminus *types.WorkObject + if parentOrder == common.PRIME_CTX { + primeTerminus = parent + } else { + primeTerminus = bc.GetHeaderByHash(header.PrimeTerminus()) + if primeTerminus == nil { + return nil, nil, fmt.Errorf("could not find prime terminus header %032x", header.PrimeTerminus()) + } } // Convert Qi to Quai msg.SetValue(misc.QiToQuai(primeTerminus.WorkObjectHeader(), tx.Value())) diff --git a/core/worker.go b/core/worker.go index 67e9d599bb..862a0b652b 100644 --- a/core/worker.go +++ b/core/worker.go @@ -64,15 +64,15 @@ const ( type environment struct { signer types.Signer - state *state.StateDB // apply state changes here - ancestors mapset.Set // ancestor set (used for checking uncle parent validity) - family mapset.Set // family set (used for checking uncle invalidity) - tcount int // tx count in cycle - gasPool *types.GasPool // available gas used to pack transactions - coinbase common.Address - etxRLimit int // Remaining number of cross-region ETXs that can be included - etxPLimit int // Remaining number of cross-prime ETXs that can be included - + state *state.StateDB // apply state changes here + ancestors mapset.Set // ancestor set (used for checking uncle parent validity) + family mapset.Set // family set (used for checking uncle invalidity) + tcount int // tx count in cycle + gasPool *types.GasPool // available gas used to pack transactions + coinbase common.Address + etxRLimit int // Remaining number of cross-region ETXs that can be included + etxPLimit int // Remaining number of cross-prime ETXs that can be included + parentOrder *int wo *types.WorkObject txs []*types.Transaction etxs []*types.Transaction @@ -594,13 +594,11 @@ func (w *worker) GeneratePendingHeader(block *types.WorkObject, fill bool) (*typ }).Info("Filled and sorted pending transactions") } - // If the current block is a prime block, its a prime terminus - _, order, err := w.CalcOrder(block) - if err != nil { - return nil, err + if work.parentOrder == nil { + return nil, fmt.Errorf("parent order not set") } var primeTerminus *types.WorkObject - if order == common.PRIME_CTX { + if *work.parentOrder == common.PRIME_CTX { primeTerminus = block } else { // convert the Quai reward into Qi and add it to the utxoFees @@ -815,12 +813,11 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t if tx.ETXSender().Location().Equal(*tx.To().Location()) { // Quai->Qi conversion txGas := tx.Gas() lock := new(big.Int).Add(env.wo.Number(w.hc.NodeCtx()), big.NewInt(params.ConversionLockPeriod)) - _, parentOrder, err := w.CalcOrder(parent) - if err != nil { - return nil, false, err + if env.parentOrder == nil { + return nil, false, errors.New("parent order not set") } var primeTerminus *types.WorkObject - if parentOrder == common.PRIME_CTX { + if *env.parentOrder == common.PRIME_CTX { primeTerminus = parent } else { primeTerminus = w.hc.GetPrimeTerminus(env.wo) @@ -881,7 +878,7 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t snap := env.state.Snapshot() // retrieve the gas used int and pass in the reference to the ApplyTransaction gasUsed := env.wo.GasUsed() - receipt, quaiFees, err := ApplyTransaction(w.chainConfig, parent, w.hc, &env.coinbase, env.gasPool, env.state, env.wo, tx, &gasUsed, *w.hc.bc.processor.GetVMConfig(), &env.etxRLimit, &env.etxPLimit, w.logger) + receipt, quaiFees, err := ApplyTransaction(w.chainConfig, parent, *env.parentOrder, w.hc, &env.coinbase, env.gasPool, env.state, env.wo, tx, &gasUsed, *w.hc.bc.processor.GetVMConfig(), &env.etxRLimit, &env.etxPLimit, w.logger) if err != nil { w.logger.WithFields(log.Fields{ "err": err, @@ -1307,6 +1304,7 @@ func (w *worker) prepareWork(genParams *generateParams, wo *types.WorkObject) (* w.logger.WithField("err", err).Error("Failed to create sealing context") return nil, err } + env.parentOrder = &order // Accumulate the uncles for the sealing work. commitUncles := func(wos *lru.Cache[common.Hash, types.WorkObjectHeader]) { var uncles []*types.WorkObjectHeader @@ -1672,12 +1670,11 @@ func (w *worker) processQiTx(tx *types.Transaction, env *environment, parent *ty return err } - _, parentOrder, err := w.CalcOrder(parent) - if err != nil { - return err + if env.parentOrder == nil { + return errors.New("parent order not set") } var primeTerminus *types.WorkObject - if parentOrder == common.PRIME_CTX { + if *env.parentOrder == common.PRIME_CTX { primeTerminus = parent } else { primeTerminus = w.hc.GetPrimeTerminus(env.wo)