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 6 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
417 changes: 236 additions & 181 deletions abci/types/types.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions proto/tendermint/abci/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ message TxResult {
uint32 index = 2;
bytes tx = 3;
ResponseDeliverTx result = 4 [(gogoproto.nullable) = false];
bytes original_hash = 5;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified abci.TxResult to include the original hash if the tx is malleated. This saves us from having to attempt to unwrap the malleated tx more than once. Alternatively, we could not cache the original hash, and unwrap the malleated transaction more than once.

adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
}

//----------------------------------------
Expand Down
19 changes: 15 additions & 4 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,22 @@ func fireEvents(
}

for i, tx := range block.Data.Txs {
var txHash []byte
var rawTx []byte
if originalHash, malleatedTx, ismalleated := types.UnwrapMalleatedTx(tx); ismalleated {
txHash = originalHash
rawTx = malleatedTx
} else {
txHash = tx.Hash()
rawTx = tx
}

if err := eventBus.PublishEventTx(types.EventDataTx{TxResult: abci.TxResult{
Height: block.Height,
Index: uint32(i),
Tx: tx,
Result: *(abciResponses.DeliverTxs[i]),
Height: block.Height,
Index: uint32(i),
Tx: rawTx,
Result: *(abciResponses.DeliverTxs[i]),
OriginalHash: txHash,
}}); err != nil {
logger.Error("failed publishing event TX", "err", err)
}
Expand Down
76 changes: 36 additions & 40 deletions state/txindex/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
dbm "github.com/tendermint/tm-db"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/state/indexer"
"github.com/tendermint/tendermint/state/txindex"
Expand Down Expand Up @@ -69,26 +70,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)
err := txi.indexResult(storeBatch, result)
if err != nil {
return err
}
Expand All @@ -105,26 +87,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)
err := txi.indexResult(b, result)
if err != nil {
return err
}
Expand Down Expand Up @@ -158,6 +121,39 @@ 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
if len(result.OriginalHash) == tmhash.Size {
hash = result.OriginalHash
} 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
45 changes: 45 additions & 0 deletions state/txindex/kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,51 @@ 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"))

tests := []test{
// we expect to get the malleated tx returned when searching using the original hash
{
tx: malleatedTx1,
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,
},
OriginalHash: tt.originalHash,
}
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
6 changes: 3 additions & 3 deletions types/event_bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/log"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
"github.com/tendermint/tendermint/libs/service"
Expand Down Expand Up @@ -178,9 +179,8 @@ func (b *EventBus) PublishEventTx(data EventDataTx) error {
ctx := context.Background()

var txHash []byte
if originalHash, malleated, ismalleated := UnwrapMalleatedTx(data.Tx); ismalleated {
txHash = originalHash
data.Tx = malleated
if len(data.OriginalHash) == tmhash.Size {
txHash = data.OriginalHash
} else {
txHash = Tx(data.Tx).Hash()
}
Expand Down
11 changes: 5 additions & 6 deletions types/event_bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ func TestEventBusPublishEventMalleatedTx(t *testing.T) {

tx := Tx("foo")
malleatedTx := Tx("foo-malleated")
wrappedMalleatedTx, err := WrapMalleatedTx(tx.Hash(), malleatedTx)
require.NoError(t, err)

result := abci.ResponseDeliverTx{
Data: []byte("bar"),
Expand All @@ -104,10 +102,11 @@ func TestEventBusPublishEventMalleatedTx(t *testing.T) {
}()

err = eventBus.PublishEventTx(EventDataTx{abci.TxResult{
Height: 1,
Index: 0,
Tx: wrappedMalleatedTx,
Result: result,
Height: 1,
Index: 0,
Tx: malleatedTx,
Result: result,
OriginalHash: tx.Hash(),
}})
assert.NoError(t, err)

Expand Down