Skip to content

Commit

Permalink
Remove direct dependencies from cardano-ledger-byron
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Oct 1, 2024
1 parent f74a2ca commit df149dd
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 214 deletions.
2 changes: 0 additions & 2 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ library
cardano-data >=1.1,
cardano-git-rev ^>=0.2.2,
cardano-ledger-api,
cardano-ledger-byron >=1.0.1.0,
cardano-ledger-core,
cardano-ledger-shelley,
cardano-ping ^>=0.4,
Expand Down Expand Up @@ -373,7 +372,6 @@ test-suite cardano-cli-golden
cardano-cli,
cardano-cli:cardano-cli-test-lib,
cardano-crypto-wrapper,
cardano-ledger-byron,
cardano-ledger-shelley >=1.10.0.0,
cardano-strict-containers ^>=0.1,
cborg,
Expand Down
9 changes: 4 additions & 5 deletions cardano-cli/src/Cardano/CLI/Byron/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ where

import Cardano.Api hiding (GenesisParameters)
import Cardano.Api.Byron hiding (GenesisParameters)
import qualified Cardano.Api.Ledger as L

import Cardano.Chain.Update (InstallerHash (..), ProtocolVersion (..),
SoftwareVersion (..), SystemTag (..))
import Cardano.CLI.Byron.Genesis
import Cardano.CLI.Byron.Key
import Cardano.CLI.Byron.Tx
Expand Down Expand Up @@ -105,9 +104,9 @@ data NodeCmds
NetworkId
(SigningKeyFile In)
ProtocolVersion
SoftwareVersion
SystemTag
InstallerHash
L.SoftwareVersion
L.SystemTag
L.InstallerHash
FilePath
ByronProtocolParametersUpdate
| -- | Update proposal filepath.
Expand Down
40 changes: 19 additions & 21 deletions cardano-cli/src/Cardano/CLI/Byron/Delegation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ where
import Cardano.Api.Byron
import qualified Cardano.Api.Ledger as L

import qualified Cardano.Chain.Delegation as Dlg
import Cardano.Chain.Slotting (EpochNumber)
import Cardano.CLI.Byron.Key (ByronKeyFailure, renderByronKeyFailure)
import Cardano.CLI.Types.Common (CertificateFile (..))
import Cardano.Crypto (ProtocolMagicId)
Expand Down Expand Up @@ -54,12 +52,12 @@ renderByronDelegationError = \case
-- issuer key, for a given protocol magic and coming into effect at given epoch.
issueByronGenesisDelegation
:: ProtocolMagicId
-> EpochNumber
-> L.EpochNumber
-> Crypto.SigningKey
-> Crypto.VerificationKey
-> Dlg.Certificate
issueByronGenesisDelegation magic epoch issuerSK delegateVK =
Dlg.signCertificate magic delegateVK epoch $
-> L.Certificate
issueByronGenesisDelegation magic epoch issuerSK delegateVK' =
signCertificate magic delegateVK' epoch $
Crypto.noPassSafeSigner issuerSK

-- | Verify that a certificate signifies genesis delegation by assumed genesis key
Expand All @@ -75,59 +73,59 @@ checkByronGenesisDelegation (CertificateFile certF) magic issuer delegate = do
ecert <- liftIO $ canonicalDecodePretty <$> LB.readFile certF
case ecert of
Left e -> left $ DlgCertificateDeserialisationFailed certF e
Right (cert :: Dlg.Certificate) -> do
Right (cert :: L.Certificate) -> do
let issues = checkDlgCert cert magic issuer delegate
unless (null issues) $
left $
CertificateValidationErrors certF issues

