def test_transaction_not_pruned(self):
        daemon_no_prune = Daemon(JSONRPCDaemon(prune_transactions=False))
        responses.add(responses.POST,
                      self.transactions_url,
                      json=self._read('test_transactions_single.json'),
                      status=200)

        tx = daemon_no_prune.transactions(
            'bbc10f5944cc3e88be576d2ab9f4f5ab5a2b46d95a7cab1027bc15c17393102c'
        )[0]
        self.assertIsNotNone(tx.blob)
        self.assertIs(type(tx.blob), bytes)
Ejemplo n.º 2
0
class JSONRPCDaemonTestCase(JSONTestCase):
    jsonrpc_url = 'http://127.0.0.1:18081/json_rpc'
    mempool_url = 'http://127.0.0.1:18081/get_transaction_pool'
    transactions_url = 'http://127.0.0.1:18081/get_transactions'
    sendrawtransaction_url = 'http://127.0.0.1:18081/sendrawtransaction'
    data_subdir = 'test_jsonrpcdaemon'

    def setUp(self):
        self.daemon = Daemon(JSONRPCDaemon())

    @responses.activate
    def test_basic_info(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_basic_info-get_info.json'),
                      status=200)
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_basic_info-get_info.json'),
                      status=200)
        self.assertTrue(self.daemon.info())
        self.assertEqual(self.daemon.height(), 294993)

    @responses.activate
    def test_net(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_basic_info-get_info.json'),
                      status=200)
        self.assertEqual(self.daemon.net, NET_STAGE)
        self.daemon.net
        self.assertEqual(len(responses.calls), 1,
                         "net value has not been cached?")

    @responses.activate
    def test_info_then_net(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_basic_info-get_info.json'),
                      status=200)
        self.daemon.info()
        self.assertEqual(self.daemon.net, NET_STAGE)
        self.assertEqual(len(responses.calls), 1,
                         "net value has not been cached?")

    @responses.activate
    def test_mempool(self):
        responses.add(responses.POST,
                      self.mempool_url,
                      json=self._read('test_mempool-transactions.json'),
                      status=200)
        txs = self.daemon.mempool()
        self.assertEqual(len(txs), 2)
        self.assertEqual(txs[0].confirmations, 0)
        self.assertEqual(txs[1].confirmations, 0)
        self.assertGreater(txs[0].fee, 0)
        self.assertGreater(txs[1].fee, 0)

    @responses.activate
    def test_block(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read(
                "test_block-423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89.json"
            ),
            status=200)
        responses.add(
            responses.POST,
            self.transactions_url,
            json=self._read(
                "test_block-423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89-txns.json"
            ),
            status=200)
        blk = self.daemon.block(
            "423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89")
        self.assertEqual(
            blk.hash,
            "423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89")
        self.assertEqual(blk.height, 451992)
        self.assertIsInstance(blk.reward, decimal.Decimal)

        self.assertIn(
            "24fb42f9f324082658524b29b4cf946a9f5fcfa82194070e2f17c1875e15d5d0",
            blk)
        for tx in blk.transactions:
            self.assertIn(tx, blk)

        # tx not in block
        self.assertNotIn(
            "e3a3b8361777c8f4f1fd423b86655b5c775de0230b44aa5b82f506135a96c53a",
            blk)
        # wrong arg type
        self.assertRaises(ValueError, lambda txid: txid in blk, 1245)

    @responses.activate
    def test_transactions(self):
        responses.add(responses.POST,
                      self.transactions_url,
                      json=self._read('test_transactions.json'),
                      status=200)
        txs = self.daemon.transactions([
            "050679bd5717cd4c3d0ed1db7dac4aa7e8a222ffc7661b249e5a595a3af37d3c",  # @471570
            "e3a3b8361777c8f4f1fd423b86655b5c775de0230b44aa5b82f506135a96c53a",  # @451993
            "e2871c4203e29433257219bc20fa58c68dc12efed8f05a86d59921969a2b97cc",  # @472279
            "035a1cfadd2f80124998f5af8c7bb6703743a4f322d0a20b7f7b502956ada59d",  # mempool
            "feed00000000000face00000000000bad00000000000beef00000000000acab0",  # doesn't exist
        ])
        self.assertEqual(len(txs), 4)
        self.assertEqual(
            txs[0].hash,
            "050679bd5717cd4c3d0ed1db7dac4aa7e8a222ffc7661b249e5a595a3af37d3c")
        self.assertEqual(txs[0].height, 471570)
        self.assertEqual(txs[0].size, 2826)
        self.assertEqual(
            txs[1].hash,
            "e3a3b8361777c8f4f1fd423b86655b5c775de0230b44aa5b82f506135a96c53a")
        self.assertEqual(txs[1].height, 451993)
        self.assertEqual(txs[1].size, 2596)
        self.assertEqual(
            txs[2].hash,
            "e2871c4203e29433257219bc20fa58c68dc12efed8f05a86d59921969a2b97cc")
        self.assertEqual(txs[2].height, 472279)
        self.assertEqual(txs[2].size, 2796)
        self.assertEqual(
            txs[3].hash,
            "035a1cfadd2f80124998f5af8c7bb6703743a4f322d0a20b7f7b502956ada59d")
        self.assertIsNone(txs[3].height)
        self.assertEqual(txs[3].size, 2724)

    @responses.activate
    def test_send_transaction(self):
        path = os.path.join(
            os.path.dirname(__file__), "data", self.data_subdir,
            "0e8fa9202e0773333360e5b9e8fb8e94272c16a8a58b6fe7cf3b4327158e3a44.tx"
        )
        responses.add(responses.POST,
                      self.sendrawtransaction_url,
                      json=self._read('test_send_transaction.json'),
                      status=200)
        tx = Transaction(blob=open(path, "rb").read())
        rsp = self.daemon.send_transaction(tx)
        self.assertEqual(rsp["status"], "OK")
