WIP crossroads on hex vs bytes interpretation

This commit is contained in:
lash 2022-03-12 14:12:02 +00:00
parent 0c9b42d086
commit e457275128
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 68 additions and 49 deletions

View File

@ -36,8 +36,10 @@ class Store:
for v in ['state', 'change', 'set', 'unset']: for v in ['state', 'change', 'set', 'unset']:
setattr(self, v, getattr(self.state_store, v)) setattr(self, v, getattr(self.state_store, v))
logg.debug('cache {}'.format(cache))
def put(self, k, v, cache_adapter=CacheTx()):
def put(self, k, v, cache_adapter=CacheTx):
n = self.counter.next() n = self.counter.next()
t = datetime.datetime.now().timestamp() t = datetime.datetime.now().timestamp()
s = to_key(t, n, k) s = to_key(t, n, k)

View File

@ -1,5 +1,11 @@
# standard imports
import hashlib
# local imports # local imports
from chainqueue.cache import Cache from chainqueue.cache import (
Cache,
CacheTokenTx,
)
class MockCounter: class MockCounter:
@ -38,3 +44,47 @@ class MockTokenCache(Cache):
def count(self, cache_filter): def count(self, cache_filter):
self.last_filter = cache_filter self.last_filter = cache_filter
class MockCacheTokenTx(CacheTokenTx):
def deserialize(self, signed_tx):
h = hashlib.sha1()
h.update(signed_tx + b'\x01')
z = h.digest()
nonce = int.from_bytes(z[:4], 'big')
token_value = int.from_bytes(z[4:8], 'big')
value = int.from_bytes(z[8:12], 'big')
h = hashlib.sha1()
h.update(z)
z = h.digest()
sender = z.hex()
h = hashlib.sha1()
h.update(z)
z = h.digest()
recipient = z.hex()
h = hashlib.sha1()
h.update(z)
z = h.digest()
token = z.hex()
h = hashlib.sha256()
h.update(z)
z = h.digest()
tx_hash = z.hex()
#tx = CacheTokenTx(normalizer=self.normalizer)
self.init(tx_hash, nonce, sender, recipient, value)
self.set('src_token', token)
self.set('dst_token', token)
self.set('src_value', token_value)
self.set('dst_value', token_value)
self.confirm(42, 13, 1024000)
return self

View File

@ -2,7 +2,6 @@
import os import os
import logging import logging
import unittest import unittest
import hashlib
import math import math
# external imports # external imports
@ -18,53 +17,15 @@ from chainqueue.cache import (
# test imports # test imports
from tests.base_shep import TestShepBase from tests.base_shep import TestShepBase
from tests.common import MockTokenCache from tests.common import (
MockTokenCache,
MockTokenCacheTx,
)
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
class MockCacheTokenTx(CacheTokenTx):
def deserialize(self, signed_tx):
h = hashlib.sha1()
h.update(signed_tx + b'\x01')
z = h.digest()
nonce = int.from_bytes(z[:4], 'big')
token_value = int.from_bytes(z[4:8], 'big')
value = int.from_bytes(z[8:12], 'big')
h = hashlib.sha1()
h.update(z)
z = h.digest()
sender = z.hex()
h = hashlib.sha1()
h.update(z)
z = h.digest()
recipient = z.hex()
h = hashlib.sha1()
h.update(z)
z = h.digest()
token = z.hex()
h = hashlib.sha256()
h.update(z)
z = h.digest()
tx_hash = z.hex()
#tx = CacheTokenTx(normalizer=self.normalizer)
self.init(tx_hash, nonce, sender, recipient, value)
self.set('src_token', token)
self.set('dst_token', token)
self.set('src_value', token_value)
self.set('dst_value', token_value)
self.confirm(42, 13, 1024000)
return self
class MockNormalizer: class MockNormalizer:
def address(self, v): def address(self, v):

View File

@ -1,6 +1,7 @@
# standard imports # standard imports
import tempfile import tempfile
import unittest import unittest
import logging
# external imports # external imports
from shep.store.file import SimpleFileStoreFactory from shep.store.file import SimpleFileStoreFactory
@ -15,8 +16,13 @@ from chainqueue import (
# test imports # test imports
from tests.common import ( from tests.common import (
MockCounter, MockCounter,
MockTokenCache MockTokenCache,
MockCacheTokenTx,
) )
from tests.base_shep import TestShepBase
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class MockContentStore: class MockContentStore:
@ -33,7 +39,7 @@ class MockContentStore:
return self.store.get(k) return self.store.get(k)
class TestShepBase(unittest.TestCase): class TestIntegrateBase(TestShepBase):
def setUp(self): def setUp(self):
self.path = tempfile.mkdtemp() self.path = tempfile.mkdtemp()
@ -46,8 +52,8 @@ class TestShepBase(unittest.TestCase):
self.store = Store(chain_spec, self.state, content_store, counter, cache=self.cache) self.store = Store(chain_spec, self.state, content_store, counter, cache=self.cache)
def test_basic(self): def test_integration_valid(self):
pass self.store.put(b'foo'.hex(), b'bar'.hex(), cache_adapter=MockCacheTokenTx)
if __name__ == '__main__': if __name__ == '__main__':