Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cardano API function calculateMinimumUTxO for minimum UTxO calculations. #3368

Merged
merged 36 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ad11edb
Miscellaneous formatting fixes.
jonathanknowles Jun 30, 2022
3f3b549
Add dependency on library `int-cast`.
jonathanknowles Jul 4, 2022
76c2756
Add dependency on library `generics-sop`.
jonathanknowles Jul 4, 2022
45ba2f5
Add function `fromAllegraPParams`.
jonathanknowles Jun 30, 2022
55fad34
Add function `fromMaryPParams`.
jonathanknowles Jun 30, 2022
04af270
Specialize function `fromShelleyPParams` to the Shelley era.
jonathanknowles Jun 30, 2022
66f9086
Add type `MinimumUTxO` with generators and shrinkers.
jonathanknowles Jun 30, 2022
d6175a5
Add field `minimumUTxO` to `ProtocolParameters` primitive type.
jonathanknowles Jun 30, 2022
6f7de9b
Add function `computeMinimumCoinForUTxO`.
jonathanknowles Jun 30, 2022
d32b55a
Use `computeMinimumCoinForUTxO` within `TransactionLayer.constraints`.
jonathanknowles Jun 30, 2022
4935f26
Simplify minimum UTxO value lookup within `toApiNetworkParameters`.
jonathanknowles Jul 1, 2022
03d1c6c
Remove field `minimumUTxOvalue` from primitive type `ProtocolParamete…
jonathanknowles Jul 1, 2022
33f480f
Use type name `MinimumUTxOForShelleyBasedEra`.
jonathanknowles Jul 4, 2022
c9a8558
Extract out constants `maxLengthCoin` and `maxLengthAddress`.
jonathanknowles Jul 5, 2022
aad2053
Revise `computeMinimumCoinForUTxO` property to check upper and lower …
jonathanknowles Jul 5, 2022
527529b
Add function `genAddressAny` to `Cardano.Api.Gen`.
jonathanknowles Jul 5, 2022
a46f500
Revise `computeMinimumCoinForUTxO` property to test Byron and Shelley…
jonathanknowles Jul 5, 2022
ae5790f
Add explanatory comments to properties in `MinimumUTxOSpec`.
jonathanknowles Jul 5, 2022
d9c8cae
Use more robust generator for `TokenBundle` values.
jonathanknowles Jul 5, 2022
009d3e3
Use coins-per-word-based calculation for Blockfrost.
jonathanknowles Jul 5, 2022
c26ef6f
Extract out `getMinimumUTxOFunction` within Blockfrost network layer.
jonathanknowles Jul 6, 2022
c2a55f6
Generalize function `verify` in `Test.QuickCheck.Extra`.
jonathanknowles Jul 7, 2022
c4f01d9
Add property `prop_computeMinimumCoinForUTxO_shelleyBasedEra_stability`.
jonathanknowles Jul 7, 2022
6bd15b1
Simplify property `prop_computeMinimumCoinForUTxO_shelleyBasedEra_bou…
jonathanknowles Jul 7, 2022
4f348ca
Add golden tests for minimum UTxO values.
jonathanknowles Jul 8, 2022
f75c995
Include golden `TokenMap` values in generator for `TokenMap`.
jonathanknowles Jul 8, 2022
75015c2
Disable `PartialTypeSignatures` in `Shelley.Compatibility`.
jonathanknowles Jul 8, 2022
763a0d3
Remove legacy function `computeMinimumAdaQuantity` from `Compatibilit…
jonathanknowles Jul 8, 2022
fc2344e
Remove legacy type `MinimumUTxOValue` from `Primitive.Types`.
jonathanknowles Jul 8, 2022
964faa4
Improve clarity of counterexample output within golden tests.
jonathanknowles Jul 11, 2022
bda5651
Add further explanatory comments.
jonathanknowles Jul 12, 2022
a4d6519
Add commentary to `prop_computeMinimumCoinForUTxO_shelleyBasedEra_sta…
jonathanknowles Jul 12, 2022
114cf90
Improve realism of generated test protocol parameters in `MinimumUTxO…
jonathanknowles Jul 13, 2022
f08e0a0
Reuse test protocol parameters from `MinimumUTxO.Gen` in `MinimumUTxO…
jonathanknowles Jul 13, 2022
eb446c6
Adjust monotonicity comment for function `embedTokenMapWithinPaddedTx…
jonathanknowles Jul 13, 2022
727c7cb
Describe motivation for `MinimumUTxOConstant` constructor.
jonathanknowles Jul 13, 2022
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
11 changes: 8 additions & 3 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ library
, cardano-crypto-class
, cardano-crypto-test
, cardano-numeric
, cardano-ledger-core
, cardano-ledger-alonzo
, cardano-ledger-alonzo-test
, cardano-ledger-babbage
, cardano-ledger-byron-test
, cardano-ledger-shelley-test
, cardano-ledger-core
, cardano-ledger-shelley
, cardano-ledger-alonzo
, cardano-ledger-shelley-ma
, cardano-ledger-shelley-test
, cardano-slotting
, cborg
, containers
Expand Down Expand Up @@ -251,6 +254,8 @@ library
Cardano.Wallet.Primitive.Types.Address
Cardano.Wallet.Primitive.Types.Coin
Cardano.Wallet.Primitive.Types.Hash
Cardano.Wallet.Primitive.Types.MinimumUTxO
Cardano.Wallet.Primitive.Types.MinimumUTxO.Gen
Cardano.Wallet.Primitive.Types.Redeemer
Cardano.Wallet.Primitive.Types.RewardAccount
Cardano.Wallet.Primitive.Types.TokenBundle
Expand Down
11 changes: 10 additions & 1 deletion lib/core/src/Cardano/Api/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Api.Gen
( genAddressByron
( genAddressAny
, genAddressByron
, genAddressInEra
, genAddressShelley
, genAlphaNum
Expand Down Expand Up @@ -860,6 +861,14 @@ genPaymentCredential =
byScript :: Gen PaymentCredential
byScript = PaymentCredentialByScript <$> genScriptHash

genAddressAny :: Gen AddressAny
genAddressAny = oneof
[ AddressByron
<$> genAddressByron
, AddressShelley
<$> genAddressShelley
]

genAddressByron :: Gen (Address ByronAddr)
genAddressByron = makeByronAddress <$> genNetworkId
<*> genVerificationKey AsByronKey
Expand Down
8 changes: 2 additions & 6 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ import Cardano.Wallet.Primitive.Types
, EpochNo (..)
, ExecutionUnitPrices (..)
, GenesisParameters (..)
, MinimumUTxOValue (..)
, NetworkParameters (..)
, NonWalletCertificate (..)
, PoolId (..)
Expand Down Expand Up @@ -1129,11 +1128,8 @@ toApiNetworkParameters (NetworkParameters gp sp pp) txConstraints toEpochInfo =
$ getDecentralizationLevel
$ view #decentralizationLevel pp
, desiredPoolNumber = view #desiredNumberOfStakePools pp
, minimumUtxoValue = toApiCoin $ case (view #minimumUTxOvalue pp) of
MinimumUTxOValue c ->
c
MinimumUTxOValueCostPerWord _perWord ->
txOutputMinimumAdaQuantity txConstraints TokenMap.empty
, minimumUtxoValue = toApiCoin $
txOutputMinimumAdaQuantity txConstraints TokenMap.empty
, eras = apiEras
, maximumCollateralInputCount =
view #maximumCollateralInputCount pp
Expand Down
47 changes: 18 additions & 29 deletions lib/core/src/Cardano/Wallet/Primitive/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ module Cardano.Wallet.Primitive.Types
, GenesisParameters (..)
, SlottingParameters (..)
, ProtocolParameters (..)
, MinimumUTxOValue (..)
, TxParameters (..)
, TokenBundleMaxSize (..)
, EraInfo (..)
Expand Down Expand Up @@ -181,6 +180,8 @@ import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..), hashFromText )
import Cardano.Wallet.Primitive.Types.MinimumUTxO
( MinimumUTxO )
import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount (..) )
import Cardano.Wallet.Primitive.Types.Tx
Expand Down Expand Up @@ -1081,23 +1082,6 @@ instance Buildable (EraInfo EpochNo) where
boundF (Just e) = " from " <> build e
boundF Nothing = " <not started>"

data MinimumUTxOValue
-- | In Shelley, tx outputs could only be created if they were larger than
-- this `MinimumUTxOValue`.
= MinimumUTxOValue Coin

-- | With Alonzo, `MinimumUTxOValue` is replaced by an ada-cost per word of
-- the output. Note that the alonzo ledger assumes fixed sizes for address
-- and coin, so the size is not the serialized size exactly.
| MinimumUTxOValueCostPerWord Coin
deriving (Eq, Generic, Show)

instance NFData MinimumUTxOValue

instance Buildable MinimumUTxOValue where
build (MinimumUTxOValue c) = "constant " <> build c
build (MinimumUTxOValueCostPerWord c) = build c <> " per word"

-- | Protocol parameters that can be changed through the update system.
--
data ProtocolParameters = ProtocolParameters
Expand All @@ -1111,9 +1095,9 @@ data ProtocolParameters = ProtocolParameters
:: Word16
-- ^ The current desired number of stakepools in the network.
-- Also known as k parameter.
, minimumUTxOvalue
:: MinimumUTxOValue
-- ^ The minimum UTxO value.
, minimumUTxO
:: MinimumUTxO
-- ^ Represents a way of calculating minimum UTxO values.
, stakeKeyDeposit
:: Coin
-- ^ Registering a stake key requires storage on the node and as such
Expand Down Expand Up @@ -1151,7 +1135,7 @@ instance NFData ProtocolParameters where
[ rnf decentralizationLevel
, rnf txParameters
, rnf desiredNumberOfStakePools
, rnf minimumUTxOvalue
, rnf minimumUTxO
, rnf stakeKeyDeposit
, rnf eras
, rnf maximumCollateralInputCount
Expand All @@ -1162,13 +1146,18 @@ instance NFData ProtocolParameters where

instance Buildable ProtocolParameters where
build pp = blockListF' "" id
[ "Decentralization level: " <> build (pp ^. #decentralizationLevel)
, "Transaction parameters: " <> build (pp ^. #txParameters)
, "Desired number of pools: " <> build (pp ^. #desiredNumberOfStakePools)
, "Minimum UTxO value: " <> build (pp ^. #minimumUTxOvalue)
, "Eras:\n" <> indentF 2 (build (pp ^. #eras))
, "Execution unit prices: " <>
maybe "not specified" build (pp ^. #executionUnitPrices)
[ "Decentralization level: "
<> build (pp ^. #decentralizationLevel)
, "Transaction parameters: "
<> build (pp ^. #txParameters)
, "Desired number of pools: "
<> build (pp ^. #desiredNumberOfStakePools)
, "Minimum UTxO: "
<> build (pp ^. #minimumUTxO)
, "Eras:\n"
<> indentF 2 (build (pp ^. #eras))
, "Execution unit prices: "
<> maybe "not specified" build (pp ^. #executionUnitPrices)
]

data ExecutionUnits = ExecutionUnits
Expand Down
144 changes: 144 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/Types/MinimumUTxO.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

-- |
-- Copyright: © 2022 IOHK
-- License: Apache-2.0
--
-- Defines the 'MinimumUTxO' type and related functions.
--
module Cardano.Wallet.Primitive.Types.MinimumUTxO
(
-- * Types
MinimumUTxO (..)
, MinimumUTxOForShelleyBasedEra (..)

-- * Constructor functions
, minimumUTxONone
, minimumUTxOConstant
, minimumUTxOForShelleyBasedEra
)
where

import Prelude

import Cardano.Api.Shelley
( ShelleyBasedEra, ShelleyLedgerEra, fromLedgerPParams )
import Cardano.Ledger.Core
( PParams )
import Cardano.Wallet.Primitive.Types.Coin
( Coin )
import Control.DeepSeq
( NFData (..) )
import Data.Function
( on )
import Fmt
( Buildable (..), blockListF )

--------------------------------------------------------------------------------
-- The 'MinimumUTxO' type
--------------------------------------------------------------------------------

-- | Represents a function for computing minimum UTxO values.
--
data MinimumUTxO where
MinimumUTxONone
:: MinimumUTxO
-- ^ Indicates that there is no minimum UTxO value.
MinimumUTxOConstant
:: Coin
-> MinimumUTxO
-- ^ Indicates a constant minimum UTxO value. This constructor is
-- useful for writing tests, where we often want to have precise
-- control over the value that is chosen.
MinimumUTxOForShelleyBasedEraOf
:: MinimumUTxOForShelleyBasedEra
-> MinimumUTxO
-- ^ Indicates a Shelley-based era-specific minimum UTxO function.

instance Buildable MinimumUTxO where
build = \case
MinimumUTxONone ->
"MinimumUTxONone"
MinimumUTxOConstant c -> blockListF
[ "MinimumUTxOConstant"
, build c
]
MinimumUTxOForShelleyBasedEraOf m -> blockListF
[ "MinimumUTxOForShelleyBasedEra"
, build m
]

instance Eq MinimumUTxO where
(==) = (==) `on` show

instance NFData MinimumUTxO where
rnf = \case
MinimumUTxONone ->
rnf ()
MinimumUTxOConstant c ->
rnf c
MinimumUTxOForShelleyBasedEraOf pp ->
rnf pp

instance Show MinimumUTxO where
show = \case
MinimumUTxONone ->
"MinimumUTxONone"
MinimumUTxOConstant c -> unwords
[ "MinimumUTxOConstant"
, show c
]
MinimumUTxOForShelleyBasedEraOf pp -> unwords
[ "MinimumUTxOForShelleyBasedEra"
, show pp
]

--------------------------------------------------------------------------------
-- The 'MinimumUTxOForShelleyBasedEra' type
--------------------------------------------------------------------------------

-- | Represents a minimum UTxO function that is specific to a Shelley-based era.
--
data MinimumUTxOForShelleyBasedEra where
MinimumUTxOForShelleyBasedEra
:: ShelleyBasedEra era
-> PParams (ShelleyLedgerEra era)
-> MinimumUTxOForShelleyBasedEra

instance Buildable MinimumUTxOForShelleyBasedEra where
build (MinimumUTxOForShelleyBasedEra era _) = blockListF
[ "MinimumUTxOForShelleyBasedEra"
, show era
]

instance Eq MinimumUTxOForShelleyBasedEra where
(==) = (==) `on` show

instance NFData MinimumUTxOForShelleyBasedEra where
rnf (MinimumUTxOForShelleyBasedEra !_ !_) = rnf ()

instance Show MinimumUTxOForShelleyBasedEra where
show (MinimumUTxOForShelleyBasedEra era pp) = unwords
[ show era
, show (fromLedgerPParams era pp)
]

--------------------------------------------------------------------------------
-- Constructor functions
--------------------------------------------------------------------------------

minimumUTxONone :: MinimumUTxO
minimumUTxONone = MinimumUTxONone

minimumUTxOConstant :: Coin -> MinimumUTxO
minimumUTxOConstant = MinimumUTxOConstant

minimumUTxOForShelleyBasedEra
:: ShelleyBasedEra era
-> PParams (ShelleyLedgerEra era)
-> MinimumUTxO
minimumUTxOForShelleyBasedEra era pp =
MinimumUTxOForShelleyBasedEraOf $
MinimumUTxOForShelleyBasedEra era pp
Loading