Beispiel #1
0
    def test_send_with_fee_and_from_addr(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            with patch('sys.stdout', new=StringIO()) as mock_print:
                with patch('neo.Prompt.Commands.Send.prompt',
                           side_effect=[UserWalletTestCase.wallet_1_pass()]):
                    PromptData.Wallet = self.GetWallet1(recreate=True)
                    args = [
                        'send', 'neo', self.watch_addr_str, '1',
                        '--from-addr=AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3',
                        '--fee=0.005'
                    ]

                    res = Wallet.CommandWallet().execute(args)

                    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
                    self.assertIn("Sending with fee: 0.005",
                                  mock_print.getvalue())
Beispiel #2
0
    def test_send_token_ok(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            with patch('neo.Prompt.Commands.Tokens.prompt',
                       side_effect=[UserWalletTestCase.wallet_1_pass()]):
                with patch('sys.stdout', new=StringIO()) as mock_print:
                    PromptData.Wallet = self.GetWallet1(recreate=True)

                    token_hash = '31730cc9a1844891a3bafd1aa929a4142860d8d3'
                    ImportToken(PromptData.Wallet, token_hash)

                    args = [
                        'send', 'NXT4', self.watch_addr_str, '30',
                        '--from-addr=%s' % self.wallet_1_addr
                    ]

                    res = Wallet.CommandWallet().execute(args)

                    self.assertTrue(res)
                    self.assertIn(
                        "Will transfer 30.00000000 NXT4 from AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3 to AGYaEi3W6ndHPUmW7T12FFfsbQ6DWymkEm",
                        mock_print.getvalue())
    def test_wallet_claim_3(self):
        self.OpenWallet1()

        # test with bad --to-addr
        with patch('sys.stdout', new=StringIO()) as mock_print:
            with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]):
                args = ['claim', '--to-addr=AGYaEi3W6ndHPUmW7T12FFfsbQ6DWymkEn']  # bad address checksum
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertEqual(claim_tx, None)
                self.assertFalse(relayed)
                self.assertIn("Address format error", mock_print.getvalue())

        # test with an invalid --to-addr
        with patch('sys.stdout', new=StringIO()) as mock_print:
            with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]):
                args = ['claim', '--to-addr=blah']  # completely wrong address format
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertEqual(claim_tx, None)
                self.assertFalse(relayed)
                self.assertIn("Not correct Address, wrong length", mock_print.getvalue())

        # test with --to-addr
        nodemgr = NodeManager()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]):
            with patch('neo.Network.node.NeoNode.relay', return_value=self.async_return(True)):
                args = ['claim', '--to-addr=' + self.watch_addr_str]
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertIsInstance(claim_tx, ClaimTransaction)
                self.assertTrue(relayed)

                json_tx = claim_tx.ToJson()
                self.assertEqual(json_tx['vout'][0]['address'], self.watch_addr_str)  # note how the --to-addr supercedes the default change address
        nodemgr.reset_for_test()
Beispiel #4
0
    def __init__(self, wallet=None):
        if not os.getenv("NEOPYTHON_UNITTEST"):
            stdio_handler = logging.StreamHandler()
            stdio_handler.setLevel(logging.INFO)
            _logger = logging.getLogger('aiohttp.access')
            _logger.addHandler(stdio_handler)
            _logger.setLevel(logging.DEBUG)

            self.app = web.Application(logger=_logger)
        else:
            self.app = web.Application()
        self.port = settings.RPC_PORT
        self.wallet = wallet
        self.nodemgr = NodeManager()

        cors = aiohttp_cors.setup(self.app, defaults={
            "*": aiohttp_cors.ResourceOptions(
                allow_headers=('Content-Type', 'Access-Control-Allow-Headers', 'Authorization', 'X-Requested-With')
            )
        })

        self.app.router.add_post("/", self.home)
        # TODO: find a fix for adding an OPTIONS route in combination with CORS. It works fine without CORS
        # self.app.router.add_options("/", self.home)
        self.app.router.add_get("/", self.home)

        for route in list(self.app.router.routes()):
            if not isinstance(route.resource, web.StaticResource):  # <<< WORKAROUND
                cors.add(route)
    def test_wallet_claim_2(self):
        self.OpenWallet2()

        # test with bad --from-addr
        with patch('sys.stdout', new=StringIO()) as mock_print:
            with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]):
                args = ['claim', '--from-addr=AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc']  # address is too short
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertEqual(claim_tx, None)
                self.assertFalse(relayed)
                self.assertIn("Not correct Address, wrong length.", mock_print.getvalue())

        # test with invalid --from-addr
        with patch('sys.stdout', new=StringIO()) as mock_print:
            with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]):
                args = ['claim', '--from-addr=VJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3']  # address does not start with 'A'
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertEqual(claim_tx, None)
                self.assertFalse(relayed)
                self.assertIn("Address format error", mock_print.getvalue())

        # successful test with --from-addr
        nodemgr = NodeManager()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]):
            with patch('neo.Network.node.NeoNode.relay', return_value=self.async_return(True)):
                args = ['claim', '--from-addr=' + self.wallet_1_addr]
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertIsInstance(claim_tx, ClaimTransaction)
                self.assertTrue(relayed)

                json_tx = claim_tx.ToJson()
                self.assertEqual(json_tx['vout'][0]['address'], self.wallet_1_addr)
        nodemgr.reset_for_test()
