From c91f26e002129c5f700f91428561a822ba4f3021 Mon Sep 17 00:00:00 2001 From: drsk Date: Wed, 11 Dec 2024 12:56:26 +0100 Subject: [PATCH] implement migrate accountV3toV4 --- .../Persistent/Account/CooldownQueue.hs | 2 +- .../Persistent/Account/StructureV1.hs | 68 ++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/CooldownQueue.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/CooldownQueue.hs index a9bece6399..dc53fabb3f 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/CooldownQueue.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/CooldownQueue.hs @@ -123,7 +123,7 @@ initialPrePreCooldownQueue target = } -- | Migrate a cooldown queue unchanged. -migrateCooldownQueue :: forall m t av. (SupportMigration m t) => CooldownQueue av -> t m (CooldownQueue av) +migrateCooldownQueue :: forall m t av1 av2. (SupportMigration m t, AVSupportsFlexibleCooldown av2) => CooldownQueue av1 -> t m (CooldownQueue av2) migrateCooldownQueue EmptyCooldownQueue = return EmptyCooldownQueue migrateCooldownQueue (CooldownQueue queueRef) = CooldownQueue <$> migrateEagerBufferedRef return queueRef diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV1.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV1.hs index c767c59562..08cf14c9a1 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV1.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV1.hs @@ -349,6 +349,32 @@ migratePersistentAccountStakeEnduringV2toV3 PersistentAccountStakeEnduringDelega liftStakedBalanceStateTT $ retainDelegator paseDelegatorId oldStake newTarget return $!! (newDelegatorInfo, emptyCooldownQueue) +-- | Migrate PersistentAccountStakeEnduring from AccountV3 to AccountV4. There +-- are no pending changes to take care of in V3, only the BakerInfoExV1 data +-- type needs to be migrated by setting the new `_bieIsSuspended` flag to +-- false. +migratePersistentAccountStakeEnduringV3toV4 :: + (SupportMigration m t, AccountMigration 'AccountV4 (t m)) => + PersistentAccountStakeEnduring 'AccountV3 -> + -- | Returns the new 'PersistentAccountStakeEnduring' and 'CooldownQueue'. + t m (PersistentAccountStakeEnduring 'AccountV4) +migratePersistentAccountStakeEnduringV3toV4 PersistentAccountStakeEnduringNone = + return PersistentAccountStakeEnduringNone +migratePersistentAccountStakeEnduringV3toV4 PersistentAccountStakeEnduringBaker{..} = do + newBakerInfo <- migrateReference (return . (\BakerInfoExV1{..} -> BakerInfoExV1{_bieIsSuspended = CTrue False, ..})) paseBakerInfo + return + PersistentAccountStakeEnduringBaker + { paseBakerInfo = newBakerInfo, + paseBakerPendingChange = NoChange, + .. + } +migratePersistentAccountStakeEnduringV3toV4 PersistentAccountStakeEnduringDelegator{..} = + return $! + PersistentAccountStakeEnduringDelegator + { paseDelegatorPendingChange = NoChange, + .. + } + -- | This relies on the fact that the 'AccountV2' hashing of 'AccountStake' is independent of the -- staked amount. instance (MonadBlobStore m) => MHashableTo m (AccountStakeHash 'AccountV2) (PersistentAccountStakeEnduring 'AccountV2) where @@ -1968,6 +1994,34 @@ migrateEnduringDataV2toV3 ed = do paedStake paedStakeCooldown +migrateEnduringDataV3toV4 :: + (SupportMigration m t, AccountMigration 'AccountV4 (t m), MonadLogger (t m)) => + -- | Current enduring data + PersistentAccountEnduringData 'AccountV3 -> + -- | New enduring data. + t m (PersistentAccountEnduringData 'AccountV4) +migrateEnduringDataV3toV4 ed = do + logEvent GlobalState LLTrace "Migrating persisting data" + paedPersistingData <- migrateEagerBufferedRef return (paedPersistingData ed) + paedEncryptedAmount <- forM (paedEncryptedAmount ed) $ \e -> do + logEvent GlobalState LLTrace "Migrating encrypted amount" + migrateReference migratePersistentEncryptedAmount e + paedReleaseSchedule <- forM (paedReleaseSchedule ed) $ \(oldRSRef, lockedAmt) -> do + logEvent GlobalState LLTrace "Migrating release schedule" + newRSRef <- migrateReference migrateAccountReleaseSchedule oldRSRef + return (newRSRef, lockedAmt) + logEvent GlobalState LLTrace "Migrating stake" + paedStake <- migratePersistentAccountStakeEnduringV3toV4 (paedStake ed) + logEvent GlobalState LLTrace "Migrating cooldown queue" + paedStakeCooldown <- migrateCooldownQueue (paedStakeCooldown ed) + logEvent GlobalState LLTrace "Reconstructing account enduring data" + makeAccountEnduringDataAV4 + paedPersistingData + paedEncryptedAmount + paedReleaseSchedule + paedStake + paedStakeCooldown + -- | Migration for 'PersistentAccountEnduringData'. Only supports 'AccountV3'. -- The data is unchanged in the migration. migrateEnduringDataV3toV3 :: @@ -2081,11 +2135,21 @@ migrateV3ToV3 acc = do migrateV3ToV4 :: ( MonadBlobStore m, MonadBlobStore (t m), - MonadTrans t + AccountMigration 'AccountV4 (t m), + MonadTrans t, + MonadLogger (t m) ) => PersistentAccount 'AccountV3 -> t m (PersistentAccount 'AccountV4) -migrateV3ToV4 _acc = error "TODO(drsk) github #1221. implement migrateV3ToV4" +migrateV3ToV4 acc = do + (accountEnduringData) <- migrateEagerBufferedRef migrateEnduringDataV3toV4 (accountEnduringData acc) + return $! + PersistentAccount + { accountNonce = accountNonce acc, + accountAmount = accountAmount acc, + accountStakedAmount = accountStakedAmount acc, + .. + } -- | A trivial migration from account version 4 to account version 4. -- In particular the data is retained as-is.