Skip to content

Commit

Permalink
Remove dependencies from cardano-ledger-alonzo and `cardano-ledger-…
Browse files Browse the repository at this point in the history
…byron`
  • Loading branch information
palas authored and smelc committed Oct 16, 2024
1 parent 4cbcb27 commit bd668b1
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 238 deletions.
4 changes: 0 additions & 4 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ library
cardano-crypto-wrapper ^>=1.5.1,
cardano-data >=1.1,
cardano-git-rev ^>=0.2.2,
cardano-ledger-alonzo,
cardano-ledger-api,
cardano-ledger-byron >=1.0.1.0,
cardano-ledger-core,
cardano-ledger-shelley,
cardano-ping ^>=0.4,
Expand Down Expand Up @@ -312,7 +310,6 @@ test-suite cardano-cli-test
cardano-api:{cardano-api, gen, internal},
cardano-cli,
cardano-cli:cardano-cli-test-lib,
cardano-ledger-alonzo,
cardano-slotting,
containers,
directory,
Expand Down Expand Up @@ -378,7 +375,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
12 changes: 6 additions & 6 deletions cardano-cli/src/Cardano/CLI/Byron/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Cardano.CLI.Byron.Commands
where

import Cardano.Api hiding (GenesisParameters)
import Cardano.Api.Byron hiding (Address, GenesisParameters, TxIn, TxOut)
import qualified Cardano.Api.Byron as Byron

import Cardano.CLI.Byron.Genesis
import Cardano.CLI.Byron.Key
Expand Down Expand Up @@ -102,12 +102,12 @@ data NodeCmds
| UpdateProposal
NetworkId
(SigningKeyFile In)
ProtocolVersion
SoftwareVersion
SystemTag
InstallerHash
Byron.ProtocolVersion
Byron.SoftwareVersion
Byron.SystemTag
Byron.InstallerHash
FilePath
ByronProtocolParametersUpdate
Byron.ByronProtocolParametersUpdate
| -- | Update proposal filepath.
SubmitUpdateProposal
SocketPath
Expand Down
35 changes: 17 additions & 18 deletions cardano-cli/src/Cardano/CLI/Byron/Delegation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ where
import Cardano.Api.Byron hiding (delegateVK)
import qualified Cardano.Api.Ledger as L

import qualified Cardano.Chain.Delegation as Dlg
import Cardano.CLI.Byron.Key (ByronKeyFailure, renderByronKeyFailure)
import Cardano.CLI.Types.Common (CertificateFile (..))
import Cardano.Crypto (ProtocolMagicId)
Expand Down Expand Up @@ -56,9 +55,9 @@ issueByronGenesisDelegation
-> EpochNumber
-> Crypto.SigningKey
-> Crypto.VerificationKey
-> Dlg.Certificate
issueByronGenesisDelegation magic epoch issuerSK delegateVK =
Dlg.signCertificate magic delegateVK epoch $
-> 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 @@ -74,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 :: 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.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 :: 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 @@ -15,12 +15,8 @@ where
import Cardano.Api (Key (..), NetworkId, writeSecrets)
import Cardano.Api.Byron (ByronKey, SerialiseAsRawBytes (..), SigningKey (..),
toByronRequiresNetworkMagic)
import qualified Cardano.Api.Byron as Byron

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 !Byron.GenesisDataGenerationError
| GenesisOutputDirAlreadyExists FilePath
| GenesisReadError !FilePath !Genesis.GenesisDataError
| GenesisReadError !FilePath !Byron.GenesisDataError
| GenesisSpecError !Text
| MakeGenesisDelegationError !Genesis.GenesisDelegationError
| MakeGenesisDelegationError !Byron.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 :: !Byron.BlockCount
, gpProtocolMagic :: !Crypto.ProtocolMagic
, gpTestnetBalance :: !Genesis.TestnetBalanceOptions
, gpFakeAvvmOptions :: !Genesis.FakeAvvmOptions
, gpAvvmBalanceFactor :: !Common.LovelacePortion
, gpTestnetBalance :: !Byron.TestnetBalanceOptions
, gpFakeAvvmOptions :: !Byron.FakeAvvmOptions
, gpAvvmBalanceFactor :: !Byron.LovelacePortion
, gpSeed :: !(Maybe Integer)
}
deriving Show

mkGenesisSpec :: GenesisParameters -> ExceptT ByronGenesisError IO Genesis.GenesisSpec
mkGenesisSpec :: GenesisParameters -> ExceptT ByronGenesisError IO Byron.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 []
Byron.mkGenesisDelegation []

withExceptT GenesisSpecError $
ExceptT . pure $
Genesis.mkGenesisSpec
(Genesis.GenesisAvvmBalances mempty)
Byron.mkGenesisSpec
(Byron.GenesisAvvmBalances mempty)
genesisDelegation
protocolParameters
(gpK gp)
(gpProtocolMagic gp)
(mkGenesisInitialiser True)
where
mkGenesisInitialiser :: Bool -> Genesis.GenesisInitializer
mkGenesisInitialiser :: Bool -> Byron.GenesisInitializer
mkGenesisInitialiser =
Genesis.GenesisInitializer
Byron.GenesisInitializer
(gpTestnetBalance gp)
(gpFakeAvvmOptions gp)
(Common.lovelacePortionToRational (gpAvvmBalanceFactor gp))
(Byron.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 (Byron.GenesisData, Byron.GeneratedSecrets)
mkGenesis gp = do
genesisSpec <- mkGenesisSpec gp

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

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

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

findDelegateCert :: SigningKey ByronKey -> ExceptT ByronGenesisError IO Certificate
findDelegateCert :: SigningKey ByronKey -> ExceptT ByronGenesisError IO Byron.Certificate
findDelegateCert bSkey@(ByronSigningKey sk) =
case List.find (isCertForSK sk) (Map.elems dlgCertMap) of
Nothing ->
Expand All @@ -219,8 +213,8 @@ 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 sk cert = delegateVK cert == Crypto.toVerification sk
isCertForSK :: Crypto.SigningKey -> Byron.Certificate -> Bool
isCertForSK sk cert = Byron.delegateVK cert == Crypto.toVerification sk

wOut :: String -> String -> (a -> ByteString) -> [a] -> IO ()
wOut = writeSecrets outDir
3 changes: 1 addition & 2 deletions cardano-cli/src/Cardano/CLI/Byron/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ where

import Cardano.Api.Byron

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 +73,7 @@ prettyPublicKey (ByronVerificationKey vk) =
% "\n public key (hex): "
% Crypto.fullVerificationKeyHexF
)
(Common.addressHash vk)
(addressHash vk)
vk
vk

Expand Down
Loading

0 comments on commit bd668b1

Please sign in to comment.