def test_dex_zero_broadcast(self, test_params): # zero length payload rpc1 = test_params.get('node1').get('rpc') rpc2 = test_params.get('node1').get('rpc') payload = '' taga = randomstring(15) tagb = randomstring(15) priority = '1' pubkey = rpc1.DEX_stats().get('publishable_pubkey') res = rpc1.DEX_broadcast(payload, priority, taga, tagb, pubkey) assert not res # should have empty response res = rpc1.DEX_broadcast(payload, priority, '', '', '') assert not res res = rpc1.DEX_broadcast(payload, priority, taga, '', '') assert not res time.sleep(15) # no messages should be listed res = rpc1.DEX_list('', '1', taga, tagb, '') assert not res.get('matches') res = rpc1.DEX_list('', '1', taga, '', '') assert not res.get('matches') res = rpc2.DEX_list('', '1', taga, tagb, '') assert not res.get('matches') res = rpc2.DEX_list('', '1', taga, '', '') assert not res.get('matches')
def test_heirfund(self, test_params): heirfund_schema = { 'type': 'object', 'properties': { 'result': { 'type': 'string' }, 'hex': { 'type': 'string' }, 'error': { 'type': 'string' } }, 'required': ['result'] } rpc1 = test_params.get('node1').get('rpc') pubkey1 = test_params.get('node1').get('pubkey') amount = '100' name = 'heir' + randomstring(5) inactivitytime = '20' res = rpc1.heirfund(amount, name, pubkey1, inactivitytime, 'testMemo') validate_template(res, heirfund_schema) txid = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(txid, rpc1)
def test_heirclaim(self, test_params): heirclaim_schema = { 'type': 'object', 'properties': { 'result': { 'type': 'string' }, 'hex': { 'type': 'string' }, 'error': { 'type': 'string' } }, 'required': ['result'] } rpc1 = test_params.get('node1').get('rpc') # create heir plan to claim pubkey1 = test_params.get('node2').get('pubkey') amount = '100' name = 'heir' + randomstring(5) inactivitytime = '120' res = rpc1.heirfund(amount, name, pubkey1, inactivitytime, 'testMemo') fundtxid = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(fundtxid, rpc1) # Wait inactivitytime and claim funds time.sleep(int(inactivitytime)) print("\n Sleeping for inactivity time") res = rpc1.heirclaim(amount, fundtxid) validate_template(res, heirclaim_schema) claimtxid = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(claimtxid, rpc1)
def test_dice_errors(self, test_params): rpc1 = test_params.get('node1').get('rpc') dicename = randomstring(4) # creating dice plan with too long name (>8 chars) res = rpc1.dicefund("THISISTOOLONG", "10000", "10", "10000", "10", "5") assert res.get('result') == 'error' # creating dice plan with < 100 funding res = rpc1.dicefund(dicename, "10", "1", "10000", "10", "5") assert res.get('result') == 'error' # adding negative and zero funds to plan try: fundtxid = rpc1.dicelist()[0] name = rpc1.diceinfo(fundtxid).get('name') res = rpc1.diceaddfunds(name, fundtxid, '0') assert res.get('result') == 'error' res = rpc1.diceaddfunds(name, fundtxid, '-123') assert res.get('result') == 'error' except IndexError: casino = TestDiceCCBase.new_casino(rpc1) fundtxid = casino.get('fundingtxid') name = rpc1.diceinfo(fundtxid).get('name') res = rpc1.diceaddfunds(name, fundtxid, '0') assert res.get('result') == 'error' res = rpc1.diceaddfunds(name, fundtxid, '-123') assert res.get('result') == 'error' # not valid dice info checking res = rpc1.diceinfo("invalid") assert res.get('result') == 'error'
def test_dex_encryption(self, test_params): rpc1 = test_params.get('node1').get('rpc') message = randomstring(15) priority = '8' taga = randomstring(6) tagb = randomstring(6) pubkey1 = rpc1.DEX_stats().get('publishable_pubkey') # case1 # blob sent without pubkey -- message should not be encrypted res = rpc1.DEX_broadcast(message, priority, taga, tagb, '', '', '') blob_bc_id = res.get('id') res = rpc1.DEX_list('', '', taga, tagb, '') message_res = '' pubkey_res = '' for blob in res.get('matches'): if blob.get('id') == blob_bc_id: message_res = blob.get('payload') pubkey_res = blob.get('pubkey') assert message_res == message assert not pubkey_res # case 2 # blob with pubkey -- message should be encrypted and visible to key owner res = rpc1.DEX_broadcast(message, priority, taga, tagb, pubkey1, '', '') blob_bc_id = res.get('id') res = rpc1.DEX_list('', '', taga, tagb, '') message_res = '' decrypt_res = '' pubkey_res = '' for blob in res.get('matches'): if blob.get('id') == blob_bc_id: message_res = blob.get('payload') decrypt_res = blob.get('decrypted') pubkey_res = blob.get('pubkey') assert message_res != message assert decrypt_res == message assert pubkey_res == pubkey1
def new_rewardsplan(proxy, schema=None): name = randomstring(4) amount = '250' apr = '25' mindays = '0' maxdays = '10' mindeposit = '10' res = proxy.rewardscreatefunding(name, amount, apr, mindays, maxdays, mindeposit) if schema: validate_template(res, schema) assert res.get('result') == 'success' txid = proxy.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(txid, proxy) rewardsplan = {'fundingtxid': txid, 'name': name} return rewardsplan
def badbets_check(proxy, fundtxid): casino = proxy.diceinfo(fundtxid) dname = str(casino.get('name')) minbet = str(casino.get('minbet')) maxbet = str(casino.get('maxbet')) maxodds = str(casino.get('maxodds')) funding = str(casino.get('funding')) res = proxy.dicebet(dname, fundtxid, '0', maxodds) # 0 bet assert res.get('result') == 'error' res = proxy.dicebet(dname, fundtxid, minbet, '0') # 0 odds assert res.get('result') == 'error' res = proxy.dicebet(dname, fundtxid, '-1', maxodds) # negative bet assert res.get('result') == 'error' res = proxy.dicebet(dname, fundtxid, minbet, '-1') # negative odds assert res.get('result') == 'error' # placing bet more than maxbet dmb = Decimal(maxbet) dmb += 1 res = proxy.dicebet(dname, fundtxid, "{0:.8f}".format(dmb), maxodds) assert res.get('result') == 'error' # placing bet with odds more than allowed dmo = Decimal(maxodds) dmo += 1 res = proxy.dicebet(dname, fundtxid, minbet, "{0:.8f}".format(dmo)) assert res.get('result') == 'error' # placing bet with amount more than funding betamount = funding res = proxy.dicebet(dname, fundtxid, str(betamount), maxodds) assert res.get('result') == 'error' # placing bet with not correct dice name name = randomstring(5) res = proxy.dicebet(name, fundtxid, minbet, maxodds) assert res.get('result') == 'error' # placing bet with not correct dice id badtxid = randomhex() res = proxy.dicebet(dname, badtxid, minbet, maxodds) assert res.get('result') == 'error'
def test_heiradd(self, test_params): hieradd_schema = { 'type': 'object', 'properties': { 'result': { 'type': 'string' }, 'hex': { 'type': 'string' }, 'error': { 'type': 'string' } }, 'required': ['result'] } rpc1 = test_params.get('node1').get('rpc') amount = '100' try: fundid = rpc1.heirlist()[0] res = rpc1.heiradd(amount, fundid) validate_template(res, hieradd_schema) assert res.get('result') == 'success' txid = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(txid, rpc1) except IndexError: print('\nNo heirplan on chain, creating one\n') pubkey1 = test_params.get('node1').get('pubkey') amount = '100' name = 'heir' + randomstring(5) inactivitytime = '20' res = rpc1.heirfund(amount, name, pubkey1, inactivitytime, 'testMemoHeirInfo') txid = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(txid, rpc1) fundid = rpc1.heirlist()[0] res = rpc1.heiradd(amount, fundid) validate_template(res, hieradd_schema) assert res.get('result') == 'success' txid = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(txid, rpc1)
def test_dex_anonsend(self, test_params): rpc1 = test_params.get('node1').get('rpc') rpc2 = test_params.get('node2').get('rpc') pub = rpc1.DEX_stats().get('publishable_pubkey') destpub = rpc2.DEX_stats().get('publishable_pubkey') message = randomstring(22) # send message to 2nd node res = rpc1.DEX_anonsend(message, '4', destpub) msg_id = res.get('id') time.sleep(15) # time for broadcasting # check message on 2nd node res = rpc2.DEX_list('', '', 'anon') assert res.get('result') == 'success' matches = res.get('matches') for match in matches: if match.get('id') == msg_id: assert match.get('anonmsg') == message assert match.get('anonsender') == pub
def new_casino(proxy, schema=None): rpc1 = proxy name = randomstring(4) funds = '777' minbet = '1' maxbet = '77' maxodds = '10' timeoutblocks = '5' res = rpc1.dicefund(name, funds, minbet, maxbet, maxodds, timeoutblocks) if schema: validate_template(res, schema) assert res.get('result') == 'success' txid = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(txid, rpc1) casino = { 'fundingtxid': txid, 'name': name, 'minbet': minbet, 'maxbet': maxbet, 'maxodds': maxodds } return casino
def test_heir_tokens_flow(self, test_params): rpc1 = test_params.get('node1').get('rpc') pubkey1 = test_params.get('node1').get('pubkey') rpc2 = test_params.get('node2').get('rpc') pubkey2 = test_params.get('node2').get('pubkey') inactivitytime = 70 # ideally, should be a bit more than blocktime amount = 100000000 plan_name = 'heir' + randomstring(5) comment = 'HeirFlowTest' + randomstring(5) token_name = 'token' + randomstring(5) # Create on-chain tokens res = rpc1.tokencreate(token_name, '1', 'heirCCTest') tokentx = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(tokentx, rpc1) res = rpc1.tokenbalance(tokentx, pubkey1)['balance'] assert res == amount # validate init tokenbalance res = rpc1.heirfund(str(amount), plan_name, pubkey2, str(inactivitytime), comment, tokentx) fundtx = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(fundtx, rpc1) time.sleep(5) # Check plan availability in heirlist res = rpc1.heirlist() assert fundtx in res # Check plan info res = rpc1.heirinfo(fundtx) assert res.get('result') == 'success' # check here stuff assert res['fundingtxid'] == fundtx assert res['name'] == plan_name assert res['owner'] == pubkey1 assert res['heir'] == pubkey2 assert res['memo'] == comment assert res['lifetime'] == str(amount) assert res['tokenid'] == tokentx assert res['type'] == 'tokens' assert res['InactivityTimeSetting'] == str(inactivitytime) # Check Heir spending allowed after inactivity time print("\n Sleeping for inactivity time") time.sleep(inactivitytime + 1) wait_blocks(rpc1, 2) check_synced(rpc1, rpc2) res = rpc1.heirinfo(fundtx) assert res['lifetime'] == str(amount) assert res['available'] == str(amount) assert res['IsHeirSpendingAllowed'] == 'true' # Claim all available funds from hier node res = rpc2.heirclaim(str(amount), fundtx) assert res.get('result') == 'success' claimtx = rpc2.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(claimtx, rpc2) time.sleep(5) # Check claim success # Wait sync wait_blocks(rpc1, 1) check_synced(rpc1, rpc2) res = rpc1.heirinfo(fundtx) assert res['lifetime'] == str(amount) assert res['available'] == '0' res = rpc2.heirinfo(fundtx) assert res['lifetime'] == str(amount) assert res['available'] == '0' # Check heir balance after claim res = rpc2.tokenbalance(tokentx, pubkey2)['balance'] assert res == amount
def test_heir_flow(self, test_params): # Check basic heirCC flow from fund to claim # Create valid heir plan rpc1 = test_params.get('node1').get('rpc') pubkey1 = test_params.get('node1').get('pubkey') rpc2 = test_params.get('node2').get('rpc') pubkey2 = test_params.get('node2').get('pubkey') inactivitytime = 70 # ideally, should be a bit more than blocktime amount = 777 plan_name = 'heir' + randomstring(5) comment = 'HeirFlowTest' + randomstring(5) res = rpc1.heirfund(str(amount), plan_name, pubkey2, str(inactivitytime), comment) fundtx = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(fundtx, rpc1) time.sleep(5) # Check plan availability in heirlist res = rpc1.heirlist() assert fundtx in res # Check plan info res = rpc1.heirinfo(fundtx) assert res.get('result') == 'success' # check here stuff assert res['fundingtxid'] == fundtx assert res['name'] == plan_name assert res['owner'] == pubkey1 assert res['heir'] == pubkey2 assert res['memo'] == comment assert res['lifetime'] == str(amount) + '.00000000' assert res['type'] == "coins" assert res['InactivityTimeSetting'] == str(inactivitytime) # Check Heir spending allowed after inactivity time print("\n Sleeping for inactivity time") time.sleep(inactivitytime + 1) wait_blocks(rpc1, 2) check_synced( rpc1, rpc2) # prevents issues when inactivity time =< block time res = rpc1.heirinfo(fundtx) assert res['lifetime'] == str(amount) + '.00000000' assert res['available'] == str(amount) + '.00000000' assert res['IsHeirSpendingAllowed'] == 'true' # Claim all available funds from hier node res = rpc2.heirclaim(str(amount), fundtx) assert res.get('result') == 'success' claimtx = rpc2.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(claimtx, rpc2) time.sleep(5) # Check claim success # Wait sync wait_blocks(rpc1, 2) check_synced(rpc1, rpc2) res = rpc1.heirinfo(fundtx) assert res['lifetime'] == str(amount) + '.00000000' assert res['available'] == '0.00000000' res = rpc2.heirinfo(fundtx) assert res['lifetime'] == str(amount) + '.00000000' assert res['available'] == '0.00000000'
def test_file_publish(self, test_params): rpc1 = test_params.get('node1').get('rpc') rpc2 = test_params.get('node2').get('rpc') pubkey = rpc1.DEX_stats().get('publishable_pubkey') filename1 = 'file_' + randomstring(5) write_file(filename1) filename2 = 'file_' + randomstring(5) write_file(filename2) size1 = get_size(filename1) size2 = get_size(filename2) fhash1 = get_filehash(filename1) fhash2 = get_filehash(filename2) # publish both files on 1st node res = rpc1.DEX_publish(filename1, '0') f_id1 = str(res.get('id')) assert res.get('result') == 'success' assert res.get('fname') == filename1 assert res.get('filesize') == size1 assert res.get('filehash') == fhash1 res = rpc1.DEX_publish(filename2, '0') f_id2 = str(res.get('id')) assert res.get('result') == 'success' assert res.get('fname') == filename2 assert res.get('filesize') == size2 assert res.get('filehash') == fhash2 time.sleep(20) # time to broadcast files to node1 # Both nodes should be able locate file by files tag and locators tag res = rpc1.DEX_list('0', '0', 'files') f_published = "" for match in res.get('matches'): f_published += (match.get('tagB') ) # if tagA is "files", filenames are tagBs assert filename1 in f_published assert filename2 in f_published res = rpc2.DEX_list('0', '0', 'files') f_published = "" for match in res.get('matches'): f_published += (match.get('tagB')) assert filename1 in f_published assert filename2 in f_published res = rpc1.DEX_list('0', '0', filename1, 'locators') res_fname = res.get('tagA') match_fname = res.get('matches')[0].get('tagA') ramount = res.get('matches')[0].get('amountA') rsize = float(ramount) * 100000000 assert res_fname == filename1 assert match_fname == filename1 assert rsize == size1 res = rpc1.DEX_list('0', '0', filename2, 'locators') res_fname = res.get('tagA') match_fname = res.get('matches')[0].get('tagA') ramount = res.get('matches')[0].get('amountA') rsize = float(ramount) * 100000000 assert res_fname == filename2 assert match_fname == filename2 assert rsize == size2 res = rpc2.DEX_list('0', '0', filename1, 'locators') res_fname = res.get('tagA') match_fname = res.get('matches')[0].get('tagA') ramount = res.get('matches')[0].get('amountA') rsize = float(ramount) * 100000000 assert res_fname == filename1 assert match_fname == filename1 assert rsize == size1 res = rpc2.DEX_list('0', '0', filename2, 'locators') res_fname = res.get('tagA') match_fname = res.get('matches')[0].get('tagA') ramount = res.get('matches')[0].get('amountA') rsize = float(ramount) * 100000000 assert res_fname == filename2 assert match_fname == filename2 assert rsize == size2 # get 1st file # Incorrect locators res = rpc2.DEX_subscribe(filename1, '0', f_id2, pubkey) assert res.get('result') == 'error' # No pubkey res = rpc2.DEX_subscribe(filename1, '0', f_id1) assert res.get('result') == 'error' res = rpc2.DEX_subscribe(filename1, '0', '0', pubkey) assert res.get('fname') == filename1 assert res.get('result') == 'success' assert res.get('filesize') == size1 assert res.get('filehash') == fhash1 # get 2nd file res = rpc2.DEX_subscribe(filename2, '0', '0', pubkey) assert res.get('fname') == filename2 assert res.get('result') == 'success' assert res.get('filesize') == size2 assert res.get('filehash') == fhash2
def test_heirinfo(self, test_params): heirinfo_schema = { 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'fundingtxid': { 'type': 'string' }, 'owner': { 'type': 'string' }, 'tokenid': { 'type': 'string' }, 'heir': { 'type': 'string' }, 'type': { 'type': 'string' }, 'lifetime': { 'type': 'string' }, 'available': { 'type': 'string' }, 'OwnerRemainderTokens': { 'type': 'string' }, 'InactivityTimeSetting': { 'type': 'string' }, 'IsHeirSpendingAllowed': { 'type': 'string' }, 'memo': { 'type': 'string' }, 'result': { 'type': 'string' }, 'error': { 'type': 'string' } }, 'required': ['result'] } rpc1 = test_params.get('node1').get('rpc') try: fundid = rpc1.heirlist()[0] res = rpc1.heirinfo(fundid) validate_template(res, heirinfo_schema) assert res.get('result') == 'success' except IndexError: print('\nNo heirplan on chain, creating one\n') pubkey1 = test_params.get('node1').get('pubkey') amount = '100' name = 'heir' + randomstring(5) inactivitytime = '20' res = rpc1.heirfund(amount, name, pubkey1, inactivitytime, 'testMemoHeirInfo') txid = rpc1.sendrawtransaction(res.get('hex')) mine_and_waitconfirms(txid, rpc1) fundid = rpc1.heirlist()[0] res = rpc1.heirinfo(fundid) validate_template(res, heirinfo_schema) assert res.get('result') == 'success'
def test_dex_listing(self, test_params): rpc1 = test_params.get('node1').get('rpc') rpc2 = test_params.get('node2').get('rpc') message_static = randomstring(22) minpriority = ['0', '9'] priority = ['1', '9'] taga = randomstring(15) tagb = randomstring(15) amounta = ['11.234', '1'] amountb = ['12.0099', '4'] stopat = '' pubkey = rpc1.DEX_stats().get('publishable_pubkey') num_broadcast = 100 for num in range(num_broadcast): rpc1.DEX_broadcast((message_static + str(num)), '4', 'inbox', 'tag_test100', '', '', '') list1 = rpc1.DEX_list(stopat, minpriority[0], '', 'tag_test100', '') list2 = rpc2.DEX_list(stopat, minpriority[0], '', 'tag_test100', '') time.sleep(5) # time to sync broadcasts for both nodes assert num_broadcast == list1.get('n') num_broadcast = 10000 for num in range(num_broadcast): rpc1.DEX_broadcast((message_static + str(num)), minpriority[0], 'inbox', 'tag_test10k', '', '', '') list1 = rpc1.DEX_list(stopat, minpriority[0], '', 'tag_test10k', '') time.sleep(5) assert in_99_range(list1['n'], num_broadcast) # Broadcast orders to sort num_broadcast_orders = 3 num_cycles = 2 # equal to amount[] entries above ids = [] hashes = [] for i in range(num_cycles): for num in range(num_broadcast_orders): rpc1.DEX_broadcast((message_static + str(num)), priority[i], taga, tagb, pubkey, amounta[i], amountb[i]) rpc1.DEX_broadcast((message_static + str(num)), priority[i], taga, tagb, '', amounta[i], amountb[i]) rpc1.DEX_broadcast((message_static + str(num)), priority[i], '', tagb, pubkey, amounta[i], amountb[i]) rpc1.DEX_broadcast((message_static + str(num)), priority[i], taga, '', pubkey, amounta[i], amountb[i]) rpc1.DEX_broadcast((message_static + str(num)), priority[i], '', tagb, '', amounta[i], amountb[i]) rpc1.DEX_broadcast((message_static + str(num)), priority[i], taga, '', '', amounta[i], amountb[i]) rpc1.DEX_broadcast((message_static + str(num)), priority[i], taga, tagb, '', amounta[i], amountb[i]) rpc1.DEX_broadcast((message_static + str(num)), priority[i], '', '', pubkey, amounta[i], amountb[i]) # get ids and hashes to use later res = rpc1.DEX_broadcast((message_static + str(num_broadcast_orders + i)), priority[i], taga, tagb, pubkey, amounta[i], amountb[i]) ids.append(res.get('id')) hashes.append(res.get('hash')) time.sleep(10) # DEX_list stopat minpriority taga tagb pubkey mina maxa minb maxb stophash res_min0 = rpc1.DEX_list(stopat, minpriority[0], taga, '', '') assert int(res_min0.get('n')) == 32 # Should have less broadcasts with high priority # Can't operate set numbers here res_min1 = rpc1.DEX_list(stopat, minpriority[1], taga, '', '') assert int(res_min1.get('n')) < int(res_min0.get('n')) # base listing check res = rpc1.DEX_list(stopat, minpriority[0], '', tagb, '') assert int(res.get('n')) == 32 res = rpc1.DEX_list(stopat, minpriority[0], '', '', pubkey) assert int(res.get('n')) >= 26 # including previous broadcasts with same pubkey, might be more than 26 # in case when tests are run together res = rpc1.DEX_list(stopat, minpriority[0], taga, tagb, pubkey) assert int(res.get('n')) == 8 res = rpc1.DEX_list(stopat, minpriority[0], taga, '', pubkey) assert int(res.get('n')) == 14 res = rpc1.DEX_list(stopat, minpriority[0], '', tagb, pubkey) assert int(res.get('n')) == 14 res = rpc1.DEX_list(stopat, minpriority[0], taga, tagb, '') assert int(res.get('n')) == 20 # check amounts res = rpc1.DEX_list(stopat, minpriority[0], taga, tagb, '', amounta[1], amounta[0], amountb[1], amountb[0]) assert int(res.get('n')) == 20 res = rpc1.DEX_list(stopat, minpriority[0], taga, tagb, '', '', '', amountb[1], amountb[0]) assert int(res.get('n')) == 20 res = rpc1.DEX_list(stopat, minpriority[0], taga, tagb, '', '', amounta[0], amountb[1], '') assert int(res.get('n')) == 20 res = rpc1.DEX_list(stopat, minpriority[0], taga, tagb, '', amounta[0], '', '', '') assert int(res.get('n')) == 10 # check stopat res = rpc1.DEX_list(str(ids[1]), minpriority[0], taga, '', '') assert int(res.get('n')) == 0 # ids[1] is last id res = rpc1.DEX_list(str(ids[0]), minpriority[0], taga, '', '') assert int(res.get('n')) == 16 # check stophash res = rpc1.DEX_list(stopat, minpriority[0], taga, '', '', '', '', '', '', hashes[1]) assert int(res.get('n')) == 0 # hashes[1] is the last orders' hash res = rpc1.DEX_list(stopat, minpriority[0], taga, '', '', '', '', '', '', hashes[0]) assert int(res.get('n')) == 16
def test_dex_broadcast_get(self, test_params): rpc1 = test_params.get('node1').get('rpc') message_static = randomstring(22) taga_valid = 'inbox' tagb_valid = randomstring(15) taga_invalid = randomstring(16) tagb_invalid = randomstring(16) amounta = '11' amountb = '12' priority = '1' pubkey = rpc1.DEX_stats().get('publishable_pubkey') # broadacast messages res = rpc1.DEX_broadcast(message_static, priority, taga_valid, tagb_valid, pubkey, amounta, amountb) assert res.get('tagA') == taga_valid assert res.get('tagB') == tagb_valid assert res.get('pubkey') == pubkey assert float(res.get('amountA')) == float(amounta) assert float(res.get('amountB')) == float(amountb) assert int(priority) <= int(res.get('priority')) # DEX_get should return exact same blob as broadcast above get_order = rpc1.DEX_get(str(res.get('id'))) assert res == get_order res = rpc1.DEX_broadcast(message_static, priority, '', tagb_valid, pubkey, amounta, amountb) assert not res.get('tagA') assert res.get('tagB') == tagb_valid assert res.get('pubkey') == pubkey assert float(res.get('amountA')) == float(amounta) assert float(res.get('amountB')) == float(amountb) assert int(priority) <= int(res.get('priority')) res = rpc1.DEX_broadcast(message_static, priority, taga_valid, '', pubkey, amounta, amountb) assert not res.get('tagB') assert res.get('tagA') == taga_valid assert res.get('pubkey') == pubkey assert float(res.get('amountA')) == float(amounta) assert float(res.get('amountB')) == float(amountb) assert int(priority) <= int(res.get('priority')) res = rpc1.DEX_broadcast(message_static, priority, taga_valid, tagb_valid, '', amounta, amountb) assert res.get('tagA') == taga_valid assert res.get('tagB') == tagb_valid assert not res.get('pubkey') assert float(res.get('amountA')) == float(amounta) assert float(res.get('amountB')) == float(amountb) assert int(priority) <= int(res.get('priority')) res = rpc1.DEX_broadcast(message_static, priority, '', tagb_valid, '', amounta, amountb) assert not res.get('tagA') assert res.get('tagB') == tagb_valid assert not res.get('pubkey') assert float(res.get('amountA')) == float(amounta) assert float(res.get('amountB')) == float(amountb) assert int(priority) <= int(res.get('priority')) res = rpc1.DEX_broadcast(message_static, priority, taga_valid, '', '', amounta, amountb) assert not res.get('tagB') assert res.get('tagA') == taga_valid assert not res.get('pubkey') assert float(res.get('amountA')) == float(amounta) assert float(res.get('amountB')) == float(amountb) assert int(priority) <= int(res.get('priority')) res = rpc1.DEX_broadcast(message_static, priority, '', '', pubkey, amounta, amountb) assert not res.get('tagA') assert not res.get('tagB') assert res.get('pubkey') == pubkey assert float(res.get('amountA')) == float(amounta) assert float(res.get('amountB')) == float(amountb) assert int(priority) <= int(res.get('priority')) res = rpc1.DEX_broadcast(message_static, priority, '', '', '', '', '') assert res.get('tagA') == 'general' assert not res.get('tagB') assert not res.get('destpub') assert res.get('payload') == message_static assert int(priority) <= int(res.get('priority')) # tags are restricted to 15 characters, rpc should fail or return -1 try: res = rpc1.DEX_broadcast(message_static, priority, taga_invalid, '', '', '', '') if res != -1: raise Exception("Should have failed") except Exception as e: print(e) try: res = rpc1.DEX_broadcast(message_static, priority, '', tagb_invalid, '', '', '') if res != -1: raise Exception("Should have failed") except Exception as e: print(e)
def test_dex_orderbooks_cancel(self, test_params): rpc1 = test_params.get('node1').get('rpc') message = randomstring(15) priority = '4' base = randomstring(6) rel = randomstring(6) amounta = '81.44' amountb = '0.7771' pubkey = rpc1.DEX_stats().get('publishable_pubkey') # broadcast orderbooks res = rpc1.DEX_broadcast(message, priority, base, rel, pubkey, amounta, amountb) assert res['tagA'] == base assert res['tagB'] == rel assert res['cancelled'] == 0 order_pubkey_id = str(res['id']) res = rpc1.DEX_broadcast(message, priority, base, rel, '', amounta, amountb) order_nopub_id = str(res['id']) # swapping base - rel values, asks - bids should swap accordingly # this simple test works under assumption that random tags are unique res = rpc1.DEX_orderbook('', '0', base, rel, '') assert not res['bids'] price_calc = float(amountb) / float(amounta) price_res = float(res.get('asks')[0].get('price')) assert round(price_calc, 6) == round(price_res, 6) # rounding due to possible diff in 8th decimal res = rpc1.DEX_orderbook('', '0', rel, base, '') assert not res['asks'] price_calc = float(amounta) / float(amountb) price_res = float(res.get('bids')[0].get('price')) assert round(price_calc, 6) == round(price_res, 6) # cancel order by id res = rpc1.DEX_cancel(order_pubkey_id) assert res.get('tagA') == 'cancel' # cancelled order should be excluded from list res = rpc1.DEX_orderbook('', '0', base, rel, '') for order in res.get('asks'): # should return only asks here assert str(order.get('id')) != order_pubkey_id # order should have its cancellation timestamp in list response as cancelled property res = rpc1.DEX_list('', '', base, '', '') for blob in res.get('matches'): if str(blob.get('id')) == order_pubkey_id: assert blob.get('cancelled') > 0 # orders broadcast without pubkey should not be cancelled res = rpc1.DEX_cancel(order_nopub_id) assert res.get('tagA') == 'cancel' res = rpc1.DEX_orderbook('', '0', base, rel, '') orders = collect_orderids(res, 'asks') assert order_nopub_id in orders res = rpc1.DEX_list('', '', base, '', '') blobs = collect_orderids(res, 'matches') assert order_nopub_id in blobs for match in res.get('matches'): if match.get('id') == order_nopub_id: assert match.get('cancelled') == 0 # broadcast more orders with different tags to cancel unique_pairs = 2 base_list = [] rel_list = [] for i in range(unique_pairs): base_list.append(randomstring(6)) rel_list.append(randomstring(6)) message = randomstring(15) amounta = '0.7777' amountb = '0.66' for i in range(unique_pairs): for num in range(5): rpc1.DEX_broadcast((message + str(num)), priority, base_list[i], rel_list[i], pubkey, amounta, amountb) rpc1.DEX_broadcast((message + str(num)), priority, rel_list[i], base_list[i], pubkey, amounta, amountb) time.sleep(15) # orders should be old enough to be cancelled by tag/pubkey # cancel by tags blobs1 = collect_orderids(rpc1.DEX_list('', '', base_list[0], rel_list[0], pubkey), 'matches') blobs2 = collect_orderids(rpc1.DEX_list('', '', base_list[1], rel_list[1], pubkey), 'matches') res = rpc1.DEX_cancel('', '', base_list[0], rel_list[0]) # cancel all orders with pubkey by tags pair cancel = res.get('timestamp') for blob in blobs1: res = rpc1.DEX_get(blob) assert compare_rough(cancel, res.get('cancelled')) # should be cancelled in 30s for blob in blobs2: # other pairs must be unaffected res = rpc1.DEX_get(blob) assert res.get('cancelled') == 0 # cancel by pubkey res = rpc1.DEX_cancel('', pubkey) cancel = res.get('timestamp') for blob in blobs2: res = rpc1.DEX_get(blob) assert compare_rough(cancel, res.get('cancelled'))
def test_file_publish(self, test_params): rpc1 = test_params.get('node1').get('rpc') rpc2 = test_params.get('node2').get('rpc') pubkey = rpc1.DEX_stats().get('publishable_pubkey') filename1 = 'file_' + randomstring(5) write_file(filename1) filename2 = 'file_' + randomstring(5) write_file(filename2) size1 = get_size(filename1) size2 = get_size(filename2) fhash1 = get_filehash(filename1) fhash2 = get_filehash(filename2) # check error filename_too_long filename_err = 'file_' + randomstring(15) res = rpc1.DEX_publish(filename_err, '0') assert res.get('result') == 'error' assert res.get('error') == "filename longer than 15 chars" # check error file not exist filename_err = randomstring(1) res = rpc1.DEX_publish(filename_err, '0') assert res.get('result') == 'error' assert res.get('error') == "file not found" assert res.get('filename') == filename_err # publish both files on 1st node res = rpc1.DEX_publish(filename1, '0') f_id1 = str(res.get('id')) assert res.get('result') == 'success' assert res.get('fname') == filename1 assert res.get('filesize') == size1 assert res.get('filehash') == fhash1 res = rpc1.DEX_publish(filename2, '0') f_id2 = str(res.get('id')) assert res.get('result') == 'success' assert res.get('fname') == filename2 assert res.get('filesize') == size2 assert res.get('filehash') == fhash2 time.sleep(20) # time to broadcast files to node1 check_synced(rpc1, rpc2) # Both nodes should be able locate file by files tag and locators tag res = rpc1.DEX_list('0', '0', 'files') f_published = "" for match in res.get('matches'): f_published += (match.get('tagB')) # if tagA is "files", filenames are tagBs assert filename1 in f_published assert filename2 in f_published res = rpc2.DEX_list('0', '0', 'files') f_published = "" for match in res.get('matches'): f_published += (match.get('tagB')) assert filename1 in f_published assert filename2 in f_published res = rpc1.DEX_list('0', '0', filename1, 'locators') res_fname = res.get('tagA') match_fname = res.get('matches')[0].get('tagA') ramount = res.get('matches')[0].get('amountA') rsize = float(ramount) * 100000000 assert res_fname == filename1 assert match_fname == filename1 assert rsize == size1 res = rpc1.DEX_list('0', '0', filename2, 'locators') res_fname = res.get('tagA') match_fname = res.get('matches')[0].get('tagA') ramount = res.get('matches')[0].get('amountA') rsize = float(ramount) * 100000000 assert res_fname == filename2 assert match_fname == filename2 assert rsize == size2 res = rpc2.DEX_list('0', '0', filename1, 'locators') res_fname = res.get('tagA') match_fname = res.get('matches')[0].get('tagA') ramount = res.get('matches')[0].get('amountA') rsize = float(ramount) * 100000000 assert res_fname == filename1 assert match_fname == filename1 assert rsize == size1 res = rpc2.DEX_list('0', '0', filename2, 'locators') res_fname = res.get('tagA') match_fname = res.get('matches')[0].get('tagA') ramount = res.get('matches')[0].get('amountA') rsize = float(ramount) * 100000000 assert res_fname == filename2 assert match_fname == filename2 assert rsize == size2 # get 1st file # Incorrect locators res = rpc2.DEX_subscribe(filename1, '0', f_id2, pubkey) assert res.get('result') == 'error' # No pubkey res = rpc2.DEX_subscribe(filename1, '0', f_id1) assert res.get('result') == 'error' res = rpc2.DEX_subscribe(filename1, '0', '0', pubkey) assert res.get('fname') == filename1 assert res.get('result') == 'success' assert res.get('filesize') == size1 assert res.get('filehash') == fhash1 # get 2nd file res = rpc2.DEX_subscribe(filename2, '0', '0', pubkey) assert res.get('fname') == filename2 assert res.get('result') == 'success' assert res.get('filesize') == size2 assert res.get('filehash') == fhash2 # test file upload from dexp2p share directory fallbackpath = '' if os.name == 'posix': dex_path = os.environ['HOME'] + '/dexp2p/' if not os.path.isdir(dex_path): os.mkdir(dex_path) if os.stat(dex_path).st_uid != os.getuid(): raise NotADirectoryError("Directory not owned by current user: "******"Directory does not exists or not owned by current user: ", dex_path) fallbackpath = os.environ['HOMEDRIVE'] + '\\tmp\\dexp2p\\' pubkey = rpc1.DEX_stats().get('publishable_pubkey') file_shortname3 = 'file_' + randomstring(5) file_fullname3 = dex_path + file_shortname3 write_empty_file(file_fullname3, 10) size = get_size(file_fullname3) fhash = get_filehash(file_fullname3) res = rpc1.DEX_publish(file_shortname3, '0') assert res.get('result') == 'success' assert res.get('fname') == file_shortname3 assert res.get('filesize') == size assert res.get('filehash') == fhash time.sleep(20) res = rpc2.DEX_subscribe(file_shortname3, '0', '0', pubkey) assert res.get('result') == 'success' assert res.get('fname') == file_shortname3 assert res.get('filesize') == size assert res.get('filehash') == fhash