Skip to content

Commit

Permalink
use Map instead of list for parameter passing
Browse files Browse the repository at this point in the history
  • Loading branch information
drsk committed Nov 7, 2024
1 parent feee3a8 commit 3ec26b1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3469,7 +3469,7 @@ doUpdateMissedRounds ::
SupportsPersistentState pv m
) =>
PersistentBlockState pv ->
[(BakerId, Word64)] ->
Map.Map BakerId Word64 ->
m (PersistentBlockState pv)
doUpdateMissedRounds pbs rds = do
bsp <- loadPBS pbs
Expand All @@ -3482,7 +3482,7 @@ doUpdateMissedRounds pbs rds = do
(\bprd -> bprd{missedRounds = (+ newMissedRounds) <$> missedRounds bprd})
)
bsp
rds
(Map.toList rds)
storePBS pbs bsp'

doProcessUpdateQueues ::
Expand Down
4 changes: 2 additions & 2 deletions concordium-consensus/src/Concordium/KonsensusV1/Scheduler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ processBlockRewards ::
-- | Transaction fees and number of "free" transactions.
TransactionRewardParameters ->
-- | Number of missed rounds per validator.
[(BakerId, Word64)] ->
Map.Map BakerId Word64 ->
-- | Block state.
UpdatableBlockState m ->
m (UpdatableBlockState m)
Expand Down Expand Up @@ -416,7 +416,7 @@ executeBlockEpilogue ::
ParticipatingBakers ->
Maybe (PaydayParameters (AccountVersionFor (MPV m))) ->
TransactionRewardParameters ->
[(BakerId, Word64)] ->
Map.Map BakerId Word64 ->
UpdatableBlockState m ->
m (PBS.HashedPersistentBlockState pv)
executeBlockEpilogue participants paydayParams transactionRewardParams missedRounds theState0 = do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
-- | Tests for the functions in 'Concordium.KonsensusV1.LeaderElection'.
module ConcordiumTests.KonsensusV1.LeaderElectionTest (tests) where

import qualified Data.Map.Strict as Map
import Data.Serialize
import Data.Word
import qualified Data.Vector as Vec
import qualified Data.Map.Strict as Map
import Data.Word
import System.Random
import Test.HUnit
import Test.Hspec
Expand Down Expand Up @@ -60,14 +60,14 @@ dummyFullBakers =

-- | A dummy `TimeoutCertificate` used for testing.
dummyTimeoutCertificate :: Word64 -> TimeoutCertificate
dummyTimeoutCertificate r =
TimeoutCertificate
{ tcRound = Round r,
tcMinEpoch = 0,
tcFinalizerQCRoundsFirstEpoch = FinalizerRounds Map.empty,
tcFinalizerQCRoundsSecondEpoch = FinalizerRounds Map.empty,
tcAggregateSignature = mempty
}
dummyTimeoutCertificate r =
TimeoutCertificate
{ tcRound = Round r,
tcMinEpoch = 0,
tcFinalizerQCRoundsFirstEpoch = FinalizerRounds Map.empty,
tcFinalizerQCRoundsSecondEpoch = FinalizerRounds Map.empty,
tcAggregateSignature = mempty
}

-- | Serialization test for FullBakers.
-- Note that we are never deserializing the full bakers in practice,
Expand Down Expand Up @@ -156,46 +156,54 @@ testUpdateSeedStateForEpoch =
ss1ShutdownTriggered = False
}

testComputeMissedRounds :: forall pv. (IsProtocolVersion pv) => SProtocolVersion pv -> Spec
testComputeMissedRounds _spv =
testComputeMissedRounds :: Spec
testComputeMissedRounds =
describe "computeMissedRounds" $ do
it "no timeout" $
computeMissedRounds
Absent
dummyFullBakers
(read "ba3aba3b6c31fb6b0251a19c83666cd90da9a0835a2b54dc4f01c6d451ab24e8")
(dummyBlock @pv 5)
6
( Map.toList $
computeMissedRounds
Absent
dummyFullBakers
(read "ba3aba3b6c31fb6b0251a19c83666cd90da9a0835a2b54dc4f01c6d451ab24e8")
5
6
)
`shouldBe` []
it "timeout present, 1 missed round" $
computeMissedRounds
(Present $ dummyTimeoutCertificate 5)
dummyFullBakers
(read "ba3aba3b6c31fb6b0251a19c83666cd90da9a0835a2b54dc4f01c6d451ab24e8")
(dummyBlock @pv 5)
6
`shouldBe` [(1,1)]
it "timeout present, 3 missed rounds" $
computeMissedRounds
(Present $ dummyTimeoutCertificate 5)
dummyFullBakers
(read "ba3aba3b6c31fb6b0251a19c83666cd90da9a0835a2b54dc4f01c6d451ab24e8")
(dummyBlock @pv 5)
8
`shouldBe` [(1,2), (2,1)]
it "timeout present, 95 missed rounds" $
computeMissedRounds
(Present $ dummyTimeoutCertificate 5)
dummyFullBakers
(read "ba3aba3b6c31fb6b0251a19c83666cd90da9a0835a2b54dc4f01c6d451ab24e8")
(dummyBlock @pv 5)
100
`shouldBe` [(1,46), (2,49)]
it "timeout present, 1 missed round" $
( Map.toList $
computeMissedRounds
(Present $ dummyTimeoutCertificate 5)
dummyFullBakers
(read "ba3aba3b6c31fb6b0251a19c83666cd90da9a0835a2b54dc4f01c6d451ab24e8")
5
6
)
`shouldBe` [(1, 1)]
it "timeout present, 3 missed rounds" $
( Map.toList $
computeMissedRounds
(Present $ dummyTimeoutCertificate 5)
dummyFullBakers
(read "ba3aba3b6c31fb6b0251a19c83666cd90da9a0835a2b54dc4f01c6d451ab24e8")
5
8
)
`shouldBe` [(1, 2), (2, 1)]
it "timeout present, 95 missed rounds" $
( Map.toList $
computeMissedRounds
(Present $ dummyTimeoutCertificate 5)
dummyFullBakers
(read "ba3aba3b6c31fb6b0251a19c83666cd90da9a0835a2b54dc4f01c6d451ab24e8")
5
100
)
`shouldBe` [(1, 46), (2, 49)]

tests :: Spec
tests = describe "KonsensusV1.LeadershipElection" $ do
testGetLeader
testUpdateSeedStateForBlock
testUpdateSeedStateForEpoch
serializeDeserializeFullBakers
testComputeMissedRounds SP8
testComputeMissedRounds

0 comments on commit 3ec26b1

Please sign in to comment.