Compare commits

...

5 Commits

Author SHA1 Message Date
lash
ff74679de8 Remove unneeded deps 2022-04-28 15:37:06 +00:00
lash
94bd5c8cdf Add cli handling and settings 2022-04-28 12:37:08 +00:00
lash
ccbbcc2157 Sync chainqueue state store on get 2022-04-27 06:23:58 +00:00
lash
57191ea378 Move outputter module to explicit module path in cli 2022-04-26 21:35:20 +00:00
lash
e646edecca Upgrade shep 2022-04-26 09:21:30 +00:00
11 changed files with 199 additions and 18 deletions

View File

@@ -1,3 +1,7 @@
- 0.1.2
* Add CLI inspection tools
- 0.1.1
*
- 0.1.0
* Replace state transitions with shep
- 0.0.3

View File

@@ -0,0 +1,11 @@
# standard imports
import os
# local imports
from .arg import process_flags
from .config import process_config
__script_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(os.path.dirname(__script_dir), 'data')
config_dir = os.path.join(data_dir, 'config')

2
chainqueue/cli/arg.py Normal file
View File

@@ -0,0 +1,2 @@
def process_flags(argparser, flags):
argparser.add_argument('--backend', type=str, help='Backend to use for state store')

8
chainqueue/cli/config.py Normal file
View File

@@ -0,0 +1,8 @@
def process_config(config, args, flags):
args_override = {}
args_override['QUEUE_BACKEND'] = getattr(args, 'backend')
config.dict_override(args_override, 'local cli args')
return config

153
chainqueue/cli/output.py Normal file
View File

@@ -0,0 +1,153 @@
# standard imports
import logging
import enum
# external imports
from hexathon import add_0x
# local imports
from chainqueue.enum import (
StatusBits,
all_errors,
is_alive,
is_error_status,
status_str,
)
logg = logging.getLogger(__name__)
class OutputCol(enum.Enum):
chainspec = 0
hash = 1
statustext = 2
statuscode = 3
signedtx = 4
class Outputter:
"""Output helper for chainqueue cli listings tools.
:param chain_spec: Chain spec to use as getter context
:type chain_spec: chainlib.chain.ChainSpec
:param writer: Writer to write output to. Will automatically flush.
:type writer: Writer
:param getter: Transaction getter
:type getter: See chainqueue.sql.backend.get_otx
:param session_method: Backend session generator method
:type session_method: varies
:param decode_status: Print status bit details
:type decode_status: bool
"""
all_cols = [
OutputCol.chainspec,
OutputCol.hash,
OutputCol.signedtx,
OutputCol.statustext,
OutputCol.statuscode,
]
default_cols = [
OutputCol.chainspec,
OutputCol.hash,
OutputCol.statustext,
OutputCol.statuscode,
]
def __init__(self, chain_spec, writer, getter, session_method=None, decode_status=True, cols=None):
self.decode_status = decode_status
self.writer = writer
self.getter = getter
self.chain_spec = chain_spec
self.chain_spec_str = str(chain_spec)
self.session = None
if session_method != None:
self.session = session_method()
self.results = {
'pending_error': 0,
'final_error': 0,
'pending': 0,
'final': 0,
}
debug_col_name = []
if cols == None:
self.cols = Outputter.default_cols
else:
self.cols = []
for col in cols:
v = getattr(OutputCol, col)
self.cols.append(v)
for col in self.cols:
debug_col_name.append(col.name)
logg.debug('outputter initialized with cols: {}'.format(','.join(debug_col_name)))
def __del__(self):
if self.session != None:
self.session.close()
def add(self, tx_hash):
"""Retrieve a transaction by hash and add it for summary output generation.
:param tx_hash: Transaction hash
:type tx_hash: str
"""
tx = self.getter(self.chain_spec, tx_hash, session=self.session)
self.__add(tx)
def __add(self, tx):
category = None
if is_alive(tx['status_code']):
category = 'pending'
else:
category = 'final'
self.results[category] += 1
if is_error_status(tx['status_code']):
logg.debug('registered {} as {} with error'.format(tx['tx_hash'], category))
self.results[category + '_error'] += 1
else:
logg.debug('registered {} as {}'.format(tx['tx_hash'], category))
def decode_summary(self):
"""Writes summary to the registered writer.
"""
self.writer.write('pending\t{}\t{}\n'.format(self.results['pending'], self.results['pending_error']))
self.writer.write('final\t{}\t{}\n'.format(self.results['final'], self.results['final_error']))
self.writer.write('total\t{}\t{}\n'.format(self.results['final'] + self.results['pending'], self.results['final_error'] + self.results['pending_error']))
def decode_single(self, tx_hash):
"""Retrieves the transaction with the given hash and writes the details to the underlying writer.
Registers the transaction with the summary generator.
:param tx_hash: Transaction hash
:type tx_hash: str
"""
tx = self.getter(self.chain_spec, tx_hash, session=self.session)
self.__add(tx)
status = tx['status']
if self.decode_status:
status = status_str(tx['status_code'], bits_only=True)
vals = [
self.chain_spec_str,
add_0x(tx_hash),
status,
str(tx['status_code']),
add_0x(tx['signed_tx']),
]
i = 0
l = len(self.cols)
for col in self.cols:
self.writer.write(vals[col.value])
i += 1
if i == l:
self.writer.write('\n')
else:
self.writer.write('\t')

View File

@@ -1,9 +1,2 @@
[database]
name =
engine =
driver =
host =
port =
user =
password =
debug = 0
[queue]
backend = mem

View File

@@ -12,7 +12,7 @@ from chainlib.chain import ChainSpec
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
# local imports
from chainqueue.cli import Outputter
from chainqueue.cli.output import Outputter
logging.basicConfig(level=logging.WARNING)

8
chainqueue/settings.py Normal file
View File

@@ -0,0 +1,8 @@
# external imports
from chainlib.settings import ChainSettings
class ChainqueueSettings(ChainSettings):
def process_queue_backend(self, config):
self.o['QUEUE_BACKEND'] = config.get('QUEUE_BACKEND')

View File

@@ -67,6 +67,7 @@ class Store:
s = self.index_store.get(k)
except FileNotFoundError:
raise NotLocalTxError(k)
self.state_store.sync()
v = self.state_store.get(s)
return (s, v,)

View File

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

View File

@@ -1,6 +1,6 @@
[metadata]
name = chainqueue
version = 0.1.0
version = 0.1.3
description = Generic blockchain transaction queue control
author = Louis Holbrook
author_email = dev@holbrook.no
@@ -25,7 +25,7 @@ licence_files =
LICENSE.txt
[options]
python_requires = >= 3.6
python_requires = >= 3.7
include_package_data = True
packages =
chainqueue
@@ -33,6 +33,7 @@ packages =
chainqueue.unittest
chainqueue.store
chainqueue.runnable
chainqueue.cli
#[options.entry_points]
#console_scripts =