Skip to content

Commit

Permalink
Drop 'TxInBlock' and use Core.Tx family.
Browse files Browse the repository at this point in the history
Since `Tx` will now be what is submitted by the wallet/node, there is no
need for a separate `TxInBlock` concept. Instead, `Tx` becomes a type
family (since it _will_ vary between eras) and everything is defined in
those terms.

As part of this, we alter the `SupportsSegWit` type class to reflect now
the isomorphism between `TxSeq` and `Seq Tx`.
  • Loading branch information
nc6 committed Jul 20, 2021
1 parent 6f1d005 commit b75367f
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 141 deletions.
8 changes: 3 additions & 5 deletions alonzo/impl/src/Cardano/Ledger/Alonzo.hs
Expand Up @@ -104,7 +104,6 @@ import qualified Shelley.Spec.Ledger.STS.Snap as Shelley
import qualified Shelley.Spec.Ledger.STS.Tick as Shelley
import qualified Shelley.Spec.Ledger.STS.Upec as Shelley
import Shelley.Spec.Ledger.STS.Utxow (UtxowPredicateFailure (UtxoFailure))
import Shelley.Spec.Ledger.Tx (Tx (Tx))
import qualified Shelley.Spec.Ledger.Tx as Shelley
import Shelley.Spec.Ledger.UTxO (balance)

Expand Down Expand Up @@ -163,8 +162,6 @@ instance API.PraosCrypto c => API.ApplyTx (AlonzoEra c) where
$ TRC (env, state, tx)
in liftEither . left API.ApplyTxError $ res

extractTx ValidatedTx {body = b, wits = w, auxiliaryData = a} = Tx b w a

instance API.PraosCrypto c => API.ApplyBlock (AlonzoEra c)

instance (API.PraosCrypto c) => API.GetLedgerView (AlonzoEra c)
Expand Down Expand Up @@ -230,13 +227,15 @@ instance CC.Crypto c => API.CLI (AlonzoEra c) where

evaluateConsumed = consumed

addKeyWitnesses (Tx b ws aux) newWits = Tx b ws' aux
addKeyWitnesses (ValidatedTx b ws aux iv) newWits = ValidatedTx b ws' aux iv
where
ws' = ws {txwitsVKey = Set.union newWits (txwitsVKey ws)}

evaluateMinLovelaceOutput pp out =
Coin $ utxoEntrySize out * unCoin (_coinsPerUTxOWord pp)

type instance Core.Tx (AlonzoEra c) = ValidatedTx (AlonzoEra c)

type instance Core.TxOut (AlonzoEra c) = TxOut (AlonzoEra c)

type instance Core.TxBody (AlonzoEra c) = TxBody (AlonzoEra c)
Expand Down Expand Up @@ -266,7 +265,6 @@ instance CC.Crypto c => ValidateAuxiliaryData (AlonzoEra c) c where

