Skip to content

Commit

Permalink
Merge pull request #5761 from oasisprotocol/ptrus/feature/tx-gas-used
Browse files Browse the repository at this point in the history
go/consensus: add GasUsed to transaction results and Size to block header
  • Loading branch information
ptrus authored Jul 11, 2024
2 parents 120a7dd + 90fa3ab commit 0fa7e30
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 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
5 changes: 3 additions & 2 deletions go/consensus/api/transaction/results/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ type Error struct {

// Result is a transaction execution result.
type Result struct {
Error Error `json:"error"`
Events []*Event `json:"events"`
Error Error `json:"error"`
Events []*Event `json:"events"`
GasUsed uint64 `json:"gas_used,omitempty"`
}

// IsSuccess returns true if transaction execution was successful.
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
2 changes: 2 additions & 0 deletions go/consensus/cometbft/full/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ func (n *commonNode) GetTransactionsWithResults(ctx context.Context, height int6
Code: rs.GetCode(),
Message: rs.GetLog(),
},
GasUsed: uint64(rs.GetGasUsed()),
}

// Transaction staking events.
Expand Down Expand Up @@ -879,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
4 changes: 4 additions & 0 deletions go/consensus/cometbft/tests/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
"github.com/oasisprotocol/oasis-core/go/common/version"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
cmt "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/crypto"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/genesis"
Expand Down Expand Up @@ -113,6 +114,9 @@ func NewTestNodeGenesisProvider(identity *identity.Identity, ent *entity.Entity,
SkipTimeoutCommit: true,
MaxBlockSize: 21 * 1024 * 1024,
MaxEvidenceSize: 1024 * 1024,
GasCosts: transaction.Costs{
consensus.GasOpTxByte: 1,
},
},
},
Staking: stakingTests.GenesisState(),
Expand Down
13 changes: 11 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 @@ -75,6 +77,13 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend)
len(txsWithResults.Transactions),
"GetTransactionsWithResults.Results length mismatch",
)
for _, res := range txsWithResults.Results {
// 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.Greater(res.GasUsed, uint64(0), "gas used should be greater than zero")
}
}

txsWithProofs, err := backend.GetTransactionsWithProofs(ctx, status.LatestHeight)
require.NoError(err, "GetTransactionsWithProofs")
Expand Down Expand Up @@ -151,7 +160,7 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend)
err = backend.SubmitTxNoWait(ctx, &transaction.SignedTransaction{})
require.Error(err, "SubmitTxNoWait should fail with invalid transaction")

testTx := transaction.NewTransaction(0, nil, staking.MethodTransfer, &staking.Transfer{})
testTx := transaction.NewTransaction(0, &transaction.Fee{Gas: 10_000}, staking.MethodTransfer, &staking.Transfer{})
testSigner := memorySigner.NewTestSigner(fmt.Sprintf("consensus tests tx signer: %T", backend))
testSigTx, err := transaction.Sign(testSigner, testTx)
require.NoError(err, "transaction.Sign")
Expand Down

0 comments on commit 0fa7e30

Please sign in to comment.