Compare commits

..

5 Commits

Author SHA1 Message Date
lash
1b24fd5439 Implement shep 0.3.x, chainlib 0.4.x 2022-11-06 15:17:55 +00:00
lash
b7a84acdb4 Bump minor version 2022-05-14 16:29:02 +00:00
lash
b7953cbd0f Add data module 2022-05-14 12:38:10 +00:00
lash
c5bd4aad3a Update cli module for chainlib 0.3.0, fix remaining settings processing 2022-05-13 13:46:30 +00:00
lash
c45e6e3310 Implement settings on chainlib 0.3.0 structure 2022-05-13 09:29:54 +00:00
9 changed files with 92 additions and 243 deletions

View File

@@ -1,3 +1,8 @@
- 0.2.1
* Implement shep 0.3.0
* Implement chainlib 0.4.x
- 0.2.0
* Implement chainlib 0.3.0
- 0.1.16
* Queue list cli tool
* State parser cli tool

View File

@@ -1,153 +0,0 @@
# 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,11 +0,0 @@
# 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')

View File

@@ -1,2 +1,8 @@
def process_flags(argparser, flags):
argparser.add_argument('--backend', type=str, help='Backend to use for state store')
def apply_flag(flag):
flag.add('queue')
return flag
def apply_arg(arg):
arg.add_long('tx-digest-size', 'queue', type=int, help='Size of transaction hash in bytes')
return arg

View File

