Skip to content

Add genBlockHeader and related functions to Test.Gen.Cardano.Api.Typed #834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ module Test.Gen.Cardano.Api.Typed
, genMintWitnessable
, genPlutusScriptWitness
, genIndexedPlutusScriptWitness
, genBlockNo
, genBlockHeader
, genBlockHeaderAt
, genBlockHeaderHash
)
where

Expand Down Expand Up @@ -1488,3 +1492,31 @@ genScriptWitnessForMint sbe = do
NoScriptDatumForMint
scriptRedeemer
<$> genExecutionUnits

genBlockNo :: Gen BlockNo
genBlockNo = BlockNo <$> Gen.word64 Range.constantBounded

-- | Fully arbitrary block header with completely random hash.
genBlockHeader :: Gen BlockHeader
genBlockHeader = genSlotNo >>= genBlockHeaderAt

-- | Generate a random block header with completely random hash, but at a
-- certain slot.
genBlockHeaderAt :: SlotNo -> Gen BlockHeader
genBlockHeaderAt slotNo = BlockHeader slotNo <$> genBlockHeaderHash <*> genBlockNo

-- | Generate a random block header hash.
-- This will error if the hash size of block headers changes (currently 32 bytes).
genBlockHeaderHash :: Gen (Hash BlockHeader)
genBlockHeaderHash =
unsafeBlockHeaderHashFromBytes . BS.pack <$> Gen.list (Range.singleton 32) Q.arbitrary
where
unsafeBlockHeaderHashFromBytes bytes =
case deserialiseFromRawBytes (proxyToAsType Proxy) bytes of
Left e ->
error $
"unsafeBlockHeaderHashFromBytes: failed on bytes "
<> show bytes
<> " with error "
<> show e
Right h -> h
Loading