def test_contract_htdf_faucet_deposit(conftest_args): with open('sol/htdf_faucet_sol_HtdfFaucet.abi', 'r') as abifile: # abi = abifile.readlines() abijson = abifile.read() # print(abijson) abi = json.loads(abijson) assert len(htdf_faucet_contract_address) > 0 contract_address = Address(htdf_faucet_contract_address[0]) htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) deposit_amount = htdf_to_satoshi(10) deposit_tx = hc.functions.deposit().buildTransaction_htdf() data = remove_0x_prefix(deposit_tx['data']) from_addr = Address(conftest_args['ADDRESS']) private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=deposit_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo='htdf_faucet.deposit()').build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) assert tx['logs'][0]['success'] == True time.sleep(8) # wait for chain state update contract_acc = htdfrpc.get_account_info(address=contract_address.address) assert contract_acc is not None assert contract_acc.balance_satoshi == deposit_amount pass
def test_txsize_excess_100000bytes(conftest_args): gas_wanted = 7500000 gas_price = 100 tx_amount = 1 # in protocol v0 v1, TxSizeLimit is 1200000 bytes # in protocol V2, TxSizeLimit is 100000 bytes data = 'ff' * 50000 memo = 'test_normal_transaction_with_data_excess_100000bytes' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) upgrade_info = htdfrpc.get_upgrade_info() protocol_version = int( upgrade_info['current_version']['UpgradeInfo']['Protocol']['version']) from_addr = Address(conftest_args['ADDRESS']) new_to_addr = HtdfPrivateKey('').address # to_addr = Address('htdf1jrh6kxrcr0fd8gfgdwna8yyr9tkt99ggmz9ja2') private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=new_to_addr, amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) if protocol_version < 2: # v0 and v1 # TODO: pass elif protocol_version == 2: # v2 try: tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) assert True == False except Exception as e: errmsg = '{}'.format(e) print(e) pass else: raise Exception("invalid protocol version {}".format(protocol_version)) pass
def check_balance(conftest_args): print("====> check_balance <=======") htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) from_addr = Address(conftest_args['ADDRESS']) acc = htdfrpc.get_account_info(address=from_addr.address) assert acc.balance_satoshi > htdf_to_satoshi(100000)
def test_normal_tx_send(conftest_args, to_addr): gas_wanted = 200000 gas_price = 100 tx_amount = 101 * 10**8 data = '' memo = 'test_normal_transaction' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) from_addr = Address(conftest_args['ADDRESS']) # new_to_addr = HtdfPrivateKey('').address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=to_addr, amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) mempool = htdfrpc.get_mempool_trasactions() pprint(mempool) memtx = htdfrpc.get_mempool_transaction(transaction_hash=tx_hash) pprint(memtx) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) tx = htdfrpc.get_transaction(transaction_hash=tx_hash)
def test_deploy_contract(conftest_args, bytecode): """ test create hrc20 token contract which implement HRC20. # test contract AJC.sol """ gas_wanted = 5000000 gas_price = 100 tx_amount = 0 data = bytecode memo = 'test_deploy_contract' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) from_addr = Address(conftest_args['ADDRESS']) # new_to_addr = HtdfPrivateKey('').address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address='', amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) assert tx['logs'][0]['success'] == True # txlog = tx['logs'][0]['log'] # txlog = json.loads(txlog) assert tx['gas_wanted'] == str(gas_wanted) assert int(tx['gas_used']) <= gas_wanted tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == '' # new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(8) # wait for chain state update # to_acc = htdfrpc.get_account_info(address=new_to_addr.address) # assert to_acc is not None # assert to_acc.balance_satoshi == tx_amount from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * int(tx['gas_used'])) log = tx['logs'][0]['log'] conaddr = log[log.find("contract address:"):log.find(", output:")] contract_address = conaddr.replace('contract address:', '').strip() contract_address = Address.hexaddr_to_bech32(contract_address) print(contract_address) hrc20_contract_address.append(contract_address) pass
def test_ecrecover(conftest_args, abi): assert len(hrc20_contract_address) > 0 contract_address = Address(hrc20_contract_address[0]) htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) # new_to_addr = HtdfPrivateKey('').address from_addr = Address(conftest_args['ADDRESS']) private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) #################### # blk = htdfrpc.get_latest_block() # last_block_number = int(blk['block_meta']['header']['height']) # last_block_number = 100 # reveal = 99 #int(os.urandom(32).hex(), 16) # commitLastBlock = unhexlify('%010x' % last_block_number) # 和uint40对应 # commit = keccak( unhexlify('%064x' % reveal) ) # print('0x' + commit.hex() ) # privateKey = unhexlify('dbbad2a5682517e4ff095f948f721563231282ca4179ae0dfea1c76143ba9607') # pk = PrivateKey(privateKey, CoinCurveECCBackend) # sh = keccak(commitLastBlock + commit) # print('sh ==========> {}'.format(sh.hex())) # sig = pk.sign_msg_hash(message_hash=sh) # print('"0x' + sig.to_bytes()[:32].hex() + '"') # print('"0x'+ sig.to_bytes()[32:-1].hex() + '"') # print( sig.to_bytes()[-1]) # r = sig.to_bytes()[:32] # s = sig.to_bytes()[32:-1] # v = sig.to_bytes()[-1] + 27 ###################### callTx = hc.functions.testecrecover().buildTransaction_htdf() data = remove_0x_prefix(callTx['data']) print('========> data{}'.format(remove_0x_prefix(callTx['data']))) # data = '227ada370000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000640b42b6393c1f53060fe3ddbfcd7aadcca894465a5a438f69c87d790b2299b9b22e07166e8a445db2a2a805af052fbbed92ad61d3cb1f81ebc1eaf927c6474f507996175577577bfe8782299a3153327e56cc28e108f4c9105b646e46d5179557000000000000000000000000000000000000000000000000000000000000001c' signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=0, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=5000000, data=data, memo='test_ecrecover').build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) pass
def test_contract_htdf_faucet_setOnceAmount(conftest_args): with open('sol/htdf_faucet_sol_HtdfFaucet.abi', 'r') as abifile: # abi = abifile.readlines() abijson = abifile.read() # print(abijson) abi = json.loads(abijson) assert len(htdf_faucet_contract_address) > 0 contract_address = Address(htdf_faucet_contract_address[0]) htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) once_htdf_satoshi_begin = hc.call(hc.functions.onceAmount()) once_htdf_to_set = int(3.5 * 10**8) deposit_tx = hc.functions.setOnceAmount( amount=once_htdf_to_set).buildTransaction_htdf() data = remove_0x_prefix(deposit_tx['data']) assert len(data) > 0 and ((len(data) & 1) == 0) # test for non-owner , it will be failed from_addr = Address('htdf188tzdtuka7yc6xnkm20pv84f3kgthz05650au5') private_key = HtdfPrivateKey( 'f3024714bb950cfbd2461b48ef4d3a9aea854309c4ab843fda57be34cdaf856e') from_acc = htdfrpc.get_account_info(address=from_addr.address) if from_acc is None or from_acc.balance_satoshi < 100 * 200000: gas_wanted = 30000 gas_price = 100 tx_amount = htdf_to_satoshi(10) #data = '' memo = 'test_normal_transaction' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) g_from_addr = Address(conftest_args['ADDRESS']) # new_to_addr = HtdfPrivateKey('').address g_from_private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) g_from_acc = htdfrpc.get_account_info(address=g_from_addr.address) assert g_from_acc is not None assert g_from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=g_from_addr, to_address=from_addr, amount_satoshi=tx_amount, sequence=g_from_acc.sequence, account_number=g_from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data='', memo=memo).build_and_sign(private_key=g_from_private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) time.sleep(10) assert tx['logs'][0]['success'] == True from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None and from_acc.balance_satoshi >= 100 * 200000 signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=0, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo='htdf_faucet.setOnceAmount()').build_and_sign( private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) assert tx['logs'][0]['success'] == False time.sleep(8) # wait for chain state update once_amount_satoshi_end = hc.call(cfn=hc.functions.onceAmount()) assert once_amount_satoshi_end == once_htdf_satoshi_begin # test for owner , it should be succeed from_addr = Address(conftest_args['ADDRESS']) private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=0, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo='htdf_faucet.deposit()').build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) assert tx['logs'][0]['success'] == True time.sleep(8) # wait for chain state update once_amount_satoshi_end = hc.call(cfn=hc.functions.onceAmount()) assert once_amount_satoshi_end == once_htdf_to_set pass
def deploy_htdf_faucet(conftest_args): """ run this test case, if only run single test run this test case, if run this test file """ gas_wanted = 3000000 gas_price = 100 tx_amount = 1 data = '60606040526305f5e10060008190555033600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506104558061005f6000396000f30060606040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680638da5cb5b14610072578063bb3ded46146100c7578063c15a96bb146100dc578063d0e30db0146100ff578063ff8dd6bf14610109575b600080fd5b341561007d57600080fd5b610085610132565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156100d257600080fd5b6100da610158565b005b34156100e757600080fd5b6100fd6004808035906020019091905050610333565b005b6101076103dd565b005b341561011457600080fd5b61011c610423565b6040518082815260200191505060405180910390f35b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054148061023257506000600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541180156102315750603c600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020544203115b5b151561023d57600080fd5b6000543073ffffffffffffffffffffffffffffffffffffffff16311015151561026557600080fd5b42600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055503373ffffffffffffffffffffffffffffffffffffffff166108fc6000549081150290604051600060405180830381858888f1935050505015156102eb57600080fd5b6000543373ffffffffffffffffffffffffffffffffffffffff167f5c73cf3606811df094e3c59bfbf3fd8fdf855b621938753f7604486280d4ca7860405160405180910390a3565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561038f57600080fd5b80600081905550803373ffffffffffffffffffffffffffffffffffffffff167f242a21804f833c63c9cb0bec112566c96b004760f7733cc0e6daf72f4b27e70660405160405180910390a350565b343373ffffffffffffffffffffffffffffffffffffffff167fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c60405160405180910390a3565b600054815600a165627a7a72305820a702de9668441382f4cf69e1418ba683a8463dc6aa3d6fa121d4a02e07d20c2b0029' memo = 'test_deploy_htdf_faucet' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) from_addr = Address(conftest_args['ADDRESS']) # new_to_addr = HtdfPrivateKey('').address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) print('from_acc balance: {}'.format(from_acc.balance_satoshi)) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address='', amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) assert tx['logs'][0]['success'] == True txlog = tx['logs'][0]['log'] txlog = json.loads(txlog) assert tx['gas_wanted'] == str(gas_wanted) assert int(tx['gas_used']) <= gas_wanted tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == '' # new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(8) # wait for chain state update # to_acc = htdfrpc.get_account_info(address=new_to_addr.address) # assert to_acc is not None # assert to_acc.balance_satoshi == tx_amount from_acc_new = htdfrpc.get_account_info(address=from_addr.address) print("from_acc_new balance is {}".format(from_acc_new.balance_satoshi)) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * int(tx['gas_used'])) logjson = json.loads(tx['logs'][0]['log']) contract_address = logjson['contract_address'] htdf_faucet_contract_address.append(contract_address) pass
def test_hrc20_approve_transferFrom(conftest_args): with open('sol/AJC_sol_AJCToken.abi', 'r') as abifile: # abi = abifile.readlines() abijson = abifile.read() # print(abijson) abi = json.loads(abijson) assert len(hrc20_contract_address) > 0 contract_address = Address(hrc20_contract_address[0]) htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) new_to_priv_key = HtdfPrivateKey('') new_to_addr = new_to_priv_key.address from_addr = Address(conftest_args['ADDRESS']) private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) cfnBalanceOf = hc.functions.balanceOf(_owner=to_checksum_address(from_addr.hex_address)) balanceFrom_begin = hc.call(cfn=cfnBalanceOf) print(balanceFrom_begin) ################## test for approve approve_amount = int(10002 * 10 ** 18) approve_tx = hc.functions.approve( _spender=to_checksum_address(new_to_addr.hex_address), _value=approve_amount).buildTransaction_htdf() data = remove_0x_prefix(approve_tx['data']) signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=0, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo='test_hrc20_approve' ).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) # self.assertTrue( len(tx_hash) == 64) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) ################## transfer some htdf for fee signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=new_to_addr, amount_satoshi=200000 * 100, sequence=from_acc.sequence + 1, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data='', memo='some htdf for fee' ).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) # self.assertTrue( len(tx_hash) == 64) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) time.sleep(8) ################# test for transferFrom transferFrom_tx = hc.functions.transferFrom( _from=to_checksum_address(from_addr.hex_address), _to=to_checksum_address(new_to_addr.hex_address), _value=approve_amount ).buildTransaction_htdf() data = remove_0x_prefix(transferFrom_tx['data']) to_acc_new = htdfrpc.get_account_info(address=new_to_addr.address) signed_tx = HtdfTxBuilder( from_address=new_to_addr, to_address=contract_address, amount_satoshi=0, sequence=to_acc_new.sequence, account_number=to_acc_new.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo='test_hrc20_transferFrom' ).build_and_sign(private_key=new_to_priv_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) # self.assertTrue( len(tx_hash) == 64) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) ########### balanceOf cfnBalanceOf = hc.functions.balanceOf(_owner=to_checksum_address(new_to_addr.hex_address)) balanceTo = hc.call(cfn=cfnBalanceOf) print(balanceTo) cfnBalanceOf = hc.functions.balanceOf(_owner=to_checksum_address(from_addr.hex_address)) balanceFrom_end = hc.call(cfn=cfnBalanceOf) print(balanceFrom_end) # check balance assert balanceFrom_end == balanceFrom_begin - approve_amount assert balanceTo == approve_amount pass
def test_5000_normal_send_txs(conftest_args): """ Node's mempool size is 5000 txs by default, if mempool is full, tx will be rejected. the blockGasLimit of tendermint is 15,000,000 , if a tx's gasWanted is 30000, single block could include 500 txs. """ txs_count = 5000 gas_wanted = 30000 gas_price = 100 tx_amount = 1 data = '' memo = 'test_2000_normal_send_txs' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) from_addr = Address(conftest_args['ADDRESS']) new_to_addr = HtdfPrivateKey('').address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > (gas_price * gas_wanted + tx_amount) * txs_count signed_tx_list = [] for n in range(txs_count): signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=new_to_addr, amount_satoshi=tx_amount, sequence=from_acc.sequence + n, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) signed_tx_list.append(signed_tx) tx_hash_list = [] for n in range(txs_count): tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx_list[n]) # print('tx_hash: {}'.format(tx_hash)) tx_hash_list.append(tx_hash) tx = htdfrpc.get_tranaction_until_timeout( transaction_hash=tx_hash_list[-1], timeout_secs=(txs_count / 500.0 * 6.0)) assert tx['logs'][0]['success'] == True time.sleep(5) # wait for chain state update to_acc = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc is not None assert to_acc.balance_satoshi == tx_amount * txs_count from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + txs_count assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * gas_wanted + tx_amount) * txs_count pass
def test_balance_less_than_fee_tx(conftest_args): """ test for issue #6 In protocol v0 and v1 , if a account's balance less than fee( gas_wanted * gas_price) its transactions still could be included into a block. In protocol v2, if a account's balance less than fee(gas_wanted * gas_price), its transaction will be rejected when it be broadcasted. """ gas_wanted = 30000 gas_price = 100 tx_amount = 1 data = '' memo = 'test_balance_less_than_fee_tx' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) upgrade_info = htdfrpc.get_upgrade_info() protocol_version = int( upgrade_info['current_version']['UpgradeInfo']['Protocol']['version']) from_addr = Address(conftest_args['ADDRESS']) new_to_privkey = HtdfPrivateKey('') new_to_addr = new_to_privkey.address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=new_to_addr, amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) assert tx['logs'][0]['success'] == True time.sleep(5) # wait for chain state update to_acc = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc is not None assert to_acc.balance_satoshi == tx_amount signed_tx_back = HtdfTxBuilder( from_address=new_to_addr, to_address=from_addr, amount_satoshi=tx_amount, sequence=to_acc.sequence, account_number=to_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=new_to_privkey) if protocol_version < 2: tx_hash_back = htdfrpc.broadcast_tx(tx_hex=signed_tx_back) print('tx_hash_back: {}'.format(tx_hash_back)) tx = htdfrpc.get_tranaction_until_timeout( transaction_hash=tx_hash_back) assert tx['logs'][0]['success'] == False time.sleep(5) # wait for chain state update to_acc_new = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc_new is not None assert to_acc_new.address == to_acc.address assert to_acc_new.balance_satoshi == to_acc.balance_satoshi # balance not change assert to_acc_new.sequence == to_acc.sequence + 1 # sequence changed assert to_acc_new.account_number == to_acc.account_number elif protocol_version == 2: try: tx_hash_back = htdfrpc.broadcast_tx(tx_hex=signed_tx_back) print('tx_hash_back: {}'.format(tx_hash_back)) # error assert False == True except Exception as e: # ok print(e) to_acc_new = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc_new is not None assert to_acc_new.address == to_acc.address assert to_acc_new.balance_satoshi == to_acc.balance_satoshi # balance not change assert to_acc_new.sequence == to_acc.sequence # sequence not change assert to_acc_new.account_number == to_acc.account_number pass else: raise Exception("invalid protocol version:{}".format(protocol_version)) pass
def test_normal_tx_gas_wanted_excess_7500000(conftest_args): gas_wanted = 7500001 # v2 max gas_wanted is 7500000 gas_price = 100 tx_amount = 1 data = '' memo = 'test_normal_transaction_gas_wanted_excess_7500000' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) upgrade_info = htdfrpc.get_upgrade_info() protocol_version = int( upgrade_info['current_version']['UpgradeInfo']['Protocol']['version']) from_addr = Address(conftest_args['ADDRESS']) new_to_addr = HtdfPrivateKey('').address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=new_to_addr, amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) tx_hash = '' try: tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) except Exception as e: assert protocol_version == 2 errmsg = '{}'.format(e) print(e) assert 'Tx could not excess TxGasLimit[7500000]' in errmsg if protocol_version < 2: tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) assert tx['logs'][0]['success'] == True assert tx['gas_wanted'] == str(gas_wanted) assert int(tx['gas_used']) < gas_wanted tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(5) # wait for chain state update to_acc = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc is not None assert to_acc.balance_satoshi == tx_amount from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * int(tx['gas_used']) + tx_amount) pass
def test_normal_tx_send(conftest_args): gas_wanted = 30000 gas_price = 100 tx_amount = 1 data = '' memo = 'test_normal_transaction' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) from_addr = Address(conftest_args['ADDRESS']) new_to_addr = HtdfPrivateKey('').address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=new_to_addr, amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) mempool = htdfrpc.get_mempool_trasactions() pprint(mempool) memtx = htdfrpc.get_mempool_transaction(transaction_hash=tx_hash) pprint(memtx) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) tx = htdfrpc.get_transaction(transaction_hash=tx_hash) assert tx['logs'][0]['success'] == True assert tx['gas_wanted'] == str(gas_wanted) assert tx['gas_used'] == str(gas_wanted) tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(8) # wait for chain state update to_acc = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc is not None assert to_acc.balance_satoshi == tx_amount from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * gas_wanted + tx_amount)
def test_normal_tx_gas_wanted_adjust(conftest_args): # in protocol V2, if gasWanted is greater than 210000, anteHandler will adjust tx's gasWanted to 30000 # in protocol V2, max gasWanted is 7500000 gas_wanted = 210001 # normal htdf send tx gas_used is 30000 normal_send_tx_gas_wanted = 30000 gas_price = 100 tx_amount = 1 data = '' memo = 'test_normal_transaction_gas_wanted' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) upgrade_info = htdfrpc.get_upgrade_info() protocol_version = int( upgrade_info['current_version']['UpgradeInfo']['Protocol']['version']) from_addr = Address(conftest_args['ADDRESS']) new_to_addr = HtdfPrivateKey('').address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=new_to_addr, amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) mempool = htdfrpc.get_mempool_trasactions() pprint(mempool) memtx = htdfrpc.get_mempool_transaction(transaction_hash=tx_hash) pprint(memtx) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) tx = htdfrpc.get_transaction(transaction_hash=tx_hash) if protocol_version < 2: # v0 and v1 assert tx['logs'][0]['success'] == True assert tx['gas_wanted'] == str(gas_wanted) assert int(tx['gas_used']) < gas_wanted assert int(tx['gas_used']) == normal_send_tx_gas_wanted tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(5) # want for chain state update to_acc = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc is not None assert to_acc.balance_satoshi == tx_amount from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * int(tx['gas_used']) + tx_amount) elif protocol_version == 2: # v2 , # if gasWanted is greater than 210000, anteHandler will adjust tx's gasWanted to 30000 assert tx['logs'][0]['success'] == True # Because of `data` is empty, so v2's anteHander adjusts tx's gasWanted to 30000. assert int(tx['gas_wanted']) == normal_send_tx_gas_wanted assert int(tx['gas_used']) == normal_send_tx_gas_wanted tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(5) # wait for chain state update to_acc = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc is not None from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * normal_send_tx_gas_wanted + tx_amount) else: raise Exception("invalid protocol version {}".format(protocol_version)) pass
def test_hrc20_transfer(conftest_args): with open('sol/AJC_sol_AJCToken.abi', 'r') as abifile: # abi = abifile.readlines() abijson = abifile.read() # print(abijson) abi = json.loads(abijson) assert len(hrc20_contract_address) > 0 contract_address = Address(hrc20_contract_address[0]) htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) new_to_addr = HtdfPrivateKey('').address from_addr = Address(conftest_args['ADDRESS']) private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) cfnBalanceOf = hc.functions.balanceOf(_owner=to_checksum_address(from_addr.hex_address)) balanceFrom_begin = hc.call(cfn=cfnBalanceOf) print(balanceFrom_begin) transfer_token_amount = int(10001 * 10 ** 18) transfer_tx = hc.functions.transfer( _to=to_checksum_address(new_to_addr.hex_address), _value=transfer_token_amount).buildTransaction_htdf() data = remove_0x_prefix(transfer_tx['data']) signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=0, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo='test_hrc20_transfer' ).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) time.sleep(8) # check balance of token cfnBalanceOf = hc.functions.balanceOf(_owner=to_checksum_address(from_addr.hex_address)) balanceFrom_end = hc.call(cfn=cfnBalanceOf) print(balanceFrom_end) assert balanceFrom_end == balanceFrom_begin - transfer_token_amount cfnBalanceOf = hc.functions.balanceOf(_owner=to_checksum_address(new_to_addr.hex_address)) balance = hc.call(cfn=cfnBalanceOf) print(balance) assert balance == transfer_token_amount pass
def test_create_hrc20_token_contract(conftest_args): """ test create hrc20 token contract which implement HRC20. # test contract AJC.sol """ gas_wanted = 2000000 gas_price = 100 tx_amount = 1 data = '60606040526000600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550341561005157600080fd5b6aa49be39dc14cb8270000006003819055506003546000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610d61806100b76000396000f3006060604052600436106100af576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100b4578063095ea7b31461014257806318160ddd1461019c57806323b872dd146101c5578063313ce5671461023e5780634d853ee51461026d57806370a08231146102c257806393c32e061461030f57806395d89b4114610348578063a9059cbb146103d6578063dd62ed3e14610430575b600080fd5b34156100bf57600080fd5b6100c761049c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101075780820151818401526020810190506100ec565b50505050905090810190601f1680156101345780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014d57600080fd5b610182600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506104d5565b604051808215151515815260200191505060405180910390f35b34156101a757600080fd5b6101af61065c565b6040518082815260200191505060405180910390f35b34156101d057600080fd5b610224600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610662565b604051808215151515815260200191505060405180910390f35b341561024957600080fd5b610251610959565b604051808260ff1660ff16815260200191505060405180910390f35b341561027857600080fd5b61028061095e565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156102cd57600080fd5b6102f9600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610984565b6040518082815260200191505060405180910390f35b341561031a57600080fd5b610346600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506109cc565b005b341561035357600080fd5b61035b610a6c565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561039b578082015181840152602081019050610380565b50505050905090810190601f1680156103c85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156103e157600080fd5b610416600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610aa5565b604051808215151515815260200191505060405180910390f35b341561043b57600080fd5b610486600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610c77565b6040518082815260200191505060405180910390f35b6040805190810160405280600981526020017f414a4320636861696e000000000000000000000000000000000000000000000081525081565b60008082148061056157506000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054145b151561056c57600080fd5b81600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60035481565b600080600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050600073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161415151561072057600080fd5b80831115151561072f57600080fd5b610780836000808873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610cfe90919063ffffffff16565b6000808773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610813836000808773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610d1790919063ffffffff16565b6000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506108688382610cfe90919063ffffffff16565b600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508373ffffffffffffffffffffffffffffffffffffffff168573ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef856040518082815260200191505060405180910390a360019150509392505050565b601281565b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141515610a2857600080fd5b80600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6040805190810160405280600381526020017f414a43000000000000000000000000000000000000000000000000000000000081525081565b60008073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1614151515610ae257600080fd5b610b33826000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610cfe90919063ffffffff16565b6000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610bc6826000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610d1790919063ffffffff16565b6000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b6000828211151515610d0c57fe5b818303905092915050565b6000808284019050838110151515610d2b57fe5b80915050929150505600a165627a7a7230582043a3cd97586e182885676a8c6e6413be040c6f728b9763d794ecdbfff9a4b7c90029' memo = 'test_create_hrc20_token_contract' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) from_addr = Address(conftest_args['ADDRESS']) # new_to_addr = HtdfPrivateKey('').address private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address='', amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo ).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) assert tx['logs'][0]['success'] == True txlog = tx['logs'][0]['log'] txlog = json.loads(txlog) assert tx['gas_wanted'] == str(gas_wanted) assert int(tx['gas_used']) <= gas_wanted tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == '' # new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(8) # wait for chain state update # to_acc = htdfrpc.get_account_info(address=new_to_addr.address) # assert to_acc is not None # assert to_acc.balance_satoshi == tx_amount from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - (gas_price * int(tx['gas_used'])) logjson = json.loads(tx['logs'][0]['log']) contract_address = logjson['contract_address'] hrc20_contract_address.append(contract_address) pass
def test_contract_htdf_faucet_getOneHtdf(conftest_args): """ run test_contract_htdf_faucet_deposit before this test case, to ensure the faucet contract has enough HTDF balance. """ with open('sol/htdf_faucet_sol_HtdfFaucet.abi', 'r') as abifile: # abi = abifile.readlines() abijson = abifile.read() # print(abijson) abi = json.loads(abijson) assert len(htdf_faucet_contract_address) > 0 contract_address = Address(htdf_faucet_contract_address[0]) htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) # because of the limitions in contract, a address could only get 1 htdf every minute. # so the second loop of this for-loop should be failed as expected. expected_result = [True, False] for n in range(2): contract_acc_begin = htdfrpc.get_account_info( address=contract_address.address) assert contract_acc_begin is not None deposit_tx = hc.functions.getOneHtdf().buildTransaction_htdf() data = remove_0x_prefix(deposit_tx['data']) from_addr = Address(conftest_args['ADDRESS']) private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=0, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo='htdf_faucet.getOneHtdf()').build_and_sign( private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) # self.assertTrue( len(tx_hash) == 64) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) # tx = htdfrpc.get_transaction(transaction_hash=tx_hash) # pprint(tx) assert tx['logs'][0]['success'] == expected_result[n] time.sleep(8) # wait for chain state update if expected_result[n] == True: once_htdf_satoshi = hc.call(hc.functions.onceAmount()) contract_acc_end = htdfrpc.get_account_info( address=contract_address.address) assert contract_acc_end is not None assert contract_acc_end.balance_satoshi == contract_acc_begin.balance_satoshi - once_htdf_satoshi elif expected_result[n] == False: contract_acc_end = htdfrpc.get_account_info( address=contract_address.address) assert contract_acc_end is not None assert contract_acc_end.balance_satoshi == contract_acc_begin.balance_satoshi # contract's balance doesn't changes pass
def hrc20_batch_transfer(token_name: str, contract_addr: str, addrs_list: list, cfg: dict): """ hrc20批量转账 """ with open('./hrc20.abi', 'r') as abifile: # abi = abifile.readlines() abijson = abifile.read() # print(abijson) abi = json.loads(abijson) contract_address = Address(contract_addr) htdfrpc = HtdfRPC(chaid_id=COMMON['CHAINID'], rpc_host=COMMON['RPC_HOST'], rpc_port=COMMON['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) from_addr = Address(cfg['ADDRESS']) private_key = HtdfPrivateKey(cfg['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) current_sequence = from_acc.sequence count = 0 for address in addrs_list: addr = Address(address) amount = get_random_amount(cfg['RANDOM_START'], cfg['RANDOM_END']) transfer_tx = hc.functions.transfer( _to=to_checksum_address(addr.hex_address), _value=int(amount * 10 ** 18)). \ buildTransaction_htdf() data = remove_0x_prefix(transfer_tx['data']) signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=0, sequence=current_sequence + count, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo=cfg['MEMO']).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('{}, {}, tx_hash: {}'.format(token_name, count, tx_hash)) # self.assertTrue( len(tx_hash) == 64) # tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) # pprint(tx) # cfnBalanceOf = hc.functions.balanceOf(_owner=to_checksum_address(new_to_addr.hex_address)) # balance = hc.call(cfn=cfnBalanceOf) # print(balance) count += 1 if count % 40 == 0: time.sleep(4.5) pass
def test_normal_tx_with_data(conftest_args): # protocol_version = subprocess.getoutput('hscli query upgrade info --chain-id=testchain -o json | jq .current_version.UpgradeInfo.Protocol.version') gas_wanted = 7500000 gas_price = 100 tx_amount = 1 data = 'ff' * 1000 memo = 'test_normal_transaction_with_data' htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) upgrade_info = htdfrpc.get_upgrade_info() protocol_version = int( upgrade_info['current_version']['UpgradeInfo']['Protocol']['version']) from_addr = Address(conftest_args['ADDRESS']) new_to_addr = HtdfPrivateKey('').address # to_addr = Address('htdf1jrh6kxrcr0fd8gfgdwna8yyr9tkt99ggmz9ja2') private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) assert from_acc is not None assert from_acc.balance_satoshi > gas_price * gas_wanted + tx_amount signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=new_to_addr, amount_satoshi=tx_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=gas_price, gas_wanted=gas_wanted, data=data, memo=memo).build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) mempool = htdfrpc.get_mempool_trasactions() pprint(mempool) memtx = htdfrpc.get_mempool_transaction(transaction_hash=tx_hash) pprint(memtx) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) tx = htdfrpc.get_transaction(transaction_hash=tx_hash) if protocol_version < 2: # v0 and v1 assert tx['logs'][0]['success'] == True assert tx['gas_wanted'] == str(gas_wanted) assert int(tx['gas_used']) < gas_wanted tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(5) # want for chain state update to_acc = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc is not None assert to_acc.balance_satoshi == tx_amount from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * int(tx['gas_used']) + tx_amount) elif protocol_version == 2: # v2 # because of `data` isn't empty. `to` must be correct contract address, if not, # this transaction be failed in V2 handler assert tx['logs'][0]['success'] == False # Because of `data` is not empty, so v2's anteHander doesn't adjust tx's gasWanted. assert tx['gas_wanted'] == str(gas_wanted) # v2 DO NOT ALLOW `data` in normal htdf transaction, # so evm execute tx failed, all the gas be consumed assert tx['gas_used'] == str(gas_wanted) tv = tx['tx']['value'] assert len(tv['msg']) == 1 assert tv['msg'][0]['type'] == 'htdfservice/send' assert int(tv['fee']['gas_wanted']) == gas_wanted assert int(tv['fee']['gas_price']) == gas_price assert tv['memo'] == memo mv = tv['msg'][0]['value'] assert mv['From'] == from_addr.address assert mv['To'] == new_to_addr.address assert mv['Data'] == data assert int(mv['GasPrice']) == gas_price assert int(mv['GasWanted']) == gas_wanted assert 'satoshi' == mv['Amount'][0]['denom'] assert tx_amount == int(mv['Amount'][0]['amount']) pprint(tx) time.sleep(5) # wait for chain state update to_acc = htdfrpc.get_account_info(address=new_to_addr.address) assert to_acc is None from_acc_new = htdfrpc.get_account_info(address=from_addr.address) assert from_acc_new.address == from_acc.address assert from_acc_new.sequence == from_acc.sequence + 1 assert from_acc_new.account_number == from_acc.account_number assert from_acc_new.balance_satoshi == from_acc.balance_satoshi - ( gas_price * gas_wanted) else: raise Exception("invalid protocol version {}".format(protocol_version)) pass