Skip to content

Commit

Permalink
Apply review hints
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Nov 8, 2023
1 parent 46d2beb commit c783671
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 91 deletions.
22 changes: 13 additions & 9 deletions lib/read/lib/Cardano/Wallet/Read/Block/Gen.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

Expand All @@ -10,6 +11,9 @@ import Prelude hiding
( (.)
)

import Cardano.Ledger.BaseTypes
( natVersion
)
import Cardano.Wallet.Read
( Tx (..)
)
Expand Down Expand Up @@ -65,28 +69,28 @@ mkBlockEra :: EraFun BlockParameters Block
mkBlockEra =
EraFun
{ byronFun = g mkByronBlock
, shelleyFun = g mkShelleyBlock
, allegraFun = g mkShelleyBlock
, maryFun = g mkShelleyBlock
, alonzoFun = g mkShelleyBlock
, babbageFun = g mkBabbageBlock
, conwayFun = g mkBabbageBlock
, shelleyFun = g $ mkShelleyBlock (natVersion @2)
, allegraFun = g $ mkShelleyBlock (natVersion @4)
, maryFun = g $ mkShelleyBlock (natVersion @5)
, alonzoFun = g $ mkShelleyBlock (natVersion @6)
, babbageFun = g $ mkBabbageBlock (natVersion @7)
, conwayFun = g $ mkBabbageBlock (natVersion @8)
}
where
g f = Block . f

genBlocks
mkConsensusBlocks
:: AllEraValue ([] :.: BlockParameters)
-> [ConsensusBlock]
genBlocks (AllEraValue source) =
mkConsensusBlocks (AllEraValue source) =
concatMap (fmap extractEraValue . sequenceEraValue)
$ runAllEraValue
$ AllEraValue
$ (toConsensusBlock . mkBlockEra) *.** source

exampleBlock :: [ConsensusBlock]
exampleBlock =
genBlocks
mkConsensusBlocks
$ AllEraValue
$ EraFun
{ byronFun = f [exampleBlockParameters [Tx exampleByronTx]]
Expand Down
31 changes: 17 additions & 14 deletions lib/read/lib/Cardano/Wallet/Read/Block/Gen/Babbage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Cardano.Ledger.BaseTypes
)
import Cardano.Ledger.Binary
( EncCBOR
, Version
)
import Cardano.Ledger.Keys
( VKey (..)
Expand All @@ -46,10 +47,10 @@ import Cardano.Wallet.Read.Block.Gen.BlockParameters
import Cardano.Wallet.Read.Block.Gen.Shelley
( HeaderEra
, bodyHash
, genKeyDSIGN'
, genKeyVRF'
, hashHeader
, mkAnyAfterShelleyBlock
, mkKeyDSIGN'
, mkKeyVRF'
, mkSignedKES
, oCertamente
)
Expand Down Expand Up @@ -77,34 +78,36 @@ mkBabbageBlock
, HeaderEra era ~ Header StandardCrypto
, TxT cardano_era ~ L.Tx (era StandardCrypto)
)
=> BlockParameters cardano_era
=> Version
-> BlockParameters cardano_era
-> O.ShelleyBlock (Praos StandardCrypto) (era StandardCrypto)
mkBabbageBlock BlockParameters{blockNumber, slotNumber, txs} =
mkAnyAfterShelleyBlock txs $ babbageHeader slotNumber' blockNumber'
mkBabbageBlock v BlockParameters{blockNumber, slotNumber, txs} =
mkAnyAfterShelleyBlock txs $ babbageHeader v slotNumber' blockNumber'
where
slotNumber' = L.SlotNo $ fromIntegral $ unSlotNo slotNumber
blockNumber' = L.BlockNo $ fromIntegral $ unBlockNo blockNumber

babbageHeader
:: L.SlotNo
:: Version
-> L.SlotNo
-> L.BlockNo
-> Header StandardCrypto
babbageHeader slotNumber blockNumber =
Header <*> mkSignedKES $ babbageBody slotNumber blockNumber
babbageHeader v slotNumber blockNumber =
Header <*> mkSignedKES $ babbageBody v slotNumber blockNumber

