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

Fill in assign script redeemers #3311

335 changes: 3 additions & 332 deletions lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ module Cardano.Wallet.Shelley.Compatibility
, fromAlonzoPParams
, fromLedgerExUnits
, toLedgerExUnits
, fromLedgerPParams
, fromLedgerAlonzoPParams
, toAlonzoPParams
, fromCardanoAddress
, toSystemStart
, toScriptPurpose
Expand Down Expand Up @@ -256,7 +253,7 @@ import Control.Applicative
import Control.Arrow
( left )
import Control.Monad
( join, when, (>=>) )
( when, (>=>) )
import Crypto.Hash.Utils
( blake2b224 )
import Data.Array
Expand Down Expand Up @@ -363,15 +360,10 @@ import qualified Cardano.Ledger.Alonzo.TxBody as Alonzo
import qualified Cardano.Ledger.Alonzo.TxSeq as Alonzo
import qualified Cardano.Ledger.Alonzo.TxWitness as Alonzo
import qualified Cardano.Ledger.Babbage as Babbage
import qualified Cardano.Ledger.Babbage.PParams as Babbage
import qualified Cardano.Ledger.Babbage.Tx as Babbage hiding
( ScriptIntegrityHash, TxBody )
import qualified Cardano.Ledger.Babbage.TxBody as Babbage
import qualified Cardano.Ledger.BaseTypes as BT
import qualified Cardano.Ledger.BaseTypes as Ledger
import qualified Cardano.Ledger.BaseTypes as SL
import qualified Cardano.Ledger.Coin as Ledger
import qualified Cardano.Ledger.Core as Ledger
import qualified Cardano.Ledger.Core as SL.Core
import qualified Cardano.Ledger.Credential as SL
import qualified Cardano.Ledger.Crypto as SL
Expand All @@ -383,7 +375,6 @@ import qualified Cardano.Ledger.Shelley as SL hiding
import qualified Cardano.Ledger.Shelley.API as SL
import qualified Cardano.Ledger.Shelley.API as SLAPI
import qualified Cardano.Ledger.Shelley.BlockChain as SL
import qualified Cardano.Ledger.Shelley.PParams as Shelley
import qualified Cardano.Ledger.Shelley.Tx as Shelley
import qualified Cardano.Ledger.ShelleyMA as MA
import qualified Cardano.Ledger.ShelleyMA.AuxiliaryData as MA
Expand Down Expand Up @@ -863,8 +854,8 @@ executionUnitPricesFromPParams pp =
prices = getField @"_prices" pp
fromAlonzoPrices Alonzo.Prices{prMem, prSteps} =
W.ExecutionUnitPrices
{ W.pricePerStep = Ledger.unboundRational prSteps
, W.pricePerMemoryUnit = Ledger.unboundRational prMem
{ W.pricePerStep = SL.unboundRational prSteps
, W.pricePerMemoryUnit = SL.unboundRational prMem
}

fromLedgerExUnits
Expand Down Expand Up @@ -906,326 +897,6 @@ txParametersFromPParams maxBundleSize getMaxExecutionUnits pp = W.TxParameters
naturalToDouble :: Natural -> Double
naturalToDouble = fromIntegral

--------------------------------------------------------------------------------
-- Copied from cardano-api
-- To be removed when once again exposed.
--------------------------------------------------------------------------------

fromLedgerPParams
:: Cardano.ShelleyBasedEra era
-> Ledger.PParams (Cardano.ShelleyLedgerEra era)
-> Cardano.ProtocolParameters
fromLedgerPParams Cardano.ShelleyBasedEraShelley = fromLedgerShelleyPParams
fromLedgerPParams Cardano.ShelleyBasedEraAllegra = fromLedgerShelleyPParams
fromLedgerPParams Cardano.ShelleyBasedEraMary = fromLedgerShelleyPParams
fromLedgerPParams Cardano.ShelleyBasedEraAlonzo = fromLedgerAlonzoPParams
fromLedgerPParams Cardano.ShelleyBasedEraBabbage = fromLedgerBabbagePParams

fromShelleyLovelace :: Ledger.Coin -> Cardano.Lovelace
fromShelleyLovelace (Ledger.Coin c) = Cardano.Lovelace c

fromLedgerNonce :: Ledger.Nonce -> Maybe Cardano.PraosNonce
fromLedgerNonce Ledger.NeutralNonce = Nothing
fromLedgerNonce (Ledger.Nonce h) =
Just (Cardano.makePraosNonce $ Crypto.hashToBytes h)

fromLedgerShelleyPParams
:: Shelley.PParams ledgerera
-> Cardano.ProtocolParameters
fromLedgerShelleyPParams
Shelley.PParams {
Shelley._minfeeA
, Shelley._minfeeB
, Shelley._maxBBSize
, Shelley._maxTxSize
, Shelley._maxBHSize
, Shelley._keyDeposit
, Shelley._poolDeposit
, Shelley._eMax
, Shelley._nOpt
, Shelley._a0
, Shelley._rho
, Shelley._tau
, Shelley._d
, Shelley._extraEntropy
, Shelley._protocolVersion
, Shelley._minUTxOValue
, Shelley._minPoolCost
} = Cardano.ProtocolParameters {
protocolParamProtocolVersion = (\(BT.ProtVer a b) -> (a,b))
_protocolVersion
, protocolParamDecentralization = Just $ SL.unboundRational _d
, protocolParamExtraPraosEntropy = fromLedgerNonce _extraEntropy
, protocolParamMaxBlockHeaderSize = _maxBHSize
, protocolParamMaxBlockBodySize = _maxBBSize
, protocolParamMaxTxSize = _maxTxSize
, protocolParamTxFeeFixed = _minfeeB
, protocolParamTxFeePerByte = _minfeeA
, protocolParamMinUTxOValue = Just (fromShelleyLovelace _minUTxOValue)
, protocolParamStakeAddressDeposit = fromShelleyLovelace _keyDeposit
, protocolParamStakePoolDeposit = fromShelleyLovelace _poolDeposit
, protocolParamMinPoolCost = fromShelleyLovelace _minPoolCost
, protocolParamPoolRetireMaxEpoch = _eMax
, protocolParamStakePoolTargetNum = _nOpt
, protocolParamPoolPledgeInfluence = SL.unboundRational _a0
, protocolParamMonetaryExpansion = SL.unboundRational _rho
, protocolParamTreasuryCut = SL.unboundRational _tau
, protocolParamUTxOCostPerWord = Nothing
, protocolParamCostModels = Map.empty
, protocolParamPrices = Nothing
, protocolParamMaxTxExUnits = Nothing
, protocolParamMaxBlockExUnits = Nothing
, protocolParamMaxValueSize = Nothing
, protocolParamCollateralPercent = Nothing
, protocolParamMaxCollateralInputs = Nothing
}

fromLedgerAlonzoPParams
:: Alonzo.PParams ledgerera
-> Cardano.ProtocolParameters
fromLedgerAlonzoPParams
Alonzo.PParams {
Alonzo._minfeeA
, Alonzo._minfeeB
, Alonzo._maxBBSize
, Alonzo._maxTxSize
, Alonzo._maxBHSize
, Alonzo._keyDeposit
, Alonzo._poolDeposit
, Alonzo._eMax
, Alonzo._nOpt
, Alonzo._a0
, Alonzo._rho
, Alonzo._tau
, Alonzo._d
, Alonzo._extraEntropy
, Alonzo._protocolVersion
, Alonzo._minPoolCost
, Alonzo._coinsPerUTxOWord
, Alonzo._costmdls
, Alonzo._prices
, Alonzo._maxTxExUnits
, Alonzo._maxBlockExUnits
, Alonzo._maxValSize
, Alonzo._collateralPercentage
, Alonzo._maxCollateralInputs
} = Cardano.ProtocolParameters {
protocolParamProtocolVersion = (\(BT.ProtVer a b) -> (a,b))
_protocolVersion
, protocolParamDecentralization = Just $ SL.unboundRational _d
, protocolParamExtraPraosEntropy = fromLedgerNonce _extraEntropy
, protocolParamMaxBlockHeaderSize = _maxBHSize
, protocolParamMaxBlockBodySize = _maxBBSize
, protocolParamMaxTxSize = _maxTxSize
, protocolParamTxFeeFixed = _minfeeB
, protocolParamTxFeePerByte = _minfeeA
, protocolParamMinUTxOValue = Nothing
, protocolParamStakeAddressDeposit = fromShelleyLovelace _keyDeposit
, protocolParamStakePoolDeposit = fromShelleyLovelace _poolDeposit
, protocolParamMinPoolCost = fromShelleyLovelace _minPoolCost
, protocolParamPoolRetireMaxEpoch = _eMax
, protocolParamStakePoolTargetNum = _nOpt
, protocolParamPoolPledgeInfluence = SL.unboundRational _a0
, protocolParamMonetaryExpansion = SL.unboundRational _rho
, protocolParamTreasuryCut = SL.unboundRational _tau
, protocolParamUTxOCostPerWord = Just (fromShelleyLovelace _coinsPerUTxOWord)
, protocolParamCostModels = fromAlonzoCostModels $ Alonzo.unCostModels _costmdls
, protocolParamPrices = Just (fromAlonzoPrices _prices)
, protocolParamMaxTxExUnits = Just (fromAlonzoExUnits _maxTxExUnits)
, protocolParamMaxBlockExUnits = Just (fromAlonzoExUnits _maxBlockExUnits)
, protocolParamMaxValueSize = Just _maxValSize
, protocolParamCollateralPercent = Just _collateralPercentage
, protocolParamMaxCollateralInputs = Just _maxCollateralInputs
}
where
fromAlonzoPrices :: Alonzo.Prices -> Cardano.ExecutionUnitPrices
fromAlonzoPrices Alonzo.Prices{Alonzo.prSteps, Alonzo.prMem} = Cardano.ExecutionUnitPrices
{ priceExecutionSteps = Ledger.unboundRational prSteps
, priceExecutionMemory = Ledger.unboundRational prMem
}

fromAlonzoExUnits :: Alonzo.ExUnits -> Cardano.ExecutionUnits
fromAlonzoExUnits Alonzo.ExUnits{Alonzo.exUnitsSteps, Alonzo.exUnitsMem} = Cardano.ExecutionUnits
{ executionSteps = exUnitsSteps
, executionMemory = exUnitsMem
}

fromAlonzoScriptLanguage :: Alonzo.Language -> Cardano.AnyPlutusScriptVersion
fromAlonzoScriptLanguage Alonzo.PlutusV1 =
Cardano.AnyPlutusScriptVersion Cardano.PlutusScriptV1
fromAlonzoScriptLanguage Alonzo.PlutusV2 =
Cardano.AnyPlutusScriptVersion Cardano.PlutusScriptV2

fromAlonzoCostModel :: Alonzo.CostModel -> Cardano.CostModel
fromAlonzoCostModel m = Cardano.CostModel $ Alonzo.getCostModelParams m

fromAlonzoCostModels
:: Map Alonzo.Language Alonzo.CostModel
-> Map Cardano.AnyPlutusScriptVersion Cardano.CostModel
fromAlonzoCostModels =
Map.fromList
. map (bimap fromAlonzoScriptLanguage fromAlonzoCostModel)
. Map.toList

fromLedgerBabbagePParams
:: Babbage.PParams ledgerera
-> Cardano.ProtocolParameters
fromLedgerBabbagePParams = undefined


toAlonzoPParams
:: Cardano.ProtocolParameters
-> Alonzo.PParams era
toAlonzoPParams
Cardano.ProtocolParameters
{ protocolParamProtocolVersion
, protocolParamDecentralization
, protocolParamExtraPraosEntropy
, protocolParamMaxBlockHeaderSize
, protocolParamMaxBlockBodySize
, protocolParamMaxTxSize
, protocolParamTxFeeFixed
, protocolParamTxFeePerByte
, protocolParamStakeAddressDeposit
, protocolParamStakePoolDeposit
, protocolParamMinPoolCost
, protocolParamPoolRetireMaxEpoch
, protocolParamStakePoolTargetNum
, protocolParamPoolPledgeInfluence
, protocolParamMonetaryExpansion
, protocolParamTreasuryCut
, protocolParamUTxOCostPerWord = Just utxoCostPerWord
, protocolParamCostModels
, protocolParamPrices = Just prices
, protocolParamMaxTxExUnits = Just maxTxExUnits
, protocolParamMaxBlockExUnits = Just maxBlockExUnits
, protocolParamMaxValueSize = Just maxValueSize
, protocolParamCollateralPercent = Just collateralPercentage
, protocolParamMaxCollateralInputs = Just maxCollateralInputs
} =
Alonzo.PParams
{ Alonzo._protocolVersion =
let (maj, minor) = protocolParamProtocolVersion
in BT.ProtVer maj minor
, Alonzo._d =
fromMaybe
(error "toAlonzoPParams: invalid Decentralization value")
(join $ Ledger.boundRational <$> protocolParamDecentralization)
, Alonzo._extraEntropy =
toLedgerNonce protocolParamExtraPraosEntropy
, Alonzo._maxBHSize =
protocolParamMaxBlockHeaderSize
, Alonzo._maxBBSize =
protocolParamMaxBlockBodySize
, Alonzo._maxTxSize =
protocolParamMaxTxSize
, Alonzo._minfeeB =
protocolParamTxFeeFixed
, Alonzo._minfeeA =
protocolParamTxFeePerByte
, Alonzo._keyDeposit =
toShelleyLovelace protocolParamStakeAddressDeposit
, Alonzo._poolDeposit =
toShelleyLovelace protocolParamStakePoolDeposit
, Alonzo._minPoolCost =
toShelleyLovelace protocolParamMinPoolCost
, Alonzo._eMax =
protocolParamPoolRetireMaxEpoch
, Alonzo._nOpt =
protocolParamStakePoolTargetNum
, Alonzo._a0 =
fromMaybe
(error "toAlonzoPParams: invalid PoolPledgeInfluence value")
(Ledger.boundRational protocolParamPoolPledgeInfluence)
, Alonzo._rho =
fromMaybe
(error "toAlonzoPParams: invalid MonetaryExpansion value")
(Ledger.boundRational protocolParamMonetaryExpansion)
, Alonzo._tau =
fromMaybe
(error "toAlonzoPParams: invalid TreasuryCut value")
(Ledger.boundRational protocolParamTreasuryCut)
, Alonzo._coinsPerUTxOWord =
toShelleyLovelace utxoCostPerWord
, Alonzo._costmdls = either
(\e -> error $ "toAlonzoPParams: invalid cost models, error: " <> e)
id
(toAlonzoCostModels protocolParamCostModels)
, Alonzo._prices =
fromMaybe
(error "toAlonzoPParams: invalid Price values")
(toAlonzoPrices prices)
, Alonzo._maxTxExUnits =
toAlonzoExUnits maxTxExUnits
, Alonzo._maxBlockExUnits =
toAlonzoExUnits maxBlockExUnits
, Alonzo._maxValSize =
maxValueSize
, Alonzo._collateralPercentage =
collateralPercentage
, Alonzo._maxCollateralInputs =
maxCollateralInputs
}
where
toShelleyLovelace :: Cardano.Lovelace -> SLAPI.Coin
toShelleyLovelace (Cardano.Lovelace l) = SLAPI.Coin l

toAlonzoCostModels
:: Map Cardano.AnyPlutusScriptVersion Cardano.CostModel
-> Either String Alonzo.CostModels
toAlonzoCostModels m = do
f <- mapM conv $ Map.toList m
Right . Alonzo.CostModels $ Map.fromList f
where
conv :: (Cardano.AnyPlutusScriptVersion, Cardano.CostModel) -> Either String (Alonzo.Language, Alonzo.CostModel)
conv (anySVer, cModel )= do
alonzoCostModel <- toAlonzoCostModel cModel (toAlonzoScriptLanguage anySVer)
Right (toAlonzoScriptLanguage anySVer, alonzoCostModel)

toAlonzoCostModel :: Cardano.CostModel -> Alonzo.Language -> Either String Alonzo.CostModel
toAlonzoCostModel (Cardano.CostModel m) l = Alonzo.mkCostModel l m

toAlonzoScriptLanguage :: Cardano.AnyPlutusScriptVersion -> Alonzo.Language
toAlonzoScriptLanguage (Cardano.AnyPlutusScriptVersion Cardano.PlutusScriptV1) =
Alonzo.PlutusV1
toAlonzoScriptLanguage (Cardano.AnyPlutusScriptVersion Cardano.PlutusScriptV2) =
Alonzo.PlutusV2

toAlonzoPrices :: Cardano.ExecutionUnitPrices -> Maybe Alonzo.Prices
toAlonzoPrices Cardano.ExecutionUnitPrices
{ priceExecutionSteps
, priceExecutionMemory
} = do
prSteps <- Ledger.boundRational priceExecutionSteps
prMem <- Ledger.boundRational priceExecutionMemory
return Alonzo.Prices { Alonzo.prSteps, Alonzo.prMem }

toAlonzoExUnits :: Cardano.ExecutionUnits -> Alonzo.ExUnits
toAlonzoExUnits Cardano.ExecutionUnits{executionSteps, executionMemory} =
Alonzo.ExUnits
{ Alonzo.exUnitsSteps = executionSteps
, Alonzo.exUnitsMem = executionMemory
}

toLedgerNonce :: Maybe Cardano.PraosNonce -> Ledger.Nonce
toLedgerNonce = \case
Nothing -> Ledger.NeutralNonce
Just nonce -> Ledger.Nonce (unsafeHashFromBytes (Cardano.serialiseToRawBytes nonce))
toAlonzoPParams Cardano.ProtocolParameters { protocolParamUTxOCostPerWord = Nothing } =
error "toAlonzoPParams: must specify protocolParamUTxOCostPerWord"
toAlonzoPParams Cardano.ProtocolParameters { protocolParamPrices = Nothing } =
error "toAlonzoPParams: must specify protocolParamPrices"
toAlonzoPParams Cardano.ProtocolParameters { protocolParamMaxTxExUnits = Nothing } =
error "toAlonzoPParams: must specify protocolParamMaxTxExUnits"
toAlonzoPParams Cardano.ProtocolParameters { protocolParamMaxBlockExUnits = Nothing } =
error "toAlonzoPParams: must specify protocolParamMaxBlockExUnits"
toAlonzoPParams Cardano.ProtocolParameters { protocolParamMaxValueSize = Nothing } =
error "toAlonzoPParams: must specify protocolParamMaxValueSize"
toAlonzoPParams Cardano.ProtocolParameters { protocolParamCollateralPercent = Nothing } =
error "toAlonzoPParams: must specify protocolParamCollateralPercent"
toAlonzoPParams Cardano.ProtocolParameters { protocolParamMaxCollateralInputs = Nothing } =
error "toAlonzoPParams: must specify protocolParamMaxCollateralInputs"

toCostModelsAsArray
:: Map Alonzo.Language Alonzo.CostModel
-> Array Alonzo.Language Alonzo.CostModel
Expand Down
Loading