Beispiel #6
0
    def test_sendmany_good_simple(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            with patch('sys.stdout', new=StringIO()) as mock_print:
                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()
                           ]):
                    PromptData.Wallet = self.GetWallet1(recreate=True)
                    args = ['sendmany', '2']
                    res = Wallet.CommandWallet().execute(args)

                    self.assertTrue(res)  # verify successful tx
                    self.assertIn("Sending with fee: 0", mock_print.getvalue())
                    json_res = res.ToJson()

                    # 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)
 async def wait_for_peers(self):
     while len(NodeManager().nodes) < 10:
         self.logger.debug(
             'waiting for at least 10 NodeManager peers. currently %s connected.'
             % len(NodeManager().nodes))
         await asyncio.sleep(1)
     self.logger.debug('%s connected NodeManager peers!' %
                       len(NodeManager().nodes))
Beispiel #8
0
def InvokeWithTokenVerificationScript(wallet, tx, token, fee=Fixed8.Zero(), invoke_attrs=None):
    try:
        wallet_tx = wallet.MakeTransaction(tx=tx, fee=fee, use_standard=True)
    except ValueError:
        print("Insufficient funds")
        return False

    if wallet_tx:

        token_contract_state = Blockchain.Default().GetContract(token.ScriptHash.ToString())
        print("token contract  %s " % token_contract_state)

        tx.Attributes = [
            TransactionAttribute(usage=TransactionAttributeUsage.Script,
                                 data=token.ScriptHash.Data)
        ]

        if invoke_attrs:
            tx.Attributes += invoke_attrs

        reedeem_script = token_contract_state.Code.Script.hex()

        # there has to be at least 1 param, and the first
        # one needs to be a signature param
        param_list = bytearray(b'\x00\x00')

        verification_contract = Contract.Create(reedeem_script, param_list, wallet.GetDefaultContract().PublicKeyHash)

        context = ContractParametersContext(wallet_tx)

        wallet.Sign(context)

        context.Add(verification_contract, 0, 0)

        if context.Completed:

            wallet_tx.scripts = context.GetScripts()

            nodemgr = NodeManager()
            relayed = nodemgr.relay(wallet_tx)

            if relayed:
                print("Relayed Tx: %s " % wallet_tx.Hash.ToString())

                # if it was relayed, we save tx
                wallet.SaveTransaction(wallet_tx)

                return wallet_tx
            else:
                print("Could not relay tx %s " % wallet_tx.Hash.ToString())
        else:

            print("Incomplete signature")

    else:
        print("Insufficient funds")

    return False
Beispiel #9
0
    async def start(self):
        Message._magic = settings.MAGIC
        self.nodemgr = NodeManager()
        self.syncmgr = SyncManager(self.nodemgr)
        ledger = Ledger()
        self.syncmgr.ledger = ledger

        logging.getLogger("asyncio").setLevel(logging.DEBUG)
        self.loop.set_debug(False)
        self.nodemgr_task = self.loop.create_task(self.nodemgr.start())
        self.loop.create_task(self.syncmgr.start())
    def test_5_wallet_claim_ok(self):

        wallet = self.GetWallet1()
        nodemgr = NodeManager()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            with patch('neo.Prompt.Commands.Wallet.prompt',
                       return_value=self.wallet_1_pass()):
                claim_tx, relayed = ClaimGas(wallet)
                self.assertIsInstance(claim_tx, ClaimTransaction)
                self.assertTrue(relayed)
