commit 719db6b9b30ac90a6a9576c9166c8818f45f3c5a
parent 6da26e02faa13a9e5545014a9a83e6a4d5e574a8
Author: nolash <dev@holbrook.no>
Date: Fri, 16 Apr 2021 07:37:44 +0200
Detach store
Diffstat:
6 files changed, 56 insertions(+), 25 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,3 @@
+inc.sh
+__pycache__
+*.pyc
diff --git a/crypto_account_cache/cache.py b/crypto_account_cache/cache.py
@@ -0,0 +1,33 @@
+# standard imports
+import os
+
+# external imports
+from moolb import Bloom
+
+# local imports
+from .name import for_label
+from .store import FileStore
+
+
+class CryptoCache:
+
+ rounds = 3
+
+ def __init__(self, chain_spec, bits_size, bloom=None):
+ if bloom == None:
+ bloom = CryptoCache.__bloom_for_size(bits_size)
+ self.chain_spec = chain_spec
+ self.bloom = bloom
+ self.bits_size = bits_size
+ self.store = None
+
+
+ def set_store(self, store):
+ self.store = store
+ if not store.initd():
+ self.store.dump(self.bloom.to_bytes())
+
+
+ @staticmethod
+ def __bloom_for_size(bits_size):
+ return Bloom(bits_size, CryptoCache.rounds)
diff --git a/crypto_account_cache/file.py b/crypto_account_cache/file.py
@@ -1,15 +0,0 @@
-# local imports
-from .name import for_label
-
-def create_for_digest(chain_spec, digest, size, salt, base_dir=None):
-
- filename = for_label(chain_spec, digest, salt)
- bloom_filter = b'\x00' * size
-
- f = open(filename, 'wb')
- c = 0
- while c < size:
- c += f.write(bloom_filter[c:])
- f.close()
-
- return (filename, bloom_filter)
diff --git a/crypto_account_cache/name.py b/crypto_account_cache/name.py
@@ -2,16 +2,12 @@
import hashlib
-def for_label(chain_spec, account, salt):
-
+def for_label(chain_spec, label, salt):
chain_str = str(chain_spec)
h = hashlib.new('sha256')
h.update(chain_str.encode('utf-8'))
- h.update(account)
+ h.update(label)
h.update(salt)
z = h.digest()
return z.hex()
-
-
-
diff --git a/tests/base.py b/tests/base.py
@@ -3,13 +3,15 @@ import os
import unittest
import logging
import tempfile
+import shutil
+
+BLOOM_BITS = 1024 * 1024 * 8
script_dir = os.path.realpath(os.path.dirname(__file__))
data_dir = os.path.join(script_dir, 'testdata')
logg = logging.getLogger().getChild(__name__)
-BLOOM_SIZE = 1024 * 1024
class TestBase(unittest.TestCase):
@@ -17,4 +19,10 @@ class TestBase(unittest.TestCase):
os.makedirs(data_dir, exist_ok=True)
self.salt = os.urandom(32)
self.session_data_dir = tempfile.mkdtemp(dir=data_dir)
- self.size = BLOOM_SIZE
+ self.bits_size = BLOOM_BITS
+ self.bytes_size = (BLOOM_BITS - 1) / 8 + 1
+
+
+
+ def tearDown(self):
+ shutil.rmtree(self.session_data_dir)
diff --git a/tests/test_basic.py b/tests/test_basic.py
@@ -6,7 +6,9 @@ import os
from chainlib.chain import ChainSpec
# local imports
-from crypto_account_cache.file import create_for_digest
+from crypto_account_cache.name import for_label
+from crypto_account_cache.cache import CryptoCache
+from crypto_account_cache.store import FileStore
# test imports
from tests.base import TestBase
@@ -17,7 +19,11 @@ class TestBasic(TestBase):
chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
account = os.urandom(20)
- create_for_digest(chain_spec, account, self.size, self.salt, base_dir=self.session_data_dir)
+ filename = for_label(chain_spec, account, self.salt)
+ filepath = os.path.join(self.session_data_dir, filename)
+ store = FileStore(filepath)
+
+ cache = CryptoCache(chain_spec, self.bits_size)
if __name__ == '__main__':