diff --git a/src/wallet/wallet_puzzle_store.py b/src/wallet/wallet_puzzle_store.py index efa6e87b5f..e020efc09e 100644 --- a/src/wallet/wallet_puzzle_store.py +++ b/src/wallet/wallet_puzzle_store.py @@ -9,11 +9,10 @@ from src.wallet.util.wallet_types import WalletType class WalletPuzzleStore: """ - This object handles CoinRecords in DB used by wallet. + WalletPuzzleStore keeps track of all generated puzzle_hashes and their derivation path / wallet. """ db_connection: aiosqlite.Connection - # Whether or not we are syncing lock: asyncio.Lock cache_size: uint32 @@ -61,6 +60,7 @@ class WalletPuzzleStore: await self.db_connection.close() async def _init_cache(self): + # TODO create cache print("init cache here") async def _clear_database(self): @@ -71,6 +71,10 @@ class WalletPuzzleStore: async def add_derivation_path_of_interest( self, index: int, puzzlehash: bytes32, pubkey: bytes, wallet_type: WalletType ): + """ + Inserts new derivation path, puzzle, pubkey, wallet into DB. + """ + cursor = await self.db_connection.execute( "INSERT OR REPLACE INTO derivation_paths VALUES(?, ?, ?, ?, ?)", (index, pubkey.hex(), puzzlehash.hex(), wallet_type.value, 0), @@ -80,6 +84,10 @@ class WalletPuzzleStore: await self.db_connection.commit() async def puzzle_hash_exists(self, puzzle_hash: bytes32) -> bool: + """ + Checks if passed puzzle_hash is present in the db. + """ + cursor = await self.db_connection.execute( "SELECT * from derivation_paths WHERE puzzle_hash=?", (puzzle_hash.hex(),) ) @@ -92,6 +100,11 @@ class WalletPuzzleStore: return False async def index_for_pubkey(self, pubkey: str) -> int: + """ + Returns derivation path for the given pubkey. + Returns -1 if not present. + """ + cursor = await self.db_connection.execute( "SELECT * from derivation_paths WHERE pubkey=?", (pubkey,) ) @@ -103,9 +116,14 @@ class WalletPuzzleStore: return -1 - async def index_for_puzzle_hash(self, pubkey: bytes32) -> int: + async def index_for_puzzle_hash(self, puzzle_hash: bytes32) -> int: + """ + Returns the derivation path for the puzzle_hash. + Returns -1 if not present. + """ + cursor = await self.db_connection.execute( - "SELECT * from derivation_paths WHERE puzzle_hash=?", (pubkey.hex(),) + "SELECT * from derivation_paths WHERE puzzle_hash=?", (puzzle_hash.hex(),) ) row = await cursor.fetchone() await cursor.close() @@ -116,7 +134,10 @@ class WalletPuzzleStore: return -1 async def get_all_puzzle_hashes(self) -> Set[bytes32]: - """ Return a set containing all puzzle_hashes we generated. """ + """ + Return a set containing all puzzle_hashes we generated. + """ + cursor = await self.db_connection.execute("SELECT * from derivation_paths") rows = await cursor.fetchall() await cursor.close() @@ -128,6 +149,10 @@ class WalletPuzzleStore: return result async def get_max_derivation_path(self): + """ + Returns the highest derivation path currently stored. + """ + cursor = await self.db_connection.execute( "SELECT MAX(id) FROM derivation_paths;" ) diff --git a/src/wallet/wallet_transaction_store.py b/src/wallet/wallet_transaction_store.py index 9bee8ede3e..9675f98328 100644 --- a/src/wallet/wallet_transaction_store.py +++ b/src/wallet/wallet_transaction_store.py @@ -9,12 +9,10 @@ from src.wallet.transaction_record import TransactionRecord class WalletTransactionStore: """ - This object handles CoinRecords in DB used by wallet. + WalletTransactionStore stores transaction history for the wallet. """ db_connection: aiosqlite.Connection - # Whether or not we are syncing - sync_mode: bool = False lock: asyncio.Lock cache_size: uint32 tx_record_cache: Dict[bytes32, TransactionRecord] @@ -88,8 +86,11 @@ class WalletTransactionStore: await cursor.close() await self.db_connection.commit() - # Store TransactionRecord in DB and Cache async def add_transaction_record(self, record: TransactionRecord) -> None: + """ + Store TransactionRecord in DB and Cache. + """ + cursor = await self.db_connection.execute( "INSERT OR REPLACE INTO transaction_record VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ( @@ -113,8 +114,11 @@ class WalletTransactionStore: first_in = list(self.tx_record_cache.keys())[0] self.tx_record_cache.pop(first_in) - # Update transaction_record to be confirmed in DB async def set_confirmed(self, id: bytes32, index: uint32): + """ + Updates transaction to be confirmed. + """ + current: Optional[TransactionRecord] = await self.get_transaction_record(id) if current is None: return @@ -133,8 +137,11 @@ class WalletTransactionStore: ) await self.add_transaction_record(tx) - # Update transaction_record to be sent in DB async def set_sent(self, id: bytes32): + """ + Updates transaction to be sent. (Full Node has received spend_bundle and sent ack). + """ + current: Optional[TransactionRecord] = await self.get_transaction_record(id) if current is None: return @@ -153,8 +160,11 @@ class WalletTransactionStore: ) await self.add_transaction_record(tx) - # Checks DB and cache for TransactionRecord with id: id and returns it async def get_transaction_record(self, id: bytes32) -> Optional[TransactionRecord]: + """ + Checks DB and cache for TransactionRecord with id: id and returns it. + """ + if id.hex() in self.tx_record_cache: return self.tx_record_cache[id.hex()] cursor = await self.db_connection.execute( @@ -168,6 +178,10 @@ class WalletTransactionStore: return None async def get_not_sent(self) -> List[TransactionRecord]: + """ + Returns the list of transaction that have not been received by full node yet. + """ + cursor = await self.db_connection.execute( "SELECT * from transaction_record WHERE sent=?", (0,) ) @@ -181,12 +195,17 @@ class WalletTransactionStore: return records async def get_not_confirmed(self) -> List[TransactionRecord]: + """ + Returns the list of transaction that have not yet been confirmed. + """ + cursor = await self.db_connection.execute( "SELECT * from transaction_record WHERE confirmed=?", (0,) ) rows = await cursor.fetchall() await cursor.close() records = [] + for row in rows: record = TransactionRecord.from_bytes(row[0]) records.append(record) @@ -194,10 +213,15 @@ class WalletTransactionStore: return records async def get_all_transactions(self) -> List[TransactionRecord]: + """ + Returns all stored transactions. + """ + cursor = await self.db_connection.execute("SELECT * from transaction_record") rows = await cursor.fetchall() await cursor.close() records = [] + for row in rows: record = TransactionRecord.from_bytes(row[0]) records.append(record)