Ejemplo n.º 3
0
 def test_multiple_outputs(self):
     daemon = Daemon(JSONRPCDaemon(host="127.0.0.1", port=38081))
     responses.add(responses.POST,
                   self.wallet_jsonrpc_url,
                   json=self._read(
                       "test_multiple_outputs-wallet-00-get_accounts.json"),
                   status=200)
     responses.add(
         responses.POST,
         self.wallet_jsonrpc_url,
         json=self._read("test_multiple_outputs-wallet-01-query_key.json"),
         status=200)
     responses.add(
         responses.POST,
         self.wallet_jsonrpc_url,
         json=self._read(
             "test_multiple_outputs-wallet-02-addresses-account-0.json"),
         status=200)
     responses.add(
         responses.POST,
         self.wallet_jsonrpc_url,
         json=self._read(
             "test_multiple_outputs-wallet-02-addresses-account-1.json"),
         status=200)
     wallet = Wallet(JSONRPCWallet(host="127.0.0.1", port=38083))
     responses.add(
         responses.POST,
         self.daemon_transactions_url,
         json=self._read(
             "test_multiple_outputs-daemon-00-get_transactions.json"),
         status=200)
     tx = daemon.transactions(
         "f79a10256859058b3961254a35a97a3d4d5d40e080c6275a3f9779acde73ca8d"
     )[0]
     outs = tx.outputs(wallet=wallet)
     self.assertEqual(len(outs), 5)
     self.assertEqual(
         outs[0].stealth_address,
         "d3eb42322566c1d48685ee0d1ad7aed2ba6210291a785ec051d8b13ae797d202")
     self.assertEqual(
         outs[1].stealth_address,
         "5bda44d7953e27b84022399850b59ed87408facdf00bbd1a2d4fda4bf9ebf72f")
     self.assertEqual(
         outs[2].stealth_address,
         "4c79c14d5d78696e72959a28a734ec192059ebabb931040b5a0714c67b507e76")
     self.assertEqual(
         outs[3].stealth_address,
         "64de2b358cdf96d498a9688edafcc0e25c60179e813304747524c876655a8e55")
     self.assertEqual(
         outs[4].stealth_address,
         "966240954892294091a48c599c6db2b028e265c67677ed113d2263a7538f9a43")
     self.assertIsNotNone(outs[0].payment)
     self.assertIsNone(
         outs[1].payment)  # FIXME: isn't that change we should recognize?
     self.assertIsNotNone(outs[2].payment)
     self.assertIsNotNone(outs[3].payment)
     self.assertIsNotNone(outs[4].payment)
     self.assertEqual(outs[0].amount, outs[0].payment.amount)
     self.assertEqual(outs[2].amount, outs[2].payment.amount)
     self.assertEqual(outs[3].amount, outs[3].payment.amount)
     self.assertEqual(outs[4].amount, outs[4].payment.amount)
     self.assertEqual(outs[0].amount, Decimal(4))
     self.assertIsNone(outs[1].amount)
     self.assertEqual(outs[2].amount, Decimal(1))
     self.assertEqual(outs[3].amount, Decimal(2))
     self.assertEqual(outs[4].amount, Decimal(8))
     self.assertEqual(
         outs[0].payment.local_address,
         "76Qt2xMZ3m7b2tagubEgkvG81pwf9P3JYdxR65H2BEv8c79A9pCBTacEFv87tfdcqXRemBsZLFVGHTWbqBpkoBJENBoJJS9"
     )
     self.assertEqual(
         outs[2].payment.local_address,
         "78zGgzb45TEL8uvRFjCayUjHS98RFry1f7P4PE4LU7oeLh42s9AtP8fYXVzWqUW4r3Nz4g3V64w9RSiV7o3zUbPZVs5DVaU"
     )
     self.assertEqual(
         outs[3].payment.local_address,
         "73ndji4W2bu4WED87rJDVALMvUsZLLYstZsigbcGfb5YG9SuNyCSYk7Qbttez2mXciKtWRzRN9aYGJbF9TPBidNQNZppnFw"
     )
     self.assertEqual(
         outs[4].payment.local_address,
         "7BJxHKTa4p5USJ9Z5GY15ZARXL6Qe84qT3FnWkMbSJSoEj9ugGjnpQ1N9H1jqkjsTzLiN5VTbCP8f4MYYVPAcXhr36bHXzP"
     )
     self.assertEqual(
         repr(outs[0]),
         "d3eb42322566c1d48685ee0d1ad7aed2ba6210291a785ec051d8b13ae797d202, 4.000000000000 "
         "to [76Qt2x]")
