Skip to content

Commit db6a3c3

Browse files
committed
Rebase
1 parent 8c35c76 commit db6a3c3

File tree

8 files changed

+63
-70
lines changed

8 files changed

+63
-70
lines changed

cabal.project

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ source-repository-package
1818
-- MAKE SURE THIS POINTS TO A COMMIT IN `*-artifacts` BEFORE MERGE!
1919
tag: 5ed76f93a6c71ef7450d540d5a3ca836b67e5253
2020

21-
source-repository-package
22-
type: git
23-
location: https://github.com/input-output-hk/cuddle.git
24-
tag: 65c64867e4d4cdace105cbd1a5097701db2c5bac
25-
--sha256: sha256-TS1qbTwLrJ+OQq/f+FqnmindvANnQf8mZbAqFDk5Kuo=
26-
2721
source-repository-package
2822
type: git
2923
location: https://github.com/input-output-hk/constrained-generators.git
@@ -108,10 +102,10 @@ if impl(ghc >=9.12)
108102

109103
source-repository-package
110104
type: git
111-
location: https://github.com/Soupstraw/cardano-cls.git
105+
location: https://github.com/tweag/cardano-cls.git
112106
subdir: merkle-tree-incremental mempack-scls scls-cbor scls-cardano scls-format scls-core
113-
--sha256: sha256-POBr11BhEN6SFfc70ZD5saGBanyH/F2Is1TK7+xbk4s=
114-
tag: c8414c077b8efd1a8818b147cc33b42504779e4e
107+
--sha256: sha256-HqkjvqBwo8zZpKHZY7R21OVhP7VQtfoBwK0vawZtNfg=
108+
tag: 98b29d8a866d4dd6658682e285a3d49c17039d0d
115109

116110
if impl(ghc >=9.14)
117111
source-repository-package