checkDlgCert
:: Dlg.ACertificate a
:: ACertificate a
-> ProtocolMagicId
-> Crypto.VerificationKey
-> Crypto.VerificationKey
-> [Text]
checkDlgCert cert magic issuerVK' delegateVK' =
mconcat
[ [ sformat "Certificate does not have a valid signature."
| not (Dlg.isValid magic' cert')
| not (isValid magic' cert')
]
, [ sformat
("Certificate issuer " . vkF . " doesn't match expected: " . vkF)
(Dlg.issuerVK cert)
(issuerVK cert)
issuerVK'
| Dlg.issuerVK cert /= issuerVK'
| issuerVK cert /= issuerVK'
]
, [ sformat
("Certificate delegate " . vkF . " doesn't match expected: " . vkF)
(Dlg.delegateVK cert)
(delegateVK cert)
delegateVK'
| Dlg.delegateVK cert /= delegateVK'
| delegateVK cert /= delegateVK'
]
]
where
magic' :: L.Annotated ProtocolMagicId ByteString
magic' = L.Annotated magic (L.serialize' L.byronProtVer magic)

epoch :: EpochNumber
epoch = L.unAnnotated $ Dlg.aEpoch cert
epoch :: L.EpochNumber
epoch = L.unAnnotated $ aEpoch cert

