Skip to content

Commit

Permalink
Merge pull request #3172 from input-output-hk/lehins/remove-encode-de…
Browse files Browse the repository at this point in the history
…code-mint

Remove `EncodeMint`/`DecodeMint` classes
  • Loading branch information
lehins committed Nov 29, 2022
2 parents 3c602e1 + 19348fa commit 94d1a85
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -259,6 +259,7 @@ in the naming of release branches.
- Removed unused `Data.BiMap` module from `cardano-data` #3089
- Removed `getMultiSigBytes` as unused #3138
- Removed `hashCostModel` as unused and invalid #3138
- Removed `EncodeMint`/`DecodeMint` classes in favor of regular `ToCBOR`/`FromCBOR` #3172

### Fixed

Expand Down
6 changes: 3 additions & 3 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxBody.hs
Expand Up @@ -108,7 +108,7 @@ import Cardano.Ledger.Shelley.TxBody (ShelleyEraTxBody (..), Wdrl (Wdrl), unWdrl
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval (..))
import Cardano.Ledger.ShelleyMA.TxBody (ShelleyMAEraTxBody (..))
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Ledger.Val (Val (..), decodeMint, encodeMint)
import Cardano.Ledger.Val (Val (..))
import Control.DeepSeq (NFData (..))
import Data.Sequence.Strict (StrictSeq)
import qualified Data.Sequence.Strict as StrictSeq
Expand Down Expand Up @@ -465,7 +465,7 @@ encodeTxBodyRaw
!> encodeKeyedStrictMaybe 6 atbrUpdate
!> encodeKeyedStrictMaybe 8 bot
!> Omit null (Key 14 (To atbrReqSignerHashes))
!> Omit (== mempty) (Key 9 (E encodeMint atbrMint))
!> Omit (== mempty) (Key 9 (To atbrMint))
!> encodeKeyedStrictMaybe 11 atbrScriptIntegrityHash
!> encodeKeyedStrictMaybe 7 atbrAuxDataHash
!> encodeKeyedStrictMaybe 15 atbrTxNetworkId
Expand Down Expand Up @@ -503,7 +503,7 @@ instance
ofield
(\x tx -> tx {atbrValidityInterval = (atbrValidityInterval tx) {invalidBefore = x}})
From
bodyFields 9 = field (\x tx -> tx {atbrMint = x}) (D decodeMint)
bodyFields 9 = field (\x tx -> tx {atbrMint = x}) From
bodyFields 11 = ofield (\x tx -> tx {atbrScriptIntegrityHash = x}) From
bodyFields 14 = field (\x tx -> tx {atbrReqSignerHashes = x}) From
bodyFields 15 = ofield (\x tx -> tx {atbrTxNetworkId = x}) From
Expand Down
Expand Up @@ -56,6 +56,7 @@ import Cardano.Ledger.Alonzo.Tx
import Cardano.Ledger.Alonzo.TxBody (AlonzoTxOut (..), ScriptIntegrityHash)
import Cardano.Ledger.Alonzo.TxWits
import Cardano.Ledger.BaseTypes (Network, StrictMaybe (..), Version)
import Cardano.Ledger.Binary (toCBOR)
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto
Expand All @@ -65,7 +66,7 @@ import Cardano.Ledger.Shelley.PParams (Update)
import Cardano.Ledger.Shelley.TxBody (DCert, Wdrl)
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval (..))
import Cardano.Ledger.TxIn (TxIn)
import Cardano.Ledger.Val (DecodeNonNegative, EncodeMint (..), Val)
import Cardano.Ledger.Val (DecodeNonNegative, Val)
import Cardano.Slotting.Slot (SlotNo)
import Codec.CBOR.Term (Term (..))
import Control.State.Transition (PredicateFailure)
Expand Down Expand Up @@ -443,7 +444,7 @@ instance Crypto c => Twiddle (Update (AlonzoEra c)) where
twiddle v = twiddle v . toTerm v

instance Crypto c => Twiddle (MultiAsset c) where
twiddle v = twiddle v . encodingToTerm v . encodeMint
twiddle v = twiddle v . encodingToTerm v . toCBOR

