Skip to content

Commit

Permalink
Add ability to remove "Parent" transactions from the mempool (#582)
Browse files Browse the repository at this point in the history
* add the new transaction wrapper

* export and move DecodeChildTx

* return a more precise type (Tx instead of []byte) for DecodeChildTx

* add test and helper function

* linter
  • Loading branch information
evan-forbes authored Nov 19, 2021
1 parent 7828d21 commit aa0e2a4
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 113 deletions.
2 changes: 2 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: v1beta1

build:
roots:
- proto
Expand Down
6 changes: 6 additions & 0 deletions mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,12 @@ func (mem *CListMempool) Update(
// https://github.com/tendermint/tendermint/issues/3322.
if e, ok := mem.txsMap.Load(TxKey(tx)); ok {
mem.removeTx(tx, e.(*clist.CElement), false)
// see if the transaction is a child transaction of a some parent
// transaction that exists in the mempool
} else if parentHash, _, isChild := types.DecodeChildTx(tx); isChild {
var parentKey [TxKeySize]byte
copy(parentKey[:], parentHash)
mem.RemoveTxByKey(parentKey, false)
}
}

Expand Down
23 changes: 23 additions & 0 deletions mempool/clist_mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,29 @@ func TestMempoolUpdate(t *testing.T) {
err = mempool.CheckTx([]byte{0x03}, nil, TxInfo{})
require.NoError(t, err)
}

// 4. Removes a parent transaction after receiving a child transaction in the update
{
mempool.Flush()
parentTx := []byte{1, 2, 3, 4}
childTx := []byte{1, 2}
parentHash := sha256.Sum256(parentTx)

// create the wrapped child transaction
wTx, err := types.WrapChildTx(parentHash[:], childTx)
require.NoError(t, err)

// add the parent transaction to the mempool
err = mempool.CheckTx(parentTx, nil, TxInfo{})
require.NoError(t, err)

// remove the parent from the mempool using the wrapped child tx
err = mempool.Update(1, []types.Tx{wTx}, abciResponses(1, abci.CodeTypeOK), nil, nil)
require.NoError(t, err)

assert.Zero(t, mempool.Size())

}
}

func TestMempool_KeepInvalidTxsInCache(t *testing.T) {
Expand Down
Loading

0 comments on commit aa0e2a4

Please sign in to comment.