chainlib

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

commit d417bbc938f93a23e3364b4d21516350246f0340
parent 51e09e5287a54cffade6fdf3704c131be8d76712
Author: nolash <dev@holbrook.no>
Date:   Thu, 18 Feb 2021 09:29:52 +0100

Add several missing files

Diffstat:
Achainlib/chain.py | 42++++++++++++++++++++++++++++++++++++++++++
Atests/base.py | 12++++++++++++
Atests/test_chain.py | 22++++++++++++++++++++++
3 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/chainlib/chain.py b/chainlib/chain.py @@ -0,0 +1,42 @@ +class ChainSpec: + + def __init__(self, engine, common_name, network_id, tag=None): + self.o = { + 'engine': engine, + 'common_name': common_name, + 'network_id': network_id, + 'tag': tag, + } + + def network_id(self): + return self.o['network_id'] + + + def chain_id(self): + return self.o['network_id'] + + + def engine(self): + return self.o['engine'] + + + def common_name(self): + return self.o['common_name'] + + + @staticmethod + def from_chain_str(chain_str): + o = chain_str.split(':') + if len(o) < 3: + raise ValueError('Chain string must have three sections, got {}'.format(len(o))) + tag = None + if len(o) == 4: + tag = o[3] + return ChainSpec(o[0], o[1], int(o[2]), tag) + + + def __str__(self): + s = '{}:{}:{}'.format(self.o['engine'], self.o['common_name'], self.o['network_id']) + if self.o['tag'] != None: + s += ':' + self.o['tag'] + return s diff --git a/tests/base.py b/tests/base.py @@ -0,0 +1,12 @@ +import unittest + + +class TestBase(unittest.TestCase): + + + def setUp(self): + pass + + + def tearDown(self): + pass diff --git a/tests/test_chain.py b/tests/test_chain.py @@ -0,0 +1,22 @@ +import unittest + +from chainlib.chain import ChainSpec + +from tests.base import TestBase + + +class TestChain(TestBase): + + def test_chain_spec(self): + + s = ChainSpec.from_chain_str('foo:bar:3') + s = ChainSpec.from_chain_str('foo:bar:3:baz') + + with self.assertRaises(ValueError): + s = ChainSpec.from_chain_str('foo:bar:a') + s = ChainSpec.from_chain_str('foo:bar') + s = ChainSpec.from_chain_str('foo') + + +if __name__ == '__main__': + unittest.main()