Skip to content

Commit

Permalink
Merge branch 'master' into sjoerdvisscher/master
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpj committed Jun 14, 2021
2 parents a883820 + c53196f commit 1c796d3
Show file tree
Hide file tree
Showing 20 changed files with 670 additions and 83 deletions.
6 changes: 3 additions & 3 deletions alonzo/formal-spec/protocol-parameters.tex
Expand Up @@ -80,7 +80,7 @@ \subsection{Script Evaluation Cost Model and Prices}
reflects the real-world costs in terms of energy usage, hardware resources etc.

In Alonzo, the protocol parameter $\var{minUTxOValue}$ is deprecated, and replaced by
$\var{adaPerUTxOWord}$. This specifies directly the deposit required for storing
$\var{coinsPerUTxOWord}$. This specifies directly the deposit required for storing
bytes of data on the ledger in the form of UTxO entries.

\textbf{Limiting Script Execution Costs.}
Expand Down Expand Up @@ -162,8 +162,8 @@ \subsection{Script Evaluation Cost Model and Prices}
% & \text{Max. total block script exec. resources}\\
\var{maxValSize} \mapsto \N & \PParams \\
% & \text{Max size a value can be}\\
\var{adaPerUTxOWord} \mapsto \Coin & \PParams \\
% & \text{Min. ada per word (8 bytes) a UTxO entry must contain}
\var{coinsPerUTxOWord} \mapsto \Coin & \PParams \\
% & \text{Min. lovelace per word (8 bytes) a UTxO entry must contain}
\var{collateralPercent} \mapsto \N & \PParams \\
% & \text{Percentage of Tx fee which collateral must cover}
\var{maxCollInputs} \mapsto \N & \PParams \\
Expand Down
4 changes: 2 additions & 2 deletions alonzo/formal-spec/utxo.tex
Expand Up @@ -561,7 +561,7 @@ \subsection{The UTXOS transition system}
Note that we do not need to check that the start slot can be converted to
time, because all pasts slots can be converted into time correctly.

\item $\fun{adaPerUTxOWord}$ is now a protocol parameter explicitly, the
\item $\fun{coinsPerUTxOWord}$ is now a protocol parameter explicitly, the
$\fun{utxoEntrySize}$ calculation is defined differently than for ShelleyMA
(see Section~\ref{sec:value-size})

Expand Down Expand Up @@ -601,7 +601,7 @@ \subsection{The UTXOS transition system}
\\~\\
\mathsf{adaID}\notin \supp {\fun{mint}~tx} \\~\\
\forall txout \in \txouts{txb}, \\
\fun{getValue}~txout \geq \fun{inject}~(\hldiff{\fun{utxoEntrySize}~{txout} * \fun{adaPerUTxOWord}~pp)} \\~
\fun{getValue}~txout \geq \fun{inject}~(\hldiff{\fun{utxoEntrySize}~{txout} * \fun{coinsPerUTxOWord}~pp)} \\~
\\
\forall txout \in \txouts{txb},\\
\hldiff{\fun{serSize}~(\fun{getValue}~txout) ~\leq ~ (\fun{maxTxSize}~pp) * (\fun{maxValSize}~pp)} \\~
Expand Down
17 changes: 12 additions & 5 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Genesis.hs
Expand Up @@ -5,6 +5,9 @@
module Cardano.Ledger.Alonzo.Genesis
( AlonzoGenesis (..),
extendPPWithGenesis,

-- * Deprecated
adaPerUTxOWord,
)
where

Expand All @@ -22,7 +25,7 @@ import Numeric.Natural
import qualified Shelley.Spec.Ledger.PParams as Shelley