Beispiel #11
0
def InvokeContract(wallet, tx, fee=Fixed8.Zero(), from_addr=None, owners=None):
    if from_addr is not None:
        from_addr = PromptUtils.lookup_addr_str(wallet, from_addr)

    try:
        wallet_tx = wallet.MakeTransaction(tx=tx, fee=fee, use_standard=True, from_addr=from_addr)
    except ValueError:
        print("Insufficient funds")
        return False

    if wallet_tx:

        if owners:
            for owner in list(owners):
                wallet_tx.Attributes.append(TransactionAttribute(usage=TransactionAttributeUsage.Script, data=owner))
            wallet_tx.Attributes = make_unique_script_attr(tx.Attributes)

        context = ContractParametersContext(wallet_tx)
        wallet.Sign(context)

        if owners:
            gather_signatures(context, wallet_tx, list(owners))

        if context.Completed:

            wallet_tx.scripts = context.GetScripts()

            passed, reason = validate_simple_policy(wallet_tx)
            if not passed:
                print(reason)
                return False

            nodemgr = NodeManager()
            relayed = nodemgr.relay(wallet_tx)

            if relayed:
                print("Relayed Tx: %s " % wallet_tx.Hash.ToString())

                wallet.SaveTransaction(wallet_tx)

                return wallet_tx
            else:
                print("Could not relay tx %s " % wallet_tx.Hash.ToString())
        else:

            print("Incomplete signature")

    else:
        print("Insufficient funds")

    return False
Beispiel #12
0
    def test_sendmany_good_complex(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            with patch('sys.stdout', new=StringIO()) as mock_print:
                with patch('neo.Prompt.Commands.Send.prompt',
                           side_effect=[
                               "neo", "AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaXK",
                               "1", "gas",
                               "AXjaFSP23Jkbe6Pk9pPGT6NBDs1HVdqaXK", "1",
                               UserWalletTestCase.wallet_1_pass()
                           ]):
                    PromptData.Wallet = self.GetWallet1(recreate=True)
                    args = [
                        'sendmany', '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 = next(
                        filter(
                            lambda b: b['asset'] ==
                            '0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7',
                            address_from_account_state['balances']))
                    address_from_gas_bal = address_from_gas['value']

                    res = Wallet.CommandWallet().execute(args)

                    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'])
                    self.assertIn("Sending with fee: 0.005",
                                  mock_print.getvalue())
Beispiel #13
0
class NetworkService(Singleton):
    def init(self):
        self.loop = asyncio.get_event_loop()
        self.syncmgr = None
        self.nodemgr = None

        self.nodemgr_task = None

    async def start(self):
        Message._magic = settings.MAGIC
        self.nodemgr = NodeManager()
        self.syncmgr = SyncManager(self.nodemgr)
        ledger = Ledger()
        self.syncmgr.ledger = ledger

        logging.getLogger("asyncio").setLevel(logging.DEBUG)
        self.loop.set_debug(False)
        self.nodemgr_task = self.loop.create_task(self.nodemgr.start())
        self.loop.create_task(self.syncmgr.start())

    async def shutdown(self):
        if self.nodemgr_task and self.nodemgr_task.done():
            # starting nodemanager can fail if a port is in use, we need to retrieve and mute this exception on shutdown
            with suppress(SystemExit):
                self.nodemgr_task.exception()

        with suppress(asyncio.CancelledError):
            if self.syncmgr:
                await self.syncmgr.shutdown()

        with suppress(asyncio.CancelledError):
            if self.nodemgr:
                await self.nodemgr.shutdown()
Beispiel #14
0
    def test_send_gas(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            with patch('sys.stdout', new=StringIO()) as mock_print:
                with patch('neo.Prompt.Commands.Send.prompt',
                           side_effect=[UserWalletTestCase.wallet_1_pass()]):
                    PromptData.Wallet = self.GetWallet1(recreate=True)
                    args = ['send', 'gas', self.watch_addr_str, '5']
                    res = Wallet.CommandWallet().execute(args)

                    self.assertTrue(res)
                    self.assertIn("Sending with fee: 0", mock_print.getvalue())
    def test_wallet_claim_4(self):
        self.OpenWallet2()

        # test with --from-addr and --to-addr
        nodemgr = NodeManager()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_2_pass()]):
            with patch('neo.Network.node.NeoNode.relay', return_value=self.async_return(True)):
                args = ['claim', '--from-addr=' + self.wallet_1_addr, '--to-addr=' + self.wallet_2_addr]
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertIsInstance(claim_tx, ClaimTransaction)
                self.assertTrue(relayed)

                json_tx = claim_tx.ToJson()
                self.assertEqual(json_tx['vout'][0]['address'],
                                 self.wallet_2_addr)  # note how the --to-addr also supercedes the from address if both are specified
        nodemgr.reset_for_test()
