def test_tracking(self):
        address1 = create_unique_address(get_exchange_account_id())
        address2 = create_unique_address("lykke-test")
        addr2s = split_unique_address(address2)

        self.storage.track_address(address1)

        self.assertRaises(AddressAlreadyTrackedException,
                          self.storage.track_address, address1)

        self.storage.track_address(address2)

        filled_operation = self.get_completed_op()
        filled_operation["to"] = addr2s["account_id"]
        filled_operation["customer_id"] = addr2s["customer_id"]
        filled_operation["amount_asset_id"] = "1.3.4123"
        filled_operation["amount_value"] = 1234
        self.storage.insert_operation(filled_operation)

        balances = self.storage.get_balances(2)

        assert address1 not in balances.keys()
        assert address2 in balances.keys()
        assert balances[address2]["1.3.4123"] == 1234

        self.storage.untrack_address(address1)

        self.assertRaises(AddressNotTrackedException,
                          self.storage.untrack_address, address1)
    def test_validate_address(self):
        assert implementations.validate_address(create_unique_address('xeroc')) ==\
            {'isValid': True}

        assert implementations.validate_address('xeroc') ==\
            {'isValid': True}

        assert implementations.validate_address(create_unique_address('xeroc', '')) ==\
            {'isValid': True}

        assert implementations.validate_address(create_unique_address('this_account_will_never_exist')) ==\
            {'isValid': False}

        assert implementations.validate_address(create_unique_address('!@$%25^&*(')) ==\
            {'isValid': False}
示例#3
0
    def test_create_unique_address(self):
        unique_wallet = create_unique_address(
            utils.get_exchange_account_id(),
            lambda: "9ed8c906-d6ab-4fb8-a314-0516a175717a")

        assert unique_wallet == utils.get_exchange_account_name(
        ) + ":9ed8c906-d6ab-4fb8-a314-0516a175717a"
class ATestOperationStorage(ATestnetTest):

    TEST_OP = {
        'block_num':
        23645414,
        'transaction_id':
        23,
        "op_in_tx":
        2,
        "tx_in_block":
        0,
        'op': [
            'transfer', {
                "fee": {
                    "amount": 1759,
                    "asset_id": "1.3.0"
                },
                "from": "1.2.20407",
                "to": "1.2.20477",
                "amount": {
                    "amount": 50000000,
                    "asset_id": "1.3.121"
                },
                "memo": {
                    "from":
                    "BTS6A9Qsp2FrrCWNndZmWqbeLq6g7Lr1T8Y9sQ5RbqutoeW5nBTAU",
                    "to":
                    "BTS5HDNjR25RHQAJ2Esnt6Yz1tgyr4fdwJEXv5r8wx5ynLSkDsyMQ",
                    "nonce":
                    "9243173171060559811",
                    "message":
                    "98af198ef4d58fc7608dee97c970dd2d684ce7ab03617123f702c156659f5eb2"
                },
                "extensions": []
            }
        ],
        "decoded_memo":
        create_unique_address("some_customer_id", "some_operation_id"),
        'expiration':
        "2018-01-12T08:25:29"
    }

    def get_in_progress_op(self):
        in_progress_op = decode_operation(self.TEST_OP.copy())

        in_progress_op.pop("block_num")

        return in_progress_op

    def get_completed_op(self):
        return decode_operation(self.TEST_OP.copy())

    def __init__(self, *args, **kwargs):
        super(ATestOperationStorage, self).__init__(*args, **kwargs)

    def setUp(self):
        super(ATestOperationStorage, self).setUp()

        self.storage = get_operation_storage("mongodbtest")
    def test_observe_address(self):
        address = create_unique_address("some_id", "user_name_bla")

        self.assertRaises(AccountDoesNotExistsException,
                          implementations.observe_address, address)

        address = create_unique_address(utils.get_exchange_account_id(),
                                        "user_name_bla")

        implementations.observe_address(address)

        self.assertRaises(AddressAlreadyTrackedException,
                          implementations.observe_address, address)

        implementations.unobserve_address(address)
        self.assertRaises(AddressNotTrackedException,
                          implementations.unobserve_address, address)
