Skip to content

Commit 60930ab

Browse files
authoredAug 18, 2022
feat: add share index to malleated (wrapped) txs (celestiaorg#819)
* feat: add share index to malleated (wrapped) txs pass the share index when wrapping * chore: refactor unrwap function to return the entire malleated tx * fix compile issue

File tree

8 files changed

+169
-132
lines changed

8 files changed

+169
-132
lines changed
 

‎mempool/v0/clist_mempool.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,9 @@ func (mem *CListMempool) Update(
616616
mem.removeTx(tx, e.(*clist.CElement), false)
617617
// see if the transaction is a malleated transaction of a some parent
618618
// transaction that exists in the mempool
619-
} else if parentHash, _, isMalleated := types.UnwrapMalleatedTx(tx); isMalleated {
619+
} else if malleatedTx, isMalleated := types.UnwrapMalleatedTx(tx); isMalleated {
620620
var parentKey [types.TxKeySize]byte
621-
copy(parentKey[:], parentHash)
621+
copy(parentKey[:], malleatedTx.OriginalTxHash)
622622
err := mem.RemoveTxByKey(parentKey)
623623
if err != nil {
624624
return err

‎mempool/v1/mempool.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ func (txmp *TxMempool) Update(
406406

407407
// Regardless of outcome, remove the transaction from the mempool.
408408
if err := txmp.removeTxByKey(originalKey); err != nil {
409-
if originalHash, _, isMalleated := types.UnwrapMalleatedTx(tx); isMalleated {
410-
copy(originalKey[:], originalHash)
409+
if malleatedTx, isMalleated := types.UnwrapMalleatedTx(tx); isMalleated {
410+
copy(originalKey[:], malleatedTx.OriginalTxHash)
411411
_ = txmp.removeTxByKey(originalKey)
412412
}
413413
}

‎mempool/v1/mempool_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ func TestMalleatedTxRemoval(t *testing.T) {
661661
originalHash := sha256.Sum256(originalTx)
662662

663663
// create the wrapped child transaction
664-
wTx, err := types.WrapMalleatedTx(originalHash[:], malleatedTx)
664+
wTx, err := types.WrapMalleatedTx(originalHash[:], 0, malleatedTx)
665665
require.NoError(t, err)
666666

667667
// add the parent transaction to the mempool

‎proto/tendermint/types/types.pb.go

+148-112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎proto/tendermint/types/types.proto

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ message TxProof {
224224
message MalleatedTx {
225225
bytes original_tx_hash = 1;
226226
bytes tx = 2;
227+
uint32 share_index = 3;
227228
}
228229

229230
// Proof represents proof of a namespace.ID in an NMT.

‎state/execution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ func fireEvents(
584584
for i, tx := range block.Data.Txs {
585585
var txHash []byte
586586
var rawTx []byte
587-
if originalHash, malleatedTx, ismalleated := types.UnwrapMalleatedTx(tx); ismalleated {
588-
txHash = originalHash
589-
rawTx = malleatedTx
587+
if malleatedTx, ismalleated := types.UnwrapMalleatedTx(tx); ismalleated {
588+
txHash = malleatedTx.OriginalTxHash
589+
rawTx = malleatedTx.Tx
590590
} else {
591591
txHash = tx.Hash()
592592
rawTx = tx

‎types/tx.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -220,30 +220,30 @@ func ComputeProtoSizeForTxs(txs []Tx) int64 {
220220
// passed is not a tmproto.MalleatedTx, since the schema for PayForMessage is kept
221221
// in the app, we cannot perform further checks without creating an import
222222
// cycle.
223-
func UnwrapMalleatedTx(tx Tx) (originalHash []byte, unwrapped Tx, isMalleated bool) {
223+
func UnwrapMalleatedTx(tx Tx) (malleatedTx tmproto.MalleatedTx, isMalleated bool) {
224224
// attempt to unmarshal into a a malleated transaction
225-
var malleatedTx tmproto.MalleatedTx
226225
err := proto.Unmarshal(tx, &malleatedTx)
227226
if err != nil {
228-
return nil, nil, false
227+
return malleatedTx, false
229228
}
230229
// this check will fail to catch unwanted types should those unmarshalled
231230
// types happen to have a hash sized slice of bytes in the same field number
232231
// as originalTxHash. TODO(evan): either fix this, or better yet use a different
233232
// mechanism
234233
if len(malleatedTx.OriginalTxHash) != tmhash.Size {
235-
return nil, nil, false
234+
return malleatedTx, false
236235
}
237-
return malleatedTx.OriginalTxHash, malleatedTx.Tx, true
236+
return malleatedTx, true
238237
}
239238

240239
// WrapMalleatedTx creates a wrapped Tx that includes the original transaction's hash
241240
// so that it can be easily removed from the mempool. note: must be unwrapped to
242241
// be a viable sdk.Tx
243-
func WrapMalleatedTx(originalHash []byte, malleated Tx) (Tx, error) {
242+
func WrapMalleatedTx(originalHash []byte, shareIndex uint32, malleated Tx) (Tx, error) {
244243
wTx := tmproto.MalleatedTx{
245244
OriginalTxHash: originalHash,
246245
Tx: malleated,
246+
ShareIndex: shareIndex,
247247
}
248248
return proto.Marshal(&wTx)
249249
}

‎types/tx_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestUnwrapMalleatedTx(t *testing.T) {
4848
// perform a simple test for being unable to decode a non
4949
// malleated transaction
5050
tx := Tx{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
51-
_, _, ok := UnwrapMalleatedTx(tx)
51+
_, ok := UnwrapMalleatedTx(tx)
5252
require.False(t, ok)
5353

5454
data := Data{
@@ -81,16 +81,16 @@ func TestUnwrapMalleatedTx(t *testing.T) {
8181

8282
// due to protobuf not actually requiring type compatibility
8383
// we need to make sure that there is some check
84-
_, _, ok = UnwrapMalleatedTx(rawBlock)
84+
_, ok = UnwrapMalleatedTx(rawBlock)
8585
require.False(t, ok)
8686

8787
pHash := sha256.Sum256(rawBlock)
88-
MalleatedTx, err := WrapMalleatedTx(pHash[:], rawBlock)
88+
MalleatedTx, err := WrapMalleatedTx(pHash[:], 0, rawBlock)
8989
require.NoError(t, err)
9090

9191
// finally, ensure that the unwrapped bytes are identical to the input
92-
unwrappedHash, unwrapped, ok := UnwrapMalleatedTx(MalleatedTx)
92+
malleated, ok := UnwrapMalleatedTx(MalleatedTx)
9393
require.True(t, ok)
94-
assert.Equal(t, 32, len(unwrappedHash))
95-
require.Equal(t, rawBlock, []byte(unwrapped))
94+
assert.Equal(t, 32, len(malleated.OriginalTxHash))
95+
require.Equal(t, rawBlock, malleated.Tx)
9696
}

0 commit comments

Comments
 (0)
Please sign in to comment.