Skip to content

Commit

Permalink
fix numero dos
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Nov 4, 2024
1 parent 2441122 commit fefbe40
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 90 deletions.
13 changes: 9 additions & 4 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,17 @@ genTxMintValue =
inEonForEra
(pure TxMintNone)
$ \w -> do
values <- Gen.list (Range.constant 1 10) (genValueForMinting w)
witnessedValues <- forM values $ \v ->
(v,) . pure <$> genScriptWitnessForMint (maryEraOnwardsToShelleyBasedEra w)
policies <- Gen.list (Range.constant 1 3) genPolicyId
assets <- forM policies $ \policy ->
(,) policy <$>
Gen.list
(Range.constant 1 3)
((,,) <$> genAssetName
<*> genPositiveQuantity
<*> fmap (fmap pure) genScriptWitnessForMint (maryEraOnwardsToShelleyBasedEra w))
Gen.choice
[ pure TxMintNone
, pure $ TxMintValue w witnessedValues
, pure $ TxMintValue w (fromList assets)
]

genTxBodyContent :: ShelleyBasedEra era -> Gen (TxBodyContent BuildTx era)
Expand Down
47 changes: 13 additions & 34 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Fee calculation
Expand Down Expand Up @@ -1353,10 +1354,8 @@ calculateChangeValue
:: ShelleyBasedEra era -> Value -> TxBodyContent build era -> Value
calculateChangeValue sbe incoming txbodycontent =
let outgoing = calculateCreatedUTOValue sbe txbodycontent
mintedValues = case txMintValue txbodycontent of
TxMintNone -> mempty
TxMintValue _ vs -> fst <$> vs
in mconcat $ [incoming] <> mintedValues <> [negateValue outgoing]
mintedValue = txMintValueToValue $ txMintValue txbodycontent
in mconcat [incoming, mintedValue, negateValue outgoing]

