Skip to content

Commit cfbf301

Browse files
feat!: extend TxStatus to include more tx info (#2543)
Fixes #2542
1 parent 8b807c8 commit cfbf301

File tree

11 files changed

+218
-67
lines changed

11 files changed

+218
-67
lines changed

consensus/replay_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ func newMockBlockStore(t *testing.T, config *cfg.Config, params types.ConsensusP
11691169
}
11701170
}
11711171

1172-
func (*mockBlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error {
1172+
func (*mockBlockStore) SaveTxInfo(block *types.Block, execTxRes []*abci.ExecTxResult) error {
11731173
return nil
11741174
}
11751175
func (bs *mockBlockStore) LoadTxInfo(hash []byte) *cmtstore.TxInfo { return &cmtstore.TxInfo{} }

proto/tendermint/store/types.pb.go

Lines changed: 143 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/tendermint/store/types.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ message TxInfo {
1818
uint32 code = 3;
1919
// The error log output generated if the transaction execution fails.
2020
string error = 4;
21+
// The error namespace/module
22+
string codespace = 5;
23+
// Requested gas limit
24+
int64 gas_wanted = 6;
25+
// Actual gas consumed
26+
int64 gas_used = 7;
2127
}

rpc/core/blocks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (mockBlockStore) SaveBlockWithExtendedCommit(block *types.Block, blockParts
364364
func (mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
365365
}
366366

367-
func (mockBlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error {
367+
func (mockBlockStore) SaveTxInfo(block *types.Block, txResults []*abci.ExecTxResult) error {
368368
return nil
369369
}
370370

rpc/core/tx.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,16 @@ func (env *Environment) TxStatus(ctx *rpctypes.Context, hash []byte) (*ctypes.Re
228228
// Check if the tx has been committed
229229
txInfo := env.BlockStore.LoadTxInfo(hash)
230230
if txInfo != nil {
231-
return &ctypes.ResultTxStatus{Height: txInfo.Height, Index: txInfo.Index, ExecutionCode: txInfo.Code, Error: txInfo.Error, Status: TxStatusCommitted}, nil
231+
return &ctypes.ResultTxStatus{
232+
Height: txInfo.Height,
233+
Index: txInfo.Index,
234+
ExecutionCode: txInfo.Code,
235+
Error: txInfo.Error,
236+
Status: TxStatusCommitted,
237+
Codespace: txInfo.Codespace,
238+
GasWanted: txInfo.GasWanted,
239+
GasUsed: txInfo.GasUsed,
240+
}, nil
232241
}
233242

234243
// Get the tx key from the hash

rpc/core/types/responses.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ type ResultTxStatus struct {
272272
ExecutionCode uint32 `json:"execution_code"`
273273
Error string `json:"error"`
274274
Status string `json:"status"`
275+
Codespace string `json:"codespace,omitempty"`
276+
GasWanted int64 `json:"gas_wanted,omitempty"`
277+
GasUsed int64 `json:"gas_used,omitempty"`
275278
}
276279

277280
type ResultDataCommitment struct {

state/execution.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,7 @@ func (blockExec *BlockExecutor) applyBlock(state State, blockID types.BlockID, b
356356
// This needs to be done prior to saving state
357357
// for correct crash recovery
358358
if blockExec.blockStore != nil {
359-
respCodes := getResponseCodes(abciResponse.TxResults)
360-
logs := getLogs(abciResponse.TxResults)
361-
if err := blockExec.blockStore.SaveTxInfo(block, respCodes, logs); err != nil {
359+
if err := blockExec.blockStore.SaveTxInfo(block, abciResponse.TxResults); err != nil {
362360
return state, err
363361
}
364362
}
@@ -930,24 +928,6 @@ func (blockExec *BlockExecutor) pruneBlocks(retainHeight int64, state State) (ui
930928
return amountPruned, nil
931929
}
932930

933-
// getResponseCodes gets response codes from a list of ResponseDeliverTx.
934-
func getResponseCodes(responses []*abci.ExecTxResult) []uint32 {
935-
responseCodes := make([]uint32, len(responses))
936-
for i, response := range responses {
937-
responseCodes[i] = response.Code
938-
}
939-
return responseCodes
940-
}
941-
942-
// getLogs gets logs from a list of ResponseDeliverTx.
943-
func getLogs(responses []*abci.ExecTxResult) []string {
944-
logs := make([]string, len(responses))
945-
for i, response := range responses {
946-
logs[i] = response.Log
947-
}
948-
return logs
949-
}
950-
951931
// saveFailedProposalBlock saves a failed proposal block to the debug directory
952932
func (blockExec *BlockExecutor) saveFailedProposalBlock(state State, block *types.Block, reason string) {
953933
if blockExec.rootDir == "" {

state/mocks/block_store.go

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

state/services.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package state
22

33
import (
4+
abci "github.com/cometbft/cometbft/abci/types"
45
"github.com/cometbft/cometbft/types"
56

67
cmtstore "github.com/cometbft/cometbft/proto/tendermint/store"
@@ -40,7 +41,7 @@ type BlockStore interface {
4041
LoadBlockExtendedCommit(height int64) *types.ExtendedCommit
4142

4243
LoadTxInfo(hash []byte) *cmtstore.TxInfo
43-
SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error
44+
SaveTxInfo(block *types.Block, execTxRes []*abci.ExecTxResult) error
4445

4546
DeleteLatestBlock() error
4647

store/store.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -791,30 +791,31 @@ func (bs *BlockStore) DeleteLatestBlock() error {
791791
return bs.saveStateAndWriteDB(batch, "failed to delete the latest block")
792792
}
793793

794-
// SaveTxInfo indexes the txs from the block with the given response codes and logs from execution.
794+
// SaveTxInfo indexes the txs from the block with the given execution results.
795795
// Only the error logs are saved for failed transactions.
796-
func (bs *BlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error {
797-
if len(txResponseCodes) != len(block.Txs) {
798-
return errors.New("txResponseCodes length mismatch with block txs length")
799-
}
800-
if len(logs) != len(block.Txs) {
801-
return errors.New("logs length mismatch with block txs length")
796+
func (bs *BlockStore) SaveTxInfo(block *types.Block, execTxRes []*abci.ExecTxResult) error {
797+
if len(execTxRes) != len(block.Txs) {
798+
return errors.New("tx execution results length mismatch with block txs length")
802799
}
803800

804801
// Create a new batch
805802
batch := bs.db.NewBatch()
806803

807804
// Batch and save txs from the block
808805
for i, tx := range block.Txs {
806+
result := execTxRes[i]
809807
txInfo := cmtstore.TxInfo{
810808
Height: block.Height,
811809
//nolint:gosec
812-
Index: uint32(i),
813-
Code: txResponseCodes[i],
810+
Index: uint32(i),
811+
Code: result.Code,
812+
Codespace: result.Codespace,
813+
GasWanted: result.GasWanted,
814+
GasUsed: result.GasUsed,
814815
}
815816
// Set error log for failed txs
816-
if txResponseCodes[i] != abci.CodeTypeOK {
817-
txInfo.Error = logs[i]
817+
if result.Code != abci.CodeTypeOK {
818+
txInfo.Error = result.Log
818819
}
819820
txInfoBytes, err := proto.Marshal(&txInfo)
820821
if err != nil {

0 commit comments

Comments
 (0)