Skip to content

Commit

Permalink
Restructure the Core for consistency, clarity and new functionality
Browse files Browse the repository at this point in the history
**Creation of class hierarchy**

**New classes**

```
EraSegWits --> EraTx --> EraTxBody --> EraTxOut --> Era
                     \              `--> EraPParams --> Era
                       `--> EraWitnesses --> EraScript --> Era
                        `--> EraAuxiliaryData --> Era
```

Note that all of the `Era*` classes have a few properties:

* depend either directly or transitively on the `Era` type class.
* define an associated type family (which was previously standalone).
* add all constraints that the associated type must implement.

This means that any function that uses an associated type family can safely use
the corresponding type class. For example a function that accepts `TxBody` as
argument can always set `EraTx` as a constraint, which will bring all of the
superclasses as constraints as well.

Loose corespondance of new classes to the old removed concepts:

* `Era` class is the same as before with the addition of type level protocol
  version and deprecation of all of the functions. It also implements now a per era
  deprecation mechanism with protocol version bounds
* `EraTxOut` has  all superclasses as in `UsesTxOut` type  synonym constraint as
  well  as defines  lenses that  supercede all  functions previously  defined in
  `Era` type class
* `EraTxBody` has all superclasses as in `UsesTxBody` type synonym constraint
* `EraAuxiliaryData` has all superclasses as in `UsesAuxiliaryData` type synonym
  constraint and consolidates `ValidateAuxiliaryData` into it
* `EraPParams` replaces `UsesPParams` type class now with an associated type family
* `EraScript` has replaced `UsesScript` type synonym constraint and the
  `ValidateScripts` type class, with an exception of `validateScript` function,
  which was moved into `EraTx` type class, since it depends on the `Tx`

**Lenses**

This PR expands usage of
[`microlens`](https://hackage.haskell.org/package/microlens) package.

Each class defines lenses for associated types, all of which are getters and
potentially setter. They also define a way to construct a minimal value of an
asscoiated type. For example if we want to construct a function that will
construct a `Tx` with a new `TxBody`, which uses `AuxiliaryData` from another
`Tx`:

```haskell
newTxWithOtherAuxData :: EraTx era => TxBody era -> Tx era -> Tx era
newTxWithOtherAuxData txBody tx =
  mkBasicTx txBody & auxDataTxL .~ (tx ^. auxDataTxL)
