Skip to content

Commit 08d802b

Browse files
Add genBlockHeader functions to Test.Gen.Cardano.Api.Typed
1 parent 5e87da5 commit 08d802b

File tree

1 file changed

+32
-0
lines changed
  • cardano-api/gen/Test/Gen/Cardano/Api

1 file changed

+32
-0
lines changed

cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ module Test.Gen.Cardano.Api.Typed
137137
, genMintWitnessable
138138
, genPlutusScriptWitness
139139
, genIndexedPlutusScriptWitness
140+
, genBlockNo
141+
, genBlockHeader
142+
, genBlockHeaderAt
143+
, genBlockHeaderHash
140144
)
141145
where
142146

@@ -1488,3 +1492,31 @@ genScriptWitnessForMint sbe = do
14881492
NoScriptDatumForMint
14891493
scriptRedeemer
14901494
<$> genExecutionUnits
1495+
1496+
genBlockNo :: Gen BlockNo
1497+
genBlockNo = BlockNo <$> Gen.word64 Range.constantBounded
1498+
1499+
-- | Fully arbitrary block header with completely random hash.
1500+
genBlockHeader :: Gen BlockHeader
1501+
genBlockHeader = genSlotNo >>= genBlockHeaderAt
1502+
1503+
-- | Generate a random block header with completely random hash, but at a
1504+
-- certain slot.
1505+
genBlockHeaderAt :: SlotNo -> Gen BlockHeader
1506+
genBlockHeaderAt slotNo = BlockHeader slotNo <$> genBlockHeaderHash <*> genBlockNo
1507+
1508+
-- | Generate a random block header hash.
1509+
-- This will error if the hash size of block headers changes (currently 32 bytes).
1510+
genBlockHeaderHash :: Gen (Hash BlockHeader)
1511+
genBlockHeaderHash =
1512+
unsafeBlockHeaderHashFromBytes . BS.pack <$> Gen.list (Range.singleton 32) Q.arbitrary
1513+
where
1514+
unsafeBlockHeaderHashFromBytes bytes =
1515+
case deserialiseFromRawBytes (proxyToAsType Proxy) bytes of
1516+
Left e ->
1517+
error $
1518+
"unsafeBlockHeaderHashFromBytes: failed on bytes "
1519+
<> show bytes
1520+
<> " with error "
1521+
<> show e
1522+
Right h -> h

0 commit comments

Comments
 (0)