Skip to content

Commit

Permalink
go/consensus: add GasUsed to transaction results
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jul 11, 2024
1 parent 120a7dd commit 16db5f3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
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/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
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
9 changes: 8 additions & 1 deletion go/consensus/tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 16db5f3

Please sign in to comment.