Skip to content

Commit

Permalink
Added some strictness.
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkywayPirate committed Oct 30, 2023
1 parent 372a0af commit 0931e7a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ data DifferenceMap = DifferenceMap

-- | Gather all accounts from the provided 'DifferenceMap' and its parent maps.
-- Accounts are returned in ascending order of their 'AccountIndex'.
flatten :: DifferenceMap -> Map.Map AccountAddress AccountIndex
flatten dmap = go (Just dmap) Map.empty
flatten :: DifferenceMap -> [(AccountAddress, AccountIndex)]
flatten dmap = go dmap []
where
go :: Maybe DifferenceMap -> Map.Map AccountAddress AccountIndex -> Map.Map AccountAddress AccountIndex
go Nothing accum = accum
go (Just DifferenceMap{..}) !accum = go dmParentMap $ dmAccounts `Map.union` accum
go :: DifferenceMap -> [(AccountAddress, AccountIndex)] -> [(AccountAddress, AccountIndex)]
go DifferenceMap{dmParentMap = Nothing, ..} !accum =
let !listOfAccounts = Map.toList dmAccounts
in listOfAccounts <> accum
go DifferenceMap{dmParentMap = Just parentMap, ..} !accum =
let !listOfAccounts = Map.toList dmAccounts
in go parentMap $! listOfAccounts <> accum

-- | Create a new empty 'DifferenceMap' based on the difference map of
-- the parent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ instance
doInsert dbh txn accounts = do
forM_ accounts $ \(accAddr, accIndex) -> do
storeRecord txn (dbh ^. accountMapStore) accAddr accIndex
return $ Just accIndex

lookup a@(AccountAddress accAddr) = asReadTransaction $ \dbh txn ->
withCursor txn (dbh ^. accountMapStore) $ \cursor -> do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ instance (SupportsPersistentAccount pv m) => BlobStorable m (Accounts pv) where
(pRegIdHistory, regIdHistory') <- storeUpdate accountRegIdHistory
case accountDiffMap of
Nothing -> return ()
Just accDiffMap -> LMDBAccountMap.insert (Map.toList $ DiffMap.flatten accDiffMap)
Just accDiffMap -> LMDBAccountMap.insert $! DiffMap.flatten accDiffMap
let newAccounts =
Accounts
{ accountTable = accountTable',
Expand Down Expand Up @@ -342,10 +342,10 @@ updateAccountsAtIndex' fupd ai = fmap snd . updateAccountsAtIndex fupd' ai
-- | Get a list of all account addresses and their assoicated account indices.
allAccounts :: (SupportsPersistentAccount pv m) => Accounts pv -> m [(AccountAddress, AccountIndex)]
allAccounts accounts = do
persistedAccs <- Map.fromList <$> LMDBAccountMap.all
persistedAccs <- LMDBAccountMap.all
case accountDiffMap accounts of
Nothing -> return $ Map.toList persistedAccs
Just accDiffMap -> return $ Map.toList $ persistedAccs `Map.union` DiffMap.flatten accDiffMap
Nothing -> return persistedAccs
Just accDiffMap -> return $! persistedAccs <> DiffMap.flatten accDiffMap

-- | Get a list of all account addresses.
accountAddresses :: (SupportsPersistentAccount pv m) => Accounts pv -> m [AccountAddress]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module GlobalStateTests.DifferenceMap where

import Concordium.ID.Types (randomAccountAddress)
import Concordium.Types
import qualified Data.Map.Strict as Map
import System.Random

import qualified Concordium.GlobalState.AccountMap.DifferenceMap as DiffMap
Expand Down Expand Up @@ -53,7 +52,7 @@ testFlatten = do
let diffMap1 = uncurry DiffMap.insert (dummyPair 1) $ DiffMap.empty Nothing
diffMap2 = uncurry DiffMap.insert (dummyPair 2) (DiffMap.empty $ Just diffMap1)
diffMap3 = uncurry DiffMap.insert (dummyPair 3) (DiffMap.empty $ Just diffMap2)
assertEqual "accounts should be the same" (Map.fromList (map dummyPair [1 .. 3])) $ DiffMap.flatten diffMap3
assertEqual "accounts should be the same" (map dummyPair [1 .. 3]) $ DiffMap.flatten diffMap3

tests :: Spec
tests = describe "AccountMap.DifferenceMap" $ do
Expand Down

0 comments on commit 0931e7a

Please sign in to comment.