2022-03-11 20:38:12 +01:00
|
|
|
# standard imports
|
|
|
|
import re
|
2022-03-12 09:48:19 +01:00
|
|
|
import datetime
|
2022-04-10 16:00:01 +02:00
|
|
|
import logging
|
2022-03-11 20:38:12 +01:00
|
|
|
|
2022-03-12 14:48:40 +01:00
|
|
|
# local imports
|
|
|
|
from chainqueue.cache import CacheTx
|
2022-03-13 16:40:45 +01:00
|
|
|
from chainqueue.entry import QueueEntry
|
2022-04-12 15:44:29 +02:00
|
|
|
from chainqueue.error import (
|
|
|
|
NotLocalTxError,
|
|
|
|
)
|
2022-03-12 14:48:40 +01:00
|
|
|
|
2022-04-10 16:00:01 +02:00
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
2022-03-12 14:48:40 +01:00
|
|
|
|
2022-03-12 09:48:19 +01:00
|
|
|
def to_key(t, n, k):
|
|
|
|
return '{}_{}_{}'.format(t, n, k)
|
2022-03-11 22:49:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def from_key(k):
|
2022-03-12 09:48:19 +01:00
|
|
|
(ts_str, seq_str, tx_hash) = k.split('_')
|
|
|
|
return (float(ts_str), int(seq_str), tx_hash, )
|
2022-03-11 22:49:23 +01:00
|
|
|
|
|
|
|
|
2022-03-11 20:38:12 +01:00
|
|
|
re_u = r'^[^_][_A-Z]+$'
|
|
|
|
class Store:
|
|
|
|
|
2022-03-12 14:48:40 +01:00
|
|
|
def __init__(self, chain_spec, state_store, index_store, counter, cache=None):
|
|
|
|
self.chain_spec = chain_spec
|
2022-03-12 09:48:19 +01:00
|
|
|
self.cache = cache
|
2022-03-11 20:38:12 +01:00
|
|
|
self.state_store = state_store
|
|
|
|
self.index_store = index_store
|
2022-03-12 09:48:19 +01:00
|
|
|
self.counter = counter
|
2022-03-11 20:38:12 +01:00
|
|
|
for s in dir(self.state_store):
|
|
|
|
if not re.match(re_u, s):
|
|
|
|
continue
|
|
|
|
v = self.state_store.from_name(s)
|
|
|
|
setattr(self, s, v)
|
2022-03-13 18:22:39 +01:00
|
|
|
for v in [
|
|
|
|
'state',
|
|
|
|
'change',
|
|
|
|
'set',
|
|
|
|
'unset',
|
|
|
|
'name',
|
|
|
|
'modified',
|
|
|
|
]:
|
2022-03-11 20:38:12 +01:00
|
|
|
setattr(self, v, getattr(self.state_store, v))
|
2022-04-10 17:30:08 +02:00
|
|
|
self.state_store.sync()
|
2022-03-11 20:38:12 +01:00
|
|
|
|
2022-03-12 15:12:02 +01:00
|
|
|
|
2022-03-15 09:06:39 +01:00
|
|
|
def put(self, v, cache_adapter=CacheTx):
|
2022-03-15 10:00:15 +01:00
|
|
|
tx = cache_adapter(self.chain_spec)
|
2022-03-15 09:06:39 +01:00
|
|
|
tx.deserialize(v)
|
|
|
|
k = tx.hash
|
2022-03-12 09:48:19 +01:00
|
|
|
n = self.counter.next()
|
|
|
|
t = datetime.datetime.now().timestamp()
|
|
|
|
s = to_key(t, n, k)
|
|
|
|
self.index_store.put(k, s)
|
2022-04-12 15:44:29 +02:00
|
|
|
self.state_store.put(s, v)
|
2022-03-12 09:48:19 +01:00
|
|
|
if self.cache != None:
|
2022-03-12 14:48:40 +01:00
|
|
|
self.cache.put(self.chain_spec, tx)
|
2022-03-15 09:06:39 +01:00
|
|
|
return (s, k,)
|
2022-03-11 20:38:12 +01:00
|
|
|
|
|
|
|
|
2022-03-11 22:49:23 +01:00
|
|
|
def get(self, k):
|
2022-04-10 17:30:08 +02:00
|
|
|
try:
|
|
|
|
s = self.index_store.get(k)
|
|
|
|
except FileNotFoundError:
|
|
|
|
raise NotLocalTxError(k)
|
2022-04-27 08:23:58 +02:00
|
|
|
self.state_store.sync()
|
2022-03-12 09:48:19 +01:00
|
|
|
v = self.state_store.get(s)
|
|
|
|
return (s, v,)
|
2022-03-11 20:38:12 +01:00
|
|
|
|
|
|
|
|
2022-03-13 18:22:39 +01:00
|
|
|
def by_state(self, state=0, limit=4096, strict=False, threshold=None):
|
2022-03-11 20:38:12 +01:00
|
|
|
hashes = []
|
|
|
|
i = 0
|
2022-03-11 22:49:23 +01:00
|
|
|
|
2022-03-13 16:40:45 +01:00
|
|
|
refs_state = self.state_store.list(state)
|
|
|
|
|
|
|
|
for ref in refs_state:
|
|
|
|
v = from_key(ref)
|
|
|
|
hsh = v[2]
|
|
|
|
|
|
|
|
if strict:
|
|
|
|
item_state = self.state_store.state(ref)
|
2022-03-11 20:43:00 +01:00
|
|
|
if item_state & state != item_state:
|
2022-03-11 20:38:12 +01:00
|
|
|
continue
|
2022-03-13 18:22:39 +01:00
|
|
|
|
|
|
|
if threshold != None:
|
|
|
|
v = self.state_store.modified(ref)
|
|
|
|
if v > threshold:
|
|
|
|
continue
|
|
|
|
|
2022-03-13 16:40:45 +01:00
|
|
|
hashes.append(hsh)
|
2022-03-11 22:49:23 +01:00
|
|
|
|
2022-03-13 18:22:39 +01:00
|
|
|
|
|
|
|
|
2022-03-11 21:16:42 +01:00
|
|
|
hashes.sort()
|
2022-03-13 16:40:45 +01:00
|
|
|
return hashes
|
2022-03-12 14:48:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def upcoming(self, limit=4096):
|
|
|
|
return self.by_state(state=self.QUEUED, limit=limit)
|
2022-03-13 16:40:45 +01:00
|
|
|
|
|
|
|
|
2022-03-13 18:22:39 +01:00
|
|
|
def deferred(self, limit=4096, threshold=None):
|
|
|
|
return self.by_state(state=self.DEFERRED, limit=limit, threshold=threshold)
|
2022-03-13 16:40:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
def pending(self, limit=4096):
|
|
|
|
return self.by_state(state=0, limit=limit, strict=True)
|
|
|
|
|
|
|
|
|
2022-03-14 20:53:54 +01:00
|
|
|
def reserve(self, k):
|
|
|
|
entry = QueueEntry(self, k)
|
|
|
|
entry.load()
|
|
|
|
entry.reserve()
|
|
|
|
|
|
|
|
|
2022-03-13 16:40:45 +01:00
|
|
|
def enqueue(self, k):
|
|
|
|
entry = QueueEntry(self, k)
|
|
|
|
entry.load()
|
|
|
|
try:
|
|
|
|
entry.retry()
|
|
|
|
except StateTransitionInvalid:
|
|
|
|
entry.readysend()
|
2022-03-13 16:45:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def fail(self, k):
|
|
|
|
entry = QueueEntry(self, k)
|
2022-03-13 17:04:14 +01:00
|
|
|
entry.load()
|
|
|
|
entry.sendfail()
|
2022-03-14 20:53:54 +01:00
|
|
|
|
|
|
|
|
2022-03-14 22:17:00 +01:00
|
|
|
def final(self, k, block, tx, error=False):
|
|
|
|
entry = QueueEntry(self, k)
|
|
|
|
entry.load()
|
|
|
|
if error:
|
|
|
|
entry.fail(block, tx)
|
|
|
|
else:
|
|
|
|
entry.succeed(block, tx)
|
|
|
|
|
|
|
|
|
2022-03-14 20:53:54 +01:00
|
|
|
def send_start(self, k):
|
|
|
|
entry = QueueEntry(self, k)
|
|
|
|
entry.load()
|
|
|
|
entry.reserve()
|
|
|
|
return entry
|
|
|
|
|
|
|
|
|
|
|
|
def send_end(self, k):
|
|
|
|
entry = QueueEntry(self, k)
|
|
|
|
entry.load()
|
|
|
|
entry.sent()
|