11 Commits

Author SHA1 Message Date
lash
63df6aa6c4 Fix wire generation bug 2022-05-10 19:00:37 +00:00
lash
11b7c4e4e8 Make status methods properties 2022-05-09 19:21:05 +00:00
lash
23ee2f4b09 Remove commented code 2022-05-09 18:48:49 +00:00
lash
883a931495 Add common tx result fields 2022-05-09 18:46:50 +00:00
lash
04a09334ea WIP implement and test genertic tx, result, block 2022-05-09 10:00:58 +00:00
lash
45785c8e9e Remove superfluous arguments for settings instantiation 2022-05-06 07:47:04 +00:00
lash
b002976ebd Upgrade hexathon 2022-05-04 18:13:30 +00:00
lash
766027a49c Add settings module 2022-04-28 12:31:11 +00:00
lash
5726181f21 Remove log output for config load by default 2022-04-26 21:36:14 +00:00
lash
a6b9eae745 Add rpc query timeout 2022-04-10 19:08:34 +00:00
lash
d3f4da5296 Upgrade confini 2022-03-06 19:29:57 +00:00
10 changed files with 137 additions and 41 deletions

View File

@@ -1,3 +1,15 @@
- 0.2.1
* Fix bug in wire format generation for tx
- 0.2.0
* Consolidate genertic blcok, tx and tx result objects
- 0.1.3
* Remove superfluous arguments for chain settings instantiation
- 0.1.2
* Upgrade hexathon dep
- 0.1.1
* Add settings object
- 0.1.0
* Upgrade deps
- 0.0.23 - 0.0.23
* Configuration variable descriptions * Configuration variable descriptions
* Arg flags to names listing method * Arg flags to names listing method

View File

@@ -2,7 +2,8 @@
import enum import enum
# local imports # local imports
from chainlib.tx import Tx from .tx import Tx
from .src import Src
class BlockSpec(enum.IntEnum): class BlockSpec(enum.IntEnum):
@@ -12,23 +13,24 @@ class BlockSpec(enum.IntEnum):
LATEST = 0 LATEST = 0
class Block: class Block(Src):
"""Base class to extend for implementation specific block object. """Base class to extend for implementation specific block object.
""" """
tx_generator = Tx tx_generator = Tx
def __init__(self, src=None):
self.number = None
self.txs = []
self.author = None
def src(self): self.get_tx = self.tx_index_by_hash
"""Return implementation specific block representation. self.tx = self.tx_by_index
:rtype: dict super(Block, self).__init__(src=src)
:returns: Block representation
"""
return self.block_src
def tx(self, idx): def tx_by_index(self, idx):
"""Return transaction object for transaction data at given index. """Return transaction object for transaction data at given index.
:param idx: Transaction index :param idx: Transaction index
@@ -39,28 +41,12 @@ class Block:
return self.tx_generator(self.txs[idx], self) return self.tx_generator(self.txs[idx], self)
def tx_src(self, idx): def tx_index_by_hash(self, hsh):
"""Return implementation specific transaction representation for transaction data at given index for tx in self.tx:
if tx == hsh:
:param idx: Transaction index return tx
:type idx: int return -1
:rtype: chainlib.tx.Tx
:returns: Transaction representation
"""
return self.txs[idx]
def __str__(self): def __str__(self):
return 'block {} {} ({} txs)'.format(self.number, self.hash, len(self.txs)) return 'block {} {} ({} txs)'.format(self.number, self.hash, len(self.txs))
@classmethod
def from_src(cls, src):
"""Instantiate an implementation specific block object from the given block representation.
:param src: Block representation
:type src: dict
:rtype: chainlib.block.Block
:returns: Block object
"""
return cls(src)

View File

@@ -46,7 +46,7 @@ class Config(confini.Config):
@classmethod @classmethod
def from_args(cls, args, arg_flags=0x0f, env=os.environ, extra_args={}, base_config_dir=None, default_config_dir=None, user_config_dir=None, default_fee_limit=None, logger=None, load_callback=logcallback, dump_writer=sys.stdout): def from_args(cls, args, arg_flags=0x0f, env=os.environ, extra_args={}, base_config_dir=None, default_config_dir=None, user_config_dir=None, default_fee_limit=None, logger=None, load_callback=None, dump_writer=sys.stdout):
"""Parses arguments in argparse.ArgumentParser instance, then match and override configuration values that match them. """Parses arguments in argparse.ArgumentParser instance, then match and override configuration values that match them.
The method processes all known argument flags from chainlib.cli.Flag passed in the "args" argument. The method processes all known argument flags from chainlib.cli.Flag passed in the "args" argument.

