chainlib

Generic blockchain access library and tooling
Log | Files | Refs | README | LICENSE

commit 84fa27ddf8982413228c879015129361907ebd43
parent b1cd745116d46f27406ff6c309530ea73757ad71
Author: lash <dev@holbrook.no>
Date:   Fri, 16 Dec 2022 09:07:13 +0000

Add missing files

Diffstat:
Achainlib/dialect.py | 20++++++++++++++++++++
Achainlib/src.py | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/test_src.py | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 212 insertions(+), 0 deletions(-)

diff --git a/chainlib/dialect.py b/chainlib/dialect.py @@ -0,0 +1,20 @@ +class DialectFilter: + + def apply_block(self, block): + return block + + + def apply_tx(self, tx): + return tx + + + def apply_result(self, result): + return result + + + def apply_src(self, src): + return src + + + def validate_src(self, src): + return src diff --git a/chainlib/src.py b/chainlib/src.py @@ -0,0 +1,96 @@ +# standard imports +import enum + + +class SrcItem(enum.Enum): + AUTO = 0 + SRC = 1 + HASH = 2 + ADDRESS = 3 + PAYLOAD = 4 + + +class Src: + + def __init__(self, src=None, dialect_filter=None): + self.__src = None + self.__hash = None + self.__wire = None + if src != None: + self.apply_src(src, dialect_filter=dialect_filter) + + + @classmethod + def src_normalize(self, v): + return v + + + def src_validate(self, v): + return v + + + def apply_src(self, src, dialect_filter=None): + src = self.src_normalize(src) + if dialect_filter != None: + src = dialect_filter.apply_src(src) + self.__src = self.src_validate(src) + return self.__src + + + @property + def src(self): + return self.__src + + + def set_hash(self, hsh): + self.__hash = hsh + + + @property + def hash(self): + return self.__hash + + + def set_wire(self, v): + self.__wire = v + + + @property + def wire(self): + return self.__wire + + + def normal(self, v, typ=SrcItem.AUTO): + if typ == SrcItem.SRC: + return self.src_normalize(v) + return v + + + def safe(self, v, typ=SrcItem.AUTO): + return v + + + def display(self, v, typ=SrcItem.AUTO): + return v + + + @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) + + + def __repr__(self): + return self.__src + + + def load_src(self, dialect_filter=None): + raise NotImplementedError() + + diff --git a/tests/test_src.py b/tests/test_src.py @@ -0,0 +1,96 @@ +# standard imports +import unittest + +# local imports +from chainlib.src import Src +from chainlib.tx import ( + Tx, + TxResult, + ) +from chainlib.block import Block +from chainlib.status import Status + +class TestBlock(Block): + + def load_src(self, dialect_filter=None): + self.number = self.src['number'] + + +class TestTx(Tx): + + def load_src(self, dialect_filter=None): + self.foo = self.src['foo'] + + +class TestTxResult(TxResult): + + def load_src(self, dialect_filter=None): + self.bar = self.src['bar'] + + +class TestSrc(unittest.TestCase): + + def test_basic(self): + v = {'foo': 'bar'} + src = Src(v) + self.assertEqual(src.src, v) + + + def test_block_basic(self): + v = {'number': 42} + block = TestBlock(v) + self.assertEqual(block.src, v) + self.assertEqual(block.number, 42) + + + def test_tx_basic(self): + v = {'foo': 'bar'} + tx = TestTx(v) + self.assertEqual(tx.src, v) + self.assertEqual(tx.foo, 'bar') + + v = {'bar': 'baz'} + result = TestTxResult(v) + self.assertEqual(result.src, v) + self.assertEqual(result.status, Status.UNKNOWN) + self.assertEqual(result.bar, 'baz') + + + def test_apply(self): + vt = {'foo': 'bar'} + tx = TestTx(vt) + + vb = {'number': 42} + block = TestBlock(vb) + + vr = {'bar': 'baz'} + result = TestTxResult(vr) + + tx.apply_block(block) + + tx.apply_result(result) + + self.assertEqual(tx.src, vt) + self.assertEqual(tx.status, Status.UNKNOWN) + self.assertEqual(tx.status_name, Status.UNKNOWN.name) + self.assertEqual(tx.foo, 'bar') + self.assertEqual(tx.result.bar, 'baz') + + tx = TestTx(vt, block=block, result=result) + + self.assertEqual(tx.src, vt) + self.assertEqual(tx.status, Status.UNKNOWN) + self.assertEqual(tx.status_name, Status.UNKNOWN.name) + self.assertEqual(tx.foo, 'bar') + self.assertEqual(tx.result.bar, 'baz') + + + def test_from(self): + v = {'foo': 'bar'} + tx = TestTx.from_src(v) + self.assertEqual(tx.src, v) + self.assertEqual(tx.foo, 'bar') + + +if __name__ == '__main__': + unittest.main()