Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eras/alonzo/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.16.0.0

* Add `ToJSON` and `FromJSON` instances for `AlonzoTxAuxData era`
* Add `FromJSON` instance for `AlonzoTxOut era`
* Add `FromJSON` instance for `AsIx ix it`
* Add `FromJSON` instance for `AlonzoPlutusPurpose AsIx era`
* Add `ToJSON` and `FromJSON` instances for `TxDats era`, `Redeemers era`, and `AlonzoTxWits era`
Expand Down
11 changes: 9 additions & 2 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxOut.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ import qualified Cardano.Ledger.Shelley.TxOut as Shelley
import Cardano.Ledger.Val (Val (..))
import Control.DeepSeq (NFData (..), rwhnf)
import Control.Monad (guard)
import Data.Aeson (ToJSON (..), object, (.=))
import qualified Data.Aeson as Aeson (Value (Null, String))
import Data.Aeson (FromJSON (..), ToJSON (..), object, (.:), (.=))
import qualified Data.Aeson as Aeson
import Data.Bits
import Data.Maybe (fromMaybe)
import Data.MemPack
Expand Down Expand Up @@ -445,6 +445,13 @@ instance (Era era, Val (Value era)) => ToJSON (AlonzoTxOut era) where
extractHash dHash
]

instance (Era era, Val (Value era)) => FromJSON (AlonzoTxOut era) where
parseJSON = Aeson.withObject "AlonzoTxOut" $ \o ->
AlonzoTxOut
<$> o .: "address"
<*> o .: "value"
<*> o .: "datahash"

pattern TxOutCompact ::
(Era era, Val (Value era), HasCallStack) =>
CompactAddr ->
Expand Down
1 change: 1 addition & 0 deletions eras/babbage/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.14.0.0

