Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics for gas price / usage #700

Merged
merged 3 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ var (
blockStateValidationTimer = metrics.NewRegisteredCounter("chain/block/validations/state", nil)
blockWriteTimer = metrics.NewRegisteredCounter("chain/block/writes", nil)

acceptorQueueGauge = metrics.NewRegisteredGauge("chain/acceptor/queue/size", nil)
acceptorWorkTimer = metrics.NewRegisteredCounter("chain/acceptor/work", nil)
acceptorWorkCount = metrics.NewRegisteredCounter("chain/acceptor/work/count", 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)
acceptorQueueGauge = metrics.NewRegisteredGauge("chain/acceptor/queue/size", nil)
acceptorWorkTimer = metrics.NewRegisteredCounter("chain/acceptor/work", nil)
acceptorWorkCount = metrics.NewRegisteredCounter("chain/acceptor/work/count", nil)
lastAcceptedBlockBaseFeeGauge = metrics.NewRegisteredGauge("chain/block/gas/basefee", nil)
blockTotalFeesGauge = metrics.NewRegisteredGauge("chain/block/gas/fees", nil)
nytzuga marked this conversation as resolved.
Show resolved Hide resolved
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)

txUnindexTimer = metrics.NewRegisteredCounter("chain/txs/unindex", nil)
acceptedTxsCounter = metrics.NewRegisteredCounter("chain/txs/accepted", nil)
Expand Down Expand Up @@ -1053,9 +1055,46 @@ 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 {
lastAcceptedBlockBaseFeeGauge.Update(baseFee.Int64())
}
total, err := TotalFees(block, bc.GetReceiptsByHash(block.Hash()))
if err != nil {
log.Error(fmt.Sprintf("TotalFees error: %s", err))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we specifically not returned any error here? If so I think this should be a warn instead of error

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think error is the right level here since this should never happen

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why dont we return the error?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could go either way. The desire was not to treat this as fatal since it's only used to add an extra metric.

} else {
blockTotalFeesGauge.Update(total.Int64())
}
return nil
}

// TotalFees computes total consumed fees in ETH. Block transactions and receipts have to have the same order.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's in wei rather than eth

func TotalFees(block *types.Block, receipts []*types.Receipt) (*big.Int, error) {
baseFee := block.BaseFee()
feesWei := new(big.Int)
if len(block.Transactions()) != len(receipts) {
return nil, errors.New("mismatch between total number of transactions and receipts")
}
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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hard to read, can we split this into two different variables?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for this PR since it's just copied from miner/worker.go

}
return feesWei, nil
}

func TotalFeesFloat(block *types.Block, receipts []*types.Receipt) (*big.Float, error) {
total, err := TotalFees(block, receipts)
if err != nil {
return nil, err
}
return new(big.Float).Quo(new(big.Float).SetInt(total), new(big.Float).SetInt(big.NewInt(params.Ether))), nil
}

func (bc *BlockChain) Reject(block *types.Block) error {
bc.chainmu.Lock()
defer bc.chainmu.Unlock()
Expand Down
16 changes: 6 additions & 10 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,13 @@ func (w *worker) handleResult(env *environment, block *types.Block, createdAt ti
logs = append(logs, receipt.Logs...)
}

totalFees, err := core.TotalFeesFloat(block, receipts)
if err != nil {
log.Error("TotalFeesFloat error: %s", err)
}

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", totalFees, "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
Expand All @@ -387,15 +392,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
Expand Down