commit e4cc7061f0dc7c0a90bd0c0adbf8ed2896e35410
parent 92cb5d1978a5ec5579432c55c98dbc963c864fc4
Author: lash <dev@holbrook.no>
Date: Tue, 15 Mar 2022 08:06:39 +0000
Force hashing of tx inside puts
Diffstat:
5 files changed, 44 insertions(+), 43 deletions(-)
diff --git a/chainqueue/entry.py b/chainqueue/entry.py
@@ -8,6 +8,9 @@ from hexathon import (
uniform,
)
+# local imports
+from chainqueue.cache import CacheTx
+
logg = logging.getLogger(__name__)
@@ -18,13 +21,15 @@ def normalize_hex(k):
class QueueEntry:
- def __init__(self, store, tx_hash):
+ def __init__(self, store, tx_hash=None, cache_adapter=CacheTx):
self.store = store
- self.tx_hash = normalize_hex(tx_hash)
+ #self.tx_hash = normalize_hex(tx_hash)
+ self.tx_hash = tx_hash
self.signed_tx = None
self.seq = None
self.k = None
self.synced = False
+ self.cache_adapter = cache_adapter
def serialize(self):
@@ -33,8 +38,10 @@ class QueueEntry:
def create(self, signed_tx):
signed_tx = normalize_hex(signed_tx)
- self.k = self.store.put(self.tx_hash, signed_tx)
+ (s, tx_hash) = self.store.put(signed_tx, cache_adapter=self.cache_adapter)
+ self.k = s
self.synced = True
+ return tx_hash
def load(self):
diff --git a/chainqueue/store/base.py b/chainqueue/store/base.py
@@ -41,17 +41,18 @@ class Store:
setattr(self, v, getattr(self.state_store, v))
- def put(self, k, v, cache_adapter=CacheTx):
+ def put(self, v, cache_adapter=CacheTx):
+ tx = cache_adapter()
+ tx.deserialize(v)
+ k = tx.hash
n = self.counter.next()
t = datetime.datetime.now().timestamp()
s = to_key(t, n, k)
self.state_store.put(s, v)
self.index_store.put(k, s)
if self.cache != None:
- tx = cache_adapter()
- tx.deserialize(v)
self.cache.put(self.chain_spec, tx)
- return s
+ return (s, k,)
def get(self, k):
diff --git a/tests/test_entry.py b/tests/test_entry.py
@@ -11,6 +11,7 @@ from chainqueue import QueueEntry
# test imports
from tests.base_shep import TestShepBase
+from tests.common import MockCacheTokenTx
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
@@ -19,22 +20,21 @@ logg = logging.getLogger()
class TestEntry(TestShepBase):
def test_entry_get(self):
- tx_hash_one = add_0x(os.urandom(32).hex())
signed_tx = add_0x(os.urandom(128).hex())
nonce = 42
- entry = QueueEntry(self.store, tx_hash_one)
- entry.create(signed_tx)
+ entry = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
+ tx_hash_one = entry.create(signed_tx)
- tx_hash_two = add_0x(os.urandom(32).hex())
signed_tx = add_0x(os.urandom(128).hex())
nonce = 42
- entry = QueueEntry(self.store, tx_hash_two)
- entry.create(signed_tx)
+ entry = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
+ tx_hash_two = entry.create(signed_tx)
txs = self.store.by_state()
self.assertEqual(len(txs), 2)
-
- entry = QueueEntry(self.store, tx_hash_one)
+
+ logg.debug('tx hash one {}'.format(tx_hash_one))
+ entry = QueueEntry(self.store, tx_hash=tx_hash_one, cache_adapter=MockCacheTokenTx)
entry.load()
entry.sent()
diff --git a/tests/test_integrate.py b/tests/test_integrate.py
@@ -42,20 +42,18 @@ class TestIntegrateBase(TestShepBase):
def test_integration_valid(self):
- self.store.put(os.urandom(4).hex(), os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+ self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
def test_state_default(self):
- hx = os.urandom(4).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
v = self.store.pending()
self.assertEqual(len(v), 1)
self.assertEqual(v[0], hx)
def test_state_enqueue(self):
- hx = os.urandom(4).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.get(hx)
self.store.enqueue(hx)
v = self.store.upcoming()
@@ -65,8 +63,7 @@ class TestIntegrateBase(TestShepBase):
def test_state_defer(self):
- hx = os.urandom(4).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx)
self.store.fail(hx)
v = self.store.deferred()
@@ -75,12 +72,11 @@ class TestIntegrateBase(TestShepBase):
def test_state_multiple(self):
- hx = os.urandom(4).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx)
self.store.fail(hx)
- hx = os.urandom(8).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx)
self.store.fail(hx)
v = self.store.deferred()
@@ -88,33 +84,30 @@ class TestIntegrateBase(TestShepBase):
def test_state_multiple_sort(self):
- hx = os.urandom(4).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx)
self.store.fail(hx)
- hx = os.urandom(4).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.enqueue(hx)
- hx = os.urandom(4).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx)
self.store.fail(hx)
- hx = os.urandom(4).hex()
- self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+
+ self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
v = self.store.deferred()
self.assertEqual(len(v), 2)
def test_state_date_threshold(self):
- hx = os.urandom(4).hex()
- s = self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx)
self.store.fail(hx)
then = self.store.modified(s)
time.sleep(0.1)
- hx = os.urandom(4).hex()
- s = self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
+ (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx)
self.store.fail(hx)
diff --git a/tests/test_shep.py b/tests/test_shep.py
@@ -15,6 +15,7 @@ from chainqueue import QueueEntry
# test imports
from tests.base_shep import TestShepBase
+from tests.common import MockCacheTokenTx
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
@@ -27,13 +28,12 @@ class TestShep(TestShepBase):
def test_shep_tx(self):
- tx_hash = add_0x(os.urandom(20).hex())
signed_tx = add_0x(os.urandom(128).hex())
nonce = 42
- tx = QueueEntry(self.store, tx_hash)
- tx.create(signed_tx)
+ tx = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
+ tx_hash = tx.create(signed_tx)
- tx_retrieved = QueueEntry(self.store, tx_hash)
+ tx_retrieved = QueueEntry(self.store, tx_hash=tx_hash)
tx_retrieved.load()
self.assertEqual(tx_retrieved.signed_tx, strip_0x(signed_tx))
@@ -52,7 +52,7 @@ class TestShep(TestShepBase):
def test_shep_cache(self):
- self.store.put('foo', 'bar')
+ self.store.put('bar', cache_adapter=MockCacheTokenTx)
if __name__ == '__main__':