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

Reduce overestimation of minimum UTxO values for Shelley-era addresses. #3416

Merged
merged 22 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
626204c
Miscellaneous formatting fixes.
jonathanknowles Jul 26, 2022
676f31d
Move `ProtocolMagic` type and functions to separate module.
jonathanknowles Jul 26, 2022
19c6d99
Move `dummyAddress` from `SelectionContext` to `SelectionConstraints`.
jonathanknowles Jul 21, 2022
521f06f
Rename `dummyAddress` to `maximumLengthChangeAddress`.
jonathanknowles Jul 29, 2022
b22d9c0
Add `BoundedAddressLength` type class and instances.
jonathanknowles Jul 26, 2022
2421a73
Add `BoundedAddressLength` constraint to functions in `Wallet`.
jonathanknowles Jul 26, 2022
cf15978
Reduce overestimation of minimum UTxO values for Shelley-era addresses.
jonathanknowles Jul 19, 2022
daeeb88
Replace `genCoinRange` with `chooseCoin` in `Migration.SelectionSpec`.
jonathanknowles Jul 29, 2022
7509c9c
Use a minimum-length address for the dummy output in module `Balance`.
jonathanknowles Jul 29, 2022
aa78b31
Raise `UnableToConstructChange.shortfall` values for `balanceTx` gold…
Anviking Jul 25, 2022
a049485
Raise cost of joining and quitting in `STAKE_POOLS_{JOIN,QUIT}_01x`.
jonathanknowles Jul 29, 2022
a813ed5
Use `errMsg403MinUTxOValue` instead of magic constants in integration…
jonathanknowles Aug 2, 2022
eab4ba9
Add `SelectionOutputCoinInsufficient` constructor to `SelectionOutput…
jonathanknowles Aug 1, 2022
c1f2c9a
Verify ada quantities of user-specified outputs in `CoinSelection.Int…
jonathanknowles Aug 1, 2022
7381e2f
Remove coverage check for `SelectionBalanceError.InsufficientMinCoinV…
jonathanknowles Aug 1, 2022
ac4ae3d
Verify values of type `SelectionOutputCoinInsufficientError`.
jonathanknowles Aug 1, 2022
87bc3fa
Adjust `TransactionSpec` to detect `SelectionOutputCoinInsufficientEr…
jonathanknowles Aug 1, 2022
4de24a4
Remove the now-unused `InsufficientMinCoinValues` error.
jonathanknowles Aug 1, 2022
10f52c9
Use a null address and minimal coin value within `performSelectionEmp…
jonathanknowles Aug 1, 2022
6a8d81d
Lower `UnableToConstructChange.shortfall` values for `balanceTx` gold…
jonathanknowles Aug 1, 2022
06b4fe0
Lower cost of joining and quitting in `STAKE_POOLS_{JOIN,QUIT}_01x`.
jonathanknowles Aug 1, 2022
cbc2c20
Use `SelectionContext` parameter consistently in `SelectionOutputError`.
jonathanknowles Aug 2, 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
17 changes: 14 additions & 3 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,20 @@ walletId =
-- | Min UTxO parameter for the test cluster.
minUTxOValue :: ApiEra -> Natural
minUTxOValue e
| e >= ApiBabbage = 1_107_670 -- needs to be overestimated for the sake of
-- long byron addresses
| e >= ApiAlonzo = 999_978 -- From 34482 lovelace per word
| e >= ApiBabbage = 995_610
-- This value is a slight overestimation for outputs with Shelley
-- addresses and no tokens.
--
-- However, it would be incorrect for outputs with Byron addresses,
-- where the lower bound would be greater by the following amount:
--
-- 4310 lovelace/byte * (86 - 57) byte ≈ 0.125 ada
--
-- However, this value appears to be fine for the purposes of
-- integration tests.
--
| e >= ApiAlonzo = 999_978
-- From 34482 lovelace/word.
| otherwise = 1_000_000

-- | Parameter in test cluster shelley genesis.
Expand Down
12 changes: 7 additions & 5 deletions lib/core-integration/src/Test/Integration/Framework/TestData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,13 @@ errMsg403InvalidConstructTx =
\Include at least one of them."

errMsg403MinUTxOValue :: String
errMsg403MinUTxOValue =
"Some outputs have ada values that are too small. There's a \
\minimum ada value specified by the protocol that each output must satisfy. \
\I'll handle that minimum value myself when you do not explicitly specify \
\an ada value for an output. Otherwise, you must specify enough ada."
errMsg403MinUTxOValue = unwords
[ "One of the outputs you've specified has an ada quantity that is"
, "below the minimum required. Either increase the ada quantity to"
, "at least the minimum, or specify an ada quantity of zero, in"
, "which case the wallet will automatically assign the correct"
, "minimum ada quantity to the output."
]

errMsg409WalletExists :: String -> String
errMsg409WalletExists walId = "This operation would yield a wallet with the following\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import Test.Integration.Framework.Request
( RequestException )
import Test.Integration.Framework.TestData
( errMsg400StartTimeLaterThanEndTime
, errMsg403MinUTxOValue
, errMsg404NoAsset
, errMsg404NoWallet
, steveToken
Expand Down Expand Up @@ -186,7 +187,7 @@ spec = describe "BYRON_TRANSACTIONS" $ do
rtx <- request @(ApiTransaction n) ctx
(Link.createTransactionOld @'Byron wSrc) Default payload
expectResponseCode HTTP.status403 rtx
expectErrorMessage "Some outputs have ada values that are too small." rtx
expectErrorMessage errMsg403MinUTxOValue rtx

describe "BYRON_TRANS_ASSETS_CREATE_02a - Multi-asset transaction with no ADA" $
forM_ [ (fixtureMultiAssetRandomWallet @n, "Byron wallet")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ import Test.Integration.Framework.DSL
, verify
)
import Test.Integration.Framework.TestData
( errMsg403Fee, errMsg403InvalidConstructTx, errMsg403NotEnoughMoney )
( errMsg403Fee
, errMsg403InvalidConstructTx
, errMsg403MinUTxOValue
, errMsg403NotEnoughMoney
)

import qualified Cardano.Wallet.Api.Link as Link
import qualified Network.HTTP.Types.Status as HTTP
Expand Down Expand Up @@ -324,7 +328,7 @@ spec = describe "NEW_BYRON_TRANSACTIONS" $ do
(Link.createUnsignedTransaction @'Byron wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage "Some outputs have ada values that are too small."
, expectErrorMessage errMsg403MinUTxOValue
]

describe "BYRON_TRANS_NEW_ASSETS_CREATE_01c - Multi-asset tx without Ada" $
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Cardano.Wallet.Api.Types
)
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Types.ProtocolMagic
( getProtocolMagic, mainnetMagic )
import Control.Monad
( when )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1433,8 +1433,8 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
costOfJoining :: Context -> Natural
costOfJoining ctx =
if _mainEra ctx >= ApiBabbage
then costOf (\coeff cst -> 458 * coeff + cst) ctx
else costOf (\coeff cst -> 454 * coeff + cst) ctx
then costOf (\coeff cst -> 454 * coeff + cst) ctx
else costOf (\coeff cst -> 450 * coeff + cst) ctx

costOfQuitting :: Context -> Natural
costOfQuitting ctx =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ spec = describe "SHELLEY_TRANSACTIONS" $ do
(Link.createTransactionOld @'Shelley wSrc) Default payload
-- It should fail with InsufficientMinCoinValueError
expectResponseCode HTTP.status403 rtx
expectErrorMessage "Some outputs have ada values that are too small." rtx
expectErrorMessage errMsg403MinUTxOValue rtx

it "TRANS_ASSETS_CREATE_02a - Multi-asset transaction without Ada" $ \ctx -> runResourceT $ do
wSrc <- fixtureMultiAssetWallet ctx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage "Some outputs have ada values that are too small."
, expectErrorMessage errMsg403MinUTxOValue
]

it "TRANS_NEW_ASSETS_CREATE_01c - Multi-asset tx without Ada" $ \ctx -> runResourceT $ do
Expand Down
2 changes: 2 additions & 0 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,12 @@ library
Cardano.Wallet.Primitive.Passphrase.Types
Cardano.Wallet.Primitive.Types
Cardano.Wallet.Primitive.Types.Address
Cardano.Wallet.Primitive.Types.Address.Constants
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.ProtocolMagic
Cardano.Wallet.Primitive.Types.Redeemer
Cardano.Wallet.Primitive.Types.RewardAccount
Cardano.Wallet.Primitive.Types.TokenBundle
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Byron/Codec/Cbor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..), DerivationType (..), Index (..) )
import Cardano.Wallet.Primitive.Passphrase
( Passphrase (..) )
import Cardano.Wallet.Primitive.Types
( ProtocolMagic (..) )
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.ProtocolMagic
( ProtocolMagic (..) )
import Cardano.Wallet.Primitive.Types.Tx
( TxIn (..), TxOut (..), unsafeCoinToTxOutCoinValue )
import Control.Monad
Expand Down
46 changes: 27 additions & 19 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ import Cardano.Wallet.Network
, NetworkLayer (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( DelegationAddress (..)
( BoundedAddressLength (..)
, DelegationAddress (..)
, Depth (..)
, DerivationIndex (..)
, DerivationPrefix (..)
Expand Down Expand Up @@ -421,8 +422,6 @@ import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount (..) )
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..) )
import Cardano.Wallet.Primitive.Types.TokenMap
( TokenMap )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenName (UnsafeTokenName), TokenPolicyId (UnsafeTokenPolicyId) )
import Cardano.Wallet.Primitive.Types.TokenQuantity
Expand Down Expand Up @@ -473,7 +472,7 @@ import Cardano.Wallet.Transaction
import Control.Applicative
( (<|>) )
import Control.Arrow
( left )
( first, left )
import Control.DeepSeq
( NFData )
import Control.Monad
Expand Down Expand Up @@ -546,7 +545,7 @@ import Data.Map.Strict
import Data.Maybe
( fromMaybe, isJust, mapMaybe )
import Data.Proxy
( Proxy )
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Set
Expand Down Expand Up @@ -922,26 +921,28 @@ getWalletUtxoSnapshot ctx wid = do
(wallet, _, pending) <- withExceptT id (readWallet @ctx @s @k ctx wid)
pp <- liftIO $ currentProtocolParameters nl
era <- liftIO $ currentNodeEra nl
let bundles = availableUTxO @s pending wallet
let txOuts = availableUTxO @s pending wallet
& unUTxO
& F.toList
& fmap (view #tokens)
pure $ pairBundleWithMinAdaQuantity era pp <$> bundles
pure $ first (view #tokens) . pairTxOutWithMinAdaQuantity era pp <$> txOuts
where
nl = ctx ^. networkLayer
tl = ctx ^. transactionLayer @k

pairBundleWithMinAdaQuantity
pairTxOutWithMinAdaQuantity
:: Cardano.AnyCardanoEra
-> ProtocolParameters
-> TokenBundle
-> (TokenBundle, Coin)
pairBundleWithMinAdaQuantity era pp bundle =
(bundle, computeMinAdaQuantity $ view #tokens bundle)
-> TxOut
-> (TxOut, Coin)
pairTxOutWithMinAdaQuantity era pp out =
(out, computeMinAdaQuantity out)
where
computeMinAdaQuantity :: TokenMap -> Coin
computeMinAdaQuantity =
view #txOutputMinimumAdaQuantity (constraints tl era pp)
computeMinAdaQuantity :: TxOut -> Coin
computeMinAdaQuantity (TxOut addr bundle) =
view #txOutputMinimumAdaQuantity
(constraints tl era pp)
(addr)
(view #tokens bundle)

-- | List the wallet's UTxO statistics.
listUtxoStatistics
Expand Down Expand Up @@ -1568,6 +1569,7 @@ balanceTransaction
, MonadRandom m
, HasLogger m WalletWorkerLog ctx
, Cardano.IsShelleyBasedEra era
, BoundedAddressLength k
)
=> ctx
-> ArgGenChange s
Expand All @@ -1591,6 +1593,7 @@ balanceTransactionWithSelectionStrategy
:: forall era m s k ctx.
( HasTransactionLayer k ctx
, GenChange s
, BoundedAddressLength k
, MonadRandom m
, HasLogger m WalletWorkerLog ctx
, Cardano.IsShelleyBasedEra era
Expand Down Expand Up @@ -1985,6 +1988,8 @@ balanceTransactionWithSelectionStrategy
intCast @Word16 @Int $ view #maximumCollateralInputCount pp
, minimumCollateralPercentage =
view #minimumCollateralPercentage pp
, maximumLengthChangeAddress =
maxLengthAddressFor $ Proxy @k
}

selectionParams = SelectionParams
Expand Down Expand Up @@ -2171,8 +2176,8 @@ calcMinimumCoinValues
calcMinimumCoinValues ctx era outs = do
pp <- currentProtocolParameters nl
pure
$ view #txOutputMinimumAdaQuantity (constraints tl era pp)
. view (#tokens . #tokens) <$> outs
$ uncurry (view #txOutputMinimumAdaQuantity (constraints tl era pp))
. (\o -> (view #address o, view (#tokens . #tokens) o)) <$> outs
where
nl = ctx ^. networkLayer
tl = ctx ^. transactionLayer @k
Expand Down Expand Up @@ -2213,7 +2218,8 @@ data SelectAssetsParams s result = SelectAssetsParams
--
selectAssets
:: forall ctx m s k result.
( HasTransactionLayer k ctx
( BoundedAddressLength k
, HasTransactionLayer k ctx
, HasLogger m WalletWorkerLog ctx
, MonadRandom m
)
Expand Down Expand Up @@ -2247,6 +2253,8 @@ selectAssets ctx era pp params transform = do
intCast @Word16 @Int $ view #maximumCollateralInputCount pp
, minimumCollateralPercentage =
view #minimumCollateralPercentage pp
, maximumLengthChangeAddress =
maxLengthAddressFor $ Proxy @k
}
let selectionParams = SelectionParams
{ assetsToMint =
Expand Down
Loading