Skip to content

Commit

Permalink
feat(rpc): update tx status to return logs (#1459)
Browse files Browse the repository at this point in the history
## Description

Part of #1454
  • Loading branch information
ninabarbakadze authored Aug 16, 2024
1 parent 9563209 commit c23345b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 34 deletions.
35 changes: 17 additions & 18 deletions proto/tendermint/store/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions proto/tendermint/store/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ message BlockStoreState {
}

// TxInfo describes the location of a tx inside a committed block
// as well as the result of executing the transaction and the raw log output.
// as well as the result of executing the transaction and the error log output.
message TxInfo {
int64 height = 1;
uint32 index = 2;
// The response code of executing the tx. 0 means
// successfully executed, all others are error codes.
uint32 code = 3;
// The log output generated during the execution of a transaction.
// Note: this is empty if the transaction succeeded.
string log = 4;
// The error log output generated if the transaction execution fails.
string error = 4;
}
2 changes: 2 additions & 0 deletions rpc/client/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ func TestTxStatus(t *testing.T) {
require.EqualValues(bres.Height, result.Height)
require.EqualValues(0, result.Index)
require.Equal("COMMITTED", result.Status)
require.Equal(abci.CodeTypeOK, result.ExecutionCode)
require.Equal("", result.Error)
}

func TestTxSearch(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions rpc/core/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,16 @@ func ProveShares(
return shareProof, nil
}

// TxStatus retrieves the status of a transaction given its hash. It returns a ResultTxStatus
// containing the height and index of the transaction within the block(if committed)
// or whether the transaction is pending, evicted from the mempool, or otherwise unknown.
// TxStatus retrieves the status of a transaction by its hash. It returns a ResultTxStatus
// with the transaction's height and index if committed, or its pending, evicted, or unknown status.
// It also includes the execution code and log for failed txs.
func TxStatus(ctx *rpctypes.Context, hash []byte) (*ctypes.ResultTxStatus, error) {
env := GetEnvironment()

// Check if the tx has been committed
txInfo := env.BlockStore.LoadTxInfo(hash)
if txInfo != nil {
return &ctypes.ResultTxStatus{Height: txInfo.Height, Index: txInfo.Index, ExecutionCode: txInfo.Code, Status: TxStatusCommitted}, nil
return &ctypes.ResultTxStatus{Height: txInfo.Height, Index: txInfo.Index, ExecutionCode: txInfo.Code, Error: txInfo.Error, Status: TxStatusCommitted}, nil
}

// Get the tx key from the hash
Expand Down
1 change: 1 addition & 0 deletions rpc/core/tx_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestTxStatus(t *testing.T) {
assert.Equal(t, height, txStatus.Height)
assert.Equal(t, uint32(i), txStatus.Index)
assert.Equal(t, uint32(0), txStatus.ExecutionCode)
assert.Equal(t, "", txStatus.Error)
}
}

Expand Down
3 changes: 2 additions & 1 deletion rpc/core/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ type ResultCommit struct {
}

// ResultTxStatus represents the status of a transaction during its life cycle.
// It contains info to locate a tx in a committed block as well as its execution code and status.
// It contains info to locate a tx in a committed block as well as its execution code, log if it fails and status.
type ResultTxStatus struct {
Height int64 `json:"height"`
Index uint32 `json:"index"`
ExecutionCode uint32 `json:"execution_code"`
Error string `json:"error"`
Status string `json:"status"`
}

Expand Down
6 changes: 3 additions & 3 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func (bs *BlockStore) SaveSeenCommit(height int64, seenCommit *types.Commit) err
}

// SaveTxInfo indexes the txs from the block with the given response codes and logs from execution.
// The logs are only saved for failed transactions.
// Only the error logs are saved for failed transactions.
func (bs *BlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error {
if len(txResponseCodes) != len(block.Txs) {
return fmt.Errorf("txResponseCodes length mismatch with block txs length")
Expand All @@ -471,9 +471,9 @@ func (bs *BlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32, l
Index: uint32(i),
Code: txResponseCodes[i],
}
// Only save Logs for failed transactions
// Set error log for failed txs
if txResponseCodes[i] != abci.CodeTypeOK {
txInfo.Log = logs[i]
txInfo.Error = logs[i]
}
txInfoBytes, err := proto.Marshal(&txInfo)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -418,10 +419,10 @@ func TestSaveTxInfo(t *testing.T) {
require.Equal(t, uint32(i), txInfo.Index)
require.Equal(t, txResponseCodes[i], txInfo.Code)
// We don't save the logs for successful transactions
if txResponseCodes[i] == 0 {
require.Equal(t, "", txInfo.Log)
if txResponseCodes[i] == abci.CodeTypeOK {
require.Equal(t, "", txInfo.Error)
} else {
require.Equal(t, logs[i], txInfo.Log)
require.Equal(t, logs[i], txInfo.Error)
}
}
}
Expand All @@ -435,7 +436,7 @@ func TestSaveTxInfo(t *testing.T) {
require.Equal(t, txInfo.Height, int64(777))
require.Equal(t, uint32(1), txInfo.Code)
require.Equal(t, uint32(5), txInfo.Index)
require.Equal(t, "failure", txInfo.Log)
require.Equal(t, "failure", txInfo.Error)
}

func TestLoadBaseMeta(t *testing.T) {
Expand Down

0 comments on commit c23345b

Please sign in to comment.