def test_converter_blocks(self): """ Test with different sample blocks which are made from JSON RPC API V2 or V3 and with the genesis block. """ for block in self.blocks_0_1a: # test for block version 0.1a block_template = get_block_template_to_convert_transactions_for_genesis( block, BLOCK_0_1a) converted_block = convert(block, block_template) self.assertTrue( validate_block(block_template, block, converted_block)) # test for block version 0.3 block_template = get_block_template_to_convert_transactions_for_genesis( block, BLOCK_0_3) converted_block = convert(block, block_template, True) self.assertTrue( validate_block(block_template, block, converted_block)) for block in self.blocks_0_3: block_template = get_block_template_to_convert_transactions_for_genesis( block, BLOCK_0_3) converted_block = convert(block, block_template, True) self.assertTrue( validate_block(block_template, block, converted_block))
def test_converter_tx_results(self): """Test for multiple types of transaction results by validating it""" for tx_result in self.transaction_results: converted_transaction_result = convert(tx_result, TRANSACTION_RESULT) self.assertTrue( validate_transaction_result(TRANSACTION_RESULT, tx_result, converted_transaction_result))
def test_converter_transactions(self): """ Test with different sample transactions which made from JSON RPC API V2 or V3. """ for transaction in self.transactions: converted_transaction = convert(transaction, TRANSACTION) self.assertTrue( validate_transaction(TRANSACTION, transaction, converted_transaction))
def send_transaction_and_wait(self, signed_transaction: SignedTransaction, full_response: bool = False) -> dict: """ Sends a transaction like icx_sendTransaction, then it will wait for the result of it for specified time. Delegates to icx_sendTransactionAndWait RPC method. :param signed_transaction: A signed transaction object :param full_response: Boolean to check whether get naive dict or refined data from server :return A transaction result object """ params = signed_transaction.signed_transaction_dict result = self.__provider.make_request('icx_sendTransactionAndWait', params, full_response) if not full_response: result = convert(result, TRANSACTION_RESULT) return result
def _process_transaction_in_local(self, request: dict): params = TypeConverter.convert(request, ParamType.TRANSACTION_PARAMS_DATA) tx_hash: bytes = create_tx_hash() params['txHash'] = tx_hash tx = {'method': 'icx_sendTransaction', 'params': params} prev_block, response = self._make_and_req_block([tx]) self._write_precommit_state(prev_block) txresults: dict = response["txResults"] # convert TX result as sdk style for txresult in txresults: key: str = txresult["txHash"] self._tx_results[key] = convert(txresult, TRANSACTION_RESULT, True) return tx_hash.hex()
def get_transaction(self, tx_hash: str, full_response: bool = False) -> dict: """ Returns the transaction information requested by transaction hash. Delegates to icx_getTransactionByHash RPC method. :param tx_hash: Transaction hash prefixed with '0x' :param full_response: Boolean to check whether get naive dict or refined data from server :return: Information about a transaction """ if not is_T_HASH(tx_hash): raise DataTypeException("This hash value is unrecognized.") params = {'txHash': tx_hash} result = self.__provider.make_request('icx_getTransactionByHash', params, full_response) if not full_response: result = convert(result, TRANSACTION) return result
def wait_transaction_result(self, tx_hash: str, full_response: bool = False) -> dict: """ Returns the result of a transaction specified by the transaction hash like get_transaction_result, but waits for some time to get the transaction result instead of returning immediately if there is no finalized result. Delegates to icx_WaitTransactionResult RPC method. :param tx_hash: Hash of a transaction prefixed with '0x' :param full_response: Boolean to check whether get naive dict or refined data from server :return A transaction result object """ if not is_T_HASH(tx_hash): raise DataTypeException("This hash value is unrecognized.") params = {'txHash': tx_hash} result = self.__provider.make_request('icx_waitTransactionResult', params, full_response) if not full_response: result = convert(result, TRANSACTION_RESULT) return result
def get_block(self, value: Union[int, str], full_response: bool = False, block_version: str = DEFAULT_BLOCK_VERSION) -> dict: """ If param is height, 1. Returns block information by block height 2-1. Delegates to icx_getBlockByHeight RPC method (When Block Version is 0.1a) 2-2. Delegates to icx_getBlock RPC method (When Block Version is 0.3) Or block hash, 1. Returns block information by block hash 2-1. Delegates to icx_getBlockByHash RPC method (When Block Version is 0.1a) 2-2. Delegates to icx_getBlock RPC method (When Block Version is 0.3) Or string value same as `latest`, 1. Returns the last block information 2-1. Delegates to icx_getLastBlock RPC method (When Block Version is 0.1a) 2-2. Delegates to icx_getBlock RPC method (When Block Version is 0.3) :param value: Integer of a block height or hash of a block prefixed with '0x' or `latest` :param full_response: Boolean to check whether get naive dict or refined data from server :param block_version: returning block format version :return result: Block data """ # Nested method of returning right name of API method def return_infos_by_block_version(_prev_method: str) -> Tuple[str, Any, bool]: """ Returns API method name, block template, bool of full print by block version :param _prev_method: previous API methods. For instance, icx_getBlockByHeight, icx_getBlockByHash and icx_getLastBlock :return: method name, block template, bool of full print """ new_method = "icx_getBlock" if block_version == self.DEFAULT_BLOCK_VERSION: return _prev_method, BLOCK_0_1a, False else: return new_method, BLOCK_0_3, True # by height if is_block_height(value): params = {'height': add_0x_prefix(hex(value))} prev_method = 'icx_getBlockByHeight' # by hash elif is_hex_block_hash(value): params = {'hash': value} prev_method = 'icx_getBlockByHash' # last block elif is_predefined_block_value(value): params = None prev_method = 'icx_getLastBlock' else: raise DataTypeException("It's unrecognized block reference:{0!r}.".format(value)) method, block_template, full_print = return_infos_by_block_version(prev_method) result = self.__provider.make_request(method, params, full_response) if not full_response: block_template = get_block_template_to_convert_transactions_for_genesis(result, block_template) result = convert(result, block_template, full_print) return result