Skip to content

Commit

Permalink
Add DRepCredential and type aliases for ledger types
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Aug 18, 2023
1 parent 66026a8 commit 95b299d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
42 changes: 42 additions & 0 deletions cardano-api/internal/Cardano/Api/DRepMetadata.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
Expand All @@ -10,9 +11,21 @@ module Cardano.Api.DRepMetadata (
validateAndHashDRepMetadata,
DRepMetadataValidationError(..),

-- * DRep credential
DRepCredential(..),
toShelleyDRepCredential,
fromShelleyDRepCredential,

-- * Data family instances
AsType(..),
Hash(..),

-- * Auxilary aliases for ledger
Constitution,
GovState,
DRepState,
DRep,
CommitteeState
) where

import Cardano.Api.Eras
Expand All @@ -21,12 +34,17 @@ import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Praos
import Cardano.Api.Keys.Shelley (DRepKey)
import Cardano.Api.Script
import Cardano.Api.SerialiseRaw

import qualified Cardano.Crypto.Hash.Class as Crypto
import qualified Cardano.Ledger.Api as L
import qualified Cardano.Ledger.CertState as L
import qualified Cardano.Ledger.Credential as L
import Cardano.Ledger.Crypto (StandardCrypto)
import qualified Cardano.Ledger.Keys as Shelley
import qualified Cardano.Ledger.Shelley.Core as L

import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
Expand Down Expand Up @@ -90,3 +108,27 @@ validateAndHashDRepMetadata bs
let mdh = DRepMetadataHash (Crypto.hashWith id bs)
return (md, mdh)
| otherwise = Left $ DRepMetadataInvalidLengthError 512 (BS.length bs)


data DRepCredential
= DRepCredentialByKey (Hash DRepKey)
| DRepCredentialByScript ScriptHash
deriving (Eq, Ord, Show)

fromShelleyDRepCredential :: L.Credential Shelley.DRepRole StandardCrypto
-> DRepCredential
fromShelleyDRepCredential = \case
L.ScriptHashObj sh -> DRepCredentialByScript $ ScriptHash sh
L.KeyHashObj kh -> DRepCredentialByKey $ DRepKeyHash kh

toShelleyDRepCredential :: DRepCredential
-> L.Credential Shelley.DRepRole StandardCrypto
toShelleyDRepCredential = \case
DRepCredentialByScript (ScriptHash sh) -> L.ScriptHashObj sh
DRepCredentialByKey (DRepKeyHash sh) -> L.KeyHashObj sh

