Skip to content

Commit

Permalink
Change TxBody.mint field to MultiAsset in shelley-ma
Browse files Browse the repository at this point in the history
  • Loading branch information
teodanciu committed Aug 8, 2022
1 parent 233665c commit a6dbc50
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions eras/shelley-ma/impl/src/Cardano/Ledger/Mary/Value.hs
Expand Up @@ -759,8 +759,8 @@ readShortByteString sbs start len =
--
-- This function is equivalent to computing the support of the value in the
-- spec.
policies :: MaryValue crypto -> Set (PolicyID crypto)
policies (MaryValue _ (MultiAsset m)) = Map.keysSet m
policies :: MultiAsset crypto -> Set (PolicyID crypto)
policies (MultiAsset m) = Map.keysSet m

lookup :: PolicyID crypto -> AssetName -> MaryValue crypto -> Integer
lookup pid aid (MaryValue _ (MultiAsset m)) =
Expand Down
9 changes: 6 additions & 3 deletions eras/shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/Era.hs
Expand Up @@ -19,7 +19,7 @@ import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Compactible (CompactForm, Compactible)
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Mary.Value (MaryValue, policies, policyID)
import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..), policies, policyID)
import qualified Cardano.Ledger.Shelley.API as Shelley
import Cardano.Ledger.Shelley.PParams (ShelleyPParams, ShelleyPParamsUpdate, updatePParams)
import qualified Cardano.Ledger.Shelley.Rules.Bbody as Shelley
Expand All @@ -30,7 +30,7 @@ import qualified Cardano.Ledger.Shelley.Rules.Rupd as Shelley
import qualified Cardano.Ledger.Shelley.Rules.Snap as Shelley
import qualified Cardano.Ledger.Shelley.Rules.Tick as Shelley
import qualified Cardano.Ledger.Shelley.Rules.Upec as Shelley
import Cardano.Ledger.Val (DecodeMint, DecodeNonNegative, EncodeMint, Val)
import Cardano.Ledger.Val (DecodeMint, DecodeNonNegative, EncodeMint, Val (zero))
import Control.DeepSeq (NFData (..))
import Data.Kind (Type)
import Data.Set as Set (Set, empty, map)
Expand Down Expand Up @@ -70,15 +70,18 @@ class
MAClass (ma :: MaryOrAllegra) crypto
where
type MAValue (ma :: MaryOrAllegra) crypto :: Type
getScriptHash :: proxy ma -> MAValue ma crypto -> Set.Set (ScriptHash crypto)
getScriptHash :: proxy ma -> MultiAsset crypto -> Set.Set (ScriptHash crypto)
promoteMultiAsset :: proxy ma -> MultiAsset crypto -> Value (ShelleyMAEra ma crypto)

instance CC.Crypto c => MAClass 'Mary c where
type MAValue 'Mary c = MaryValue c
getScriptHash _ x = Set.map policyID (policies x)
promoteMultiAsset _ ma = MaryValue 0 ma

instance CC.Crypto c => MAClass 'Allegra c where
type MAValue 'Allegra c = Coin
getScriptHash _ _ = Set.empty
promoteMultiAsset _ _ = zero

-- | The actual Mary and Allegra instances, rolled into one, the MAClass superclass
-- provides the era-specific code for where they differ.
Expand Down
Expand Up @@ -202,7 +202,7 @@ consumed ::
UTxO era ->
TxBody era ->
Value era
consumed pp u txBody = Shelley.consumed pp u txBody <> txBody ^. mintTxBodyL
consumed pp u txBody = Shelley.consumed pp u txBody <> txBody ^. mintValueTxBodyF

-- | The UTxO transition rule for the Shelley-MA (Mary and Allegra) eras.
utxoTransition ::
Expand Down Expand Up @@ -298,7 +298,7 @@ validateTriesToForgeADA ::
TxBody era ->
Test (UtxoPredicateFailure era)
validateTriesToForgeADA txb =
failureUnless (Val.coin (txb ^. mintTxBodyL) == Val.zero) TriesToForgeADA
failureUnless (Val.coin (txb ^. mintValueTxBodyF) == Val.zero) TriesToForgeADA

