Exemple #1
0
    def init(self):
        try:
            self.blockLimit = 500
            # check chainID
            common.check_int_range(client_config.groupid,
                                   BcosClient.max_group_id)
            # check group id
            common.check_int_range(client_config.fiscoChainId,
                                   BcosClient.max_chain_id)
            # check protocol
            if client_config.client_protocol.lower(
            ) not in BcosClient.protocol_list:
                raise BcosException(
                    "invalid configuration, must be: {}".format(''.join(
                        BcosClient.protocol_list)))
            # check account keyfile
            self.keystore_file = "{}/{}".format(
                client_config.account_keyfile_path,
                client_config.account_keyfile)
            if os.path.exists(self.keystore_file) is False:
                raise BcosException(
                    ("keystore file {} doesn't exist, "
                     "please check client_config.py again "
                     "and make sure this account exist").format(
                         self.keystore_file))

            self.fiscoChainId = client_config.fiscoChainId
            self.groupid = client_config.groupid

            if client_config.client_protocol == client_config.PROTOCOL_RPC \
                    and client_config.remote_rpcurl is not None:
                self.rpc = utils.rpc.HTTPProvider(client_config.remote_rpcurl)
                self.rpc.logger = self.logger

            if client_config.client_protocol == client_config.PROTOCOL_CHANNEL:
                if os.path.exists(client_config.channel_node_cert) is False:
                    raise BcosException("{} not found!".format(
                        client_config.channel_node_cert))
                if os.path.exists(client_config.channel_node_key) is False:
                    raise BcosException("{} not found!".format(
                        client_config.channel_node_key))

                self.channel_handler = ChannelHandler()
                self.channel_handler.logger = self.logger
                self.channel_handler.initTLSContext(
                    client_config.channel_ca, client_config.channel_node_cert,
                    client_config.channel_node_key)
                self.channel_handler.start_channel(client_config.channel_host,
                                                   client_config.channel_port)
                blockNumber = self.getBlockNumber()
                self.channel_handler.setBlockNumber(blockNumber)
                self.channel_handler.getBlockNumber(self.groupid)

            self.logger.info("using protocol " + client_config.client_protocol)
            return self.getinfo()
        except Exception as e:
            raise BcosException("init bcosclient failed, reason: {}".format(e))
Exemple #2
0
 def getBlockByNumber(self, num, _includeTransactions=True):
     """
     get block according to number
     """
     cmd = "getBlockByNumber"
     number = common.check_int_range(num)
     includeTransactions = common.check_and_trans_to_bool(_includeTransactions)
     params = [self.groupid, hex(number), includeTransactions]
     return self.common_request(cmd, params)
 def exec_cmd_with_int_param(self, cmd, params):
     """
     execute cmd with one param, and the param is a int
     """
     if cmd not in RPCConsole.functions["one_int"]:
         return
     # check param
     common.check_param_num(params, 1, True)
     # check int range
     number = common.check_int_range(params[0])
     self.exec_command(cmd, [number])
Exemple #4
0
 def exec_cmd_with_two_param(self, cmd, params):
     """
     execute command with two params:
     """
     if cmd not in RPCConsole.functions["two"]:
         return
     # check param
     common.check_param_num(params, 2, False)
     if len(params) > 3:
         raise ArgumentsError("{} : params must be no more than 3")
     index = common.check_int_range(params[1])
     result = None
     # check param type
     if cmd == "getTransactionByBlockHashAndIndex":
         common.check_hash(params[0])
         result = self.exec_command(cmd, [params[0], index])
     if cmd == "getTransactionByBlockNumberAndIndex":
         number = common.check_int_range(params[0])
         result = self.exec_command(cmd, [number, index])
     self.parse_tx_and_receipt(result, cmd, params)
 def getTransactionByBlockNumberAndIndex(self, num, index):
     cmd = "getTransactionByBlockNumberAndIndex"
     common.check_int_range(num)
     common.check_int_range(index)
     params = [self.groupid, hex(num), hex(index)]
     return self.common_request(cmd, params)
 def getTransactionByBlockHashAndIndex(self, hash, index):
     cmd = "getTransactionByBlockHashAndIndex"
     common.check_hash(hash)
     common.check_int_range(index)
     params = [self.groupid, hash, hex(index)]
     return self.common_request(cmd, params)
 def getBlockHashByNumber(self, num):
     cmd = "getBlockHashByNumber"
     common.check_int_range(num)
     params = [self.groupid, hex(num)]
     return self.common_request(cmd, params)