diff --git a/network_driver/network.py b/network_driver/network.py new file mode 100644 index 00000000..038372a7 --- /dev/null +++ b/network_driver/network.py @@ -0,0 +1,70 @@ +import random +import json +import uuid +import copy +import time + +from node import Node + +CHAIN_GENESIS = {"genesis_time": "0001-01-01T00:00:00Z", + "chain_id": "", + "validators": [], + "app_hash": ""} + + +class Network(): + + def __init__(self, max_size=1, namespace='default', chain_id=None): + # NOTE: a maximum of 19 validators can be allocated + self.max_size = max_size + self.priv_validator_list = load_validators(max_size) + self.namespace = namespace + self.genesis = copy.deepcopy(CHAIN_GENESIS) + self.genesis['chain_id'] = chain_id or uuid.uuid4().hex + self.genesis['validators'] = [extract_validator(v) for v in self.priv_validator_list] + self.nodes = [] + + for i in range(0, max_size): + self.nodes.append(Node(self.priv_validator_list[i], self.genesis, self.namespace)) + + def ensure_started(self, n=None, concurrent=True): + """ Ensure that n nodes of the network are running + NOTE: In order for the network to work properly 2/3 of the nodes + should be operational + """ + if n is None: + n = self.max_size + + n = min(n, self.max_size) + for i in range(0, n): + if not self.nodes[i].is_running: + self.nodes[i].start(not concurrent) + + nodes_online = 0 + while concurrent: + if nodes_online == n: + break + + for i in range(0, n): + if self.nodes[i].is_running: + nodes_online += 1 + + time.sleep(1) + + def stop(self, n=None): + if n is None: + n = self.max_size + + for i in range(0, n): + self.nodes[i].stop() + + +def load_validators(count): + with open('validators.json') as json_data: + return random.sample(json.load(json_data), count) + + +def extract_validator(priv_validator): + return {"pub_key": priv_validator['pub_key'], + "power": 10, + "name": ""} diff --git a/network_driver/node.py b/network_driver/node.py index 407b38c3..fd2b8891 100644 --- a/network_driver/node.py +++ b/network_driver/node.py @@ -1,20 +1,32 @@ import json import time import uuid +import copy import kubernetes -# import kubernetes.client from kubernetes.client.rest import ApiException from kubernetes.stream import stream # TODO: create a default pod spec of BDB -BDB_POD_SPEC = {} +MONGODB_SPEC = {"name": "mongodb", + "image": "mongodb:bdb-itest"} +TENDERMINT_SPEC = {"name": "tendermint", + "image": "tendermint:bdb-itest", + "command": ["/bin/bash"], + "args": ["-c", "./tendermint_entrypoint.sh '{}' '{}'"], + "ports": [{"containerPort": 46656}, + {"containerPort": 46657}]} +BIGCHAINDB_SPEC = {"name": "bigchaindb", + "image": "bigchaindb:bdb-itest", + "ports": [{"containerPort": 9984}]} class Node(): - def __init__(self, namespace='default', name=None, spec=BDB_POD_SPEC): + def __init__(self, tendermint_priv_validator=None, tendermint_genesis=None, + namespace='default', name=None): + kubernetes.config.load_kube_config() config = kubernetes.client.Configuration() config.assert_hostname = False @@ -28,12 +40,16 @@ class Node(): pod.version = 'v1' pod.kind = 'Pod' pod.metadata = {"name": self.name} - pod.spec = {"containers": [{"name": "mongodb", - "image": "mongodb:bdb-itest"}, - {"name": "tendermint", - "image": "tendermint:bdb-itest"}, - {"name": "bigchaindb", - "image": "bigchaindb:bdb-itest"}]} + + tendermint_spec = copy.deepcopy(TENDERMINT_SPEC) + [arg, command] = tendermint_spec["args"] + command = command.format(json.dumps(tendermint_priv_validator), + json.dumps(tendermint_genesis)) + tendermint_spec["args"] = [arg, command] + + pod.spec = {"containers": [MONGODB_SPEC, + tendermint_spec, + BIGCHAINDB_SPEC]} self.pod = pod def start(self, return_after_running=True): @@ -41,7 +57,7 @@ class Node(): try: self.api_instance.create_namespaced_pod(self.namespace, self.pod) if return_after_running: - for i in range(1, 20): + for i in range(1, 3600): if self.is_running: break time.sleep(1) @@ -61,6 +77,14 @@ class Node(): break time.sleep(1) + def reset(self): + self.stop_tendermint() + self.stop_bigchaindb() + self._exec_command('tendermint', 'tendermint unsafe_reset_all') + self._exec_command('bigchaindb', 'bigchaindb -y drop') + self.start_tendermint() + self.start_bigchaindb() + @property def is_running(self): """Get the current status of node""" diff --git a/network_driver/tendermint_entrypoint.sh b/network_driver/tendermint_entrypoint.sh index f81e6d0b..53f385be 100755 --- a/network_driver/tendermint_entrypoint.sh +++ b/network_driver/tendermint_entrypoint.sh @@ -1,6 +1,11 @@ #!/bin/sh echo "Starting Tendermint" +if [ "$1" != "" ] && [ "$2" != "" ] ; then + echo $1 > /tendermint/priv_validator.json + echo $2 > /tendermint/genesis.json +fi + pkill tendermint /go/bin/tendermint node & diff --git a/network_driver/validators.json b/network_driver/validators.json new file mode 100644 index 00000000..410f8257 --- /dev/null +++ b/network_driver/validators.json @@ -0,0 +1,272 @@ +[ +{ + "address": "05ADE4E6FD1774FE1AB1988D245EB03F6CFAC37E", + "pub_key": { + "type": "ed25519", + "data": "0D93D299AD2E723C744BADEE1B22560D22E1CA8859DAB277228D8F224502BA0D" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "BE24F189721C84D1362F89F9CD44E3A43DDE8369868A4F7A8D1A6DCD88DD34B00D93D299AD2E723C744BADEE1B22560D22E1CA8859DAB277228D8F224502BA0D" + } +}, +{ + "address": "64DEEAB76789E974FBED7DE351E380DC4B4862E6", + "pub_key": { + "type": "ed25519", + "data": "7363CE0C0DA05803DCA7EB34A96EC9C2EEE1E11A5D242F7A9C6E04818695CC67" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "E5AFF344DE70DC54C9765D6006D8BC9BD3AF4FDC4857D15C43D7474787E416667363CE0C0DA05803DCA7EB34A96EC9C2EEE1E11A5D242F7A9C6E04818695CC67" + } +}, +{ + "address": "9BFF02349DF4A81752153F6991CE1ECA2BE786C7", + "pub_key": { + "type": "ed25519", + "data": "D15592EAFEF39114B9EE2283DFCACB01805AA479E35A154C3A3CBA40F5F9286C" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "1C946722D9887A04C038766ABC7A214D9581EE6A2992235978E13BE9F607082CD15592EAFEF39114B9EE2283DFCACB01805AA479E35A154C3A3CBA40F5F9286C" + } +}, +{ + "address": "F6EA06EC17AC919F1EC54F5C1B65678BD68F3031", + "pub_key": { + "type": "ed25519", + "data": "FD3C04BBFDBE0FD08E894A9DE37E195F17845076DE520BE2397138E56A155DBF" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "356C6D9E0036DE767861DF7598AAC47E55E6A18CB806F14BA8716B6311AD0A20FD3C04BBFDBE0FD08E894A9DE37E195F17845076DE520BE2397138E56A155DBF" + } +}, +{ + "address": "8C3860EC2A6D41B3D3E4B667EE077C30ADAF11C0", + "pub_key": { + "type": "ed25519", + "data": "115AFE8B4F3D5CEE8A494F8A2FFFB9678AEBF95E758BA535B9C0B89B97239304" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "08A9CA24F055BBBD9158791EDC36308DB06A271E648240FE49E09DC371FF8100115AFE8B4F3D5CEE8A494F8A2FFFB9678AEBF95E758BA535B9C0B89B97239304" + } +}, +{ + "address": "E5C198689516CE9183CBC6D08335CBFFD7466B9E", + "pub_key": { + "type": "ed25519", + "data": "A1A3AAA057076AEC664F9372CB4F8CE13416E4209E40CD20E462E30551A588FE" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "92DC4805A8445297918D141982455E051AB5610ADA240F045EC8013ECE927CA8A1A3AAA057076AEC664F9372CB4F8CE13416E4209E40CD20E462E30551A588FE" + } +}, +{ + "address": "84C940AD02A55C0C15A2C210C8FF4D8B067A274B", + "pub_key": { + "type": "ed25519", + "data": "4B665A05B01EE4E4BA34D463E73CDE4027D6E745A4F83603DA258E03A158B2B4" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "70A3CF9B57D4AA240C28CD7D757B4416A6053F36BE2BD7748646B125D93970BD4B665A05B01EE4E4BA34D463E73CDE4027D6E745A4F83603DA258E03A158B2B4" + } +}, +{ + "address": "35E8AC7C23A070CC3E263DA0AA32A5825C8CF9FD", + "pub_key": { + "type": "ed25519", + "data": "00CAC839EBD142CBE809E172341F3D50AE81464227B1AEBE0D859DB487821AFC" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "0866C94EEDEA7746B470D8125576C11DF4AC1900D832DB15C9C14C897C5B47A000CAC839EBD142CBE809E172341F3D50AE81464227B1AEBE0D859DB487821AFC" + } +}, +{ + "address": "DF3E2C86F0186CDA236F29746E680C1C05025F8B", + "pub_key": { + "type": "ed25519", + "data": "A1D09C3ECAD103FCD827499F7383750D183CF9E9273F406C8C1C185A61757ADA" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "D5696F4288220076FFB1A85064F0BB7E7DB357C70805098D4089D24AF046B91AA1D09C3ECAD103FCD827499F7383750D183CF9E9273F406C8C1C185A61757ADA" + } +}, +{ + "address": "D2E2587DBE80EBF90D4E6065CB58D8A5DB147FF8", + "pub_key": { + "type": "ed25519", + "data": "68AF5889564D5414D942A553318E4A452F883298D6768D7BE42E1E64BEF2CFF7" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "98F7A27000E443E1FF2E53B390D2F0B7D023462785AE5615B7DE8BF29B283D3068AF5889564D5414D942A553318E4A452F883298D6768D7BE42E1E64BEF2CFF7" + } +}, +{ + "address": "3E92953ABBD27D3721647D19435F42E1A3025BCC", + "pub_key": { + "type": "ed25519", + "data": "F4BF1EADDD576FA61BD8EEBA56D0168792DFB1A792E115E922790D987CE733C8" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "892B2C5506840C4774527F68F23909D768FA56CF7002A31728EBC2F770275721F4BF1EADDD576FA61BD8EEBA56D0168792DFB1A792E115E922790D987CE733C8" + } +}, +{ + "address": "4A7278187F2D0BC6620AC18968ED31F4C21417F0", + "pub_key": { + "type": "ed25519", + "data": "CDE9BA5D79682C948AD697B7AC6C9B8FD4FEBAE3F1085CFB8D91B4C696D98034" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "0CFF744C7D4F269F4906D6195E401CBDB6F474024FE5080CB6D423DCB24617B0CDE9BA5D79682C948AD697B7AC6C9B8FD4FEBAE3F1085CFB8D91B4C696D98034" + } +}, +{ + "address": "551ADCD81BE1C6BE491F440F5662B01E329BFA2F", + "pub_key": { + "type": "ed25519", + "data": "25495E57E6DDD481BEFFF83F0B24A90472B400EE4FAF2F6BF075F98C4202A94B" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "715B6186782817E13E1E96BDDC99EF5821E413E042A7D95CBEAA232D7D756FD925495E57E6DDD481BEFFF83F0B24A90472B400EE4FAF2F6BF075F98C4202A94B" + } +}, +{ + "address": "E51ACC74AA13F8E755EC463D3A2AF9F3A40D73B7", + "pub_key": { + "type": "ed25519", + "data": "F6688F75E6FF3FAFD5701E72C9C607715B42DE31BA07EE65B09E302174F1864A" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "3C430178FB99D0DAF32122CC7B325D95BED0BA7493F7F635D2575D97F48350ECF6688F75E6FF3FAFD5701E72C9C607715B42DE31BA07EE65B09E302174F1864A" + } +}, +{ + "address": "2A5B46C37B14AAD4D7AF7486D3B2AA9873C2A060", + "pub_key": { + "type": "ed25519", + "data": "9F658ACB13B78BFCB1BF56D6E38A326FA0724426FFE381536C9CDC9B3AFD32E7" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "877B6DAEBA60E5E089D2575BD223B0F7A300D97EADCA62118B1974DB91000AEE9F658ACB13B78BFCB1BF56D6E38A326FA0724426FFE381536C9CDC9B3AFD32E7" + } +}, +{ + "address": "25703CCB22476E2BC5D0487BD48A245C4411BEA1", + "pub_key": { + "type": "ed25519", + "data": "0918EAB7E06592D63E413D3F2C967027AAF708299FDFDFAE0B392A7ABD6AD478" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "353CED2062782CF74DF38E51B3DE4DDD478AC42BF101E6244E914B58F00AD11E0918EAB7E06592D63E413D3F2C967027AAF708299FDFDFAE0B392A7ABD6AD478" + } +}, +{ + "address": "E1290BDCF4751E8C8C5F0BBEE976E7421AE5B355", + "pub_key": { + "type": "ed25519", + "data": "608D839D7100466D6BA6BE79C320F8B81DE93CFAA58CF9768CF921C6371F2553" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "02A2CDB0019F8CA70743930E43BDD6FDAA13F25F797721F74ED63ADBFCE4175B608D839D7100466D6BA6BE79C320F8B81DE93CFAA58CF9768CF921C6371F2553" + } +}, +{ + "address": "08E85824FA047DCD50CACEA25FED39813BAA67A5", + "pub_key": { + "type": "ed25519", + "data": "01FD364C0BD803244003880B3C5D1DC63196320FD52AAD0D65B3EE015DE61575" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null, + "priv_key": { + "type": "ed25519", + "data": "BBE5548EDF2C3EF1F933518169BA117538D981CC9BE2291D3B6E08571AEA651801FD364C0BD803244003880B3C5D1DC63196320FD52AAD0D65B3EE015DE61575" + } +} +]