Skip to content

Commit d43da78

Browse files
committed
Fill in undefineds
1 parent ee5eccf commit d43da78

File tree

6 files changed

+118
-25
lines changed

6 files changed

+118
-25
lines changed

cardano-api/src/Cardano/Api/Certificate/Internal.hs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ module Cardano.Api.Certificate.Internal
6767
, fromShelleyCertificate
6868
, toShelleyPoolParams
6969
, fromShelleyPoolParams
70+
, fromShelleyStakePoolState
7071

7172
-- * Data family instances
7273
, AsType (..)
@@ -100,6 +101,7 @@ import Cardano.Ledger.Api qualified as L
100101
import Cardano.Ledger.BaseTypes (strictMaybe)
101102
import Cardano.Ledger.Coin qualified as L
102103
import Cardano.Ledger.Keys qualified as Ledger
104+
import Cardano.Ledger.State qualified as Ledger
103105

104106
import Control.Monad.Except (MonadError (..))
105107
import Data.ByteString (ByteString)
@@ -781,6 +783,74 @@ fromShelleyPoolParams
781783
Text.encodeUtf8
782784
. Ledger.dnsToText
783785

786+
fromShelleyStakePoolState
787+
:: Ledger.KeyHash Ledger.StakePool
788+
-> Ledger.StakePoolState
789+
-> StakePoolParameters
790+
fromShelleyStakePoolState
791+
poolId
792+
Ledger.StakePoolState
793+
{ Ledger.spsVrf
794+
, Ledger.spsPledge
795+
, Ledger.spsCost
796+
, Ledger.spsMargin
797+
, Ledger.spsRewardAccount
798+
, Ledger.spsOwners
799+
, Ledger.spsRelays
800+
, Ledger.spsMetadata
801+
} =
802+
StakePoolParameters
803+
{ stakePoolId = StakePoolKeyHash poolId
804+
, stakePoolVRF = VrfKeyHash (Ledger.fromVRFVerKeyHash spsVrf)
805+
, stakePoolCost = spsCost
806+
, stakePoolMargin = Ledger.unboundRational spsMargin
807+
, stakePoolRewardAccount = fromShelleyStakeAddr spsRewardAccount
808+
, stakePoolPledge = spsPledge
809+
, stakePoolOwners = map StakeKeyHash (toList spsOwners)
810+
, stakePoolRelays =
811+
map
812+
fromShelleyStakePoolRelay
813+
(toList spsRelays)
814+
, stakePoolMetadata =
815+
fromShelleyPoolMetadata
816+
<$> Ledger.strictMaybeToMaybe spsMetadata
817+
}
818+
where
819+
fromShelleyStakePoolRelay :: Ledger.StakePoolRelay -> StakePoolRelay
820+
fromShelleyStakePoolRelay (Ledger.SingleHostAddr mport mipv4 mipv6) =
821+
StakePoolRelayIp
822+
(Ledger.strictMaybeToMaybe mipv4)
823+
(Ledger.strictMaybeToMaybe mipv6)
824+
(fromIntegral . Ledger.portToWord16 <$> Ledger.strictMaybeToMaybe mport)
825+
fromShelleyStakePoolRelay (Ledger.SingleHostName mport dnsname) =
826+
StakePoolRelayDnsARecord
827+
(fromShelleyDnsName dnsname)
828+
(fromIntegral . Ledger.portToWord16 <$> Ledger.strictMaybeToMaybe mport)
829+
fromShelleyStakePoolRelay (Ledger.MultiHostName dnsname) =
830+
StakePoolRelayDnsSrvRecord
831+
(fromShelleyDnsName dnsname)
832+
833+
fromShelleyPoolMetadata :: Ledger.PoolMetadata -> StakePoolMetadataReference
834+
fromShelleyPoolMetadata
835+
Ledger.PoolMetadata
836+
{ Ledger.pmUrl
837+
, Ledger.pmHash
838+
} =
839+
StakePoolMetadataReference
840+
{ stakePoolMetadataURL = Ledger.urlToText pmUrl
841+
, stakePoolMetadataHash =
842+
StakePoolMetadataHash
843+
. fromMaybe (error "fromShelleyPoolMetadata: invalid hash. TODO: proper validation")
844+
. Ledger.hashFromBytes
845+
$ pmHash
846+
}
847+
848+
-- TODO: change the ledger rep of the DNS name to use ShortByteString
849+
fromShelleyDnsName :: Ledger.DnsName -> ByteString
850+
fromShelleyDnsName =
851+
Text.encodeUtf8
852+
. Ledger.dnsToText
853+
784854
data AnchorDataFromCertificateError
785855
= InvalidPoolMetadataHashError Ledger.Url ByteString
786856
deriving (Eq, Show)