Beispiel #16
0
    def test_bad_attributes(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            with patch('neo.Prompt.Commands.Send.prompt',
                       side_effect=[UserWalletTestCase.wallet_1_pass()]):
                PromptData.Wallet = self.GetWallet1(recreate=True)
                args = [
                    'send', 'gas', self.watch_addr_str, '2',
                    '--tx-attr=[{"usa:241"data":his is a remark"}]'
                ]

                res = Wallet.CommandWallet().execute(args)

                self.assertTrue(res)
                self.assertEqual(1, len(res.Attributes))
Beispiel #17
0
    def test_transaction_size_1(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('sys.stdout', new=StringIO()) as mock_print:
            with patch('neo.Prompt.Commands.Send.prompt',
                       side_effect=[UserWalletTestCase.wallet_1_pass()]):
                with patch('neo.Core.TX.Transaction.Transaction.Size',
                           return_value=1026):  # returns a size of 1026
                    PromptData.Wallet = self.GetWallet1(recreate=True)
                    args = ['send', 'gas', self.watch_addr_str, '5']

                    res = Wallet.CommandWallet().execute(args)

                    self.assertFalse(res)
                    self.assertIn(
                        'Transaction cancelled. The tx size (1026) exceeds the max free tx size (1024).\nA network fee of 0.001 GAS is required.',
                        mock_print.getvalue()
                    )  # notice the required fee is equal to the low priority threshold
    def test_wallet_claim_1(self):
        # test with no wallet
        with patch('sys.stdout', new=StringIO()) as mock_print:
            args = ['claim']
            res = CommandWallet().execute(args)
            self.assertFalse(res)
            self.assertIn("Please open a wallet", mock_print.getvalue())

        self.OpenWallet1()

        # test wrong password
        with patch('sys.stdout', new=StringIO()) as mock_print:
            with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=["wrong"]):
                args = ['claim']
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertEqual(claim_tx, None)
                self.assertFalse(relayed)
                self.assertIn("Incorrect password", mock_print.getvalue())

        # test successful
        nodemgr = NodeManager()
        nodemgr.nodes = [NeoNode(object, object)]
        with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]):
            with patch('neo.Network.node.NeoNode.relay', return_value=self.async_return(True)):
                args = ['claim']
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertIsInstance(claim_tx, ClaimTransaction)
                self.assertTrue(relayed)

                json_tx = claim_tx.ToJson()
                self.assertEqual(json_tx['vout'][0]['address'], self.wallet_1_addr)
        nodemgr.reset_for_test()

        # test nothing to claim anymore
        with patch('sys.stdout', new=StringIO()) as mock_print:
            with patch('neo.Prompt.Commands.Wallet.prompt', side_effect=[WalletFixtureTestCase.wallet_1_pass()]):
                args = ['claim']
                claim_tx, relayed = CommandWallet().execute(args)
                self.assertEqual(claim_tx, None)
                self.assertFalse(relayed)
                self.assertIn("No claims to process", mock_print.getvalue())
Beispiel #19
0
    def test_9_send_neo_tx(self):
        with patch('neo.Network.node.NeoNode.relay', return_value=self.async_return(True)):
            wallet = self.GetWallet1()

            tx = ContractTransaction()
            tx.outputs = [TransactionOutput(Blockchain.SystemShare().Hash, Fixed8.FromDecimal(10.0), self.import_watch_addr)]

            try:
                tx = wallet.MakeTransaction(tx)
            except (ValueError):
                pass

            cpc = ContractParametersContext(tx)
            wallet.Sign(cpc)
            tx.scripts = cpc.GetScripts()

            nodemgr = NodeManager()
            # we need at least 1 node for relay to be mocked
            nodemgr.nodes = [NeoNode(object, object)]
            result = nodemgr.relay(tx)
            self.assertEqual(result, True)