instance CC.Crypto c => EraModule.SupportsSegWit (AlonzoEra c) where
type TxSeq (AlonzoEra c) = Alonzo.TxSeq (AlonzoEra c)
type TxInBlock (AlonzoEra c) = ValidatedTx (AlonzoEra c)
fromTxSeq = Alonzo.txSeqTxns
toTxSeq = Alonzo.TxSeq
hashTxSeq = Alonzo.hashTxSeq
Expand Down
10 changes: 5 additions & 5 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Bbody.hs
Expand Up @@ -28,7 +28,7 @@ import qualified Cardano.Ledger.Alonzo.TxSeq as Alonzo (TxSeq)
import Cardano.Ledger.Alonzo.TxWitness (TxWitness)
import Cardano.Ledger.BaseTypes (ShelleyBase, UnitInterval, epochInfo)
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Era (Era (Crypto), SupportsSegWit (..), TxInBlock)
import Cardano.Ledger.Era (Era (Crypto), SupportsSegWit (..))
import qualified Cardano.Ledger.Era as Era
import Cardano.Ledger.Keys (DSignable, Hash, coerceKeyRole)
import Cardano.Ledger.Slot (epochInfoEpoch, epochInfoFirst)
Expand Down Expand Up @@ -139,13 +139,13 @@ bbodyTransition ::
Embed (Core.EraRule "LEDGERS" era) (someBBODY era),
Environment (Core.EraRule "LEDGERS" era) ~ LedgersEnv era,
State (Core.EraRule "LEDGERS" era) ~ LedgerState era,
Signal (Core.EraRule "LEDGERS" era) ~ Seq (TxInBlock era),
Signal (Core.EraRule "LEDGERS" era) ~ Seq (Core.Tx era),
-- Conditions to define the rule in this Era
HasField "_d" (Core.PParams era) UnitInterval,
HasField "_maxBlockExUnits" (Core.PParams era) ExUnits,
Era era, -- supplies WellFormed HasField, and Crypto constraints
Era.TxSeq era ~ Alonzo.TxSeq era,
Era.TxInBlock era ~ Alonzo.ValidatedTx era,
Core.Tx era ~ Alonzo.ValidatedTx era,
Core.Witnesses era ~ TxWitness era
) =>
TransitionRule (someBBODY era)
Expand Down Expand Up @@ -212,11 +212,11 @@ instance
State (Core.EraRule "LEDGERS" era) ~ LedgerState era,
Signal (Core.EraRule "LEDGERS" era) ~ Seq (Alonzo.ValidatedTx era),
Era era,
TxInBlock era ~ Alonzo.ValidatedTx era,
Core.Tx era ~ Alonzo.ValidatedTx era,
HasField "_d" (Core.PParams era) UnitInterval,
HasField "_maxBlockExUnits" (Core.PParams era) ExUnits,
Era.TxSeq era ~ Alonzo.TxSeq era,
Era.TxInBlock era ~ Alonzo.ValidatedTx era,
Core.Tx era ~ Alonzo.ValidatedTx era,
Core.Witnesses era ~ TxWitness era,
SupportsSegWit era
) =>
Expand Down
10 changes: 5 additions & 5 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Ledger.hs
Expand Up @@ -26,7 +26,7 @@ import Cardano.Ledger.Alonzo.Tx (IsValidating (..), ValidatedTx (..))
import Cardano.Ledger.BaseTypes (ShelleyBase)
import Cardano.Ledger.Coin (Coin)
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Era (Crypto, Era, TxInBlock)
import Cardano.Ledger.Era (Crypto, Era)
import Cardano.Ledger.Keys (DSignable, Hash)
import Control.State.Transition
( Assertion (..),
Expand Down Expand Up @@ -68,7 +68,7 @@ data AlonzoLEDGER era
-- make it concrete. Depends only on the "certs" and "isValidating" HasField instances.
ledgerTransition ::
forall (someLEDGER :: Type -> Type) era.
( Signal (someLEDGER era) ~ TxInBlock era,
( Signal (someLEDGER era) ~ Core.Tx era,
State (someLEDGER era) ~ (UTxOState era, DPState (Crypto era)),
Environment (someLEDGER era) ~ LedgerEnv era,
Embed (Core.EraRule "UTXOW" era) (someLEDGER era),
Expand All @@ -78,9 +78,9 @@ ledgerTransition ::
Signal (Core.EraRule "DELEGS" era) ~ Seq (DCert (Crypto era)),
Environment (Core.EraRule "UTXOW" era) ~ UtxoEnv era,
State (Core.EraRule "UTXOW" era) ~ UTxOState era,
Signal (Core.EraRule "UTXOW" era) ~ TxInBlock era,
Signal (Core.EraRule "UTXOW" era) ~ Core.Tx era,
HasField "certs" (Core.TxBody era) (StrictSeq (DCert (Crypto era))),
HasField "isValidating" (TxInBlock era) IsValidating,
HasField "isValidating" (Core.Tx era) IsValidating,
Era era
) =>
TransitionRule (someLEDGER era)
Expand Down Expand Up @@ -121,7 +121,7 @@ instance
Show (Core.PParamsDelta era),
DSignable (Crypto era) (Hash (Crypto era) EraIndependentTxBody),
Era era,
TxInBlock era ~ ValidatedTx era,
Core.Tx era ~ ValidatedTx era,
Embed (Core.EraRule "DELEGS" era) (AlonzoLEDGER era),
Embed (Core.EraRule "UTXOW" era) (AlonzoLEDGER era),
Environment (Core.EraRule "UTXOW" era) ~ UtxoEnv era,
Expand Down
12 changes: 6 additions & 6 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Utxo.hs
Expand Up @@ -46,7 +46,7 @@ import Cardano.Ledger.BaseTypes
import Cardano.Ledger.Coin
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Credential (Credential (..))
import Cardano.Ledger.Era (Crypto, Era, TxInBlock, ValidateScript (..))
import Cardano.Ledger.Era (Crypto, Era, ValidateScript (..))
import qualified Cardano.Ledger.Era as Era
import qualified Cardano.Ledger.Mary.Value as Alonzo (Value)
import Cardano.Ledger.Rules.ValidationMode ((?!#))
Expand Down Expand Up @@ -257,7 +257,7 @@ feesOK ::
( Era era,
ValidateScript era, -- isTwoPhaseScriptAddress
Core.TxOut era ~ Alonzo.TxOut era, -- balance requires this,
Era.TxInBlock era ~ Alonzo.ValidatedTx era,
Core.Tx era ~ Alonzo.ValidatedTx era,
Core.Witnesses era ~ TxWitness era,
HasField
"collateral" -- to get inputs to pay the fees
Expand All @@ -269,7 +269,7 @@ feesOK ::
HasField "_collateralPercentage" (Core.PParams era) Natural
) =>
Core.PParams era ->
TxInBlock era ->
Core.Tx era ->
UTxO era ->
Rule (AlonzoUTXO era) 'Transition ()
feesOK pp tx (UTxO m) = do
Expand Down Expand Up @@ -310,7 +310,7 @@ utxoTransition ::
Embed (Core.EraRule "UTXOS" era) (AlonzoUTXO era),
Environment (Core.EraRule "UTXOS" era) ~ Shelley.UtxoEnv era,
State (Core.EraRule "UTXOS" era) ~ Shelley.UTxOState era,
Signal (Core.EraRule "UTXOS" era) ~ TxInBlock era,
Signal (Core.EraRule "UTXOS" era) ~ Core.Tx era,
-- We leave Core.PParams abstract
UsesPParams era,
HasField "_minfeeA" (Core.PParams era) Natural,
Expand All @@ -325,11 +325,11 @@ utxoTransition ::
HasField "_collateralPercentage" (Core.PParams era) Natural,
HasField "_maxCollateralInputs" (Core.PParams era) Natural,
-- We fix Core.Tx, Core.Value, Core.TxBody, and Core.TxOut
Core.Tx era ~ Alonzo.ValidatedTx era,
Core.TxOut era ~ Alonzo.TxOut era,
Core.Value era ~ Alonzo.Value (Crypto era),
Core.TxBody era ~ Alonzo.TxBody era,
Core.Witnesses era ~ TxWitness era,
TxInBlock era ~ Alonzo.ValidatedTx era,
Era.TxSeq era ~ Alonzo.TxSeq era
) =>
TransitionRule (AlonzoUTXO era)
Expand Down Expand Up @@ -493,7 +493,7 @@ instance
Core.Witnesses era ~ TxWitness era,
Core.TxOut era ~ Alonzo.TxOut era,
Era.TxSeq era ~ Alonzo.TxSeq era,
Era.TxInBlock era ~ Alonzo.ValidatedTx era
Core.Tx era ~ Alonzo.ValidatedTx era
) =>
STS (AlonzoUTXO era)
where
Expand Down
8 changes: 4 additions & 4 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Utxow.hs
Expand Up @@ -41,7 +41,7 @@ import Cardano.Ledger.BaseTypes
)
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Credential (Credential (KeyHashObj))
import Cardano.Ledger.Era (Crypto, Era, SupportsSegWit (..), ValidateScript (..))
import Cardano.Ledger.Era (Crypto, Era, ValidateScript (..))
import Cardano.Ledger.Keys (GenDelegs, KeyHash, KeyRole (..), asWitness)
import Cardano.Ledger.Rules.ValidationMode ((?!#))
import Control.DeepSeq (NFData (..))
Expand Down Expand Up @@ -220,7 +220,7 @@ alonzoStyleWitness ::
forall era utxow.
( Era era,
-- Fix some Core types to the Alonzo Era
TxInBlock era ~ ValidatedTx era, -- scriptsNeeded, checkScriptData etc. are fixed at Alonzo.Tx
Core.Tx era ~ ValidatedTx era, -- scriptsNeeded, checkScriptData etc. are fixed at Alonzo.Tx
Core.PParams era ~ PParams era,
Core.Script era ~ Script era,
-- Allow UTXOW to call UTXO
Expand Down Expand Up @@ -251,7 +251,7 @@ alonzoStyleWitness = do
{- txw := txwits tx -}
{- witsKeyHashes := { hashKey vk | vk ∈ dom(txwitsVKey txw) } -}
let utxo = _utxo u'
txbody = getField @"body" (tx :: TxInBlock era)
txbody = getField @"body" (tx :: Core.Tx era)
witsKeyHashes = unWitHashes $ witsFromTxWitnesses @era tx

{- { h | (_ → (a,_,h)) ∈ txins tx ◁ utxo, isNonNativeScriptAddress tx a} = dom(txdats txw) -}
Expand Down Expand Up @@ -418,7 +418,7 @@ data AlonzoUTXOW era
instance
forall era.
( -- Fix some Core types to the Alonzo Era
TxInBlock era ~ ValidatedTx era,
Core.Tx era ~ ValidatedTx era,
Core.PParams era ~ PParams era,
Core.Script era ~ Script era,
-- Allow UTXOW to call UTXO
Expand Down
2 changes: 1 addition & 1 deletion alonzo/impl/src/Cardano/Ledger/Alonzo/Tools.hs
Expand Up @@ -20,7 +20,7 @@ import Cardano.Ledger.Alonzo.Scripts
ExUnits (..),
Script (..),
)
import Cardano.Ledger.Alonzo.Tx (DataHash, ScriptPurpose (Spending), rdptr)
import Cardano.Ledger.Alonzo.Tx (DataHash, ScriptPurpose (Spending), ValidatedTx (..), rdptr)
import Cardano.Ledger.Alonzo.TxBody (TxOut (..))
import Cardano.Ledger.Alonzo.TxInfo (txInfo, valContext)
import Cardano.Ledger.Alonzo.TxWitness (RdmrPtr (..), unRedeemers, unTxDats)
Expand Down
20 changes: 6 additions & 14 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Translation.hs
Expand Up @@ -36,7 +36,6 @@ import Cardano.Ledger.Era
TranslationContext,
translateEra',
)
import qualified Cardano.Ledger.Era as Era
import Cardano.Ledger.Mary (MaryEra)
import Control.Monad.Except (Except, throwError)
import Data.Coders
Expand Down Expand Up @@ -86,13 +85,6 @@ instance
nesPd = nesPd nes
}

instance Crypto c => TranslateEra (AlonzoEra c) Core.Tx where
type TranslationError (AlonzoEra c) Core.Tx = DecoderError
translateEra _ctx tx =
case decodeAnnotator "tx" fromCBOR (serialize tx) of
Right newTx -> pure newTx
Left decoderError -> throwError decoderError

instance Crypto c => TranslateEra (AlonzoEra c) ShelleyGenesis where
translateEra ctxt genesis =
return
Expand All @@ -114,16 +106,16 @@ instance Crypto c => TranslateEra (AlonzoEra c) ShelleyGenesis where
API.sgStaking = API.sgStaking genesis
}

newtype TxInBlock era = TxInBlock {unTxInBlock :: (Era.TxInBlock era)}
newtype Tx era = Tx {unTx :: Core.Tx era}

instance
( Crypto c,
Era.TxInBlock (AlonzoEra c) ~ ValidatedTx (AlonzoEra c)
Core.Tx (AlonzoEra c) ~ ValidatedTx (AlonzoEra c)
) =>
TranslateEra (AlonzoEra c) TxInBlock
TranslateEra (AlonzoEra c) Tx
where
type TranslationError (AlonzoEra c) TxInBlock = DecoderError
translateEra _ctxt (TxInBlock tx) = do
type TranslationError (AlonzoEra c) Tx = DecoderError
translateEra _ctxt (Tx tx) = do
-- Note that this does not preserve the hidden bytes field of the transaction.
-- This is under the premise that this is irrelevant for TxInBlocks, which are
-- not transmitted as contiguous chunks.
Expand All @@ -134,7 +126,7 @@ instance
SNothing -> pure SNothing
SJust axd -> SJust <$> translateViaCBORAnn "auxiliarydata" axd
let validating = IsValidating True
pure $ TxInBlock $ ValidatedTx bdy txwits validating aux
pure $ Tx $ ValidatedTx bdy txwits validating aux

--------------------------------------------------------------------------------
-- Auxiliary instances and functions
Expand Down
2 changes: 1 addition & 1 deletion alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs
Expand Up @@ -433,7 +433,7 @@ indexedRdmrs ::
HasField "inputs" (Core.TxBody era) (Set (TxIn (Crypto era))),
HasField "wdrls" (Core.TxBody era) (Wdrl (Crypto era)),
HasField "certs" (Core.TxBody era) (StrictSeq (DCert (Crypto era))),
HasField "wits" tx (TxWitness era), -- Generalized over tx, so tx can be Tx or TxInBlock
HasField "wits" tx (TxWitness era),
HasField "body" tx (Core.TxBody era)
) =>
tx ->
Expand Down
27 changes: 11 additions & 16 deletions cardano-ledger-core/src/Cardano/Ledger/Era.hs
Expand Up @@ -82,12 +82,12 @@ class
class
( Era era,
SafeToHash (Core.Script era),
HasField "body" (TxInBlock era) (Core.TxBody era)
HasField "body" (Core.Tx era) (Core.TxBody era)
) =>
ValidateScript era
where
scriptPrefixTag :: Core.Script era -> BS.ByteString
validateScript :: Core.Script era -> TxInBlock era -> Bool
validateScript :: Core.Script era -> Core.Tx era -> Bool
hashScript :: Core.Script era -> ScriptHash (Crypto era)
-- ONE SHOULD NOT OVERIDE THE hashScript DEFAULT METHOD
-- UNLESS YOU UNDERSTAND THE SafeToHash class, AND THE ROLE OF THE scriptPrefixTag
Expand Down Expand Up @@ -116,21 +116,16 @@ class
-- - A 'TxSeq`, which represents the decoded structure of a sequence of
-- transactions as represented in the encoded block; that is, with witnessing,
-- metadata and other non-body parts split separately.
-- - A 'TxInBlock', which represents a transaction as included in a block. In
-- general, we expect this to be the same as a normal 'Tx'. However, we know
-- that in future eras it will include extra data not present when the
-- transaction is first created.

