Skip to content

Commit

Permalink
Switch to cardano-ledger-binary from cardano-binary throughout le…
Browse files Browse the repository at this point in the history
…dger:

* Few additions to `cardano-ledger-binary`:
  * Addition of `dropCBOR`
  * Instances for 6-tuple and roundtrip tests for tuples and dropper
  * Move Helpers needed for Byron into the test-lib.
  * Add helper bindings for Byron and Shelley versions, since those are
    most common for now
  * Move `CBORGroup` into `cardano-ledger-binary`
  * Add `mkVersion64`
  * Add `NFData` and `NoThunks` for `Version`
  * Addition of `QC` that allows instance of `StatefulGen` for
    QuickCheck's `Gen`.
  * Improve `Arbitrary` by tapping into `StatefulGen`
  * Add helpers for creating hashes and `StdGen` from serialized data:
    `mkDummyHash` and `mkHashStdGen`
  * Add `hashWithEncoder` and `hashToCBOR`
  * Improve RounTrip testing and error reporting
  * Added `ToCBOR`/`FromCBOR` instances for `OutputVRF`, `CertifiedVRF`,
    `SlotNo`, `WithOrigin, `EpochNo`, `EpochSize`, `SystemStart` and `BlockN`.

* Switched `pvMajor` in `ProtVer` to `Version`
* Start using `_protocolVersion` from `PParams` wherever `Version`
  is needed and `PParams` are available. Derive `Version` from `era` in
  all other places by using `eraProtVerLow` and `eraProtVerHigh`
* Created `translateEraThroughCBOR` for easy translation of types from
  one era to another
  • Loading branch information
lehins committed Nov 21, 2022
1 parent dc5331f commit a7686f6
Show file tree
Hide file tree
Showing 356 changed files with 4,279 additions and 5,959 deletions.
20 changes: 15 additions & 5 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,8 @@ in the naming of release branches.
- Also add a harmless dummy `Show` instance for `PlutusDebug` with a constant `"PlutusDebug Omitted"`
- Create a `cardano-ledger-binary` package that does versioned serialization as a
replacement for `cardano-binary` package: #3063, #3078
- Switch to `cardano-ledger-binary` package throughout the ledger codebase: #3138
- Created `translateEraThroughCBOR`: #3138
- Start on the `cardano-ledger-api` package and implement
`setMinCoinTxOut`+`setMinCoinSizedTxOut`: #2995
- Added `getMinCoinTxOut`/`getMinCoinSizedTxOut` to `EraTxOut`: #3008
Expand All @@ -30,6 +32,7 @@ in the naming of release branches.

### Changed

- Changed major version in `ProtVer` to use new type `Version` instead of `Natural`: #3138
- Renamed records fields in `Cardano.Ledger` to names without `_` (underscores) #3126
- `Alonzo.TxBody.TxBodyRaw` to `Alonzo.TxBody.AlonzoTxBodyRaw`
- `_inputs` to `atbrInputs`
Expand Down Expand Up @@ -179,7 +182,7 @@ in the naming of release branches.
- Moved Cardano.Ledger.Shelley.Orphans to Cardano.Ledger.Orphans in the core modules
- Moved Cardano.Ledger.Shelley.PoolParms to Cardano.Ledger.PoolParams in the core modules
- Moved Cardano.Ledger.Shelley.EpochBoundary to Cardano.Ledger.EpochBoundary in the core modules
- Added deprecated modules that import the moved ones and export their contents
- Added deprecated modules that import the moved ones and export their contents
- Moved `Data.AbstractSize` from `cardano-data` to `byron-spec-ledger` #3046
- Renamed in `Cardano.Ledger.Mary.Value`: #3047
- `insert` to `insertMultiAsset`
Expand All @@ -188,15 +191,15 @@ in the naming of release branches.
#2954
- All Shelley rules are now available through `Cadano.Ledger.Shelley.Rules` module: #2996
- Renamed the `Crypto` dependent type in the `Era` class to `EraCrypto` #3009
- Renamed in `Cardano.Ledger.Core`:
- Renamed in `Cardano.Ledger.Core` #2976:
- `EraWitnesses` to `EraTxWits`
- `Witnesses` to `EraTxWits`
- `mkBasicWitnesses` to `mkBasicTxWits`
- `addrWitsL` to `addrTxWitsL`
- `bootAddrWitsL` to `bootAddrTxWitsL`
- `scriptWitsL` to `scriptTxWitsL`

- Renamed in (new) module `Cardano.Ledger.Alonzo.TxWits` (renamed from `Cardano.Ledger.Alonzo.TxWitness`):
- Renamed in (new) module `Cardano.Ledger.Alonzo.TxWits` (renamed from `Cardano.Ledger.Alonzo.TxWitness`) #2976:
- `AlonzoEraWitnesses` to `AlonzoEraTxWits`
- `TxWitness` to `AlonzoTxWits`
- `addrAlonzoWitsL` to `addrAlonzoTxWitsL`
Expand All @@ -207,13 +210,12 @@ in the naming of release branches.
- `rdmrsWitsL` to `rdmrsTxWitsL`
- `rdmrsAlonzoWitsL` to `rdmrsAlonzoTxWitsL`

- Renamed in (new) module `Cardano.Ledger.Shelley.TxWits` (extracted from `Cardano.Ledger.Shelley.Tx`):
- Renamed in (new) module `Cardano.Ledger.Shelley.TxWits` (extracted from `Cardano.Ledger.Shelley.Tx`) #2976:
- `ShelleyWitnesses` to `ShelleyTxWits`
- `addrShelleyWitsL` to `addrShelleyTxWitsL`
- `bootAddrShelleyWitsL` to `bootAddrShelleyTxWitsL`
- `scriptShelleyWitsL` to `scriptShelleyTxWitsL`
- Updated package metadata #3023
#2976
- Moved `TxOut` to a separate module in each era #3024
- Moved `mintedTxBodyF` into `ShelleyMAEraTxBody` class #3019
- Moved thet `RewardType` and `Reward` types from the `Cardano.Ledger.Shelley.Reward` module in the
Expand All @@ -222,6 +224,13 @@ in the naming of release branches.

### Removed

- Deprecated `Cardano.Ledger.Serialization` in favor of `Cardano.Ledger.Binary` from
`cardano-ledger-binary`: #3138
- Removed `Data.Coders` from `cardano-data` in favor of `Cardano.Ledger.Binary.Coders` from
`cardano-ledger-binary`: #3138
- Removed `Data.Sharing` from `cardano-data` in favor of `Cardano.Ledger.Binary` from
`cardano-ledger-binary`: #3138
- Removed `boundedRationalFromCBOR`/`boundedRationalToCBOR` as obsolete and invalid: #3138
- Removed pattern synonym `Cardano.Ledger.ShelleyMA.TxBody.MATxBody.TxBody'` with fields #3126
- `adHash'`
- `certs'`
Expand All @@ -248,6 +257,7 @@ in the naming of release branches.
- The `Cardano.Ledger.Alonzo.Scripts` module no longer re-exports the
`plutus-ledger-api`'s `assertWellFormedCostModelParams`. #3065
- Removed unused `Data.BiMap` module from `cardano-data` #3089
- Removed `getMultiSigBytes` as unused #?

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion eras/alonzo/impl/cardano-ledger-alonzo.cabal
Expand Up @@ -66,9 +66,9 @@ library
base-deriving-via,
base64-bytestring,
bytestring,
cardano-binary,
cardano-data,
cardano-crypto-class,
cardano-ledger-binary,
cardano-ledger-core,
cardano-ledger-shelley-ma,
cardano-slotting,
Expand Down
52 changes: 28 additions & 24 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Data.hs
Expand Up @@ -12,6 +12,7 @@
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
-- This is needed to make Plutus.Data instances
Expand Down Expand Up @@ -43,24 +44,29 @@ module Cardano.Ledger.Alonzo.Data
)
where