eras/dijkstra/impl/test/Test/Cardano/Ledger/Dijkstra/Binary/CddlSpec.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Cardano.Ledger.Conway.Governance (GovAction, ProposalProcedure, VotingPro
1313
import Cardano.Ledger.Core
1414
import Cardano.Ledger.Dijkstra (DijkstraEra)
1515
import Cardano.Ledger.Dijkstra.HuddleSpec (dijkstraCDDL)
16+
import Cardano.Ledger.Dijkstra.Scripts (AccountBalanceInterval, AccountBalanceIntervals)
1617
import Cardano.Ledger.Plutus.Data (Data, Datum)
1718
import Test.Cardano.Ledger.Binary.Cuddle (
1819
huddleDecoderEquivalenceSpec,
@@ -30,6 +31,9 @@ spec = do
3031
describe "CDDL" $ do
3132
let v = eraProtVerHigh @DijkstraEra
3233
describe "Huddle" $ specWithHuddle dijkstraCDDL $ do
34+
huddleRoundTripCborSpec @(AccountBalanceInterval DijkstraEra) v "account_balance_interval"
35+
huddleRoundTripCborSpec @(AccountBalanceIntervals DijkstraEra) v "account_balance_intervals"
36+
huddleRoundTripArbitraryValidate @(AccountBalanceInterval DijkstraEra) v "account_balance_interval"
3337
huddleRoundTripCborSpec @(Value DijkstraEra) v "positive_coin"
3438
huddleRoundTripArbitraryValidate @(Value DijkstraEra) v "value"
3539
describe "MultiAsset" $ do

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/cardano-ledger-binary/cardano-ledger-binary.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ library testlib
113113
ghc-options:
114114
-Wredundant-constraints
115115
build-depends:
116-
antigen >=0.3.1.0 && < 0.4,
117116
ImpSpec,
118117
QuickCheck,
118+
antigen >=0.3.1.0 && <0.4,
119119
base,
120120
base16-bytestring,
121121
bytestring,
@@ -128,7 +128,7 @@ library testlib
128128
cardano-strict-containers,
129129
cborg,
130130
containers,
131-
cuddle >=1.1 && <1.1.2,
131+
cuddle >=1.2,
132132
directory,
133133
filepath,
134134
formatting,

libs/cardano-ledger-binary/testlib/Test/Cardano/Ledger/Binary/Cuddle.hs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import qualified Codec.CBOR.Pretty as CBOR
5656
import qualified Codec.CBOR.Term as CBOR
5757
import qualified Codec.CBOR.Write as C
5858
import qualified Codec.CBOR.Write as CBOR
59+
import Control.Monad (unless)
5960
import Data.Data (Proxy (..))
6061
import Data.Either (isLeft)
6162
import qualified Data.Text as T
@@ -85,11 +86,15 @@ import Test.Hspec (
8586
it,
8687
shouldBe,
8788
)
88-
import Test.Hspec.Core.Spec (Example (..), paramsQuickCheckArgs)
89-
import Test.QuickCheck (Arbitrary (..), Args (replay), Gen, Testable (..), forAll)
90-
import Test.QuickCheck.Gen (Gen (..))
91-
import Test.QuickCheck.Random (QCGen, mkQCGen)
92-
import Text.Pretty.Simple (pShow)
89+
import Test.QuickCheck (
90+
Arbitrary (..),
91+
Gen,
92+
Testable (..),
93+
counterexample,
94+
discard,
95+
forAll,
96+
)
97+
import Test.QuickCheck.Property (Property)
9398

9499
huddleDecoderEquivalenceSpec ::
95100
forall a.
@@ -263,30 +268,20 @@ showValidationTrace (Evidenced _ t) =
263268
T.unpack . Ansi.renderStrict . layoutPretty defaultLayoutOptions $
264269
prettyValidationTrace defaultTraceOptions t
265270

266-
huddleRoundTripArbitraryValidate ::
271+
huddleRoundTripGenValidate ::
267272
forall a.
268-
( DecCBOR a
269-
, EncCBOR a
270-
, Arbitrary a
271-
, Show a
272-
) =>
273-
Version ->
274-
T.Text ->
275-
SpecWith (CTreeRoot MonoReferenced)
276-
huddleRoundTripArbitraryValidate version ruleName =
273+
(DecCBOR a, Show a, EncCBOR a) => Gen a -> Version -> T.Text -> SpecWith (CTreeRoot MonoReferenced)
274+
huddleRoundTripGenValidate gen version ruleName =
277275
let lbl = label $ Proxy @a
278276
in describe "Encode an arbitrary value and check against CDDL"
279277
. it (T.unpack ruleName <> ": " <> T.unpack lbl)
280-
$ \cddl -> property $
278+
$ \cddl -> property . forAll gen $
281279
\(val :: a) -> do
282280
let
283281
bs = serialize' version val
284282
res = validateCBOR bs (Name ruleName) (mapIndex cddl)
285-
if isValid res
286-
then pure ()
287-
else
288-
expectationFailure $
289-
"CBOR Validation failed\nError:\n" <> showValidationTrace res
283+
unless (isValid res) . expectationFailure $
284+
"CBOR Validation failed\nError:\n" <> showValidationTrace res
290285

291286
huddleRoundTripArbitraryValidate ::
292287
forall a.
@@ -297,7 +292,7 @@ huddleRoundTripArbitraryValidate ::
297292
) =>
298293
Version ->
299294
T.Text ->
300-
SpecWith CuddleData
295+
SpecWith (CTreeRoot MonoReferenced)
301296
huddleRoundTripArbitraryValidate = huddleRoundTripGenValidate $ arbitrary @a
302297

303298
--------------------------------------------------------------------------------

libs/cardano-ledger-core/cardano-ledger-core.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,13 @@ library cddl
193193
-Wunused-packages
194194

195195
build-depends:
196+
QuickCheck,
196197
base,
197198
bytestring,
198199
cardano-ledger-binary,
199200
cardano-ledger-core,
200201
cborg,
201-
cuddle >=1.1,
202+
cuddle >=1.2,
202203
heredoc,
203204
mempack,
204205
random,

libs/cardano-ledger-core/cddl/Cardano/Ledger/Core/HuddleSpec.hs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ import Codec.CBOR.Cuddle.CDDL (Name (..))
2323
import Codec.CBOR.Cuddle.CDDL.CBORGenerator (WrappedTerm (..))
2424
import Codec.CBOR.Cuddle.Huddle
2525
import Codec.CBOR.Term (Term (..))
26-
import Control.Monad (join)
2726
import Data.Bits (Bits (..))
27+
import Data.ByteString (ByteString)
2828
import qualified Data.ByteString as BS
2929
import Data.MemPack (VarLen (..), packByteString)
3030
import Data.Proxy (Proxy (..))
3131
import qualified Data.Text as T
3232
import Data.Word (Word16, Word32, Word64)
33-
import System.Random.Stateful (
34-
Uniform (..),
35-
UniformRange (..),
36-
uniformByteStringM,
37-
)
33+
import Test.QuickCheck (Arbitrary (..), Gen, choose, oneof, vector)
3834
import Text.Heredoc
3935
import Prelude hiding ((/))
4036

@@ -72,25 +68,23 @@ instance Era era => HuddleRule "unit_interval" era where
7268
|our encoders/decoders. Which means we cannot use the actual
7369
|definition here and we hard code the value to 1/2
7470
|]
75-
. withGenerator generator
71+
. withGenerator (const generator)
7672
$ pname =.= tag 30 (arr [a VUInt, a VUInt])
7773
where
78-
generator g = do
74+
generator = do
7975
let genUnitInterval64 l u = do
80-
d <- uniformRM (max 1 l, u) g
81-
n <- uniformRM (l, d) g
76+
d <- choose (max 1 l, u)
77+
n <- choose (l, d)
8278
pure (n, d)
8379
max64 = toInteger (maxBound @Word64)
8480
(n, d) <-
85-
join $
86-
pickOne
87-
[ genUnitInterval64 0 max64
88-
, genUnitInterval64 0 1000
89-
, genUnitInterval64 (max64 - 1000) max64
90-
]
91-
g
81+
oneof
82+
[ genUnitInterval64 0 max64
83+
, genUnitInterval64 0 1000
84+
, genUnitInterval64 (max64 - 1000) max64
85+
]
9286
S . TTagged 30
93-
<$> genArrayTerm [TInteger $ toInteger n, TInteger $ toInteger d] g
87+
<$> genArrayTerm [TInteger $ toInteger n, TInteger $ toInteger d]
9488