babbageBody :: L.SlotNo -> L.BlockNo -> HeaderBody StandardCrypto
babbageBody slotNumber blockNumber =
babbageBody :: Version -> L.SlotNo -> L.BlockNo -> HeaderBody StandardCrypto
babbageBody v slotNumber blockNumber =
HeaderBody
{ hbBlockNo = blockNumber
, hbSlotNo = slotNumber
, hbPrev = BlockHash hashHeader
, hbVk = VKey $ deriveVerKeyDSIGN genKeyDSIGN'
, hbVrfVk = deriveVerKeyVRF genKeyVRF'
, hbVk = VKey $ deriveVerKeyDSIGN mkKeyDSIGN'
, hbVrfVk = deriveVerKeyVRF mkKeyVRF'
, hbVrfRes =
uncurry CertifiedVRF
$ evalVRF () ("" :: ByteString) genKeyVRF'
$ evalVRF () ("" :: ByteString) mkKeyVRF'
, hbBodySize = 42
, hbBodyHash = bodyHash
, hbOCert = oCertamente
, hbProtVer = ProtVer (natVersion @4) 1
, hbProtVer = ProtVer v 0
}
2 changes: 1 addition & 1 deletion lib/read/lib/Cardano/Wallet/Read/Block/Gen/Byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ headerHash :: Byron.HeaderHash
headerHash = coerce $ serializeCborHash ("HeaderHash" :: Text)

protocolVersion :: Byron.ProtocolVersion
protocolVersion = Byron.ProtocolVersion 1 1 1
protocolVersion = Byron.ProtocolVersion 3 0 0

