Example #1
0
 def __init__(self,
              name: str,
              level: int,
              node_count: int,
              threshold: int,
              blockchain_id: int,
              ip_list: IPList,
              username: str = USERNAME,
              password: str = PASSWD) -> None:
     # Check if the input params are legal.
     if node_count > ip_list.get_full_count():
         raise ValueError("not enough IPs")
     self.username = username
     self.password = password
     self.level = level
     self.chain_id = name  # chain id
     self.node_count = node_count
     self.threshold = threshold
     self.blockchain_id = blockchain_id
     self.ip_list = ip_list
     self.nodes = []
     self.ips = set()
     self.if_set_number = False
     self.if_set_level = False
     self.if_set_id = False
     self.is_terminal = False
     self.config_file = None
     self.accounts = []
     self.map = defaultdict(dict)  # map of {tag: {enode: node}}
Example #2
0
 def __init__(self, ip_list: IPList, pbft_id: int, node_index: int, blockchain_id: int,
              username: str = USERNAME) -> None:
     self.id = node_index    # used in rpc call
     self.ip, self.rpc_port, self.ethereum_network_port = ip_list.get_new_port()
     self.pbft_id = pbft_id
     self.node_index = node_index
     self.blockchain_id = blockchain_id
     self.name = 'geth-pbft' + str(self.rpc_port)    # docker container name of this node
     self.enode = ''
     self.accounts = []  # accounts list of a geth node
     self._headers = {'Content-Type': 'application/json'}    # for rpc call use # 'Connection': 'close'?
     self.username = username    # user name of login user of a server
Example #3
0
        # try:
        #     written_time_str = block_time[str(block_index)]['written']
        #     written_time = datetime.strptime(written_time_str, '%Y-%m-%d-%H:%M:%S.%f')    # type: datetime
        #     tx_count = block_time[str(block_index)]['tx_count']
        #     with open('../data/elapsed_time.txt', 'a') as log:
        #         log.write('%s block index: %d, time: %s  TX count:%d\n' % (filename, block_index, written_time, tx_count))
        # except KeyError as e:
        #     print(e)

    # @staticmethod
    # def connect(node1: 'GethNode', node2: 'GethNode', tag: int):
    #     node1.ipc_add_peer(node2.enode, tag)
    #     time.sleep(0.2)

if __name__ == "__main__":
    ip_list = IPList(ip_file=IP_CONFIG)
    ip_list.stop_all_containers()
    node_count = 4
    c = SingleChain(name='01',
                    level=0,
                    node_count=node_count,
                    threshold=node_count * 2 // 3 + 1,
                    blockchain_id=121,
                    ip_list=ip_list)
    c.singlechain_start()
    c.config_consensus_chain()
    c.run_nodes()

    fail_count = 0
    for i in range(1, node_count + 1):
        node = c.get_node_by_index(i)
Example #4
0
    def __init__(self,
                 chain_id_list: [str],
                 thresh_list: [tuple],
                 ip_list: IPList,
                 username: str = USERNAME,
                 password: str = PASSWD) -> None:
        # Check if the input params are legal
        if not len(chain_id_list) == len(thresh_list):
            raise ValueError(
                "length of chain_id_list should match length of thresh_list")
        needed_count = sum(node_count for (node_count, _) in thresh_list)
        containers_count = ip_list.get_full_count()
        if needed_count > containers_count:
            raise ValueError(
                "%d containers needed but only %d containers available" %
                (needed_count, containers_count))

        self.username = username
        self.password = password
        self.chains = []
        self.structured_chains = []
        self.chain_id_list = chain_id_list
        self.thresh_list = thresh_list
        self.ip_list = ip_list
        self.max_level = len(chain_id_list[-1]) // 2
        self.if_set_number = False
        self.if_set_level = False
        self.if_set_id = False

        self.init_chains()

        threads = []
        for level in self.structured_chains[:-1]:
            for chain in level:
                t = threading.Thread(target=chain.config_consensus_chain)
                t.start()
                threads.append(t)
        for t in threads:
            t.join()

        threads = []
        if not self.structured_chains[-1][0].is_terminal:  # no terminals
            for chain in self.structured_chains[-1]:
                t = threading.Thread(target=chain.config_consensus_chain)
                t.start()
                threads.append(t)
        else:  # terminals
            for chain in self.structured_chains[-2]:
                print('--------------')
                print('config leaf chains-----------------------')
                chain.config_leaf_chain(
                    self.structured_chains[-1])  # config leaf chains
                # break  # TODO need to be optimized
            for chain in self.structured_chains[-1]:
                print('-----------------')
                print('----------------------config terminals')
                t = threading.Thread(
                    target=chain.config_terminal)  # config terminals
                t.start()
                threads.append(t)
        for t in threads:
            t.join()

        time.sleep(3)

        threads = []
        for chain in self.chains:
            t = threading.Thread(target=chain.run_nodes)
            t.start()
            threads.append(t)
            # time.sleep(1)
        for t in threads:
            t.join()
        time.sleep(0.5)