From 10d174adcc619225cf982962510b49fc72189a1c Mon Sep 17 00:00:00 2001 From: crypto-facs <84574577+crypto-facs@users.noreply.github.com> Date: Thu, 24 Jun 2021 13:47:41 -0400 Subject: [PATCH] rpc: Add `TotalGasUsed` to `block_results` response (#6615) Closes #6551 Simple PR to add the total gas used in the block by adding the gas used in all the transactions. This adds a `TotalGasUsed` field to `coretypes.ResultBlockResults`. Its my first PR to the repo so let me know if there is anything I am missing! @fedekunze In case you want to take a look --- CHANGELOG_PENDING.md | 3 ++- rpc/core/blocks.go | 6 ++++++ rpc/core/blocks_test.go | 7 ++++--- rpc/core/types/responses.go | 1 + rpc/openapi/openapi.yaml | 3 +++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index ae75eadd2..fc1c38169 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -126,6 +126,7 @@ Friendly reminder: We have a [bug bounty program](https://hackerone.com/tendermi - [p2p/pex] \#6509 Improve addrBook.hash performance (@cuonglm) - [consensus/metrics] \#6549 Change block_size gauge to a histogram for better observability over time (@marbar3778) - [statesync] \#6587 Increase chunk priority and re-request chunks that don't arrive (@cmwaters) +- [rpc] \#6615 Add TotalGasUsed to block_results response (@crypto-facs) ### BUG FIXES @@ -134,5 +135,5 @@ Friendly reminder: We have a [bug bounty program](https://hackerone.com/tendermi - [blockchain/v1] \#5711 Fix deadlock (@melekes) - [evidence] \#6375 Fix bug with inconsistent LightClientAttackEvidence hashing (cmwaters) - [rpc] \#6507 fix RPC client doesn't handle url's without ports (@JayT106) -- [statesync] \#6463 Adds Reverse Sync feature to fetch historical light blocks after state sync in order to verify any evidence (@cmwaters) +- [statesync] \#6463 Adds Reverse Sync feature to fetch historical light blocks after state sync in order to verify any evidence (@cmwaters) - [fastsync] \#6590 Update the metrics during fast-sync (@JayT106) diff --git a/rpc/core/blocks.go b/rpc/core/blocks.go index f36a4e3de..6e92a8805 100644 --- a/rpc/core/blocks.go +++ b/rpc/core/blocks.go @@ -161,9 +161,15 @@ func (env *Environment) BlockResults(ctx *rpctypes.Context, heightPtr *int64) (* return nil, err } + var totalGasUsed int64 + for _, tx := range results.GetDeliverTxs() { + totalGasUsed += tx.GetGasUsed() + } + return &ctypes.ResultBlockResults{ Height: height, TxsResults: results.DeliverTxs, + TotalGasUsed: totalGasUsed, BeginBlockEvents: results.BeginBlock.Events, EndBlockEvents: results.EndBlock.Events, ValidatorUpdates: results.EndBlock.ValidatorUpdates, diff --git a/rpc/core/blocks_test.go b/rpc/core/blocks_test.go index 5e7d85934..70bd4c723 100644 --- a/rpc/core/blocks_test.go +++ b/rpc/core/blocks_test.go @@ -72,9 +72,9 @@ func TestBlockchainInfo(t *testing.T) { func TestBlockResults(t *testing.T) { results := &tmstate.ABCIResponses{ DeliverTxs: []*abci.ResponseDeliverTx{ - {Code: 0, Data: []byte{0x01}, Log: "ok"}, - {Code: 0, Data: []byte{0x02}, Log: "ok"}, - {Code: 1, Log: "not ok"}, + {Code: 0, Data: []byte{0x01}, Log: "ok", GasUsed: 10}, + {Code: 0, Data: []byte{0x02}, Log: "ok", GasUsed: 5}, + {Code: 1, Log: "not ok", GasUsed: 0}, }, EndBlock: &abci.ResponseEndBlock{}, BeginBlock: &abci.ResponseBeginBlock{}, @@ -97,6 +97,7 @@ func TestBlockResults(t *testing.T) { {100, false, &ctypes.ResultBlockResults{ Height: 100, TxsResults: results.DeliverTxs, + TotalGasUsed: 15, BeginBlockEvents: results.BeginBlock.Events, EndBlockEvents: results.EndBlock.Events, ValidatorUpdates: results.EndBlock.ValidatorUpdates, diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index b8eb5bd48..d6d4b0983 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -62,6 +62,7 @@ type ResultCommit struct { type ResultBlockResults struct { Height int64 `json:"height"` TxsResults []*abci.ResponseDeliverTx `json:"txs_results"` + TotalGasUsed int64 `json:"total_gas_used"` BeginBlockEvents []abci.Event `json:"begin_block_events"` EndBlockEvents []abci.Event `json:"end_block_events"` ValidatorUpdates []abci.ValidatorUpdate `json:"validator_updates"` diff --git a/rpc/openapi/openapi.yaml b/rpc/openapi/openapi.yaml index 9e1200bc6..6a2c16d19 100644 --- a/rpc/openapi/openapi.yaml +++ b/rpc/openapi/openapi.yaml @@ -1666,6 +1666,9 @@ components: codespace: type: string example: "ibc" + total_gas_used: + type: string + example: "100" begin_block_events: type: array nullable: true