@@ -25,7 +25,7 @@ import Concordium.Types.Updates
25
25
import Concordium.Utils
26
26
27
27
import qualified Concordium.GlobalState.AccountMap.DifferenceMap as DiffMap
28
- import Concordium.GlobalState.BlockState
28
+ import Concordium.GlobalState.BlockState as BlockState
29
29
import Concordium.GlobalState.Parameters hiding (getChainParameters )
30
30
import qualified Concordium.GlobalState.Persistent.BlockState as PBS
31
31
import qualified Concordium.GlobalState.Statistics as Stats
@@ -436,6 +436,11 @@ loadCertifiedBlocks = do
436
436
_ -> Nothing
437
437
loadCertBlock (storedBlock, qc) loadedBlocks = do
438
438
blockPointer <- mkBlockPointer storedBlock
439
+ let bh = getHash @ BlockHash blockPointer
440
+ -- Cache the block state first, as this is required for reconstructing the account
441
+ -- difference map.
442
+ cacheBlockState (bpState blockPointer)
443
+
439
444
-- As only finalized accounts are stored in the account map, then
440
445
-- we need to reconstruct the 'DiffMap.DifferenceMap' here for the certified block we're loading.
441
446
let accountsToInsert = mapMaybe getAccountAddressFromDeployment (blockTransactions storedBlock)
@@ -456,7 +461,29 @@ loadCertifiedBlocks = do
456
461
-- append to the accumulator with this new difference map reference
457
462
let loadedBlocks' = HM. insert (getHash storedBlock) newDifferenceMap loadedBlocks
458
463
459
- cacheBlockState (bpState blockPointer)
464
+ -- Validate that the 'accountsToInsert' are now accessible.
465
+ -- This should never fail, but it is worth verifying here since there are likely to be
466
+ -- few such accounts and it is better to catch any bug here than end up with a corrupted
467
+ -- account map.
468
+ forM_ accountsToInsert $ \ addr -> do
469
+ BlockState. getAccount (bpState blockPointer) addr >>= \ case
470
+ Nothing ->
471
+ throwM . TreeStateInvariantViolation $
472
+ " Account "
473
+ ++ show addr
474
+ ++ " not found after loading certified block "
475
+ ++ show bh
476
+ Just (_, acc) -> do
477
+ actualAddr <- getAccountCanonicalAddress acc
478
+ unless (actualAddr == addr) $
479
+ throwM . TreeStateInvariantViolation $
480
+ " Account address mismatch ("
481
+ ++ show actualAddr
482
+ ++ " != "
483
+ ++ show addr
484
+ ++ " ) after loading certified block "
485
+ ++ show bh
486
+
460
487
blockTable . liveMap . at' (getHash blockPointer) ?=! blockPointer
461
488
addToBranches blockPointer
462
489
forM_ (blockTransactions blockPointer) $ \ tr -> do
0 commit comments