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

Index malleated transactions properly #612

Merged
merged 8 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
83 changes: 37 additions & 46 deletions state/txindex/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
defer storeBatch.Close()

for _, result := range b.Ops {
hash := types.Tx(result.Tx).Hash()

// index tx by events
err := txi.indexEvents(result, hash, storeBatch)
if err != nil {
return err
}

// index by height (always)
err = storeBatch.Set(keyForHeight(result), hash)
if err != nil {
return err
}

rawBytes, err := proto.Marshal(result)
if err != nil {
return err
}
// index by hash (always)
err = storeBatch.Set(hash, rawBytes)
if err != nil {
return err
}
txi.indexResult(storeBatch, result)
}

return storeBatch.WriteSync()
Expand All @@ -105,29 +83,7 @@ func (txi *TxIndex) Index(result *abci.TxResult) error {
b := txi.store.NewBatch()
defer b.Close()

hash := types.Tx(result.Tx).Hash()

// index tx by events
err := txi.indexEvents(result, hash, b)
if err != nil {
return err
}

// index by height (always)
err = b.Set(keyForHeight(result), hash)
if err != nil {
return err
}

rawBytes, err := proto.Marshal(result)
if err != nil {
return err
}
// index by hash (always)
err = b.Set(hash, rawBytes)
if err != nil {
return err
}
txi.indexResult(b, result)

return b.WriteSync()
}
Expand Down Expand Up @@ -158,6 +114,41 @@ func (txi *TxIndex) indexEvents(result *abci.TxResult, hash []byte, store dbm.Ba
return nil
}

func (txi *TxIndex) indexResult(batch dbm.Batch, result *abci.TxResult) error {
var hash []byte
originalHash, unwrappedTx, isMalleated := types.UnwrapMalleatedTx(types.Tx(result.Tx))
if isMalleated {
hash = originalHash
result.Tx = unwrappedTx
} else {
hash = types.Tx(result.Tx).Hash()
}

rawBytes, err := proto.Marshal(result)
if err != nil {
return err
}

// index tx by events
err = txi.indexEvents(result, hash, batch)
if err != nil {
return err
}

// index by height (always)
err = batch.Set(keyForHeight(result), hash)
if err != nil {
return err
}

// index by hash (always)
err = batch.Set(hash, rawBytes)
if err != nil {
return err
}
return nil
}

// Search performs a search using the given query.
//
// It breaks the query into conditions (like "tx.height > 5"). For each
Expand Down
46 changes: 46 additions & 0 deletions state/txindex/kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,52 @@ func TestTxIndex(t *testing.T) {
assert.True(t, proto.Equal(txResult2, loadedTxResult2))
}

func TestMalleatedTxIndex(t *testing.T) {
type test struct {
tx types.Tx
originalHash []byte
expectedTx []byte
}
originalTx1 := types.Tx([]byte("ORIGINAL_TX"))
malleatedTx1 := types.Tx([]byte("MALLEATED_TX"))
wrappedTx, err := types.WrapMalleatedTx(originalTx1.Hash(), malleatedTx1)
require.NoError(t, err)

tests := []test{
// we expect to get the malleated tx returned when searching using the original hash
{
tx: wrappedTx,
originalHash: originalTx1.Hash(),
expectedTx: malleatedTx1,
},
}

indexer := NewTxIndex(db.NewMemDB())

for i, tt := range tests {

txResult := &abci.TxResult{
Height: int64(i),
Index: 0,
Tx: tt.tx,
Result: abci.ResponseDeliverTx{
Data: []byte{0},
Code: abci.CodeTypeOK, Log: "", Events: nil,
},
}
batch := txindex.NewBatch(1)
if err := batch.Add(txResult); err != nil {
t.Error(err)
}
err := indexer.AddBatch(batch)
require.NoError(t, err)

loadedTxResult, err := indexer.Get(tt.originalHash)
require.NoError(t, err)
assert.Equal(t, tt.expectedTx, loadedTxResult.Tx)
}
}

func TestTxSearch(t *testing.T) {
indexer := NewTxIndex(db.NewMemDB())

Expand Down