Force hashing of tx inside puts

This commit is contained in:
lash 2022-03-15 08:06:39 +00:00
parent 92cb5d1978
commit e4cc7061f0
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 44 additions and 43 deletions

View File

@ -8,6 +8,9 @@ from hexathon import (
uniform, uniform,
) )
# local imports
from chainqueue.cache import CacheTx
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
@ -18,13 +21,15 @@ def normalize_hex(k):
class QueueEntry: class QueueEntry:
def __init__(self, store, tx_hash): def __init__(self, store, tx_hash=None, cache_adapter=CacheTx):
self.store = store 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.signed_tx = None
self.seq = None self.seq = None
self.k = None self.k = None
self.synced = False self.synced = False
self.cache_adapter = cache_adapter
def serialize(self): def serialize(self):
@ -33,8 +38,10 @@ class QueueEntry:
def create(self, signed_tx): def create(self, signed_tx):
signed_tx = normalize_hex(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 self.synced = True
return tx_hash
def load(self): def load(self):

View File

@ -41,17 +41,18 @@ class Store:
setattr(self, v, getattr(self.state_store, v)) 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() 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)
self.state_store.put(s, v) self.state_store.put(s, v)
self.index_store.put(k, s) self.index_store.put(k, s)
if self.cache != None: if self.cache != None:
tx = cache_adapter()
tx.deserialize(v)
self.cache.put(self.chain_spec, tx) self.cache.put(self.chain_spec, tx)
return s return (s, k,)
def get(self, k): def get(self, k):

View File

@ -11,6 +11,7 @@ from chainqueue import QueueEntry
# test imports # test imports
from tests.base_shep import TestShepBase from tests.base_shep import TestShepBase
from tests.common import MockCacheTokenTx
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
@ -19,22 +20,21 @@ logg = logging.getLogger()
class TestEntry(TestShepBase): class TestEntry(TestShepBase):
def test_entry_get(self): def test_entry_get(self):
tx_hash_one = add_0x(os.urandom(32).hex())
signed_tx = add_0x(os.urandom(128).hex()) signed_tx = add_0x(os.urandom(128).hex())
nonce = 42 nonce = 42
entry = QueueEntry(self.store, tx_hash_one) entry = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
entry.create(signed_tx) tx_hash_one = entry.create(signed_tx)
tx_hash_two = add_0x(os.urandom(32).hex())
signed_tx = add_0x(os.urandom(128).hex()) signed_tx = add_0x(os.urandom(128).hex())
nonce = 42 nonce = 42
entry = QueueEntry(self.store, tx_hash_two) entry = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
entry.create(signed_tx) tx_hash_two = entry.create(signed_tx)
txs = self.store.by_state() txs = self.store.by_state()
self.assertEqual(len(txs), 2) 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.load()
entry.sent() entry.sent()

View File

@ -42,20 +42,18 @@ class TestIntegrateBase(TestShepBase):
def test_integration_valid(self): 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): def test_state_default(self):
hx = os.urandom(4).hex() (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
v = self.store.pending() v = self.store.pending()
self.assertEqual(len(v), 1) self.assertEqual(len(v), 1)
self.assertEqual(v[0], hx) self.assertEqual(v[0], hx)
def test_state_enqueue(self): def test_state_enqueue(self):
hx = os.urandom(4).hex() (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.get(hx) self.store.get(hx)
self.store.enqueue(hx) self.store.enqueue(hx)
v = self.store.upcoming() v = self.store.upcoming()
@ -65,8 +63,7 @@ class TestIntegrateBase(TestShepBase):
def test_state_defer(self): def test_state_defer(self):
hx = os.urandom(4).hex() (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx) self.store.reserve(hx)
self.store.fail(hx) self.store.fail(hx)
v = self.store.deferred() v = self.store.deferred()
@ -75,12 +72,11 @@ class TestIntegrateBase(TestShepBase):
def test_state_multiple(self): def test_state_multiple(self):
hx = os.urandom(4).hex() (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx) self.store.reserve(hx)
self.store.fail(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.reserve(hx)
self.store.fail(hx) self.store.fail(hx)
v = self.store.deferred() v = self.store.deferred()
@ -88,33 +84,30 @@ class TestIntegrateBase(TestShepBase):
def test_state_multiple_sort(self): def test_state_multiple_sort(self):
hx = os.urandom(4).hex() (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx) self.store.reserve(hx)
self.store.fail(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) 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.reserve(hx)
self.store.fail(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() v = self.store.deferred()
self.assertEqual(len(v), 2) self.assertEqual(len(v), 2)
def test_state_date_threshold(self): def test_state_date_threshold(self):
hx = os.urandom(4).hex() (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
s = self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx) self.store.reserve(hx)
self.store.fail(hx) self.store.fail(hx)
then = self.store.modified(s) then = self.store.modified(s)
time.sleep(0.1) time.sleep(0.1)
hx = os.urandom(4).hex() (s, hx) = self.store.put(os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
s = self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
self.store.reserve(hx) self.store.reserve(hx)
self.store.fail(hx) self.store.fail(hx)

View File

@ -15,6 +15,7 @@ from chainqueue import QueueEntry
# test imports # test imports
from tests.base_shep import TestShepBase from tests.base_shep import TestShepBase
from tests.common import MockCacheTokenTx
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
@ -27,13 +28,12 @@ class TestShep(TestShepBase):
def test_shep_tx(self): def test_shep_tx(self):
tx_hash = add_0x(os.urandom(20).hex())
signed_tx = add_0x(os.urandom(128).hex()) signed_tx = add_0x(os.urandom(128).hex())
nonce = 42 nonce = 42
tx = QueueEntry(self.store, tx_hash) tx = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
tx.create(signed_tx) 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() tx_retrieved.load()
self.assertEqual(tx_retrieved.signed_tx, strip_0x(signed_tx)) self.assertEqual(tx_retrieved.signed_tx, strip_0x(signed_tx))
@ -52,7 +52,7 @@ class TestShep(TestShepBase):
def test_shep_cache(self): def test_shep_cache(self):
self.store.put('foo', 'bar') self.store.put('bar', cache_adapter=MockCacheTokenTx)
if __name__ == '__main__': if __name__ == '__main__':