diff --git a/aleo_types/vm_basic.py b/aleo_types/vm_basic.py index 59e6fa49..01fad218 100644 --- a/aleo_types/vm_basic.py +++ b/aleo_types/vm_basic.py @@ -13,7 +13,8 @@ class AleoID(AleoIDProtocol): def __init__(self, data: bytes): if len(self._prefix) != 2: - raise ValueError("locator_prefix must be 2 bytes") + if self._prefix != "solution": + raise ValueError("locator_prefix must be 2 bytes") self._data = data self._bech32m = Bech32m(data, self._prefix) @@ -100,6 +101,8 @@ class TransactionID(AleoID): class TransitionID(AleoID): _prefix = "au" +class SolutionID(AleoID): + _prefix = "solution" ## Saved for reference # class RecordCiphertext(AleoObject): diff --git a/db/migrate.py b/db/migrate.py index 0a58d8fe..006de472 100644 --- a/db/migrate.py +++ b/db/migrate.py @@ -21,6 +21,7 @@ async def migrate(self): (1, self.migrate_1_add_rejected_original_id), (2, self.migrate_2_set_on_delete_cascade), (3, self.migrate_3_fix_finalize_operation_function), + (4, self.migrate_4_add_solution_id) ] async with self.pool.connection() as conn: async with conn.cursor() as cur: @@ -93,3 +94,39 @@ async def migrate_2_set_on_delete_cascade(conn: psycopg.AsyncConnection[DictRow] @staticmethod async def migrate_3_fix_finalize_operation_function(conn: psycopg.AsyncConnection[DictRow]): await conn.execute(cast(LiteralString, open("migration_3.sql").read())) + + + @staticmethod + async def migrate_4_add_solution_id(conn: psycopg.AsyncConnection[DictRow]): + # add column + try: + async with conn.transaction(): + await conn.execute(""" + ALTER TABLE solution + ADD COLUMN solution_id TEXT NULL + """) + except Exception as e: + raise RuntimeError(f"Failed to add column 'solution_id' to 'solution': {e}") + + async with conn.transaction(): + async with conn.cursor() as cur: + await cur.execute( + "SELECT id, epoch_hash, address, counter FROM solution ", + ) + rows = await cur.fetchall() + if not rows: + break + + for row in rows: + solution_id = SolutionID.load( + BytesIO(aleo_explorer_rust.solution_to_id( + str(row["epoch_hash"]), + str(row["address"]), + int(row["counter"]) + ) + ) + ) + await cur.execute( + "UPDATE solution SET solution_id = %s WHERE id = %s", + (str(solution_id), row["id"]) + )