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 41bb4e0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 36 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/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func DefaultLocalGenesisBlock(consensusEngine string) *Genesis {
Nonce: 66,
ExtraData: hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fb"),
GasLimit: 5000000,
Difficulty: big.NewInt(5000000),
Difficulty: big.NewInt(500000),
}
}
return &Genesis{
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
30 changes: 18 additions & 12 deletions core/types/wo.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,30 @@ func (wo *WorkObject) QiTransactionsWithoutCoinbase() []*Transaction {
return qiTxs
}

func (wo *WorkObject) TransactionsWithReceipts() []*Transaction {
txs := make([]*Transaction, 0)
for i, t := range wo.Transactions() {
if i == 0 && IsCoinBaseTx(t, wo.woHeader.parentHash, wo.woHeader.location) {
// ignore the Quai coinbase tx
continue
}
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 +385,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 +584,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
2 changes: 1 addition & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
Blake3PowColosseumGenesisHash = common.HexToHash("0x0ca6ff4426eab568de6773fb21d623d3fe6f2f0d2572c0b0b978384373e0895d")
Blake3PowGardenGenesisHash = common.HexToHash("0x02874a9e708fd75a6ed73bf1d6dbc275a87b8914c3f57da0bbdc959c381d0f67")
Blake3PowOrchardGenesisHash = common.HexToHash("0x43af9e6e91fbf408b9c6fd423ed34e5ad0bd2154737cefc204d145ffc660fc9c")
Blake3PowLocalGenesisHash = common.HexToHash("0xecbfee9f6bce9583520d16795bc9850783a8a86563c0ce9a5082d86f497b728f")
Blake3PowLocalGenesisHash = common.HexToHash("0xcc34daeac18fc7ad2f7fae0aecad5a34a0def7629776f6fd74a18463d9661588")
Blake3PowLighthouseGenesisHash = common.HexToHash("0x43af9e6e91fbf408b9c6fd423ed34e5ad0bd2154737cefc204d145ffc660fc9c")
)

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 41bb4e0

Please sign in to comment.