Ejemplo n.º 1
0
def _init_database(
    data_dir: FilePath,
    password: str,
    msg_aggregator: MessagesAggregator,
    db_settings: Optional[Dict[str, Any]],
    ignored_assets: Optional[List[Asset]],
    blockchain_accounts: BlockchainAccounts,
) -> DBHandler:
    db = DBHandler(data_dir, password, msg_aggregator)
    settings = {
        # DO not submit usage analytics during tests
        'submit_usage_analytics': False,
        'main_currency': DEFAULT_TESTS_MAIN_CURRENCY,
    }
    # Set the given db_settings. The pre-set values have priority unless overriden here
    if db_settings is not None:
        for key, value in db_settings.items():
            settings[key] = value
    db.set_settings(ModifiableDBSettings(**settings))

    if ignored_assets:
        for asset in ignored_assets:
            db.add_to_ignored_assets(asset)

    # Make sure that the fixture provided accounts are in the blockchain
    db.add_blockchain_accounts(SupportedBlockchain.ETHEREUM,
                               blockchain_accounts.eth)
    db.add_blockchain_accounts(SupportedBlockchain.BITCOIN,
                               blockchain_accounts.btc)

    return db
Ejemplo n.º 2
0
def add_blockchain_accounts_to_db(db: DBHandler, blockchain_accounts: BlockchainAccounts) -> None:
    db.add_blockchain_accounts(
        SupportedBlockchain.ETHEREUM,
        [BlockchainAccountData(address=x) for x in blockchain_accounts.eth],
    )
    db.add_blockchain_accounts(
        SupportedBlockchain.BITCOIN,
        [BlockchainAccountData(address=x) for x in blockchain_accounts.btc],
    )
Ejemplo n.º 3
0
def add_blockchain_accounts_to_db(db: DBHandler, blockchain_accounts: BlockchainAccounts) -> None:
    try:
        db.add_blockchain_accounts(
            SupportedBlockchain.ETHEREUM,
            [BlockchainAccountData(address=x) for x in blockchain_accounts.eth],
        )
        db.add_blockchain_accounts(
            SupportedBlockchain.BITCOIN,
            [BlockchainAccountData(address=x) for x in blockchain_accounts.btc],
        )
    except InputError as e:
        raise AssertionError(
            f'Got error at test setup blockchain account addition: {str(e)} '
            f'Probably using two different databases or too many fixtures initialized. '
            f'For example do not initialize both a rotki api server and another DB at same time',
        ) from e
Ejemplo n.º 4
0
def _init_database(
    data_dir: FilePath,
    password: str,
    msg_aggregator: MessagesAggregator,
    db_settings: Optional[Dict[str, Any]],
    ignored_assets: Optional[List[Asset]],
    blockchain_accounts: BlockchainAccounts,
    include_etherscan_key: bool,
) -> DBHandler:
    db = DBHandler(data_dir, password, msg_aggregator)
    settings = {
        # DO not submit usage analytics during tests
        'submit_usage_analytics': False,
        'main_currency': DEFAULT_TESTS_MAIN_CURRENCY,
    }
    # Set the given db_settings. The pre-set values have priority unless overriden here
    if db_settings is not None:
        for key, value in db_settings.items():
            settings[key] = value
    db.set_settings(ModifiableDBSettings(**settings))

    if ignored_assets:
        for asset in ignored_assets:
            db.add_to_ignored_assets(asset)

    # Make sure that the fixture provided accounts are in the blockchain
    db.add_blockchain_accounts(
        SupportedBlockchain.ETHEREUM,
        [BlockchainAccountData(address=x) for x in blockchain_accounts.eth],
    )
    db.add_blockchain_accounts(
        SupportedBlockchain.BITCOIN,
        [BlockchainAccountData(address=x) for x in blockchain_accounts.btc],
    )
    if include_etherscan_key:
        # Add the tests only etherscan API key
        db.add_external_service_credentials([
            ExternalServiceApiCredentials(
                service=ExternalService.ETHERSCAN,
                api_key=ApiKey('8JT7WQBB2VQP5C3416Y8X3S8GBA3CVZKP4'),
            )
        ])

    return db
