def run(self): dbloop = task.LoopingCall(Blockchain.Default().PersistBlocks) dbloop.start(.1) Blockchain.Default().PersistBlocks() while Blockchain.Default().Height < 2: print("Waiting for chain to sync...") time.sleep(1) # Claim initial NEO address = "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y" self.claim_initial_neo(address) # Open wallet again print("Opening wallet %s" % self.wallet_fn) self.wallet = UserWallet.Open(self.wallet_fn, to_aes_key("coz")) self.wallet.ProcessBlocks() self._walletdb_loop = task.LoopingCall(self.wallet.ProcessBlocks) self._walletdb_loop.start(1) # self.wallet.Rebuild() # print("\nOpened wallet. Rebuilding...") # time.sleep(10) print("\nWait %s min before claiming GAS." % self.min_wait) time.sleep(60 * self.min_wait) print("\nSending NEO to own wallet...") with patch('neo.Prompt.Commands.Send.prompt', side_effect=["coz"]): framework = construct_send_basic(self.wallet, ["neo", address, "100000000"]) tx = process_transaction(self.wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) if not tx: print("Something went wrong, no tx.") return # Wait until transaction is on blockchain self.wait_for_tx(tx) print("Claiming the GAS...") claim_tx, relayed = ClaimGas(self.wallet, require_password=False) self.wait_for_tx(claim_tx) # Finally, need to rebuild the wallet self.wallet.Rebuild() print("\nAll done!") print("- Wallet file: %s" % self.wallet_fn) print("- Wallet pwd: %s" % self.wallet_pwd) if self.wif_fn: with open(self.wif_fn, "w") as f: f.write("KxDgvEKzgSBPPfuVfw67oPQBSjidEiqTHURKSDL1R7yGaGYAeYnr") self.quit()
def test_sendmany_good_simple(self): with patch('neo.Prompt.Commands.Send.prompt', side_effect=[ "neo", self.watch_addr_str, "1", "gas", self.watch_addr_str, "1", UserWalletTestCase.wallet_1_pass() ]): wallet = self.GetWallet1(recreate=True) args = ["2"] framework = construct_send_many(wallet, args) res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], scripthash_change=framework[2], fee=framework[3], owners=framework[4], user_tx_attributes=framework[5]) self.assertTrue(res) # verify successful tx json_res = res.ToJson() self.assertEqual( self.watch_addr_str, json_res['vout'][0]['address']) # verify correct address_to # check for 2 transfers transfers = 0 for info in json_res['vout']: if info['address'] == self.watch_addr_str: transfers += 1 self.assertEqual(2, transfers)
def test_send_with_fee_and_from_addr(self): with patch('neo.Prompt.Commands.Send.prompt', side_effect=[UserWalletTestCase.wallet_1_pass()]): wallet = self.GetWallet1(recreate=True) args = [ 'neo', self.watch_addr_str, '1', '--from-addr=AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3', '--fee=0.005' ] framework = construct_send_basic(wallet, args) res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) self.assertTrue(res) # verify successful tx json_res = res.ToJson() self.assertEqual( self.watch_addr_str, json_res['vout'][0]['address']) # verify correct address_to self.assertEqual( self.wallet_1_addr, json_res['vout'][1]['address']) # verify correct address_from self.assertEqual(json_res['net_fee'], "0.005") # verify correct fee
def test_owners(self, mock): with patch('neo.Prompt.Commands.Send.prompt', side_effect=[UserWalletTestCase.wallet_1_pass()]): wallet = self.GetWallet1(recreate=True) args = [ 'gas', self.wallet_1_addr, '2', "--owners=['AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaXK','APRgMZHZubii29UXF9uFa6sohrsYupNAvx']" ] framework = construct_send_basic(wallet, args) process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) self.assertTrue(mock.called)
def test_bad_password(self): with patch('neo.Prompt.Commands.Send.prompt', side_effect=['blah']): wallet = self.GetWallet1(recreate=True) args = ['neo', self.watch_addr_str, '50'] framework = construct_send_basic(wallet, args) res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) self.assertFalse(res)
def test_insufficient_funds(self): wallet = self.GetWallet1(recreate=True) args = ['gas', self.watch_addr_str, '72620'] framework = construct_send_basic(wallet, args) res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) self.assertFalse(res)
def run(self): dbloop = task.LoopingCall(Blockchain.Default().PersistBlocks) dbloop.start(.1) Blockchain.Default().PersistBlocks() while Blockchain.Default().Height < 2: print("Waiting for chain to sync...") time.sleep(1) # Open wallet again print("Opening wallet %s" % self.wallet_fn) self.wallet = UserWallet.Open(self.wallet_fn, to_aes_key("coz")) self.wallet.ProcessBlocks() self._walletdb_loop = task.LoopingCall(self.wallet.ProcessBlocks) self._walletdb_loop.start(1) print("\nWait %s min before claiming GAS." % self.min_wait) time.sleep(60 * self.min_wait) self.wallet.Rebuild() print("\nRebuilding wallet...") time.sleep(20) print("\nSending NEO to own wallet...") address = "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y" with patch('neo.Prompt.Commands.Send.prompt', side_effect=["coz"]): framework = construct_send_basic(self.wallet, ["neo", address, "100000000"]) tx = process_transaction(self.wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) if not tx: print("Something went wrong, no tx.") return # Wait until transaction is on blockchain self.wait_for_tx(tx) print("Claiming the GAS...") claim_tx, relayed = ClaimGas(self.wallet, require_password=False) self.wait_for_tx(claim_tx) # Finally, need to rebuild the wallet # self.wallet.Rebuild() self.quit()
def test_fails_to_sign_tx(self): with patch('neo.Prompt.Commands.Send.prompt', side_effect=[UserWalletTestCase.wallet_1_pass()]): with patch('neo.Wallets.Wallet.Wallet.Sign', return_value=False): wallet = self.GetWallet1(recreate=True) args = ['gas', self.watch_addr_str, '2'] framework = construct_send_basic(wallet, args) res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) self.assertFalse(res)
def test_send_gas_mimic_prompt(self): with patch('neo.Prompt.Commands.Send.prompt', side_effect=[UserWalletTestCase.wallet_1_pass()]): wallet = self.GetWallet1(recreate=True) args = ['gas', self.watch_addr_str, '5'] res = False framework = construct_send_basic(wallet, args) if type(framework) is list: res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) self.assertTrue(res)
def test_could_not_send(self, mocked_tracback_module): # mocking traceback module to avoid stacktrace printing during test run wallet = self.GetWallet1(recreate=True) args = ['gas', self.watch_addr_str, '2'] contract_tx, scripthash_from, fee, owners, user_tx_attributes = construct_send_basic( wallet, args) scripthash_change = scripthash_from # mocking wallet to trigger the exception wallet = MagicMock() wallet.MakeTransaction.side_effect = Exception res = process_transaction( wallet, contract_tx, scripthash_from, scripthash_change, fee, owners, user_tx_attributes) # forces the 'try:' to fail self.assertFalse(res)
def test_bad_attributes(self): with patch('neo.Prompt.Commands.Send.prompt', side_effect=[UserWalletTestCase.wallet_1_pass()]): wallet = self.GetWallet1(recreate=True) args = [ 'gas', self.watch_addr_str, '2', '--tx-attr=[{"usa:241"data":his is a remark"}]' ] framework = construct_send_basic(wallet, args) res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) self.assertTrue(res) self.assertEqual(1, len(res.Attributes))
def test_attributes(self): with patch('neo.Prompt.Commands.Send.prompt', side_effect=[UserWalletTestCase.wallet_1_pass()]): wallet = self.GetWallet1(recreate=True) args = [ 'gas', self.watch_addr_str, '2', '--tx-attr={"usage":241,"data":"This is a remark"}' ] framework = construct_send_basic(wallet, args) res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], fee=framework[2], owners=framework[3], user_tx_attributes=framework[4]) self.assertTrue(res) self.assertEqual( 2, len(res.Attributes) ) # By default the script_hash of the transaction sender is added to the TransactionAttribute list, therefore the Attributes length is `count` + 1
def test_sendmany_good_complex(self): with patch('neo.Prompt.Commands.Send.prompt', side_effect=[ "neo", "AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaXK", "1", "gas", "AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaXK", "1", UserWalletTestCase.wallet_1_pass() ]): wallet = self.GetWallet1(recreate=True) args = [ "2", '--from-addr=%s' % self.wallet_1_addr, '--change-addr=%s' % self.watch_addr_str, '--fee=0.005' ] address_from_account_state = Blockchain.Default().GetAccountState( self.wallet_1_addr).ToJson() address_from_gas_bal = address_from_account_state['balances'][ '0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7'] framework = construct_send_many(wallet, args) res = process_transaction(wallet, contract_tx=framework[0], scripthash_from=framework[1], scripthash_change=framework[2], fee=framework[3], owners=framework[4], user_tx_attributes=framework[5]) self.assertTrue(res) # verify successful tx json_res = res.ToJson() self.assertEqual( "AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaXK", json_res['vout'][0]['address']) # verify correct address_to self.assertEqual(self.watch_addr_str, json_res['vout'][2] ['address']) # verify correct change address self.assertEqual( float(address_from_gas_bal) - 1 - 0.005, float(json_res['vout'][3]['value'])) self.assertEqual('0.005', json_res['net_fee'])