Skip to content

Commit 9563209

Browse files
feat(store): add rawlog in txstatus (#1458)
## Description Fixes #1454 --------- Co-authored-by: Rootul P <rootulp@gmail.com>
1 parent 353761f commit 9563209

File tree

10 files changed

+109
-23
lines changed

10 files changed

+109
-23
lines changed

consensus/replay_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
12241224
func (bs *mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil }
12251225
func (bs *mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
12261226
}
1227-
func (bs *mockBlockStore) SaveTxInfo(block *types.Block, txResponseCode []uint32) error {
1227+
func (bs *mockBlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error {
12281228
return nil
12291229
}
12301230
func (bs *mockBlockStore) LoadTxInfo(hash []byte) *cmtstore.TxInfo { return &cmtstore.TxInfo{} }

proto/tendermint/store/types.pb.go

Lines changed: 62 additions & 8 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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ message BlockStoreState {
99
}
1010

1111
// TxInfo describes the location of a tx inside a committed block
12-
// as well as the result of executing the transaction.
12+
// as well as the result of executing the transaction and the raw log output.
1313
message TxInfo {
1414
int64 height = 1;
1515
uint32 index = 2;
1616
// The response code of executing the tx. 0 means
1717
// successfully executed, all others are error codes.
1818
uint32 code = 3;
19+
// The log output generated during the execution of a transaction.
20+
// Note: this is empty if the transaction succeeded.
21+
string log = 4;
1922
}

rpc/core/blocks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (mockBlockStore) LoadSeenCommit(height int64) *types.Commit { retur
301301
func (mockBlockStore) PruneBlocks(height int64) (uint64, error) { return 0, nil }
302302
func (mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
303303
}
304-
func (mockBlockStore) SaveTxInfo(block *types.Block, txResponseCode []uint32) error {
304+
func (mockBlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error {
305305
return nil
306306
}
307307

state/execution.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ func (blockExec *BlockExecutor) ApplyBlock(
248248
// for correct crash recovery
249249
if blockExec.blockStore != nil {
250250
respCodes := getResponseCodes(abciResponses.DeliverTxs)
251-
if err := blockExec.blockStore.SaveTxInfo(block, respCodes); err != nil {
251+
logs := getLogs(abciResponses.DeliverTxs)
252+
if err := blockExec.blockStore.SaveTxInfo(block, respCodes, logs); err != nil {
252253
return state, 0, err
253254
}
254255
}
@@ -684,3 +685,12 @@ func getResponseCodes(responses []*abci.ResponseDeliverTx) []uint32 {
684685
}
685686
return responseCodes
686687
}
688+
689+
// getLogs gets logs from a list of ResponseDeliverTx.
690+
func getLogs(responses []*abci.ResponseDeliverTx) []string {
691+
logs := make([]string, len(responses))
692+
for i, response := range responses {
693+
logs[i] = response.Log
694+
}
695+
return logs
696+
}

state/execution_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestApplyBlockWithBlockStore(t *testing.T) {
9393
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: block.MakePartSet(testPartSize).Header()}
9494

9595
// Check that SaveTxInfo is called with correct arguments
96-
blockStore.On("SaveTxInfo", block, mock.AnythingOfType("[]uint32")).Return(nil)
96+
blockStore.On("SaveTxInfo", block, mock.AnythingOfType("[]uint32"), mock.AnythingOfType("[]string")).Return(nil)
9797

9898
_, _, err = blockExec.ApplyBlock(state, blockID, block, nil)
9999
require.Nil(t, err)

state/mocks/block_store.go

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

state/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type BlockStore interface {
2626
LoadBlock(height int64) *types.Block
2727

2828
SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit)
29-
SaveTxInfo(block *types.Block, txResponseCode []uint32) error
29+
SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error
3030

3131
PruneBlocks(height int64) (uint64, error)
3232

store/store.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
dbm "github.com/cometbft/cometbft-db"
88
"github.com/gogo/protobuf/proto"
99

10+
abci "github.com/tendermint/tendermint/abci/types"
1011
cmtsync "github.com/tendermint/tendermint/libs/sync"
1112
cmtstore "github.com/tendermint/tendermint/proto/tendermint/store"
1213
cmtproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -450,11 +451,15 @@ func (bs *BlockStore) SaveSeenCommit(height int64, seenCommit *types.Commit) err
450451
return bs.db.Set(calcSeenCommitKey(height), seenCommitBytes)
451452
}
452453

453-
// SaveTxInfo indexes the txs from the block with the given response codes from execution.
454-
func (bs *BlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32) error {
454+
// SaveTxInfo indexes the txs from the block with the given response codes and logs from execution.
455+
// The logs are only saved for failed transactions.
456+
func (bs *BlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32, logs []string) error {
455457
if len(txResponseCodes) != len(block.Txs) {
456458
return fmt.Errorf("txResponseCodes length mismatch with block txs length")
457459
}
460+
if len(logs) != len(block.Txs) {
461+
return fmt.Errorf("logs length mismatch with block txs length")
462+
}
458463

459464
// Create a new batch
460465
batch := bs.db.NewBatch()
@@ -466,6 +471,10 @@ func (bs *BlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32) e
466471
Index: uint32(i),
467472
Code: txResponseCodes[i],
468473
}
474+
// Only save Logs for failed transactions
475+
if txResponseCodes[i] != abci.CodeTypeOK {
476+
txInfo.Log = logs[i]
477+
}
469478
txInfoBytes, err := proto.Marshal(&txInfo)
470479
if err != nil {
471480
return fmt.Errorf("unable to marshal tx: %w", err)

store/store_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,24 +384,27 @@ func TestSaveTxInfo(t *testing.T) {
384384

385385
// Create 1000 blocks
386386
txResponseCodes := make([]uint32, len(block.Txs))
387+
logs := make([]string, len(block.Txs))
387388
for h := int64(1); h <= 1000; h++ {
388389
block := makeBlock(h, state, new(types.Commit))
389390
partSet := block.MakePartSet(2)
390391
seenCommit := makeTestCommit(h, cmttime.Now())
391392
blockStore.SaveBlock(block, partSet, seenCommit)
392393

393-
// Set the response codes for the transactions
394+
// Set the response codes and logs for the transactions
394395
for i := range block.Txs {
395396
// If even set it to 0
396397
if i%2 == 0 {
397398
txResponseCodes[i] = 0
399+
logs[i] = "success"
398400
} else {
399401
txResponseCodes[i] = 1
402+
logs[i] = "failure"
400403
}
401404
}
402405

403406
// Save the tx info
404-
err := blockStore.SaveTxInfo(block, txResponseCodes)
407+
err := blockStore.SaveTxInfo(block, txResponseCodes, logs)
405408
require.NoError(t, err)
406409
}
407410

@@ -414,6 +417,12 @@ func TestSaveTxInfo(t *testing.T) {
414417
require.Equal(t, block.Height, txInfo.Height)
415418
require.Equal(t, uint32(i), txInfo.Index)
416419
require.Equal(t, txResponseCodes[i], txInfo.Code)
420+
// We don't save the logs for successful transactions
421+
if txResponseCodes[i] == 0 {
422+
require.Equal(t, "", txInfo.Log)
423+
} else {
424+
require.Equal(t, logs[i], txInfo.Log)
425+
}
417426
}
418427
}
419428

@@ -426,6 +435,7 @@ func TestSaveTxInfo(t *testing.T) {
426435
require.Equal(t, txInfo.Height, int64(777))
427436
require.Equal(t, uint32(1), txInfo.Code)
428437
require.Equal(t, uint32(5), txInfo.Index)
438+
require.Equal(t, "failure", txInfo.Log)
429439
}
430440

431441
func TestLoadBaseMeta(t *testing.T) {
@@ -597,7 +607,7 @@ func TestPruneBlocksPrunesTxs(t *testing.T) {
597607
partSet := block.MakePartSet(2)
598608
seenCommit := makeTestCommit(h, cmttime.Now())
599609
blockStore.SaveBlock(block, partSet, seenCommit)
600-
err := blockStore.SaveTxInfo(block, make([]uint32, len(block.Txs)))
610+
err := blockStore.SaveTxInfo(block, make([]uint32, len(block.Txs)), make([]string, len(block.Txs)))
601611
require.NoError(t, err)
602612
for _, tx := range block.Txs {
603613
indexedTxHashes = append(indexedTxHashes, tx.Hash())

0 commit comments

Comments
 (0)