cardano-api/src/Cardano/Api/Experimental/Tx.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ newtype UnsignedTxError
214214
= UnsignedTxError TxBodyError
215215

216216
makeUnsignedTx
217-
:: Ledger.ProtVerAtMost (LedgerEra era) 11
218-
=> Era era
217+
:: Era era
219218
-> TxBodyContent BuildTx era
220219
-> Either TxBodyError (UnsignedTx era)
221-
makeUnsignedTx era bc = obtainCommonConstraints era $ do
220+
makeUnsignedTx DijkstraEra _ = error "makeUnsignedTx: Dijkstra era not supported yet"
221+
makeUnsignedTx era@ConwayEra bc = obtainCommonConstraints era $ do
222222
let sbe = convert era
223223
aeon = convert era
224224
TxScriptWitnessRequirements languages scripts datums redeemers <-

cardano-api/src/Cardano/Api/Internal/Orphans/Serialisation.hs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ import Cardano.Chain.Update.Validation.Voting qualified as L.Voting
4545
import Cardano.Crypto.Hash qualified as Crypto
4646
import Cardano.Ledger.Allegra.Rules qualified as L
4747
import Cardano.Ledger.Alonzo.PParams qualified as Ledger
48+
import Cardano.Ledger.Alonzo.Rules qualified as Alonzo
4849
import Cardano.Ledger.Alonzo.Rules qualified as L
4950
import Cardano.Ledger.Alonzo.Tx qualified as L
5051
import Cardano.Ledger.Api qualified as L
5152
import Cardano.Ledger.Babbage.PParams qualified as Ledger
53+
import Cardano.Ledger.Babbage.Rules qualified as Babbage
5254
import Cardano.Ledger.Babbage.Rules qualified as L
5355
import Cardano.Ledger.BaseTypes (strictMaybeToMaybe)
5456
import Cardano.Ledger.BaseTypes qualified as L
@@ -91,11 +93,20 @@ import PlutusLedgerApi.V2 qualified as V2
9193

9294
import Codec.Binary.Bech32 qualified as Bech32
9395
import Codec.CBOR.Read qualified as CBOR
94-
import Data.Aeson (KeyValue ((.=)), ToJSON (..), ToJSONKey (..), object, pairs)
96+
import Data.Aeson
97+
( KeyValue ((.=))
98+
, ToJSON (..)
99+
, ToJSONKey (..)
100+
, defaultOptions
101+
, genericToJSON
102+
, object
103+
, pairs
104+
)
95105
import Data.Aeson qualified as A
96106
import Data.Aeson qualified as Aeson
97107
import Data.Bifunctor
98108
import Data.ByteString qualified as BS
109+
import Data.ByteString.Base16 qualified as B16
99110
import Data.ByteString.Base16 qualified as Base16
100111
import Data.ByteString.Char8 qualified as C8
101112
import Data.ByteString.Short qualified as SBS
@@ -106,6 +117,7 @@ import Data.ListMap qualified as ListMap
106117
import Data.Maybe.Strict (StrictMaybe (..))
107118
import Data.Monoid
108119
import Data.Text qualified as T
120+
import Data.Text qualified as Text
109121
import Data.Text.Encoding qualified as Text
110122
import Data.Typeable (Typeable)
111123
import GHC.Exts (IsList (..), IsString (..))
@@ -200,17 +212,22 @@ instance
200212
, ToJSON (L.PlutusPurpose L.AsItem ledgerera)
201213
, ToJSON (L.PlutusPurpose L.AsIx ledgerera)
202214
)
203-
=> ToJSON (L.AlonzoUtxowPredFailure ledgerera) where
204-
toJSON = undefined
215+
=> ToJSON (L.AlonzoUtxowPredFailure ledgerera)
216+
where
217+
toJSON = genericToJSON defaultOptions
218+
219+
instance ToJSON C8.ByteString where
220+
toJSON = Aeson.String . Text.decodeLatin1 . B16.encode
205221

