Skip to content

Commit

Permalink
Update dependencies and wire up new auxiliary scripts feature
Browse files Browse the repository at this point in the history
Visible changes:
* IntersectMBO/cardano-ledger#1993
* IntersectMBO/cardano-ledger#2021

One of the changes in Allegra over Shelley is the notion of transaction
auxiliary data. We retrospectively define tx metadata to be part of the
auxiliary data where in the Shelley era this consists only of the
existing tx metadata.

But from the Allegra era the auxiliary data contains both the tx
metadata and also auxiliary scripts. So where we previously hashed the
tx metadata, we now hash the auxiliary data.

Take the opportunity to refactor the tx metadata representation and
conversion functions to fit better with the new scheme.

Also take the opportunity to tidy up the imports in the Query module
since we have some very similarly-named type classes.

Co-authored-by: Duncan Coutts <[email protected]>
  • Loading branch information
mrBliss and dcoutts committed Dec 1, 2020
1 parent c2bbcbe commit a6829f3
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 133 deletions.
13 changes: 6 additions & 7 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ package io-sim-classes
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-base
tag: 980e6ca052c8dbad91336b6230c58b78ac1032f5
--sha256: 0vid3qi20pfadjxqmklanbh701avjbclx549cqb38dvmz5p0g0xi
tag: 2574600da11065937c1f07e4b234ecb451016a2e
--sha256: 0nq8bpzsr3fd2i59a6s6qb6slpymjh47zv57wlifjfvhh0xlgmpx
subdir:
binary
binary/test
Expand All @@ -109,8 +109,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-ledger-specs
tag: 7c64ed8ca2e2141780326ac2990f59ab942356af
--sha256: 0md1lm50jyg0navmw9yazzsvw4xrbzvsvawlk91rmpsxfscn92yz
tag: 9cd2b6cebfd19e73974a149ca25186aa8eaa036d
--sha256: 0sy9yqgr6izfw2a7bvpcj65035a1ijhidgsy55j2xfr02h29yjgh
subdir:
byron/chain/executable-spec
byron/crypto
Expand Down Expand Up @@ -158,8 +158,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/input-output-hk/ouroboros-network
tag: 680b1725cdc10d6ae09b311509ef7c59c93372fd
--sha256: 0pm7cz0ww2hap3c88p1r2kx9nnd8x86wj9q3f7jsjsdca2r3ki9a
tag: 7e00984293b51fed8e72683607d8b9e0717382bd
--sha256: 1m89ih9m4p48nw1mybzf3ssr5va9ppsskx9pl9130aczhhi5ix85
subdir:
io-sim
io-sim-classes
Expand All @@ -182,7 +182,6 @@ constraints:
-- systemd-2.3.0 requires at least network 3.1.1.0 but it doesn't declare
-- that dependency
, network >= 3.1.1.0
, streaming-bytestring < 0.1.7

