From 80bfbd265e96b85c21a678e9611b0decd1d9bdb6 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Thu, 22 Jul 2021 08:33:17 -0400 Subject: [PATCH] Add `count_unblinded_tokens` to the database layer --- src/_zkapauthorizer/model.py | 17 +++++++++++++++++ src/_zkapauthorizer/tests/test_model.py | 14 +++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/_zkapauthorizer/model.py b/src/_zkapauthorizer/model.py index 4f8b051..4b2dd85 100644 --- a/src/_zkapauthorizer/model.py +++ b/src/_zkapauthorizer/model.py @@ -558,6 +558,23 @@ class VoucherStore(object): in texts ) + @with_cursor + def count_unblinded_tokens(self, cursor): + """ + Return the largest number of unblinded tokens that can be requested from + ``get_unblinded_tokens`` without causing it to raise + ``NotEnoughTokens``. + """ + cursor.execute( + """ + SELECT count(1) + FROM [unblinded-tokens] + WHERE [token] NOT IN [in-use] + """, + ) + (count,) = cursor.fetchone() + return count + @with_cursor def discard_unblinded_tokens(self, cursor, unblinded_tokens): """ diff --git a/src/_zkapauthorizer/tests/test_model.py b/src/_zkapauthorizer/tests/test_model.py index 6a0d445..a2a8e5a 100644 --- a/src/_zkapauthorizer/tests/test_model.py +++ b/src/_zkapauthorizer/tests/test_model.py @@ -739,14 +739,26 @@ class UnblindedTokenStoreTests(TestCase): ) def test_unblinded_tokens_round_trip(self, get_config, now, voucher_value, public_key, completed, data): """ - Unblinded tokens that are added to the store can later be retrieved. + Unblinded tokens that are added to the store can later be retrieved and counted. """ random_tokens, unblinded_tokens = paired_tokens(data) store = self.useFixture(TemporaryVoucherStore(get_config, lambda: now)).store store.add(voucher_value, len(random_tokens), 0, lambda: random_tokens) store.insert_unblinded_tokens_for_voucher(voucher_value, public_key, unblinded_tokens, completed) + + # All the tokens just inserted should be counted. + self.expectThat( + store.count_unblinded_tokens(), + Equals(len(unblinded_tokens)), + ) retrieved_tokens = store.get_unblinded_tokens(len(random_tokens)) + # All the tokens just extracted should not be counted. + self.expectThat( + store.count_unblinded_tokens(), + Equals(0), + ) + self.expectThat( set(unblinded_tokens), Equals(set(retrieved_tokens)), -- GitLab