From 16db5f36b2566432559b819e48cc401390b19525 Mon Sep 17 00:00:00 2001 From: ptrus Date: Sat, 6 Jul 2024 17:38:43 +0200 Subject: [PATCH] go/consensus: add GasUsed to transaction results --- go/consensus/api/transaction/results/results.go | 5 +++-- go/consensus/cometbft/full/common.go | 1 + go/consensus/cometbft/tests/genesis/genesis.go | 4 ++++ go/consensus/tests/tester.go | 9 ++++++++- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/go/consensus/api/transaction/results/results.go b/go/consensus/api/transaction/results/results.go index 9357fa030c1..1b2bf257706 100644 --- a/go/consensus/api/transaction/results/results.go +++ b/go/consensus/api/transaction/results/results.go @@ -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. diff --git a/go/consensus/cometbft/full/common.go b/go/consensus/cometbft/full/common.go index 33e63c6327a..ad4461dfb7f 100644 --- a/go/consensus/cometbft/full/common.go +++ b/go/consensus/cometbft/full/common.go @@ -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. diff --git a/go/consensus/cometbft/tests/genesis/genesis.go b/go/consensus/cometbft/tests/genesis/genesis.go index ad524bae24e..f7757eea69f 100644 --- a/go/consensus/cometbft/tests/genesis/genesis.go +++ b/go/consensus/cometbft/tests/genesis/genesis.go @@ -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" @@ -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(), diff --git a/go/consensus/tests/tester.go b/go/consensus/tests/tester.go index 8c01a7b439f..9cbffbedc8b 100644 --- a/go/consensus/tests/tester.go +++ b/go/consensus/tests/tester.go @@ -75,6 +75,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.True(res.GasUsed > 0, "gas used should be greater than zero") + } + } txsWithProofs, err := backend.GetTransactionsWithProofs(ctx, status.LatestHeight) require.NoError(err, "GetTransactionsWithProofs") @@ -151,7 +158,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")