示例#6
0
    def test_split_address(self):
        unique_wallet = create_unique_address(
            utils.get_exchange_account_id(),
            lambda: "9ed8c906-d6ab-4fb8-a314-0516a175717a")

        address = split_unique_address(unique_wallet)

        assert address["account_id"] == utils.get_exchange_account_id()
    def test_get_balance(self):
        address = create_unique_address(get_exchange_account_id())
        addrs = split_unique_address(address)
        asset = "1.3.131"

        self.storage.track_address(address)

        filled_operation = self.get_completed_op()
        filled_operation["to"] = addrs["account_id"]
        filled_operation["amount_asset_id"] = asset
        filled_operation["customer_id"] = addrs["customer_id"]

        filled_operation["incident_id"] = "some_operation_id_1"
        filled_operation["chain_identifier"] = "some_chain_identifier_1"
        filled_operation["amount_value"] = 10
        self.storage.insert_operation(filled_operation)

        filled_operation["incident_id"] = "some_operation_id_2"
        filled_operation["chain_identifier"] = "some_chain_identifier_2"
        filled_operation["amount_value"] = 20
        self.storage.insert_operation(filled_operation)

        filled_operation["incident_id"] = "some_operation_id_3"
        filled_operation["chain_identifier"] = "some_chain_identifier_3"
        filled_operation["amount_value"] = -5
        self.storage.insert_operation(filled_operation)

        filled_operation["from"] = addrs["account_id"]
        filled_operation["to"] = get_exchange_account_id()

        filled_operation["incident_id"] = "some_operation_id_4"
        filled_operation["chain_identifier"] = "some_chain_identifier_4"
        filled_operation["amount_value"] = 7
        self.storage.insert_operation(filled_operation)

        balances = self.storage.get_balances(2)

        assert balances[address][asset] == 18

        self.assertRaises(InputInvalidException, self.storage.get_balances, 2,
                          "§=)$")

        self.assertRaises(InputInvalidException, self.storage.get_balances, 2,
                          "069548")
示例#8
0
    def test_history(self):
        address = create_unique_address(self.get_customer_id())
        self.client.post(url_for('Blockchain.Api.observe_address_history_from', address=address))
        self.invalidate(url_for('Blockchain.Api.observe_address_history_from', address=address),
                        409,
                        method=self.client.post,
                        response_json_contains={"errorCode": "unknown"})

        self.client.delete(url_for('Blockchain.Api.unobserve_address_history_from', address=address))
        self.invalidate(url_for('Blockchain.Api.unobserve_address_history_from', address=address),
                        204,
                        method=self.client.delete)

        self.client.post(url_for('Blockchain.Api.observe_address_history_to', address=address))
        self.invalidate(url_for('Blockchain.Api.observe_address_history_to', address=address),
                        409,
                        method=self.client.post,
                        response_json_contains={"errorCode": "unknown"})

        self.client.delete(url_for('Blockchain.Api.unobserve_address_history_to', address=address))
        self.invalidate(url_for('Blockchain.Api.unobserve_address_history_to', address=address),
                        204,
                        method=self.client.delete)