9589
instance Era era => HuddleRule "nonnegative_interval" era where
9690
huddleRuleNamed pname p =
@@ -175,6 +169,11 @@ instance Era era => HuddleRule "coin" era where
175169
instance Era era => HuddleRule "positive_coin" era where
176170
huddleRuleNamed pname p = pname =.= (1 :: Integer) ... huddleRule @"max_word64" p
177171

172+
genHash28 :: Gen ByteString
173+
genHash28 =
174+
-- TODO better implementation
175+
BS.pack <$> vector 28
176+
178177
instance Era era => HuddleRule "address" era where
179178
huddleRuleNamed pname _ =
180179
comment
@@ -209,21 +208,20 @@ instance Era era => HuddleRule "address" era where
209208
| 1111: account address: scripthash28
210209
|1001-1101: future formats
211210
|]
212-
. withGenerator generator
211+
. withGenerator (const generator)
213212
$ pname =.= VBytes
214213
where
215-
generator g = do
216-
stakeRef <- uniformRM (0, 0b11) g
214+
generator = do
215+
stakeRef <- choose (0, 0b11)
217216
let
218217
stakeRefMask = stakeRef `shiftL` 5 -- 0b0xx00000
219218
mkMask mask isMask = if isMask then mask else 0
220-
isPaymentScriptMask <- mkMask 0b00010000 <$> uniformM g
221-
isMainnetMask <- mkMask 0b00000001 <$> uniformM g
219+
isPaymentScriptMask <- mkMask 0b00010000 <$> arbitrary
220+
isMainnetMask <- mkMask 0b00000001 <$> arbitrary
222221
let
223222
header = stakeRefMask .|. isPaymentScriptMask .|. isMainnetMask
224-
genHash28 = uniformByteStringM 28 g
225-
genVar32 = VarLen <$> uniformM @Word32 g
226-
genVar16 = VarLen <$> uniformM @Word16 g
223+
genVar32 = VarLen <$> arbitrary @Word32
224+
genVar16 = VarLen <$> arbitrary @Word16
227225
stakeCred <- case stakeRef of
228226
0b00 -> genHash28 -- staking payment hash
229227
0b01 -> genHash28 -- staking script hash
@@ -240,16 +238,16 @@ instance Era era => HuddleRule "address" era where
240238
pure $ S bytesTerm
241239

242240
instance Era era => HuddleRule "reward_account" era where
243-
huddleRuleNamed pname _ = withGenerator generator $ pname =.= VBytes
241+
huddleRuleNamed pname _ = withGenerator (const generator) $ pname =.= VBytes
244242
where
245-
generator g = do
246-
isMainnet <- uniformM g
247-
isScript <- uniformM g
243+
generator = do
244+
isMainnet <- arbitrary
245+
isScript <- arbitrary
248246
let
249247
mainnetMask | isMainnet = 0b00000001 | otherwise = 0x00
250248
scriptMask | isScript = 0b00010000 | otherwise = 0x00
251249
header = 0b11100000 .|. mainnetMask .|. scriptMask
252-
payload <- uniformByteStringM 28 g
250+
payload <- genHash28
253251
let term = TBytes $ BS.cons header payload
254252
pure $ S term
255253

libs/cardano-ledger-core/cddl/Cardano/Ledger/Huddle.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import qualified Data.Text as T
4242
import qualified Data.Text.Lazy as LT
4343
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
4444
import System.Random.Stateful (StatefulGen, UniformRange (..))
45+
import Test.QuickCheck (Gen, elements)
4546

4647
class (KnownSymbol name, Era era) => HuddleRule (name :: Symbol) era where
4748
huddleRuleNamed :: Proxy name -> Proxy era -> Rule
@@ -77,8 +78,8 @@ infixr 0 =.=
7778

7879
infixr 0 =.~
7980

80-
genArrayTerm :: StatefulGen g m => [Term] -> g -> m Term
81-
genArrayTerm es = pickOne [TList es, TListI es]
81+
genArrayTerm :: [Term] -> Gen Term
82+
genArrayTerm es = elements [TList es, TListI es]
8283

8384
pickOne :: StatefulGen g m => NonEmpty a -> g -> m a
8485
pickOne es g = do

0 commit comments

Comments
 (0)