import Cardano.Binary
( DecoderError (..),
import Cardano.Crypto.Hash.Class (HashAlgorithm)
import Cardano.HeapWords (HeapWords (..), heapWords0, heapWords1)
import Cardano.Ledger.Alonzo.Era
import Cardano.Ledger.Alonzo.Language (Language (..))
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..), validScript)
import Cardano.Ledger.AuxiliaryData (AuxiliaryDataHash (..))
import Cardano.Ledger.BaseTypes (ProtVer, StrictMaybe (..))
import Cardano.Ledger.Binary
( Annotator (..),
DecoderError (..),
FromCBOR (..),
ToCBOR (..),
TokenType (..),
decodeAnnotator,
decodeFullAnnotator,
decodeNestedCborBytes,
decodeStrictSeq,
encodeTag,
fromPlainDecoder,
fromPlainEncoding,
peekTokenType,
withSlice,
)
import Cardano.Crypto.Hash.Class (HashAlgorithm)
import Cardano.HeapWords (HeapWords (..), heapWords0, heapWords1)
import Cardano.Ledger.Alonzo.Era
import Cardano.Ledger.Alonzo.Language (Language (..))
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..), validScript)
import Cardano.Ledger.AuxiliaryData (AuxiliaryDataHash (..))
import Cardano.Ledger.BaseTypes (ProtVer, StrictMaybe (..))
import Cardano.Ledger.Binary.Coders
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (HASH)
import qualified Cardano.Ledger.Crypto as CC
Expand All @@ -71,13 +77,11 @@ import Cardano.Ledger.SafeHash
SafeToHash (..),
hashAnnotated,
)
import Cardano.Ledger.Serialization (mapFromCBOR)
import Cardano.Ledger.Shelley.Metadata (Metadatum, validMetadatum)
import qualified Codec.Serialise as Cborg (Serialise (..))
import Control.DeepSeq (NFData)
import Data.ByteString.Lazy (fromStrict)
import Data.ByteString.Short (ShortByteString, fromShort, toShort)
import Data.Coders
import Data.Foldable (foldl')
import Data.Map (Map)
import Data.Maybe (mapMaybe)
Expand All @@ -95,10 +99,10 @@ import qualified PlutusLedgerApi.V1 as Plutus
-- instances to also work in the ledger.

instance FromCBOR (Annotator Plutus.Data) where
fromCBOR = pure <$> Cborg.decode
fromCBOR = pure <$> fromPlainDecoder Cborg.decode

instance ToCBOR Plutus.Data where
toCBOR = Cborg.encode
toCBOR = fromPlainEncoding . Cborg.encode

deriving instance NoThunks Plutus.Data

Expand All @@ -112,7 +116,7 @@ newtype PlutusData era = PlutusData Plutus.Data
deriving newtype (Eq, Generic, Show, ToCBOR, NFData, NoThunks, Cborg.Serialise)

instance Typeable era => FromCBOR (Annotator (PlutusData era)) where
fromCBOR = pure <$> Cborg.decode
fromCBOR = pure <$> fromPlainDecoder Cborg.decode

newtype Data era = DataConstr (MemoBytes PlutusData era)
deriving (Eq, Generic)
Expand Down Expand Up @@ -168,9 +172,9 @@ makeBinaryData sbs = do
Left e -> Left $ "Invalid CBOR for Data: " <> show e
Right _d -> Right binaryData

decodeBinaryData :: Era era => BinaryData era -> Either DecoderError (Data era)
decodeBinaryData :: forall era. Era era => BinaryData era -> Either DecoderError (Data era)
decodeBinaryData (BinaryData sbs) = do
plutusData <- decodeAnnotator "Data" fromCBOR (fromStrict (fromShort sbs))
plutusData <- decodeFullAnnotator (eraProtVerLow @era) "Data" fromCBOR (fromStrict (fromShort sbs))
pure (DataConstr (mkMemoBytes plutusData $ shortToLazy sbs))

-- | It is safe to convert `BinaryData` to `Data` because the only way to
Expand Down Expand Up @@ -276,10 +280,10 @@ encodeRaw metadata allScripts =
Tag 259 $
Keyed
(\m tss p1 p2 -> AuxiliaryDataRaw m (StrictSeq.fromList $ tss <> p1 <> p2))
!> Omit null (Key 0 $ mapEncode metadata)
!> Omit null (Key 1 $ E (encodeFoldable . mapMaybe getTimelock) timelocks)
!> Omit null (Key 2 $ E (encodeFoldable . mapMaybe getPlutus) plutusV1Scripts)
!> Omit null (Key 3 $ E (encodeFoldable . mapMaybe getPlutus) plutusV2Scripts)
!> Omit null (Key 0 $ To metadata)
!> Omit null (Key 1 $ E (toCBOR . mapMaybe getTimelock) timelocks)
!> Omit null (Key 2 $ E (toCBOR . mapMaybe getPlutus) plutusV1Scripts)
!> Omit null (Key 3 $ E (toCBOR . mapMaybe getPlutus) plutusV2Scripts)
where
getTimelock (TimelockScript x) = Just x
getTimelock _ = Nothing
Expand Down Expand Up @@ -313,13 +317,13 @@ instance
decodeShelley =
decode
( Ann (Emit AuxiliaryDataRaw)
<*! Ann (D mapFromCBOR)
<*! Ann From
<*! Ann (Emit StrictSeq.empty)
)
decodeShelleyMA =
decode
( Ann (RecD AuxiliaryDataRaw)
<*! Ann (D mapFromCBOR)
<*! Ann From
<*! D
( sequence
<$> decodeStrictSeq
Expand All @@ -332,7 +336,7 @@ instance
SparseKeyed "AuxiliaryData" (pure emptyAuxData) auxDataField []

auxDataField :: Word -> Field (Annotator (AuxiliaryDataRaw era))
auxDataField 0 = fieldA (\x ad -> ad {txMD' = x}) (D mapFromCBOR)
auxDataField 0 = fieldA (\x ad -> ad {txMD' = x}) From
auxDataField 1 =
fieldAA
(\x ad -> ad {scripts' = scripts' ad <> (TimelockScript <$> x)})
Expand Down
77 changes: 51 additions & 26 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Genesis.hs
Expand Up @@ -17,7 +17,6 @@ module Cardano.Ledger.Alonzo.Genesis
)
where

import Cardano.Binary
import Cardano.Crypto.Hash.Class (hashToTextAsHex)
import Cardano.Ledger.Alonzo.Language (Language (..))
import Cardano.Ledger.Alonzo.PParams
Expand All @@ -32,14 +31,15 @@ import Cardano.Ledger.Alonzo.Scripts
)
import Cardano.Ledger.Alonzo.TxBody (AlonzoTxOut (AlonzoTxOut))
import qualified Cardano.Ledger.BaseTypes as BT
import Cardano.Ledger.Binary
import Cardano.Ledger.Binary.Coders
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Core
import Cardano.Ledger.SafeHash (extractHash)
import Cardano.Ledger.Shelley.PParams (ShelleyPParams)
import Data.Aeson (Array, FromJSON (..), Object, ToJSON (..), object, (.!=), (.:), (.:?), (.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (FromJSONKey (..), Parser, ToJSONKey (..), toJSONKeyText)
import Data.Coders
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe, maybeToList)
Expand Down Expand Up @@ -318,30 +318,55 @@ instance FromJSON (AlonzoPParams era) where
parseJSON =
Aeson.withObject "PParams" $ \obj ->
AlonzoPParams
<$> obj .: "minFeeA"
<*> obj .: "minFeeB"
<*> obj .: "maxBlockBodySize"
<*> obj .: "maxTxSize"
<*> obj .: "maxBlockHeaderSize"
<*> obj .: "keyDeposit"
<*> obj .: "poolDeposit"
<*> obj .: "eMax"
<*> obj .: "nOpt"
<*> obj .: "a0"
<*> obj .: "rho"
<*> obj .: "tau"
<*> obj .: "decentralisationParam"
<*> obj .: "extraEntropy"
<*> obj .: "protocolVersion"
<*> obj .: "minPoolCost" .!= mempty
<*> obj .: "lovelacePerUTxOWord"
<*> obj .: "costmdls"
<*> obj .: "prices"
<*> obj .: "maxTxExUnits"
<*> obj .: "maxBlockExUnits"
<*> obj .: "maxValSize"
<*> obj .: "collateralPercentage"
<*> obj .: "maxCollateralInputs"
<$> obj
.: "minFeeA"
<*> obj
.: "minFeeB"
<*> obj
.: "maxBlockBodySize"
<*> obj
.: "maxTxSize"
<*> obj
.: "maxBlockHeaderSize"
<*> obj
.: "keyDeposit"
<*> obj
.: "poolDeposit"
<*> obj
.: "eMax"
<*> obj
.: "nOpt"
<*> obj
.: "a0"
<*> obj
.: "rho"
<*> obj
.: "tau"
<*> obj
.: "decentralisationParam"
<*> obj
.: "extraEntropy"
<*> obj
.: "protocolVersion"
<*> obj
.: "minPoolCost"
.!= mempty
<*> obj
.: "lovelacePerUTxOWord"
<*> obj
.: "costmdls"
<*> obj
.: "prices"
<*> obj
.: "maxTxExUnits"
<*> obj
.: "maxBlockExUnits"
<*> obj
.: "maxValSize"
<*> obj
.: "collateralPercentage"
<*> obj
.: "maxCollateralInputs"

deriving instance ToJSON (AlonzoPParamsUpdate era)

Expand Down

0 comments on commit a7686f6

Please sign in to comment.