Skip to content

Commit

Permalink
implement migrate accountV3toV4 (#1300)
Browse files Browse the repository at this point in the history
  • Loading branch information
drsk0 authored Dec 11, 2024
1 parent 057d4b0 commit 8907fab
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ::
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 8907fab

Please sign in to comment.