commit 6da26e02faa13a9e5545014a9a83e6a4d5e574a8
Author: nolash <dev@holbrook.no>
Date: Fri, 16 Apr 2021 06:53:39 +0200
Initial commit
Diffstat:
5 files changed, 79 insertions(+), 0 deletions(-)
diff --git a/crypto_account_cache/file.py b/crypto_account_cache/file.py
@@ -0,0 +1,15 @@
+# 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
@@ -0,0 +1,17 @@
+# standard imports
+import hashlib
+
+
+def for_label(chain_spec, account, salt):
+
+ chain_str = str(chain_spec)
+ h = hashlib.new('sha256')
+ h.update(chain_str.encode('utf-8'))
+ h.update(account)
+ h.update(salt)
+ z = h.digest()
+
+ return z.hex()
+
+
+
diff --git a/requirements.txt b/requirements.txt
@@ -0,0 +1,3 @@
+chainsyncer==0.0.2a2
+chainlib==0.0.2a15
+moolb==0.1.1b2
diff --git a/tests/base.py b/tests/base.py
@@ -0,0 +1,20 @@
+# standard imports
+import os
+import unittest
+import logging
+import tempfile
+
+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):
+
+ def setUp(self):
+ 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
diff --git a/tests/test_basic.py b/tests/test_basic.py
@@ -0,0 +1,24 @@
+# standard imports
+import unittest
+import os
+
+# external imports
+from chainlib.chain import ChainSpec
+
+# local imports
+from crypto_account_cache.file import create_for_digest
+
+# test imports
+from tests.base import TestBase
+
+class TestBasic(TestBase):
+
+ def test_create_file(self):
+ 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)
+
+
+if __name__ == '__main__':
+ unittest.main()