Skip to content

Commit

Permalink
Cache the hash of finalizers used when computing block result hash
Browse files Browse the repository at this point in the history
  • Loading branch information
limemloh committed Dec 11, 2023
1 parent aede8c2 commit 9088111
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
9 changes: 6 additions & 3 deletions concordium-consensus/src/Concordium/KonsensusV1/Consensus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,17 @@ computeFinalizationCommittee FullBakers{..} FinalizationCommitteeParameters{..}
committeeFinalizers = Vec.fromList $ zipWith mkFinalizer [FinalizerIndex 0 ..] sortedFinalizers
committeeTotalWeight = sum $ finalizerWeight <$> committeeFinalizers

-- | Compute the finalization committee given the bakers and the finalization committee parameters,
-- returning a 'BakersAndFinalizers'.
-- | Compute the finalization committee and finalization committee hash given the bakers and the
-- finalization committee parameters, returning a 'BakersAndFinalizers'.
computeBakersAndFinalizers :: FullBakers -> FinalizationCommitteeParameters -> BakersAndFinalizers
computeBakersAndFinalizers bakers fcp =
BakersAndFinalizers
{ _bfBakers = bakers,
_bfFinalizers = computeFinalizationCommittee bakers fcp
_bfFinalizers = finalizers,
_bfFinalizerHash = computeFinalizationCommitteeHash finalizers
}
where
finalizers = computeFinalizationCommittee bakers fcp

-- | Get the baker identity and finalizer info if we are a finalizer in the specified epoch.
-- This checks that the signing key and aggregate signing key match those for the finalizer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1334,21 +1334,19 @@ computeBlockResultHash ::
computeBlockResultHash newState relativeBlockHeight = do
theBlockStateHash <- getStateHash newState
transactionOutcomesHash <- getTransactionOutcomesHash newState
currentFinalizationCommitteeHash <- do
finalizationCommittee <- use (to bakersForCurrentEpoch . bfFinalizers)
return $ computeFinalizationCommitteeHash finalizationCommittee
currentFinalizationCommitteeHash <- use $ to bakersForCurrentEpoch . bfFinalizerHash
nextFinalizationCommitteeHash <- do
-- Attempt to get the finalization committee from SkovData otherwise compute it from the
-- information in the block state.
currentEpoch <- use (roundStatus . rsCurrentEpoch)
nextSkovBakersAndFinalizers <- gets (getBakersForEpoch (currentEpoch + 1))
nextFinalizationCommittee <- case nextSkovBakersAndFinalizers of
Just bakersAndFinalizers -> return $ bakersAndFinalizers ^. bfFinalizers
case nextSkovBakersAndFinalizers of
Just bakersAndFinalizers -> return $ bakersAndFinalizers ^. bfFinalizerHash
Nothing -> do
nextFullBakers <- getNextEpochBakers newState
nextFinalizationParameters <- getNextEpochFinalizationCommitteeParameters newState
return $ computeFinalizationCommittee nextFullBakers nextFinalizationParameters
return $ computeFinalizationCommitteeHash nextFinalizationCommittee
let nextFinalizationCommittee = computeFinalizationCommittee nextFullBakers nextFinalizationParameters
return $ computeFinalizationCommitteeHash nextFinalizationCommittee
blockHeightInfo <- do
genesisBlockHeightInfo <- use genesisBlockHeight
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ data BakersAndFinalizers = BakersAndFinalizers
{ -- | Bakers set.
_bfBakers :: !FullBakers,
-- | Finalizers set.
_bfFinalizers :: !FinalizationCommittee
_bfFinalizers :: !FinalizationCommittee,
-- | Hash computed from the BLS verify key and weight of each finalizer included in the set above.
_bfFinalizerHash :: !FinalizationCommitteeHash
}
deriving (Eq, Show)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ testMakeQuorumCertificate = describe "Quorum Certificate creation" $ do
fi fIdx = FinalizerInfo (FinalizerIndex fIdx) 1 sigPublicKey vrfPublicKey blsPublicKey (BakerId $ AccountIndex (fromIntegral fIdx))
blsPublicKey = Bls.derivePublicKey someBlsSecretKey
vrfPublicKey = VRF.publicKey someVRFKeyPair
finalizers = FinalizationCommittee (Vec.fromList $ fi <$> [1, 2, 3]) 3
bfs =
BakersAndFinalizers
{ _bfBakers = FullBakers Vec.empty 0,
_bfFinalizers = FinalizationCommittee (Vec.fromList $ fi <$> [1, 2, 3]) 3
_bfFinalizers = finalizers,
_bfFinalizerHash = computeFinalizationCommitteeHash finalizers
}
-- A skov data not capable of forming a quorum certificate
sd =
Expand Down Expand Up @@ -179,7 +181,12 @@ testReceiveQuorumMessage = describe "Receive quorum message" $ do
fi fIdx = FinalizerInfo (FinalizerIndex fIdx) 1 sigPublicKey vrfPublicKey blsPublicKey (BakerId $ AccountIndex (fromIntegral fIdx))
blsPublicKey = Bls.derivePublicKey someBlsSecretKey
vrfPublicKey = VRF.publicKey someVRFKeyPair
bakersAndFinalizers = BakersAndFinalizers (FullBakers Vec.empty 0) (FinalizationCommittee (Vec.fromList [fi 0, fi 1, fi 2, fi 3]) 4)
finalizers = FinalizationCommittee (Vec.fromList [fi 0, fi 1, fi 2, fi 3]) 4
bakersAndFinalizers =
BakersAndFinalizers
(FullBakers Vec.empty 0)
finalizers
(computeFinalizationCommitteeHash finalizers)
-- A skov data where
-- - the current round and epoch is set to 1 (next payday is at epoch 2).
-- - there are 3 finalizers 1, 2 and 3 each with a weight of 1 (total weight is 3).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,11 @@ testReceiveTimeoutMessage = describe "Receive timeout message" $ do
-- COnstruct the finalization committee
finalizers = FinalizationCommittee (Vec.fromList [fi 0, fi 1, fi 2]) 3
-- Construct a set of 0 bakers and 3 finalisers with indices 0,1,2.
bakersAndFinalizers = BakersAndFinalizers (FullBakers Vec.empty 0) finalizers
bakersAndFinalizers =
BakersAndFinalizers
(FullBakers Vec.empty 0)
finalizers
(computeFinalizationCommitteeHash finalizers)
-- A tree state where the following applies:
-- - Current round is 2
-- - Current epoch is 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ dummyBakersAndFinalizers :: BakersAndFinalizers
dummyBakersAndFinalizers =
BakersAndFinalizers
{ _bfBakers = FullBakers Vec.empty 0,
_bfFinalizers = FinalizationCommittee Vec.empty 0
_bfFinalizers = finalizers,
_bfFinalizerHash = computeFinalizationCommitteeHash finalizers
}
where
finalizers = FinalizationCommittee Vec.empty 0

-- | A valid 'AccountCreation' with expiry 1596409020
validAccountCreation :: AccountCreation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ dummyBakersAndFinalizers :: BakersAndFinalizers
dummyBakersAndFinalizers =
BakersAndFinalizers
{ _bfBakers = FullBakers Vec.empty 0,
_bfFinalizers = FinalizationCommittee Vec.empty 0
_bfFinalizers = finalizers,
_bfFinalizerHash = computeFinalizationCommitteeHash finalizers
}
where
finalizers = FinalizationCommittee Vec.empty 0

-- | Dummy epoch bakers. This is only suitable for when the actual value is not meaningfully used.
dummyEpochBakers :: EpochBakers
Expand Down

0 comments on commit 9088111

Please sign in to comment.