Skip to content

Commit

Permalink
More compact encoding of the overlay schedule
Browse files Browse the repository at this point in the history
Fixes #1374.
  • Loading branch information
mrBliss committed Apr 17, 2020
1 parent b0f4b02 commit 1f6fbe3
Showing 1 changed file with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ import Cardano.Crypto.Hash (byteCount)
import Cardano.Prelude (NoUnexpectedThunks (..))
import Control.Monad.Trans.Reader (ReaderT (..), asks)
import Data.Foldable (toList)
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NonEmpty
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Proxy (Proxy (..))
Expand Down Expand Up @@ -395,7 +397,7 @@ instance Crypto crypto => FromCBOR (UTxOState crypto)
data OBftSlot crypto =
NonActiveSlot
| ActiveSlot !(GenKeyHash crypto)
deriving (Show, Eq, Generic)
deriving (Show, Eq, Ord, Generic)

instance Crypto crypto
=> ToCBOR (OBftSlot crypto)
Expand Down Expand Up @@ -433,7 +435,7 @@ instance Crypto crypto => ToCBOR (NewEpochState crypto)
where
toCBOR (NewEpochState e bp bc es ru pd os) =
encodeListLen 7 <> toCBOR e <> toCBOR bp <> toCBOR bc <> toCBOR es
<> toCBOR ru <> toCBOR pd <> toCBOR os
<> toCBOR ru <> toCBOR pd <> toCBOR (compactOverlaySchedule os)

instance Crypto crypto => FromCBOR (NewEpochState crypto)
where
Expand All @@ -445,9 +447,33 @@ instance Crypto crypto => FromCBOR (NewEpochState crypto)
es <- fromCBOR
ru <- fromCBOR
pd <- fromCBOR
os <- fromCBOR
os <- decompactOverlaySchedule <$> fromCBOR
pure $ NewEpochState e bp bc es ru pd os

-- | Convert the overlay schedule to a representation that is more compact
-- when serialised to a bytestring, but less efficient for lookups.
--
-- Each genesis key hash will only be stored once, instead of each time it is
-- assigned to a slot.
compactOverlaySchedule
:: Map SlotNo (OBftSlot crypto)
-> Map (OBftSlot crypto) (NonEmpty SlotNo)
compactOverlaySchedule =
Map.foldrWithKey'
(\slot obftSlot ->
Map.insertWith (<>) obftSlot (pure slot))
Map.empty

-- | Inverse of 'compactOverlaySchedule'
decompactOverlaySchedule
:: Map (OBftSlot crypto) (NonEmpty SlotNo)
-> Map SlotNo (OBftSlot crypto)
decompactOverlaySchedule compact = Map.fromList
[ (slot, obftSlot)
| (obftSlot, slots) <- Map.toList compact
, slot <- NonEmpty.toList slots
]

getGKeys
:: NewEpochState crypto
-> Set (GenKeyHash crypto)
Expand Down

0 comments on commit 1f6fbe3

Please sign in to comment.