Skip to content

Commit

Permalink
Merge #2129
Browse files Browse the repository at this point in the history
2129: Initial CLI update for Allegra and Mary eras r=intricate a=dcoutts

Re-implement the tx build-raw command in terms of the new era-generic TxBodyContent API.

This should allow it to work for all eras, including Byron. It checks that each feature can be used in the selected era.

There are still a number of TODOs, but this establishes the pattern.

Co-authored-by: Duncan Coutts <[email protected]>
Co-authored-by: Jordan Millar <[email protected]>
  • Loading branch information
3 people authored Nov 26, 2020
2 parents a48e28e + 9c9a10a commit d0b9bb6
Show file tree
Hide file tree
Showing 13 changed files with 877 additions and 180 deletions.
27 changes: 23 additions & 4 deletions cardano-api/src/Cardano/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ module Cardano.API (
AllegraEra,
MaryEra,
CardanoEra(..),
CardanoEraStyle(..),
IsCardanoEra(..),
InAnyCardanoEra(..),

-- ** Shelley-based eras
ShelleyBasedEra(..),
IsShelleyBasedEra(..),
ShelleyLedgerEra,
InAnyShelleyBasedEra(..),
CardanoEraStyle(..),
cardanoEraStyle,

-- ** Deprecated
Byron,
Shelley,
Expand Down Expand Up @@ -112,6 +116,7 @@ module Cardano.API (
lovelaceToQuantity,
selectLovelace,
lovelaceToValue,
valueToLovelace,

-- * Building transactions
-- | Constructing and inspecting transactions
Expand All @@ -120,6 +125,7 @@ module Cardano.API (
TxBody,
makeTransactionBody,
TxBodyContent(..),
TxBodyError(..),

-- ** Transaction Ids
TxId,
Expand All @@ -146,9 +152,10 @@ module Cardano.API (
TxMintValue(..),

-- ** Era-dependent transaction body features
OnlyAdaSupportedInEra(..),
MultiAssetSupportedInEra(..),
TxFeesExplicitInEra (..),
OnlyAdaSupportedInEra(..),
TxFeesExplicitInEra(..),
TxFeesImplicitInEra(..),
ValidityUpperBoundSupportedInEra(..),
ValidityNoUpperBoundSupportedInEra(..),
ValidityLowerBoundSupportedInEra(..),
Expand All @@ -158,6 +165,18 @@ module Cardano.API (
CertificatesSupportedInEra(..),
UpdateProposalSupportedInEra(..),

-- ** Feature availability functions
multiAssetSupportedInEra,
txFeesExplicitInEra,
validityUpperBoundSupportedInEra,
validityNoUpperBoundSupportedInEra,
validityLowerBoundSupportedInEra,
txMetadataSupportedInEra,
auxScriptsSupportedInEra,
withdrawalsSupportedInEra,
certificatesSupportedInEra,
updateProposalSupportedInEra,

-- * Signing transactions
-- | Creating transaction witnesses one by one, or all in one go.
Tx,
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ anyAddressInEra (AddressByron addr) =
Just (AddressInEra ByronAddressInAnyEra addr)

anyAddressInEra (AddressShelley addr) =
case cardanoEraStyle of
case cardanoEraStyle cardanoEra of
LegacyByronEra -> Nothing
ShelleyBasedEra era -> Just (AddressInEra (ShelleyAddressInEra era) addr)

Expand Down
93 changes: 70 additions & 23 deletions cardano-api/src/Cardano/Api/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module Cardano.Api.Eras
, AllegraEra
, MaryEra
, CardanoEra(..)
, CardanoEraStyle(..)
, IsCardanoEra(..)
, InAnyCardanoEra(..)

-- * Deprecated aliases
, Byron
Expand All @@ -25,15 +25,24 @@ module Cardano.Api.Eras
-- * Shelley-based eras
, ShelleyBasedEra(..)
, IsShelleyBasedEra(..)
, InAnyShelleyBasedEra(..)

-- ** Mapping to era types from the Shelley ledger library
, ShelleyLedgerEra

-- * Cardano eras, as Byron vs Shelley-based
, CardanoEraStyle(..)
, cardanoEraStyle

-- * Data family instances
, AsType(AsByronEra, AsShelleyEra, AsAllegraEra, AsMaryEra,
AsByron, AsShelley, AsAllegra, AsMary)
) where

import Prelude

import Data.Type.Equality (TestEquality(..), (:~:)(Refl))

import Ouroboros.Consensus.Shelley.Eras as Ledger
(StandardShelley, StandardAllegra, StandardMary)

Expand Down Expand Up @@ -125,47 +134,43 @@ deriving instance Eq (CardanoEra era)
deriving instance Ord (CardanoEra era)
deriving instance Show (CardanoEra era)


-- | This is the same essential information as 'CardanoEra' but instead of a
-- flat set of alternative eras, it is factored into the legcy Byron era and
-- the current Shelley-based eras.
--
-- This way of factoring the eras is useful because in many cases the
-- major differences are between the Byron and Shelley-based eras, and
-- the Shelley-based eras can often be treated uniformly.
--
data CardanoEraStyle era where
LegacyByronEra :: CardanoEraStyle ByronEra
ShelleyBasedEra :: ShelleyBasedEra era -> CardanoEraStyle era

deriving instance Eq (CardanoEraStyle era)
deriving instance Ord (CardanoEraStyle era)
deriving instance Show (CardanoEraStyle era)
instance TestEquality CardanoEra where
testEquality ByronEra ByronEra = Just Refl
testEquality ShelleyEra ShelleyEra = Just Refl
testEquality AllegraEra AllegraEra = Just Refl
testEquality MaryEra MaryEra = Just Refl
testEquality _ _ = Nothing


-- | The class of Cardano eras. This allows uniform handling of all Cardano
-- eras, but also non-uniform by making case distinctions on the 'CardanoEra'
-- or 'CardanoEraStyle' constructors.
-- constructors, or the 'CardanoEraStyle' constructors via `cardanoEraStyle`.
--
class HasTypeProxy era => IsCardanoEra era where
cardanoEra :: CardanoEra era
cardanoEraStyle :: CardanoEraStyle era

instance IsCardanoEra ByronEra where
cardanoEra = ByronEra
cardanoEraStyle = LegacyByronEra

instance IsCardanoEra ShelleyEra where
cardanoEra = ShelleyEra
cardanoEraStyle = ShelleyBasedEra ShelleyBasedEraShelley

instance IsCardanoEra AllegraEra where
cardanoEra = AllegraEra
cardanoEraStyle = ShelleyBasedEra ShelleyBasedEraAllegra

instance IsCardanoEra MaryEra where
cardanoEra = MaryEra
cardanoEraStyle = ShelleyBasedEra ShelleyBasedEraMary


-- | This pairs up some era-dependent type with a 'CardanoEra' value that tells
-- us what era it is, but hides the era type. This is useful when the era is
-- not statically known, for example when deserialising from a file.
--
data InAnyCardanoEra thing where
InAnyCardanoEra :: IsCardanoEra era -- Provide class constraint
=> CardanoEra era -- and explicit value.
-> thing era
-> InAnyCardanoEra thing


-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -206,6 +211,48 @@ instance IsShelleyBasedEra MaryEra where
shelleyBasedEra = ShelleyBasedEraMary


-- | This pairs up some era-dependent type with a 'ShelleyBasedEra' value that
-- tells us what era it is, but hides the era type. This is useful when the era
-- is not statically known, for example when deserialising from a file.
--
data InAnyShelleyBasedEra thing where
InAnyShelleyBasedEra :: IsShelleyBasedEra era -- Provide class constraint
=> ShelleyBasedEra era -- and explicit value.
-> thing era
-> InAnyShelleyBasedEra thing


-- ----------------------------------------------------------------------------
-- Cardano eras factored as Byron vs Shelley-based
--

-- | This is the same essential information as 'CardanoEra' but instead of a
-- flat set of alternative eras, it is factored into the legcy Byron era and
-- the current Shelley-based eras.
--
-- This way of factoring the eras is useful because in many cases the
-- major differences are between the Byron and Shelley-based eras, and
-- the Shelley-based eras can often be treated uniformly.
--
data CardanoEraStyle era where
LegacyByronEra :: CardanoEraStyle ByronEra
ShelleyBasedEra :: IsShelleyBasedEra era -- Also provide class constraint
=> ShelleyBasedEra era
-> CardanoEraStyle era

deriving instance Eq (CardanoEraStyle era)
deriving instance Ord (CardanoEraStyle era)
deriving instance Show (CardanoEraStyle era)

-- | The 'CardanoEraStyle' for a 'CardanoEra'.
--
cardanoEraStyle :: CardanoEra era -> CardanoEraStyle era
cardanoEraStyle ByronEra = LegacyByronEra
cardanoEraStyle ShelleyEra = ShelleyBasedEra ShelleyBasedEraShelley
cardanoEraStyle AllegraEra = ShelleyBasedEra ShelleyBasedEraAllegra
cardanoEraStyle MaryEra = ShelleyBasedEra ShelleyBasedEraMary


-- ----------------------------------------------------------------------------
-- Conversion to Shelley ledger library types
--
Expand Down
21 changes: 21 additions & 0 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Cardano.Api.Script (
, SignatureFeature
, TimeLocksFeature
, HasScriptFeatures
, coerceSimpleScriptEra

-- * Deprecated aliases
, MultiSigScript
Expand Down Expand Up @@ -372,6 +373,26 @@ timelockToSimpleScript signaturesInEra timeLocksInEra = go
go (Timelock.RequireMOf i s) = RequireMOf i (map go (toList s))


--TODO: eliminate the need for this
coerceSimpleScriptEra :: forall era.
ShelleyBasedEra era
-> SimpleScript ShelleyEra
-> SimpleScript era
coerceSimpleScriptEra era = go
where
go :: SimpleScript ShelleyEra -> SimpleScript era
go (RequireSignature _ pkh) = RequireSignature signaturesFeature pkh
go (RequireAllOf s) = RequireAllOf (map go s)
go (RequireAnyOf s) = RequireAnyOf (map go s)
go (RequireMOf m s) = RequireMOf m (map go s)

signaturesFeature :: ScriptFeatureInEra SignatureFeature era
signaturesFeature =
case era of
ShelleyBasedEraShelley -> SignaturesInShelleyEra
ShelleyBasedEraAllegra -> SignaturesInAllegraEra
ShelleyBasedEraMary -> SignaturesInMaryEra


--
-- JSON serialisation
Expand Down
Loading

0 comments on commit d0b9bb6

Please sign in to comment.