-- | This is used in the balance calculation in the event where
-- the user does not supply the UTxO(s) they intend to spend
Expand Down Expand Up @@ -1627,40 +1626,20 @@ substituteExecutionUnits
:: TxMintValue BuildTx era
-> Either (TxBodyErrorAutoBalance era) (TxMintValue BuildTx era)
mapScriptWitnessesMinting TxMintNone = Right TxMintNone
mapScriptWitnessesMinting (TxMintValue w valueWitnesses) = do
let valuesWithPoliciesWithWitnesses = fromList
[ (policyId, (value, witness))
| let ValueNestedRep bundle = valueToNestedRep value
, ValueNestedBundle policyId _ <- bundle
, (value, witness) <- valueWitnesses
mapScriptWitnessesMinting txMintValue'@(TxMintValue w _) = do
let mappedScriptWitnesses =
[ (policyId, pure . (assetName',quantity,) <$> substitutedWitness)
| (ix, policyId, assetName', quantity, BuildTxWith witness) <- txMintValueToIndexed txMintValue'
, let substitutedWitness = BuildTxWith <$> substituteExecUnits ix witness
]
-- all policies sorted with correct indices
allPolicies = zip [0..] $ Map.keys valuesWithPoliciesWithWitnesses
forM valueWitnesses $ \(value, witness) ->
-- TODO

-- TxMintValue supported value $ BuildTxWith $ fromList
let mappedScriptWitnesses
:: [(PolicyId, Either (TxBodyErrorAutoBalance era) (ScriptWitness WitCtxMint era))]
mappedScriptWitnesses =
[ (policyid, eWitness)
| -- The minting policies are indexed in policy id order in the value
let ValueNestedRep bundle = valueToNestedRep value
, (ix, ValueNestedBundle policyid _) <- zip [0 ..] bundle
, witness <- maybeToList (Map.lookup policyid witnesses)
, let eWitness = substituteExecUnits (ScriptWitnessIndexMint ix) witness
]
-- let mappedScriptWitnesses =
-- [(value, eWitness) | let eWitness substituteExecUnits witness
final <- traverseScriptWitnesses mappedScriptWitnesses
Right . TxMintValue w value . BuildTxWith $
fromList final
final <- Map.fromListWith (<>) <$> traverseScriptWitnesses mappedScriptWitnesses
pure $ TxMintValue w final

traverseScriptWitnesses
:: [(a, Either (TxBodyErrorAutoBalance era) (ScriptWitness ctx era))]
-> Either (TxBodyErrorAutoBalance era) [(a, ScriptWitness ctx era)]
:: [(a, Either (TxBodyErrorAutoBalance era) b)]
-> Either (TxBodyErrorAutoBalance era) [(a, b)]
traverseScriptWitnesses =
traverse (\(item, eScriptWitness) -> eScriptWitness >>= (\sWit -> Right (item, sWit)))
traverse (\(item, eRes) -> eRes >>= (\res -> Right (item, res)))

calculateMinimumUTxO
:: ShelleyBasedEra era
Expand Down
14 changes: 13 additions & 1 deletion cardano-api/internal/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module Cardano.Api.Script
, WitCtxMint
, WitCtxStake
, WitCtx (..)
, WitCtxMaybe (..)
, ScriptWitness (..)
, Witness (..)
, KeyWitnessInCtx (..)
Expand Down Expand Up @@ -165,7 +166,7 @@ import Data.String (IsString)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Type.Equality (TestEquality (..), (:~:) (Refl))
import Data.Type.Equality (TestEquality (..), type (==), (:~:) (Refl))
import Data.Typeable (Typeable)
import Data.Vector (Vector)
import GHC.Exts (IsList (..))
Expand Down Expand Up @@ -676,6 +677,17 @@ data WitCtx witctx where
WitCtxMint :: WitCtx WitCtxMint
WitCtxStake :: WitCtx WitCtxStake

-- TODO: not needed anymore - remove

-- | A typed version of a 'Maybe'. Allows to store value of type @a@ when @purpose ~ witctx@, otherwise it is empty.
data WitCtxMaybe purpose witctx a where
WitCtxJust :: WitCtx purpose -> a -> WitCtxMaybe purpose purpose a
WitCtxNothing :: (purpose == witctx) ~ False => WitCtxMaybe purpose witctx a

instance Show a => Show (WitCtxMaybe p w a) where
show (WitCtxJust _ a) = "WitCtxJust " <> show a
show WitCtxNothing = "WitCtxNothing"

-- | Scripts can now exist in the UTxO at a transaction output. We can
-- reference these scripts via specification of a reference transaction input
-- in order to witness spending inputs, withdrawals, certificates
Expand Down
105 changes: 60 additions & 45 deletions cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ module Cardano.Api.Tx.Body
, TxCertificates (..)
, TxUpdateProposal (..)
, TxMintValue (..)
, txMintValueToValue
, txMintValueToIndexed
, TxVotingProcedures (..)
, mkTxVotingProcedures
, TxProposalProcedures (..)
Expand Down Expand Up @@ -215,7 +217,6 @@ import Cardano.Api.ProtocolParameters
import qualified Cardano.Api.ReexposeLedger as Ledger
import Cardano.Api.Script
import Cardano.Api.ScriptData
import Cardano.Api.ScriptData ()
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseJSON
import Cardano.Api.SerialiseRaw
Expand Down Expand Up @@ -1275,16 +1276,46 @@ data TxMintValue build era where
TxMintNone :: TxMintValue build era
TxMintValue
:: MaryEraOnwards era
-- This seems ill-defined:
-- Value here should not contain coins, they are ignored down the line
-- this basically means it should be a policyId -> witness map
-> [(Value, BuildTxWith build (ScriptWitness WitCtxMint era))]
-> Map
PolicyId
[ ( AssetName
, Quantity
, BuildTxWith build (ScriptWitness WitCtxMint era)
)
]
-> TxMintValue build era

deriving instance Eq (TxMintValue build era)

deriving instance Show (TxMintValue build era)

-- | Convert 'TxMintValue' to a more handy 'Value'.
txMintValueToValue :: TxMintValue build era -> Value
txMintValueToValue TxMintNone = mempty
txMintValueToValue (TxMintValue _ policiesWithAssets) =
fromList
[ (AssetId policyId' assetName', quantity)
| (policyId', assets) <- toList policiesWithAssets
, (assetName', quantity, _) <- assets
]

-- | Index the assets with witnesses in the order of policy ids.
txMintValueToIndexed
:: TxMintValue build era
-> [ ( ScriptWitnessIndex
, PolicyId
, AssetName
, Quantity
, BuildTxWith build (ScriptWitness WitCtxMint era)
)
]
txMintValueToIndexed TxMintNone = []
txMintValueToIndexed (TxMintValue _ policiesWithAssets) =
[ (ScriptWitnessIndexMint ix, policyId', assetName', quantity, witness)
| (ix, (policyId', assets)) <- zip [0 ..] $ toList policiesWithAssets
, (assetName', quantity, witness) <- assets
]

-- ----------------------------------------------------------------------------
-- Votes within transactions (era-dependent)
--
Expand Down Expand Up @@ -1588,7 +1619,7 @@ data TxBodyError
| TxBodyOutputNegative !Quantity !TxOutInAnyEra
| TxBodyOutputOverflow !Quantity !TxOutInAnyEra
| TxBodyMetadataError ![(Word64, TxMetadataRangeError)]
| TxBodyMintAdaError
| TxBodyMintAdaError -- TODO remove - case nonexistent
| TxBodyInIxOverflow !TxIn
| TxBodyMissingProtocolParams
| TxBodyProtocolParamsConversionError !ProtocolParametersConversionError
Expand Down Expand Up @@ -1858,11 +1889,9 @@ validateTxOuts sbe txOuts = do
| txout@(TxOut _ v _ _) <- txOuts
]

-- TODO remove
validateMintValue :: TxMintValue build era -> Either TxBodyError ()
validateMintValue txMintValue =
case txMintValue of
TxMintNone -> return ()
TxMintValue _ vs -> forM_ vs $ \(v, _) -> guard (selectLovelace v == 0) ?! TxBodyMintAdaError
validateMintValue _txMintValue = pure ()

inputIndexDoesNotExceedMax :: [(TxIn, a)] -> Either TxBodyError ()
inputIndexDoesNotExceedMax txIns =
Expand Down Expand Up @@ -2318,26 +2347,20 @@ fromLedgerTxMintValue
:: ShelleyBasedEra era
-> Ledger.TxBody (ShelleyLedgerEra era)
-> TxMintValue ViewTx era
fromLedgerTxMintValue sbe body = forEraInEon (toCardanoEra sbe) TxMintNone $ \w -> maryEraOnwardsConstraints w $ do
let mint = MaryValue (Ledger.Coin 0) (body ^. L.mintTxBodyL)
if L.isZero mint
then TxMintNone
else TxMintValue w [(fromMaryValue mint, ViewTx)]

-- TxMintValue w undefined
-- case sbe of
-- ShelleyBasedEraShelley -> TxMintNone
-- ShelleyBasedEraAllegra -> TxMintNone
-- ShelleyBasedEraMary -> toMintValue body MaryEraOnwardsMary
-- ShelleyBasedEraAlonzo -> toMintValue body MaryEraOnwardsAlonzo
-- ShelleyBasedEraBabbage -> toMintValue body MaryEraOnwardsBabbage
-- ShelleyBasedEraConway -> toMintValue body MaryEraOnwardsConway
-- where
-- toMintValue txBody maInEra
-- | L.isZero mint = TxMintNone
-- | otherwise = TxMintValue maInEra (fromMaryValue mint) ViewTx
-- where
-- mint = MaryValue (Ledger.Coin 0) (txBody ^. L.mintTxBodyL)
fromLedgerTxMintValue sbe body = forEraInEon (toCardanoEra sbe) TxMintNone $ \w ->
maryEraOnwardsConstraints w $ do
let mint = MaryValue (Ledger.Coin 0) (body ^. L.mintTxBodyL)
if L.isZero mint
then TxMintNone
else do
let assetMap = toList $ fromMaryValue mint
TxMintValue w $
Map.fromListWith
(<>)
[ (policyId', [(assetName', quantity, ViewTx)])
| -- only non-ada can be here
(AssetId policyId' assetName', quantity) <- toList assetMap
]

makeByronTransactionBody
:: ()
Expand Down Expand Up @@ -2451,13 +2474,9 @@ convTxUpdateProposal sbe = \case
TxUpdateProposal _ p -> bimap TxBodyProtocolParamsConversionError pure $ toLedgerUpdate sbe p

convMintValue :: TxMintValue build era -> MultiAsset StandardCrypto
convMintValue txMintValue =
case txMintValue of
TxMintNone -> mempty
TxMintValue _ vs ->
mconcat $ flip map vs $ \(v, _) ->
case toMaryValue v of
MaryValue _ ma -> ma
convMintValue txMintValue = do
let L.MaryValue _coin multiAsset = toMaryValue $ txMintValueToValue txMintValue
multiAsset

convExtraKeyWitnesses
:: TxExtraKeyWitnesses era -> Set (Shelley.KeyHash Shelley.Witness StandardCrypto)
Expand Down Expand Up @@ -3371,14 +3390,10 @@ collectTxBodyScriptWitnesses
:: TxMintValue BuildTx era
-> [(ScriptWitnessIndex, AnyScriptWitness era)]
scriptWitnessesMinting TxMintNone = []
scriptWitnessesMinting (TxMintValue _ _vw) = undefined
-- FIXME
-- [ (ScriptWitnessIndexMint ix, AnyScriptWitness witness)
-- \| -- The minting policies are indexed in policy id order in the value
-- let ValueNestedRep bundle = valueToNestedRep value
-- , (ix, ValueNestedBundle policyid _) <- zip [0 ..] bundle
-- , witness <- maybeToList (Map.lookup policyid witnesses)
-- ]
scriptWitnessesMinting txMintValue' =
[ (ix, AnyScriptWitness witness)
| (ix, _, _, _, BuildTxWith witness) <- txMintValueToIndexed txMintValue'
]

scriptWitnessesVoting
:: TxVotingProcedures BuildTx era
Expand Down
1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ calcMinimumDeposit v =
-- ----------------------------------------------------------------------------
-- An alternative nested representation
--
-- TODO remove ? - it is now unused

-- | An alternative nested representation for 'Value' that groups assets that
-- share a 'PolicyId'.
Expand Down
3 changes: 3 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ module Cardano.Api
, TxCertificates (..)
, TxUpdateProposal (..)
, TxMintValue (..)
, txMintValueToValue
, txMintValueToIndexed
, TxVotingProcedures (..)
, mkTxVotingProcedures
, TxProposalProcedures (..)
Expand Down Expand Up @@ -527,6 +529,7 @@ module Cardano.Api
, WitCtxMint
, WitCtxStake
, WitCtx (..)
, WitCtxMaybe (..)
, ScriptWitness (..)
, Witness (..)
, KeyWitnessInCtx (..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ prop_make_transaction_body_autobalance_return_correct_fee_for_multi_asset = H.pr
let txMint =
TxMintValue
meo
undefined
-- [(AssetId policyId' "eeee", 1)]
-- (BuildTxWith [(policyId', plutusWitness)])
[(policyId', [("eeee", 1, BuildTxWith plutusWitness)])]

-- tx body content without an asset in TxOut
let content =
Expand Down Expand Up @@ -168,9 +166,7 @@ prop_make_transaction_body_autobalance_multi_asset_collateral = H.propertyOnce $
let txMint =
TxMintValue
meo
undefined
-- [(AssetId policyId' "eeee", 1)]
-- (BuildTxWith [(policyId', plutusWitness)])
[(policyId', [("eeee", 1, BuildTxWith plutusWitness)])]

let content =
defaultTxBodyContent sbe
Expand Down

0 comments on commit fefbe40

Please sign in to comment.