def create_icx_origin_v3(self, is_raw_data=False): params = dict() params["version"] = "0x3" params["from"] = self.address params["to"] = self.to_address params["value"] = hex(int(self.value * ICX_FACTOR)) params["stepLimit"] = "0x3000000" params["timestamp"] = hex(utils.get_now_time_stamp()) params["nonce"] = "0x0" params["nid"] = self.nid if self.message is not None: params["dataType"] = "message" params["data"] = self.message.encode('utf-8').hex() hash_for_sign = self.__hash_generators["0x3"].generate_hash(params) params["signature"] = self.create_signature(hash_for_sign) if self.is_logging: logging.debug(f"icx_sendTransaction params for v3: {params}") self.__last_tx_hash = Hash32(hash_for_sign).hex_0x() icx_origin = dict() icx_origin["jsonrpc"] = "2.0" icx_origin["method"] = "icx_sendTransaction" icx_origin["id"] = random.randrange(0, 100000) icx_origin["params"] = params return icx_origin if is_raw_data else params
async def relay_all_txs(self): rs_client = ObjectManager().channel_service.rs_client if not rs_client: return items = list(self.__tx_queue.d.values()) self.__tx_queue.d.clear() for item in items: tx = item.value if not util.is_in_time_boundary(tx.timestamp, conf.TIMESTAMP_BOUNDARY_SECOND, util.get_now_time_stamp()): continue ts = TransactionSerializer.new(tx.version, tx.type(), self.blockchain.tx_versioner) if tx.version == v2.version: rest_method = RestMethod.SendTransaction2 elif tx.version == v3.version: rest_method = RestMethod.SendTransaction3 else: continue raw_data = ts.to_raw_data(tx) raw_data["from_"] = raw_data.pop("from") for i in range(conf.RELAY_RETRY_TIMES): try: await rs_client.call_async( rest_method, rest_method.value.params(**raw_data)) except Exception as e: util.logger.warning(f"Relay failed. Tx({tx}), {e}") else: break
def __pre_validate(self, tx: Transaction): if tx.hash.hex() in self.__tx_queue: raise TransactionDuplicatedHashError(tx) if not util.is_in_time_boundary(tx.timestamp, conf.TIMESTAMP_BOUNDARY_SECOND): raise TransactionOutOfTimeBound(tx, util.get_now_time_stamp())
def __pre_validate(self, tx: Transaction): if tx.hash.hex() in self.__txQueue: raise TransactionInvalidDuplicatedHash(tx.hash.hex()) if not util.is_in_time_boundary(tx.timestamp, conf.ALLOW_TIMESTAMP_BOUNDARY_SECOND): raise TransactionInvalidOutOfTimeBound(tx.hash.hex(), tx.timestamp, util.get_now_time_stamp())
def test_transaction_invalid_hash_not_match(self): wallet = IcxWallet() wallet.value = 1 # V3 params does not have `txHash`. # So it cannot raise `HashNotMatch` Exception but `AddressNotMatch` paramsv2 = wallet.create_icx_origin() time.sleep(0.1) paramsv2["timestamp"] = str(utils.get_now_time_stamp()) self.__test_wallet_exception(paramsv2, TransactionInvalidHashNotMatchError)
def __create_icx_origin(self): icx_origin = dict() icx_origin["from"] = self.send_address icx_origin["to"] = self.receive_address icx_origin["value"] = "0xde0b6b3a7640000" icx_origin["fee"] = "0x2386f26fc10000" icx_origin["timestamp"] = str(util.get_now_time_stamp()) icx_origin["nonce"] = "0x3" tx_hash = self.__create_hash(icx_origin) self.tx_hash = tx_hash icx_origin["tx_hash"] = tx_hash logging.debug(f"tx_hash : {tx_hash}") self.__create_signature_to_origin(icx_origin, tx_hash) return icx_origin
def __create_icx_origin_v3(self): icx_origin = dict() icx_origin["from"] = self.send_address icx_origin["to"] = self.receive_address icx_origin["value"] = "0xde0b6b3a7640000" icx_origin["fee"] = "0x2386f26fc10000" icx_origin["timestamp"] = str(util.get_now_time_stamp()) icx_origin["nonce"] = "0x3" icx_origin["nid"] = NID.testnet.value icx_origin["stepLimit"] = "0x12345" icx_origin["version"] = "0x3" tx_hash = self.__create_hash(icx_origin) self.tx_hash = tx_hash self.__create_signature_to_origin(icx_origin, tx_hash) return icx_origin
def create_icx_origin(self, is_raw_data=False): params = dict() params["from"] = self.address params["to"] = self.to_address params["value"] = hex(int(self.value * ICX_FACTOR)) params["fee"] = hex(int(self.fee * ICX_FACTOR)) params["timestamp"] = str(utils.get_now_time_stamp()) tx_hash = self.__hash_generator.generate_hash(params) params["tx_hash"] = tx_hash params["signature"] = self.create_signature(tx_hash) icx_origin = dict() icx_origin["jsonrpc"] = "2.0" icx_origin["method"] = "icx_sendTransaction" icx_origin["id"] = random.randrange(0, 100000) icx_origin["params"] = params self.__last_tx_hash = tx_hash print(params) return icx_origin if is_raw_data else params
def put_data(self, data, time_stamp=None): """데이터 입력 data를 받으면 해당 시간의 Time stamp와 data를 가지고 Hash를 생성해서 기록한다. :param data: Transaction에 넣고 싶은 data. data가 스트링인 경우 bytearray로 변환한다. :param time_stamp: :return Transaction의 data를 가지고 만든 Hash값: """ if isinstance(data, str): self.__data = bytearray(data, 'utf-8') else: self.__data = data if time_stamp is None: self.__time_stamp = util.get_now_time_stamp() else: self.__time_stamp = time_stamp # logging.debug("transaction Time %s , time_stamp Type %s", self.__time_stamp, type(self.__time_stamp)) return self.__generate_hash()
def test_transcation_invalid_address_not_match(self): wallet = IcxWallet() wallet.value = 1 paramsv2 = wallet.create_icx_origin() paramsv3 = wallet.create_icx_origin_v3() another_wallet = IcxWallet() # sign hash with another wallet's private key paramsv2['signature'] = another_wallet.create_signature( self.hash_generator.generate_hash(paramsv2)) paramsv3['signature'] = another_wallet.create_signature( self.hash_generator.generate_hash(paramsv3)) logging.info("address: " + wallet.address) logging.info("another addres: " + another_wallet.address) ChannelProperty().nid = '0x3' exception: TransactionInvalidAddressNotMatchError = self.__test_wallet_exception( paramsv2, TransactionInvalidAddressNotMatchError) self.assertEquals(exception.address, wallet.address) self.assertEquals(exception.expected_address, another_wallet.address) exception: TransactionInvalidAddressNotMatchError = self.__test_wallet_exception( paramsv3, TransactionInvalidAddressNotMatchError) self.assertEquals(exception.address, wallet.address) self.assertEquals(exception.expected_address, another_wallet.address) # These codes below affects hash generation. # V3 params does not have `txHash`. # So it cannot raise `HashNotMatch` Exception but `AddressNotMatch` paramsv3 = wallet.create_icx_origin_v3() paramsv3["timestamp"] = hex(utils.get_now_time_stamp()) exception: TransactionInvalidAddressNotMatchError = self.__test_wallet_exception( paramsv3, TransactionInvalidAddressNotMatchError) self.assertEquals(exception.address, wallet.address)
def __pre_validate(self, tx: Transaction): if not util.is_in_time_boundary(tx.timestamp, conf.TIMESTAMP_BOUNDARY_SECOND): raise TransactionOutOfTimeBound(tx, util.get_now_time_stamp())