Skip to content

Commit

Permalink
Move this commit to a different branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jul 31, 2024
1 parent e4fdbbd commit eb4e150
Show file tree
Hide file tree
Showing 8 changed files with 603 additions and 360 deletions.
4 changes: 2 additions & 2 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ library internal
Cardano.Api.Orphans
Cardano.Api.Pretty
Cardano.Api.Protocol
Cardano.Api.Protocol.Version
Cardano.Api.ProtocolParameters
Cardano.Api.Query
Cardano.Api.Query.Expr
Expand All @@ -132,6 +131,8 @@ library internal
Cardano.Api.ReexposeNetwork
Cardano.Api.Rewards
Cardano.Api.Script
Cardano.Api.Experimental.Eras
Cardano.Api.Experimental.Tx
Cardano.Api.ScriptData
Cardano.Api.SerialiseBech32
Cardano.Api.SerialiseCBOR
Expand Down Expand Up @@ -236,7 +237,6 @@ library
Cardano.Api.ChainSync.Client
Cardano.Api.ChainSync.ClientPipelined
Cardano.Api.Crypto.Ed25519Bip32
Cardano.Api.Experimental
Cardano.Api.Ledger
Cardano.Api.Network
Cardano.Api.Shelley
Expand Down
17 changes: 14 additions & 3 deletions cardano-api/internal/Cardano/Api/Convenience/Construction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ where
import Cardano.Api.Address
import Cardano.Api.Certificate
import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eras
import Cardano.Api.Experimental.Eras (sbeToEra)
import Cardano.Api.Experimental.Tx
import Cardano.Api.Fees
import Cardano.Api.ProtocolParameters
import Cardano.Api.Query
Expand All @@ -31,6 +34,7 @@ import qualified Cardano.Ledger.Keys as L

import qualified Data.List as List
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
Expand Down Expand Up @@ -72,7 +76,9 @@ constructBalancedTx
stakeDelegDeposits
drepDelegDeposits
shelleyWitSigningKeys = do
BalancedTxBody _ txbody _txBalanceOutput _fee <-
let availableEra = fromMaybe (error "TODO") $ sbeToEra sbe

BalancedTxBody _ unsignedTx _txBalanceOutput _fee <-
makeTransactionBodyAutoBalance
sbe
systemStart
Expand All @@ -86,8 +92,13 @@ constructBalancedTx
changeAddr
mOverrideWits

let keyWits = map (makeShelleyKeyWitness sbe txbody) shelleyWitSigningKeys
return $ makeSignedTransaction keyWits txbody
let alternateKeyWits = map (makeKeyWitness availableEra unsignedTx) shelleyWitSigningKeys
signedTx = signTx availableEra [] alternateKeyWits unsignedTx

caseShelleyToAlonzoOrBabbageEraOnwards
(const $ error "constructBalancedTx: TODO Fail")
(\w -> return $ ShelleyTx sbe $ obtainShimConstraints w signedTx)
sbe

data TxInsExistError
= TxInsDoNotExist [TxIn]
Expand Down
119 changes: 119 additions & 0 deletions cardano-api/internal/Cardano/Api/Experimental/Eras.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilyDependencies #-}
-- UndecidableInstances needed for 9.2.7 and 8.10.7
{-# LANGUAGE UndecidableInstances #-}
-- Only for UninhabitableType
{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}

-- | This module defines the protocol versions corresponding to the eras in the Cardano blockchain.
module Cardano.Api.Experimental.Eras
( BabbageEra
, ConwayEra
, Era (..)
, ToConstrainedEra
, UseEra
, AvailableErasToSbe
, SbeToAvailableEras
, useEra
, protocolVersionToSbe
, sbeToEra
)
where

import Cardano.Api.Eon.ShelleyBasedEra (ShelleyBasedEra (..))
import qualified Cardano.Api.Eras.Core as Api

import qualified Cardano.Ledger.Babbage as Ledger
import qualified Cardano.Ledger.Conway as Ledger

import Data.Kind

-- | Users typically interact with the latest features on the mainnet or experiment with features
-- from the upcoming era. Hence, the protocol versions are limited to the current mainnet era
-- and the next era (upcoming era).
data BabbageEra

data ConwayEra

-- Allows us to gradually change the api without breaking things.
-- This will eventually be removed.
type family AvailableErasToSbe era = (r :: Type) | r -> era where
AvailableErasToSbe BabbageEra = Api.BabbageEra
AvailableErasToSbe ConwayEra = Api.ConwayEra

type family SbeToAvailableEras era = (r :: Type) | r -> era where
SbeToAvailableEras Api.BabbageEra = BabbageEra
SbeToAvailableEras Api.ConwayEra = ConwayEra

type family ToConstrainedEra era = (r :: Type) | r -> era where
ToConstrainedEra BabbageEra = Ledger.Babbage
ToConstrainedEra ConwayEra = Ledger.Conway

-- | Represents the eras in Cardano's blockchain.
-- This type represents eras currently on mainnet and new eras which are
-- in development.
--
-- After a hardfork, the from which we hardfork from gets deprecated and
-- after deprecation period, gets removed. During deprecation period,
-- consumers of cardano-api should update their codebase to the mainnet era.
data Era era where
-- | The era currently active on Cardano's mainnet.
BabbageEra :: Era BabbageEra
-- | The upcoming era in development.
ConwayEra :: Era ConwayEra

-- | How to deprecate an era
--
-- 1. Add DEPRECATED pragma to the era type tag and the era constructor at the same time:
-- @
-- {-# DEPRECATED BabbageEra "BabbageEra no longer supported, use ConwayEra" #-}
-- data BabbageEra
-- @
--
-- 2. Update haddock for the constructor of the deprecated era, mentioning deprecation.
--
-- @
-- data Era era where
-- {-# DEPRECATED BabbageEra "BabbageEra no longer supported, use ConwayEra" #-}
-- BabbageEra :: Era BabbageEra
-- -- | The era currently active on Cardano's mainnet.
-- ConwayEra :: Era ConwayEra
-- @
--
-- 3. Add new 'UseEra' instance and update the deprecated era instance to produce a compile-time error:
-- @
-- instance TypeError ('Text "UseEra BabbageEra: Deprecated. Update to ConwayEra") => UseEra BabbageEra where
-- useEra = error "unreachable"
--
-- instance UseEra ConwayEra where
-- useEra = ConwayEra
-- @
protocolVersionToSbe
:: Era era
-> ShelleyBasedEra (AvailableErasToSbe era)
protocolVersionToSbe BabbageEra = ShelleyBasedEraBabbage
protocolVersionToSbe ConwayEra = ShelleyBasedEraConway

sbeToEra :: ShelleyBasedEra era -> Maybe (Era (SbeToAvailableEras era))
sbeToEra ShelleyBasedEraBabbage = Just BabbageEra
sbeToEra ShelleyBasedEraConway = Just ConwayEra
sbeToEra _ = Nothing

-------------------------------------------------------------------------

-- | Type class interface for the 'Era' type.
class UseEra era where
useEra :: Era era

instance UseEra BabbageEra where
useEra = BabbageEra

instance UseEra ConwayEra where
useEra = ConwayEra
Loading

0 comments on commit eb4e150

Please sign in to comment.