Remove Filtered Transactions From Queue

Christopher Vollick created

The previous code would successfully _skip_ the filtered items, as in it
wouldn't run the body of the map, but it would also just leave them
there, meaning they'd be there next time.

So it would successfully skip stuff, but that's not really what we
wanted...

This both skips and clears.

Change summary

lib/pending_transaction_repo.rb       | 19 ++++++++++++++++++-
test/test_pending_transaction_repo.rb | 10 ++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)

Detailed changes

lib/pending_transaction_repo.rb 🔗

@@ -136,7 +136,7 @@ class PendingTransactionRepo
 		end
 	end
 
-	def filter(txids_and_customer_ids)
+	def run_filters(txids_and_customer_ids)
 		@filters ||= [
 			IgnoredTransactionFilter.new(redis, @ignored_key),
 			WrongCustomerFilter.new(redis, @customer_address_template),
@@ -150,6 +150,23 @@ class PendingTransactionRepo
 		end
 	end
 
+	# run_filters removes the ones we don't care about from the list, but that
+	# list is just in memory. The rest of this removes it from the redis queue
+	# so we're not just skipping over these things every time in the list but
+	# otherwise leaving them there.
+	def filter(txids_and_customer_ids)
+		passed = run_filters(txids_and_customer_ids)
+
+		unless txids_and_customer_ids.length == passed.length
+			redis.hdel(
+				@key,
+				(txids_and_customer_ids - passed).map(&:first)
+			)
+		end
+
+		passed
+	end
+
 	def build_transaction(txid)
 		tx_hash, address = txid.split("/", 2)
 		txn = electrum.gettransaction(tx_hash)

test/test_pending_transaction_repo.rb 🔗

@@ -398,6 +398,11 @@ class TestPendingTransactionRepo < Minitest::Test
 			[["one/a", "1234"], ["two/a", "1234"], ["three/a", "1234"]],
 			["key"]
 		)
+		repo.redis.expect(
+			:hdel,
+			2,
+			["key", ["two/a", "three/a"]]
+		)
 		repo.electrum.expect(
 			:gettransaction,
 			FakeElectrumTransaction.new("one", 6, 0.5),
@@ -437,6 +442,11 @@ class TestPendingTransactionRepo < Minitest::Test
 			[["one/a", "1234"], ["two/a", "1234"], ["three/a", "1234"]],
 			["key"]
 		)
+		repo.redis.expect(
+			:hdel,
+			3,
+			["key", ["one/a", "two/a", "three/a"]]
+		)
 
 		v = repo.map { |pending, _customer_id|
 			pending.tx_hash