Beispiel #20
0
    def test_attributes(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            with patch('neo.Prompt.Commands.Send.prompt',
                       side_effect=[UserWalletTestCase.wallet_1_pass()]):
                PromptData.Wallet = self.GetWallet1(recreate=True)
                args = [
                    'send', 'gas', self.watch_addr_str, '2',
                    '--tx-attr={"usage":241,"data":"This is a remark"}'
                ]

                res = Wallet.CommandWallet().execute(args)

                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
Beispiel #21
0
    def execute(self, arguments):
        c1 = get_arg(arguments)
        if c1 is not None:

            try:
                c1 = int(c1)
            except ValueError:
                print("Invalid argument")
                return

            if c1 > 10:
                print("Max peers is limited to 10")
                return

            try:
                settings.set_max_peers(c1)
                if c1 < settings.CONNECTED_PEER_MIN:
                    settings.set_min_peers(c1)
                    print(f"Minpeers set to {c1}")
            except ValueError:
                print("Please supply a positive integer for maxpeers")
                return

            nodemgr = NodeManager()
            nodemgr.max_clients = c1

            current_max = settings.CONNECTED_PEER_MAX
            connected_count = len(nodemgr.nodes)
            if current_max < connected_count:
                to_remove = connected_count - c1
                for _ in range(to_remove):
                    last_connected_node = nodemgr.nodes[-1]
                    wait_for(last_connected_node.disconnect()
                             )  # need to avoid it being labelled as dead/bad

            print(f"Maxpeers set to {c1}")
            return c1
        else:
            print(f"Maintaining maxpeers at {settings.CONNECTED_PEER_MAX}")
            return
Beispiel #22
0
    def test_6_split_unspent(self):
        wallet = self.GetWallet1(recreate=True)
        addr = wallet.ToScriptHash('AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3')

        nodemgr = NodeManager()
        nodemgr.nodes = [NeoNode(object, object)]

        with patch('neo.Network.node.NeoNode.relay',
                   return_value=self.async_return(True)):
            # bad inputs
            tx = SplitUnspentCoin(None, self.NEO, addr, 0, 2)
            self.assertEqual(tx, None)

            tx = SplitUnspentCoin(wallet, self.NEO, addr, 3, 2)
            self.assertEqual(tx, None)

            tx = SplitUnspentCoin(wallet, 'bla', addr, 0, 2)
            self.assertEqual(tx, None)

            # should be ok
            with patch('neo.Prompt.Commands.WalletAddress.prompt',
                       return_value=self.wallet_1_pass()):
                tx = SplitUnspentCoin(wallet, self.NEO, addr, 0, 2)
                self.assertIsNotNone(tx)

                # rebuild wallet and try with non-even amount of neo, should be split into integer values of NEO
                wallet = self.GetWallet1(True)
                tx = SplitUnspentCoin(wallet, self.NEO, addr, 0, 3)
                self.assertIsNotNone(tx)
                self.assertEqual([
                    Fixed8.FromDecimal(17),
                    Fixed8.FromDecimal(17),
                    Fixed8.FromDecimal(16)
                ], [item.Value for item in tx.outputs])

                # try with gas
                wallet = self.GetWallet1(True)
                tx = SplitUnspentCoin(wallet, self.GAS, addr, 0, 3)
                self.assertIsNotNone(tx)
Beispiel #23
0
    def execute(self, arguments=None):
        show_verbose = get_arg(arguments) == 'verbose'
        show_queued = get_arg(arguments) == 'queued'
        show_known = get_arg(arguments) == 'known'
        show_bad = get_arg(arguments) == 'bad'

        nodemgr = NodeManager()
        len_nodes = len(nodemgr.nodes)
        out = ""
        if len_nodes > 0:
            out = f"Connected: {len_nodes} of max {nodemgr.max_clients}\n"
            for i, node in enumerate(nodemgr.nodes):
                out += f"Peer {i} {node.version.user_agent:>12} {node.address:>21} height: {node.best_height:>8}\n"
        else:
            print("No nodes connected yet\n")

        if show_verbose:
            out += f"\n"
            out += f"Addresses in queue: {len(nodemgr.queued_addresses)}\n"
            out += f"Known addresses: {len(nodemgr.known_addresses)}\n"
            out += f"Bad addresses: {len(nodemgr.bad_addresses)}\n"

        if show_queued:
            out += f"\n"
            if len(nodemgr.queued_addresses) == 0:
                out += "No queued addresses"
            else:
                out += f"Queued addresses:\n"
                for addr in nodemgr.queued_addresses:
                    out += f"{addr}\n"

        if show_known:
            out += f"\n"
            if len(nodemgr.known_addresses) == 0:
                out += "No known addresses other than connect peers"
            else:
                out += f"Known addresses:\n"
                for addr in nodemgr.known_addresses:
                    out += f"{addr}\n"

        if show_bad:
            out += f"\n"
            if len(nodemgr.bad_addresses) == 0:
                out += "No bad addresses"
            else:
                out += f"Bad addresses:\n"
                for addr in nodemgr.bad_addresses:
                    out += f"{addr}\n"
        print(out)
        return out
Beispiel #24
0
def parse_and_sign(wallet, jsn):
    try:
        context = ContractParametersContext.FromJson(jsn)
        if context is None:
            print("Failed to parse JSON")
            return

        wallet.Sign(context)

        if context.Completed:

            print("Signature complete, relaying...")

            tx = context.Verifiable
            tx.scripts = context.GetScripts()

            wallet.SaveTransaction(tx)

            print("will send tx: %s " % json.dumps(tx.ToJson(), indent=4))

            nodemgr = NodeManager()
            relayed = nodemgr.relay(tx)

            if relayed:
                print("Relayed Tx: %s " % tx.Hash.ToString())
            else:
                print("Could not relay tx %s " % tx.Hash.ToString())
            return
        else:
            print("Transaction initiated, but the signature is incomplete")
            print(json.dumps(context.ToJson(), separators=(',', ':')))
            return

    except Exception as e:
        print("Could not send: %s " % e)
        traceback.print_stack()
        traceback.print_exc()
    def test_show_nodes(self):
        nodemgr = NodeManager()
        nodemgr.reset_for_test()

        # test "nodes" with no nodes connected
        args = ['nodes']
        with patch('sys.stdout', new=StringIO()) as mock_print:
            res = CommandShow().execute(args)
            self.assertFalse(res)
            self.assertIn('No nodes connected yet', mock_print.getvalue())

        # test "nodes verbose" with no nodes connected
        args = ['nodes', 'verbose']
        res = CommandShow().execute(args)
        self.assertIn('Addresses in queue: 0', res)
        self.assertIn('Known addresses: 0', res)
        self.assertIn('Bad addresses: 0', res)

        # test "nodes queued" with no nodes connected
        args = ['nodes', 'queued']
        res = CommandShow().execute(args)
        self.assertIn('No queued addresses', res)

        # test "nodes known" with no nodes connected
        args = ['nodes', 'known']
        res = CommandShow().execute(args)
        self.assertIn('No known addresses other than connect peers', res)

        # test "nodes bad" with no nodes connected
        args = ['nodes', 'bad']
        res = CommandShow().execute(args)
        self.assertIn('No bad addresses', res)

        # query nodes with connected peers
        # first make sure we have a predictable state
        node1 = NeoNode(object, object)
        node2 = NeoNode(object, object)
        node1.address = "127.0.0.1:20333"
        node2.address = "127.0.0.1:20334"
        node1.best_height = 1025
        node2.best_height = 1026
        node1.version = MagicMock()
        node2.version = MagicMock()
        node1.version.user_agent = "test_user_agent"
        node2.version.user_agent = "test_user_agent"

        nodemgr.nodes = [node1, node2]

        queued_address = "127.0.0.1:20335"
        known_address = "127.0.0.1:20336"
        bad_address = "127.0.0.1:20337"

        nodemgr.queued_addresses.append(queued_address)
        nodemgr.known_addresses.append(known_address)
        nodemgr.bad_addresses.append(bad_address)

        # now use "node"
        args = ['node']
        res = CommandShow().execute(args)
        self.assertIn("Connected: 2", res)
        self.assertIn("Peer 1", res)
        self.assertIn("1025", res)

        # test "nodes verbose" with queued, known, and bad addresses
        args = ['nodes', 'verbose']
        res = CommandShow().execute(args)
        self.assertIn("Addresses in queue: 1", res)
        self.assertIn("Known addresses: 1", res)
        self.assertIn("Bad addresses: 1", res)

        # test "nodes queued" with queued, known, and bad addresses
        args = ['nodes', 'queued']
        res = CommandShow().execute(args)
        self.assertIn("Queued addresses:", res)
        self.assertIn(queued_address, res)

        # test "nodes known" with queued, known, and bad addresses
        args = ['nodes', 'known']
        res = CommandShow().execute(args)
        self.assertIn("Known addresses:", res)
        self.assertIn(known_address, res)

        # test "nodes bad" with queued, known, and bad addresses
        args = ['nodes', 'bad']
        res = CommandShow().execute(args)
        self.assertIn("Bad addresses:", res)
        self.assertIn(bad_address, res)

        nodemgr.reset_for_test()
Beispiel #26
0
def SplitUnspentCoin(wallet,
                     asset_id,
                     from_addr,
                     index,
                     divisions,
                     fee=Fixed8.Zero()):
    """
    Split unspent asset vins into several vouts

    Args:
        wallet (neo.Wallet): wallet to show unspent coins from.
        asset_id (UInt256): a bytearray (len 32) representing an asset on the blockchain.
        from_addr (UInt160): a bytearray (len 20) representing an address.
        index (int): index of the unspent vin to split
        divisions (int): number of vouts to create
        fee (Fixed8): A fee to be attached to the Transaction for network processing purposes.

    Returns:
        neo.Core.TX.Transaction.ContractTransaction: contract transaction created
    """

    if wallet is None:
        print("Please open a wallet.")
        return

    unspent_items = wallet.FindUnspentCoinsByAsset(asset_id,
                                                   from_addr=from_addr)
    if not unspent_items:
        print(f"No unspent assets matching the arguments.")
        return

    if index < len(unspent_items):
        unspent_item = unspent_items[index]
    else:
        print(f"unspent-items: {unspent_items}")
        print(
            f"Could not find unspent item for asset {asset_id} with index {index}"
        )
        return

    outputs = split_to_vouts(asset_id, from_addr, unspent_item.Output.Value,
                             divisions)

    # subtract a fee from the first vout
    if outputs[0].Value > fee:
        outputs[0].Value -= fee
    else:
        print("Fee could not be subtracted from outputs.")
        return

    contract_tx = ContractTransaction(outputs=outputs,
                                      inputs=[unspent_item.Reference])

    ctx = ContractParametersContext(contract_tx)
    wallet.Sign(ctx)

    print("Splitting: %s " % json.dumps(contract_tx.ToJson(), indent=4))
    try:
        passwd = prompt("[Password]> ", is_password=True)
    except KeyboardInterrupt:
        print("Splitting cancelled")
        return
    if not wallet.ValidatePassword(passwd):
        print("incorrect password")
        return

    if ctx.Completed:
        contract_tx.scripts = ctx.GetScripts()

        nodemgr = NodeManager()
        # this blocks, consider moving this wallet function to async instead
        relayed = nodemgr.relay(contract_tx)

        if relayed:
            wallet.SaveTransaction(contract_tx)
            print("Relayed Tx: %s " % contract_tx.Hash.ToString())
            return contract_tx
        else:
            print("Could not relay tx %s " % contract_tx.Hash.ToString())
    async def run_loop(self):
        nodemgr = NodeManager()
        while not nodemgr.running:
            await asyncio.sleep(0.1)

        await self.custom_background_code()
Beispiel #28
0
 async def get_status(self, request):
     return {
         'current_height': Blockchain.Default().Height,
         'version': settings.VERSION_NAME,
         'num_peers': len(NodeManager().nodes)
     }
    def setUpClass(cls):

        Blockchain.DeregisterBlockchain()

        super(BlockchainFixtureTestCase, cls).setUpClass()

        # for some reason during testing asyncio.get_event_loop() fails and does not create a new one if needed. This is the workaround
        try:
            loop = asyncio.get_running_loop()
        except RuntimeError:
            loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        nodemgr = NodeManager()
        nodemgr.reset_for_test()

        # setup Blockchain DB
        if not os.path.exists(cls.FIXTURE_FILENAME):
            logger.info(
                "downloading fixture block database from %s. this may take a while"
                % cls.FIXTURE_REMOTE_LOC)

            response = requests.get(cls.FIXTURE_REMOTE_LOC, stream=True)

            response.raise_for_status()
            os.makedirs(os.path.dirname(cls.FIXTURE_FILENAME), exist_ok=True)
            with open(cls.FIXTURE_FILENAME, 'wb+') as handle:
                for block in response.iter_content(1024):
                    handle.write(block)

        try:
            tar = tarfile.open(cls.FIXTURE_FILENAME)
            tar.extractall(path=settings.DATA_DIR_PATH)
            tar.close()
        except Exception as e:
            raise Exception(
                "Could not extract tar file - %s. You may want need to remove the fixtures file %s manually to fix this."
                % (e, cls.FIXTURE_FILENAME))

        if not os.path.exists(cls.leveldb_testpath()):
            raise Exception("Error downloading fixtures at %s" %
                            cls.leveldb_testpath())

        settings.setup_unittest_net()

        cls._blockchain = Blockchain(
            getBlockchainDB(path=cls.leveldb_testpath()),
            skip_version_check=True)

        cls._blockchain.UT = True
        Blockchain.RegisterBlockchain(cls._blockchain)

        # setup Notification DB
        if not os.path.exists(cls.N_FIXTURE_FILENAME):
            logger.info(
                "downloading fixture notification database from %s. this may take a while"
                % cls.N_FIXTURE_REMOTE_LOC)

            response = requests.get(cls.N_FIXTURE_REMOTE_LOC, stream=True)

            response.raise_for_status()
            with open(cls.N_FIXTURE_FILENAME, 'wb+') as handle:
                for block in response.iter_content(1024):
                    handle.write(block)

        try:
            tar = tarfile.open(cls.N_FIXTURE_FILENAME)
            tar.extractall(path=settings.DATA_DIR_PATH)
            tar.close()

        except Exception as e:
            raise Exception(
                "Could not extract tar file - %s. You may want need to remove the fixtures file %s manually to fix this."
                % (e, cls.N_FIXTURE_FILENAME))
        if not os.path.exists(cls.N_NOTIFICATION_DB_NAME):
            raise Exception("Error downloading fixtures at %s" %
                            cls.N_NOTIFICATION_DB_NAME)

        settings.NOTIFICATION_DB_PATH = cls.N_NOTIFICATION_DB_NAME
        ndb = NotificationDB.instance()
        ndb.start()
Beispiel #30
0
def process_transaction(wallet,
                        contract_tx,
                        scripthash_from=None,
                        scripthash_change=None,
                        fee=None,
                        owners=None,
                        user_tx_attributes=None):
    try:
        tx = wallet.MakeTransaction(tx=contract_tx,
                                    change_address=scripthash_change,
                                    fee=fee,
                                    from_addr=scripthash_from)
    except ValueError:
        print(
            "Insufficient funds. No unspent outputs available for building the transaction.\n"
            "If you are trying to sent multiple transactions in 1 block, then make sure you have enough 'vouts'\n."
            "Use `wallet unspent` and `wallet address split`, or wait until the first transaction is processed before sending another."
        )
        return

    if tx is None:
        logger.debug("insufficient funds")
        return

    try:
        print("Validate your transaction details")
        print("-" * 33)
        input_coinref = wallet.FindCoinsByVins(tx.inputs)[0]
        source_addr = input_coinref.Address
        for order in tx.outputs:
            dest_addr = order.Address
            value = order.Value.ToString()  # fixed8
            if order.AssetId == Blockchain.Default().SystemShare().Hash:
                asset_name = 'NEO'
            else:
                asset_name = 'GAS'

            if source_addr != dest_addr:
                print(
                    f"Sending {value} {asset_name} from {source_addr} to {dest_addr}"
                )
            else:
                print(
                    f"Returning {value} {asset_name} as change to {dest_addr}")
        print(" ")
        print("Enter your password to send to the network")

        try:
            passwd = prompt("[Password]> ", is_password=True)
        except KeyboardInterrupt:
            print("Transaction cancelled")
            return
        if not wallet.ValidatePassword(passwd):
            print("Incorrect password")
            return

        standard_contract = wallet.GetStandardAddress()

        if scripthash_from is not None:
            signer_contract = wallet.GetContract(scripthash_from)
        else:
            signer_contract = wallet.GetContract(standard_contract)

        if not signer_contract.IsMultiSigContract and owners is None:
            data = standard_contract.Data
            tx.Attributes = [
                TransactionAttribute(usage=TransactionAttributeUsage.Script,
                                     data=data)
            ]

        # insert any additional user specified tx attributes
        tx.Attributes = tx.Attributes + user_tx_attributes

        if owners:
            owners = list(owners)
            for owner in owners:
                tx.Attributes.append(
                    TransactionAttribute(
                        usage=TransactionAttributeUsage.Script, data=owner))

        context = ContractParametersContext(
            tx, isMultiSig=signer_contract.IsMultiSigContract)
        wallet.Sign(context)

        if owners:
            owners = list(owners)
            gather_signatures(context, tx, owners)

        if context.Completed:

            tx.scripts = context.GetScripts()

            passed, reason = validate_simple_policy(tx)
            if not passed:
                print(reason)
                return

            nodemgr = NodeManager()
            relayed = nodemgr.relay(tx)

            if relayed:
                wallet.SaveTransaction(tx)

                print("Relayed Tx: %s " % tx.Hash.ToString())
                return tx
            else:

                print("Could not relay tx %s " % tx.Hash.ToString())

        else:
            print(
                "Transaction initiated, but the signature is incomplete. Use the `sign` command with the information below to complete signing."
            )
            print(json.dumps(context.ToJson(), separators=(',', ':')))
            return

    except Exception as e:
        print("Could not send: %s " % e)
        traceback.print_stack()
        traceback.print_exc()

    return