206222
instance
207223
( ToJSON (L.PredicateFailure (L.EraRule "UTXO" ledgerera))
208224
, ToJSON (L.TxCert ledgerera)
209225
, ToJSON (L.PlutusPurpose L.AsItem ledgerera)
210226
, ToJSON (L.PlutusPurpose L.AsIx ledgerera)
211227
)
212-
=> ToJSON (L.BabbageUtxowPredFailure ledgerera) where
213-
toJSON = undefined
228+
=> ToJSON (L.BabbageUtxowPredFailure ledgerera)
229+
where
230+
toJSON = genericToJSON defaultOptions
214231

215232
deriving anyclass instance
216233
ToJSON (L.PredicateFailure (L.EraRule "LEDGER" ledgerera))

cardano-api/src/Cardano/Api/Query/Internal/Type/QueryInMode.hs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,11 @@ toConsensusQueryShelleyBased sbe = \case
643643
)
644644
(const $ Some (consensusQueryInEraInMode era Consensus.GetFuturePParams))
645645
sbe
646-
QueryDRepState _creds ->
646+
QueryDRepState creds ->
647647
caseShelleyToBabbageOrConwayEraOnwards
648648
(const $ error "toConsensusQueryShelleyBased: QueryDRepState is only available in the Conway era")
649649
( \w ->
650-
Some (consensusQueryInEraInMode era (conwayEraOnwardsConstraints w $ Consensus.GetDRepState _creds))
650+
Some (consensusQueryInEraInMode era (conwayEraOnwardsConstraints w $ Consensus.GetDRepState creds))
651651
)
652652
sbe
653653
QueryDRepStakeDistr dreps ->
@@ -664,16 +664,15 @@ toConsensusQueryShelleyBased sbe = \case
664664
)
665665
(const $ Some (consensusQueryInEraInMode era (Consensus.GetSPOStakeDistr spos)))
666666
sbe
667-
QueryCommitteeMembersState _coldCreds _hotCreds _statuses ->
667+
QueryCommitteeMembersState coldCreds hotCreds statuses ->
668668
caseShelleyToBabbageOrConwayEraOnwards
669669
( const $
670670
error "toConsensusQueryShelleyBased: QueryCommitteeMembersState is only available in the Conway era"
671671
)
672-
undefined
673-
-- ( const $
674-
-- Some
675-
-- (consensusQueryInEraInMode era (Consensus.GetCommitteeMembersState coldCreds hotCreds statuses))
676-
-- )
672+
( const $
673+
Some
674+
(consensusQueryInEraInMode era (Consensus.GetCommitteeMembersState coldCreds hotCreds statuses))
675+
)
677676
sbe
678677
QueryStakeVoteDelegatees creds ->
679678
caseShelleyToBabbageOrConwayEraOnwards
@@ -933,10 +932,10 @@ fromConsensusQueryResultShelleyBased sbe sbeQuery q' r' =
933932
_ -> fromConsensusQueryResultMismatch
934933
QueryStakePoolParameters{} ->
935934
case q' of
936-
Consensus.GetStakePoolParams{} ->
937-
Map.map fromShelleyPoolParams
938-
. Map.mapKeysMonotonic StakePoolKeyHash
939-
$ undefined -- r'
935+
Consensus.GetStakePoolParams{} -> do
936+
Map.mapKeysMonotonic StakePoolKeyHash
937+
. Map.mapWithKey fromShelleyStakePoolState
938+
$ r'
940939
_ -> fromConsensusQueryResultMismatch
941940
QueryDebugLedgerState{} ->
942941
case q' of

