Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protocol version 7 #1075

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ genesisBakerInfoEx spv cp GenesisBaker{..} = case spv of
SP4 -> binfoV1
SP5 -> binfoV1
SP6 -> binfoV1
SP7 -> binfoV1
where
bkrInfo =
BakerInfo
Expand Down
1 change: 1 addition & 0 deletions concordium-consensus/src/Concordium/GlobalState/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ blockVersion SP3 = 2
blockVersion SP4 = 2
blockVersion SP5 = 2
blockVersion SP6 = 3
blockVersion SP7 = 3
{-# INLINE blockVersion #-}

-- | Type class that supports serialization of a block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ migratePersistentAccount m@StateMigrationParametersP2P3 (PAV0 acc) = PAV0 <$> V0
migratePersistentAccount m@StateMigrationParametersP3ToP4{} (PAV0 acc) = PAV1 <$> V0.migratePersistentAccount m acc
migratePersistentAccount m@StateMigrationParametersP4ToP5{} (PAV1 acc) = PAV2 <$> V1.migratePersistentAccountFromV0 m acc
migratePersistentAccount m@StateMigrationParametersP5ToP6{} (PAV2 acc) = PAV2 <$> V1.migratePersistentAccount m acc
migratePersistentAccount m@StateMigrationParametersP6ToP7{} (PAV2 acc) = PAV2 <$> V1.migratePersistentAccount m acc

-- | Migrate a 'PersistentBakerInfoRef' between protocol versions according to a state migration.
migratePersistentBakerInfoRef ::
Expand All @@ -551,6 +552,7 @@ migratePersistentBakerInfoRef m@StateMigrationParametersP2P3 (PBIRV0 bir) = PBIR
migratePersistentBakerInfoRef m@StateMigrationParametersP3ToP4{} (PBIRV0 bir) = PBIRV1 <$> V0.migratePersistentBakerInfoEx m bir
migratePersistentBakerInfoRef m@StateMigrationParametersP4ToP5{} (PBIRV1 bir) = PBIRV2 <$> V1.migratePersistentBakerInfoExFromV0 m bir
migratePersistentBakerInfoRef m@StateMigrationParametersP5ToP6{} (PBIRV2 bir) = PBIRV2 <$> V1.migratePersistentBakerInfoEx m bir
migratePersistentBakerInfoRef m@StateMigrationParametersP6ToP7{} (PBIRV2 bir) = PBIRV2 <$> V1.migratePersistentBakerInfoEx m bir

-- * Conversion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ migratePersistentBakerInfoEx ::
t m (PersistentBakerInfoEx (AccountVersionFor pv))
migratePersistentBakerInfoEx StateMigrationParametersTrivial = migrateReference return
migratePersistentBakerInfoEx StateMigrationParametersP5ToP6{} = migrateReference return
migratePersistentBakerInfoEx StateMigrationParametersP6ToP7 = migrateReference return
limemloh marked this conversation as resolved.
Show resolved Hide resolved

-- | Migrate a 'V0.PersistentBakerInfoEx' to a 'PersistentBakerInfoEx'.
-- See documentation of @migratePersistentBlockState@.
Expand Down Expand Up @@ -1354,6 +1355,7 @@ migratePersistentAccount ::
t m (PersistentAccount (AccountVersionFor pv))
migratePersistentAccount StateMigrationParametersTrivial acc = migrateV2ToV2 acc
migratePersistentAccount StateMigrationParametersP5ToP6{} acc = migrateV2ToV2 acc
migratePersistentAccount StateMigrationParametersP6ToP7 acc = migrateV2ToV2 acc

-- | Migration for 'PersistentAccount' from 'V0.PersistentAccount'. This supports migration from
-- 'P4' to 'P5'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ migratePersistentEpochBakers migration PersistentEpochBakers{..} = do
StateMigrationParametersP3ToP4{} -> NoParam
StateMigrationParametersP4ToP5 -> NoParam
(StateMigrationParametersP5ToP6 P6.StateMigrationData{..}) -> SomeParam $ P6.updateFinalizationCommitteeParameters migrationProtocolUpdateData
StateMigrationParametersP6ToP7 -> _bakerFinalizationCommitteeParameters
return
PersistentEpochBakers
{ _bakerInfos = newBakerInfos,
Expand Down Expand Up @@ -298,6 +299,10 @@ migratePersistentActiveDelegators StateMigrationParametersP5ToP6{} = \case
PersistentActiveDelegatorsV1{..} -> do
newDelegators <- Trie.migrateTrieN True return adDelegators
return PersistentActiveDelegatorsV1{adDelegators = newDelegators, ..}
migratePersistentActiveDelegators StateMigrationParametersP6ToP7 = \case
PersistentActiveDelegatorsV1{..} -> do
newDelegators <- Trie.migrateTrieN True return adDelegators
return PersistentActiveDelegatorsV1{adDelegators = newDelegators, ..}

emptyPersistentActiveDelegators :: forall av. (IsAccountVersion av) => PersistentActiveDelegators av
emptyPersistentActiveDelegators =
Expand Down Expand Up @@ -346,6 +351,7 @@ migrateTotalActiveCapital StateMigrationParametersP2P3 _ x = x
migrateTotalActiveCapital (StateMigrationParametersP3ToP4 _) bts TotalActiveCapitalV0 = TotalActiveCapitalV1 bts
migrateTotalActiveCapital StateMigrationParametersP4ToP5 _ (TotalActiveCapitalV1 bts) = TotalActiveCapitalV1 bts
migrateTotalActiveCapital StateMigrationParametersP5ToP6{} _ (TotalActiveCapitalV1 bts) = TotalActiveCapitalV1 bts
migrateTotalActiveCapital StateMigrationParametersP6ToP7 _ (TotalActiveCapitalV1 bts) = TotalActiveCapitalV1 bts

instance (IsAccountVersion av) => Serialize (TotalActiveCapital av) where
put TotalActiveCapitalV0 = return ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ migrateSeedState StateMigrationParametersP4ToP5{} ss = ss
migrateSeedState (StateMigrationParametersP5ToP6 (P6.StateMigrationData _ time)) SeedStateV0{..} =
let seed = H.hash $ "Regenesis" <> encode ss0CurrentLeadershipElectionNonce
in initialSeedStateV1 seed time
migrateSeedState StateMigrationParametersP6ToP7 SeedStateV1{..} =
SeedStateV1
{ -- Reset the epoch to 0.
ss1Epoch = 0,
ss1CurrentLeadershipElectionNonce = newNonce,
ss1UpdatedNonce = newNonce,
-- We maintain the trigger block time. This forces an epoch transition as soon as possible
-- which will effectively substitute for the epoch transition that would have happened
-- on the previous consensus, had it not shut down.
ss1TriggerBlockTime = ss1TriggerBlockTime,
-- We flag the epoch transition as triggered so that the epoch transition will happen
-- as soon as possible.
ss1EpochTransitionTriggered = True,
-- We clear the shutdown flag.
ss1ShutdownTriggered = False
}
where
-- We derive the new nonce from the updated nonce on the basis that it was fixed
-- at the trigger block from the previous consensus.
newNonce = H.hash $ "Regenesis" <> encode ss1UpdatedNonce
limemloh marked this conversation as resolved.
Show resolved Hide resolved

-- | See documentation of @migratePersistentBlockState@.
--
Expand Down Expand Up @@ -605,6 +625,10 @@ migrateBlockRewardDetails StateMigrationParametersP5ToP6{} _ _ (SomeParam TimePa
(BlockRewardDetailsV1 hbr) ->
BlockRewardDetailsV1
<$> migrateHashedBufferedRef (migratePoolRewards (rewardPeriodEpochs _tpRewardPeriodLength)) hbr
migrateBlockRewardDetails StateMigrationParametersP6ToP7 _ _ (SomeParam TimeParametersV1{..}) _ = \case
(BlockRewardDetailsV1 hbr) ->
BlockRewardDetailsV1
<$> migrateHashedBufferedRef (migratePoolRewards (rewardPeriodEpochs _tpRewardPeriodLength)) hbr

instance (MonadBlobStore m) => MHashableTo m (Rewards.BlockRewardDetailsHash av) (BlockRewardDetails av) where
getHashM (BlockRewardDetailsV0 heb) = return $ Rewards.BlockRewardDetailsHashV0 (getHash heb)
Expand Down Expand Up @@ -2167,6 +2191,7 @@ doGetRewardStatus pbs = do
SP4 -> rewardsV1
SP5 -> rewardsV1
SP6 -> rewardsV1
SP7 -> rewardsV1

doRewardFoundationAccount :: (SupportsPersistentState pv m) => PersistentBlockState pv -> Amount -> m (PersistentBlockState pv)
doRewardFoundationAccount pbs reward = do
Expand Down Expand Up @@ -2292,6 +2317,7 @@ doModifyAccount pbs aUpd@AccountUpdate{..} = do
SP4 -> accountCanonicalAddress acc'
SP5 -> return _auIndex
SP6 -> return _auIndex
SP7 -> return _auIndex
!oldRel <- accountNextReleaseTimestamp acc
!newRel <- accountNextReleaseTimestamp acc'
return (acctRef :: RSAccountRef pv, oldRel, newRel)
Expand Down Expand Up @@ -2834,6 +2860,7 @@ doProcessReleaseSchedule pbs ts = do
SP4 -> processAccountP1
SP5 -> processAccountP5
SP6 -> processAccountP5
SP7 -> processAccountP5
(newAccs, newRS) <- foldM processAccount (bspAccounts bsp, remRS) affectedAccounts
storePBS pbs (bsp{bspAccounts = newAccs, bspReleaseSchedule = newRS})

Expand Down Expand Up @@ -3685,6 +3712,7 @@ migrateBlockPointers migration BlockStatePointers{..} = do
Nothing -> error "Account with release schedule does not exist"
Just ai -> ai
StateMigrationParametersP5ToP6{} -> RSMNewToNew
StateMigrationParametersP6ToP7 -> RSMNewToNew
newReleaseSchedule <- migrateReleaseSchedule rsMigration bspReleaseSchedule
newAccounts <- Accounts.migrateAccounts migration bspAccounts
newModules <- migrateHashedBufferedRef Modules.migrateModules bspModules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
StateMigrationParametersP5ToP6{} -> case pElectionDifficultyQueue of
SomeParam _ -> return NoParam
StateMigrationParametersP6ToP7 -> case pElectionDifficultyQueue of
NoParam -> return NoParam
newTimeParameters <- case migration of
StateMigrationParametersTrivial -> case pTimeParametersQueue of
NoParam -> return NoParam
Expand All @@ -318,6 +320,8 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
StateMigrationParametersP5ToP6{} -> case pTimeParametersQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
StateMigrationParametersP6ToP7 -> case pTimeParametersQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
newCooldownParameters <- case migration of
StateMigrationParametersTrivial -> case pCooldownParametersQueue of
NoParam -> return NoParam
Expand All @@ -334,6 +338,8 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
StateMigrationParametersP5ToP6{} -> case pCooldownParametersQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
StateMigrationParametersP6ToP7 -> case pCooldownParametersQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
newTimeoutParameters <- case migration of
StateMigrationParametersTrivial -> case pTimeoutParametersQueue of
NoParam -> return NoParam
Expand All @@ -349,6 +355,8 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
StateMigrationParametersP5ToP6{} -> do
(!hbr, _) <- refFlush =<< refMake emptyUpdateQueue
return (SomeParam hbr)
StateMigrationParametersP6ToP7 -> case pTimeoutParametersQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
newMinBlockTimeQueue <- case migration of
StateMigrationParametersTrivial -> case pMinBlockTimeQueue of
NoParam -> return NoParam
Expand All @@ -364,6 +372,8 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
StateMigrationParametersP5ToP6{} -> do
(!hbr, _) <- refFlush =<< refMake emptyUpdateQueue
return (SomeParam hbr)
StateMigrationParametersP6ToP7 -> case pMinBlockTimeQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
newBlockEnergyLimitQueue <- case migration of
StateMigrationParametersTrivial -> case pBlockEnergyLimitQueue of
NoParam -> return NoParam
Expand All @@ -379,6 +389,8 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
StateMigrationParametersP5ToP6{} -> do
(!hbr, _) <- refFlush =<< refMake emptyUpdateQueue
return (SomeParam hbr)
StateMigrationParametersP6ToP7 -> case pBlockEnergyLimitQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
newFinalizationCommitteeParametersQueue <- case migration of
StateMigrationParametersTrivial -> case pFinalizationCommitteeParametersQueue of
NoParam -> return NoParam
Expand All @@ -394,6 +406,8 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
StateMigrationParametersP5ToP6{} -> do
(!hbr, _) <- refFlush =<< refMake emptyUpdateQueue
return (SomeParam hbr)
StateMigrationParametersP6ToP7 -> case pFinalizationCommitteeParametersQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
return $!
PendingUpdates
{ pRootKeysUpdateQueue = newRootKeys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import qualified Concordium.Genesis.Data.P3 as P3
import qualified Concordium.Genesis.Data.P4 as P4
import qualified Concordium.Genesis.Data.P5 as P5
import qualified Concordium.Genesis.Data.P6 as P6
import qualified Concordium.Genesis.Data.P7 as P7
import qualified Concordium.GlobalState.Basic.BlockState.PoolRewards as Basic
import qualified Concordium.GlobalState.CapitalDistribution as CapDist
import qualified Concordium.GlobalState.Persistent.Account as Account
Expand Down Expand Up @@ -74,6 +75,9 @@ genesisState gd = MTL.runExceptT $ case Types.protocolVersion @pv of
Types.SP6 -> case gd of
GenesisData.GDP6 P6.GDP6Initial{..} ->
buildGenesisBlockState (CGPV1 genesisCore) genesisInitialState
Types.SP7 -> case gd of
GenesisData.GDP7 P7.GDP7Initial{..} ->
buildGenesisBlockState (CGPV1 genesisCore) genesisInitialState

-------- Types -----------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ instance (MonadBlobStore m, IsProtocolVersion pv) => BlobStorable m (ReleaseSche
SP4 -> fmap ReleaseScheduleP0 <$> load
SP5 -> fmap ReleaseScheduleP5 <$> load
SP6 -> fmap ReleaseScheduleP5 <$> load
SP7 -> fmap ReleaseScheduleP5 <$> load

instance (MonadBlobStore m) => Cacheable m (ReleaseSchedule pv) where
cache (ReleaseScheduleP0 rs) = ReleaseScheduleP0 <$> cache rs
Expand Down Expand Up @@ -293,6 +294,7 @@ emptyReleaseSchedule = case protocolVersion @pv of
SP4 -> rsP0
SP5 -> rsP1
SP6 -> rsP1
SP7 -> rsP1
where
rsP0 :: (RSAccountRef pv ~ AccountAddress) => m (ReleaseSchedule pv)
rsP0 = do
Expand Down Expand Up @@ -342,6 +344,7 @@ trivialReleaseScheduleMigration = case protocolVersion @pv of
SP4 -> RSMLegacyToLegacy
SP5 -> RSMNewToNew
SP6 -> RSMNewToNew
SP7 -> RSMNewToNew

-- | Migrate a release schedule from one protocol version to another, given by a
-- 'ReleaseScheduleMigration'.
Expand Down
4 changes: 3 additions & 1 deletion concordium-consensus/src/Concordium/KonsensusV1/TestMonad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import Concordium.Types

import qualified Concordium.Genesis.Data.BaseV1 as BaseV1
import qualified Concordium.Genesis.Data.P6 as P6
import qualified Concordium.Genesis.Data.P7 as P7
import qualified Concordium.GlobalState.AccountMap.LMDB as LMDBAccountMap
import Concordium.GlobalState.BlockState
import Concordium.GlobalState.Parameters (
GenesisData (GDP6),
GenesisData (GDP6, GDP7),
defaultRuntimeParameters,
genesisBlockHash,
)
Expand Down Expand Up @@ -145,6 +146,7 @@ instance HasBakerContext (TestContext pv) where
genesisCore :: forall pv. (IsConsensusV1 pv, IsProtocolVersion pv) => GenesisData pv -> BaseV1.CoreGenesisParametersV1
genesisCore = case protocolVersion @pv of
SP6 -> \(GDP6 P6.GDP6Initial{genesisCore = core}) -> core
SP7 -> \(GDP7 P7.GDP7Initial{genesisCore = core}) -> core

-- | Run an operation in the 'TestMonad' with the given baker, time and genesis data.
-- This sets up a temporary blob store for the block state that is deleted after use.
Expand Down
61 changes: 61 additions & 0 deletions concordium-consensus/src/Concordium/ProtocolUpdate/P7.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}

module Concordium.ProtocolUpdate.P7 (
Update (..),
checkUpdate,
updateRegenesis,
updateNextProtocolVersion,
) where

import Control.Monad.State
import qualified Data.HashMap.Strict as HM
import Data.Serialize

import qualified Concordium.Crypto.SHA256 as SHA256
import Concordium.Types
import Concordium.Types.Updates

import Concordium.GlobalState.BlockState
import qualified Concordium.GlobalState.Persistent.BlockState as PBS
import Concordium.GlobalState.Types
import qualified Concordium.GlobalState.Types as GSTypes
import Concordium.KonsensusV1.TreeState.Implementation
import Concordium.KonsensusV1.TreeState.Types
import qualified Concordium.ProtocolUpdate.P7.Reboot as Reboot

-- | Updates that are supported from protocol version P7.
data Update = Reboot
deriving (Show)

-- | Hash map for resolving updates from their specification hash.
updates :: HM.HashMap SHA256.Hash (Get Update)
updates = HM.fromList [(Reboot.updateHash, return Reboot)]

-- | Determine if a 'ProtocolUpdate' corresponds to a supported update type.
checkUpdate :: ProtocolUpdate -> Either String Update
checkUpdate ProtocolUpdate{..} = case HM.lookup puSpecificationHash updates of
Nothing -> Left "Specification hash does not correspond to a known protocol update."
Just updateGet -> case runGet updateGet puSpecificationAuxiliaryData of
Left err -> Left $! "Could not deserialize auxiliary data: " ++ err
Right update -> return update

-- | Construct the genesis data for a P7 update.
updateRegenesis ::
( MPV m ~ 'P7,
BlockStateStorage m,
MonadState (SkovData (MPV m)) m,
GSTypes.BlockState m ~ PBS.HashedPersistentBlockState (MPV m)
) =>
-- | The update taking effect.
Update ->
-- | The terminal block of the old chain.
BlockPointer (MPV m) ->
m (PVInit m)
updateRegenesis Reboot = Reboot.updateRegenesis

-- | Determine the protocol version the update will update to.
updateNextProtocolVersion ::
Update ->
SomeProtocolVersion
updateNextProtocolVersion Reboot{} = SomeProtocolVersion SP7
Loading
Loading