def test_remove_incoming_transaction_from_db(patch_blockchain) -> None:
    """
    Setup:
        DB:
            1. inventory account with 1 incoming initial transaction of 1000 coins
            2. 2 user accounts with incoming transaction
        Blockchain:
            1. 1 inventory incoming transaction
            2. 1 user incoming transaction
    Action: sync_db() expected:
        1. Remove transaction with version 1 from LRW DB
    """
    setup_inventory_with_initial_transaction(
        patch_blockchain=patch_blockchain,
        initial_funds=1000,
        mocked_initial_balance=1100,
    )

    # should be removed during sync
    REMOVED_VERSION = 1
    add_incoming_transaction_to_db(
        receiver_sub_address=SUB_ADDRESS_1,
        amount=100,
        sender_address=OTHER_ADDRESS_1,
        sequence=1,
        version=REMOVED_VERSION,
        account_name="test_account",
    )

    NO_CHANGE_VERSION = 2
    setup_incoming_transaction(
        patch_blockchain=patch_blockchain,
        receiver_sub_address=SUB_ADDRESS_2,
        amount=75,
        sender_address=OTHER_ADDRESS_2,
        sequence=2,
        version=NO_CHANGE_VERSION,
        name="test_account_2",
    )

    check_balance(1175)

    check_number_of_transactions(3)

    sync_db()

    check_number_of_transactions(2)

    check_balance(1075)

    assert (
        Transaction.query.filter_by(blockchain_version=REMOVED_VERSION).first() is None
    )

    assert (
        Transaction.query.filter_by(blockchain_version=NO_CHANGE_VERSION).first()
        is not None
    )
def test_add_incoming_transaction_from_blockchain(patch_blockchain) -> None:
    """
    Setup:
        DB:
            1. inventory account with 1 incoming initial transaction of 1000 coins
            2. 1 user account with incoming transaction
        Blockchain:
            1. 1 inventory incoming transaction
            2. 2 user incoming transactions
    Action: sync_db() expected:
        1. Add transaction with version 2 into LRW DB
    """
    setup_inventory_with_initial_transaction(patch_blockchain,
                                             1000,
                                             mocked_initial_balance=1175)

    NO_CHANGE_VERSION = 6
    setup_incoming_transaction(
        patch_blockchain=patch_blockchain,
        receiver_sub_address=SUB_ADDRESS_1,
        amount=100,
        sender_address=OTHER_ADDRESS_1,
        sequence=6,
        version=NO_CHANGE_VERSION,
        name="test_account",
    )

    # should be added during sync
    ADDED_VERSION = 2
    add_incoming_transaction_to_blockchain(
        patch_blockchain=patch_blockchain,
        receiver_sub_address=SUB_ADDRESS_2,
        amount=75,
        sender_address=OTHER_ADDRESS_2,
        sequence=2,
        version=ADDED_VERSION,
    )

    check_balance(1100)

    check_number_of_transactions(2)

    sync_db()

    check_number_of_transactions(3)

    check_balance(1175)

    assert (Transaction.query.filter_by(
        blockchain_version=NO_CHANGE_VERSION).first() is not None)

    assert (Transaction.query.filter_by(
        blockchain_version=ADDED_VERSION).first() is not None)
Beispiel #3
0
def test_sync_multiple_transactions(patch_blockchain):
    """
    Setup:
        DB:
            1. inventory account with 1 incoming initial transaction of 1000 coins
            2. 3 users accounts with incoming transaction
            3. 1 user account with outgoing transaction
        Blockchain:
            1. 1 inventory incoming transaction
            2. 2 users incoming transactions
            3. 2 users outgoing transactions
    Action: sync_db() expected:
        1. Add transaction with version 4 into LRW DB
        2. Remove transaction with version 5 from LRW DB
    """
    setup_inventory_with_initial_transaction(
        patch_blockchain, 1000, mock_blockchain_initial_balance=1000)

    NO_CHANGE_VERSION_1 = 1
    setup_incoming_transaction(
        patch_blockchain=patch_blockchain,
        receiver_sub_address=SUB_ADDRESS_1,
        amount=100,
        sender_address=OTHER_ADDRESS_1,
        sequence=1,
        version=NO_CHANGE_VERSION_1,
        name="test_account",
    )

    NO_CHANGE_VERSION_2 = 2
    setup_outgoing_transaction(
        patch_blockchain=patch_blockchain,
        sender_sub_address=SUB_ADDRESS_2,
        amount=75,
        receiver_address=OTHER_ADDRESS_2,
        sequence=2,
        version=NO_CHANGE_VERSION_2,
        name="test_account_2",
    )

    NO_CHANGE_VERSION_3 = 3
    setup_incoming_transaction(
        patch_blockchain=patch_blockchain,
        receiver_sub_address=SUB_ADDRESS_3,
        amount=50,
        sender_address=OTHER_ADDRESS_3,
        sequence=3,
        version=NO_CHANGE_VERSION_3,
        name="test_account_3",
    )

    # should be removed during sync
    REMOVED_VERSION = 5
    add_incoming_transaction_to_db(
        receiver_sub_address=SUB_ADDRESS_4,
        amount=25,
        sender_address=OTHER_ADDRESS_4,
        sequence=5,
        version=REMOVED_VERSION,
        account_name="test_account_4",
    )

    # should be added during sync
    ADDED_VERSION = 4
    add_outgoing_transaction_to_blockchain(patch_blockchain, SUB_ADDRESS_5,
                                           200, OTHER_ADDRESS_5, 4,
                                           ADDED_VERSION)

    check_balance(1100)

    check_number_of_transactions(5)

    sync_db()

    check_number_of_transactions(5)

    check_balance(875)

    assert (Transaction.query.filter_by(
        blockchain_version=NO_CHANGE_VERSION_1).first() is not None)

    assert (Transaction.query.filter_by(
        blockchain_version=NO_CHANGE_VERSION_2).first() is not None)

    assert (Transaction.query.filter_by(
        blockchain_version=NO_CHANGE_VERSION_3).first() is not None)

    assert (Transaction.query.filter_by(
        blockchain_version=ADDED_VERSION).first() is not None)

    assert (Transaction.query.filter_by(
        blockchain_version=REMOVED_VERSION).first() is None)
