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

Consensus add gas used, size fields #891

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,22 @@ func (m *processor) queueBlockInserts(batch *storage.QueryBatch, data *consensus
proposerAddr = common.Ptr(entity.String())
}
}

var gasUsed uint64
for _, txr := range data.TransactionsWithResults {
gasUsed += txr.Result.GasUsed
}

batch.Queue(
queries.ConsensusBlockUpsert,
data.BlockHeader.Height,
data.BlockHeader.Hash.Hex(),
data.BlockHeader.Time.UTC(),
len(data.TransactionsWithResults),
data.GasLimit,
gasUsed,
data.SizeLimit,
data.BlockHeader.Size,
data.Epoch,
data.BlockHeader.StateRoot.Namespace.String(),
int64(data.BlockHeader.StateRoot.Version),
Expand Down Expand Up @@ -548,6 +556,7 @@ func (m *processor) queueTransactionInserts(batch *storage.QueryBatch, data *con
module,
result.Error.Code,
message,
result.GasUsed,
)
// Bump the nonce.
if m.mode != analyzer.FastSyncMode { // Skip during fast sync; nonce will be provided by the genesis.
Expand Down
2 changes: 1 addition & 1 deletion analyzer/consensus/data_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
sdkConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"

beacon "github.com/oasisprotocol/nexus/coreapi/v22.2.11/beacon/api"
consensus "github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api"
roothash "github.com/oasisprotocol/nexus/coreapi/v22.2.11/roothash/api"
consensus "github.com/oasisprotocol/nexus/coreapi/v24.0/consensus/api"
"github.com/oasisprotocol/nexus/storage/oasis/nodeapi"
)

Expand Down
10 changes: 6 additions & 4 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ var (
height = excluded.height`

ConsensusBlockUpsert = `
INSERT INTO chain.blocks (height, block_hash, time, num_txs, gas_limit, size_limit, epoch, namespace, version, state_root, proposer_entity_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
INSERT INTO chain.blocks (height, block_hash, time, num_txs, gas_limit, gas_used, size_limit, size, epoch, namespace, version, state_root, proposer_entity_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
ON CONFLICT (height) DO UPDATE
SET
block_hash = excluded.block_hash,
Expand All @@ -179,7 +179,9 @@ var (
state_root = excluded.state_root,
epoch = excluded.epoch,
gas_limit = excluded.gas_limit,
gas_used = excluded.gas_used,
size_limit = excluded.size_limit,
size = excluded.size,
proposer_entity_id = excluded.proposer_entity_id`

ConsensusBlockAddSigners = `
Expand Down Expand Up @@ -211,8 +213,8 @@ var (
end_height = GREATEST(old.end_height, excluded.end_height)`

ConsensusTransactionInsert = `
INSERT INTO chain.transactions (block, tx_hash, tx_index, nonce, fee_amount, max_gas, method, sender, body, module, code, message)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`
INSERT INTO chain.transactions (block, tx_hash, tx_index, nonce, fee_amount, max_gas, method, sender, body, module, code, message, gas_used)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`

ConsensusAccountUpsert = `
INSERT INTO chain.accounts
Expand Down
44 changes: 29 additions & 15 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ components:

Block:
type: object
required: [height, hash, timestamp, num_transactions, gas_limit, epoch, state_root, proposer] # TODO: size, gas_used
required: [height, hash, timestamp, num_transactions, gas_limit, epoch, state_root, proposer]
properties:
height:
type: integer
Expand All @@ -1445,13 +1445,16 @@ components:
description: Number of transactions in the block.
example: 17
gas_limit:
allOf: [$ref: '#/components/schemas/TextBigInt']
type: integer
format: uint64
description: |
The gas limit for the block. A gas limit 0 means that the max block gas was unlimited.
Blocks from Cobalt, Damask, and early Eden had no gas limit as their sizes were only
restricted by byte size until an upgrade during Eden introduced a gas limit.
example: 30000
size_limit:
allOf: [$ref: '#/components/schemas/TextBigInt']
type: integer
format: uint64
description: |
The size limit for the block in bytes.
epoch:
Expand All @@ -1471,17 +1474,19 @@ components:
items:
allOf: [$ref: '#/components/schemas/EntityInfo']
description: A list of the entities that signed the block.
# TODO: Not available on backend
# size:
# type: integer
# format: int64
# description: Size of the block, in bytes. TODO: Be more exact. Should this include results? By far the easiest is to give the total bytesize of txs here.
# example: 123456
# gas_used:
# type: integer
# format: int64
# description: Total gas used by the transactions in the block.
# example: 123456
size:
type: integer
format: uint64
description: |
The size of the block in bytes.
This field is missing for blocks pre Oasis-Core 24.3.
gas_used:
type: integer
format: int64
description: |
Total gas used by the transactions in the block.
This field is missing for blocks pre Oasis-Core 24.3.
example: 123456
description: |
A consensus block.

Expand Down Expand Up @@ -1688,9 +1693,18 @@ components:
to pay to execute it.
example: 1000
gas_limit:
allOf: [$ref: '#/components/schemas/TextBigInt']
type: integer
format: uint64
description: |
The maximum gas that a transaction can use.
example: 30000
gas_used:
type: integer
format: uint64
description: |
The gas used by the transaction.
This field is missing for transactions pre Oasis-Core 24.3.
example: 30000
method:
allOf: [$ref: '#/components/schemas/ConsensusTxMethod']
description: The method that was called.
Expand Down
4 changes: 4 additions & 0 deletions coreapi/v24.0/consensus/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type Block struct {
Time time.Time `json:"time"`
// StateRoot is the Merkle root of the consensus state tree.
StateRoot mkvsNode.Root `json:"state_root"`
// Size is the size of the block in bytes.
Size uint64 `json:"size,omitempty"` // Added in 24.3.
// Meta contains the consensus backend specific block metadata.
Meta cbor.RawMessage `json:"meta"`
}
Expand Down Expand Up @@ -178,6 +180,8 @@ type Status struct { // nolint: maligned
LatestEpoch beacon.EpochTime `json:"latest_epoch"`
// LatestStateRoot is the Merkle root of the consensus state tree.
LatestStateRoot mkvsNode.Root `json:"latest_state_root"`
// LatestBlockSize is the size (in bytes) of the latest block.
LatestBlockSize uint64 `json:"latest_block_size,omitempty"` // Added in 24.3.

// GenesisHeight is the height of the genesis block.
GenesisHeight int64 `json:"genesis_height"`
Expand Down
3 changes: 3 additions & 0 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ func (c *StorageClient) Blocks(ctx context.Context, r apiTypes.GetConsensusBlock
&b.Timestamp,
&b.NumTransactions,
&b.GasLimit,
&b.GasUsed,
&b.SizeLimit,
&b.Size,
&b.Epoch,
&b.StateRoot,
&proposerRow,
Expand Down Expand Up @@ -428,6 +430,7 @@ func (c *StorageClient) Transactions(ctx context.Context, p apiTypes.GetConsensu
&t.Nonce,
&t.Fee,
&t.GasLimit,
&t.GasUsed,
&t.Method,
&t.Body,
&code,
Expand Down
5 changes: 4 additions & 1 deletion storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const (
time,
num_txs,
gas_limit,
gas_used,
size_limit,
size,
epoch,
state_root,
ROW(
Expand Down Expand Up @@ -76,12 +78,13 @@ const (
chain.transactions.nonce as nonce,
chain.transactions.fee_amount as fee_amount,
chain.transactions.max_gas as gas_limit,
chain.transactions.gas_used as gas_used,
chain.transactions.method as method,
chain.transactions.body as body,
chain.transactions.code as code,
chain.transactions.module as module,
chain.transactions.message as message,
chain.blocks.time as time
chain.blocks.time as time,
FROM chain.transactions
JOIN chain.blocks ON chain.transactions.block = chain.blocks.height
LEFT JOIN chain.accounts_related_transactions ON chain.transactions.block = chain.accounts_related_transactions.tx_block
Expand Down
3 changes: 3 additions & 0 deletions storage/migrations/00_consensus.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ CREATE TABLE chain.blocks
time TIMESTAMP WITH TIME ZONE NOT NULL,
num_txs UINT31 NOT NULL,
gas_limit UINT_NUMERIC NOT NULL DEFAULT 0, -- uint64 in go; because the value might conceivably be >2^63, we use UINT_NUMERIC over UINT63 here.
-- gas_used UINT_NUMERIC, -- Added in 12_consensus_tx_new_fields.up.sql
size_limit UINT_NUMERIC NOT NULL DEFAULT 0, -- uint64 in go; because the value might conceivably be >2^63, we use UINT_NUMERIC over UINT63 here.
--size UINT_NUMERIC, -- Added in 12_consensus_tx_new_fields.up.sql
epoch UINT63 NOT NULL DEFAULT 0,

-- State Root Info
Expand All @@ -62,6 +64,7 @@ CREATE TABLE chain.transactions
nonce UINT63 NOT NULL,
fee_amount UINT_NUMERIC,
max_gas UINT_NUMERIC, -- uint64 in go; because the value might conceivably be >2^63, we use UINT_NUMERIC over UINT63 here.
--gas_used UINT_NUMERIC, -- Added in 12_consensus_tx_new_fields.up.sql
method TEXT NOT NULL,
sender oasis_addr NOT NULL,
body JSONB,
Expand Down
8 changes: 8 additions & 0 deletions storage/migrations/12_consensus_tx_new_fields.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BEGIN;

ALTER TABLE chain.transactions ADD COLUMN gas_used UINT_NUMERIC;
ALTER TABLE chain.blocks
ADD COLUMN size UINT_NUMERIC,
ADD COLUMN gas_used UINT_NUMERIC;

COMMIT;
Loading
Loading