@@ -28,19 +28,12 @@ config_dir = os.path.join(script_dir, '..', 'data', 'config')
arg_flags = chainlib.cli.argflag_std_base | chainlib.cli.Flag.CHAIN_SPEC | chainlib.cli.Flag.UNSAFE
argparser = chainlib.cli.ArgumentParser(arg_flags)
argparser.add_argument('--backend', type=str, default='sql', help='Backend to use')
argparser.add_argument('--state-dir', type=str, dest='state_dir', help='Backend to use')
argparser.add_argument('--tx-digest-size', type=int, dest='tx_digest_size', default=32, help='Size of tx digest in bytes')
#argparser.add_argument('--session-id', type=str, dest='session_id', help='Session id to list')
#argparser.add_argument('--start', type=str, help='Oldest transaction to include in results')
#argparser.add_argument('--end', type=str, help='Newest transaction to include in results')
argparser.add_argument('--error', action='store_true', help='Only show transactions which have error state')
argparser.add_argument('--no-final', action='store_true', dest='no_final', help='Omit finalized transactions')
argparser.add_argument('--status-mask', type=str, dest='status_mask', action='append', default=[], help='Manually specify status bitmask value to match (overrides --error and --pending)')
argparser.add_argument('--exact', action='store_true', help='Match status exact')
argparser.add_argument('--include-pending', action='store_true', dest='include_pending', help='Include transactions in unprocessed state (pending)')
argparser.add_argument('--renderer', type=str, default=[], action='append', help='Transaction renderer for output')
#argparser.add_argument('--summary', action='store_true', help='output summary for each status category')
#argparser.add_argument('-o', '--column', dest='column', action='append', type=str, help='add a column to display')
argparser.add_positional('address', required=False, type=str, help='Ethereum address of recipient')
args = argparser.parse_args()
extra_args = {
@@ -48,16 +41,11 @@ extra_args = {
'backend': None,
'state_dir': None,
'exact': None,
# 'tx_digest_size': None,
# 'start': None,
# 'end': None,
'error': None,
'include_pending': '_PENDING',
'status_mask': None,
'no_final': None,
'renderer': None,
# 'column': None,
# 'summary': None,
}
config = chainlib.cli.Config.from_args(args, arg_flags, extra_args=extra_args, base_config_dir=config_dir)
config = chainqueue.cli.config.process_config(config, args, 0)

View File

@@ -10,77 +10,90 @@ from chainqueue.store import Store
logg = logging.getLogger(__name__)
class ChainqueueSettings(ChainSettings):
def process_queue_tx(self, config):
self.o['TX_DIGEST_SIZE'] = config.get('TX_DIGEST_SIZE')
def process_queue_tx(settings, config):
settings.set('TX_DIGEST_SIZE', config.get('TX_DIGEST_SIZE'))
return settings
def process_queue_backend(self, config):
self.o['QUEUE_BACKEND'] = config.get('QUEUE_BACKEND')
self.o['QUEUE_STATE_STORE'] = Status(self.o['QUEUE_STORE_FACTORY'], allow_invalid=True)
self.o['QUEUE_STORE'] = Store(
self.o['CHAIN_SPEC'],
self.o['QUEUE_STATE_STORE'],
self.o['QUEUE_INDEX_STORE'],
self.o['QUEUE_COUNTER_STORE'],
sync=True,
)
def process_queue_store(settings, config):
status = Status(settings.get('QUEUE_STORE_FACTORY'), allow_invalid=True)
settings.set('QUEUE_STATE_STORE', status)
store = Store(
settings.get('CHAIN_SPEC'),
settings.get('QUEUE_STATE_STORE'),
settings.get('QUEUE_INDEX_STORE'),
settings.get('QUEUE_COUNTER_STORE'),
sync=True,
)
settings.set('QUEUE_STORE', store)
return settings
def process_queue_paths(self, config):
index_dir = config.get('QUEUE_INDEX_PATH')
if index_dir == None:
index_dir = os.path.join(config.get('QUEUE_STATE_PATH'), 'tx')
def process_queue_paths(settings, config):
index_dir = config.get('QUEUE_INDEX_PATH')
if index_dir == None:
index_dir = os.path.join(config.get('STATE_PATH'), 'tx')
counter_dir = config.get('QUEUE_COUNTER_PATH')
if counter_dir == None:
counter_dir = os.path.join(config.get('QUEUE_STATE_PATH'))
counter_dir = config.get('QUEUE_COUNTER_PATH')
if counter_dir == None:
counter_dir = os.path.join(config.get('STATE_PATH'))
self.o['QUEUE_STATE_PATH'] = config.get('QUEUE_STATE_PATH')
self.o['QUEUE_INDEX_PATH'] = index_dir
self.o['QUEUE_COUNTER_PATH'] = counter_dir
settings.set('QUEUE_STATE_PATH', config.get('STATE_PATH'))
settings.set('QUEUE_INDEX_PATH', index_dir)
settings.set('QUEUE_COUNTER_PATH', counter_dir)
return settings
def process_queue_backend_fs(self, config):
from chainqueue.store.fs import IndexStore
from chainqueue.store.fs import CounterStore
from shep.store.file import SimpleFileStoreFactory
index_store = IndexStore(self.o['QUEUE_INDEX_PATH'], digest_bytes=self.o['TX_DIGEST_SIZE'])
counter_store = CounterStore(self.o['QUEUE_COUNTER_PATH'])
factory = SimpleFileStoreFactory(self.o['QUEUE_STATE_PATH'], use_lock=True).add
def process_queue_backend_fs(settings, config):
from chainqueue.store.fs import IndexStore
from chainqueue.store.fs import CounterStore
from shep.store.file import SimpleFileStoreFactory
index_store = IndexStore(settings.o['QUEUE_INDEX_PATH'], digest_bytes=int(settings.o['TX_DIGEST_SIZE']))
counter_store = CounterStore(settings.o['QUEUE_COUNTER_PATH'])
factory = SimpleFileStoreFactory(settings.o['QUEUE_STATE_PATH'], use_lock=True).add
self.o['QUEUE_INDEX_STORE'] = index_store
self.o['QUEUE_COUNTER_STORE'] = counter_store
self.o['QUEUE_STORE_FACTORY'] = factory
settings.set('QUEUE_INDEX_STORE', index_store)
settings.set('QUEUE_COUNTER_STORE', counter_store)
settings.set('QUEUE_STORE_FACTORY', factory)
return settings
def process_queue_status_filter(self, config):
states = 0
if len(config.get('_STATUS_MASK')) == 0:
for v in self.o['QUEUE_STATE_STORE'].all(numeric=True):
states |= v
logg.debug('state store {}'.format(states))
else:
for v in config.get('_STATUS_MASK'):
try:
states |= int(v)
continue
except ValueError:
pass
state = self.o['QUEUE_STATE_STORE'].from_name(v)
logg.debug('resolved state argument {} to numeric state {}'.format(v, state))
states |= state
def process_queue_status_filter(settings, config):
states = 0
store = settings.get('QUEUE_STATE_STORE')
if len(config.get('_STATUS_MASK')) == 0:
for v in store.all(numeric=True):
states |= v
logg.debug('state store {}'.format(states))
else:
for v in config.get('_STATUS_MASK'):
try:
states |= int(v)
continue
except ValueError:
pass
state = store.from_name(v)
logg.debug('resolved state argument {} to numeric state {}'.format(v, state))
states |= state
self.o['QUEUE_STATUS_FILTER'] = states
settings.set('QUEUE_STATUS_FILTER', states)
return settings
def process(self, config):
super(ChainqueueSettings, self).process(config)
self.process_queue_tx(config)
self.process_queue_paths(config)
if config.get('_BACKEND') == 'fs':
self.process_queue_backend_fs(config)
self.process_queue_backend(config)
self.process_queue_status_filter(config)
def process_queue(settings, config):
settings = process_queue_tx(settings, config)
settings = process_queue_paths(settings, config)
if config.get('QUEUE_BACKEND') == 'fs':
settings = process_queue_backend_fs(settings, config)
settings = process_queue_backend(settings, config)
settings = process_queue_store(settings, config)
settings = process_queue_status_filter(settings, config)
return settings
def process_settings(settings, config):
super(ChainqueueSettings, settings).process(config)
settings = settings.process_queue(settings, config)
return settings

View File

@@ -9,7 +9,7 @@ logg = logging.getLogger(__name__)
class Verify:
def verify(self, state_store, from_state, to_state):
def verify(self, state_store, key, from_state, to_state):
to_state_name = state_store.name(to_state)
m = None
try:

View File

@@ -1,5 +1,5 @@
hexathon~=0.1.7
leveldir~=0.3.0
confini~=0.6.0
chainlib~=0.2.0
shep~=0.2.9
confini~=0.6.1
chainlib~=0.4.0
shep~=0.3.0

View File

@@ -1,6 +1,6 @@
[metadata]
name = chainqueue
version = 0.1.16
version = 0.2.1
description = Generic blockchain transaction queue control
author = Louis Holbrook
author_email = dev@holbrook.no
@@ -34,6 +34,7 @@ packages =
chainqueue.store
chainqueue.runnable
chainqueue.cli
chainqueue.data
[options.entry_points]
console_scripts =