data AlonzoGenesis = AlonzoGenesis
{ adaPerUTxOWord :: Coin,
{ coinsPerUTxOWord :: Coin,
costmdls :: Map Language CostModel,
prices :: Prices,
maxTxExUnits :: ExUnits,
Expand All @@ -33,6 +36,10 @@ data AlonzoGenesis = AlonzoGenesis
}
deriving (Eq, Generic, NoThunks)

{-# DEPRECATED adaPerUTxOWord "Use coinsPerUTxOWord instead" #-}
adaPerUTxOWord :: AlonzoGenesis -> Coin
adaPerUTxOWord = coinsPerUTxOWord

-- | Given the missing pieces turn a Shelley.PParams' into an Params'
extendPPWithGenesis ::
Shelley.PParams' Identity era1 ->
Expand All @@ -41,7 +48,7 @@ extendPPWithGenesis ::
extendPPWithGenesis
pp
AlonzoGenesis
{ adaPerUTxOWord,
{ coinsPerUTxOWord,
costmdls,
prices,
maxTxExUnits,
Expand All @@ -52,7 +59,7 @@ extendPPWithGenesis
} =
extendPP
pp
adaPerUTxOWord
coinsPerUTxOWord
costmdls
prices
maxTxExUnits
Expand Down Expand Up @@ -81,7 +88,7 @@ instance FromCBOR AlonzoGenesis where
instance ToCBOR AlonzoGenesis where
toCBOR
AlonzoGenesis
{ adaPerUTxOWord,
{ coinsPerUTxOWord,
costmdls,
prices,
maxTxExUnits,
Expand All @@ -92,7 +99,7 @@ instance ToCBOR AlonzoGenesis where
} =
encode $
Rec AlonzoGenesis
!> To adaPerUTxOWord
!> To coinsPerUTxOWord
!> To costmdls
!> To prices
!> To maxTxExUnits
Expand Down
22 changes: 11 additions & 11 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/PParams.hs
Expand Up @@ -156,8 +156,8 @@ data PParams' f era = PParams
_minPoolCost :: !(HKD f Coin),
-- new/updated for alonzo

-- | Cost in ada per byte of UTxO storage (instead of _minUTxOValue)
_adaPerUTxOWord :: !(HKD f Coin),
-- | Cost in lovelace per word (8 bytes) of UTxO storage (instead of _minUTxOValue)
_coinsPerUTxOWord :: !(HKD f Coin),
-- | Cost models for non-native script languages
_costmdls :: !(HKD f (Map Language CostModel)),
-- | Prices of execution units (for non-native script languages)
Expand Down Expand Up @@ -206,7 +206,7 @@ instance (Era era) => ToCBOR (PParams era) where
_protocolVersion = protocolVersion',
_minPoolCost = minPoolCost',
-- new/updated for alonzo
_adaPerUTxOWord = adaPerUTxOWord',
_coinsPerUTxOWord = coinsPerUTxOWord',
_costmdls = costmdls',
_prices = prices',
_maxTxExUnits = maxTxExUnits',
Expand Down Expand Up @@ -234,7 +234,7 @@ instance (Era era) => ToCBOR (PParams era) where
!> E toCBORGroup protocolVersion'
!> To minPoolCost'
-- new/updated for alonzo
!> To adaPerUTxOWord'
!> To coinsPerUTxOWord'
!> mapEncode costmdls'
!> To prices'
!> To maxTxExUnits'
Expand Down Expand Up @@ -268,7 +268,7 @@ instance
<! (D fromCBORGroup) -- _protocolVersion :: ProtVer
<! From -- _minPoolCost :: Natural
-- new/updated for alonzo
<! From -- _adaPerUTxOWord :: Coin
<! From -- _coinsPerUTxOWord :: Coin
<! (D mapFromCBOR) -- _costmdls :: (Map Language CostModel)
<! From -- _prices = prices',
<! From -- _maxTxExUnits = maxTxExUnits',
Expand Down Expand Up @@ -298,7 +298,7 @@ emptyPParams =
_protocolVersion = ProtVer 0 0,
_minPoolCost = mempty,
-- new/updated for alonzo
_adaPerUTxOWord = Coin 0,
_coinsPerUTxOWord = Coin 0,
_costmdls = mempty,
_prices = Prices (Coin 0) (Coin 0),
_maxTxExUnits = ExUnits 0 0,
Expand Down Expand Up @@ -328,7 +328,7 @@ instance Ord (PParams' StrictMaybe era) where
<== (_extraEntropy x, _extraEntropy y)
<== (_protocolVersion x, _protocolVersion y)
<== (_minPoolCost x, _minPoolCost y)
<== (_adaPerUTxOWord x, _adaPerUTxOWord y)
<== (_coinsPerUTxOWord x, _coinsPerUTxOWord y)
<== (_costmdls x, _costmdls y)
<== (_prices x, _prices y)
<== ( case compareEx (_maxTxExUnits x) (_maxTxExUnits y) of
Expand Down Expand Up @@ -388,7 +388,7 @@ encodePParamsUpdate ppup =
!> omitStrictMaybe 13 (_extraEntropy ppup) toCBOR
!> omitStrictMaybe 14 (_protocolVersion ppup) toCBOR
!> omitStrictMaybe 16 (_minPoolCost ppup) toCBOR
!> omitStrictMaybe 17 (_adaPerUTxOWord ppup) toCBOR
!> omitStrictMaybe 17 (_coinsPerUTxOWord ppup) toCBOR
!> omitStrictMaybe 18 (_costmdls ppup) mapToCBOR
!> omitStrictMaybe 19 (_prices ppup) toCBOR
!> omitStrictMaybe 20 (_maxTxExUnits ppup) toCBOR
Expand Down Expand Up @@ -432,7 +432,7 @@ emptyPParamsUpdate =
_protocolVersion = SNothing,
_minPoolCost = SNothing,
-- new/updated for alonzo
_adaPerUTxOWord = SNothing,
_coinsPerUTxOWord = SNothing,
_costmdls = SNothing,
_prices = SNothing,
_maxTxExUnits = SNothing,
Expand All @@ -459,7 +459,7 @@ updateField 12 = field (\x up -> up {_d = SJust x}) From
updateField 13 = field (\x up -> up {_extraEntropy = SJust x}) From
updateField 14 = field (\x up -> up {_protocolVersion = SJust x}) From
updateField 16 = field (\x up -> up {_minPoolCost = SJust x}) From
updateField 17 = field (\x up -> up {_adaPerUTxOWord = SJust x}) From
updateField 17 = field (\x up -> up {_coinsPerUTxOWord = SJust x}) From
updateField 18 = field (\x up -> up {_costmdls = x}) (D $ SJust <$> mapFromCBOR)
updateField 19 = field (\x up -> up {_prices = SJust x}) From
updateField 20 = field (\x up -> up {_maxTxExUnits = SJust x}) From
Expand Down Expand Up @@ -497,7 +497,7 @@ updatePParams pp ppup =
_protocolVersion = fromMaybe' (_protocolVersion pp) (_protocolVersion ppup),
_minPoolCost = fromMaybe' (_minPoolCost pp) (_minPoolCost ppup),
-- new/updated for alonzo
_adaPerUTxOWord = fromMaybe' (_adaPerUTxOWord pp) (_adaPerUTxOWord ppup),
_coinsPerUTxOWord = fromMaybe' (_coinsPerUTxOWord pp) (_coinsPerUTxOWord ppup),
_costmdls = fromMaybe' (_costmdls pp) (_costmdls ppup),
_prices = fromMaybe' (_prices pp) (_prices ppup),
_maxTxExUnits = fromMaybe' (_maxTxExUnits pp) (_maxTxExUnits ppup),
Expand Down
11 changes: 5 additions & 6 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Utxo.hs
Expand Up @@ -319,7 +319,7 @@ utxoTransition ::
HasField "_maxTxSize" (Core.PParams era) Natural,
HasField "_prices" (Core.PParams era) Prices,
HasField "_maxTxExUnits" (Core.PParams era) ExUnits,
HasField "_adaPerUTxOWord" (Core.PParams era) Coin,
HasField "_coinsPerUTxOWord" (Core.PParams era) Coin,
HasField "_maxValSize" (Core.PParams era) Natural,
HasField "_collateralPercentage" (Core.PParams era) Natural,
HasField "_maxCollateralInputs" (Core.PParams era) Natural,
Expand Down Expand Up @@ -378,8 +378,8 @@ utxoTransition = do
-- Here in the implementation, we store the adaId policyID in the coin field of the value.
Val.coin (getField @"mint" txb) == Val.zero ?!# TriesToForgeADA

{- ∀ txout ∈ txouts txb, getValuetxout ≥ inject(uxoEntrySizetxout ∗ adaPerUTxOWordp p) -}
let (Coin adaPerUTxOWord) = getField @"_adaPerUTxOWord" pp
{- ∀ txout ∈ txouts txb, getValuetxout ≥ inject(uxoEntrySizetxout ∗ coinsPerUTxOWord p) -}
let (Coin coinsPerUTxOWord) = getField @"_coinsPerUTxOWord" pp
outputs = Map.elems $ unUTxO (txouts @era txb)
outputsTooSmall =
filter
Expand All @@ -389,7 +389,7 @@ utxoTransition = do
Val.pointwise -- pointwise is used because non-ada amounts must be >= 0 too
(>=)
v
(Val.inject $ Coin (utxoEntrySize out * adaPerUTxOWord))
(Val.inject $ Coin (utxoEntrySize out * coinsPerUTxOWord))
)
outputs
null outputsTooSmall ?! OutputTooSmallUTxO outputsTooSmall
Expand Down Expand Up @@ -478,11 +478,10 @@ instance
HasField "_minfeeB" (Core.PParams era) Natural,
HasField "_keyDeposit" (Core.PParams era) Coin,
HasField "_poolDeposit" (Core.PParams era) Coin,
HasField "_adaPerUTxOWord" (Core.PParams era) Coin,
HasField "_maxTxSize" (Core.PParams era) Natural,
HasField "_prices" (Core.PParams era) Prices,
HasField "_maxTxExUnits" (Core.PParams era) ExUnits,
HasField "_adaPerUTxOWord" (Core.PParams era) Coin,
HasField "_coinsPerUTxOWord" (Core.PParams era) Coin,
HasField "_maxValSize" (Core.PParams era) Natural,
HasField "_collateralPercentage" (Core.PParams era) Natural,
HasField "_maxCollateralInputs" (Core.PParams era) Natural,
Expand Down
2 changes: 1 addition & 1 deletion alonzo/impl/src/Cardano/Ledger/Alonzo/Scripts.hs
Expand Up @@ -98,7 +98,7 @@ instance NoThunks Tag
-- | Scripts in the Alonzo Era, Either a Timelock script or a Plutus script.
data Script era
= TimelockScript (Timelock (Crypto era))
| PlutusScript (ShortByteString) -- A Plutus.V1.Ledger.Scripts(Script) that has been 'Flat'ened
| PlutusScript (ShortByteString) -- A Plutus.V1.Ledger.Scripts(Script) that has been 'CBOR' encoded
deriving (Eq, Generic, Ord)

instance (ValidateScript era, Core.Script era ~ Script era) => Show (Script era) where
Expand Down
5 changes: 1 addition & 4 deletions alonzo/test/cardano-ledger-alonzo-test.cabal
Expand Up @@ -40,6 +40,7 @@ library
Test.Cardano.Ledger.Alonzo.Serialisation.Generators
Test.Cardano.Ledger.Alonzo.AlonzoEraGen
Test.Cardano.Ledger.Alonzo.Trace
Test.Cardano.Ledger.Alonzo.PlutusScripts
build-depends:
bytestring,
cardano-binary,
Expand All @@ -49,10 +50,8 @@ library
cardano-ledger-shelley-ma,
cardano-slotting,
containers,
flat,
hashable,
plutus-tx,
plutus-tx-plugin,
plutus-ledger-api,
QuickCheck,
shelley-spec-ledger-test,
Expand Down Expand Up @@ -89,10 +88,8 @@ test-suite cardano-ledger-alonzo-test
cardano-slotting,
containers,
data-default-class,
flat,
plutus-core,
plutus-tx,
plutus-tx-plugin,
plutus-ledger-api,
QuickCheck,
small-steps,
Expand Down
45 changes: 34 additions & 11 deletions alonzo/test/lib/Test/Cardano/Ledger/Alonzo/AlonzoEraGen.hs
Expand Up @@ -49,7 +49,6 @@ import Cardano.Ledger.Val (adaOnly, (<+>), (<×>))
import Cardano.Slotting.Slot (SlotNo (..))
import Control.Iterate.SetAlgebra (eval, (◁))
import Control.Monad (replicateM)
import Data.ByteString.Short (ShortByteString, toShort)
import Data.Hashable (Hashable (..))
import qualified Data.List as List
import Data.Map as Map
Expand All @@ -59,19 +58,17 @@ import Data.Sequence.Strict (StrictSeq ((:|>)))
import qualified Data.Sequence.Strict as Seq (fromList)
import Data.Set as Set
import Data.Word (Word64)
import Flat (flat)
import GHC.Records (HasField (..))
import Plutus.V1.Ledger.Api (defaultCostModelParams)
import qualified Plutus.V1.Ledger.Scripts as P
import qualified PlutusTx as P (Data (..), compile)
import qualified PlutusTx as P (Data (..))
import qualified PlutusTx as Plutus
import qualified PlutusTx.Prelude as P
import Shelley.Spec.Ledger.Address (Addr (..))
import Shelley.Spec.Ledger.Credential (Credential (..))
import Shelley.Spec.Ledger.PParams (Update)
import Shelley.Spec.Ledger.TxBody (DCert, TxIn, Wdrl)
import Shelley.Spec.Ledger.UTxO (UTxO (..))
import Test.Cardano.Ledger.AllegraEraGen (genValidityInterval)
import Test.Cardano.Ledger.Alonzo.PlutusScripts (evendata3, guessTheNumber3, odddata3)
import Test.Cardano.Ledger.MaryEraGen (addTokens, genMint, maryGenesisValue, policyIndex)
import Test.QuickCheck hiding ((><))
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock)
Expand Down Expand Up @@ -107,8 +104,9 @@ phase2scripts =
TwoPhaseInfo (alwaysSucceeds 3) (hashScript @(AlonzoEra c) (alwaysSucceeds 3)) (P.I 1) (P.I 1, bigMem, bigStep),
TwoPhaseInfo (alwaysSucceeds 3) (hashScript @(AlonzoEra c) (alwaysSucceeds 3)) (P.I 1) (P.I 1, bigMem, bigStep),
TwoPhaseInfo (alwaysSucceeds 3) (hashScript @(AlonzoEra c) (alwaysSucceeds 3)) (P.I 1) (P.I 1, bigMem, bigStep),
TwoPhaseInfo guess (hashScript @(AlonzoEra c) guess) (P.I 9) (P.I 9, bigMem, bigStep),
TwoPhaseInfo guess (hashScript @(AlonzoEra c) guess) (P.I 9) (P.I 9, bigMem, bigStep)
TwoPhaseInfo guessTheNumber3 (hashScript @(AlonzoEra c) guessTheNumber3) (P.I 9) (P.I 9, bigMem, bigStep),
TwoPhaseInfo evendata3 (hashScript @(AlonzoEra c) evendata3) (P.I 8) (P.I 8, bigMem, bigStep),
TwoPhaseInfo odddata3 (hashScript @(AlonzoEra c) odddata3) (P.I 9) (P.I 9, bigMem, bigStep)
]

-- ================================================================
Expand Down Expand Up @@ -367,7 +365,7 @@ getDataMap scriptinfo scrips = Map.foldlWithKey' accum Map.empty scrips
Just (TwoPhaseInfo _script _hash dat _redeem) -> Map.insert (hashData @era dat) (Data dat) ans

instance Mock c => MinGenTxout (AlonzoEra c) where
calcEraMinUTxO tout pp = (utxoEntrySize tout <×> getField @"_adaPerUTxOWord" pp)
calcEraMinUTxO tout pp = (utxoEntrySize tout <×> getField @"_coinsPerUTxOWord" pp)
addValToTxOut v (TxOut a u _b) = TxOut a (v <+> u) (dataFromAddr a) -- _b
genEraTxOut genv genVal addrs = do
values <- (replicateM (length addrs) genVal)
Expand Down Expand Up @@ -426,7 +424,7 @@ guessTheNumber3args' d1 d2 _d3 = if d1 P.== d2 then () else (P.error ())
guessTheNumber3args :: ShortByteString
guessTheNumber3args =
toShort . flat . P.fromCompiledCode $
toShort . toStrict . serialise . P.fromCompiledCode $
$$(P.compile [||guessTheNumber3args'||])
-}

Expand All @@ -436,12 +434,37 @@ guessTheNumber3args = read "\SOH\NUL\NUL2\NUL2\NUL2\NUL32\NUL \STX\NUL3 \STX\NUL
-}

guess :: Alonzo.Script era
guess = Alonzo.PlutusScript guessTheNumber3
guess = guessTheNumber3

{-
guessTheNumber'3 :: P.Data -> P.Data -> P.Data -> ()
guessTheNumber'3 d1 d2 _d3 = if d1 P.== d2 then () else (P.error ())
guessTheNumber3 :: ShortByteString
guessTheNumber3 =
toShort . flat . P.fromCompiledCode $
toShort . toStrict . serialise . P.fromCompiledCode $
$$(P.compile [||guessTheNumber'3||])
isEven3 :: [Word8]
isEven3 =
concat
[ [1, 0, 0, 51, 50, 0, 32, 2, 0, 50, 0, 50, 0, 51, 32],
[2, 0, 51, 50, 0, 32, 2, 0, 51, 51, 51, 32, 2, 0, 32],
[2, 0, 32, 2, 0, 51, 32, 2, 0, 50, 0, 50, 0, 0, 18],
[0, 32, 2, 0, 51, 51, 51, 83, 0, 112, 3, 32, 2, 0, 98],
[0, 32, 2, 0, 98, 0, 32, 3, 51, 83, 1, 99, 51, 80, 20],
[1, 83, 55, 144, 1, 36, 0, 137, 0, 1, 0, 0, 73, 0, 26],
[128, 56, 4, 128, 65, 0, 16, 3, 16, 1, 0, 48, 3, 9, 0],
[48, 144, 0, 0, 144, 0, 0, 144, 0, 144, 1, 0, 16, 1, 0],
[16, 1, 128, 40, 3, 16, 1, 0, 9, 0, 16, 1, 0, 16, 1],
[0, 25, 128, 32, 3, 128, 49, 0, 9, 0, 16, 1, 0, 16, 1],
[0, 24, 1, 128, 49, 0, 9, 0, 16, 1, 0, 16, 1, 0, 24],
[1, 0, 49, 0, 9, 0, 16, 1, 0, 16, 1, 0, 24, 0, 128],
[49, 0, 0, 8, 137, 0, 16, 0, 1, 9, 0, 16, 0, 144, 1],
[0, 25, 128, 8, 2, 0, 24, 144, 0, 0, 136, 144, 1, 0, 9],
[0, 25, 128, 8, 1, 128, 16, 137, 0, 0, 8, 144, 0, 0, 144],
[1, 0, 25, 0, 25, 154, 189, 64, 4, 1, 128, 20, 205, 210, 0],
[64, 2, 36, 0, 64, 0, 4, 36, 0, 64, 0, 2, 64, 0, 3]
]
-}

0 comments on commit 1c796d3

Please sign in to comment.