softwareVersion :: Byron.SoftwareVersion
softwareVersion = Byron.SoftwareVersion (Byron.ApplicationName "Golden") 99
Expand Down
115 changes: 59 additions & 56 deletions lib/read/lib/Cardano/Wallet/Read/Block/Gen/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
module Cardano.Wallet.Read.Block.Gen.Shelley
( block
, bodyHash
, genKeyDSIGN'
, genKeyKES'
, genKeyVRF'
, mkKeyDSIGN'
, mkKeyKES'
, mkKeyVRF'
, hashHeader
, HeaderEra
, mkAnyAfterShelleyBlock
Expand All @@ -33,7 +33,7 @@ import Cardano.Crypto.KES
( KESAlgorithm (..)
)
import Cardano.Crypto.Seed
( readSeedFromSystemEntropy
( mkSeedFromBytes
)
import Cardano.Crypto.Util
( SignableRepresentation
Expand All @@ -47,7 +47,7 @@ import Cardano.Crypto.VRF.Praos
)
import Cardano.Ledger.BaseTypes
( ProtVer (..)
, natVersion
, Version
)
import Cardano.Ledger.Binary
( EncCBOR
Expand Down Expand Up @@ -96,9 +96,6 @@ import Ouroboros.Consensus.Protocol.TPraos
( StandardCrypto
, TPraos
)
import System.IO.Unsafe
( unsafePerformIO
)

import qualified Cardano.Crypto.DSIGN as Crypto
import qualified Cardano.Crypto.Hash as Crypto
Expand All @@ -107,7 +104,8 @@ import qualified Cardano.Ledger.Api as L
import qualified Cardano.Ledger.Block as L
import qualified Cardano.Ledger.Core as L
import qualified Cardano.Ledger.Slot as L
import qualified Data.ByteString.Short as B
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Short as BS
import qualified Data.Sequence.Strict as Seq
import qualified Ouroboros.Consensus.Shelley.Ledger as O
import qualified Ouroboros.Consensus.Shelley.Protocol.Abstract as O
Expand All @@ -125,46 +123,48 @@ type family HeaderEra era where
-- valid for shelley , allegra, mary, alonzo
--------------------------------------------------------------------------------

hbody :: L.SlotNo -> L.BlockNo -> BHBody StandardCrypto
hbody slotNumber blockNumber =
hbody :: Version -> L.SlotNo -> L.BlockNo -> BHBody StandardCrypto
hbody v slotNumber blockNumber =
BHBody
{ bheaderBlockNo = blockNumber
, bheaderSlotNo = slotNumber
, bheaderPrev = BlockHash hashHeader
, bheaderVk = VKey $ deriveVerKeyDSIGN genKeyDSIGN'
, bheaderVrfVk = deriveVerKeyVRF genKeyVRF'
, bheaderVk = VKey $ deriveVerKeyDSIGN mkKeyDSIGN'
, bheaderVrfVk = deriveVerKeyVRF mkKeyVRF'
, bheaderEta =
uncurry CertifiedVRF
$ evalVRF () ("" :: ByteString) genKeyVRF'
$ evalVRF () ("" :: ByteString) mkKeyVRF'
, bheaderL =
uncurry CertifiedVRF
$ evalVRF () ("" :: ByteString) genKeyVRF'
$ evalVRF () ("" :: ByteString) mkKeyVRF'
, bsize = 42
, bhash = bodyHash
, bheaderOCert = oCertamente
, bprotver = ProtVer (natVersion @4) 1
, bprotver = ProtVer v 0
}

headerShelley
:: L.SlotNo
:: Version
-> L.SlotNo
-> L.BlockNo
-> BHeader StandardCrypto
headerShelley slotNumber blockNumber =
BHeader <*> mkSignedKES $ hbody slotNumber blockNumber
headerShelley v slotNumber blockNumber =
BHeader <*> mkSignedKES $ hbody v slotNumber blockNumber

mkShelleyBlock
:: ( L.EraSegWits (era StandardCrypto)
, EncCBOR (HeaderEra era)
, HeaderEra era ~ BHeader StandardCrypto
, TxT cardano_era ~ L.Tx (era StandardCrypto)
)
=> BlockParameters cardano_era
=> Version
-> BlockParameters cardano_era
-> O.ShelleyBlock (TPraos StandardCrypto) (era StandardCrypto)
mkShelleyBlock BlockParameters{blockNumber, slotNumber, txs} =
mkAnyAfterShelleyBlock txs $ headerShelley slotNumber' blockNumber'
where
slotNumber' = L.SlotNo $ fromIntegral $ unSlotNo slotNumber
blockNumber' = L.BlockNo $ fromIntegral $ unBlockNo blockNumber
mkShelleyBlock v BlockParameters{blockNumber, slotNumber, txs} =
mkAnyAfterShelleyBlock txs $ headerShelley v slotNumber' blockNumber'
where
slotNumber' = L.SlotNo $ fromIntegral $ unSlotNo slotNumber
blockNumber' = L.BlockNo $ fromIntegral $ unBlockNo blockNumber

--------------------------------------------------------------------------------
-- valid for any era after shelley
Expand All @@ -186,7 +186,7 @@ mkAnyAfterShelleyBlock txs header =
txs' = unTx <$> txs

hash :: O.ShelleyHash StandardCrypto
hash = O.ShelleyHash $ Crypto.UnsafeHash $ B.pack $ replicate 32 42
hash = O.ShelleyHash $ Crypto.UnsafeHash $ BS.pack $ replicate 32 42

block
:: ( L.EraSegWits (era StandardCrypto)
Expand All @@ -209,55 +209,58 @@ mkSignedKES
-> Crypto.SignedKES
(Crypto.Sum6KES Crypto.Ed25519DSIGN Crypto.Blake2b_256)
a
mkSignedKES hbody' = Crypto.signedKES () 42 hbody' $ genKeyKES seedGenKES
mkSignedKES hbody' = Crypto.signedKES () 42 hbody' $ genKeyKES seedKeyKES

oCertamente :: OCert StandardCrypto
oCertamente =
OCert
{ ocertVkHot = deriveVerKeyKES $ genKeyKES seedGenKES
{ ocertVkHot = deriveVerKeyKES $ genKeyKES seedKeyKES
, ocertN = 42
, ocertKESPeriod = KESPeriod 42
, ocertSigma = signedDSIGN () oCertSignable $ genKeyDSIGN seedGenDSIGN
, ocertSigma = signedDSIGN () oCertSignable $ genKeyDSIGN seedKeyDSIGN
}

oCertSignable :: OCertSignable StandardCrypto
oCertSignable =
OCertSignable (deriveVerKeyKES $ genKeyKES seedGenKES) 42
$ KESPeriod 42
OCertSignable (deriveVerKeyKES $ genKeyKES seedKeyKES) 42 $ KESPeriod 42

{-# NOINLINE seedGenKES #-}
seedGenKES :: Crypto.Seed
seedGenKES =
unsafePerformIO
$ readSeedFromSystemEntropy
$ seedSizeKES
(Proxy :: Proxy (Crypto.Sum6KES Crypto.Ed25519DSIGN Crypto.Blake2b_256))
seedKeyKES :: Crypto.Seed
seedKeyKES =
mkSeedFromBytes
$ B.pack
$ flip replicate 'a'
$ fromIntegral
$ sizeSignKeyKES
(Proxy @(Crypto.Sum6KES Crypto.Ed25519DSIGN Crypto.Blake2b_256))

genKeyKES' :: KESAlgorithm a => SignKeyKES a
genKeyKES' = genKeyKES seedGenKES
mkKeyKES' :: KESAlgorithm a => SignKeyKES a
mkKeyKES' = genKeyKES seedKeyKES

bodyHash :: Crypto.Hash Crypto.Blake2b_256 L.EraIndependentBlockBody
bodyHash = Crypto.UnsafeHash $ B.pack $ replicate 32 42
bodyHash = Crypto.UnsafeHash $ BS.pack $ replicate 32 42

{-# NOINLINE seedGenVRF #-}
seedGenVRF :: Crypto.Seed
seedGenVRF =
unsafePerformIO
$ readSeedFromSystemEntropy
seedKeyVRF :: Crypto.Seed
seedKeyVRF =
mkSeedFromBytes
$ B.pack
$ flip replicate 'a'
$ fromIntegral
$ sizeSignKeyVRF (Proxy :: Proxy PraosVRF)

genKeyVRF' :: VRFAlgorithm a => SignKeyVRF a
genKeyVRF' = genKeyVRF seedGenVRF
mkKeyVRF' :: VRFAlgorithm a => SignKeyVRF a
mkKeyVRF' = genKeyVRF seedKeyVRF

hashHeader :: HashHeader StandardCrypto
hashHeader = HashHeader $ Crypto.UnsafeHash $ B.pack $ replicate 32 42
hashHeader = HashHeader $ Crypto.UnsafeHash $ BS.pack $ replicate 32 42

{-# NOINLINE seedGenDSIGN #-}
seedGenDSIGN :: Crypto.Seed
seedGenDSIGN =
unsafePerformIO
$ readSeedFromSystemEntropy
{-# NOINLINE seedKeyDSIGN #-}
seedKeyDSIGN :: Crypto.Seed
seedKeyDSIGN =
mkSeedFromBytes
$ B.pack
$ flip replicate 'a'
$ fromIntegral
$ Crypto.seedSizeDSIGN (Proxy :: Proxy Ed25519DSIGN)

genKeyDSIGN' :: DSIGNAlgorithm a => SignKeyDSIGN a
genKeyDSIGN' = genKeyDSIGN seedGenDSIGN
mkKeyDSIGN' :: DSIGNAlgorithm a => SignKeyDSIGN a
mkKeyDSIGN' = genKeyDSIGN seedKeyDSIGN
8 changes: 4 additions & 4 deletions lib/read/lib/Cardano/Wallet/Read/Tx/Gen/Allegra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Cardano.Wallet.Read.Tx.Gen.Allegra
( mkAllegraTx
, exampleAllegraTx
, validity
, exampleValidity
)
where

Expand Down Expand Up @@ -71,12 +71,12 @@ body ins outs =
certs
wdrls
txfee
validity
exampleValidity
upd
auxb

validity :: ValidityInterval
validity = ValidityInterval SNothing SNothing
exampleValidity :: ValidityInterval
exampleValidity = ValidityInterval SNothing SNothing

exampleAllegraTx :: ShelleyTx (AllegraEra StandardCrypto)
exampleAllegraTx = mkAllegraTx exampleTxParameters
4 changes: 2 additions & 2 deletions lib/read/lib/Cardano/Wallet/Read/Tx/Gen/Alonzo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import Cardano.Ledger.Shelley.Tx
( TxIn
)
import Cardano.Wallet.Read.Tx.Gen.Allegra
( validity
( exampleValidity
)
import Cardano.Wallet.Read.Tx.Gen.Mary
( mkMaryValue
Expand Down Expand Up @@ -113,7 +113,7 @@ body ins outs =
certs
wdrls
txfee
validity
exampleValidity
upd
whash
mint
Expand Down
Loading

0 comments on commit c783671

Please sign in to comment.