-- | Ensure that there are no `TxOut`s that have `Value` of size larger than @MaxValSize@
--
Expand Down
23 changes: 14 additions & 9 deletions eras/shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/TxBody.hs
Expand Up @@ -50,6 +50,7 @@ import Cardano.Ledger.BaseTypes (StrictMaybe (SJust, SNothing))
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core hiding (TxBody)
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Mary.Value (MultiAsset (..))
import Cardano.Ledger.SafeHash (HashAnnotated, SafeToHash)
import Cardano.Ledger.Serialization (encodeFoldable)
import Cardano.Ledger.Shelley.PParams (Update)
Expand All @@ -61,13 +62,12 @@ import Cardano.Ledger.Shelley.TxBody
addrEitherShelleyTxOutL,
valueEitherShelleyTxOutL,
)
import Cardano.Ledger.ShelleyMA.Era (MAClass (getScriptHash), ShelleyMAEra)
import Cardano.Ledger.ShelleyMA.Era (MAClass (..), ShelleyMAEra)
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval (..))
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Ledger.Val
( DecodeMint (..),
EncodeMint (..),
Val (..),
)
import Control.DeepSeq (NFData (..))
import Data.Coders
Expand Down Expand Up @@ -106,7 +106,7 @@ data TxBodyRaw era = TxBodyRaw
vldt :: !ValidityInterval, -- imported from Timelocks
update :: !(StrictMaybe (Update era)),
adHash :: !(StrictMaybe (AuxiliaryDataHash (Crypto era))),
mint :: !(Value era)
mint :: !(MultiAsset (Crypto era))
}

deriving instance
Expand Down Expand Up @@ -154,7 +154,7 @@ txSparse (TxBodyRaw inp out cert wdrl fee (ValidityInterval bot top) up hash frg
!> encodeKeyedStrictMaybe 6 up
!> encodeKeyedStrictMaybe 7 hash
!> encodeKeyedStrictMaybe 8 bot
!> Omit isZero (Key 9 (E encodeMint frge))
!> Omit (== mempty) (Key 9 (E encodeMint frge))

bodyFields :: ShelleyMAEraTxBody era => Word -> Field (TxBodyRaw era)
bodyFields 0 = field (\x tx -> tx {inputs = x}) (D (decodeSet fromCBOR))
Expand All @@ -169,7 +169,7 @@ bodyFields 8 = ofield (\x tx -> tx {vldt = (vldt tx) {invalidBefore = x}}) From
bodyFields 9 = field (\x tx -> tx {mint = x}) (D decodeMint)
bodyFields n = invalidField n

initial :: (Val (Value era)) => TxBodyRaw era
initial :: TxBodyRaw era
initial =
TxBodyRaw
empty
Expand All @@ -180,7 +180,7 @@ initial =
(ValidityInterval SNothing SNothing)
SNothing
SNothing
zero
mempty

-- ===========================================================================
-- Wrap it all up in a newtype, hiding the insides with a pattern construtor.
Expand Down Expand Up @@ -228,7 +228,7 @@ pattern MATxBody ::
ValidityInterval ->
StrictMaybe (Update era) ->
StrictMaybe (AuxiliaryDataHash (Crypto era)) ->
Value era ->
MultiAsset (Crypto era) ->
MATxBody era
pattern MATxBody inputs outputs certs wdrls txfee vldt update adHash mint <-
TxBodyConstr
Expand Down Expand Up @@ -260,7 +260,7 @@ pattern TxBody' ::
ValidityInterval ->
StrictMaybe (Update era) ->
StrictMaybe (AuxiliaryDataHash (Crypto era)) ->
Value era ->
MultiAsset (Crypto era) ->
MATxBody era
pattern TxBody' {inputs', outputs', certs', wdrls', txfee', vldt', update', adHash', mint'} <-
TxBodyConstr
Expand Down Expand Up @@ -331,7 +331,9 @@ class
where
vldtTxBodyL :: Lens' (Core.TxBody era) ValidityInterval

mintTxBodyL :: Lens' (Core.TxBody era) (Value era)
mintTxBodyL :: Lens' (Core.TxBody era) (MultiAsset (Crypto era))

mintValueTxBodyF :: SimpleGetter (Core.TxBody era) (Core.Value era)

instance MAClass ma crypto => ShelleyMAEraTxBody (ShelleyMAEra ma crypto) where
vldtTxBodyL =
Expand All @@ -340,6 +342,9 @@ instance MAClass ma crypto => ShelleyMAEraTxBody (ShelleyMAEra ma crypto) where
mintTxBodyL =
lensTxBodyRaw mint (\txBodyRaw mint_ -> txBodyRaw {mint = mint_})

mintValueTxBodyF =
to (\(TxBodyConstr (Memo txBodyRaw _)) -> promoteMultiAsset (Proxy @ma) (mint txBodyRaw))

instance MAClass ma crypto => EraTxOut (ShelleyMAEra ma crypto) where
type TxOut (ShelleyMAEra ma crypto) = ShelleyTxOut (ShelleyMAEra ma crypto)

Expand Down

0 comments on commit a6dbc50

Please sign in to comment.