Simplify queueentry

This commit is contained in:
lash 2022-03-11 21:49:23 +00:00
parent 790c9ddf13
commit b763d11eff
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 31 additions and 36 deletions

View File

@ -11,15 +11,6 @@ from hexathon import (
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
def to_key(k, v):
return '{:>010s}_{}'.format(k, v)
def from_key(k):
(seq_str, tx_hash) = k.split('_')
return (int(seq_str), tx_hash,)
def normalize_hex(k): def normalize_hex(k):
k = strip_0x(k) k = strip_0x(k)
return uniform(k) return uniform(k)
@ -39,16 +30,12 @@ class QueueEntry:
def create(self, seq, signed_tx): def create(self, seq, signed_tx):
n = str(seq) n = str(seq)
signed_tx = normalize_hex(signed_tx) signed_tx = normalize_hex(signed_tx)
self.k = to_key(n, self.tx_hash) self.k = self.store.put(self.tx_hash, n, signed_tx)
self.store.put(self.k, signed_tx)
self.store.put_seq(self.tx_hash, n)
self.synced = True self.synced = True
def load(self): def load(self):
seq = self.store.get_seq(self.tx_hash) (self.k, self.signed_tx) = self.store.get(self.tx_hash)
self.k = to_key(seq, self.tx_hash)
self.signed_tx = self.store.get(self.k)
self.synced = True self.synced = True

View File

@ -2,12 +2,19 @@
import logging import logging
import re import re
# local imports
from .entry import from_key
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
def to_key(k, v):
return '{:>010s}_{}'.format(k, v)
def from_key(k):
(seq_str, tx_hash) = k.split('_')
return (int(seq_str), tx_hash,)
re_u = r'^[^_][_A-Z]+$' re_u = r'^[^_][_A-Z]+$'
class Store: class Store:
@ -19,35 +26,36 @@ class Store:
continue continue
v = self.state_store.from_name(s) v = self.state_store.from_name(s)
setattr(self, s, v) setattr(self, s, v)
for v in ['put', 'get', '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))
def put(self, k, v): def put(self, k, n, v):
self.index_store.put(k, n)
k = to_key(n, k)
self.state_store.put(k, v) self.state_store.put(k, v)
def get(self, k, v): def get(self, k):
return self.state_store.get(k) n = self.index_store.get(k)
k = to_key(n, k)
return (k, self.state_store.get(k))
def put_seq(self, k, seq):
self.index_store.put(k, seq)
def get_seq(self, k):
return self.index_store.get(k)
def list(self, state=0, limit=4096, strict=False): def list(self, state=0, limit=4096, strict=False):
hashes = [] hashes = []
i = 0 i = 0
for k in self.state_store.list(state):
item_state = self.state_store.state(k) hashes_state = self.state_store.list(state)
if strict: if strict:
for k in hashes_state:
item_state = self.state_store.state(k)
if item_state & state != item_state: if item_state & state != item_state:
continue continue
hashes.append(k) hashes.append(k)
else:
hashes = hashes_state
hashes.sort() hashes.sort()
hashes_out = [] hashes_out = []
for h in hashes: for h in hashes:

View File

@ -23,13 +23,13 @@ class MockTranslator:
class TestShep(TestShepBase): class TestShep(TestShepBase):
def test_entry_get(self): def test_entry_get(self):
tx_hash_one = add_0x(os.urandom(20).hex()) 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, tx_hash_one)
entry.create(nonce, signed_tx) entry.create(nonce, signed_tx)
tx_hash_two = add_0x(os.urandom(20).hex()) 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, tx_hash_two)