def test_sync_until_specific_version(patch_blockchain):
    """
    Setup:
        DB:
            1. inventory account with 1 incoming initial transaction of 1000 coins
            2. 2 users accounts with outgoing transaction
            3. 1 user account with incoming transaction --> highest version - 10139
        Blockchain:
            1. 1 inventory incoming transaction
            2. 2 users incoming transactions --> 1 transaction with higher version from DB highest version (10151)
            3. 3 users outgoing transactions --> 1 transaction with lower version from DB highest version (10132)
    Action: sync_db() expected:
        1. Add transaction with version 4 into LRW DB
        2. Remove transaction with version 5 from LRW DB
    """
    setup_inventory_with_initial_transaction(
        patch_blockchain, 1000, mocked_initial_balance=880
    )

    NO_CHANGE_VERSION_1 = 10131
    setup_outgoing_transaction(
        patch_blockchain=patch_blockchain,
        sender_sub_address=SUB_ADDRESS_1,
        amount=100,
        receiver_address=OTHER_ADDRESS_1,
        sequence=0,
        version=NO_CHANGE_VERSION_1,
        name="user_test",
    )

    NO_CHANGE_VERSION_2 = 10137
    setup_outgoing_transaction(
        patch_blockchain=patch_blockchain,
        sender_sub_address=SUB_ADDRESS_2,
        amount=75,
        receiver_address=OTHER_ADDRESS_2,
        sequence=1,
        version=NO_CHANGE_VERSION_2,
        name="user_test_2",
    )

    HIGHEST_VERSION_IN_DB = 10139
    setup_incoming_transaction(
        patch_blockchain=patch_blockchain,
        receiver_sub_address=SUB_ADDRESS_3,
        amount=80,
        sender_address=OTHER_ADDRESS_3,
        sequence=12,
        version=HIGHEST_VERSION_IN_DB,
        name="test_account_3",
    )

    HIGHER_THAN_DB_HIHEST_VERSION = 10151
    add_incoming_transaction_to_blockchain(
        patch_blockchain,
        SUB_ADDRESS_4,
        25,
        OTHER_ADDRESS_4,
        7,
        HIGHER_THAN_DB_HIHEST_VERSION,
    )
    ADDED_VERSION = 10132
    add_outgoing_transaction_to_blockchain(
        patch_blockchain,
        SUB_ADDRESS_5,
        50,
        OTHER_ADDRESS_5,
        2,
        ADDED_VERSION,
    )

    check_balance(905)

    check_number_of_transactions(4)

    sync_db()

    check_number_of_transactions(5)

    check_balance(855)

    assert (
        Transaction.query.filter_by(blockchain_version=NO_CHANGE_VERSION_1).first()
        is not None
    )

    assert (
        Transaction.query.filter_by(blockchain_version=NO_CHANGE_VERSION_2).first()
        is not None
    )

    assert (
        Transaction.query.filter_by(blockchain_version=HIGHEST_VERSION_IN_DB).first()
        is not None
    )

    assert (
        Transaction.query.filter_by(blockchain_version=ADDED_VERSION).first()
        is not None
    )

    assert (
        Transaction.query.filter_by(
            blockchain_version=HIGHER_THAN_DB_HIHEST_VERSION
        ).first()
        is None
    )