示例#9
0
    def test_track_balance(self, bitshares_instance=None):
        store = factory.get_operation_storage(purge=True)

        addressDW = self.client.post(
            url_for('Blockchain.SignService.wallets')).json["publicAddress"]

        addressEW = create_unique_address(self.get_customer_id(), "")

        addressHW = self.client.post(
            url_for('Blockchain.SignService.wallets')).json["publicAddress"]

        #         customer_id = split_unique_address(addressDW)["customer_id"]

        response = self.client.get(
            url_for('Blockchain.Api.address_validity', address=addressDW))
        assert response.status_code == 200

        response = self.client.post(
            url_for('Blockchain.Api.observe_address', address=addressDW))
        assert response.status_code == 200

        response = self.client.post(
            url_for('Blockchain.Api.observe_address', address=addressHW))
        assert response.status_code == 200

        def build_sign_and_broadcast(op, memo_key, active_key):
            op["fromAddressContext"] = memo_key
            transaction = self.client.post(
                url_for('Blockchain.Api.build_transaction'),
                data=json.dumps(op))

            assert transaction.status_code == 200

            sign_transaction = {
                "transactionContext": transaction.json["transactionContext"],
                "privateKeys": [active_key]
            }
            signed_transaction = self.client.post(
                url_for('Blockchain.SignService.sign'),
                data=json.dumps(sign_transaction))
            broadcast_transaction = {
                "operationId": op["operationId"],
                "signedTransaction":
                signed_transaction.json["signedTransaction"]
            }
            broadcast = self.client.post(
                url_for('Blockchain.Api.broadcast_transaction'),
                data=json.dumps(broadcast_transaction))
            assert broadcast.status_code == 200

            print(broadcast_transaction)

            return broadcast.json["block"]

        def flag_completed(block_num):
            network = Config.get("network_type")
            connection = Config.get("bitshares", "connection", network)
            #             connection["keys"] = key
            instance = BitShares(**connection)

            irr = instance.rpc.get_dynamic_global_properties().get(
                "last_irreversible_block_num")
            head = instance.rpc.get_dynamic_global_properties().get(
                "head_block_number")

            print("Blockchain Monitor: Looking for block " + str(block_num),
                  ", current head=" + str(head) + ", irreversible=" + str(irr))

            monitor = BlockchainMonitor(storage=store,
                                        bitshares_instance=instance)
            monitor.start_block = block_num - 1
            monitor.stop_block = block_num + 1
            monitor.listen()

        block_num = build_sign_and_broadcast(
            {
                "operationId": "cbeea30e-2218-4405-9089-86d003e4df60",
                "fromAddress": addressEW,
                "toAddress": addressDW,
                "assetId": "1.3.0",
                "amount": 110000,
                "includeFee": False
            }, self.get_customer_memo_key(), self.get_customer_active_key())
        flag_completed(block_num)

        response = self.client.get(
            url_for('Blockchain.Api.get_balances') + "?take=2")
        assert response.status_code == 200
        self.assertEqual(response.json["items"], [{
            'address': addressDW,
            'assetId': '1.3.0',
            'balance': 110000,
            'block': block_num * 10
        }])

        block_num = build_sign_and_broadcast(
            {
                "operationId": "cbeea30e-2218-4405-9089-86d003e4df61",
                "fromAddress": addressDW,
                "toAddress": addressHW,
                "assetId": "1.3.0",
                "amount": 100000,
                "includeFee": True
            }, utils.get_exchange_memo_key(), utils.get_exchange_active_key())

        response = self.client.get(
            url_for('Blockchain.Api.get_balances') + "?take=2")
        assert response.status_code == 200
        self.assertEqual(response.json["items"][0]["balance"], 10000)
        assert block_num > 0

        balance_block_num = block_num

        response = self.client.get(
            url_for('Blockchain.Api.get_broadcasted_transaction',
                    operationId="cbeea30e-2218-4405-9089-86d003e4df61"))
        self.assertEqual(response.json["block"], block_num * 10)

        WW = create_unique_address(self.get_customer_id(), "some_user_memo")

        block_num = build_sign_and_broadcast(
            {
                "operationId": "cbeea30e-2218-4405-9089-86d003e4df62",
                "fromAddress": addressHW,
                "toAddress": WW,
                "assetId": "1.3.0",
                "amount": 100000,
                "includeFee": True
            }, utils.get_exchange_memo_key(), utils.get_exchange_active_key())
        flag_completed(block_num)

        response = self.client.get(
            url_for('Blockchain.Api.get_balances') + "?take=2")
        assert response.status_code == 200
        self.assertEqual(response.json["items"],
                         [{
                             'address': addressDW,
                             'assetId': '1.3.0',
                             'balance': 10000,
                             'block': balance_block_num * 10
                         }])

        self.maxDiff = None

        toDW = self.client.get(
            url_for('Blockchain.Api.get_address_history_to', address=addressDW)
            + "?take=3")
        assert toDW.status_code == 200
        self.assertEqual(toDW.json, [{
            'amount': '110000',
            'assetId': '1.3.0',
            'fromAddress': addressEW,
            'hash': toDW.json[0]['hash'],
            'timestamp': toDW.json[0]['timestamp'],
            'toAddress': addressDW
        }])

        fromDW = self.client.get(
            url_for('Blockchain.Api.get_address_history_from',
                    address=addressDW) + "?take=3")
        assert fromDW.status_code == 200
        self.assertEqual(fromDW.json, [{
            'amount': '100000',
            'assetId': '1.3.0',
            'fromAddress': addressDW,
            'hash': fromDW.json[0]['hash'],
            'timestamp': fromDW.json[0]['timestamp'],
            'toAddress': 'lykke-test'
        }])

        toHW = self.client.get(
            url_for('Blockchain.Api.get_address_history_to', address=addressHW)
            + "?take=3")
        assert toHW.status_code == 200
        assert toHW.json == []

        fromHW = self.client.get(
            url_for('Blockchain.Api.get_address_history_from',
                    address=split_unique_address(addressHW)["account_id"]) +
            "?take=3")
        assert fromHW.status_code == 200
        self.assertEqual(fromHW.json,
                         [{
                             'amount': '99900',
                             'assetId': '1.3.0',
                             'fromAddress': get_exchange_account_name(),
                             'hash': fromHW.json[0]['hash'],
                             'timestamp': fromHW.json[0]['timestamp'],
                             'toAddress': WW
                         }])

        response = self.client.get(
            url_for('Blockchain.Api.get_broadcasted_transaction',
                    operationId="cbeea30e-2218-4405-9089-86d003e4df60"))
        assert response.status_code == 200
        self.assertEqual(response.json['operationId'],
                         'cbeea30e-2218-4405-9089-86d003e4df60')

        response = self.client.get(
            url_for('Blockchain.Api.get_broadcasted_transaction',
                    operationId="cbeea30e-2218-4405-9089-86d003e4df61"))
        assert response.status_code == 200
        self.assertEqual(response.json['operationId'],
                         'cbeea30e-2218-4405-9089-86d003e4df61')

        response = self.client.get(
            url_for('Blockchain.Api.get_broadcasted_transaction',
                    operationId="cbeea30e-2218-4405-9089-86d003e4df62"))
        assert response.status_code == 200
        self.assertEqual(response.json['operationId'],
                         'cbeea30e-2218-4405-9089-86d003e4df62')

        response = self.client.delete(
            url_for('Blockchain.Api.unobserve_address', address=addressDW))
        assert response.status_code == 200

        response = self.client.delete(
            url_for('Blockchain.Api.unobserve_address', address=addressHW))
        assert response.status_code == 200

        response = self.client.get(
            url_for('Blockchain.Api.get_balances') + "?take=2")
        assert response.status_code == 200
        self.assertEqual(response.json["items"], [])