From 4893f1a877817cbb0148eec757195a9600e27689 Mon Sep 17 00:00:00 2001 From: Cesar Date: Mon, 26 Jun 2023 14:43:50 -0400 Subject: [PATCH] Add metrics for gas price / usage --- core/blockchain.go | 28 ++++++++++++++++++++++++++++ miner/worker.go | 11 +---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index e9dc0a98f2..707c7feaa8 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -81,6 +81,8 @@ var ( acceptorQueueGauge = metrics.NewRegisteredGauge("chain/acceptor/queue/size", nil) acceptorWorkTimer = metrics.NewRegisteredCounter("chain/acceptor/work", nil) acceptorWorkCount = metrics.NewRegisteredCounter("chain/acceptor/work/count", nil) + blockGasPriceGauge = metrics.NewRegisteredGauge("chain/block/gas/price", nil) + blockTotalFeesGauge = metrics.NewRegisteredGauge("chain/block/fees", nil) processedBlockGasUsedCounter = metrics.NewRegisteredCounter("chain/block/gas/used/processed", nil) acceptedBlockGasUsedCounter = metrics.NewRegisteredCounter("chain/block/gas/used/accepted", nil) badBlockCounter = metrics.NewRegisteredCounter("chain/block/bad/count", nil) @@ -1053,9 +1055,35 @@ func (bc *BlockChain) Accept(block *types.Block) error { bc.addAcceptorQueue(block) acceptedBlockGasUsedCounter.Inc(int64(block.GasUsed())) acceptedTxsCounter.Inc(int64(len(block.Transactions()))) + if baseFee := block.BaseFee(); baseFee != nil { + blockGasPriceGauge.Update(baseFee.Int64()) + } + + blockTotalFeesGauge.Update(TotalFees(block, bc.GetReceiptsByHash(block.Hash())).Int64()) return nil } +// TotalFees computes total consumed fees in ETH. Block transactions and receipts have to have the same order. +func TotalFees(block *types.Block, receipts []*types.Receipt) *big.Int { + baseFee := block.BaseFee() + feesWei := new(big.Int) + for i, tx := range block.Transactions() { + var minerFee *big.Int + if baseFee == nil { + // legacy block, no baseFee + minerFee = tx.GasPrice() + } else { + minerFee = new(big.Int).Add(baseFee, tx.EffectiveGasTipValue(baseFee)) + } + feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), minerFee)) + } + return feesWei +} + +func TotalFeesFloat(block *types.Block, receipts []*types.Receipt) *big.Float { + return new(big.Float).Quo(new(big.Float).SetInt(TotalFees(block, receipts)), new(big.Float).SetInt(big.NewInt(params.Ether))) +} + func (bc *BlockChain) Reject(block *types.Block) error { bc.chainmu.Lock() defer bc.chainmu.Unlock() diff --git a/miner/worker.go b/miner/worker.go index e74c7b61da..f1ded796ee 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -369,7 +369,7 @@ func (w *worker) handleResult(env *environment, block *types.Block, createdAt ti } log.Info("Commit new mining work", "number", block.Number(), "hash", hash, "timestamp", block.Time(), "uncles", 0, "txs", env.tcount, - "gas", block.GasUsed(), "fees", totalFees(block, receipts), "elapsed", common.PrettyDuration(time.Since(env.start))) + "gas", block.GasUsed(), "fees", core.TotalFeesFloat(block, receipts), "elapsed", common.PrettyDuration(time.Since(env.start))) // Note: the miner no longer emits a NewMinedBlock event. Instead the caller // is responsible for running any additional verification and then inserting @@ -387,15 +387,6 @@ func copyReceipts(receipts []*types.Receipt) []*types.Receipt { return result } -// totalFees computes total consumed fees in ETH. Block transactions and receipts have to have the same order. -func totalFees(block *types.Block, receipts []*types.Receipt) *big.Float { - feesWei := new(big.Int) - for i, tx := range block.Transactions() { - feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), tx.GasPrice())) - } - return new(big.Float).Quo(new(big.Float).SetInt(feesWei), new(big.Float).SetInt(big.NewInt(params.Ether))) -} - // enforcePredicates takes a set of pending transactions (grouped by sender, and ordered by nonce) // and returns the subset of those transactions (following the same grouping) that satisfy predicateContext. // Any transaction that fails predicate verification will be removed from the tx pool and excluded