cardano-api/src/Cardano/Api/Tx/Internal/Body.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ import Cardano.Crypto.Hashing qualified as Byron
272272
import Cardano.Ledger.Allegra.Core qualified as L
273273
import Cardano.Ledger.Alonzo.Core qualified as L
274274
import Cardano.Ledger.Alonzo.Scripts qualified as Alonzo
275+
import Cardano.Ledger.Alonzo.Tx qualified as L
275276
-- import Cardano.Ledger.Alonzo.Tx qualified as Alonzo (hashScriptIntegrity)
276277
import Cardano.Ledger.Alonzo.TxWits qualified as Alonzo
277278
import Cardano.Ledger.Api qualified as L
@@ -1981,13 +1982,13 @@ convPParamsToScriptIntegrityHash
19811982
-> Alonzo.TxDats (ShelleyLedgerEra era)
19821983
-> Set Plutus.Language
19831984
-> StrictMaybe L.ScriptIntegrityHash
1984-
convPParamsToScriptIntegrityHash w (BuildTxWith mTxProtocolParams) _ _ _ = -- redeemers datums languages =
1985+
convPParamsToScriptIntegrityHash w (BuildTxWith mTxProtocolParams) redeemers datums languages =
19851986
alonzoEraOnwardsConstraints w $
19861987
case mTxProtocolParams of
19871988
Nothing -> SNothing
1988-
Just (LedgerProtocolParameters _) -> undefined
1989-
-- Just (LedgerProtocolParameters pp) ->
1990-
-- Alonzo.hashScriptIntegrity (Set.map (L.getLanguageView pp) languages) redeemers datums
1989+
Just (LedgerProtocolParameters pp) ->
1990+
let scriptIntegrity = L.ScriptIntegrity redeemers datums (Set.map (L.getLanguageView pp) languages)
1991+
in SJust $ L.hashScriptIntegrity scriptIntegrity
19911992

19921993
convLanguages :: [(ScriptWitnessIndex, AnyScriptWitness era)] -> Set Plutus.Language
19931994
convLanguages witnesses =

cardano-api/src/Cardano/Api/Tx/Internal/Body/Lens.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE GADTs #-}
23
{-# LANGUAGE RankNTypes #-}
34

45
{- HLINT ignore "Eta reduce" -}
56

7+
-- TODO: Deprecate all the lenses that use eons. Explore parameterizing them on `Era era` instead.
8+
69
module Cardano.Api.Tx.Internal.Body.Lens
710
( -- * Types
811
LedgerTxBody (..)
@@ -163,7 +166,10 @@ collateralInputsTxBodyL w = alonzoEraOnwardsConstraints w $ txBodyL . L.collater
163166

164167
reqSignerHashesTxBodyL
165168
:: AlonzoEraOnwards era -> Lens' (LedgerTxBody era) (Set (L.KeyHash L.Witness))
166-
reqSignerHashesTxBodyL w = alonzoEraOnwardsConstraints w $ txBodyL . undefined -- L.reqSignerHashesTxBodyL
169+
reqSignerHashesTxBodyL w@AlonzoEraOnwardsAlonzo = alonzoEraOnwardsConstraints w $ txBodyL . L.reqSignerHashesTxBodyL
170+
reqSignerHashesTxBodyL w@AlonzoEraOnwardsBabbage = alonzoEraOnwardsConstraints w $ txBodyL . L.reqSignerHashesTxBodyL
171+
reqSignerHashesTxBodyL w@AlonzoEraOnwardsConway = alonzoEraOnwardsConstraints w $ txBodyL . L.reqSignerHashesTxBodyL
172+
reqSignerHashesTxBodyL AlonzoEraOnwardsDijkstra = error "reqSignerHashesTxBodyL: DijkstraEra not supported yet"
167173

168174
referenceInputsTxBodyL
169175
:: BabbageEraOnwards era -> Lens' (LedgerTxBody era) (Set L.TxIn)

0 commit comments

Comments
 (0)