instance Crypto c => Twiddle (ScriptIntegrityHash c) where
twiddle v = twiddle v . toTerm v
Expand Down
11 changes: 3 additions & 8 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/TxBody.hs
Expand Up @@ -163,12 +163,7 @@ import Cardano.Ledger.Shelley.PParams (Update)
import Cardano.Ledger.Shelley.TxBody (Wdrl (Wdrl), unWdrl)
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval (..))
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Ledger.Val
( DecodeNonNegative,
Val (..),
decodeMint,
encodeMint,
)
import Cardano.Ledger.Val (DecodeNonNegative, Val (..))
import Control.DeepSeq (NFData)
import Data.Sequence.Strict (StrictSeq, (|>))
import qualified Data.Sequence.Strict as StrictSeq
Expand Down Expand Up @@ -727,7 +722,7 @@ encodeTxBodyRaw
!> encodeKeyedStrictMaybe 6 btbrUpdate
!> encodeKeyedStrictMaybe 8 bot
!> Omit null (Key 14 (To btbrReqSignerHashes))
!> Omit (== mempty) (Key 9 (E encodeMint btbrMint))
!> Omit (== mempty) (Key 9 (To btbrMint))
!> encodeKeyedStrictMaybe 11 btbrScriptIntegrityHash
!> encodeKeyedStrictMaybe 7 btbrAuxDataHash
!> encodeKeyedStrictMaybe 15 btbrTxNetworkId
Expand Down Expand Up @@ -784,7 +779,7 @@ instance
ofield
(\x tx -> tx {btbrValidityInterval = (btbrValidityInterval tx) {invalidBefore = x}})
From
bodyFields 9 = field (\x tx -> tx {btbrMint = x}) (D decodeMint)
bodyFields 9 = field (\x tx -> tx {btbrMint = x}) From
bodyFields 11 = ofield (\x tx -> tx {btbrScriptIntegrityHash = x}) From
bodyFields 14 = field (\x tx -> tx {btbrReqSignerHashes = x}) From
bodyFields 15 = ofield (\x tx -> tx {btbrTxNetworkId = x}) From
Expand Down
29 changes: 6 additions & 23 deletions eras/shelley-ma/impl/src/Cardano/Ledger/Mary/Value.hs
Expand Up @@ -57,12 +57,7 @@ import Cardano.Ledger.Coin (Coin (..), CompactForm (..), integerToWord64)
import Cardano.Ledger.Compactible (Compactible (..))
import qualified Cardano.Ledger.Crypto as CC
import Cardano.Ledger.Shelley.Scripts (ScriptHash (..))
import Cardano.Ledger.Val
( DecodeMint (..),
DecodeNonNegative (..),
EncodeMint (..),
Val (..),
)
import Cardano.Ledger.Val (DecodeNonNegative (..), Val (..))
import Control.DeepSeq (NFData (..), deepseq, rwhnf)
import Control.Monad (forM_)
import Control.Monad.ST (runST)
Expand Down Expand Up @@ -152,11 +147,11 @@ instance Group (MultiAsset c) where
invert (MultiAsset m) =
MultiAsset (canonicalMap (canonicalMap ((-1 :: Integer) *)) m)

instance CC.Crypto c => DecodeMint (MultiAsset c) where
decodeMint = decodeMultiAssetMaps decodeIntegerBounded64
instance CC.Crypto c => FromCBOR (MultiAsset c) where
fromCBOR = decodeMultiAssetMaps decodeIntegerBounded64

instance CC.Crypto c => EncodeMint (MultiAsset c) where
encodeMint = encodeMultiAssetMaps
instance CC.Crypto c => ToCBOR (MultiAsset c) where
toCBOR = encodeMultiAssetMaps

-- | The Value representing MultiAssets
data MaryValue c = MaryValue !Integer !(MultiAsset c)
Expand Down Expand Up @@ -334,7 +329,7 @@ instance
encode $
Rec MaryValue
!> To c
!> E encodeMultiAssetMaps ma
!> To ma

instance
CC.Crypto c =>
Expand All @@ -348,12 +343,6 @@ instance
where
decodeNonNegative = decodeNonNegativeValue

instance
CC.Crypto c =>
DecodeMint (MaryValue c)
where
decodeMint = MaryValue 0 <$> decodeMint

-- Note: we do not use `decodeInt64` from the cborg library here because the
-- implementation contains "-- TODO FIXME: overflow"
decodeIntegerBounded64 :: Decoder s Integer
Expand Down Expand Up @@ -382,12 +371,6 @@ decodeIntegerBounded64 = do
maxval = fromIntegral (maxBound :: Int64)
minval = fromIntegral (minBound :: Int64)

instance
CC.Crypto c =>
EncodeMint (MaryValue c)
where
encodeMint (MaryValue _ multiasset) = encodeMint multiasset