```

Above function is guaranteed to work with transactions of all eras.

Some are just getters, which simply relay some information from one or more
fields of a concrete type, altohugh they might do some minimal computation. For
example `allInputsTxBodyF` will return all of the inputs present in the
`TxBody`.

**Naming convention**

All lenses are suffixed with the type name they operate on and either `L`ens or `G`etter.

**Migration guide**

**Accessor fields**

Names suffered some very minor adjustments that don't match the spec (tagged with *)

**TxOut**

* `makeTxOut (Proxy era)` -> `mkBasicTxOut`
* `getField @"value" txOut` -> `txOut ^. valueTxOutL`
* `_` -> `txOut ^. compactValueTxOutL`
* `getCoin` -> `txOut ^. coinTxOutL`
* `getTxOutAddr txOut` -> `txOut ^. addrTxOutL`
* `getTxOutEitherAddr txOut` -> `txOut ^. addrEitherTxOutL`
* `getTxOutCompactAddr txOut` -> `txOut ^. compactAddrTxOutL`
* `getTxOutBootstrapAddress txOut` -> `txOut ^. bootAddrTxOutF`

* `getField @"datahash" txOut` -> `txOut ^. dataHashTxOutL` (*)
* `getField @"referenceScript" txOut` -> `txOut ^. referenceScriptTxOutL`
* `_` -> `txOut ^. dataTxOutL`
* `_` -> `txOut ^. datumTxOutL`

**TxBody**

* `getField @"inputs" txBody` -> ` txBody ^. inputsTxBodyL`
* `getField @"outputs" txBody` -> ` txBody ^. outputsTxBodyL`
* `getField @"txfee" txBody` -> ` txBody ^. feeTxBodyL` (*)
* `getField @"auxiliaryData" txBody` -> ` txBody ^. auxDataHashTxBodyL` (*)
* `getAllInputs txBody` -> ` txBody ^. allInputsTxBodyF`
* `getField @"minted" txBody` -> ` txBody ^. mintedTxBodyF`

* `getField @"wdrls" txBody` -> `txBody ^. wdrlsTxBodyL`
* `getField @"ttl" txBody` -> `txBody ^. ttlTxBodyL`
* `getField @"update" txBody` -> `txBody ^. updateTxBodyL`
* `getField @"certs" txBody` -> `txBody ^. certsTxBodyL`
* `getField @"vldt" txBody` -> `txBody ^. vldtTxBodyL`
* `getField @"mint" txBody` -> `txBody ^. mintTxBodyL`
* `getField @"collateral" txBody` -> `txBody ^. collateralInputsTxBodyL` (*)
* `getField @"reqSignerHashes" txBody` -> `txBody ^. reqSignerHashesTxBodyL`
* `getField @"scriptIntegrityHash" txBody` -> `txBody ^. scriptIntegrityHashTxBodyL`
* `getField @"txnetworkid" txBody` -> `txBody ^. networkIdTxBodyL` (*)

* `getField @"sizedOutputs" txBody` -> `txBody ^. sizedOutputsTxBodyL`
* `getField @"referenceInputs" txBody` -> `txBody ^. referenceInputsTxBodyL`
* `getField @"totalCollateral" txBody` -> `txBody ^. totalCollateralTxBodyL`
* `getField @"collateralReturn" txBody` -> `txBody ^. collateralReturnTxBodyL`
* `getField @"sizedCollateralReturn" txBody` -> `txBody ^. sizedCollateralReturnTxBodyL`

**Tx**

* `getField @"body" tx` -> `tx ^. bodyTxL`
* `getField @"wits" tx` -> `tx ^. witsTxL`
* `getField @"auxData" tx` -> `tx ^. auxDataTxL`
* `getField @"size" tx` -> `tx ^. sizeTxF`
* `getField @"isValid" tx` -> `tx ^. isValidTxL`

Nested instances are no longer needed, since composition of lenses works much better:

* `getField @"addrWits" tx` -> `tx ^. witsTxL . addrWitsL`
* `getField @"scriptWits" tx` -> `tx ^. witsTxL . scriptsWitsL`
* `getField @"bootWits" tx` -> `tx ^. witsTxL . bootAddrWitsL`
* `getField @"txdatahash" tx` -> `tx ^. witsTxL . datsWitsL . to unTxDats`

**Witnesses**

* `getField @"addr" txWits` -> `txWits ^. addrWitsL`
* `getField @"bootAddr" txWits` -> `txWits ^. bootAddrWitsL`
* `getField @"script" txWits` -> `txWits ^. scriptWitsL`
* `getField @"dats" txWits` -> `txWits ^. datsWitsL`
* `getField @"rdmrs" txWits` -> `txWits ^. rdmrsWitsL`

**Removal of constraint type synonyms**

All of the `Trans*`, `Uses*` constraints have been removed as they are fully
replaced by the above hierarchy

Names removed and their closest replacements:

* `BlockAnn` -> `EraTx`
* `ChainData` - no replacemnt, this was just an adhoc collection of classes
    SerialisableData,
* `AnnotatedData` -> `FromCBOR (Annotator t)` + `ToCBOR t`
* `SerialisableData` -> `FromCBOR t` + `ToCBOR t`
* `WellFormed` -> `EraTx`
* `ShelleyEraCrypto`

**`Val` class**

Addition of two class methods that allow efficient manipulation of compact coin
representation: `injectCompact` and `modifyCompactCoin`, without uncompacting
the actual type `t` for which the `Val` instance is being defined

**Data type renaming**

All concrete types for every era where either inconsistent or clashing with the
core type families:

* Before this commit:

| Core          | Shelley       | ShelleyMA       | Alonzo        | Babbage       |
|:-------------:|:-------------:|:---------------:|:-------------:|:-------------:|
| Tx            | Tx            |    --           | ValidatedTx   |    --         |
| TxBody        | TxBody        | MATxBody        | TxBody        | TxBody        |
| TxOut         | TxOut         |    --           | TxOut         | TxOut         |
| AuxiliaryData | AuxiliaryData | MAAuxiliaryData | AuxiliaryData |    --         |
| Witnesses     | WitnessSetHKD |    --           | TxWitness     |    --         |
| Script        | MultiSig      | Timelock        | Script        |    --         |
| Value         | Coin          | Value           | --            |    --         |
| PParams       | PParams       |    --           | PParams       | PParams       |
| PParamsDelta  | PParamsUpdate |    --           | PParamsUpdate | PParamsUpdate |

* After this commit

| Core          | Shelley              | ShelleyMA       | Alonzo              | Babbage              |
|:-------------:|:--------------------:|:---------------:|:-------------------:|:--------------------:|
| Tx            | ShelleyTx            |    --           | AlonzoTx            |    --                |
| TxBody        | ShelleyTxBody        | MATxBody        | AlonzoTxBody        | BabbageTxBody        |
| TxOut         | ShelleyTxOut         |    --           | AlonzoTxOut         | BabbageTxOut         |
| AuxiliaryData | ShelleyAuxiliaryData | MAAuxiliaryData | AlonzoAuxiliaryData |    --                |
| Witnesses     | WitnessSetHKD        |    --           | TxWitness           |    --                |
| Script        | MultiSig             | Timelock        | AlonzoScript        |    --                |
| Value         | Coin                 | MaryValue       | --                  |    --                |
| PParams       | ShelleyPParams       |    --           | AlonzoPParams       | BabbagePParams       |
| PParamsUpdate | ShelleyPParamsUpdate |    --           | AlonzoPParamsUpdate | BabbagePParamsUpdate |

**Module restructure**

Concrete era data type (eg. `BabageEra`) is no longer defined at the top level
module (eg. `Cardano.Ledger.Babbage`), but rather in a new private `.Era` module
(eg. `Cardano.Ledger.Babbage.Era`). It is later re-exported from the top level
module together with all of the orphan instances. This allows us to define all
of the relevant instances in their respective modules (eg. `EraTxBody` is defined
in the `TxBody` module)

**Other minor changes**

* Defined `NFData` for `Sized`
* Rename `Cardano.Ledger.Shelley.Core` to `Cardano.Ledger.Shelley.Type`
* Move module `Cardano.Ledger.Shelley.Address.Bootstrap`
  (`cardano-ledger-shelley`) -> `Cardano.Ledger.Keys.Bootstrap` (`cardano-ledger-core`)
* Moved `langsUsed` into tests, since that was the only use site.
  • Loading branch information
lehins committed Jul 26, 2022
1 parent 14e1bcc commit acd88e8
Show file tree
Hide file tree
Showing 259 changed files with 9,933 additions and 10,287 deletions.
94 changes: 94 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,100 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
in the naming of release branches.


### Added
- Core type classes: `EraSegWits`, `EraTx`, `EraTxBody`, `EraTxOut`, `EraPParams`,
`EraAuxiliaryData`, `EraWitnesses`, `EraScript`
- Era specific type classes: `ShelleyEraTxBody`, `ShelleyMAEraTxBody`,
`AlonzoEraTxBody`, `AlonzoEraTxOut`, `AlonzoEraTx`, `BabbageEraTxBody`, `BabbageEraTxOut`
- Type class hierarchy:
```
EraSegWits --> EraTx --> EraTxBody --> EraTxOut --> Era
\ `--> EraPParams --> Era
`--> EraWitnesses --> EraScript --> Era
`--> EraAuxiliaryData --> Era
```
- Shelley:
```
ShelleyEraTxBody --> EraTxBody --> EraTxOut --> Era
```
- ShelleyMA:
```
ShelleyMAEraTxBody --> ShelleyEraTxBody --> EraTxBody --> EraTxOut --> Era
```
- Alonzo:
```
AlonzoEraTx --> EraTx --> ...
`--> AlonzoEraTxBody --> ShelleyMAEraTxBody --> ShelleyEraTxBody --> EraTxBody --> ...
`--> AlonzoEraTxOut -> ShelleyEraTxOut --> EraTxOut --> ...
```
- Babbage:
```
BabbageEraTxBody --> AlonzoEraTxBody --> ....
`--> BabbageEraTxOut -> AlonzoEraTxOut -->
```
### Changed
- Renamed `SupportsSegWit` to `EraSegWits`
- Split `ValidateScript` into `EraScript` and `EraTx.validateScript`
- Renamed `ValidateAuxiliaryData` to `EraAuxiliaryData` while removing usage of FunDeps.
- Renamed in `Cardano.Ledger.Shelley`:
- `Tx` to `ShelleyTx` (kept type synonym with a deprecation message)
- `TxOut` to `ShelleyTxOut` (kept type synonym with a deprecation message)
- `TxBody` to `ShelleyTxBody` (kept type synonym with a deprecation message)
- `PParams` to `ShelleyPParams` (kept type synonym with a deprecation message)
- `PParamsUpdate` to `ShelleyPParamsUpdate` (kept type synonym with a deprecation message)
- `AuxiliaryData` to `ShelleyAuxiliaryData` (kept type synonym with a deprecation message)
- Renamed in `Cardano.Ledger.Mary`:
- Renamed `Value` to `MaryValue` (kept type synonym with a deprecation message)
- Renamed in `Cardano.Ledger.ShelleyMA`:
- `TxBody` to `MATxBody` (kept type synonym with a deprecation message)
- `AuxiliaryData` to `ShelleyAuxiliaryData` (kept type synonym with a deprecation message)
- Renamed in `Cardano.Ledger.Alonzo`:
- `ValidatedTx` to `AlonzoTx` (kept type synonym with a deprecation message)
- `TxOut` to `AlonzoTxOut` (kept type synonym with a deprecation message)
- `TxBody` to `AlonzoTxBody` (kept type synonym with a deprecation message)
- `Script` to `AlonzoScript` (kept type synonym with a deprecation message)
- `PParams` to `AlonzoPParams` (kept type synonym with a deprecation message)
- `PParamsUpdate` to `AlonzoPParamsUpdate` (kept type synonym with a deprecation message)
- `AuxiliaryData` to `AlonzoAuxiliaryData` (kept type synonym with a deprecation message)
- Renamed in `Cardano.Ledger.Babbage`:
- `TxOut` to `BabbageTxOut` (kept type synonym with a deprecation message)
- `TxBody` to `BabbageTxBody` (kept type synonym with a deprecation message)
- `PParams` to `BabbagePParams` (kept type synonym with a deprecation message)
- `PParamsUpdate` to `BabbagePParamsUpdate` (kept type synonym with a deprecation message)
### Deprecated
- `getTxOutAddr txOut` in favor of `txOut ^. addrTxOutL`
- `getTxOutEitherAddr txOut` in favor of `txOut ^. addrEitherTxOutL`
- `getTxOutCompactAddr txOut` in favor of `txOut ^. compactAddrTxOutL`
- `getTxOutBootstrapAddress txOut` in favor of `txOut ^. bootAddrTxOutF`
- `getAllInputs txBody` in favor of ` txBody ^. allInputsTxBodyF`
- `getCoin txOut` in favor of `txOut ^. coinTxOutL`
### Removed
- `makeTxOut` in favor of `mkBasicTxOut`
- `HasField` instances for: `"inputs"`, `"outputs"`, `"txfee"`,
`"auxiliaryData"`, `"minted"`, `"wdrls"`, `"ttl"`, `"update"`, `"certs"`,
`"vldt"`, `"mint"`, `"collateral"`, `"reqSignerHashes"`,
`"scriptIntegrityHash"`, `"txnetworkid"`, `"sizedOutputs"`,
`"referenceInputs"`, `"totalCollateral"`, `"collateralReturn"`,
`"sizedCollateralReturn"`, `"body"`, `"wits"`, `"auxData"`, `"size"`,
`"isValid"`, `"addrWits"`, `"scriptWits"`, `"bootWits"`, `"txdatahash"`,
`"addr"`, `"bootAddr"`, `"script"`, `"dats"`, `"rdmrs"`
- `ValidateScript` in favor of `EraScript` and `EraTx`
- Type class synonyms:
- `Trans*`
- `Uses*`: `UsesPParams`, `UsesScript`, `UsesTxBody`, `UsesTxOut`, `UsesAuxiliaryData`
- `BlockAnn`
- `ChainData`
- `AnnotatedData`
- `SerialisableData`
- `WellFormed`
- `ConcreteAlonzo`
- `ConcreteBabbage`
- ...

### Fixed


## [Unreleased]
### Added
- Added `coinsPerUTxOByteToCoinsPerUTxOWord` helper function for Babbage
Expand Down
15 changes: 9 additions & 6 deletions eras/alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ library
Cardano.Ledger.Alonzo.Language
Cardano.Ledger.Alonzo.PlutusScriptApi
Cardano.Ledger.Alonzo.PParams
Cardano.Ledger.Alonzo.Rules.Bbody
Cardano.Ledger.Alonzo.Rules.Ledger
Cardano.Ledger.Alonzo.Rules.Utxo
Cardano.Ledger.Alonzo.Rules.Utxos
Cardano.Ledger.Alonzo.Rules.Utxow
Cardano.Ledger.Alonzo.Rules
Cardano.Ledger.Alonzo.Scripts
Cardano.Ledger.Alonzo.Tools
Cardano.Ledger.Alonzo.Translation
Expand All @@ -55,7 +51,13 @@ library
Cardano.Ledger.Alonzo.TxInfo
Cardano.Ledger.Alonzo.TxSeq
Cardano.Ledger.Alonzo.TxWitness
Cardano.Ledger.DescribeEras
other-modules:
Cardano.Ledger.Alonzo.Era
Cardano.Ledger.Alonzo.Rules.Bbody
Cardano.Ledger.Alonzo.Rules.Ledger
Cardano.Ledger.Alonzo.Rules.Utxo
Cardano.Ledger.Alonzo.Rules.Utxos
Cardano.Ledger.Alonzo.Rules.Utxow
build-depends:
aeson >= 2,
array,
Expand All @@ -74,6 +76,7 @@ library
deepseq,
measures,
mtl,
microlens,
nothunks,
plutus-ledger-api ^>= 1.0,
plutus-tx ^>= 1.0,
Expand Down
Loading

0 comments on commit acd88e8

Please sign in to comment.