From 67b866a4ad244ff52e7cd34f410be59a93e2dfa1 Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Wed, 24 Nov 2021 22:08:55 +0300 Subject: [PATCH 1/2] Fix generation of invalid `collateralPercent` --- libs/cardano-ledger-test/src/Test/Cardano/Ledger/Properties.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Properties.hs b/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Properties.hs index 3e5f611a15c..1088a34a38e 100644 --- a/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Properties.hs +++ b/libs/cardano-ledger-test/src/Test/Cardano/Ledger/Properties.hs @@ -766,7 +766,7 @@ genTxAndLEDGERState = do txIx <- arbitrary maxTxExUnits <- arbitrary Positive maxCollateralInputs <- arbitrary - collateralPercentage <- fromIntegral <$> chooseInt (0, 10000) + collateralPercentage <- fromIntegral <$> chooseInt (1, 10000) minfeeA <- fromIntegral <$> chooseInt (0, 1000) minfeeB <- fromIntegral <$> chooseInt (0, 10000) let genT = do From 7ef96a683bc0ce6ab1be25ac0ced5c212dea90a7 Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Wed, 24 Nov 2021 22:21:08 +0300 Subject: [PATCH 2/2] Fix assumption that `BiMap` is indeed encoded in ascending order and has no duplicates --- libs/small-steps/src/Control/Iterate/SetAlgebra.hs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/libs/small-steps/src/Control/Iterate/SetAlgebra.hs b/libs/small-steps/src/Control/Iterate/SetAlgebra.hs index 950b60a7be9..4dfd3800d90 100644 --- a/libs/small-steps/src/Control/Iterate/SetAlgebra.hs +++ b/libs/small-steps/src/Control/Iterate/SetAlgebra.hs @@ -7,6 +7,7 @@ {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} @@ -17,6 +18,7 @@ module Control.Iterate.SetAlgebra where import Cardano.Binary ( Decoder, + DecoderError (DecoderErrorCustom), FromCBOR (..), ToCBOR (..), decodeListLen, @@ -26,8 +28,8 @@ import Cardano.Binary import Codec.CBOR.Encoding (encodeListLen) import Control.DeepSeq (NFData (rnf)) import Control.Iterate.Collect -import Control.Monad (void) -import Data.Coders (invalidKey) +import Control.Monad (unless, void) +import Data.Coders (cborError, invalidKey) import Data.List (sortBy) import qualified Data.List as List import Data.Map.Internal (Map (..), link, link2) @@ -179,7 +181,11 @@ instance decodeMapAsBimap :: (FromCBOR a, FromCBOR b, Ord a, Ord b) => Decoder s (BiMap b a b) -decodeMapAsBimap = decodeMapSkel biMapFromAscDistinctList +decodeMapAsBimap = do + bimap@(MkBiMap mf mb) <- decodeMapSkel biMapFromAscDistinctList + unless (Map.valid mf && Map.valid mb) $ + cborError $ DecoderErrorCustom "BiMap" "Expected distinct keys in ascending order" + pure bimap instance (NoThunks a, NoThunks b) => NoThunks (BiMap v a b) where showTypeOf _ = "BiMap" @@ -236,6 +242,8 @@ biMapFromList comb xs = foldr addEntry biMapEmpty xs where newv = comb oldv v +-- | /Warning/ - invariant that keys are distinct and in ascending order is not +-- checked. Make sure it is not violated, otherwise crazy things will happen. biMapFromAscDistinctList :: (Ord k, Ord v) => [(k, v)] -> BiMap v k v biMapFromAscDistinctList xs = MkBiMap bmForward bmBackward