Skip to content

Commit

Permalink
Remove ExternalTransactions from QuaiTransactions and disable receipt…
Browse files Browse the repository at this point in the history
…s from Qi ETXs
  • Loading branch information
jdowning100 committed Jun 14, 2024
1 parent e7400a5 commit c3f41fa
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 35 deletions.
4 changes: 2 additions & 2 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ func NewEVMBlockContext(header *types.WorkObject, chain ChainContext, author *co
}
}

// Prime terminus determines which location is eligible to except the etx
// Prime terminus determines which location is eligible to accept the etx
primeTerminus := header.PrimeTerminus()
primeTerminusHeader := chain.GetHeaderByHash(primeTerminus)
if primeTerminusHeader == nil {
log.Global.Error("Prime terminus header not found", "headerHash", header.Hash(), "primeTerminus", primeTerminus)
log.Global.Error("Prime terminus header not found ", " headerHash ", header.Hash(), " primeTerminus ", primeTerminus)
return vm.BlockContext{}, ErrSubNotSyncedToDom
}
etxEligibleSlices := primeTerminusHeader.EtxEligibleSlices()
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *para
}).Error("Missing body but have receipt")
return nil
}
if err := receipts.DeriveFields(config, hash, number, body.QuaiTransactionsWithoutCoinbase()); err != nil {
if err := receipts.DeriveFields(config, hash, number, body.TransactionsWithReceipts()); err != nil {
db.Logger().WithFields(log.Fields{
"hash": hash,
"number": number,
Expand Down
9 changes: 7 additions & 2 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
timePrepare += timePrepareDelta

var receipt *types.Receipt
var addReceipt bool
if tx.Type() == types.ExternalTxType {
startTimeEtx := time.Now()
// ETXs MUST be included in order, so popping the first from the queue must equal the first in the block
Expand Down Expand Up @@ -418,6 +419,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
if err != nil {
return nil, nil, nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
addReceipt = true
totalEtxGas += receipt.GasUsed
timeEtxDelta := time.Since(startTimeEtx)
timeEtx += timeEtxDelta
Expand All @@ -429,6 +431,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
if err != nil {
return nil, nil, nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
addReceipt = true
timeTxDelta := time.Since(startTimeTx)
timeTx += timeTxDelta
} else {
Expand All @@ -439,8 +442,10 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
emittedEtxs = append(emittedEtxs, etx)
}
}
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
if addReceipt {
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
}
i++
}

Expand Down
4 changes: 3 additions & 1 deletion core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,9 @@ func (pool *TxPool) RemoveQiTxs(txs []*common.Hash) {
// Mempool lock must be held.
func (pool *TxPool) removeQiTxsLocked(txs []*types.Transaction) {
for _, tx := range txs {
delete(pool.qiPool, tx.Hash())
if tx.Type() == types.QiTxType {
delete(pool.qiPool, tx.Hash())
}
}
qiTxGauge.Sub(float64(len(txs)))
}
Expand Down
26 changes: 14 additions & 12 deletions core/types/wo.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,26 @@ func (wo *WorkObject) QiTransactionsWithoutCoinbase() []*Transaction {
return qiTxs
}

func (wo *WorkObject) TransactionsWithReceipts() []*Transaction {
txs := make([]*Transaction, 0)
for _, t := range wo.Transactions() {
if t.Type() == QuaiTxType || (t.Type() == ExternalTxType && t.To().IsInQuaiLedgerScope()) {
txs = append(txs, t)
}
}
return txs
}

// QuaiTransactionsWithoutCoinbase returns all Quai EVM transactions in the block, excluding the coinbase transaction.
// This also returns all transactions that have an associated receipt.
func (wo *WorkObject) QuaiTransactionsWithoutCoinbase() []*Transaction {
quaiTxs := make([]*Transaction, 0)
for i, t := range wo.Transactions() {
if i == 0 && IsCoinBaseTx(t, wo.woHeader.parentHash, wo.woHeader.location) || t.Type() == QiTxType || (t.Type() == ExternalTxType && t.To().IsInQiLedgerScope()) {
// ignore the Quai coinbase tx and Quai->Qi to comply with prior functionality as it is not a normal transaction
continue
}
if t.Type() != QiTxType {
if t.Type() == QuaiTxType {
quaiTxs = append(quaiTxs, t)
}
}
Expand All @@ -369,16 +381,6 @@ func (wo *WorkObject) InputsAndOutputsWithoutCoinbase() (uint, uint) {
return uint(inputs), uint(outputs)
}

func (wo *WorkObject) QuaiTransactionsWithFees() []*Transaction {
quaiTxs := make([]*Transaction, 0)
for _, t := range wo.Transactions() {
if t.Type() == QuaiTxType { // QuaiTxType is the only type that gives Quai fees to the miner
quaiTxs = append(quaiTxs, t)
}
}
return quaiTxs
}

func (wo *WorkObject) NumberArray() []*big.Int {
numArray := make([]*big.Int, common.HierarchyDepth)
for i := 0; i < common.HierarchyDepth; i++ {
Expand Down Expand Up @@ -578,7 +580,7 @@ func (wb *WorkObjectBody) QiTransactions() []*Transaction {
func (wb *WorkObjectBody) QuaiTransactions() []*Transaction {
quaiTxs := make([]*Transaction, 0)
for _, t := range wb.Transactions() {
if t.Type() != QiTxType {
if t.Type() == QuaiTxType {
quaiTxs = append(quaiTxs, t)
}
}
Expand Down
34 changes: 19 additions & 15 deletions core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ func (w *worker) commitUncle(env *environment, uncle *types.WorkObjectHeader) er
return nil
}

func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, tx *types.Transaction) ([]*types.Log, error) {
func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, tx *types.Transaction) ([]*types.Log, bool, error) {
if tx != nil {
if tx.Type() == types.ExternalTxType && tx.To().IsInQiLedgerScope() {
gasUsed := env.wo.GasUsed()
Expand All @@ -740,7 +740,7 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t
lock := new(big.Int).Add(env.wo.Number(w.hc.NodeCtx()), big.NewInt(params.ConversionLockPeriod))
primeTerminus := w.hc.GetPrimeTerminus(env.wo)
if primeTerminus == nil {
return nil, errors.New("prime terminus not found")
return nil, false, errors.New("prime terminus not found")
}
value := misc.QuaiToQi(primeTerminus, tx.Value())
denominations := misc.FindMinDenominations(value)
Expand All @@ -759,29 +759,29 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t
}
txGas -= params.CallValueTransferGas
if err := env.gasPool.SubGas(params.CallValueTransferGas); err != nil {
return nil, err
return nil, false, err
}
gasUsed += params.CallValueTransferGas
// the ETX hash is guaranteed to be unique
if err := env.state.CreateUTXO(tx.Hash(), outputIndex, types.NewUtxoEntry(types.NewTxOut(uint8(denomination), tx.To().Bytes(), lock))); err != nil {
return nil, err
return nil, false, err
}
outputIndex++
}
}
} else {
// This Qi ETX should cost more gas
if err := env.gasPool.SubGas(params.CallValueTransferGas); err != nil {
return nil, err
return nil, false, err
}
if err := env.state.CreateUTXO(tx.OriginatingTxHash(), tx.ETXIndex(), types.NewUtxoEntry(types.NewTxOut(uint8(tx.Value().Uint64()), tx.To().Bytes(), big.NewInt(0)))); err != nil {
return nil, err
return nil, false, err
}
gasUsed += params.CallValueTransferGas
}
env.wo.Header().SetGasUsed(gasUsed)
env.txs = append(env.txs, tx)
return []*types.Log{}, nil // need to make sure this does not modify receipt hash
return []*types.Log{}, false, nil
}
snap := env.state.Snapshot()
// retrieve the gas used int and pass in the reference to the ApplyTransaction
Expand All @@ -795,7 +795,7 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t
"gasUsed": gasUsed,
}).Debug("Error playing transaction in worker")
env.state.RevertToSnapshot(snap)
return nil, err
return nil, false, err
}
if receipt.Status == types.ReceiptStatusSuccessful {
env.etxs = append(env.etxs, receipt.Etxs...)
Expand All @@ -806,9 +806,9 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t
env.wo.Header().SetGasUsed(gasUsed)
env.txs = append(env.txs, tx)
env.receipts = append(env.receipts, receipt)
return receipt.Logs, nil
return receipt.Logs, true, nil
}
return nil, errors.New("error finding transaction")
return nil, false, errors.New("error finding transaction")
}

func (w *worker) commitTransactions(env *environment, parent *types.WorkObject, txs *types.TransactionsByPriceAndNonce, interrupt *int32) bool {
Expand Down Expand Up @@ -852,10 +852,12 @@ func (w *worker) commitTransactions(env *environment, parent *types.WorkObject,
break
}
env.state.Prepare(etx.Hash(), env.tcount)
logs, err := w.commitTransaction(env, parent, etx)
if err == nil {
logs, receipt, err := w.commitTransaction(env, parent, etx)
if err == nil && receipt {
coalescedLogs = append(coalescedLogs, logs...)
env.tcount++
} else if err == nil && !receipt {
env.tcount++
}
oldestIndex.Add(oldestIndex, big.NewInt(1))
}
Expand Down Expand Up @@ -925,7 +927,7 @@ func (w *worker) commitTransactions(env *environment, parent *types.WorkObject,
// Start executing the transaction
env.state.Prepare(tx.Hash(), env.tcount)

logs, err := w.commitTransaction(env, parent, tx)
logs, receipt, err := w.commitTransaction(env, parent, tx)
switch {
case errors.Is(err, types.ErrGasLimitReached):
// Pop the current out-of-gas transaction without shifting in the next from the account
Expand Down Expand Up @@ -955,7 +957,9 @@ func (w *worker) commitTransactions(env *environment, parent *types.WorkObject,

case errors.Is(err, nil):
// Everything ok, collect the logs and shift in the next transaction from the same account
coalescedLogs = append(coalescedLogs, logs...)
if receipt {
coalescedLogs = append(coalescedLogs, logs...)
}
env.tcount++
txs.PopNoSort()

Expand Down Expand Up @@ -1423,7 +1427,7 @@ func copyReceipts(receipts []*types.Receipt) []*types.Receipt {
// totalFees computes total consumed miner fees in ETH. Block transactions and receipts have to have the same order.
func totalFees(block *types.WorkObject, receipts []*types.Receipt) *big.Float {
feesWei := new(big.Int)
for i, tx := range block.QuaiTransactionsWithoutCoinbase() {
for i, tx := range block.TransactionsWithReceipts() {
minerFee, _ := tx.EffectiveGasTip(block.BaseFee())
feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), minerFee))
}
Expand Down
4 changes: 2 additions & 2 deletions quai/gasprice/feehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
return
}

sorter := make(sortGasAndReward, len(bf.block.QuaiTransactionsWithoutCoinbase()))
for i, tx := range bf.block.QuaiTransactionsWithoutCoinbase() {
sorter := make(sortGasAndReward, len(bf.block.TransactionsWithReceipts()))
for i, tx := range bf.block.TransactionsWithReceipts() {
reward, _ := tx.EffectiveGasTip(bf.block.BaseFee())
sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward}
}
Expand Down

0 comments on commit c3f41fa

Please sign in to comment.