Skip to content

Commit

Permalink
Enforced 21k min gas for conversion ETXs
Browse files Browse the repository at this point in the history
  • Loading branch information
jdowning100 committed Jul 16, 2024
1 parent 0e663ec commit 5413ddd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 12 additions & 2 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
if err := statedb.CreateUTXO(etx.Hash(), outputIndex, types.NewUtxoEntry(types.NewTxOut(uint8(denomination), tx.To().Bytes(), block.Number(nodeCtx)))); err != nil {
return nil, nil, nil, nil, 0, err
}
p.logger.Debugf("Creating UTXO for coinbase %032x with denomination %d index %d\n", tx.Hash(), denomination, outputIndex)
outputIndex++
}
}
Expand All @@ -418,6 +419,15 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
}
value := misc.QuaiToQi(primeTerminus.WorkObjectHeader(), etx.Value()) // convert Quai to Qi
txGas := etx.Gas()
if txGas < params.TxGas {
continue
}
txGas -= params.TxGas
if err := gp.SubGas(params.TxGas); err != nil {
return nil, nil, nil, nil, 0, err
}
*usedGas += params.TxGas
totalEtxGas += params.TxGas
denominations := misc.FindMinDenominations(value)
outputIndex := uint16(0)
// Iterate over the denominations in descending order
Expand All @@ -441,7 +451,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
if err := statedb.CreateUTXO(etx.Hash(), outputIndex, types.NewUtxoEntry(types.NewTxOut(uint8(denomination), etx.To().Bytes(), lock))); err != nil {
return nil, nil, nil, nil, 0, err
}
log.Global.Infof("Converting Quai to Qi %032x with denomination %d index %d lock %d", tx.Hash(), denomination, outputIndex, lock)
p.logger.Infof("Converting Quai to Qi %032x with denomination %d index %d lock %d\n", tx.Hash(), denomination, outputIndex, lock)
outputIndex++
}
}
Expand Down Expand Up @@ -470,7 +480,7 @@ func (p *StateProcessor) Process(block *types.WorkObject) (types.Receipts, []*ty
// Convert Qi to Quai
msg.SetValue(misc.QiToQuai(primeTerminus.WorkObjectHeader(), etx.Value()))
msg.SetData([]byte{}) // data is not used in conversion
log.Global.Infof("Converting Qi to Quai for ETX %032x with value %d lock %d", tx.Hash(), msg.Value().Uint64(), msg.Lock().Uint64())
p.logger.Infof("Converting Qi to Quai for ETX %032x with value %d lock %d\n", tx.Hash(), msg.Value().Uint64(), msg.Lock().Uint64())
}
prevZeroBal := prepareApplyETX(statedb, msg.Value(), nodeLocation)
receipt, quaiFees, err = applyTransaction(msg, parent, p.config, p.hc, nil, gp, statedb, blockNumber, blockHash, etx, usedGas, vmenv, &etxRLimit, &etxPLimit, p.logger)
Expand Down
18 changes: 17 additions & 1 deletion core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,8 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t
}
if tx.Type() == types.ExternalTxType && tx.To().IsInQiLedgerScope() {
gasUsed := env.wo.GasUsed()
txGas := tx.Gas()
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.engine.CalcOrder(parent)
if err != nil {
Expand All @@ -828,6 +828,15 @@ func (w *worker) commitTransaction(env *environment, parent *types.WorkObject, t
return nil, false, errors.New("prime terminus not found")
}
}
if txGas < params.TxGas {
// No gas, the result is a no-op but the tx is still valid
return nil, false, nil
}
txGas -= params.TxGas
if err := env.gasPool.SubGas(params.TxGas); err != nil {
return nil, false, err
}
gasUsed += params.TxGas
value := misc.QuaiToQi(primeTerminus.WorkObjectHeader(), tx.Value())
denominations := misc.FindMinDenominations(value)
outputIndex := uint16(0)
Expand Down Expand Up @@ -1616,6 +1625,9 @@ func (w *worker) processQiTx(tx *types.Transaction, env *environment, parent *ty
addresses[toAddr.Bytes20()] = struct{}{}

if toAddr.Location().Equal(location) && toAddr.IsInQuaiLedgerScope() { // Qi->Quai conversion
if conversion && !toAddr.Equal(convertAddress) { // All convert outputs must have the same To address for aggregation
return fmt.Errorf("tx %032x emits multiple convert UTXOs with different To addresses", tx.Hash())
}
conversion = true
convertAddress = toAddr
if txOut.Denomination < params.MinQiConversionDenomination {
Expand Down Expand Up @@ -1709,6 +1721,10 @@ func (w *worker) processQiTx(tx *types.Transaction, env *environment, parent *ty
// Limit ETX gas to max ETX gas limit (the rest is burned)
remainingGas = new(big.Int).SetUint64(env.wo.GasLimit() / params.MinimumEtxGasDivisor)
}
if remainingGas.Uint64() < params.TxGas {
// Minimum gas for ETX is TxGas
return fmt.Errorf("tx %032x has insufficient remaining gas for conversion ETX, have %d want %d", tx.Hash(), remainingGas.Uint64(), params.TxGas)
}
ETXPCount++ // conversion is technically a cross-prime ETX
if ETXPCount > env.etxPLimit {
return fmt.Errorf("tx [%v] emits too many cross-prime ETXs for block. emitted: %d, limit: %d", tx.Hash().Hex(), ETXPCount, env.etxPLimit)
Expand Down

0 comments on commit 5413ddd

Please sign in to comment.