Normalize hex in queueentry

This commit is contained in:
lash 2022-03-11 20:16:42 +00:00
parent 3880249683
commit 790c9ddf13
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 39 additions and 10 deletions

View File

@ -1,27 +1,45 @@
# standard imports
import logging
# ecxternal imports
from hexathon import (
add_0x,
strip_0x,
uniform,
)
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):
k = strip_0x(k)
return uniform(k)
class QueueEntry:
def __init__(self, store, tx_hash):
self.store = store
self.tx_hash = tx_hash
self.tx_hash = normalize_hex(tx_hash)
self.signed_tx = None
self.seq = None
self.k = None
self.synced = False
def __to_key(self, k, v):
return '{:>010s}_{}'.format(k, v)
def create(self, seq, signed_tx):
n = str(seq)
self.k = self.__to_key(n, self.tx_hash)
signed_tx = normalize_hex(signed_tx)
self.k = to_key(n, self.tx_hash)
self.store.put(self.k, signed_tx)
self.store.put_seq(self.tx_hash, n)
self.synced = True
@ -29,7 +47,7 @@ class QueueEntry:
def load(self):
seq = self.store.get_seq(self.tx_hash)
self.k = self.__to_key(seq, self.tx_hash)
self.k = to_key(seq, self.tx_hash)
self.signed_tx = self.store.get(self.k)
self.synced = True

View File

@ -2,6 +2,9 @@
import logging
import re
# local imports
from .entry import from_key
logg = logging.getLogger(__name__)
@ -45,4 +48,9 @@ class Store:
if item_state & state != item_state:
continue
hashes.append(k)
return hashes
hashes.sort()
hashes_out = []
for h in hashes:
pair = from_key(h)
hashes_out.append(pair[1])
return hashes_out

View File

@ -4,7 +4,10 @@ import logging
import unittest
# external imports
from hexathon import add_0x
from hexathon import (
add_0x,
strip_0x,
)
from shep.error import StateTransitionInvalid
# local imports
@ -32,7 +35,7 @@ class TestShep(TestShepBase):
tx_retrieved = QueueEntry(self.store, tx_hash)
tx_retrieved.load()
self.assertEqual(tx_retrieved.signed_tx, signed_tx)
self.assertEqual(tx_retrieved.signed_tx, strip_0x(signed_tx))
def test_shep_valid(self):