2022-03-11 20:38:12 +01:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
|
2022-05-06 10:30:14 +02:00
|
|
|
# external imports
|
2022-03-11 21:16:42 +01:00
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
uniform,
|
|
|
|
)
|
|
|
|
|
2022-03-15 09:06:39 +01:00
|
|
|
# local imports
|
|
|
|
from chainqueue.cache import CacheTx
|
|
|
|
|
2022-03-11 20:38:12 +01:00
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-03-11 21:16:42 +01:00
|
|
|
def normalize_hex(k):
|
|
|
|
k = strip_0x(k)
|
|
|
|
return uniform(k)
|
|
|
|
|
|
|
|
|
2022-03-11 20:38:12 +01:00
|
|
|
class QueueEntry:
|
|
|
|
|
2022-03-15 09:06:39 +01:00
|
|
|
def __init__(self, store, tx_hash=None, cache_adapter=CacheTx):
|
2022-03-11 20:38:12 +01:00
|
|
|
self.store = store
|
2022-03-15 09:06:39 +01:00
|
|
|
#self.tx_hash = normalize_hex(tx_hash)
|
|
|
|
self.tx_hash = tx_hash
|
2022-03-11 20:38:12 +01:00
|
|
|
self.signed_tx = None
|
|
|
|
self.seq = None
|
|
|
|
self.k = None
|
|
|
|
self.synced = False
|
2022-03-15 09:06:39 +01:00
|
|
|
self.cache_adapter = cache_adapter
|
2022-03-11 20:38:12 +01:00
|
|
|
|
|
|
|
|
2022-03-14 20:53:54 +01:00
|
|
|
def serialize(self):
|
|
|
|
return self.signed_tx
|
|
|
|
|
|
|
|
|
2022-03-12 09:48:19 +01:00
|
|
|
def create(self, signed_tx):
|
2022-03-11 21:16:42 +01:00
|
|
|
signed_tx = normalize_hex(signed_tx)
|
2022-03-15 09:06:39 +01:00
|
|
|
(s, tx_hash) = self.store.put(signed_tx, cache_adapter=self.cache_adapter)
|
|
|
|
self.k = s
|
2022-03-11 20:38:12 +01:00
|
|
|
self.synced = True
|
2022-03-15 09:06:39 +01:00
|
|
|
return tx_hash
|
2022-03-11 20:38:12 +01:00
|
|
|
|
|
|
|
|
2022-05-06 10:30:14 +02:00
|
|
|
def local_state(self):
|
|
|
|
state = self.store.state(self.k)
|
|
|
|
state_str = self.store.name(state)
|
|
|
|
return (state, state_str,)
|
|
|
|
|
|
|
|
|
2022-03-11 20:38:12 +01:00
|
|
|
def load(self):
|
2022-03-11 22:49:23 +01:00
|
|
|
(self.k, self.signed_tx) = self.store.get(self.tx_hash)
|
2022-03-11 20:38:12 +01:00
|
|
|
self.synced = True
|
|
|
|
|
|
|
|
|
|
|
|
def __match_state(self, state):
|
|
|
|
return bool(self.store.state(self.k) & state)
|
|
|
|
|
|
|
|
|
|
|
|
def waitforfunds(self):
|
|
|
|
if self.__match_state(self.store.INSUFFICIENT_FUNDS):
|
|
|
|
return
|
|
|
|
self.store.move(self.k, self.store.INSUFFICIENT_FUNDS)
|
|
|
|
|
|
|
|
|
|
|
|
def fubar(self):
|
|
|
|
if self.__match_state(self.store.UNKNOWN_ERROR):
|
|
|
|
return
|
|
|
|
self.store.set(self.k, self.store.UNKNOWN_ERROR)
|
|
|
|
|
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
if self.__match_state(self.store.NODE_ERROR):
|
|
|
|
return
|
|
|
|
self.store.set(self.k, self.store.NODE_ERROR)
|
|
|
|
|
|
|
|
|
|
|
|
def override(self, manual=False):
|
|
|
|
if manual:
|
|
|
|
self.store.set(self.k, self.store.OBSOLETE | self.store.MANUAL)
|
|
|
|
else:
|
|
|
|
self.store.set(self.k, self.store.OBSOLETE)
|
|
|
|
|
|
|
|
|
|
|
|
def manual(self):
|
|
|
|
self.store.set(self.k, self.store.MANUAL)
|
|
|
|
|
|
|
|
|
|
|
|
def retry(self):
|
|
|
|
if self.__match_state(self.store.QUEUED):
|
|
|
|
return
|
|
|
|
self.store.change(self.k, self.store.QUEUED, self.store.INSUFFICIENT_FUNDS)
|
|
|
|
|
|
|
|
|
|
|
|
def readysend(self):
|
|
|
|
if self.__match_state(self.store.QUEUED):
|
|
|
|
return
|
|
|
|
self.store.change(self.k, self.store.QUEUED, self.store.INSUFFICIENT_FUNDS)
|
|
|
|
|
|
|
|
|
|
|
|
def sent(self):
|
|
|
|
if self.__match_state(self.store.IN_NETWORK):
|
|
|
|
return
|
|
|
|
self.store.change(self.k, self.store.IN_NETWORK, self.store.RESERVED | self.store.DEFERRED | self.store.QUEUED | self.store.LOCAL_ERROR | self.store.NODE_ERROR)
|
|
|
|
|
|
|
|
|
|
|
|
def sendfail(self):
|
|
|
|
if self.__match_state(self.store.NODE_ERROR):
|
|
|
|
return
|
|
|
|
self.store.change(self.k, self.store.LOCAL_ERROR | self.store.DEFERRED, self.store.RESERVED | self.store.QUEUED | self.store.INSUFFICIENT_FUNDS)
|
|
|
|
|
|
|
|
|
|
|
|
def reserve(self):
|
|
|
|
if self.__match_state(self.store.RESERVED):
|
|
|
|
return
|
|
|
|
self.store.change(self.k, self.store.RESERVED, self.store.QUEUED)
|
|
|
|
|
|
|
|
|
2022-03-14 22:17:00 +01:00
|
|
|
def fail(self, block, tx):
|
2022-03-11 20:38:12 +01:00
|
|
|
if self.__match_state(self.store.NETWORK_ERROR):
|
|
|
|
return
|
2022-04-10 16:00:01 +02:00
|
|
|
v = self.store.state(self.k)
|
|
|
|
self.store.change(self.k, v | self.store.NETWORK_ERROR, self.store.QUEUED)
|
2022-03-14 22:17:00 +01:00
|
|
|
if self.store.cache:
|
|
|
|
self.store.cache.set_block(self.tx_hash, block, tx)
|
2022-03-11 20:38:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def cancel(self, confirmed=False):
|
|
|
|
if confirmed:
|
|
|
|
self.store.change(self.k, self.store.OBSOLETE | self.store.FINAL, self.store.RESERVED | self.store.QUEUED)
|
|
|
|
else:
|
|
|
|
self.store.change(self.k, self.store.OBSOLETE, self.store.RESERVED | self.store.QUEUED)
|
|
|
|
|
|
|
|
|
2022-03-14 22:17:00 +01:00
|
|
|
def succeed(self, block, tx):
|
2022-03-11 20:38:12 +01:00
|
|
|
self.store.set(self.k, self.store.FINAL)
|
2022-03-14 22:17:00 +01:00
|
|
|
if self.store.cache:
|
|
|
|
self.store.cache.set_block(self.tx_hash, block, tx)
|
2022-03-13 16:40:45 +01:00
|
|
|
|
|
|
|
|
2022-04-29 08:28:01 +02:00
|
|
|
def test(self, state):
|
|
|
|
return self.__match_state(state)
|
|
|
|
|
|
|
|
|
2022-03-13 16:40:45 +01:00
|
|
|
def __str__(self):
|
|
|
|
v = self.store.get(self.tx_hash)
|
|
|
|
n = self.store.state(v[0])
|
|
|
|
s = self.store.name(n)
|
2022-05-06 10:30:14 +02:00
|
|
|
return '{}: {} ({})'.format(self.k, s, n)
|