package v1 import ( "fmt" "testing" "time" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/mempool" ) func TestTxStore_GetTxBySender(t *testing.T) { txs := NewTxStore() wtx := &WrappedTx{ tx: []byte("test_tx"), sender: "foo", priority: 1, timestamp: time.Now(), } res := txs.GetTxBySender(wtx.sender) require.Nil(t, res) txs.SetTx(wtx) res = txs.GetTxBySender(wtx.sender) require.NotNil(t, res) require.Equal(t, wtx, res) } func TestTxStore_GetTxByHash(t *testing.T) { txs := NewTxStore() wtx := &WrappedTx{ tx: []byte("test_tx"), sender: "foo", priority: 1, timestamp: time.Now(), } key := mempool.TxKey(wtx.tx) res := txs.GetTxByHash(key) require.Nil(t, res) txs.SetTx(wtx) res = txs.GetTxByHash(key) require.NotNil(t, res) require.Equal(t, wtx, res) } func TestTxStore_SetTx(t *testing.T) { txs := NewTxStore() wtx := &WrappedTx{ tx: []byte("test_tx"), priority: 1, timestamp: time.Now(), } key := mempool.TxKey(wtx.tx) txs.SetTx(wtx) res := txs.GetTxByHash(key) require.NotNil(t, res) require.Equal(t, wtx, res) wtx.sender = "foo" txs.SetTx(wtx) res = txs.GetTxByHash(key) require.NotNil(t, res) require.Equal(t, wtx, res) } func TestTxStore_GetOrSetPeerByTxHash(t *testing.T) { txs := NewTxStore() wtx := &WrappedTx{ tx: []byte("test_tx"), priority: 1, timestamp: time.Now(), } key := mempool.TxKey(wtx.tx) txs.SetTx(wtx) res, ok := txs.GetOrSetPeerByTxHash(mempool.TxKey([]byte("test_tx_2")), 15) require.Nil(t, res) require.False(t, ok) res, ok = txs.GetOrSetPeerByTxHash(key, 15) require.NotNil(t, res) require.False(t, ok) res, ok = txs.GetOrSetPeerByTxHash(key, 15) require.NotNil(t, res) require.True(t, ok) require.True(t, txs.TxHasPeer(key, 15)) require.False(t, txs.TxHasPeer(key, 16)) } func TestTxStore_RemoveTx(t *testing.T) { txs := NewTxStore() wtx := &WrappedTx{ tx: []byte("test_tx"), priority: 1, timestamp: time.Now(), } txs.SetTx(wtx) key := mempool.TxKey(wtx.tx) res := txs.GetTxByHash(key) require.NotNil(t, res) txs.RemoveTx(res) res = txs.GetTxByHash(key) require.Nil(t, res) } func TestTxStore_Size(t *testing.T) { txStore := NewTxStore() numTxs := 1000 for i := 0; i < numTxs; i++ { txStore.SetTx(&WrappedTx{ tx: []byte(fmt.Sprintf("test_tx_%d", i)), priority: int64(i), timestamp: time.Now(), }) } require.Equal(t, numTxs, txStore.Size()) }