package comonad
flags: -test-doctests
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ module Cardano.API (

-- * Transaction metadata
-- | Embedding additional structured data within transactions.
TxMetadata(TxMetadata),
TxMetadata(..),

-- ** Constructing metadata
TxMetadataValue(..),
Expand Down
21 changes: 0 additions & 21 deletions cardano-api/src/Cardano/Api/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ module Cardano.Api.Shelley

-- * Transaction metadata
-- | Embedding additional structured data within transactions.
TxMetadata
( TxMetadata
, TxMetadataShelley
),
toShelleyMetaData,
fromShelleyMetaData,

Expand Down Expand Up @@ -154,20 +150,3 @@ import Cardano.Api.Typed
import Cardano.Api.Address
import Cardano.Api.Value


-- For the deprecated functions below
import Prelude
import Data.Word
import Data.Map (Map)
import qualified Shelley.Spec.Ledger.MetaData as Shelley

{-# DEPRECATED toShelleyMetaData
"Use the 'TxMetadata' and 'TxMetadataShelley' constructors" #-}
toShelleyMetaData :: Map Word64 TxMetadataValue -> Shelley.MetaData
toShelleyMetaData = (\(TxMetadataShelley m) -> m) . TxMetadata

{-# DEPRECATED fromShelleyMetaData
"Use the 'TxMetadata' and 'TxMetadataShelley' constructors" #-}
fromShelleyMetaData :: Shelley.MetaData -> Map Word64 TxMetadataValue
fromShelleyMetaData = (\(TxMetadata m) -> m) . TxMetadataShelley

2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import Ouroboros.Consensus.Shelley.Protocol.Crypto (StandardCrypto)

import qualified Cardano.Ledger.Core as Ledger
import qualified Cardano.Ledger.Era as Ledger
import qualified Cardano.Ledger.Shelley as Shelley
import qualified Cardano.Ledger.Shelley.Constraints as Shelley

import qualified Shelley.Spec.Ledger.Address.Bootstrap as Shelley
import Shelley.Spec.Ledger.BaseTypes (maybeToStrictMaybe, strictMaybeToMaybe)
Expand Down
115 changes: 90 additions & 25 deletions cardano-api/src/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ import Data.String (IsString)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Short as SBS
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import qualified Data.Sequence.Strict as Seq
import qualified Data.Set as Set
import Data.Word (Word64)

import Cardano.Binary (Annotated (..), reAnnotate, recoverBytes)
import qualified Cardano.Binary as CBOR
Expand All @@ -104,13 +106,16 @@ import qualified Cardano.Chain.UTxO as Byron

import qualified Cardano.Ledger.Era as Ledger
import qualified Cardano.Ledger.Core as Ledger
import qualified Cardano.Ledger.Shelley as Ledger
import qualified Cardano.Ledger.Shelley.Constraints as Ledger
import qualified Cardano.Ledger.ShelleyMA.TxBody as Allegra
import Ouroboros.Consensus.Shelley.Eras (StandardShelley)
import qualified Cardano.Ledger.ShelleyMA.Metadata as Allegra
import qualified Shelley.Spec.Ledger.MetaData as Ledger (MetaDataHash, hashMetadata)
import Ouroboros.Consensus.Shelley.Eras
(StandardShelley, StandardAllegra, StandardMary)
import Ouroboros.Consensus.Shelley.Protocol.Crypto (StandardCrypto)

import qualified Shelley.Spec.Ledger.Address as Shelley
import Shelley.Spec.Ledger.BaseTypes (StrictMaybe(..))
import Shelley.Spec.Ledger.BaseTypes (StrictMaybe(..), maybeToStrictMaybe)
import qualified Shelley.Spec.Ledger.Credential as Shelley
import qualified Shelley.Spec.Ledger.Genesis as Shelley
import qualified Shelley.Spec.Ledger.Keys as Shelley
Expand Down Expand Up @@ -704,7 +709,14 @@ data TxBody era where
ShelleyTxBody
:: ShelleyBasedEra era
-> Ledger.TxBody (ShelleyLedgerEra era)
-> Maybe Shelley.MetaData

-- The 'Ledger.Metadata' is really the /auxiliary/ data, it consists
-- of one or several things, depending on era:
-- * tranaction metadata (in Shelley and later)
-- * auxiliary scripts (in Allegra and later)
-- * auxiliary script data (in Allonzo and later)
-> Maybe (Ledger.Metadata (ShelleyLedgerEra era))

-> TxBody era
-- The 'ShelleyBasedEra' GADT tells us what era we are in.
-- The 'ShelleyLedgerEra' type family maps that to the era type from the
Expand All @@ -719,7 +731,10 @@ instance Eq (TxBody era) where

(==) (ShelleyTxBody era txbodyA txmetadataA)
(ShelleyTxBody _ txbodyB txmetadataB) =
txmetadataA == txmetadataB
case era of
ShelleyBasedEraShelley -> txmetadataA == txmetadataB
ShelleyBasedEraAllegra -> txmetadataA == txmetadataB
ShelleyBasedEraMary -> txmetadataA == txmetadataB
&& case era of
ShelleyBasedEraShelley -> txbodyA == txbodyB
ShelleyBasedEraAllegra -> txbodyA == txbodyB
Expand Down Expand Up @@ -918,12 +933,17 @@ makeShelleyTransactionBody era@ShelleyBasedEraShelley
(case txUpdateProposal of
TxUpdateProposalNone -> SNothing
TxUpdateProposal _ p -> SJust (toShelleyUpdate p))
(case txMetadata of
TxMetadataNone -> SNothing
TxMetadataInEra _ m -> SJust (toShelleyMetadataHash m)))
(case txMetadata of
TxMetadataNone -> Nothing
TxMetadataInEra _ m -> Just (toShelleyMetadata m))
(maybeToStrictMaybe (hashAuxiliaryData <$> txAuxData)))
txAuxData
where
txAuxData :: Maybe (Ledger.Metadata StandardShelley)
txAuxData
| Map.null ms = Nothing
| otherwise = Just (toShelleyAuxiliaryData ms)
where
ms = case txMetadata of
TxMetadataNone -> Map.empty
TxMetadataInEra _ (TxMetadata ms') -> ms'

makeShelleyTransactionBody era@ShelleyBasedEraAllegra
TxBodyContent {
Expand All @@ -932,7 +952,7 @@ makeShelleyTransactionBody era@ShelleyBasedEraAllegra
txFee,
txValidityRange = (lowerBound, upperBound),
txMetadata,
txAuxScripts = _unused, --TODO: now supported in ledger
txAuxScripts,
txWithdrawals,
txCertificates,
txUpdateProposal
Expand Down Expand Up @@ -963,13 +983,22 @@ makeShelleyTransactionBody era@ShelleyBasedEraAllegra
(case txUpdateProposal of
TxUpdateProposalNone -> SNothing
TxUpdateProposal _ p -> SJust (toShelleyUpdate p))
(case txMetadata of
TxMetadataNone -> SNothing
TxMetadataInEra _ m -> SJust (toShelleyMetadataHash m))
(maybeToStrictMaybe (hashAuxiliaryData <$> txAuxData))
mempty) -- No minting in Allegra, only Mary
(case txMetadata of
TxMetadataNone -> Nothing
TxMetadataInEra _ m -> Just (toShelleyMetadata m))
txAuxData
where
txAuxData :: Maybe (Ledger.Metadata StandardAllegra)
txAuxData
| Map.null ms
, null ss = Nothing
| otherwise = Just (toAllegraAuxiliaryData ms ss)
where
ms = case txMetadata of
TxMetadataNone -> Map.empty
TxMetadataInEra _ (TxMetadata ms') -> ms'
ss = case txAuxScripts of
TxAuxScriptsNone -> []
TxAuxScripts _ ss' -> ss'

makeShelleyTransactionBody era@ShelleyBasedEraMary
TxBodyContent {
Expand All @@ -978,7 +1007,7 @@ makeShelleyTransactionBody era@ShelleyBasedEraMary
txFee,
txValidityRange = (lowerBound, upperBound),
txMetadata,
txAuxScripts = _unused, --TODO: now supported in ledger
txAuxScripts,
txWithdrawals,
txCertificates,
txUpdateProposal,
Expand Down Expand Up @@ -1010,15 +1039,24 @@ makeShelleyTransactionBody era@ShelleyBasedEraMary
(case txUpdateProposal of
TxUpdateProposalNone -> SNothing
TxUpdateProposal _ p -> SJust (toShelleyUpdate p))
(case txMetadata of
TxMetadataNone -> SNothing
TxMetadataInEra _ m -> SJust (toShelleyMetadataHash m))
(maybeToStrictMaybe (hashAuxiliaryData <$> txAuxData))
(case txMintValue of
TxMintNone -> mempty
TxMintValue _ v -> toMaryValue v))
(case txMetadata of
TxMetadataNone -> Nothing
TxMetadataInEra _ m -> Just (toShelleyMetadata m))
txAuxData
where
txAuxData :: Maybe (Ledger.Metadata StandardMary)
txAuxData
| Map.null ms
, null ss = Nothing
| otherwise = Just (toAllegraAuxiliaryData ms ss)
where
ms = case txMetadata of
TxMetadataNone -> Map.empty
TxMetadataInEra _ (TxMetadata ms') -> ms'
ss = case txAuxScripts of
TxAuxScriptsNone -> []
TxAuxScripts _ ss' -> ss'


toShelleyWithdrawal :: [(StakeAddress, Lovelace)] -> Shelley.Wdrl ledgerera
Expand All @@ -1028,6 +1066,33 @@ toShelleyWithdrawal withdrawals =
[ (toShelleyStakeAddr stakeAddr, toShelleyLovelace value)
| (stakeAddr, value) <- withdrawals ]

-- | In the Shelley era the auxiliary data consists only of the tx metadata
toShelleyAuxiliaryData :: Map Word64 TxMetadataValue
-> Ledger.Metadata StandardShelley
toShelleyAuxiliaryData m =
Shelley.MetaData
(toShelleyMetaData m)

-- | In the Allegra and Mary eras the auxiliary data consists of the tx metadata
-- and the axiliary scripts.
--
toAllegraAuxiliaryData :: forall era ledgeera.
ShelleyLedgerEra era ~ ledgeera
=> Ledger.Metadata ledgeera ~ Allegra.Metadata ledgeera
=> Ledger.AnnotatedData (Ledger.Script ledgeera)
=> Ord (Ledger.Script ledgeera)
=> Map Word64 TxMetadataValue
-> [ScriptInEra era]
-> Ledger.Metadata ledgeera
toAllegraAuxiliaryData m ss =
Allegra.Metadata
(toShelleyMetaData m)
(Seq.fromList (map toShelleyScript ss))

hashAuxiliaryData :: Shelley.ValidateMetadata ledgeera
=> Ledger.Metadata ledgeera -> Ledger.MetaDataHash ledgeera
hashAuxiliaryData = Ledger.hashMetadata


-- ----------------------------------------------------------------------------
-- Transitional utility functions for making transaction bodies
Expand Down
Loading

0 comments on commit a6829f3

Please sign in to comment.