Exemple #1
0
 def __init__(self,
              networking=None,
              config=None,
              workingdir=None,
              domain_id=None,
              loglevel="all",
              logname=None):
     self.networking = networking
     self.core = networking.core
     self.stats = networking.core.stats
     self.logger = logger.get_logger(key="data_handler",
                                     level=loglevel,
                                     logname=logname)
     self.domain_id = domain_id
     self.domain_id_str = bbclib.convert_id_to_string(domain_id)
     self.config = config
     self.working_dir = workingdir
     self.storage_root = os.path.join(self.working_dir, self.domain_id_str)
     if not os.path.exists(self.storage_root):
         os.makedirs(self.storage_root, exist_ok=True)
     self.use_external_storage = self.storage_setup()
     self.replication_strategy = DataHandler.REPLICATION_ALL
     self.db_adaptors = list()
     self.dbs = list()
     self.db_setup()
Exemple #2
0
    def test_02_add_node(self):
        print("-----", sys._getframe().f_code.co_name, "-----")
        domain_id = binascii.a2b_hex("0000000000000000000000000000000000000000000000000000000000000000")
        dconf = config.get_domain_config(domain_id)
        assert dconf is not None

        node_id_str = bbclib.convert_id_to_string(bbclib.get_new_id("testnode1"))
        dconf['static_nodes'][node_id_str] = [1, 2, 3]
        config.update_config()
        with open(".bbc1/config.json", "r") as f:
            print(f.read())
Exemple #3
0
 def get_asset_group_config(self, domain_id, asset_group_id, create_if_new=False):
     dc = self.get_domain_config(domain_id, create_if_new=create_if_new)
     if dc is None:
         return None
     asset_group_str = bbclib.convert_id_to_string(asset_group_id)
     if asset_group_str not in dc['asset_group_ids']:
         dc['asset_group_ids'][asset_group_str] = {
             'storage_type': StorageType.FILESYSTEM,
             'storage_path': None,
             'advertise_in_domain0': False,
         }
     return dc['asset_group_ids'][asset_group_str]
Exemple #4
0
def get_mynodeinfo(client):
    conf = json.loads(get_config(client))
    mynode_conf = {}
    for domain_id_str in conf['domains'].keys():
        domain_id = bbclib.convert_idstring_to_bytes(domain_id_str)
        client.get_domain_peerlist(domain_id)
        results = client.callback.synchronize()
        if results is None:
            continue
        node_id_str = bbclib.convert_id_to_string(results[0][0])
        mynode_conf.setdefault(domain_id_str,
                               dict())[node_id_str] = (results[0][1],
                                                       results[0][2],
                                                       results[0][3])
    print("## {'domain_id': {'node_id': (IPv4, IPv6, port)}}")
    print(mynode_conf)
Exemple #5
0
    def save_all_peer_lists(self):
        """
        Save all peer_lists in the config file

        :return:
        """
        self.logger.info("Saving the current peer lists")
        for domain_id in self.domains.keys():
            conf = self.config.get_domain_config(domain_id)
            conf['peer_list'] = dict()
            for node_id, nodeinfo in self.domains[
                    domain_id].id_ip_mapping.items():
                nid = bbclib.convert_id_to_string(node_id)
                conf['peer_list'][nid] = [
                    nodeinfo.ipv4, nodeinfo.ipv6, nodeinfo.port
                ]
        self.logger.info("Done...")
Exemple #6
0
 def get_domain_config(self, domain_id, create_if_new=False):
     domain_id_str = bbclib.convert_id_to_string(domain_id)
     if create_if_new and domain_id_str not in self.config['domains']:
         self.config['domains'][domain_id_str] = {
             'module': 'simple_cluster',
             'static_nodes': {
                 # id : [ipv4, ipv6, port]
             },
             'peer_list': {
                 # id : [ipv4, ipv6, port]
             },
             'asset_group_ids': {
                 # id: { config }
             }
         }
     if domain_id_str in self.config['domains']:
         return self.config['domains'][domain_id_str]
     return None
Exemple #7
0
    def save_all_static_node_list(self):
        """
        Save all static nodes in the config file

        :return:
        """
        self.logger.info("Saving the neighbor list")
        for domain_id in self.domains.keys():
            conf = self.config.get_domain_config(domain_id)
            conf['static_node'] = dict()
            for node_id, nodeinfo in self.domains[domain_id][
                    'neighbor'].nodeinfo_list.items():
                if nodeinfo.is_static:
                    nid = bbclib.convert_id_to_string(node_id)
                    info = convert_to_string(
                        [nodeinfo.ipv4, nodeinfo.ipv6, nodeinfo.port])
                    conf['static_node'][nid] = info
        self.config.update_config()
        self.logger.info("Done...")
