Skip to content

Commit

Permalink
Add-guard-against-deposit-transaction (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil authored Sep 17, 2024
1 parent f05f5e2 commit 8b0ac2e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func AdaptCosmosTxsToEthTxs(cosmosTxs bfttypes.Txs) (ethtypes.Transactions, erro
return ethtypes.Transactions{}, nil
}
txsBytes := cosmosTxs.ToSliceOfBytes()
ethTxs, err := getDepositTxs(txsBytes)
ethTxs, err := GetDepositTxs(txsBytes)
if err != nil {
return nil, fmt.Errorf("get deposit txs: %v", err)
}
Expand All @@ -122,7 +122,7 @@ func AdaptCosmosTxsToEthTxs(cosmosTxs bfttypes.Txs) (ethtypes.Transactions, erro
return ethTxs, nil
}

func getDepositTxs(txsBytes [][]byte) (ethtypes.Transactions, error) {
func GetDepositTxs(txsBytes [][]byte) (ethtypes.Transactions, error) {
cosmosEthTx := new(sdktx.Tx)
if err := cosmosEthTx.Unmarshal(txsBytes[0]); err != nil {
return nil, fmt.Errorf("unmarshal cosmos tx: %v", err)
Expand Down
8 changes: 8 additions & 0 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

comettypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/polymerdao/monomer"
"github.com/polymerdao/monomer/utils"
)

Expand Down Expand Up @@ -38,6 +39,13 @@ func (p *Pool) Enqueue(userTxn comettypes.Tx) (err error) {
// Unfortunately, comet's DB interface doesn't support it.
// Moving to a different DB interface is left for future work.

// Attempt to adapt the Cosmos transaction to an Ethereum deposit transaction.
// If the adaptation succeeds, it indicates that the
// user transaction is a deposit transaction, which is not allowed in the pool.
if _, err := monomer.GetDepositTxs([][]byte{userTxn}); err == nil {
return errors.New("deposit txs are not allowed in the pool")
}

batch := p.db.NewBatch()
defer func() {
err = utils.WrapCloseErr(err, batch)
Expand Down
12 changes: 12 additions & 0 deletions mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

comettypes "github.com/cometbft/cometbft/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/polymerdao/monomer"
"github.com/polymerdao/monomer/mempool"
"github.com/polymerdao/monomer/testutils"
"github.com/stretchr/testify/assert"
Expand All @@ -22,6 +24,16 @@ func TestMempool(t *testing.T) {
require.Error(t, err)
})

t.Run("deposit transaction", func(t *testing.T) {
_, depositTx, _ := testutils.GenerateEthTxs(t)
depositTxBytes, err := depositTx.MarshalBinary()
require.NoError(t, err)

cosmosTxs, err := monomer.AdaptPayloadTxsToCosmosTxs([]hexutil.Bytes{depositTxBytes}, nil, "")
require.NoError(t, err)
require.ErrorContains(t, pool.Enqueue(cosmosTxs[0]), "deposit txs are not allowed in the pool")
})

// enqueue multiple to empty
for i := byte(0); i < 3; i++ {
require.NoError(t, pool.Enqueue(comettypes.Tx([]byte{i})))
Expand Down

0 comments on commit 8b0ac2e

Please sign in to comment.