type Constitution era = L.Constitution (ShelleyLedgerEra era)
type GovState era = L.GovState (ShelleyLedgerEra era)
type DRepState = L.DRepState StandardCrypto
type DRep = L.DRep StandardCrypto
type CommitteeState era = L.CommitteeState (ShelleyLedgerEra era)
35 changes: 17 additions & 18 deletions cardano-api/internal/Cardano/Api/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ module Cardano.Api.Query (
import Cardano.Api.Address
import Cardano.Api.Block
import Cardano.Api.Certificate
import Cardano.Api.DRepMetadata
import Cardano.Api.EraCast
import Cardano.Api.Eras
import Cardano.Api.GenesisParameters
Expand All @@ -94,17 +95,12 @@ import Cardano.Api.TxBody
import Cardano.Api.Value

import qualified Cardano.Chain.Update.Validation.Interface as Byron.Update
import qualified Cardano.Ledger.Api as L
import Cardano.Ledger.Binary
import qualified Cardano.Ledger.Binary.Plain as Plain
import qualified Cardano.Ledger.CertState as L
import qualified Cardano.Ledger.Credential as L
import qualified Cardano.Ledger.Credential as Shelley
import Cardano.Ledger.Crypto (Crypto)
import qualified Cardano.Ledger.Keys as L
import qualified Cardano.Ledger.Shelley.API as Shelley
import qualified Cardano.Ledger.Shelley.Core as Core
import qualified Cardano.Ledger.Shelley.Core as L
import qualified Cardano.Ledger.Shelley.LedgerState as Shelley
import Cardano.Slotting.EpochInfo (hoistEpochInfo)
import Cardano.Slotting.Slot (WithOrigin (..))
Expand Down Expand Up @@ -300,21 +296,21 @@ data QueryInShelleyBasedEra era result where
-> QueryInShelleyBasedEra era (Map StakeCredential Lovelace)

QueryConstitution
:: QueryInShelleyBasedEra era (Maybe (L.Constitution (ShelleyLedgerEra era)))
:: QueryInShelleyBasedEra era (Maybe (Constitution era))

QueryGovState
:: QueryInShelleyBasedEra era (L.GovState (ShelleyLedgerEra era))
:: QueryInShelleyBasedEra era (GovState era)

QueryDRepState
:: Set (L.Credential L.DRepRole (Core.EraCrypto (ShelleyLedgerEra era)))
-> QueryInShelleyBasedEra era (Map StakeCredential (L.DRepState (ShelleyLedgerEra era)))
:: Set DRepCredential
-> QueryInShelleyBasedEra era (Map DRepCredential DRepState)

QueryDRepStakeDistr
:: Set (L.DRep (Core.EraCrypto (ShelleyLedgerEra era)))
-> QueryInShelleyBasedEra era (Map (L.DRep (Core.EraCrypto (ShelleyLedgerEra era))) Lovelace)
:: Set DRep
-> QueryInShelleyBasedEra era (Map DRep Lovelace)

QueryCommitteeState
:: QueryInShelleyBasedEra era (L.CommitteeState era)
:: QueryInShelleyBasedEra era (CommitteeState era)


instance NodeToClientVersionOf (QueryInShelleyBasedEra era result) where
Expand Down Expand Up @@ -680,7 +676,10 @@ toConsensusQueryShelleyBased erainmode QueryGovState =
Some (consensusQueryInEraInMode erainmode (Consensus.GetCBOR Consensus.GetGovState))

toConsensusQueryShelleyBased erainmode (QueryDRepState creds) =
Some (consensusQueryInEraInMode erainmode (Consensus.GetCBOR (Consensus.GetDRepState creds)))
Some (consensusQueryInEraInMode erainmode (Consensus.GetCBOR (Consensus.GetDRepState creds')))
where
creds' = Set.map toShelleyDRepCredential creds


toConsensusQueryShelleyBased erainmode (QueryDRepStakeDistr dreps) =
Some (consensusQueryInEraInMode erainmode (Consensus.GetCBOR $ Consensus.GetDRepStakeDistr dreps))
Expand Down Expand Up @@ -963,17 +962,17 @@ fromConsensusQueryResultShelleyBased _ QueryGovState{} q' govState' =

fromConsensusQueryResultShelleyBased _ QueryDRepState{} q' drepState' =
case q' of
Consensus.GetDRepState{} -> error "TODO" drepState'
_ -> fromConsensusQueryResultMismatch
Consensus.GetDRepState{} -> Map.mapKeysMonotonic fromShelleyDRepCredential drepState'
_ -> fromConsensusQueryResultMismatch

fromConsensusQueryResultShelleyBased _ QueryDRepStakeDistr{} q' stakeDistr' =
case q' of
Consensus.GetDRepStakeDistr{} -> error "TODO" stakeDistr'
_ -> fromConsensusQueryResultMismatch
Consensus.GetDRepStakeDistr{} -> Map.map fromShelleyLovelace stakeDistr'
_ -> fromConsensusQueryResultMismatch

fromConsensusQueryResultShelleyBased _ QueryCommitteeState{} q' committeeState' =
case q' of
Consensus.GetCommitteeState{} -> error "TODO" committeeState'
Consensus.GetCommitteeState{} -> committeeState'
_ -> fromConsensusQueryResultMismatch

-- | This should /only/ happen if we messed up the mapping in 'toConsensusQuery'
Expand Down
5 changes: 5 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,11 @@ module Cardano.Api (
DRepMetadata,
DRepMetadataReference,
DRepMetadataValidationError,
Constitution,
GovState,
DRepState,
DRep,
CommitteeState,
validateAndHashDRepMetadata,

-- ** Governance related certificates
Expand Down

0 comments on commit 95b299d

Please sign in to comment.