Skip to content

Commit

Permalink
Rewrite tokenBundleSizeAssessor with ledger types (#4098)
Browse files Browse the repository at this point in the history
This removes the dependency on
`Cardano.Wallet.Shelley.Compatibility.toCardanoValue`

### Issue Number

ADP-3081

<!-- Reference the Jira/GitHub issue that this PR relates to, and which
requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->
  • Loading branch information
Anviking authored Sep 14, 2023
2 parents 49393a6 + 270b6ad commit ae6e90b
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 127 deletions.
3 changes: 1 addition & 2 deletions lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ library
Cardano.Wallet.Write.Tx
Cardano.Wallet.Write.Tx.Balance
Cardano.Wallet.Write.Tx.Balance.TokenBundleSize
Cardano.Wallet.Write.Tx.Balance.TokenBundleSize.Gen
Cardano.Wallet.Write.Tx.Gen
Cardano.Wallet.Write.Tx.Redeemers
Cardano.Wallet.Write.Tx.Sign
Expand Down Expand Up @@ -932,8 +931,8 @@ test-suite unit
Cardano.Wallet.Submissions.OperationsSpec
Cardano.Wallet.Submissions.PrimitivesSpec
Cardano.Wallet.TokenMetadataSpec
Cardano.Wallet.Write.Tx.Balance.TokenBundleSizeSpec
Cardano.Wallet.Write.TxSpec
Cardano.Wallet.Write.Tx.Balance.TokenBundleSizeSpec
Cardano.WalletSpec
Control.Concurrent.ConciergeSpec
Control.Monad.UtilSpec
Expand Down
4 changes: 3 additions & 1 deletion lib/wallet/src/Cardano/Wallet/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ module Cardano.Wallet.Write.Tx
, feeOfBytes
, maxScriptExecutionCost
, stakeKeyDeposit
, ProtVer (..)
, Version

-- * Tx
, Core.Tx
Expand Down Expand Up @@ -169,7 +171,7 @@ import Cardano.Ledger.Api.UTxO
import Cardano.Ledger.Babbage.TxBody
( BabbageTxOut (..) )
import Cardano.Ledger.BaseTypes
( maybeToStrictMaybe )
( ProtVer (..), Version, maybeToStrictMaybe )
import Cardano.Ledger.Binary
( Sized (..) )
import Cardano.Ledger.Coin
Expand Down
4 changes: 2 additions & 2 deletions lib/wallet/src/Cardano/Wallet/Write/Tx/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ import Cardano.Wallet.Write.Tx
, withConstraints
)
import Cardano.Wallet.Write.Tx.Balance.TokenBundleSize
( getTokenBundleMaxSize, mkTokenBundleSizeAssessor )
( mkTokenBundleSizeAssessor )
import Cardano.Wallet.Write.Tx.Redeemers
( ErrAssignRedeemers (..), Redeemer (..), assignScriptRedeemers )
import Cardano.Wallet.Write.Tx.Sign
Expand Down Expand Up @@ -926,7 +926,7 @@ selectAssets era (ProtocolParameters pp) utxoAssumptions outs redeemers

selectionConstraints = SelectionConstraints
{ tokenBundleSizeAssessor =
mkTokenBundleSizeAssessor (getTokenBundleMaxSize era pp)
mkTokenBundleSizeAssessor era pp
, computeMinimumAdaQuantity = \addr tokens -> W.toWallet $
computeMinimumCoinForTxOut
era
Expand Down
95 changes: 36 additions & 59 deletions lib/wallet/src/Cardano/Wallet/Write/Tx/Balance/TokenBundleSize.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}

-- | Assessing sizes of token bundles
module Cardano.Wallet.Write.Tx.Balance.TokenBundleSize
( TokenBundleSizeAssessor (..)
, TokenBundleMaxSize (..)
, computeTokenBundleSerializedLengthBytes
, mkTokenBundleSizeAssessor
, getTokenBundleMaxSize
, computeTokenBundleSerializedLengthBytes
)
where

