Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve error handling in LoadTxInfo #1571

Open
wants to merge 5 commits into
base: v0.34.x-celestia
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,20 +561,25 @@ func LoadBlockStoreState(db dbm.DB) cmtstore.BlockStoreState {
}

// LoadTxInfo loads the TxInfo from disk given its hash.
func (bs *BlockStore) LoadTxInfo(txHash []byte) *cmtstore.TxInfo {
// Returns err notfound if the transaction is not found.
func (bs *BlockStore) LoadTxInfo(txHash []byte) (*cmtstore.TxInfo, error) {
if len(txHash) == 0 {
return nil, fmt.Errorf("cannot load tx info for empty txHash")
}

bz, err := bs.db.Get(calcTxHashKey(txHash))
if err != nil {
panic(err)
return nil, fmt.Errorf("failed to get tx info from db: %w", err)
rootulp marked this conversation as resolved.
Show resolved Hide resolved
}
if len(bz) == 0 {
return nil
return nil, fmt.Errorf("transaction not found")
}

var txi cmtstore.TxInfo
if err = proto.Unmarshal(bz, &txi); err != nil {
panic(fmt.Errorf("unmarshal to TxInfo failed: %w", err))
return nil, fmt.Errorf("failed to unmarshal tx info: %w", err)
}
return &txi
return &txi, nil
}

// mustEncode proto encodes a proto.message and panics if fails
Expand Down
133 changes: 128 additions & 5 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ func TestSaveTxInfo(t *testing.T) {
block := blockStore.LoadBlock(h)
// Check that transactions exist in the block
for i, tx := range block.Txs {
txInfo := blockStore.LoadTxInfo(tx.Hash())
txInfo, err := blockStore.LoadTxInfo(tx.Hash())
require.NoError(t, err)
require.NotNil(t, txInfo)
require.Equal(t, block.Height, txInfo.Height)
require.Equal(t, uint32(i), txInfo.Index)
require.Equal(t, allTxResponseCodes[txIndex], txInfo.Code)
Expand All @@ -435,7 +437,9 @@ func TestSaveTxInfo(t *testing.T) {
// Get a random transaction and make sure it's indexed properly
block := blockStore.LoadBlock(7)
tx := block.Txs[0]
txInfo := blockStore.LoadTxInfo(tx.Hash())
txInfo, err := blockStore.LoadTxInfo(tx.Hash())
require.NoError(t, err)
require.NotNil(t, txInfo)
require.Equal(t, block.Height, txInfo.Height)
require.Equal(t, block.Height, int64(7))
require.Equal(t, txInfo.Height, int64(7))
Expand Down Expand Up @@ -625,7 +629,7 @@ func TestPruneBlocksPrunesTxs(t *testing.T) {

// Check that the saved txs exist in the block store.
for _, hash := range indexedTxHashes {
txInfo := blockStore.LoadTxInfo(hash)
txInfo, err := blockStore.LoadTxInfo(hash)
require.NoError(t, err)
require.NotNil(t, txInfo, "transaction was not saved in the database")
}
Expand All @@ -638,10 +642,13 @@ func TestPruneBlocksPrunesTxs(t *testing.T) {
// removed 11 blocks, each block has 1 tx so 11 txs should no longer
// exist in the db.
for i, hash := range indexedTxHashes {
txInfo := blockStore.LoadTxInfo(hash)
txInfo, err := blockStore.LoadTxInfo(hash)
if int64(i) < 11 {
require.Error(t, err)
require.Contains(t, err.Error(), "transaction not found")
require.Nil(t, txInfo)
} else {
require.NoError(t, err)
require.NotNil(t, txInfo)
}
}
Expand All @@ -651,7 +658,7 @@ func TestPruneBlocksPrunesTxs(t *testing.T) {
block := blockStore.LoadBlock(height)
for i, tx := range block.Txs {
hash := tx.Hash()
txInfo := blockStore.LoadTxInfo(hash)
txInfo, err := blockStore.LoadTxInfo(hash)
require.NoError(t, err)
require.NotNil(t, txInfo)
require.Equal(t, height, txInfo.Height)
Expand Down Expand Up @@ -776,3 +783,119 @@ func newBlock(hdr types.Header, lastCommit *types.Commit) *types.Block {
LastCommit: lastCommit,
}
}

func TestLoadTxInfoErrors(t *testing.T) {
config := cfg.ResetTestRoot("blockchain_reactor_test")
defer os.RemoveAll(config.RootDir)
bs := NewBlockStore(dbm.NewMemDB())

testCases := []struct {
name string
txHash []byte
expectedError string
dbError error
}{
{
name: "Empty txHash",
txHash: []byte{},
expectedError: "cannot load tx info for empty txHash",
},
{
name: "Non-existent tx",
txHash: []byte("non_existent_hash"),
expectedError: "transaction not found", // no error, just nil result
tiendn marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: "Corrupted data",
txHash: []byte("corrupted_hash"),
expectedError: "failed to unmarshal tx info",
},
{
name: "Database error",
txHash: []byte("error_hash"),
dbError: fmt.Errorf("mock db error"),
expectedError: "failed to get tx info from db: mock db error",
},
}

// Test corrupted data case by manually inserting invalid data
err := bs.db.Set(calcTxHashKey([]byte("corrupted_hash")), []byte("invalid_data"))
require.NoError(t, err)

// Create a BlockStore with mock DB for testing database errors
bsWithMockDB := &BlockStore{
db: &mockDBWithError{},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.dbError != nil {
// Use mock DB that returns an error
bsWithMockDB.db = &mockDBWithError{err: tc.dbError}
txInfo, err := bsWithMockDB.LoadTxInfo(tc.txHash)
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedError)
require.Nil(t, txInfo)
} else {
// Use regular DB
txInfo, err := bs.LoadTxInfo(tc.txHash)
if tc.expectedError != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedError)
require.Nil(t, txInfo)
} else {
require.NoError(t, err)
require.Nil(t, txInfo)
}
}
})
}
}

// mockDBWithError implements a mock database that returns errors
type mockDBWithError struct {
dbm.DB
err error
}

func (m *mockDBWithError) Get(key []byte) ([]byte, error) {
return nil, m.err
}

func TestLoadTxInfoNotFound(t *testing.T) {
bs := NewBlockStore(dbm.NewMemDB())

// Test cases for "not found" scenarios
tiendn marked this conversation as resolved.
Show resolved Hide resolved
testCases := []struct {
name string
txHash []byte
setup func(db dbm.DB)
}{
{
name: "completely non-existent key",
txHash: []byte("non_existent_hash"),
},
{
name: "key exists but value is empty",
txHash: []byte("empty_value_hash"),
setup: func(db dbm.DB) {
err := db.Set(calcTxHashKey([]byte("empty_value_hash")), []byte{})
require.NoError(t, err)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.setup != nil {
tc.setup(bs.db)
}

// Both cases should return (nil, nil) to indicate "not found"
tiendn marked this conversation as resolved.
Show resolved Hide resolved
txInfo, err := bs.LoadTxInfo(tc.txHash)
require.Error(t, err)
require.Contains(t, err.Error(), "transaction not found")
require.Nil(t, txInfo)
})
}
}
Loading