* Add `FromJSON` instance for `BabbageTxOut era`
* Replace arguments of `babbageEvalScriptsTxInvalid` with `StAnnTx`
* Replace arguments of `expectScriptsToPass` with `StAnnTx`
* Change `Signal` to `StAnnTx TopTx era` for: `BabbageLEDGER`, `BabbageUTXOW`, `BabbageUTXO`, `BabbageUTXOS`
Expand Down
11 changes: 10 additions & 1 deletion eras/babbage/impl/src/Cardano/Ledger/Babbage/TxOut.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ import Cardano.Ledger.Plutus.Data (
)
import Cardano.Ledger.Val (Val (..))
import Control.DeepSeq (NFData (rnf), rwhnf)
import Data.Aeson (ToJSON (..), (.=))
import Data.Aeson (FromJSON (..), ToJSON (..), (.:), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import Data.Maybe (fromMaybe)
import Data.MemPack
Expand Down Expand Up @@ -335,6 +336,14 @@ instance (Era era, Val (Value era), ToJSON (Script era)) => ToKeyValuePairs (Bab
, "referenceScript" .= mRefScript
]

instance (Era era, Val (Value era), FromJSON (Script era)) => FromJSON (BabbageTxOut era) where
parseJSON = Aeson.withObject "BabbageTxOut" $ \o ->
BabbageTxOut
<$> o .: "address"
<*> o .: "value"
<*> o .: "datum"
<*> o .: "referenceScript"

viewCompactTxOut ::
forall era.
Val (Value era) =>
Expand Down
2 changes: 2 additions & 0 deletions eras/mary/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.11.0.0

* Add `FromJSON` instance for `MaryValue`
* Add `FromJSON` and `FromJSONKey` instances for `AssetName` (hex-decoding from `ToJSON` format)
* Add `ApplyTick` instance for `MaryEra`
* Add `EraForecast` and `ShelleyEraForecast` instances for `MaryEra`.

Expand Down
28 changes: 25 additions & 3 deletions eras/mary/impl/src/Cardano/Ledger/Mary/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import Control.DeepSeq (NFData (..), deepseq, rwhnf)
import Control.Exception (assert)
import Control.Monad (forM_, guard, unless, when)
import Control.Monad.ST (runST)
import Data.Aeson (FromJSON, FromJSONKey, ToJSON (..), (.=))
import Data.Aeson (FromJSON, FromJSONKey (..), ToJSON (..), (.:), (.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (ToJSONKey (..), toJSONKeyText)
import qualified Data.ByteString as BS
Expand Down Expand Up @@ -98,7 +98,7 @@ import qualified Data.Semigroup as Semigroup (Sum (..))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import Data.Text.Encoding (decodeLatin1)
import Data.Text.Encoding (decodeLatin1, encodeUtf8)
import Data.Word (Word16, Word32, Word64)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..), OnlyCheckWhnfNamed (..))
Expand All @@ -123,6 +123,10 @@ assetNameToBytesAsHex = BS16.encode . SBS.fromShort . assetNameBytes
assetNameToTextAsHex :: AssetName -> Text
assetNameToTextAsHex = decodeLatin1 . assetNameToBytesAsHex

assetNameFromText :: Text -> Either String AssetName
assetNameFromText t =
AssetName . SBS.toShort <$> BS16.decode (encodeUtf8 t)

instance DecCBOR AssetName where
decCBOR = do
an <- decCBOR
Expand Down Expand Up @@ -153,7 +157,7 @@ newtype PolicyID = PolicyID {policyID :: ScriptHash}

-- | The MultiAssets map
newtype MultiAsset = MultiAsset (Map PolicyID (Map AssetName Integer))
deriving (Show, Generic, ToJSON, EncCBOR)
deriving (Show, Generic, FromJSON, ToJSON, EncCBOR)

instance Eq MultiAsset where
MultiAsset x == MultiAsset y = pointWise (pointWise (==)) x y
Expand Down Expand Up @@ -383,6 +387,12 @@ decodeIntegerBounded64 = do
-- ========================================================================
-- JSON

instance FromJSON MaryValue where
parseJSON = Aeson.withObject "MaryValue" $ \o ->
MaryValue
<$> o .: "lovelace"
<*> o .: "policies"

instance ToKeyValuePairs MaryValue where
toKeyValuePairs (MaryValue l ps) =
[ "lovelace" .= l
Expand All @@ -395,6 +405,18 @@ instance ToJSON AssetName where
instance ToJSONKey AssetName where
toJSONKey = toJSONKeyText assetNameToTextAsHex

instance FromJSON AssetName where
parseJSON = Aeson.withText "AssetName" $ \t ->
case assetNameFromText t of
Left e -> fail $ "AssetName: invalid hex: " <> e
Right bs -> pure bs

instance FromJSONKey AssetName where
fromJSONKey = Aeson.FromJSONKeyTextParser $ \t ->
case assetNameFromText t of
Left e -> fail $ "AssetName: invalid hex: " <> e
Right bs -> pure bs

-- ========================================================================
-- Compactible
-- This is used in the TxOut which stores the (CompactForm MaryValue).
Expand Down
1 change: 1 addition & 0 deletions eras/shelley/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.19.0.0

* Add `ToJSON` and `FromJSON` instances for `ShelleyTxAuxData era`
* Add `FromJSON` instance for `ShelleyTxOut era`
* Add `ToJSON` and `FromJSON` instances for `ShelleyTxWits era`
* Change `NoThunks` instance for `BlockTransitionError` to not check for thunks in its contents
* Add `NFData` constraint to `BlockTransitionError` constructor.
Expand Down
9 changes: 8 additions & 1 deletion eras/shelley/impl/src/Cardano/Ledger/Shelley/TxOut.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ import Cardano.Ledger.Shelley.Era (ShelleyEra)
import Cardano.Ledger.Shelley.PParams ()
import Cardano.Ledger.Val (Val)
import Control.DeepSeq (NFData (rnf))
import Data.Aeson (ToJSON (..), (.=))
import Data.Aeson (FromJSON (..), ToJSON (..), (.:), (.=))
import qualified Data.Aeson as Aeson
import Data.Maybe (fromMaybe)
import Data.MemPack
import GHC.Generics (Generic)
Expand Down Expand Up @@ -183,3 +184,9 @@ instance (Era era, Val (Value era)) => ToKeyValuePairs (ShelleyTxOut era) where
[ "address" .= addr
, "amount" .= amount
]

instance (Era era, Val (Value era)) => FromJSON (ShelleyTxOut era) where
parseJSON = Aeson.withObject "ShelleyTxOut" $ \o ->
ShelleyTxOut
<$> o .: "address"
<*> o .: "amount"
4 changes: 4 additions & 0 deletions libs/cardano-ledger-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
* Rename `kindObject` (which returned `Value`) to `kindObjectValue`
* Add `kindObject :: Text -> [Pair] -> Object` returning an `Aeson.Object`
* Add `NFData (Script era)`, `ToJSON (Script era)`, `FromJSON (Script era)`, `ToJSON (NativeScript era)`, and `FromJSON (NativeScript era)` as superclass constraints to `EraScript`
* Add `FromJSON (TxOut era)` as `EraTxOut` superclass constraint
* Add `FromJSON t` as `Val t` superclass constraint
* Add `ToJSON` and `FromJSON` instances for `Datum era`
* Add `ToJSON`, `FromJSON` and `NFData` as `EraTxWits` superclass constraints
* Add `ToJSONKey` and `FromJSONKey` instances to `AccountId`
* Add `ToJSON` and `FromJSON` instances for `Inclusive a` and `Exclusive a`
Expand Down Expand Up @@ -64,6 +67,7 @@
* Add round-trip JSON property test for `NativeScript era` and `Script era` to the shared era spec
* Add round-trip JSON property test for `TxAuxData era` to the shared era spec
* Add round-trip JSON property test for `TxWits era` to the shared era spec
* Add round-trip JSON property test for `TxOut era` to the shared era spec

## 1.20.0.0

Expand Down
1 change: 1 addition & 0 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ class
-- | Abstract interface into specific fields of a `TxOut`
class
( Val (Value era)
, FromJSON (TxOut era)
, ToJSON (TxOut era)
, DecCBOR (Value era)
, DecCBOR (CompactForm (Value era))
Expand Down
19 changes: 10 additions & 9 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import Cardano.Ledger.MemoBytes (
)
import Cardano.Ledger.MemoBytes.Internal (mkMemoBytesShort)
import qualified Codec.Serialise as Cborg (Serialise (..))
import Control.Applicative (asum)
import Control.Applicative (asum, (<|>))
import Control.DeepSeq (NFData)
import Control.Monad ((<$!>))
import Data.Aeson (FromJSON (..), ToJSON (..), Value (Null), object, withObject, (.:), (.=))
Expand Down Expand Up @@ -306,14 +306,15 @@ instance Era era => DecCBOR (Datum era) where
decodeDatum k = Invalid k

instance Era era => ToJSON (Datum era) where
toJSON d =
case datumDataHash d of
SNothing -> Null
SJust dh -> toJSON dh
toEncoding d =
case datumDataHash d of
SNothing -> toEncoding Null
SJust dh -> toEncoding dh
toJSON NoDatum = Null
toJSON (DatumHash dh) = object ["datumhash" .= dh]
toJSON (Datum bd) = object ["datum" .= binaryDataToData @era bd]

instance Era era => FromJSON (Datum era) where
parseJSON Null = pure NoDatum
parseJSON v =
withObject "DatumHash" (\o -> DatumHash <$> o .: "datumhash") v
<|> withObject "Datum" (\o -> Datum . dataToBinaryData <$> o .: "datum") v

mkInlineDatum :: forall era. Era era => PV1.Data -> Datum era
mkInlineDatum = Datum . dataToBinaryData . Data @era
Expand Down
4 changes: 2 additions & 2 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Val.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}

-- | This module defines a generalised notion of a "value" - that is, something
Expand All @@ -21,7 +20,7 @@ import Cardano.Ledger.Binary (DecCBOR, EncCBOR)
import Cardano.Ledger.Coin (Coin (..), CompactForm (..), DeltaCoin (..))
import Cardano.Ledger.Compactible (Compactible (..))
import Control.DeepSeq (NFData)
import Data.Aeson (ToJSON)
import Data.Aeson (FromJSON, ToJSON)
import Data.Coerce
import Data.Foldable as F (foldl')
import Data.Group (Abelian)
Expand All @@ -36,6 +35,7 @@ class
, NoThunks t
, EncCBOR t
, DecCBOR t
, FromJSON t
, ToJSON t
, NFData t
, Show t
Expand Down
2 changes: 2 additions & 0 deletions libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Era.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ ledgerEraTestMain extraEraSpec =
roundTripAesonProperty @(TxAuxData era)
prop (show $ typeRep $ Proxy @(TxWits era)) $
roundTripAesonProperty @(TxWits era)
prop (show $ typeRep $ Proxy @(TxOut era)) $
roundTripAesonProperty @(TxOut era)
describe "Era-specific spec" extraEraSpec

-- | This is a helper function that uses `mkTestAccountState` to register an account.
Expand Down