Expand All @@ -16,73 +11,55 @@ import Prelude
import Cardano.CoinSelection.Size
( TokenBundleSizeAssessment (..), TokenBundleSizeAssessor (..) )
import Cardano.Ledger.Api
( ppMaxValSizeL )
( StandardCrypto, ppMaxValSizeL, ppProtocolVersionL )
import Cardano.Ledger.BaseTypes
( ProtVer (pvMajor) )
import Cardano.Ledger.Binary
( serialize', shelleyProtVer )
( serialize )
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( TxSize (..) )
import Cardano.Wallet.Shelley.Compatibility
( toCardanoValue )
import Cardano.Wallet.Shelley.Compatibility.Ledger
( toLedger )
import Cardano.Wallet.Write.Tx
( PParams, RecentEra, ShelleyLedgerEra, withConstraints )
import Control.DeepSeq
( NFData )
( PParams, RecentEra, ShelleyLedgerEra, Value, Version, withConstraints )
import Control.Lens
( (^.) )
import GHC.Generics
( Generic )
import Numeric.Natural
( Natural )
import Data.IntCast
( intCastMaybe )

import qualified Cardano.Api.Shelley as Cardano
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Data.ByteString as BS

-- | The maximum size of a serialized 'TokenBundle'.
-- ('_maxValSize' in the Alonzo ledger)
newtype TokenBundleMaxSize = TokenBundleMaxSize
{ unTokenBundleMaxSize :: TxSize }
deriving (Eq, Generic, Show)

instance NFData TokenBundleMaxSize
import qualified Data.ByteString.Lazy as BL

-- | Assesses a token bundle size in relation to the maximum size that can be
-- included in a transaction output.
--
-- See 'W.TokenBundleSizeAssessor' for the expected properties of this function.
-- See 'TokenBundleSizeAssessor' for the expected properties of this function.
--
mkTokenBundleSizeAssessor :: TokenBundleMaxSize -> TokenBundleSizeAssessor
mkTokenBundleSizeAssessor maxSize =
TokenBundleSizeAssessor { assessTokenBundleSize }
mkTokenBundleSizeAssessor
:: RecentEra era
-> PParams (ShelleyLedgerEra era)
-> TokenBundleSizeAssessor
mkTokenBundleSizeAssessor era pp = TokenBundleSizeAssessor $ \tb ->
if computeTokenBundleSerializedLengthBytes tb ver > maxValSize
then TokenBundleSizeExceedsLimit
else TokenBundleSizeWithinLimit
where
assessTokenBundleSize tb
| serializedLengthBytes <= maxSize' =
TokenBundleSizeWithinLimit
| otherwise =
TokenBundleSizeExceedsLimit
where
serializedLengthBytes :: TxSize
serializedLengthBytes = computeTokenBundleSerializedLengthBytes tb
maxValSize :: TxSize
maxValSize = TxSize $ withConstraints era $ pp ^. ppMaxValSizeL

maxSize' :: TxSize
maxSize' = unTokenBundleMaxSize maxSize
ver :: Version
ver = withConstraints era $ pvMajor $ pp ^. ppProtocolVersionL

computeTokenBundleSerializedLengthBytes :: TokenBundle.TokenBundle -> TxSize
computeTokenBundleSerializedLengthBytes =
TxSize
. safeCast
. BS.length
. serialize' shelleyProtVer
. Cardano.toMaryValue
. toCardanoValue
computeTokenBundleSerializedLengthBytes
:: TokenBundle.TokenBundle
-> Version
-> TxSize
computeTokenBundleSerializedLengthBytes tb ver = serSize (toLedger tb)
where
safeCast :: Int -> Natural
safeCast = fromIntegral

-- | Get a 'TokenBundleMaxSize' from a 'PParam era'.
getTokenBundleMaxSize
:: RecentEra era
-> PParams (ShelleyLedgerEra era)
-> TokenBundleMaxSize
getTokenBundleMaxSize era pp = withConstraints era $
TokenBundleMaxSize $ TxSize $ pp ^. ppMaxValSizeL
serSize :: Value StandardCrypto -> TxSize
serSize v = maybe err TxSize
. intCastMaybe
. BL.length
$ serialize ver v
where
err = error $ "negative serialized size of value: " <> show v

This file was deleted.

Loading

0 comments on commit ae6e90b

Please sign in to comment.