diff --git a/src/wallet/wallet_puzzle_store.py b/src/wallet/wallet_puzzle_store.py index 119ae6784c72..79feb2e456b5 100644 --- a/src/wallet/wallet_puzzle_store.py +++ b/src/wallet/wallet_puzzle_store.py @@ -39,8 +39,7 @@ async def create(cls, connection: aiosqlite.Connection, cache_size: uint32 = uin " puzzle_hash text PRIMARY_KEY," " wallet_type int," " wallet_id int," - " used tinyint," - " current tinyint)" + " used tinyint)" ) ) await self.db_connection.execute( @@ -80,14 +79,6 @@ async def add_derivation_paths(self, records: List[DerivationRecord]) -> None: """ sql_records = [] for record in records: - last: Optional[DerivationRecord] = await self.get_current_derivation_record_for_wallet(record.wallet_id) - if last is None: - current = 1 - elif record.index >= last.index: - current = 1 - await self.update_not_current(last.index, last.wallet_id) - else: - current = 0 self.all_puzzle_hashes.add(record.puzzle_hash) sql_records.append( ( @@ -97,12 +88,11 @@ async def add_derivation_paths(self, records: List[DerivationRecord]) -> None: record.wallet_type, record.wallet_id, 0, - current, ), ) cursor = await self.db_connection.executemany( - "INSERT OR REPLACE INTO derivation_paths VALUES(?, ?, ?, ?, ?, ?, ?)", + "INSERT OR REPLACE INTO derivation_paths VALUES(?, ?, ?, ?, ?, ?)", sql_records, ) @@ -167,17 +157,6 @@ async def set_used_up_to(self, index: uint32) -> None: await cursor.close() await self.db_connection.commit() - async def update_not_current(self, index: uint32, wallet_id: uint32) -> None: - """ - Sets a derivation path to used so we don't use it again. - """ - cursor = await self.db_connection.execute( - "UPDATE derivation_paths SET current=0 WHERE derivation_index<=? and wallet_id=?", - (index, wallet_id), - ) - await cursor.close() - 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. @@ -319,25 +298,20 @@ async def get_last_derivation_path_for_wallet(self, wallet_id: int) -> Optional[ return None - async def get_current_derivation_record_for_wallet(self, wallet_id: int) -> Optional[DerivationRecord]: + async def get_current_derivation_record_for_wallet(self, wallet_id: uint32) -> Optional[DerivationRecord]: """ Returns the current derivation record by derivation_index. """ cursor = await self.db_connection.execute( - f"SELECT * FROM derivation_paths WHERE wallet_id={wallet_id} and current=1;" + f"SELECT MAX(derivation_index) FROM derivation_paths WHERE wallet_id={wallet_id} and used=1;" ) row = await cursor.fetchone() await cursor.close() if row is not None and row[0] is not None: - return DerivationRecord( - row[0], - bytes.fromhex(row[2]), - G1Element.from_bytes(bytes.fromhex(row[1])), - row[3], - row[4], - ) + index = uint32(row[0]) + return await self.get_derivation_record(index, wallet_id) return None