Exemple #8
0
    def create_domain(self,
                      domain_id=ZEROS,
                      network_module=None,
                      get_new_node_id=False):
        """
        Create domain and register user in the domain

        :param domain_id:
        :param network_module: string of module script file
        :param get_new_node_id: If True, the node_id is newly created again
        :return:
        """
        if domain_id in self.domains:
            return False

        nw_module = None
        if network_module is not None:
            if isinstance(network_module, bytes):
                network_module = network_module.decode()
            nw_module = __import__(network_module)

        if nw_module is None:
            return None

        conf = self.config.get_domain_config(domain_id, create_if_new=True)
        if 'node_id' not in conf or get_new_node_id:
            node_id = bbclib.get_random_id()
            conf['node_id'] = bbclib.convert_id_to_string(node_id)
            self.config.update_config()
        else:
            node_id = bbclib.convert_idstring_to_bytes(conf.get('node_id'))

        self.domains[domain_id] = nw_module.NetworkDomain(
            network=self,
            config=self.config,
            domain_id=domain_id,
            node_id=node_id,
            loglevel=self.logger.level,
            logname=self.logname)
        if domain_id != bbclib.domain_global_0:
            self.core.ledger_manager.add_domain(domain_id)
        return True
Exemple #9
0
 def get_domain_config(self, domain_id, create_if_new=False):
     domain_id_str = bbclib.convert_id_to_string(domain_id)
     if create_if_new and domain_id_str not in self.config['domains']:
         self.config['domains'][domain_id_str] = {
             'storage': {
                 "type": "internal",  # or "external"
             },
             'db': {
                 "db_type": "sqlite",            # or "mysql"
                 "db_name": "bbc_ledger.sqlite",
                 "replication_strategy": "all",  # or "p2p"/"external" (valid only in db_type=mysql)
                 "db_servers": [{"db_addr": "127.0.0.1", "db_port": 3306, "db_user": "******", "db_pass": "******"}]
                 # valid only in the case of db_type=mysql
             },
             'static_nodes': {
                 # id : [ipv4, ipv6, port]
             },
         }
     if domain_id_str in self.config['domains']:
         return self.config['domains'][domain_id_str]
     return None
    def test_01_setup(self):
        print("\n-----", sys._getframe().f_code.co_name, "-----")
        global data_handler
        dummycore = DummyCore()
        conf = config["domains"][bbclib.convert_id_to_string(domain_id)]
        data_handler = DataHandler(networking=dummycore.networking,
                                   config=conf,
                                   workingdir="testdir",
                                   domain_id=domain_id)

        global transactions
        for i in range(10):
            txobj = bbclib.BBcTransaction()
            evt = bbclib.BBcEvent()
            evt.asset_group_id = asset_group_id1
            evt.asset = bbclib.BBcAsset()
            evt.asset.add(user_id=user_id1, asset_body=b'aaaaaa')
            rtn = bbclib.BBcRelation()
            rtn.asset_group_id = asset_group_id2
            rtn.asset = bbclib.BBcAsset()
            rtn.asset.add(user_id=user_id2,
                          asset_body=b'bbbbbb',
                          asset_file=b'cccccccccc%d' % i)
            ptr = bbclib.BBcPointer()
            ptr.add(transaction_id=txid1)
            rtn.add(pointer=ptr)
            if i > 0:
                ptr = bbclib.BBcPointer()
                ptr.add(transaction_id=transactions[-1].transaction_id)
                rtn.add(pointer=ptr)
            wit = bbclib.BBcWitness()
            txobj.add(event=evt, relation=rtn, witness=wit)
            wit.add_witness(user_id1)
            sig = txobj.sign(key_type=bbclib.KeyType.ECDSA_SECP256k1,
                             private_key=keypair1.private_key,
                             public_key=keypair1.public_key)
            txobj.add_signature(user_id=user_id1, signature=sig)
            txobj.digest()
            transactions.append(txobj)
Exemple #11
0
    def add_static_node_to_domain(self, domain_id, node_id, ipv4, ipv6, port):
        """
        Add static peer node for the domain

        :param domain_id:
        :param node_id:
        :param ipv4:
        :param ipv6:
        :param port:
        :return:
        """
        if domain_id not in self.domains:
            return
        self.domains[domain_id].add_peer_node_ip46(node_id, ipv4, ipv6, port)
        conf = self.config.get_domain_config(domain_id)
        if node_id not in conf['static_nodes']:
            if not isinstance(ipv4, str):
                ipv4 = ipv4.decode()
            if not isinstance(ipv6, str):
                ipv6 = ipv6.decode()
            conf['static_nodes'][bbclib.convert_id_to_string(node_id)] = [
                ipv4, ipv6, port
            ]
