Skip to content

Commit

Permalink
go/consensus: add Size to block header
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jul 11, 2024
1 parent 16db5f3 commit 90fa3ab
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changelog/5761.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/consensus: add GasUsed to transaction results and Size to block header
4 changes: 4 additions & 0 deletions go/consensus/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,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"`
// Meta contains the consensus backend specific block metadata.
Meta cbor.RawMessage `json:"meta"`
}
Expand Down Expand Up @@ -320,6 +322,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"`

// GenesisHeight is the height of the genesis block.
GenesisHeight int64 `json:"genesis_height"`
Expand Down
1 change: 1 addition & 0 deletions go/consensus/cometbft/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func NewBlock(blk *cmttypes.Block) *consensus.Block {
Type: mkvsNode.RootTypeState,
Hash: stateRoot,
},
Size: uint64(blk.Size()),
Meta: rawMeta,
}
}
Expand Down
1 change: 1 addition & 0 deletions go/consensus/cometbft/full/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ func (n *commonNode) GetStatus(ctx context.Context) (*consensusAPI.Status, error
status.LatestHash = latestBlk.Hash
status.LatestTime = latestBlk.Time
status.LatestStateRoot = latestBlk.StateRoot
status.LatestBlockSize = latestBlk.Size

var epoch beaconAPI.EpochTime
epoch, err = n.beacon.GetEpoch(ctx, status.LatestHeight)
Expand Down
6 changes: 4 additions & 2 deletions go/consensus/tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend)
blk, err := backend.GetBlock(ctx, consensus.HeightLatest)
require.NoError(err, "GetBlock")
require.NotNil(blk, "returned block should not be nil")
require.True(blk.Height > 0, "block height should be greater than zero")
require.Greater(blk.Height, int64(0), "block height should be greater than zero")
require.Greater(blk.Size, uint64(0), "block size should be greater than zero")

status, err := backend.GetStatus(ctx)
require.NoError(err, "GetStatus")
Expand All @@ -58,6 +59,7 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend)
require.EqualValues(blk.Height, status.LatestHeight, "latest block heights should match")
require.EqualValues(blk.Hash, status.LatestHash, "latest block hashes should match")
require.EqualValues(blk.StateRoot, status.LatestStateRoot, "latest state roots should match")
require.EqualValues(blk.Size, status.LatestBlockSize, "latest block sizes should match")

txs, err := backend.GetTransactions(ctx, status.LatestHeight)
require.NoError(err, "GetTransactions")
Expand All @@ -79,7 +81,7 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend)
// Quick and dirty filter for meta transactions, which don't use any gas. Most other
// transactions do emit events so this should be good enough for here.
if len(res.Events) > 0 {
require.True(res.GasUsed > 0, "gas used should be greater than zero")
require.Greater(res.GasUsed, uint64(0), "gas used should be greater than zero")
}
}

Expand Down

0 comments on commit 90fa3ab

Please sign in to comment.