From a199669627da144e5ff6012dda71f51e41a7d5d2 Mon Sep 17 00:00:00 2001 From: syntrust Date: Tue, 28 May 2024 17:29:09 +0800 Subject: [PATCH 1/4] fix gas fee cap --- ethstorage/miner/l1_mining_api.go | 135 +++++++++++++++++------------- 1 file changed, 78 insertions(+), 57 deletions(-) diff --git a/ethstorage/miner/l1_mining_api.go b/ethstorage/miner/l1_mining_api.go index b43c9ba7..a88347d2 100644 --- a/ethstorage/miner/l1_mining_api.go +++ b/ethstorage/miner/l1_mining_api.go @@ -85,70 +85,21 @@ func (m *l1MiningAPI) GetDataHashes(ctx context.Context, contract common.Address func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Address, rst result, cfg Config) (common.Hash, error) { m.lg.Debug("Submit mined result", "shard", rst.startShardId, "block", rst.blockNumber, "nonce", rst.nonce) - blockHeader, err := m.HeaderByNumber(ctx, rst.blockNumber) + calldata, err := m.composeCalldata(ctx, rst) if err != nil { - m.lg.Error("Failed to get block header", "error", err) + m.lg.Error("Failed to compose calldata", "error", err) return common.Hash{}, err } - headerRlp, err := rlp.EncodeToBytes(blockHeader) + tip, gasFeeCap, predictedGasPrice, err := m.suggestGasPrices(ctx, cfg) if err != nil { - m.lg.Error("Failed to encode block header", "error", err) + m.lg.Error("Failed to suggest gas prices", "error", err) return common.Hash{}, err } - uint256Type, _ := abi.NewType("uint256", "", nil) - uint256Array, _ := abi.NewType("uint256[]", "", nil) - addrType, _ := abi.NewType("address", "", nil) - bytes32Array, _ := abi.NewType("bytes32[]", "", nil) - bytesArray, _ := abi.NewType("bytes[]", "", nil) - bytesType, _ := abi.NewType("bytes", "", nil) - dataField, _ := abi.Arguments{ - {Type: uint256Type}, - {Type: uint256Type}, - {Type: addrType}, - {Type: uint256Type}, - {Type: bytes32Array}, - {Type: uint256Array}, - {Type: bytesType}, - {Type: bytesArray}, - {Type: bytesArray}, - }.Pack( - rst.blockNumber, - new(big.Int).SetUint64(rst.startShardId), - rst.miner, - new(big.Int).SetUint64(rst.nonce), - rst.encodedData, - rst.masks, - headerRlp, - rst.inclusiveProofs, - rst.decodeProof, - ) - calldata := append(mineSig[0:4], dataField...) - - gasPrice := cfg.GasPrice - if gasPrice == nil || gasPrice.Cmp(common.Big0) == 0 { - suggested, err := m.SuggestGasPrice(ctx) - if err != nil { - m.lg.Error("Query gas price failed", "error", err.Error()) - return common.Hash{}, err - } - gasPrice = suggested - m.lg.Info("Query gas price done", "gasPrice", gasPrice) - } - tip := cfg.PriorityGasPrice - if tip == nil || tip.Cmp(common.Big0) == 0 { - suggested, err := m.SuggestGasTipCap(ctx) - if err != nil { - m.lg.Error("Query gas tip cap failed", "error", err.Error()) - suggested = common.Big0 - } - tip = suggested - m.lg.Info("Query gas tip cap done", "gasTipGap", tip) - } estimatedGas, err := m.EstimateGas(ctx, ethereum.CallMsg{ From: cfg.SignerAddr, To: &contract, GasTipCap: tip, - GasFeeCap: gasPrice, + GasFeeCap: gasFeeCap, Value: common.Big0, Data: calldata, }) @@ -157,7 +108,7 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add return common.Hash{}, fmt.Errorf("failed to estimate gas: %w", err) } m.lg.Info("Estimated gas done", "gas", estimatedGas) - cost := new(big.Int).Mul(new(big.Int).SetUint64(estimatedGas), gasPrice) + cost := new(big.Int).Mul(new(big.Int).SetUint64(estimatedGas), predictedGasPrice) reward, err := m.GetMiningReward(rst.startShardId, rst.blockNumber.Int64()) if err != nil { m.lg.Error("Query mining reward failed", "error", err.Error()) @@ -185,7 +136,7 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add ChainID: m.NetworkID, Nonce: nonce, GasTipCap: tip, - GasFeeCap: gasPrice, + GasFeeCap: gasFeeCap, Gas: gas, To: &contract, Value: common.Big0, @@ -198,10 +149,80 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add } err = m.SendTransaction(ctx, signedTx) if err != nil { - m.lg.Error("Send tx failed", "txNonce", nonce, "gasPrice", gasPrice, "error", err) + m.lg.Error("Send tx failed", "txNonce", nonce, "gasFeeCap", gasFeeCap, "error", err) return common.Hash{}, err } m.lg.Info("Submit mined result done", "shard", rst.startShardId, "block", rst.blockNumber, "nonce", rst.nonce, "txSigner", cfg.SignerAddr.Hex(), "hash", signedTx.Hash().Hex()) return signedTx.Hash(), nil } + +func (m *l1MiningAPI) composeCalldata(ctx context.Context, rst result) ([]byte, error) { + blockHeader, err := m.HeaderByNumber(ctx, rst.blockNumber) + if err != nil { + m.lg.Error("Failed to get block header", "error", err) + return nil, err + } + headerRlp, err := rlp.EncodeToBytes(blockHeader) + if err != nil { + m.lg.Error("Failed to encode block header", "error", err) + return nil, err + } + uint256Type, _ := abi.NewType("uint256", "", nil) + uint256Array, _ := abi.NewType("uint256[]", "", nil) + addrType, _ := abi.NewType("address", "", nil) + bytes32Array, _ := abi.NewType("bytes32[]", "", nil) + bytesArray, _ := abi.NewType("bytes[]", "", nil) + bytesType, _ := abi.NewType("bytes", "", nil) + dataField, _ := abi.Arguments{ + {Type: uint256Type}, + {Type: uint256Type}, + {Type: addrType}, + {Type: uint256Type}, + {Type: bytes32Array}, + {Type: uint256Array}, + {Type: bytesType}, + {Type: bytesArray}, + {Type: bytesArray}, + }.Pack( + rst.blockNumber, + new(big.Int).SetUint64(rst.startShardId), + rst.miner, + new(big.Int).SetUint64(rst.nonce), + rst.encodedData, + rst.masks, + headerRlp, + rst.inclusiveProofs, + rst.decodeProof, + ) + calldata := append(mineSig[0:4], dataField...) + return calldata, nil +} + +func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.Int, *big.Int, *big.Int, error) { + tip := cfg.PriorityGasPrice + if tip == nil || tip.Cmp(common.Big0) == 0 { + suggested, err := m.SuggestGasTipCap(ctx) + if err != nil { + m.lg.Error("Query gas tip cap failed", "error", err.Error()) + suggested = common.Big0 + } + tip = suggested + m.lg.Info("Query gas tip cap done", "gasTipGap", tip) + } + gasFeeCap := cfg.GasPrice + predictedGasPrice := gasFeeCap + if gasFeeCap == nil || gasFeeCap.Cmp(common.Big0) == 0 { + blockHeader, err := m.HeaderByNumber(ctx, nil) + if err != nil { + m.lg.Error("Failed to get block header", "error", err) + return nil, nil, nil, err + } + // Doubling the base fee to ensure the transaction will remain marketable for six consecutive 100% full blocks. + gasFeeCap = new(big.Int).Add(tip, new(big.Int).Mul(blockHeader.BaseFee, common.Big2)) + // Use `tip + base fee` as predicted gas price that will be used to evaluate the mining profit + predictedGasPrice = new(big.Int).Add(tip, blockHeader.BaseFee) + m.lg.Info("Compute gas fee cap done", "gasFeeCap", gasFeeCap, "predictedGasPrice", predictedGasPrice) + } + return tip, gasFeeCap, predictedGasPrice, nil +} From 44ae31307d8664097dc5d66e471740c5c51e4a27 Mon Sep 17 00:00:00 2001 From: syntrust Date: Thu, 12 Sep 2024 11:29:36 +0800 Subject: [PATCH 2/4] update estimation --- ethstorage/miner/l1_mining_api.go | 76 +++++++++++++++++++---------- ethstorage/miner/worker.go | 11 +++-- integration_tests/node_mine_test.go | 2 +- 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/ethstorage/miner/l1_mining_api.go b/ethstorage/miner/l1_mining_api.go index b14059a2..53f667f1 100644 --- a/ethstorage/miner/l1_mining_api.go +++ b/ethstorage/miner/l1_mining_api.go @@ -90,16 +90,17 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add m.lg.Error("Failed to compose calldata", "error", err) return common.Hash{}, err } - tip, gasFeeCap, predictedGasPrice, err := m.suggestGasPrices(ctx, cfg) + tip, baseFee, err := m.suggestGasPrices(ctx, cfg) if err != nil { m.lg.Error("Failed to suggest gas prices", "error", err) return common.Hash{}, err } + m.lg.Info("Suggested gas prices", "gasTipCap", tip, "baseFee", baseFee) estimatedGas, err := m.EstimateGas(ctx, ethereum.CallMsg{ From: cfg.SignerAddr, To: &contract, GasTipCap: tip, - GasFeeCap: gasFeeCap, + GasFeeCap: new(big.Int).Add(baseFee, tip), Value: common.Big0, Data: calldata, }) @@ -108,22 +109,55 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add return common.Hash{}, fmt.Errorf("failed to estimate gas: %w", err) } m.lg.Info("Estimated gas done", "gas", estimatedGas) - cost := new(big.Int).Mul(new(big.Int).SetUint64(estimatedGas), predictedGasPrice) reward, err := m.GetMiningReward(rst.startShardId, rst.blockNumber.Int64()) if err != nil { m.lg.Error("Query mining reward failed", "error", err.Error()) return common.Hash{}, err } - profit := new(big.Int).Sub(reward, cost) - m.lg.Info("Estimated reward and cost (in ether)", "reward", weiToEther(reward), "cost", weiToEther(cost), "profit", weiToEther(profit)) - if profit.Cmp(cfg.MinimumProfit) == -1 { - m.lg.Warn("Will drop the tx: the profit will not meet expectation", - "profitEstimated", weiToEther(profit), - "minimumProfit", weiToEther(cfg.MinimumProfit), - ) + isProfitable := func(gasPrice *big.Int) bool { + cost := new(big.Int).Mul(new(big.Int).SetUint64(estimatedGas), gasPrice) + profit := new(big.Int).Sub(reward, cost) + m.lg.Debug("Estimating profit", "gasPrice", gasPrice) + m.lg.Debug("Estimated reward and cost (in ether)", "reward", fmtEth(reward), "cost", fmtEth(cost)) + m.lg.Debug("Estimated profit (in ether)", "profit", fmtEth(profit), "minimumProfit", fmtEth(cfg.MinimumProfit)) + return profit.Cmp(cfg.MinimumProfit) == 1 + } + + var useConfig bool + gasFeeCap := cfg.GasPrice + if gasFeeCap == nil || gasFeeCap.Cmp(common.Big0) == 0 { + gasFeeCap = new(big.Int).Add(baseFee, tip) + } else { + useConfig = true + m.lg.Info("Using configured gas price as gasFeeCap", "gasFeeCap", gasFeeCap) + } + if !isProfitable(gasFeeCap) { + m.lg.Warn("Will drop the tx: the profit will not meet expectation", "minimumProfit", fmtEth(cfg.MinimumProfit)) return common.Hash{}, errDropped } + if !useConfig { + // Max fee cap doubles the base fee to ensure the transaction will remain marketable for six consecutive 100% full blocks. + maxGasFeeCap := new(big.Int).Add(tip, new(big.Int).Mul(baseFee, common.Big2)) + baseFeeVar := baseFee + for { + // increase baseFee by 12.5% until maxGasFeeCap + baseFeeVar = new(big.Int).Div(new(big.Int).Mul(baseFeeVar, big.NewInt(9)), big.NewInt(8)) + gasFeeCapNext := new(big.Int).Add(tip, baseFeeVar) + m.lg.Debug("Increasing gasFeeCap", "gasFeeCap", gasFeeCapNext) + if gasFeeCapNext.Cmp(maxGasFeeCap) == 1 { + m.lg.Debug("Dropped the gasFeeCap that exceeds the maximum", "gasFeeCap", gasFeeCapNext, "maxGasFeeCap", maxGasFeeCap) + break + } + if !isProfitable(gasFeeCapNext) { + m.lg.Debug("Dropped the gasFeeCap that will not meet the minimum profit", "gasFeeCap", gasFeeCapNext) + break + } + gasFeeCap = gasFeeCapNext + m.lg.Debug("GasFeeCap increased", "gasFeeCap", gasFeeCap) + } + m.lg.Info("Using suggested gasFeeCap", "gasFeeCap", gasFeeCap) + } sign := cfg.SignerFnFactory(m.NetworkID) nonce, err := m.NonceAt(ctx, cfg.SignerAddr, big.NewInt(rpc.LatestBlockNumber.Int64())) if err != nil { @@ -216,7 +250,7 @@ func (m *l1MiningAPI) composeCalldata(ctx context.Context, rst result) ([]byte, return calldata, nil } -func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.Int, *big.Int, *big.Int, error) { +func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.Int, *big.Int, error) { tip := cfg.PriorityGasPrice if tip == nil || tip.Cmp(common.Big0) == 0 { suggested, err := m.SuggestGasTipCap(ctx) @@ -227,19 +261,11 @@ func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.In tip = suggested m.lg.Info("Query gas tip cap done", "gasTipGap", tip) } - gasFeeCap := cfg.GasPrice - predictedGasPrice := gasFeeCap - if gasFeeCap == nil || gasFeeCap.Cmp(common.Big0) == 0 { - blockHeader, err := m.HeaderByNumber(ctx, nil) - if err != nil { - m.lg.Error("Failed to get block header", "error", err) - return nil, nil, nil, err - } - // Doubling the base fee to ensure the transaction will remain marketable for six consecutive 100% full blocks. - gasFeeCap = new(big.Int).Add(tip, new(big.Int).Mul(blockHeader.BaseFee, common.Big2)) - // Use `tip + base fee` as predicted gas price that will be used to evaluate the mining profit - predictedGasPrice = new(big.Int).Add(tip, blockHeader.BaseFee) - m.lg.Info("Compute gas fee cap done", "gasFeeCap", gasFeeCap, "predictedGasPrice", predictedGasPrice) + blockHeader, err := m.HeaderByNumber(ctx, nil) + if err != nil { + m.lg.Error("Failed to get block header", "error", err) + return nil, nil, err } - return tip, gasFeeCap, predictedGasPrice, nil + baseFee := blockHeader.BaseFee + return tip, baseFee, nil } diff --git a/ethstorage/miner/worker.go b/ethstorage/miner/worker.go index b2c239f7..55e55b6f 100644 --- a/ethstorage/miner/worker.go +++ b/ethstorage/miner/worker.go @@ -487,9 +487,9 @@ func (w *worker) checkTxStatus(txHash common.Hash, miner common.Address) { } if reward != nil { log.Info("Mining transaction accounting (in ether)", - "reward", weiToEther(reward), - "cost", weiToEther(cost), - "profit", weiToEther(new(big.Int).Sub(reward, cost)), + "reward", fmtEth(reward), + "cost", fmtEth(cost), + "profit", fmtEth(new(big.Int).Sub(reward, cost)), ) } } else if receipt.Status == 0 { @@ -511,6 +511,11 @@ func weiToEther(wei *big.Int) *big.Float { return f.Quo(fWei.SetInt(wei), big.NewFloat(params.Ether)) } +func fmtEth(wei *big.Int) string { + f := weiToEther(wei) + return fmt.Sprintf("%.9f", f) +} + // mineTask actually executes a mining task func (w *worker) mineTask(t *taskItem) (bool, error) { startTime := time.Now() diff --git a/integration_tests/node_mine_test.go b/integration_tests/node_mine_test.go index 23c60a5c..cab2dd4e 100644 --- a/integration_tests/node_mine_test.go +++ b/integration_tests/node_mine_test.go @@ -432,7 +432,7 @@ func initMiningConfig(t *testing.T, client *eth.PollingClient) *miner.Config { miningConfig.ZKProverMode = 2 miningConfig.ZKProverImpl = 2 miningConfig.ThreadsPerShard = 2 - miningConfig.MinimumProfit = new(big.Int).SetInt64(-1e18) + miningConfig.MinimumProfit = new(big.Int).SetInt64(-500000000000) return miningConfig } From b47fdada6f4039529b2c0819feff2fa627a338b8 Mon Sep 17 00:00:00 2001 From: syntrust Date: Thu, 26 Sep 2024 15:38:40 +0800 Subject: [PATCH 3/4] fix comment --- ethstorage/miner/l1_mining_api.go | 52 ++++++++++++++++--------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/ethstorage/miner/l1_mining_api.go b/ethstorage/miner/l1_mining_api.go index 53f667f1..32c2cfd2 100644 --- a/ethstorage/miner/l1_mining_api.go +++ b/ethstorage/miner/l1_mining_api.go @@ -90,17 +90,17 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add m.lg.Error("Failed to compose calldata", "error", err) return common.Hash{}, err } - tip, baseFee, err := m.suggestGasPrices(ctx, cfg) + + baseFee, tip, gasFeeCap, useConfig, err := m.suggestGasPrices(ctx, cfg) if err != nil { m.lg.Error("Failed to suggest gas prices", "error", err) return common.Hash{}, err } - m.lg.Info("Suggested gas prices", "gasTipCap", tip, "baseFee", baseFee) estimatedGas, err := m.EstimateGas(ctx, ethereum.CallMsg{ From: cfg.SignerAddr, To: &contract, GasTipCap: tip, - GasFeeCap: new(big.Int).Add(baseFee, tip), + GasFeeCap: gasFeeCap, Value: common.Big0, Data: calldata, }) @@ -109,6 +109,7 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add return common.Hash{}, fmt.Errorf("failed to estimate gas: %w", err) } m.lg.Info("Estimated gas done", "gas", estimatedGas) + reward, err := m.GetMiningReward(rst.startShardId, rst.blockNumber.Int64()) if err != nil { m.lg.Error("Query mining reward failed", "error", err.Error()) @@ -123,14 +124,6 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add return profit.Cmp(cfg.MinimumProfit) == 1 } - var useConfig bool - gasFeeCap := cfg.GasPrice - if gasFeeCap == nil || gasFeeCap.Cmp(common.Big0) == 0 { - gasFeeCap = new(big.Int).Add(baseFee, tip) - } else { - useConfig = true - m.lg.Info("Using configured gas price as gasFeeCap", "gasFeeCap", gasFeeCap) - } if !isProfitable(gasFeeCap) { m.lg.Warn("Will drop the tx: the profit will not meet expectation", "minimumProfit", fmtEth(cfg.MinimumProfit)) return common.Hash{}, errDropped @@ -250,22 +243,31 @@ func (m *l1MiningAPI) composeCalldata(ctx context.Context, rst result) ([]byte, return calldata, nil } -func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.Int, *big.Int, error) { +func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.Int, *big.Int, *big.Int, bool, error) { + var baseFee *big.Int + gasFeeCap := cfg.GasPrice tip := cfg.PriorityGasPrice - if tip == nil || tip.Cmp(common.Big0) == 0 { - suggested, err := m.SuggestGasTipCap(ctx) + useConfig := true + if gasFeeCap == nil || gasFeeCap.Cmp(common.Big0) == 0 { + useConfig = false + blockHeader, err := m.HeaderByNumber(ctx, nil) if err != nil { - m.lg.Error("Query gas tip cap failed", "error", err.Error()) - suggested = common.Big0 + m.lg.Error("Failed to get block header", "error", err) + return nil, nil, nil, false, err } - tip = suggested - m.lg.Info("Query gas tip cap done", "gasTipGap", tip) - } - blockHeader, err := m.HeaderByNumber(ctx, nil) - if err != nil { - m.lg.Error("Failed to get block header", "error", err) - return nil, nil, err + baseFee = blockHeader.BaseFee + m.lg.Info("Query baseFee done", "baseFee", baseFee) + if tip == nil || tip.Cmp(common.Big0) == 0 { + suggested, err := m.SuggestGasTipCap(ctx) + if err != nil { + m.lg.Error("Query gas tip cap failed", "error", err.Error()) + suggested = common.Big0 + } + tip = suggested + m.lg.Info("Query gas tip cap done", "gasTipGap", tip) + } + gasFeeCap = new(big.Int).Add(baseFee, tip) + m.lg.Info("Suggested gas fee cap", "gasFeeCap", gasFeeCap) } - baseFee := blockHeader.BaseFee - return tip, baseFee, nil + return baseFee, tip, gasFeeCap, useConfig, nil } From 77d9fad18f571a994b8d66ab62298763f2e69ffd Mon Sep 17 00:00:00 2001 From: syntrust Date: Sun, 29 Sep 2024 15:01:15 +0800 Subject: [PATCH 4/4] use min profitable gas --- ethstorage/miner/l1_mining_api.go | 53 ++++++++----------------------- 1 file changed, 13 insertions(+), 40 deletions(-) diff --git a/ethstorage/miner/l1_mining_api.go b/ethstorage/miner/l1_mining_api.go index 32c2cfd2..fd6601e5 100644 --- a/ethstorage/miner/l1_mining_api.go +++ b/ethstorage/miner/l1_mining_api.go @@ -91,7 +91,7 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add return common.Hash{}, err } - baseFee, tip, gasFeeCap, useConfig, err := m.suggestGasPrices(ctx, cfg) + tip, gasFeeCap, useConfig, err := m.suggestGasPrices(ctx, cfg) if err != nil { m.lg.Error("Failed to suggest gas prices", "error", err) return common.Hash{}, err @@ -115,41 +115,16 @@ func (m *l1MiningAPI) SubmitMinedResult(ctx context.Context, contract common.Add m.lg.Error("Query mining reward failed", "error", err.Error()) return common.Hash{}, err } - isProfitable := func(gasPrice *big.Int) bool { - cost := new(big.Int).Mul(new(big.Int).SetUint64(estimatedGas), gasPrice) - profit := new(big.Int).Sub(reward, cost) - m.lg.Debug("Estimating profit", "gasPrice", gasPrice) - m.lg.Debug("Estimated reward and cost (in ether)", "reward", fmtEth(reward), "cost", fmtEth(cost)) - m.lg.Debug("Estimated profit (in ether)", "profit", fmtEth(profit), "minimumProfit", fmtEth(cfg.MinimumProfit)) - return profit.Cmp(cfg.MinimumProfit) == 1 - } - - if !isProfitable(gasFeeCap) { - m.lg.Warn("Will drop the tx: the profit will not meet expectation", "minimumProfit", fmtEth(cfg.MinimumProfit)) + profitableGasFeeCap := new(big.Int).Div(new(big.Int).Sub(reward, cfg.MinimumProfit), new(big.Int).SetUint64(estimatedGas)) + m.lg.Info("Minimum profitable gas fee cap", "gasFeeCap", profitableGasFeeCap) + if gasFeeCap.Cmp(profitableGasFeeCap) == 1 { + profit := new(big.Int).Sub(reward, new(big.Int).Mul(new(big.Int).SetUint64(estimatedGas), gasFeeCap)) + m.lg.Warn("Mining tx dropped: the profit will not meet expectation", "estimatedProfit", fmtEth(profit), "minimumProfit", fmtEth(cfg.MinimumProfit)) return common.Hash{}, errDropped } - if !useConfig { - // Max fee cap doubles the base fee to ensure the transaction will remain marketable for six consecutive 100% full blocks. - maxGasFeeCap := new(big.Int).Add(tip, new(big.Int).Mul(baseFee, common.Big2)) - baseFeeVar := baseFee - for { - // increase baseFee by 12.5% until maxGasFeeCap - baseFeeVar = new(big.Int).Div(new(big.Int).Mul(baseFeeVar, big.NewInt(9)), big.NewInt(8)) - gasFeeCapNext := new(big.Int).Add(tip, baseFeeVar) - m.lg.Debug("Increasing gasFeeCap", "gasFeeCap", gasFeeCapNext) - if gasFeeCapNext.Cmp(maxGasFeeCap) == 1 { - m.lg.Debug("Dropped the gasFeeCap that exceeds the maximum", "gasFeeCap", gasFeeCapNext, "maxGasFeeCap", maxGasFeeCap) - break - } - if !isProfitable(gasFeeCapNext) { - m.lg.Debug("Dropped the gasFeeCap that will not meet the minimum profit", "gasFeeCap", gasFeeCapNext) - break - } - gasFeeCap = gasFeeCapNext - m.lg.Debug("GasFeeCap increased", "gasFeeCap", gasFeeCap) - } - m.lg.Info("Using suggested gasFeeCap", "gasFeeCap", gasFeeCap) + gasFeeCap = profitableGasFeeCap + m.lg.Info("Using profitable gas fee cap", "gasFeeCap", gasFeeCap) } sign := cfg.SignerFnFactory(m.NetworkID) nonce, err := m.NonceAt(ctx, cfg.SignerAddr, big.NewInt(rpc.LatestBlockNumber.Int64())) @@ -243,8 +218,7 @@ func (m *l1MiningAPI) composeCalldata(ctx context.Context, rst result) ([]byte, return calldata, nil } -func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.Int, *big.Int, *big.Int, bool, error) { - var baseFee *big.Int +func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.Int, *big.Int, bool, error) { gasFeeCap := cfg.GasPrice tip := cfg.PriorityGasPrice useConfig := true @@ -253,10 +227,9 @@ func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.In blockHeader, err := m.HeaderByNumber(ctx, nil) if err != nil { m.lg.Error("Failed to get block header", "error", err) - return nil, nil, nil, false, err + return nil, nil, false, err } - baseFee = blockHeader.BaseFee - m.lg.Info("Query baseFee done", "baseFee", baseFee) + m.lg.Info("Query baseFee done", "baseFee", blockHeader.BaseFee) if tip == nil || tip.Cmp(common.Big0) == 0 { suggested, err := m.SuggestGasTipCap(ctx) if err != nil { @@ -266,8 +239,8 @@ func (m *l1MiningAPI) suggestGasPrices(ctx context.Context, cfg Config) (*big.In tip = suggested m.lg.Info("Query gas tip cap done", "gasTipGap", tip) } - gasFeeCap = new(big.Int).Add(baseFee, tip) + gasFeeCap = new(big.Int).Add(blockHeader.BaseFee, tip) m.lg.Info("Suggested gas fee cap", "gasFeeCap", gasFeeCap) } - return baseFee, tip, gasFeeCap, useConfig, nil + return tip, gasFeeCap, useConfig, nil }