Exemple #12
0
user_id1 = bbclib.get_new_id("destination_id_test1")
user_id2 = bbclib.get_new_id("destination_id_test2")
domain_id = bbclib.get_new_id("test_domain")
asset_group_id1 = bbclib.get_new_id("asset_group_1")
asset_group_id2 = bbclib.get_new_id("asset_group_2")
txid1 = bbclib.get_new_id("dummy_txid_1")
txid2 = bbclib.get_new_id("dummy_txid_2")
keypair1 = bbclib.KeyPair()
keypair1.generate()

transactions = list()

data_handler = None
config = {
    "domains": {
        bbclib.convert_id_to_string(domain_id): {
            "storage": {
                "type": "internal",
            },
            "db": {
                "db_type":
                "mysql",
                "db_name":
                "bbctest",
                'replication_strategy':
                "external",
                "db_servers": [{
                    "db_addr": "127.0.0.1",
                    "db_port": 3306,
                    "db_user": "******",
                    "db_pass": "******"
Exemple #13
0
        ipv4 = str(dst_ip)
    else:
        ipv6 = str(dst_ip)
    print("Request domain_ping to %s, %s, %d" % (ipv4, ipv6, dst_port))
    bbcclient.send_domain_ping(domain_id, ipv4, ipv6, dst_port)


if __name__ == '__main__':
    port = None
    parsed_args = argument_parser()
    if parsed_args.domain_id is None:
        print("### -d option is mandatory!")
        sys.exit(1)
    if parsed_args.id:
        value = bbclib.get_new_id(parsed_args.id, include_timestamp=False)
        print(bbclib.convert_id_to_string(value))
        sys.exit(0)
    if parsed_args.timebaseid:
        value = bbclib.get_new_id(parsed_args.id, include_timestamp=True)
        print(bbclib.convert_id_to_string(value))
        sys.exit(0)

    if parsed_args.ip4address:
        addr = parsed_args.ip4address
    if parsed_args.ip6address:
        addr = parsed_args.ip6address
    port = parsed_args.port
    bbcclient = bbc_app.BBcAppClient(host=addr, port=port, loglevel="all")

    domain_id = bbclib.convert_idstring_to_bytes(parsed_args.domain_id)
Exemple #14
0
    def create_domain(self, domain_id=ZEROS, config=None):
        """
        Create domain and register user in the domain

        :param domain_id:
        :param config: config for the domain
        :return:
        """
        if domain_id in self.domains:
            return False

        conf = self.config.get_domain_config(domain_id, create_if_new=True)
        if config is not None:
            conf.update(config)
        if 'node_id' not in conf:
            node_id = bbclib.get_random_id()
            conf['node_id'] = bbclib.convert_id_to_string(node_id)
            self.config.update_config()
        else:
            node_id = bbclib.convert_idstring_to_bytes(conf.get('node_id'))

        self.domains[domain_id] = dict()
        self.domains[domain_id]['node_id'] = node_id
        self.domains[domain_id]['name'] = node_id.hex()[:4]
        self.domains[domain_id]['neighbor'] = NeighborInfo(
            network=self,
            domain_id=domain_id,
            node_id=node_id,
            my_info=self.get_my_socket_info(node_id))
        self.domains[domain_id]['topology'] = TopologyManagerBase(
            network=self,
            domain_id=domain_id,
            node_id=node_id,
            logname=self.logname,
            loglevel=self.loglevel)
        self.domains[domain_id]['user'] = UserMessageRouting(
            self, domain_id, logname=self.logname, loglevel=self.loglevel)
        self.get_domain_keypair(domain_id)

        workingdir = self.config.get_config()['workingdir']
        if domain_id == ZEROS:
            self.domains[domain_id]['data'] = DataHandlerDomain0(
                self,
                domain_id=domain_id,
                logname=self.logname,
                loglevel=self.loglevel)
            self.domain0manager = Domain0Manager(self,
                                                 node_id=node_id,
                                                 logname=self.logname,
                                                 loglevel=self.loglevel)
        else:
            self.domains[domain_id]['data'] = DataHandler(
                self,
                config=conf,
                workingdir=workingdir,
                domain_id=domain_id,
                logname=self.logname,
                loglevel=self.loglevel)

        self.domains[domain_id]['repair'] = RepairManager(
            self,
            domain_id,
            workingdir=workingdir,
            logname=self.logname,
            loglevel=self.loglevel)

        if self.domain0manager is not None:
            self.domain0manager.update_domain_belong_to()
            for dm in self.domains.keys():
                if dm != ZEROS:
                    self.domains[dm]['neighbor'].my_info.update(domain0=True)
            self.domains[domain_id]['topology'].update_refresh_timer_entry(1)
        self.stats.update_stats_increment("network", "num_domains", 1)
        return True