-- ========================================================================
-- Compactible
-- This is used in the TxOut which stores the (CompactForm MaryValue).
Expand Down
6 changes: 1 addition & 5 deletions eras/shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/Core.hs
Expand Up @@ -9,14 +9,10 @@ where
import Cardano.Ledger.Mary.Value (MultiAsset (..))
import Cardano.Ledger.Shelley.Core
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval (..))
import Cardano.Ledger.Val (DecodeMint, EncodeMint)
import Data.Set (Set)
import Lens.Micro (Lens', SimpleGetter)

class
(ShelleyEraTxBody era, EncodeMint (Value era), DecodeMint (Value era)) =>
ShelleyMAEraTxBody era
where
class ShelleyEraTxBody era => ShelleyMAEraTxBody era where
vldtTxBodyL :: Lens' (TxBody era) ValidityInterval

mintTxBodyL :: Lens' (TxBody era) (MultiAsset (EraCrypto era))
Expand Down
4 changes: 1 addition & 3 deletions eras/shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/Era.hs
Expand Up @@ -35,7 +35,7 @@ import Cardano.Ledger.Shelley.Rules
ShelleyTICKF,
ShelleyUPEC,
)
import Cardano.Ledger.Val (DecodeMint, DecodeNonNegative, EncodeMint, Val, zero)
import Cardano.Ledger.Val (DecodeNonNegative, Val, zero)
import Control.DeepSeq (NFData (..))
import Data.Kind (Type)
import Data.Set as Set (Set, empty, map)
Expand Down Expand Up @@ -72,8 +72,6 @@ class
Eq (MAValue ma c),
FromCBOR (MAValue ma c),
ToCBOR (MAValue ma c),
EncodeMint (MAValue ma c),
DecodeMint (MAValue ma c),
NoThunks (MAValue ma c),
KnownNat (MAProtVer ma),
MinVersion <= MAProtVer ma,
Expand Down
8 changes: 2 additions & 6 deletions eras/shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/TxBody.hs
Expand Up @@ -84,10 +84,6 @@ import Cardano.Ledger.ShelleyMA.Era
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval (..))
import Cardano.Ledger.ShelleyMA.TxOut
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Ledger.Val
( DecodeMint (..),
EncodeMint (..),
)
import Control.DeepSeq (NFData (..))
import qualified Data.Map.Strict as Map
import Data.Proxy
Expand Down Expand Up @@ -166,7 +162,7 @@ txSparse (MATxBodyRaw inp out cert wdrl fee (ValidityInterval bot top) up hash f
!> encodeKeyedStrictMaybe 6 up
!> encodeKeyedStrictMaybe 7 hash
!> encodeKeyedStrictMaybe 8 bot
!> Omit (== mempty) (Key 9 (E encodeMint frge))
!> Omit (== mempty) (Key 9 (To frge))

bodyFields :: EraTxOut era => Word -> Field (MATxBodyRaw era)
bodyFields 0 = field (\x tx -> tx {matbrInputs = x}) From
Expand Down Expand Up @@ -194,7 +190,7 @@ bodyFields 8 =
}
)
From
bodyFields 9 = field (\x tx -> tx {matbrMint = x}) (D decodeMint)
bodyFields 9 = field (\x tx -> tx {matbrMint = x}) From
bodyFields n = invalidField n

initial :: MATxBodyRaw era
Expand Down
22 changes: 1 addition & 21 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Val.hs
Expand Up @@ -14,12 +14,10 @@ module Cardano.Ledger.Val
sumVal,
adaOnly,
DecodeNonNegative (..),
DecodeMint (..),
EncodeMint (..),
)
where

import Cardano.Ledger.Binary (Decoder, Encoding, decodeWord64, toCBOR)
import Cardano.Ledger.Binary (Decoder, decodeWord64)
import Cardano.Ledger.Coin (Coin (..), CompactForm (..), DeltaCoin (..))
import Cardano.Ledger.Compactible (Compactible (..))
import Data.Coerce
Expand Down Expand Up @@ -144,21 +142,3 @@ instance (DecodeNonNegative a, Compactible a, Show a) => DecodeNonNegative (Comp
decodeNonNegative = do
v <- decodeNonNegative
maybe (fail $ "illegal value: " <> show v) pure (toCompact v)

-- =============================================================

class DecodeMint v where
decodeMint :: Decoder s v

instance DecodeMint Coin where
decodeMint = fail "cannot have coin in mint field"

-- =============================================================

class EncodeMint v where
encodeMint :: v -> Encoding

instance EncodeMint Coin where
-- we expect nothing to be able to successfully decode this
-- this is an alternative to throwing an error at encoding
encodeMint = toCBOR

0 comments on commit 94d1a85

Please sign in to comment.