Ejemplo n.º 5
0
def setup_ethereum_transactions_test(
    database: DBHandler,
    transaction_already_queried: bool,
    one_receipt_in_db: bool = False,
) -> Tuple[List[EthereumTransaction], List[EthereumTxReceipt]]:
    dbethtx = DBEthTx(database)
    tx_hash1 = deserialize_evm_tx_hash(
        '0x692f9a6083e905bdeca4f0293f3473d7a287260547f8cbccc38c5cb01591fcda'
    )  # noqa: E501
    addr1 = string_to_ethereum_address(
        '0x443E1f9b1c866E54e914822B7d3d7165EdB6e9Ea')
    addr2 = string_to_ethereum_address(
        '0x442068F934BE670aDAb81242C87144a851d56d16')
    database.add_blockchain_accounts(
        blockchain=SupportedBlockchain.ETHEREUM,
        account_data=[
            BlockchainAccountData(address=addr1),
            BlockchainAccountData(address=addr2),
        ],
    )
    transaction1 = EthereumTransaction(
        tx_hash=tx_hash1,
        timestamp=Timestamp(1630532276),
        block_number=13142218,
        from_address=addr1,
        to_address=string_to_ethereum_address(
            '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'),
        value=int(10 * 10**18),
        gas=194928,
        gas_price=int(0.000000204 * 10**18),
        gas_used=136675,
        input_data=hexstring_to_bytes(
            '0x7ff36ab5000000000000000000000000000000000000000000000367469995d0723279510000000000000000000000000000000000000000000000000000000000000080000000000000000000000000443e1f9b1c866e54e914822b7d3d7165edb6e9ea00000000000000000000000000000000000000000000000000000000612ff9b50000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002a3bff78b79a009976eea096a51a948a3dc00e34'
        ),  # noqa: E501
        nonce=13,
    )
    tx_hash2 = deserialize_evm_tx_hash(
        '0x6beab9409a8f3bd11f82081e99e856466a7daf5f04cca173192f79e78ed53a77'
    )  # noqa: E501
    transaction2 = EthereumTransaction(
        tx_hash=tx_hash2,
        timestamp=Timestamp(1631013757),
        block_number=13178342,
        from_address=addr2,
        to_address=string_to_ethereum_address(
            '0xEaDD9B69F96140283F9fF75DA5FD33bcF54E6296'),
        value=0,
        gas=77373,
        gas_price=int(0.000000100314697497 * 10**18),
        gas_used=46782,
        input_data=hexstring_to_bytes(
            '0xa9059cbb00000000000000000000000020c8032d4f7d4a380385f87aeadf05bed84504cb000000000000000000000000000000000000000000000000000000003b9deec6'
        ),  # noqa: E501
        nonce=3,
    )
    transactions = [transaction1, transaction2]
    if transaction_already_queried is True:
        dbethtx.add_ethereum_transactions(ethereum_transactions=[transaction1],
                                          relevant_address=addr1)  # noqa: E501
        dbethtx.add_ethereum_transactions(ethereum_transactions=[transaction2],
                                          relevant_address=addr2)  # noqa: E501
        result = dbethtx.get_ethereum_transactions(
            ETHTransactionsFilterQuery.make(), True)
        assert result == transactions

    expected_receipt1 = EthereumTxReceipt(
        tx_hash=tx_hash1,
        contract_address=None,
        status=True,
        type=0,
        logs=[
            EthereumTxReceiptLog(
                log_index=295,
                data=hexstring_to_bytes(
                    '0x0000000000000000000000000000000000000000000000008ac7230489e80000'
                ),  # noqa: E501
                address=string_to_ethereum_address(
                    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
                removed=False,
                topics=[
                    hexstring_to_bytes(
                        '0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d'
                    ),  # noqa: E501
                ],
            ),
            EthereumTxReceiptLog(
                log_index=296,
                data=hexstring_to_bytes(
                    '0x0000000000000000000000000000000000000000000000008ac7230489e80000'
                ),  # noqa: E501
                address=string_to_ethereum_address(
                    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
                removed=False,
                topics=[
                    hexstring_to_bytes(
                        '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x000000000000000000000000caa004418eb42cdf00cb057b7c9e28f0ffd840a5'
                    ),  # noqa: E501
                ],
            ),
            EthereumTxReceiptLog(
                log_index=297,
                data=hexstring_to_bytes(
                    '0x00000000000000000000000000000000000000000000036ba1d53baeeda5ed20'
                ),  # noqa: E501
                address=string_to_ethereum_address(
                    '0x2a3bFF78B79A009976EeA096a51A948a3dC00e34'),
                removed=False,
                topics=[
                    hexstring_to_bytes(
                        '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x000000000000000000000000caa004418eb42cdf00cb057b7c9e28f0ffd840a5'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x000000000000000000000000443e1f9b1c866e54e914822b7d3d7165edb6e9ea'
                    ),  # noqa: E501
                ],
            ),
            EthereumTxReceiptLog(
                log_index=298,
                data=hexstring_to_bytes(
                    '0x000000000000000000000000000000000000000000007b6ea033189ba7d047e30000000000000000000000000000000000000000000000140bc8194dd0f5e4be'
                ),  # noqa: E501
                address=string_to_ethereum_address(
                    '0xcaA004418eB42cdf00cB057b7C9E28f0FfD840a5'),
                removed=False,
                topics=[
                    hexstring_to_bytes(
                        '0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1'
                    )
                ],  # noqa: E501
            ),
            EthereumTxReceiptLog(
                log_index=299,
                data=hexstring_to_bytes(
                    '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008ac7230489e8000000000000000000000000000000000000000000000000036ba1d53baeeda5ed200000000000000000000000000000000000000000000000000000000000000000'
                ),  # noqa: E501
                address=string_to_ethereum_address(
                    '0xcaA004418eB42cdf00cB057b7C9E28f0FfD840a5'),
                removed=False,
                topics=[
                    hexstring_to_bytes(
                        '0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x000000000000000000000000443e1f9b1c866e54e914822b7d3d7165edb6e9ea'
                    ),  # noqa: E501
                ],
            ),
        ],
    )
    expected_receipt2 = EthereumTxReceipt(
        tx_hash=tx_hash2,
        contract_address=None,
        status=True,
        type=2,
        logs=[
            EthereumTxReceiptLog(
                log_index=438,
                data=hexstring_to_bytes(
                    '0x000000000000000000000000000000000000000000000000000000003b9deec6'
                ),  # noqa: E501
                address=string_to_ethereum_address(
                    '0xEaDD9B69F96140283F9fF75DA5FD33bcF54E6296'),
                removed=False,
                topics=[
                    hexstring_to_bytes(
                        '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x000000000000000000000000442068f934be670adab81242c87144a851d56d16'
                    ),  # noqa: E501
                    hexstring_to_bytes(
                        '0x00000000000000000000000020c8032d4f7d4a380385f87aeadf05bed84504cb'
                    ),  # noqa: E501
                ],
            ),
        ],
    )

    if one_receipt_in_db:
        dbethtx.add_receipt_data(txreceipt_to_data(expected_receipt1))

    return transactions, [expected_receipt1, expected_receipt2]