From 144cd1f1c6b7b3a3b54169d2f1a2a7abae1b7354 Mon Sep 17 00:00:00 2001 From: Thomas Dinsdale-Young Date: Fri, 8 Mar 2024 17:22:02 +0100 Subject: [PATCH 1/2] Remove unused state serialization code. --- concordium-base | 2 +- .../GlobalState/Basic/BlockState/Account.hs | 100 ------------------ .../BlockState/AccountReleaseSchedule.hs | 34 ------ .../BlockState/AccountReleaseScheduleV0.hs | 42 -------- .../src/Concordium/GlobalState/BlockState.hs | 6 -- .../GlobalState/Persistent/Account.hs | 12 --- .../Persistent/Account/StructureV0.hs | 57 ---------- .../Persistent/Account/StructureV1.hs | 65 ------------ .../GlobalState/Persistent/Accounts.hs | 8 -- .../GlobalState/Persistent/BlockState.hs | 57 ---------- .../BlockState/AccountReleaseScheduleV1.hs | 17 --- .../Persistent/BlockState/Modules.hs | 17 --- .../Persistent/BlockState/Updates.hs | 44 -------- .../GlobalState/Persistent/Instances.hs | 48 --------- .../tests/globalstate/Basic/Accounts.hs | 47 -------- 15 files changed, 1 insertion(+), 555 deletions(-) diff --git a/concordium-base b/concordium-base index 825fab04fb..9fa4e270d3 160000 --- a/concordium-base +++ b/concordium-base @@ -1 +1 @@ -Subproject commit 825fab04fb78445c9f1cdcaf4cb9dae33f3b831a +Subproject commit 9fa4e270d3f3928c753fcc2e2982c736d1d29226 diff --git a/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/Account.hs b/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/Account.hs index 021f0d5373..7b6835ce9d 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/Account.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/Account.hs @@ -14,11 +14,8 @@ module Concordium.GlobalState.Basic.BlockState.Account ( module Concordium.GlobalState.Basic.BlockState.Account, ) where -import Control.Monad import Data.Coerce import qualified Data.Map.Strict as Map -import Data.Maybe -import qualified Data.Serialize as S import GHC.Stack (HasCallStack) import Lens.Micro.Platform @@ -28,12 +25,9 @@ import Concordium.GlobalState.Basic.BlockState.AccountReleaseSchedule import Concordium.ID.Parameters import Concordium.ID.Types import Concordium.Types.HashableTo -import Concordium.Utils.Serialization -import Concordium.Genesis.Data import Concordium.Types import Concordium.Types.Accounts -import Concordium.Types.Migration -- | Type for how a 'PersistingAccountData' value is stored as part of -- an account. This is stored with its hash. @@ -97,100 +91,6 @@ unsafeAccountDelegator = singular accountDelegator instance HasPersistingAccountData (Account av) where persistingAccountData = accountPersisting . unhashed --- | Serialize an account. The serialization format may depend on the protocol version. --- --- This format allows accounts to be stored in a reduced format by --- eliding (some) data that can be inferred from context, or is --- the default value. Note that there can be multiple representations --- of the same account. -serializeAccount :: (IsAccountVersion av) => GlobalContext -> S.Putter (Account av) -serializeAccount cryptoParams acct@Account{..} = do - S.put flags - when asfExplicitAddress $ S.put _accountAddress - when asfExplicitEncryptionKey $ S.put _accountEncryptionKey - unless asfThresholdIsOne $ S.put (aiThreshold _accountVerificationKeys) - putCredentials - when asfHasRemovedCredentials $ S.put (_accountRemovedCredentials ^. unhashed) - S.put _accountNonce - S.put _accountAmount - when asfExplicitEncryptedAmount $ S.put _accountEncryptedAmount - when asfExplicitReleaseSchedule $ serializeAccountReleaseSchedule _accountReleaseSchedule - when asfHasBakerOrDelegation $ serializeAccountStake _accountStaking - where - PersistingAccountData{..} = acct ^. persistingAccountData - flags = AccountSerializationFlags{..} - initialCredId = - credId - ( Map.findWithDefault - (error "Account missing initial credential") - initialCredentialIndex - _accountCredentials - ) - asfExplicitAddress = _accountAddress /= addressFromRegIdRaw initialCredId - -- There is an opportunity for improvement here. We do not have to convert - -- the raw key to a structured one. We can check the equality directly on - -- the byte representation (in fact equality is defined on those). However - -- that requires a bit of work to expose the right raw values from - -- cryptographic parameters. - asfExplicitEncryptionKey = unsafeEncryptionKeyFromRaw _accountEncryptionKey /= makeEncryptionKey cryptoParams (unsafeCredIdFromRaw initialCredId) - (asfMultipleCredentials, putCredentials) = case Map.toList _accountCredentials of - [(i, cred)] | i == initialCredentialIndex -> (False, S.put cred) - _ -> (True, putSafeMapOf S.put S.put _accountCredentials) - asfExplicitEncryptedAmount = _accountEncryptedAmount /= initialAccountEncryptedAmount - asfExplicitReleaseSchedule = _accountReleaseSchedule /= emptyAccountReleaseSchedule - asfHasBakerOrDelegation = _accountStaking /= AccountStakeNone - asfThresholdIsOne = aiThreshold _accountVerificationKeys == 1 - asfHasRemovedCredentials = _accountRemovedCredentials ^. unhashed /= EmptyRemovedCredentials - --- | Deserialize an account. --- The serialization format may depend on the protocol version, and maybe migrated from one version --- to another, using the 'StateMigrationParameters' provided. -deserializeAccount :: - forall oldpv pv. - (IsProtocolVersion oldpv, IsProtocolVersion pv) => - StateMigrationParameters oldpv pv -> - GlobalContext -> - S.Get (Account (AccountVersionFor pv)) -deserializeAccount migration cryptoParams = do - AccountSerializationFlags{..} <- S.get - preAddress <- if asfExplicitAddress then Just <$> S.get else return Nothing - preEncryptionKey <- if asfExplicitEncryptionKey then Just <$> S.get else return Nothing - threshold <- if asfThresholdIsOne then return 1 else S.get - let getCredentials - | asfMultipleCredentials = do - creds <- getSafeMapOf S.get S.get - case Map.lookup initialCredentialIndex creds of - Nothing -> fail $ "Account has no credential with index " ++ show initialCredentialIndex - Just cred -> return (creds, credId cred) - | otherwise = do - cred <- S.get - return (Map.singleton initialCredentialIndex cred, credId cred) - (_accountCredentials, initialCredId) <- getCredentials - _accountRemovedCredentials <- if asfHasRemovedCredentials then makeHashed <$> S.get else return emptyHashedRemovedCredentials - let _accountVerificationKeys = getAccountInformation threshold _accountCredentials - let _accountAddress = fromMaybe (addressFromRegIdRaw initialCredId) preAddress - -- There is an opportunity for improvement here. We do not have to convert - -- the raw credId to a structured one. We can directly construct the - -- However that requires a bit of work to expose the right raw values from - -- cryptographic parameters. - _accountEncryptionKey = fromMaybe (toRawEncryptionKey (makeEncryptionKey cryptoParams (unsafeCredIdFromRaw initialCredId))) preEncryptionKey - _accountNonce <- S.get - _accountAmount <- S.get - _accountEncryptedAmount <- - if asfExplicitEncryptedAmount - then S.get - else return initialAccountEncryptedAmount - _accountReleaseSchedule <- - if asfExplicitReleaseSchedule - then deserializeAccountReleaseSchedule (accountVersion @(AccountVersionFor oldpv)) - else return emptyAccountReleaseSchedule - _accountStaking <- - if asfHasBakerOrDelegation - then migrateAccountStake migration <$> deserializeAccountStake - else return AccountStakeNone - let _accountPersisting = makeAccountPersisting PersistingAccountData{..} - return Account{..} - -- | Generate hash inputs from an account for 'AccountV0' and 'AccountV1'. accountHashInputsV0 :: (IsAccountVersion av, AccountStructureVersionFor av ~ 'AccountStructureV0) => Account av -> AccountHashInputsV0 av accountHashInputsV0 Account{..} = diff --git a/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/AccountReleaseSchedule.hs b/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/AccountReleaseSchedule.hs index 52278a9b10..e2766703c7 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/AccountReleaseSchedule.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/AccountReleaseSchedule.hs @@ -10,7 +10,6 @@ module Concordium.GlobalState.Basic.BlockState.AccountReleaseSchedule where import Data.Function -import Data.Serialize import Lens.Micro.Platform import Concordium.Types @@ -146,39 +145,6 @@ nextReleaseTimestamp = case accountVersion @av of SAccountV1 -> ARSV0.nextReleaseTimestamp . theAccountReleaseSchedule SAccountV2 -> ARSV1.nextReleaseTimestamp . theAccountReleaseSchedule --- | Serialize an account release schedule. The serialization format depends on the account version. -serializeAccountReleaseSchedule :: forall av. (IsAccountVersion av) => Putter (AccountReleaseSchedule av) -serializeAccountReleaseSchedule = case accountVersion @av of - SAccountV0 -> put . theAccountReleaseSchedule - SAccountV1 -> put . theAccountReleaseSchedule - SAccountV2 -> put . theAccountReleaseSchedule - --- | Deserialize an account release schedule, possibly upgrading the account version. --- --- Precondition: @av@ is at least @avold@. -deserializeAccountReleaseSchedule :: forall avold av. (IsAccountVersion av) => SAccountVersion avold -> Get (AccountReleaseSchedule av) -deserializeAccountReleaseSchedule SAccountV0 = AccountReleaseSchedule . convert <$> get - where - convert :: ARSV0.AccountReleaseSchedule -> AccountReleaseSchedule' av - convert = case accountVersion @av of - SAccountV0 -> id - SAccountV1 -> id - SAccountV2 -> ARSV1.fromAccountReleaseScheduleV0 -deserializeAccountReleaseSchedule SAccountV1 = AccountReleaseSchedule . convert <$> get - where - convert :: ARSV0.AccountReleaseSchedule -> AccountReleaseSchedule' av - convert = case accountVersion @av of - SAccountV0 -> id - SAccountV1 -> id - SAccountV2 -> ARSV1.fromAccountReleaseScheduleV0 -deserializeAccountReleaseSchedule SAccountV2 = AccountReleaseSchedule . convert <$> get - where - convert :: ARSV1.AccountReleaseSchedule -> AccountReleaseSchedule' av - convert = case accountVersion @av of - SAccountV0 -> error "deserializeAccountReleaseSchedule: cannot downgrade account version" - SAccountV1 -> error "deserializeAccountReleaseSchedule: cannot downgrade account version" - SAccountV2 -> id - -- | Get the total locked balance. totalLockedUpBalance :: forall av. (IsAccountVersion av) => SimpleGetter (AccountReleaseSchedule av) Amount totalLockedUpBalance = case accountVersion @av of diff --git a/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/AccountReleaseScheduleV0.hs b/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/AccountReleaseScheduleV0.hs index bd00ba03ea..1746daaa95 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/AccountReleaseScheduleV0.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Basic/BlockState/AccountReleaseScheduleV0.hs @@ -37,7 +37,6 @@ module Concordium.GlobalState.Basic.BlockState.AccountReleaseScheduleV0 ( sumOfReleases, ) where -import Control.Monad import qualified Data.ByteString as BS import Data.Foldable import Data.Function @@ -53,7 +52,6 @@ import Concordium.Crypto.SHA256 import Concordium.Types import Concordium.Types.Accounts.Releases import Concordium.Types.HashableTo -import Concordium.Utils.Serialization ----------------------------------- Release ------------------------------------ @@ -121,46 +119,6 @@ toAccountReleaseSummary AccountReleaseSchedule{..} = AccountReleaseSummary{..} } releaseSchedule = map makeScheduledRelease sortedAndGroupedByTimestamp -instance Serialize AccountReleaseSchedule where - get = do - vecLength <- getLength - _values <- - Vector.replicateM - vecLength - ( getMaybe - ( do - l <- getLength - item <- replicateM l get - txh <- get - return (item, txh) - ) - ) - let (_pendingReleases, _totalLockedUpBalance) = - Vector.ifoldl' - ( \acc idx -> \case - Nothing -> acc - Just (rel, _) -> - let f (pending, am) Release{..} = - let pending' = Map.alter (maybe (Just [idx]) (Just . (idx :))) timestamp pending - am' = am + amount - in (pending', am') - in foldl' f acc rel - ) - (Map.empty, 0) - _values - return AccountReleaseSchedule{..} - put AccountReleaseSchedule{..} = do - putLength $ Vector.length _values - Vector.mapM_ - ( putMaybe - ( \(rel, txh) -> do - putLength $ length rel - mapM_ put rel - put txh - ) - ) - _values - -- λ: getHash $ addReleases ([(3,5), (4,10)], th) $ addReleases ([(1,2), (3,4)], th) emptyAccountReleaseSchedule :: Hash -- 5473ef105c995db8d8dfe75881d8a2018bb12eaeef32032569edfff6814f1b50 -- λ: h1 = hash ((runPut $ put (1 :: Timestamp) >> put (2 :: Amount)) <> hashToByteString (hash (runPut $ put (3 :: Timestamp) >> put (4 :: Amount)))) diff --git a/concordium-consensus/src/Concordium/GlobalState/BlockState.hs b/concordium-consensus/src/Concordium/GlobalState/BlockState.hs index 7a648d1487..ab12f4b302 100644 --- a/concordium-consensus/src/Concordium/GlobalState/BlockState.hs +++ b/concordium-consensus/src/Concordium/GlobalState/BlockState.hs @@ -1431,10 +1431,6 @@ class (BlockStateOperations m, FixedSizeSerialization (BlockStateRef m)) => Bloc -- In particular the 'StateHash' should be supplied if loading a non-genesis block state. loadBlockState :: Maybe StateHash -> BlockStateRef m -> m (BlockState m) - -- | Serialize the block state to a byte string. - -- This serialization does not include transaction outcomes. - serializeBlockState :: BlockState m -> m BS.ByteString - -- | Retrieve the callback that is needed to read state that is not in -- memory. This is needed for using V1 contract state. blockStateLoadCallback :: m LoadCallback @@ -1721,7 +1717,6 @@ instance (Monad (t m), MonadTrans t, BlockStateStorage m) => BlockStateStorage ( saveAccounts = lift . saveAccounts reconstructAccountDifferenceMap bs parentMap = lift . reconstructAccountDifferenceMap bs parentMap loadBlockState hsh = lift . loadBlockState hsh - serializeBlockState = lift . serializeBlockState blockStateLoadCallback = lift blockStateLoadCallback collapseCaches = lift collapseCaches cacheBlockState = lift . cacheBlockState @@ -1735,7 +1730,6 @@ instance (Monad (t m), MonadTrans t, BlockStateStorage m) => BlockStateStorage ( {-# INLINE saveBlockState #-} {-# INLINE reconstructAccountDifferenceMap #-} {-# INLINE loadBlockState #-} - {-# INLINE serializeBlockState #-} {-# INLINE blockStateLoadCallback #-} {-# INLINE collapseCaches #-} {-# INLINE cacheBlockState #-} diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account.hs index 56bc59a91f..ca9728b27c 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account.hs @@ -20,7 +20,6 @@ import Concordium.Types.Accounts.Releases import Concordium.Types.Execution import Concordium.Types.HashableTo import Concordium.Types.Parameters -import Concordium.Utils.Serialization.Put import qualified Concordium.Crypto.SHA256 as Hash import Concordium.Genesis.Data @@ -506,17 +505,6 @@ makePersistentBakerInfoRef = case accountVersion @av of -- * Migration --- | Serialize an account. The serialization format may depend on the protocol version. --- --- This format allows accounts to be stored in a reduced format by --- eliding (some) data that can be inferred from context, or is --- the default value. Note that there can be multiple representations --- of the same account. -serializeAccount :: (MonadBlobStore m, MonadPut m) => GlobalContext -> PersistentAccount av -> m () -serializeAccount gc (PAV0 acc) = V0.serializeAccount gc acc -serializeAccount gc (PAV1 acc) = V0.serializeAccount gc acc -serializeAccount gc (PAV2 acc) = V1.serializeAccount gc acc - -- | Migrate a 'PersistentAccount' between protocol versions according to a state migration. migratePersistentAccount :: forall oldpv pv t m. diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV0.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV0.hs index 5beecea451..d21a27dafa 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV0.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV0.hs @@ -37,8 +37,6 @@ import Concordium.Types.Accounts.Releases import Concordium.Types.Execution import Concordium.Types.HashableTo import qualified Concordium.Types.Migration as Migration -import Concordium.Utils.Serialization -import Concordium.Utils.Serialization.Put import Concordium.GlobalState.Account hiding (addIncomingEncryptedAmount, addToSelfEncryptedAmount, replaceUpTo) import Concordium.GlobalState.BakerInfo (BakerAdd (..), BakerKeyUpdate (..), bakerKeyUpdateToInfo, genesisBakerInfo) @@ -1195,61 +1193,6 @@ migratePersistentAccount migration PersistentAccount{..} = do _accountStake = newAccountStake } --- ** Serialization - --- | Serialize an account. The serialization format may depend on the protocol version. --- --- This format allows accounts to be stored in a reduced format by --- eliding (some) data that can be inferred from context, or is --- the default value. Note that there can be multiple representations --- of the same account. -serializeAccount :: forall m av. (MonadBlobStore m, MonadPut m, IsAccountVersion av, AVStructureV0 av) => GlobalContext -> PersistentAccount av -> m () -serializeAccount cryptoParams PersistentAccount{..} = do - PersistingAccountData{..} <- refLoad _persistingData - let initialCredId = - credId - ( Map.findWithDefault - (error "Account missing initial credential") - initialCredentialIndex - _accountCredentials - ) - asfExplicitAddress = _accountAddress /= addressFromRegIdRaw initialCredId - -- There is an opportunity for improvement here. There is no need to go - -- through the deserialized key. The way the encryption key is formed is - -- that the first half is the generator, the second half is the credId. - -- So we could just concatenate them. This requires a bit of scaffolding - -- to get the right component out of cryptoParams, so it is not yet - -- done. - asfExplicitEncryptionKey = _accountEncryptionKey /= toRawEncryptionKey (makeEncryptionKey cryptoParams (unsafeCredIdFromRaw initialCredId)) - (asfMultipleCredentials, putCredentials) = case Map.toList _accountCredentials of - [(i, cred)] | i == initialCredentialIndex -> (False, put cred) - _ -> (True, putSafeMapOf put put _accountCredentials) - asfThresholdIsOne = aiThreshold _accountVerificationKeys == 1 - asfHasRemovedCredentials = _accountRemovedCredentials ^. unhashed /= EmptyRemovedCredentials - aea <- refLoad _accountEncryptedAmount - (asfExplicitEncryptedAmount, putEA) <- - putAccountEncryptedAmountV0 aea <&> \case - Nothing -> (False, return ()) - Just p -> (True, p) - arSched <- loadPersistentAccountReleaseSchedule =<< refLoad _accountReleaseSchedule - let asfExplicitReleaseSchedule = arSched /= ARSV0.emptyAccountReleaseSchedule - asfHasBakerOrDelegation = case _accountStake of - PersistentAccountStakeNone -> False - _ -> True - stake <- loadAccountStake _accountStake - liftPut $ do - put AccountSerializationFlags{..} - when asfExplicitAddress $ put _accountAddress - when asfExplicitEncryptionKey $ put _accountEncryptionKey - unless asfThresholdIsOne $ put (aiThreshold _accountVerificationKeys) - putCredentials - when asfHasRemovedCredentials $ put (_accountRemovedCredentials ^. unhashed) - put _accountNonce - put _accountAmount - putEA - when asfExplicitReleaseSchedule $ put arSched - when asfHasBakerOrDelegation $ serializeAccountStake stake - -- ** Conversion -- | Converts an account to a transient (i.e. in memory) account. (Used for testing.) diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV1.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV1.hs index 8e88850ba2..5ac1770145 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV1.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/Account/StructureV1.hs @@ -45,8 +45,6 @@ import qualified Concordium.GlobalState.Persistent.BlockState.AccountReleaseSche import Concordium.GlobalState.Persistent.BlockState.AccountReleaseScheduleV1 import Concordium.ID.Parameters import Concordium.Types.Accounts.Releases -import Concordium.Utils.Serialization -import Concordium.Utils.Serialization.Put import Control.Monad.Trans import qualified Data.Map.Strict as Map @@ -1427,69 +1425,6 @@ migratePersistentAccountFromV0 StateMigrationParametersP4ToP5{} V0.PersistentAcc .. } --- ** Serialization - --- | Serialize an account. The serialization format may depend on the protocol version. --- --- This format allows accounts to be stored in a reduced format by --- eliding (some) data that can be inferred from context, or is --- the default value. Note that there can be multiple representations --- of the same account. -serializeAccount :: (MonadBlobStore m, MonadPut m) => GlobalContext -> PersistentAccount 'AccountV2 -> m () -serializeAccount cryptoParams acc@PersistentAccount{..} = do - let ed = enduringData acc - let PersistingAccountData{..} = persistingData acc - let initialCredId = - credId - ( Map.findWithDefault - (error "Account missing initial credential") - initialCredentialIndex - _accountCredentials - ) - asfExplicitAddress = _accountAddress /= addressFromRegIdRaw initialCredId - -- There is an opportunity for improvement here. There is no need to go - -- through the deserialized key. The way the encryption key is formed is - -- that the first half is the generator, the second half is the credId. - -- So we could just concatenate them. This requires a bit of scaffolding - -- to get the right component out of cryptoParams, so it is not yet - -- done. - asfExplicitEncryptionKey = _accountEncryptionKey /= toRawEncryptionKey (makeEncryptionKey cryptoParams (unsafeCredIdFromRaw initialCredId)) - (asfMultipleCredentials, putCredentials) = case Map.toList _accountCredentials of - [(i, cred)] | i == initialCredentialIndex -> (False, put cred) - _ -> (True, putSafeMapOf put put _accountCredentials) - asfThresholdIsOne = aiThreshold _accountVerificationKeys == 1 - asfHasRemovedCredentials = _accountRemovedCredentials ^. unhashed /= EmptyRemovedCredentials - (asfExplicitEncryptedAmount, putEA) <- do - case paedEncryptedAmount ed of - Null -> return (False, return ()) - Some aeaRef -> do - aea <- refLoad aeaRef - putAccountEncryptedAmountV0 aea <&> \case - Nothing -> (False, return ()) - Just p -> (True, p) - (asfExplicitReleaseSchedule, putRS) <- do - case paedReleaseSchedule ed of - Null -> return (False, return ()) - Some (rsRef, _) -> do - rs <- refLoad rsRef - if isEmptyAccountReleaseSchedule rs - then return (False, return ()) - else (True,) <$> serializeAccountReleaseSchedule rs - let asfHasBakerOrDelegation = hasStake acc - stake <- getStake acc - liftPut $ do - put AccountSerializationFlags{..} - when asfExplicitAddress $ put _accountAddress - when asfExplicitEncryptionKey $ put _accountEncryptionKey - unless asfThresholdIsOne $ put (aiThreshold _accountVerificationKeys) - putCredentials - when asfHasRemovedCredentials $ put (_accountRemovedCredentials ^. unhashed) - put accountNonce - put accountAmount - putEA - putRS - when asfHasBakerOrDelegation $ serializeAccountStake stake - -- ** Conversion -- | Converts an account to a transient (i.e. in memory) account. (Used for testing.) diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/Accounts.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/Accounts.hs index e5bb621e7f..4a98b21c75 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/Accounts.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/Accounts.hs @@ -73,12 +73,10 @@ import Concordium.GlobalState.Persistent.CachedRef import Concordium.GlobalState.Persistent.LFMBTree (LFMBTree', LFMBTreeHash, LFMBTreeHash' (..)) import qualified Concordium.GlobalState.Persistent.LFMBTree as L import qualified Concordium.GlobalState.Persistent.Trie as Trie -import Concordium.ID.Parameters import qualified Concordium.ID.Types as ID import Concordium.Types import Concordium.Types.HashableTo import Concordium.Types.Option (Option (..)) -import Concordium.Utils.Serialization.Put import Control.Monad import Control.Monad.Reader import Data.Foldable (foldlM) @@ -449,12 +447,6 @@ allAccounts accounts = do accountAddresses :: (SupportsPersistentAccount pv m) => Accounts pv -> m [AccountAddress] accountAddresses accounts = map fst <$> allAccounts accounts --- | Serialize accounts in V0 format. -serializeAccounts :: (SupportsPersistentAccount pv m, MonadPut m) => GlobalContext -> Accounts pv -> m () -serializeAccounts cryptoParams Accounts{..} = do - liftPut $ putWord64be $ L.size accountTable - L.mmap_ (serializeAccount cryptoParams) accountTable - -- | Fold over the account table in ascending order of account index. foldAccounts :: (SupportsPersistentAccount pv m) => (a -> PersistentAccount (AccountVersionFor pv) -> m a) -> a -> Accounts pv -> m a foldAccounts f a accts = L.mfold f a (accountTable accts) diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState.hs index d6764145b5..1c0ba20332 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState.hs @@ -373,13 +373,6 @@ freezeContractState cs = case Wasm.getWasmVersion @v of (hsh, persistent) <- liftIO (StateV1.freeze cbk cs) return (hsh, Instances.InstanceStateV1 persistent) --- | Serialize 'PersistentBirkParameters' in V0 format. -putBirkParametersV0 :: forall m pv. (IsProtocolVersion pv, MonadBlobStore m, MonadPut m) => PersistentBirkParameters pv -> m () -putBirkParametersV0 PersistentBirkParameters{..} = withIsSeedStateVersionFor (protocolVersion @pv) $ do - sPut _birkSeedState - putEpochBakers =<< refLoad _birkNextEpochBakers - putEpochBakers =<< refLoad _birkCurrentEpochBakers - instance (IsProtocolVersion pv, MonadBlobStore m) => MHashableTo m H.Hash (PersistentBirkParameters pv) where getHashM PersistentBirkParameters{..} = withIsSeedStateVersionFor (protocolVersion @pv) $ do nextHash <- getHashM _birkNextEpochBakers @@ -547,19 +540,6 @@ consEpochBlock b hebbs = do hebHash = Rewards.epochBlockHash b (hebHash hebbs) } --- | Serialize the 'HashedEpochBlocks' structure in V0 format. -putHashedEpochBlocksV0 :: (MonadBlobStore m, MonadPut m) => HashedEpochBlocks -> m () -putHashedEpochBlocksV0 HashedEpochBlocks{..} = do - ebs <- loadEB Seq.empty hebBlocks - liftPut $ do - putLength (Seq.length ebs) - mapM_ put ebs - where - loadEB s Null = return s - loadEB s (Some ebref) = do - EpochBlock{..} <- refLoad ebref - loadEB (s Seq.|> ebBakerId) ebPrevious - data BlockRewardDetails' (av :: AccountVersion) (bhv :: BlockHashVersion) where BlockRewardDetailsV0 :: !HashedEpochBlocks -> BlockRewardDetails' 'AccountV0 bhv BlockRewardDetailsV1 :: (AVSupportsDelegation av) => !(HashedBufferedRef' (Rewards.PoolRewardsHash bhv) (PoolRewards bhv)) -> BlockRewardDetails' av bhv @@ -638,13 +618,6 @@ instance (MonadBlobStore m, IsBlockHashVersion bhv) => Cacheable m (BlockRewardD cache (BlockRewardDetailsV0 heb) = BlockRewardDetailsV0 <$> cache heb cache (BlockRewardDetailsV1 hpr) = BlockRewardDetailsV1 <$> cache hpr -putBlockRewardDetails :: - (MonadBlobStore m, MonadPut m, IsBlockHashVersion bhv) => - BlockRewardDetails' av bhv -> - m () -putBlockRewardDetails (BlockRewardDetailsV0 heb) = putHashedEpochBlocksV0 heb -putBlockRewardDetails (BlockRewardDetailsV1 hpr) = refLoad hpr >>= putPoolRewards - -- | Extend a 'BlockRewardDetails' ''AccountV0' with an additional baker. consBlockRewardDetails :: (MonadBlobStore m) => @@ -999,32 +972,6 @@ emptyBlockState bspBirkParameters cryptParams keysCollection chainParams = do } liftIO $ newIORef $! bsp --- | Serialize the block state. The format may depend on the protocol version. -putBlockStateV0 :: (SupportsPersistentState pv m, MonadPut m) => PersistentBlockState pv -> m () -putBlockStateV0 pbs = do - BlockStatePointers{..} <- loadPBS pbs - -- BirkParameters - putBirkParametersV0 bspBirkParameters - -- CryptographicParameters - cryptoParams <- refLoad bspCryptographicParameters - sPut cryptoParams - -- IdentityProviders - sPut =<< refLoad bspIdentityProviders - -- AnonymityRevokers - sPut =<< refLoad bspAnonymityRevokers - -- Modules - Modules.putModulesV0 =<< refLoad bspModules - -- BankStatus - sPut $ _unhashed bspBank - -- Accounts - Accounts.serializeAccounts cryptoParams bspAccounts - -- Instances - Instances.putInstancesV0 bspInstances - -- Updates - putUpdatesV0 =<< refLoad bspUpdates - -- Epoch blocks / pool rewards - putBlockRewardDetails bspRewardDetails - loadPBS :: (SupportsPersistentState pv m) => PersistentBlockState pv -> m (BlockStatePointers pv) loadPBS = loadBufferedRef <=< liftIO . readIORef {-# INLINE loadPBS #-} @@ -3683,10 +3630,6 @@ instance (IsProtocolVersion pv, PersistentState av pv r m) => BlockStateStorage Just hpbsHash -> return HashedPersistentBlockState{..} Nothing -> hashBlockState hpbsPointers - serializeBlockState hpbs = do - p <- runPutT (putBlockStateV0 (hpbsPointers hpbs)) - return $ runPut p - blockStateLoadCallback = asks blobLoadCallback {-# INLINE blockStateLoadCallback #-} diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/AccountReleaseScheduleV1.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/AccountReleaseScheduleV1.hs index 84e778b80d..c686bde263 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/AccountReleaseScheduleV1.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/AccountReleaseScheduleV1.hs @@ -262,23 +262,6 @@ migrateAccountReleaseScheduleFromV0 schedule = do releases <- lift $ refLoad release >>= ARSV0.listRelease addReleases (releases, transactionHash) schedule' --- | Serialize an 'AccountReleaseSchedule' in the serialization format for --- 'TARSV1.AccountReleaseSchedule'. -serializeAccountReleaseSchedule :: forall m. (MonadBlobStore m) => AccountReleaseSchedule -> m Put -serializeAccountReleaseSchedule AccountReleaseSchedule{..} = do - foldlM putEntry putLen arsReleases - where - putLen = putLength (Vector.length arsReleases) - putEntry :: Put -> ReleaseScheduleEntry -> m Put - putEntry put0 ReleaseScheduleEntry{..} = do - Releases{..} <- refLoad rseReleasesRef - let start = fromIntegral rseNextReleaseIndex - return $ do - put0 - put relTransactionHash - putLength (Vector.length relReleases - start) - mapM_ put (Vector.drop start relReleases) - -- | Convert a transient account release schedule to the persistent one. makePersistentAccountReleaseSchedule :: (MonadBlobStore m) => TARSV1.AccountReleaseSchedule -> m AccountReleaseSchedule makePersistentAccountReleaseSchedule tars = do diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/Modules.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/Modules.hs index 21adcb1ad0..047b73164f 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/Modules.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/Modules.hs @@ -27,9 +27,6 @@ module Concordium.GlobalState.Persistent.BlockState.Modules ( moduleRefList, newModuleCache, unsafeToModuleV, - - -- * Serialization - putModulesV0, migrateModules, ) where @@ -45,7 +42,6 @@ import Concordium.Types import Concordium.Types.HashableTo import Concordium.Utils import Concordium.Utils.Serialization -import Concordium.Utils.Serialization.Put import Concordium.Wasm import Control.Monad.Trans import qualified Data.ByteString as BS @@ -245,12 +241,6 @@ instance (MonadBlobStore m) => DirectBlobStorable m Module where mkModule SV0 = ModuleV0 mkModule SV1 = ModuleV1 --- | Serialize a module in V0 format. --- This only serializes the source. -putModuleV0 :: (MonadBlobStore m, MonadPut m) => Module -> m () -putModuleV0 (ModuleV0 ModuleV{..}) = sPut =<< loadRef moduleVSource -putModuleV0 (ModuleV1 ModuleV{..}) = sPut =<< loadRef moduleVSource - -------------------------------------------------------------------------------- -- | A cached 'Module' accessed via a cached 'Reference' i.e., a 'Reference' @@ -385,13 +375,6 @@ moduleRefList mods = Map.keys (mods ^. modulesMap) -------------------------------------------------------------------------------- --- | Serialize modules in V0 format. -putModulesV0 :: (SupportsPersistentModule m, MonadPut m) => Modules -> m () -putModulesV0 mods = do - let mt = mods ^. modulesTable - liftPut $ putWord64be $ LFMB.size mt - LFMB.mmap_ putModuleV0 mt - -- | Migrate smart contract modules from context @m@ to the context @t m@. migrateModules :: forall t m. diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/Updates.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/Updates.hs index ddf35b0d52..7fb2a5b153 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/Updates.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/BlockState/Updates.hs @@ -627,33 +627,6 @@ instance where cpv = chainParametersVersion @cpv --- | Serialize the pending updates. -putPendingUpdatesV0 :: - forall m cpv. - (MonadBlobStore m, MonadPut m, IsChainParametersVersion cpv) => - PendingUpdates cpv -> - m () -putPendingUpdatesV0 PendingUpdates{..} = withCPVConstraints (chainParametersVersion @cpv) $ do - putUpdateQueueV0 =<< refLoad pRootKeysUpdateQueue - putUpdateQueueV0 =<< refLoad pLevel1KeysUpdateQueue - putUpdateQueueV0 =<< refLoad pLevel2KeysUpdateQueue - putUpdateQueueV0 =<< refLoad pProtocolQueue - mapM_ (putUpdateQueueV0 <=< refLoad) pElectionDifficultyQueue - putUpdateQueueV0 =<< refLoad pEuroPerEnergyQueue - putUpdateQueueV0 =<< refLoad pMicroGTUPerEuroQueue - putUpdateQueueV0 =<< refLoad pFoundationAccountQueue - putUpdateQueueV0 =<< refLoad pMintDistributionQueue - putUpdateQueueV0 =<< refLoad pTransactionFeeDistributionQueue - putUpdateQueueV0 =<< refLoad pGASRewardsQueue - putUpdateQueueV0 =<< refLoad pPoolParametersQueue - putUpdateQueueV0 =<< refLoad pAddAnonymityRevokerQueue - putUpdateQueueV0 =<< refLoad pAddIdentityProviderQueue - mapM_ (putUpdateQueueV0 <=< refLoad) pCooldownParametersQueue - mapM_ (putUpdateQueueV0 <=< refLoad) pTimeParametersQueue - mapM_ (putUpdateQueueV0 <=< refLoad) pTimeoutParametersQueue - mapM_ (putUpdateQueueV0 <=< refLoad) pMinBlockTimeQueue - mapM_ (putUpdateQueueV0 <=< refLoad) pBlockEnergyLimitQueue - -- | Initial pending updates with empty queues. emptyPendingUpdates :: forall m cpv. @@ -1694,23 +1667,6 @@ lookupCurrentParameters uref = do Updates{..} <- refLoad uref unStoreSerialized <$> refLoad currentParameters --- | Serialize updates in V0 format. -putUpdatesV0 :: - forall m cpv. - (MonadBlobStore m, MonadPut m, IsChainParametersVersion cpv) => - Updates' cpv -> - m () -putUpdatesV0 Updates{..} = do - withIsAuthorizationsVersionFor (chainParametersVersion @cpv) $ - sPut . unStoreSerialized =<< refLoad currentKeyCollection - case currentProtocolUpdate of - Null -> liftPut $ putWord8 0 - Some pu -> do - liftPut $ putWord8 1 - sPut . unStoreSerialized =<< refLoad pu - sPut . unStoreSerialized =<< refLoad currentParameters - putPendingUpdatesV0 pendingUpdates - -- | Look up the pending changes to the time parameters. lookupPendingTimeParameters :: (MonadBlobStore m, IsChainParametersVersion cpv) => diff --git a/concordium-consensus/src/Concordium/GlobalState/Persistent/Instances.hs b/concordium-consensus/src/Concordium/GlobalState/Persistent/Instances.hs index 249685b88f..ce45fc341a 100644 --- a/concordium-consensus/src/Concordium/GlobalState/Persistent/Instances.hs +++ b/concordium-consensus/src/Concordium/GlobalState/Persistent/Instances.hs @@ -25,8 +25,6 @@ import qualified Concordium.Crypto.SHA256 as H import Concordium.Types import Concordium.Types.HashableTo import Concordium.Utils -import Concordium.Utils.Serialization (putByteStringLen) -import Concordium.Utils.Serialization.Put import qualified Concordium.Wasm as Wasm import Concordium.GlobalState.BlockState ( @@ -367,35 +365,6 @@ mkInstanceInfoV PersistentInstanceV{..} = do .. } --- | Serialize a V0 smart contract instance in V0 format. -putV0InstanceV0 :: (MonadBlobStore m, MonadPut m) => PersistentInstanceV GSWasm.V0 -> m () -putV0InstanceV0 PersistentInstanceV{pinstanceModel = InstanceStateV0 model, ..} = do - -- Instance parameters - PersistentInstanceParameters{..} <- refLoad pinstanceParameters - liftPut $ do - -- only put the subindex part of the address - put (contractSubindex pinstanceAddress) - put pinstanceOwner - put pinstanceContractModule - put pinstanceInitName - put model - put pinstanceAmount - --- | Serialize a V1 smart contract instance in V0 format. -putV1InstanceV0 :: (MonadBlobStore m, MonadPut m) => PersistentInstanceV GSWasm.V1 -> m () -putV1InstanceV0 PersistentInstanceV{pinstanceModel = InstanceStateV1 model, ..} = do - -- Instance parameters - PersistentInstanceParameters{..} <- refLoad pinstanceParameters - stateString <- StateV1.toByteString model - liftPut $ do - -- only put the subindex part of the address - put (contractSubindex pinstanceAddress) - put pinstanceOwner - put pinstanceContractModule - put pinstanceInitName - putByteStringLen stateString -- serialize with explicit length to enable serialization via FFI. - put pinstanceAmount - ---------------------------------------------------------------------------------------------------- -- | An alias to document when a hash is intended to be the parameter hash. @@ -781,20 +750,3 @@ allInstances (InstancesTree _ it) = mapReduceIT mfun it where mfun (Left _) = return mempty mfun (Right inst) = (: []) . pinstanceAddress <$> loadInstanceParameters inst - --- | Serialize instances in V0 format. -putInstancesV0 :: (IsProtocolVersion pv, SupportsPersistentModule m, MonadPut m) => Instances pv -> m () -putInstancesV0 InstancesEmpty = liftPut $ putWord8 0 -putInstancesV0 (InstancesTree _ it) = do - mapReduceIT putOptInstance it - liftPut $ putWord8 0 - where - putOptInstance (Left ca) = liftPut $ do - putWord8 1 - put (contractSubindex ca) - putOptInstance (Right (PersistentInstanceV0 inst)) = do - liftPut $ putWord8 2 - putV0InstanceV0 inst - putOptInstance (Right (PersistentInstanceV1 inst)) = do - liftPut $ putWord8 3 - putV1InstanceV0 inst diff --git a/concordium-consensus/tests/globalstate/Basic/Accounts.hs b/concordium-consensus/tests/globalstate/Basic/Accounts.hs index 2a0769f548..c6be28c71b 100644 --- a/concordium-consensus/tests/globalstate/Basic/Accounts.hs +++ b/concordium-consensus/tests/globalstate/Basic/Accounts.hs @@ -7,22 +7,17 @@ module Basic.Accounts where -import Concordium.Genesis.Data import qualified Concordium.GlobalState.AccountMap as AccountMap import Concordium.GlobalState.Basic.BlockState.Account import Concordium.GlobalState.Basic.BlockState.AccountReleaseSchedule import qualified Concordium.GlobalState.Basic.BlockState.LFMBTree as LFMBTree import Concordium.GlobalState.BlockState (AccountsHash (..)) -import Concordium.ID.Parameters import qualified Concordium.ID.Types as ID import Concordium.Types -import Concordium.Types.Accounts import Concordium.Types.HashableTo -import Control.Monad import Data.Foldable import qualified Data.Map.Strict as Map import qualified Data.Sequence as Seq -import Data.Serialize import GHC.Stack (HasCallStack) import Lens.Micro.Internal (Index, IxValue, Ixed) import Lens.Micro.Platform @@ -216,45 +211,3 @@ accountList = zip [0 ..] . toList . accountTable -- | Fold over the account table in ascending order of account index. foldAccounts :: (a -> Account (AccountVersionFor pv) -> a) -> a -> Accounts pv -> a foldAccounts f a = foldl' f a . accountTable - --- | Serialize 'Accounts' in V0 format. -serializeAccounts :: (IsProtocolVersion pv) => GlobalContext -> Putter (Accounts pv) -serializeAccounts cryptoParams Accounts{..} = do - putWord64be $ fromIntegral $ Seq.length accountTable - forM_ accountTable $ \acct -> serializeAccount cryptoParams acct - --- | Deserialize 'Accounts'. The serialization format may depend on the protocol version. --- The state migration determines how do construct an 'Accounts' at a new protocol version 'pv' --- from the serialization format of another protocol version 'oldpv'. --- This validates the following invariants: --- --- * Every baker account's 'BakerId' must match the account index. --- * 'CredentialRegistrationID's must not be used on more than one account. -deserializeAccounts :: (IsProtocolVersion oldpv, IsProtocolVersion pv) => StateMigrationParameters oldpv pv -> GlobalContext -> Get (Accounts pv) -deserializeAccounts migration cryptoParams = do - nAccounts <- getWord64be - let loop i accts@Accounts{..} - | i < nAccounts = do - acct <- deserializeAccount migration cryptoParams - let acctId = AccountIndex i - case _accountStaking acct of - AccountStakeBaker bkr -> - unless (bkr ^. accountBakerInfo . bakerIdentity == BakerId acctId) $ - fail "BakerID does not match account index" - _ -> return () - let addRegId regids cred - | cred `Map.member` regids = fail "Duplicate credential" - | otherwise = return $ Map.insert cred acctId regids - newRegIds <- - foldM addRegId accountRegIds $ - (ID.credId <$> Map.elems (acct ^. accountCredentials)) - ++ removedCredentialsToList (acct ^. accountRemovedCredentials . unhashed) - loop - (i + 1) - Accounts - { accountMap = AccountMap.insertPure (acct ^. accountAddress) acctId accountMap, - accountTable = accountTable Seq.|> acct, - accountRegIds = newRegIds - } - | otherwise = return accts - loop 0 emptyAccounts From a7faf4ec6d813c70aa29b6571d8699d8aa5a7932 Mon Sep 17 00:00:00 2001 From: Thomas Dinsdale-Young Date: Wed, 13 Mar 2024 11:05:10 +0100 Subject: [PATCH 2/2] Update base --- concordium-base | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concordium-base b/concordium-base index 9fa4e270d3..825fab04fb 160000 --- a/concordium-base +++ b/concordium-base @@ -1 +1 @@ -Subproject commit 9fa4e270d3f3928c753fcc2e2982c736d1d29226 +Subproject commit 825fab04fb78445c9f1cdcaf4cb9dae33f3b831a