Initial shep provisions

This commit is contained in:
lash 2022-03-11 11:02:47 +00:00
parent 5a92058e74
commit fce9bce6fc
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 144 additions and 3 deletions

View File

@ -1,3 +1,5 @@
- 0.1.0
* Replace state transitions with shep
- 0.0.3
* cli tool for listing queue by address
* ensure lowercase hex input in db

1
chainqueue/__init__.py Normal file
View File

@ -0,0 +1 @@
from .state import Status

65
chainqueue/state.py Normal file
View File

@ -0,0 +1,65 @@
# standard imports
import logging
# external imports
import shep.persist
logg = logging.getLogger(__name__)
class Verify:
def verify(self, state_store, from_state, to_state):
to_state_name = state_store.name(to_state)
m = None
try:
m = getattr(self, 'verify_' + to_state_name)
except AttributeError:
logg.debug('foo {}'.format(to_state_name))
return None
r = m(state_store, from_state)
if r != None:
from_state_name = state_store.name(from_state)
r = '{} -> {}: {}'.format(from_state_name, to_state_name, r)
return r
def verify_GAS_ISSUES(self, state_store, from_state):
if from_state & state_store.FINAL:
return 'already finalized'
if from_state & state_store.GAS_ISSUES:
return 'already in network'
class Status(shep.persist.PersistedState):
def __init__(self, store_factory):
verify = Verify().verify
self.set_default_state('PENDING')
super(Status, self).__init__(store_factory, 12, verifier=verify)
self.add('QUEUED')
self.add('RESERVED')
self.add('IN_NETWORK')
self.add('DEFERRED')
self.add('GAS_ISSUES')
self.add('LOCAL_ERROR')
self.add('NODE_ERROR')
self.add('NETWORK_ERROR')
self.add('UNKNOWN_ERROR')
self.add('FINAL')
self.add('OBSOLETE')
self.add('MANUAL')
self.alias('SENDFAIL', self.DEFERRED | self.LOCAL_ERROR)
self.alias('RETRY', self.DEFERRED | self.QUEUED)
self.alias('OBSOLETED', self.OBSOLETE | self.IN_NETWORK)
self.alias('FUBAR', self.FINAL | self.UNKNOWN_ERROR)
self.alias('CANCELLED', self.IN_NETWORK | self.FINAL | self.OBSOLETE)
self.alias('OVERRIDDEN', self.FINAL | self.OBSOLETE | self.MANUAL)
self.alias('REJECTED', self.NODE_ERROR | self.FINAL)
self.alias('REVERTED', self.IN_NETWORK | self.FINAL, self.NETWORK_ERROR)
self.alias('SUCCESS', self.IN_NETWORK | self.FINAL)

21
chainqueue/tx.py Normal file
View File

@ -0,0 +1,21 @@
class Tx:
def __init__(self, store, seq, tx_hash, signed_tx, cache=None):
self.store = store
self.seq = seq
self.tx_hash = tx_hash
self.signed_tx = signed_tx
self.cache = cache
def __to_key(self, k, v):
return '{:>010s}_{}'.format(k, v)
def create(self):
k = self.__to_key(str(self.seq), self.tx_hash)
self.store.put(k, self.signed_tx)
def waitforgas(self):
pass

View File

@ -1,8 +1,9 @@
pysha3==1.0.2
hexathon~=0.1.0
hexathon~=0.1.5
leveldir~=0.3.0
alembic==1.4.2
SQLAlchemy==1.3.20
confini~=0.5.1
confini~=0.6.0
pyxdg~=0.27
chainlib~=0.0.12
chainlib>=0.1.0b1,<=0.1.0
shep~=0.1.1

51
tests/test_shep.py Normal file
View File

@ -0,0 +1,51 @@
# standard imports
import os
import logging
import unittest
import tempfile
# external imports
from hexathon import add_0x
from shep.store.file import SimpleFileStoreFactory
from shep.error import StateTransitionInvalid
# local imports
from chainqueue import Status
from chainqueue.tx import Tx
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestShepBase(unittest.TestCase):
def setUp(self):
self.path = tempfile.mkdtemp()
factory = SimpleFileStoreFactory(self.path).add
self.state = Status(factory)
class TestShep(TestShepBase):
def test_shep_setup(self):
pass
def test_shep_tx(self):
tx_hash = add_0x(os.urandom(20).hex())
signed_tx = add_0x(os.urandom(128).hex())
nonce = 42
tx = Tx(self.state, nonce, tx_hash, signed_tx)
tx.create()
logg.debug('file {}'.format(self.path))
def test_shep_invalid(self):
self.state.put('foo', 'bar')
self.state.set('foo', self.state.FINAL)
with self.assertRaises(StateTransitionInvalid):
self.state.move('foo', self.state.GAS_ISSUES)
if __name__ == '__main__':
unittest.main()