-- | Indicates that an era supports segregated witnessing.
--
-- This class is embodies an isomorphism between 'TxSeq era' and 'StrictSeq
-- (TxInBlock era)', witnessed by 'fromTxSeq' and 'toTxSeq'.
-- (Tx era)', witnessed by 'fromTxSeq' and 'toTxSeq'.
class SupportsSegWit era where
type TxSeq era :: Type
type TxInBlock era :: Type

fromTxSeq :: TxSeq era -> StrictSeq (TxInBlock era)
toTxSeq :: StrictSeq (TxInBlock era) -> TxSeq era
fromTxSeq :: TxSeq era -> StrictSeq (Core.Tx era)
toTxSeq :: StrictSeq (Core.Tx era) -> TxSeq era

-- | Get the block body hash from the TxSeq. Note that this is not a regular
-- "hash the stored bytes" function since the block body hash forms a small
Expand Down Expand Up @@ -243,12 +238,12 @@ type WellFormed era =
HasField "txfee" (Core.TxBody era) Coin,
HasField "minted" (Core.TxBody era) (Set (ScriptHash (Crypto era))),
HasField "adHash" (Core.TxBody era) (StrictMaybe (AuxiliaryDataHash (Crypto era))),
-- TxInBlock
HasField "body" (TxInBlock era) (Core.TxBody era),
HasField "wits" (TxInBlock era) (Core.Witnesses era),
HasField "auxiliaryData" (TxInBlock era) (StrictMaybe (Core.AuxiliaryData era)),
HasField "txsize" (TxInBlock era) Integer,
HasField "scriptWits" (TxInBlock era) (Map (ScriptHash (Crypto era)) (Core.Script era)),
-- Tx
HasField "body" (Core.Tx era) (Core.TxBody era),
HasField "wits" (Core.Tx era) (Core.Witnesses era),
HasField "auxiliaryData" (Core.Tx era) (StrictMaybe (Core.AuxiliaryData era)),
HasField "txsize" (Core.Tx era) Integer,
HasField "scriptWits" (Core.Tx era) (Map (ScriptHash (Crypto era)) (Core.Script era)),
-- TxOut
HasField "value" (Core.TxOut era) (Core.Value era),
-- HashAnnotated
Expand Down
5 changes: 4 additions & 1 deletion shelley-ma/impl/src/Cardano/Ledger/ShelleyMA.hs
Expand Up @@ -130,6 +130,10 @@ instance CryptoClass.Crypto c => UsesPParams (ShelleyMAEra 'Allegra c) where

type instance Core.Value (ShelleyMAEra m c) = MAValue m c

type instance
Core.Tx (ShelleyMAEra (ma :: MaryOrAllegra) c) =
Tx (ShelleyMAEra ma c)

type instance
Core.TxOut (ShelleyMAEra (ma :: MaryOrAllegra) c) =
TxOut (ShelleyMAEra ma c)
Expand Down Expand Up @@ -185,7 +189,6 @@ instance
) =>
SupportsSegWit (ShelleyMAEra ma c)
where
type TxInBlock (ShelleyMAEra ma c) = Tx (ShelleyMAEra ma c)
type TxSeq (ShelleyMAEra ma c) = Shelley.TxSeq (ShelleyMAEra ma c)
fromTxSeq = Shelley.txSeqTxns
toTxSeq = Shelley.TxSeq
Expand Down
6 changes: 3 additions & 3 deletions shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/Rules/Utxow.hs
Expand Up @@ -12,7 +12,7 @@ module Cardano.Ledger.ShelleyMA.Rules.Utxow where
import Cardano.Ledger.Address (Addr)
import Cardano.Ledger.BaseTypes
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Era (Era (Crypto), TxInBlock)
import Cardano.Ledger.Era (Era (Crypto))
import Cardano.Ledger.ShelleyMA.Rules.Utxo (UTXO, UtxoPredicateFailure)
import Cardano.Ledger.ShelleyMA.TxBody ()
import Control.State.Transition.Extended
Expand Down Expand Up @@ -53,14 +53,14 @@ instance
Embed (Core.EraRule "UTXO" era) (UTXOW era),
Environment (Core.EraRule "UTXO" era) ~ UtxoEnv era,
State (Core.EraRule "UTXO" era) ~ UTxOState era,
Signal (Core.EraRule "UTXO" era) ~ TxInBlock era,
Signal (Core.EraRule "UTXO" era) ~ Core.Tx era,
-- Supply the HasField and Validate instances for Mary and Allegra (which match Shelley)
ShelleyStyleWitnessNeeds era
) =>
STS (UTXOW era)
where
type State (UTXOW era) = UTxOState era
type Signal (UTXOW era) = TxInBlock era
type Signal (UTXOW era) = Core.Tx era
type Environment (UTXOW era) = UtxoEnv era
type BaseM (UTXOW era) = ShelleyBase
type
Expand Down

0 comments on commit b75367f

Please sign in to comment.