def run(): while True: try: sync_db() except Exception: logging.getLogger("sync-db").exception("sync db failed") time.sleep(60)
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_outgoing_transaction_from_blockchain(patch_blockchain) -> None: """ Setup: DB: 1. inventory account with 1 incoming initial transaction of 1000 coins 2. 1 user account with outgoing transaction Blockchain: 1. 1 inventory incoming transaction 2. 2 user outgoing 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=825) NO_CHANGE_VERSION = 5 setup_outgoing_transaction( patch_blockchain=patch_blockchain, sender_sub_address=SUB_ADDRESS_1, amount=100, receiver_address=OTHER_ADDRESS_1, sequence=5, version=NO_CHANGE_VERSION, name="user_test", ) # should be added during sync ADDED_VERSION = 2 add_outgoing_transaction_to_blockchain( patch_blockchain=patch_blockchain, sender_sub_address=SUB_ADDRESS_2, amount=75, receiver_address=OTHER_ADDRESS_2, sequence=2, version=ADDED_VERSION, ) check_balance(900) check_number_of_transactions(2) sync_db() check_number_of_transactions(3) check_balance(825) 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)
def test_sync_with_db_with_orphan_tx(patch_blockchain): """ Setup: DB: 1. inventory account with no transactions 2. there is one outgoing transaction that sill has no blockchain version and no sequence Blockchain: 1. 1 inventory incoming transaction 2. 1 user incoming transaction 3. 1 user outgoing transaction Action: sync_db() expected: 1. Add transactions to DB """ add_inventory_account_to_db() mock_account(patch_blockchain, mocked_balance_value=45) add_outgoing_transaction_to_db( sender_sub_address=SUB_ADDRESS_2, amount=1, receiver_address=OTHER_ADDRESS_2, sequence=None, version=None, account_name="test_account_2", ) add_outgoing_transaction_to_blockchain( patch_blockchain=patch_blockchain, sender_sub_address=SUB_ADDRESS_2, amount=75, receiver_address=OTHER_ADDRESS_2, sequence=1, version=0, ) add_incoming_transaction_to_blockchain( patch_blockchain=patch_blockchain, receiver_sub_address=SUB_ADDRESS_1, amount=120, sender_address=OTHER_ADDRESS_1, sequence=2, version=7, ) check_balance(-1) check_number_of_transactions(1) sync_db() check_number_of_transactions(2) check_balance(45)
def test_sync_when_no_data_in_blockchain(patch_blockchain): """ Setup: DB: 1. inventory account with 1 incoming initial transaction of 1000 coins 2. 1 user accounts with incoming transaction 3. 1 user account with outgoing transaction Blockchain: EMPTY Action: sync_db() expected: 2. Remove transactions from DB """ add_inventory_account_to_db() mock_account(patch_blockchain, mocked_balance_value=45) add_incoming_transaction_to_db( receiver_sub_address=SUB_ADDRESS_1, amount=120, sender_address=OTHER_ADDRESS_1, sequence=1, version=1, account_name="test_account_1", ) add_outgoing_transaction_to_db( sender_sub_address=SUB_ADDRESS_2, amount=35, receiver_address=OTHER_ADDRESS_2, sequence=1, version=5, account_name="test_account_2", ) check_balance(85) check_number_of_transactions(2) sync_db() check_number_of_transactions(0) check_balance(0)
def test_sync_with_empty_db(patch_blockchain): """ Setup: DB: 1. inventory account with no transactions Blockchain: 1. 1 inventory incoming transaction 2. 1 user incoming transaction 3. 1 user outgoing transaction Action: sync_db() expected: 1. Add transactions to DB """ add_inventory_account_to_db() mock_account(patch_blockchain, mocked_balance_value=45) add_outgoing_transaction_to_blockchain( patch_blockchain=patch_blockchain, sender_sub_address=SUB_ADDRESS_2, amount=75, receiver_address=OTHER_ADDRESS_2, sequence=1, version=0, ) add_incoming_transaction_to_blockchain( patch_blockchain=patch_blockchain, receiver_sub_address=SUB_ADDRESS_1, amount=120, sender_address=OTHER_ADDRESS_1, sequence=2, version=7, ) check_balance(0) check_number_of_transactions(0) sync_db() check_number_of_transactions(2) check_balance(45)
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 )