From b8e72b4cc73846e32bbaad93b54e7b901c300ccc Mon Sep 17 00:00:00 2001 From: Wang Gerui Date: Sat, 23 Nov 2024 00:56:20 +0800 Subject: [PATCH] feat: Gasless --- cmd/utils/flags.go | 4 ++++ common/gas.go | 13 ++++++++++++- core/tx_pool.go | 4 +++- eth/gasprice/gasprice.go | 3 +++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 9880e8709..4c0444fdb 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1144,6 +1144,10 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { } if ctx.GlobalIsSet(GasPriceFlag.Name) { cfg.GasPrice = GlobalBig(ctx, GasPriceFlag.Name) + if len(cfg.GasPrice.Bits()) == 0 { //IsUint64() && cfg.GasPrice.Uint64() == 0 { + common.Gasless = true + log.Info("Gasless enabled. You can run transactions with zero gas fee.") + } } if ctx.GlobalIsSet(VMEnableDebugFlag.Name) { // TODO(fjl): force-enable this in --dev mode diff --git a/common/gas.go b/common/gas.go index 26783af02..632770e57 100644 --- a/common/gas.go +++ b/common/gas.go @@ -7,7 +7,12 @@ import ( var MinGasPrice50x = big.NewInt(12500000000) var GasPrice50x = big.NewInt(12500000000) +var Gasless = false + func GetGasFee(blockNumber, gas uint64) *big.Int { + if Gasless { + return big.NewInt(0) + } fee := new(big.Int).SetUint64(gas) if blockNumber >= uint64(10) { //temp fix trc21issuer test fail fee = fee.Mul(fee, GasPrice50x) @@ -16,6 +21,9 @@ func GetGasFee(blockNumber, gas uint64) *big.Int { } func GetGasPrice(number *big.Int) *big.Int { + if Gasless { + return big.NewInt(0) + } if number == nil { return new(big.Int).Set(TRC21GasPrice) } @@ -23,8 +31,11 @@ func GetGasPrice(number *big.Int) *big.Int { } func GetMinGasPrice(number *big.Int) *big.Int { + if Gasless { + return big.NewInt(0) + } if number == nil { return new(big.Int).Set(MinGasPrice) } return new(big.Int).Set(MinGasPrice50x) -} \ No newline at end of file +} diff --git a/core/tx_pool.go b/core/tx_pool.go index 16656f60b..f03dcadf1 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -664,7 +664,9 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error { // Check zero gas price. if tx.GasPrice().Cmp(new(big.Int).SetInt64(0)) == 0 { - return ErrZeroGasPrice + if !common.Gasless { + return ErrZeroGasPrice + } } // under min gas price diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go index 784f39145..96ccc845f 100644 --- a/eth/gasprice/gasprice.go +++ b/eth/gasprice/gasprice.go @@ -75,6 +75,9 @@ func NewOracle(backend ethapi.Backend, params Config) *Oracle { // SuggestPrice returns the recommended gas price. func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) { + if common.Gasless { + return big.NewInt(0), nil + } gpo.cacheLock.RLock() lastHead := gpo.lastHead lastPrice := gpo.lastPrice