commit 790c9ddf135151d96d2362075daeb8d2374c5a94
parent 3880249683efe79837833ee547ebbfbf4f898a41
Author: lash <dev@holbrook.no>
Date: Fri, 11 Mar 2022 20:16:42 +0000
Normalize hex in queueentry
Diffstat:
3 files changed, 39 insertions(+), 10 deletions(-)
diff --git a/chainqueue/entry.py b/chainqueue/entry.py
@@ -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
diff --git a/chainqueue/store.py b/chainqueue/store.py
@@ -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
diff --git a/tests/test_shep.py b/tests/test_shep.py
@@ -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):