class JSONRPCDaemonTestCase(JSONTestCase):
    jsonrpc_url = 'http://127.0.0.1:18081/json_rpc'
    mempool_url = 'http://127.0.0.1:18081/get_transaction_pool'
    transactions_url = 'http://127.0.0.1:18081/get_transactions'
    sendrawtransaction_url = 'http://127.0.0.1:18081/sendrawtransaction'
    data_subdir = 'test_jsonrpcdaemon'

    def setUp(self):
        self.daemon = Daemon(JSONRPCDaemon())

        # this is disabled b/c raw_request logs errors
        logging.getLogger('monero.backends.jsonrpc.daemon').disabled = True

    @responses.activate
    def test_basic_info(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_basic_info-get_info.json'),
                      status=200)
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_basic_info-get_info.json'),
                      status=200)
        self.assertTrue(self.daemon.info())
        self.assertEqual(self.daemon.height(), 294993)

    @responses.activate
    def test_net(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_basic_info-get_info.json'),
                      status=200)
        self.assertEqual(self.daemon.net, NET_STAGE)
        self.daemon.net
        self.assertEqual(len(responses.calls), 1,
                         "net value has not been cached?")

    @responses.activate
    def test_info_then_net(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_basic_info-get_info.json'),
                      status=200)
        self.daemon.info()
        self.assertEqual(self.daemon.net, NET_STAGE)
        self.assertEqual(len(responses.calls), 1,
                         "net value has not been cached?")

    @responses.activate
    def test_mempool(self):
        responses.add(responses.POST,
                      self.mempool_url,
                      json=self._read('test_mempool-transactions.json'),
                      status=200)
        txs = self.daemon.mempool()
        self.assertEqual(len(txs), 2)
        self.assertEqual(txs[0].confirmations, 0)
        self.assertEqual(txs[1].confirmations, 0)
        self.assertGreater(txs[0].fee, 0)
        self.assertGreater(txs[1].fee, 0)

    @responses.activate
    def test_block(self):
        responses.add(
            responses.POST,
            self.jsonrpc_url,
            json=self._read(
                "test_block-423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89.json"
            ),
            status=200)
        responses.add(
            responses.POST,
            self.transactions_url,
            json=self._read(
                "test_block-423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89-txns.json"
            ),
            status=200)
        blk = self.daemon.block(
            "423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89")
        self.assertEqual(
            blk.hash,
            "423cd4d170c53729cf25b4243ea576d1e901d86e26c06d6a7f79815f3fcb9a89")
        self.assertEqual(blk.height, 451992)
        self.assertIsInstance(blk.reward, decimal.Decimal)

        self.assertIn(
            "24fb42f9f324082658524b29b4cf946a9f5fcfa82194070e2f17c1875e15d5d0",
            blk)
        for tx in blk.transactions:
            self.assertIn(tx, blk)

        # tx not in block
        self.assertNotIn(
            "e3a3b8361777c8f4f1fd423b86655b5c775de0230b44aa5b82f506135a96c53a",
            blk)
        # wrong arg type
        self.assertRaises(ValueError, lambda txid: txid in blk, 1245)

        # block or hash not specified
        with self.assertRaises(ValueError):
            self.daemon.block()

    @responses.activate
    def test_transactions(self):
        responses.add(responses.POST,
                      self.transactions_url,
                      json=self._read('test_transactions_pruned.json'),
                      status=200)
        txs = self.daemon.transactions([
            "050679bd5717cd4c3d0ed1db7dac4aa7e8a222ffc7661b249e5a595a3af37d3c",  # @471570
            "e3a3b8361777c8f4f1fd423b86655b5c775de0230b44aa5b82f506135a96c53a",  # @451993
            "e2871c4203e29433257219bc20fa58c68dc12efed8f05a86d59921969a2b97cc",  # @472279
            "035a1cfadd2f80124998f5af8c7bb6703743a4f322d0a20b7f7b502956ada59d",  # mempool
            "feed00000000000face00000000000bad00000000000beef00000000000acab0",  # doesn't exist
        ])
        self.assertEqual(len(txs), 4)
        self.assertEqual(
            txs[0].hash,
            "050679bd5717cd4c3d0ed1db7dac4aa7e8a222ffc7661b249e5a595a3af37d3c")
        self.assertEqual(txs[0].height, 471570)
        with self.assertRaises(TransactionWithoutBlob):
            txs[0].size
        self.assertEqual(txs[0].fee, decimal.Decimal('0.000331130000'))
        self.assertIsNone(txs[0].blob)
        self.assertEqual(
            txs[1].hash,
            "e3a3b8361777c8f4f1fd423b86655b5c775de0230b44aa5b82f506135a96c53a")
        self.assertEqual(txs[1].height, 451993)
        with self.assertRaises(TransactionWithoutBlob):
            txs[1].size
        self.assertEqual(txs[1].fee, decimal.Decimal('0.000265330000'))
        self.assertIsNone(txs[1].blob)
        self.assertEqual(
            txs[2].hash,
            "e2871c4203e29433257219bc20fa58c68dc12efed8f05a86d59921969a2b97cc")
        self.assertEqual(txs[2].height, 472279)
        with self.assertRaises(TransactionWithoutBlob):
            txs[2].size
        self.assertEqual(txs[2].fee, decimal.Decimal('0.000327730000'))
        self.assertIsNone(txs[2].blob)
        self.assertEqual(
            txs[3].hash,
            "035a1cfadd2f80124998f5af8c7bb6703743a4f322d0a20b7f7b502956ada59d")
        self.assertIsNone(txs[3].height)
        with self.assertRaises(TransactionWithoutBlob):
            txs[3].size
        self.assertEqual(txs[3].fee, decimal.Decimal('0.000320650000'))
        self.assertIsNone(txs[3].blob)

    @responses.activate
    def test_transactions_single(self):
        responses.add(responses.POST,
                      self.transactions_url,
                      json=self._read('test_transactions_single_pruned.json'),
                      status=200)

        tx = self.daemon.transactions(
            'bbc10f5944cc3e88be576d2ab9f4f5ab5a2b46d95a7cab1027bc15c17393102c'
        )[0]

        self.assertEqual(tx.height, 2279770)
        self.assertIsNone(tx.blob)

    @responses.activate
    def test_transaction_not_pruned(self):
        daemon_no_prune = Daemon(JSONRPCDaemon(prune_transactions=False))
        responses.add(responses.POST,
                      self.transactions_url,
                      json=self._read('test_transactions_single.json'),
                      status=200)

        tx = daemon_no_prune.transactions(
            'bbc10f5944cc3e88be576d2ab9f4f5ab5a2b46d95a7cab1027bc15c17393102c'
        )[0]
        self.assertIsNotNone(tx.blob)
        self.assertIs(type(tx.blob), bytes)

    @responses.activate
    def test_send_transaction(self):
        path = os.path.join(
            os.path.dirname(__file__), "data", self.data_subdir,
            "0e8fa9202e0773333360e5b9e8fb8e94272c16a8a58b6fe7cf3b4327158e3a44.tx"
        )
        responses.add(responses.POST,
                      self.sendrawtransaction_url,
                      json=self._read('test_send_transaction.json'),
                      status=200)

        with open(path, "rb") as blob_file:
            tx = Transaction(blob=blob_file.read())
            rsp = self.daemon.send_transaction(tx)
            self.assertEqual(rsp["status"], "OK")

    @responses.activate
    def test_chunking(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_chunking-10-block-693324.json'),
                      status=200)
        responses.add(
            responses.POST,
            self.transactions_url,
            json=self._read('test_chunking-20-get_transactions_1of2.json'),
            status=200)
        responses.add(
            responses.POST,
            self.transactions_url,
            json=self._read('test_chunking-20-get_transactions_2of2.json'),
            status=200)
        blk = self.daemon.block(height=693324)
        self.assertEqual(len(blk.transactions), 105)
        self.assertEqual(len(set(blk.transactions)), 105)

    @responses.activate
    def test_headers(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_headers_2279790_2279799.json'),
                      status=200)
        headers = self.daemon.headers(2279790, 2279799)
        self.assertEqual(len(headers), 10)
        self.assertEqual(
            headers[0]['hash'],
            '2763e0b9738c46317602a8e338b6b3ece893be4b9e1c4586824beb4f33286992')
        self.assertEqual(headers[9]['nonce'], 275623)

    @responses.activate
    def test_invalid_param(self):
        responses.add(responses.POST,
                      self.jsonrpc_url,
                      json=self._read('test_invalid_param.json'),
                      status=200)
        with self.assertRaises(RPCError):
            blk = self.daemon.block(height=-1)

    def test_init_default_backend(self):
        daemon1 = Daemon(host='localhost')
        daemon2 = Daemon()

        with self.assertRaises(ValueError):
            daemon3 = Daemon(backend=JSONRPCDaemon(), port=18089)
Ejemplo n.º 5
0
                        help="Request timeout")
argsparser.add_argument(
    "-v",
    dest="verbosity",
    action="count",
    default=0,
    help="Verbosity (repeat to increase; -v for INFO, -vv for DEBUG",
)
args = argsparser.parse_args()
level = logging.WARNING
if args.verbosity == 1:
    level = logging.INFO
elif args.verbosity > 1:
    level = logging.DEBUG
logging.basicConfig(level=level, format="%(asctime)-15s %(message)s")

d = Daemon(JSONRPCDaemon(timeout=args.timeout, **args.daemon_rpc_url))

txs = list(d.transactions(args.tx_id))
print("Found {:d} transaction(s)".format(len(txs)))
if len(txs) > 0:
    print("-" * 79)
for tx in txs:
    print("id: {:s}".format(tx.hash))
    print("height: {:s}".format(
        "None" if tx.height is None else "{:d}".format(tx.height)))
    print("size: {:d}".format(tx.size))
    print("JSON:")
    print(json.dumps(tx.json, indent=2))
    print("-" * 79)