cert' :: Dlg.ACertificate ByteString
cert' :: ACertificate ByteString
cert' =
let unannotated =
cert
{ Dlg.aEpoch = L.Annotated epoch ()
, Dlg.annotation = ()
{ aEpoch = L.Annotated epoch ()
, annotation = ()
}
in unannotated
{ Dlg.annotation = L.serialize' L.byronProtVer unannotated
, Dlg.aEpoch = L.Annotated epoch (L.serialize' L.byronProtVer epoch)
{ annotation = L.serialize' L.byronProtVer unannotated
, aEpoch = L.Annotated epoch (L.serialize' L.byronProtVer epoch)
}

vkF :: forall r. Format r (Crypto.VerificationKey -> r)
vkF = Crypto.fullVerificationKeyF

serialiseDelegationCert :: Dlg.Certificate -> ByteString
serialiseDelegationCert :: L.Certificate -> ByteString
serialiseDelegationCert = LB.toStrict . canonicalEncodePretty

serialiseByronWitness :: SomeByronSigningKey -> ByteString
Expand Down
76 changes: 35 additions & 41 deletions cardano-cli/src/Cardano/CLI/Byron/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ module Cardano.CLI.Byron.Genesis
where

import Cardano.Api (Key (..), NetworkId, writeSecrets)
import Cardano.Api.Byron (ByronKey, SerialiseAsRawBytes (..), SigningKey (..),
import Cardano.Api.Byron (ByronKey, SerialiseAsRawBytes (..), SigningKey (..), delegateVK,
toByronRequiresNetworkMagic)
import qualified Cardano.Api.Ledger as L

import qualified Cardano.Chain.Common as Common
import Cardano.Chain.Delegation hiding (Map, epoch)
import Cardano.Chain.Genesis (GeneratedSecrets (..))
import qualified Cardano.Chain.Genesis as Genesis
import qualified Cardano.Chain.UTxO as UTxO
import Cardano.CLI.Byron.Delegation
import Cardano.CLI.Byron.Key
import Cardano.CLI.Pretty
Expand All @@ -34,7 +30,6 @@ import Control.Monad.Trans.Except.Extra (firstExceptT, left, right)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LB
import qualified Data.List as List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.String (IsString)
import Data.Text (Text)
Expand All @@ -48,11 +43,11 @@ import System.Directory (createDirectory, doesPathExist)
data ByronGenesisError
= ByronDelegationCertSerializationError !ByronDelegationError
| ByronDelegationKeySerializationError ByronDelegationError
| GenesisGenerationError !Genesis.GenesisDataGenerationError
| GenesisGenerationError !L.GenesisDataGenerationError
| GenesisOutputDirAlreadyExists FilePath
| GenesisReadError !FilePath !Genesis.GenesisDataError
| GenesisReadError !FilePath !L.GenesisDataError
| GenesisSpecError !Text
| MakeGenesisDelegationError !Genesis.GenesisDelegationError
| MakeGenesisDelegationError !L.GenesisDelegationError
| NoGenesisDelegationForKey !Text
| ProtocolParametersParseFailed !FilePath !Text
| PoorKeyFailure !ByronKeyFailure
Expand Down Expand Up @@ -89,16 +84,16 @@ newtype NewDirectory
data GenesisParameters = GenesisParameters
{ gpStartTime :: !UTCTime
, gpProtocolParamsFile :: !FilePath
, gpK :: !Common.BlockCount
, gpK :: !L.BlockCount
, gpProtocolMagic :: !Crypto.ProtocolMagic
, gpTestnetBalance :: !Genesis.TestnetBalanceOptions
, gpFakeAvvmOptions :: !Genesis.FakeAvvmOptions
, gpAvvmBalanceFactor :: !Common.LovelacePortion
, gpTestnetBalance :: !L.TestnetBalanceOptions
, gpFakeAvvmOptions :: !L.FakeAvvmOptions
, gpAvvmBalanceFactor :: !L.LovelacePortion
, gpSeed :: !(Maybe Integer)
}
deriving Show

mkGenesisSpec :: GenesisParameters -> ExceptT ByronGenesisError IO Genesis.GenesisSpec
mkGenesisSpec :: GenesisParameters -> ExceptT ByronGenesisError IO L.GenesisSpec
mkGenesisSpec gp = do
protoParamsRaw <- lift . LB.readFile $ gpProtocolParamsFile gp

Expand All @@ -111,24 +106,24 @@ mkGenesisSpec gp = do
-- We're relying on the generator to fake AVVM and delegation.
genesisDelegation <-
withExceptT MakeGenesisDelegationError $
Genesis.mkGenesisDelegation []
L.mkGenesisDelegation []

withExceptT GenesisSpecError $
ExceptT . pure $
Genesis.mkGenesisSpec
(Genesis.GenesisAvvmBalances mempty)
L.mkGenesisSpec
(L.GenesisAvvmBalances mempty)
genesisDelegation
protocolParameters
(gpK gp)
(gpProtocolMagic gp)
(mkGenesisInitialiser True)
where
mkGenesisInitialiser :: Bool -> Genesis.GenesisInitializer
mkGenesisInitialiser :: Bool -> L.GenesisInitializer
mkGenesisInitialiser =
Genesis.GenesisInitializer
L.GenesisInitializer
(gpTestnetBalance gp)
(gpFakeAvvmOptions gp)
(Common.lovelacePortionToRational (gpAvvmBalanceFactor gp))
(L.lovelacePortionToRational (gpAvvmBalanceFactor gp))

-- | Generate a genesis, for given blockchain start time, protocol parameters,
-- security parameter, protocol magic, testnet balance options, fake AVVM options,
Expand All @@ -138,36 +133,36 @@ mkGenesisSpec gp = do
-- or if the genesis fails generation.
mkGenesis
:: GenesisParameters
-> ExceptT ByronGenesisError IO (Genesis.GenesisData, Genesis.GeneratedSecrets)
-> ExceptT ByronGenesisError IO (L.GenesisData, L.GeneratedSecrets)
mkGenesis gp = do
genesisSpec <- mkGenesisSpec gp

withExceptT GenesisGenerationError $
Genesis.generateGenesisData (gpStartTime gp) genesisSpec
L.generateGenesisData (gpStartTime gp) genesisSpec

-- | Read genesis from a file.
readGenesis
:: GenesisFile
-> NetworkId
-> ExceptT ByronGenesisError IO Genesis.Config
-> ExceptT ByronGenesisError IO L.Config
readGenesis (GenesisFile file) nw =
firstExceptT (GenesisReadError file) $ do
(genesisData, genesisHash) <- Genesis.readGenesisData file
(genesisData, genesisHash) <- L.readGenesisData file
return
Genesis.Config
{ Genesis.configGenesisData = genesisData
, Genesis.configGenesisHash = genesisHash
, Genesis.configReqNetMagic = toByronRequiresNetworkMagic nw
, Genesis.configUTxOConfiguration = UTxO.defaultUTxOConfiguration
L.Config
{ L.configGenesisData = genesisData
, L.configGenesisHash = genesisHash
, L.configReqNetMagic = toByronRequiresNetworkMagic nw
, L.configUTxOConfiguration = L.defaultUTxOConfiguration
}

-- | Write out genesis into a directory that must not yet exist. An error is
-- thrown if the directory already exists, or the genesis has delegate keys that
-- are not delegated to.
dumpGenesis
:: NewDirectory
-> Genesis.GenesisData
-> Genesis.GeneratedSecrets
-> L.GenesisData
-> L.GeneratedSecrets
-> ExceptT ByronGenesisError IO ()
dumpGenesis (NewDirectory outDir) genesisData gs = do
exists <- liftIO $ doesPathExist outDir
Expand All @@ -176,33 +171,32 @@ dumpGenesis (NewDirectory outDir) genesisData gs = do
else liftIO $ createDirectory outDir
liftIO $ LB.writeFile genesisJSONFile (canonicalEncodePretty genesisData)

dlgCerts <- mapM (findDelegateCert . ByronSigningKey) $ gsRichSecrets gs
dlgCerts <- mapM (findDelegateCert . ByronSigningKey) $ L.gsRichSecrets gs

liftIO $
wOut
"genesis-keys"
"key"
serialiseToRawBytes
(map ByronSigningKey $ gsDlgIssuersSecrets gs)
(map ByronSigningKey $ L.gsDlgIssuersSecrets gs)
liftIO $
wOut
"delegate-keys"
"key"
serialiseToRawBytes
(map ByronSigningKey $ gsRichSecrets gs)
(map ByronSigningKey $ L.gsRichSecrets gs)
liftIO $
wOut
"poor-keys"
"key"
serialiseToRawBytes
(map (ByronSigningKey . Genesis.poorSecretToKey) $ gsPoorSecrets gs)
(map (ByronSigningKey . L.poorSecretToKey) $ L.gsPoorSecrets gs)
liftIO $ wOut "delegation-cert" "json" serialiseDelegationCert dlgCerts
liftIO $ wOut "avvm-secrets" "secret" printFakeAvvmSecrets $ gsFakeAvvmSecrets gs
liftIO $ wOut "avvm-secrets" "secret" printFakeAvvmSecrets $ L.gsFakeAvvmSecrets gs
where
dlgCertMap :: Map Common.KeyHash Certificate
dlgCertMap = Genesis.unGenesisDelegation $ Genesis.gdHeavyDelegation genesisData
dlgCertMap = L.unGenesisDelegation $ L.gdHeavyDelegation genesisData

findDelegateCert :: SigningKey ByronKey -> ExceptT ByronGenesisError IO Certificate
findDelegateCert :: SigningKey ByronKey -> ExceptT ByronGenesisError IO L.Certificate
findDelegateCert bSkey@(ByronSigningKey sk) =
case List.find (isCertForSK sk) (Map.elems dlgCertMap) of
Nothing ->
Expand All @@ -219,7 +213,7 @@ dumpGenesis (NewDirectory outDir) genesisData gs = do
printFakeAvvmSecrets rskey = Text.encodeUtf8 . toStrict . toLazyText $ build rskey

-- Compare a given 'SigningKey' with a 'Certificate' 'VerificationKey'
isCertForSK :: Crypto.SigningKey -> Certificate -> Bool
isCertForSK :: Crypto.SigningKey -> L.Certificate -> Bool
isCertForSK sk cert = delegateVK cert == Crypto.toVerification sk

wOut :: String -> String -> (a -> ByteString) -> [a] -> IO ()
Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/Byron/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module Cardano.CLI.Byron.Key
where

import Cardano.Api.Byron
import Cardano.Api.Ledger (addressHash)

import qualified Cardano.Chain.Common as Common
import Cardano.CLI.Types.Common
import qualified Cardano.Crypto.Signing as Crypto

Expand Down Expand Up @@ -74,7 +74,7 @@ prettyPublicKey (ByronVerificationKey vk) =
% "\n public key (hex): "
% Crypto.fullVerificationKeyHexF
)
(Common.addressHash vk)
(addressHash vk)
vk
vk

Expand Down
Loading

0 comments on commit df149dd

Please sign in to comment.