View File

@@ -102,7 +102,8 @@ class RPCConnection:
} }
__constructors_for_chains = {} __constructors_for_chains = {}
def __init__(self, url=None, chain_spec=None, auth=None, verify_identity=True): def __init__(self, url=None, chain_spec=None, auth=None, verify_identity=True, timeout=1.0):
self.timeout = timeout
self.chain_spec = chain_spec self.chain_spec = chain_spec
self.location = None self.location = None
self.basic = None self.basic = None
@@ -329,6 +330,7 @@ class JSONRPCHTTPConnection(HTTPConnection):
req, req,
data=data.encode('utf-8'), data=data.encode('utf-8'),
context=ssl_ctx, context=ssl_ctx,
timeout=self.timeout,
) )
except URLError as e: except URLError as e:
raise RPCException(e) raise RPCException(e)

26
chainlib/settings.py Normal file
View File

@@ -0,0 +1,26 @@
# local imports
from .chain import ChainSpec
class ChainSettings:
def __init__(self):
self.o = {}
self.get = self.o.get
def process_common(self, config):
self.o['CHAIN_SPEC'] = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
def process(self, config):
self.process_common(config)
def __str__(self):
ks = list(self.o.keys())
ks.sort()
s = ''
for k in ks:
s += '{}: {}\n'.format(k, self.o.get(k))
return s

View File

@@ -4,6 +4,7 @@ import enum
class Status(enum.Enum): class Status(enum.Enum):
"""Representation of transaction status in network. """Representation of transaction status in network.
""" """
UNKNOWN = -1
PENDING = 0 PENDING = 0
SUCCESS = 1 SUCCESS = 1
ERROR = 2 ERROR = 2

View File

@@ -1,4 +1,9 @@
class Tx: # local imports
from .status import Status
from .src import Src
class Tx(Src):
"""Base class to extend for implementation specific transaction objects. """Base class to extend for implementation specific transaction objects.
:param src: Transaction representation source :param src: Transaction representation source
@@ -7,8 +12,73 @@ class Tx:
:type block: chainlib.block.Block :type block: chainlib.block.Block
""" """
def __init__(self, src, block=None): def __init__(self, src=None, block=None, result=None, strict=False):
self.txs = []
self.src = src
self.block = block self.block = block
self.block_src = None self.index = -1
self.fee_limit = None
self.fee_price = None
self.nonce = None
self.value = 0
self.outputs = []
self.inputs = []
self.payload = None
self.result = None
super(Tx, self).__init__(src)
if block != None:
self.apply_block(block)
if result != None:
self.apply_result(result)
def apply_result(self, result):
self.result = result
def apply_block(self, block):
self.block = block
@property
def status(self):
if self.result == None:
return None
return self.result.status
@property
def status_name(self):
if self.result == None:
return None
return self.result.status.name
def generate_wire(self, chain_spec):
pass
def as_dict(self):
raise NotImplementedError()
def __str__(self):
if self.block != None:
return 'tx {} status {} block {} index {}'.format(self.display_hash(), self.status_name(), self.block.number, self.index)
else:
return 'tx {} status {}'.format(self.display_hash(), self.hash, self.status_name())
class TxResult(Src):
def __init__(self, src=None):
self.status = Status.UNKNOWN
self.tx_index = None
self.block_hash = None
self.fee_cost = 0
super(TxResult, self).__init__(src=src)

View File

@@ -1,4 +1,4 @@
funga~=0.5.2 funga~=0.5.2
pysha3==1.0.2 pysha3==1.0.2
hexathon~=0.1.5 hexathon~=0.1.7
confini~=0.5.7 confini~=0.6.0

View File

@@ -214,7 +214,6 @@ if examples != None:
f.write(".SH EXAMPLES\n\n") f.write(".SH EXAMPLES\n\n")
f.write(examples) f.write(examples)
if seealso != None: if seealso != None:
seealso_description = seealso seealso_description = seealso

View File

@@ -3,7 +3,7 @@ name=chainlib
license=WTFPL2 license=WTFPL2
author_email=dev@holbrook.no author_email=dev@holbrook.no
description=Generic blockchain access library and tooling description=Generic blockchain access library and tooling
version=0.0.23 version=0.2.1
url=https://gitlab.com/chaintools/chainlib url=https://gitlab.com/chaintools/chainlib
author=Louis Holbrook author=Louis Holbrook