Skip to content

Commit

Permalink
WIP: Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed May 12, 2021
1 parent aca5565 commit 32a1c83
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 279 deletions.
8 changes: 4 additions & 4 deletions cardano-api/src/Cardano/Api/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ anyCardanoEra ShelleyEra = AnyCardanoEra ShelleyEra
anyCardanoEra AllegraEra = AnyCardanoEra AllegraEra
anyCardanoEra MaryEra = AnyCardanoEra MaryEra

anyCardanoEraShelleyBased :: ShelleyBasedEra era -> AnyCardanoEra
anyCardanoEraShelleyBased ShelleyBasedEraShelley = AnyCardanoEra ShelleyEra
anyCardanoEraShelleyBased ShelleyBasedEraAllegra = AnyCardanoEra AllegraEra
anyCardanoEraShelleyBased ShelleyBasedEraMary = AnyCardanoEra MaryEra
anyCardanoEraShelleyBased :: ShelleyBasedEra era -> CardanoEra era
anyCardanoEraShelleyBased ShelleyBasedEraShelley = ShelleyEra
anyCardanoEraShelleyBased ShelleyBasedEraAllegra = AllegraEra
anyCardanoEraShelleyBased ShelleyBasedEraMary = MaryEra

-- | This pairs up some era-dependent type with a 'CardanoEra' value that tells
-- us what era it is, but hides the era type. This is useful when the era is
Expand Down
2 changes: 2 additions & 0 deletions cardano-api/src/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ transactionFee sbe txFeeFixed txFeePerByte tx =
case tx of
ShelleyTx _ tx' -> let x = getField @"txsize" tx'
in Lovelace (a * x + b)
--TODO: This can be made to work for Byron txs too. Do that: fill in this case
-- and remove the IsShelleyBasedEra constraint.
ByronTx _ -> case sbe :: ShelleyBasedEra ByronEra of {}


Expand Down
9 changes: 7 additions & 2 deletions cardano-api/src/Cardano/Api/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Cardano.Api.Orphans () where
import Prelude

import Control.Iterate.SetAlgebra (BiMap (..), Bimap)
import Data.Aeson (ToJSON (..), object, (.=))
import Data.Aeson (FromJSON (..), ToJSON (..), object, withObject, (.:), (.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (ToJSONKey (..), toJSONKeyText)
import qualified Data.ByteString.Base16 as B16
Expand All @@ -23,6 +23,7 @@ import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text

import qualified Cardano.Crypto.Hash.Class as Crypto
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Coin as Shelley
import qualified Cardano.Ledger.Core as Core
import qualified Cardano.Ledger.Crypto as Crypto
Expand All @@ -37,8 +38,8 @@ import qualified Shelley.Spec.Ledger.Delegation.Certificates as Shelley
import qualified Shelley.Spec.Ledger.EpochBoundary as ShelleyEpoch
import qualified Shelley.Spec.Ledger.LedgerState as ShelleyLedger
import Shelley.Spec.Ledger.PParams (PParamsUpdate)
import qualified Shelley.Spec.Ledger.Rewards as Shelley
import qualified Shelley.Spec.Ledger.RewardUpdate as Shelley
import qualified Shelley.Spec.Ledger.Rewards as Shelley

-- Orphan instances involved in the JSON output of the API queries.
-- We will remove/replace these as we provide more API wrapper types
Expand Down Expand Up @@ -278,3 +279,7 @@ instance ToJSON Shelley.RewardType where
instance ToJSON (SafeHash.SafeHash c a) where
toJSON = toJSON . SafeHash.extractHash

instance FromJSON Alonzo.Prices where
parseJSON = withObject "Prices" $ \o -> do
obj <- o .: "executionUnitPrices"
Alonzo.Prices <$> obj .: "unitSpace" <*> obj .: "unitTime"
329 changes: 84 additions & 245 deletions cardano-api/src/Cardano/Api/ProtocolParameters.hs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions cardano-api/src/Cardano/Api/ProtocolParametersUpdate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ data ProtocolParametersUpdate =
--protocolParamMinUTxOValue in the Alonzo era onwards).
protocolUpdateUTxOCostPerByte :: Maybe Lovelace,

-- | Cost models for non-native script languages.
-- | Cost models for script languages that use them.
protocolUpdateCostModels :: Map AnyScriptLanguage CostModel,

-- | Map AnyScriptLanguage ExecutionUnitPrices of execution units (for non-native script languages).
protocolUpdatePrices :: Map AnyScriptLanguage ExecutionUnitPrices,
-- | Map of script language execution unit prices.
protocolUpdatePrices :: Map AnyScriptLanguage Alonzo.Prices,

-- | Max total script execution resources units allowed per tx
protocolUpdateMaxTxExUnits :: Maybe MaxTxExecutionUnits,
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ data QueryInShelleyBasedEra era result where
:: QueryInShelleyBasedEra era (GenesisParameters era)

QueryProtocolParameters
:: QueryInShelleyBasedEra era (ProtocolParameters era)
:: QueryInShelleyBasedEra era ProtocolParameters

QueryProtocolParametersUpdate
:: QueryInShelleyBasedEra era
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/test/Test/Cardano/Api/Typed/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ genMaybePraosNonce :: Gen (Maybe PraosNonce)
genMaybePraosNonce =
Gen.maybe (makePraosNonce <$> Gen.bytes (Range.linear 0 32))

genProtocolParameters :: ShelleyBasedEra era -> Gen (ProtocolParameters era)
genProtocolParameters :: ShelleyBasedEra era -> Gen ProtocolParameters
genProtocolParameters sbe =
case sbe of
ShelleyBasedEraShelley -> genPreAlonzo
Expand Down
11 changes: 5 additions & 6 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import Cardano.CLI.Types
import Cardano.Binary (decodeFull)
import Cardano.Crypto.Hash (hashToBytesAsHex)

import Cardano.Ledger.Coin
import qualified Cardano.Ledger.Crypto as Crypto
import qualified Cardano.Ledger.Era as Era
import qualified Cardano.Ledger.Shelley.Constraints as Ledger
Expand All @@ -56,14 +57,13 @@ import Ouroboros.Network.Block (Serialised (..))
import Ouroboros.Network.Protocol.LocalStateQuery.Type as LocalStateQuery
(AcquireFailure (..))
import qualified Shelley.Spec.Ledger.API.Protocol as Ledger
import Cardano.Ledger.Coin
import Shelley.Spec.Ledger.EpochBoundary
import Shelley.Spec.Ledger.Keys (KeyHash (..), KeyRole (..))
import Shelley.Spec.Ledger.LedgerState hiding (_delegations)
import Shelley.Spec.Ledger.Scripts ()

import qualified Ouroboros.Consensus.HardFork.History.Qry as Qry
import qualified Data.Text.IO as T
import qualified Ouroboros.Consensus.HardFork.History.Qry as Qry
import qualified System.IO as IO

{- HLINT ignore "Reduce duplication" -}
Expand Down Expand Up @@ -151,9 +151,8 @@ runQueryProtocolParameters (AnyConsensusModeParams cModeParams) network mOutFile
Nothing -> left $ ShelleyQueryCmdEraConsensusModeMismatch (AnyConsensusMode cMode) anyE
where
writeProtocolParameters
:: IsCardanoEra era
=> Maybe OutputFile
-> ProtocolParameters era
:: Maybe OutputFile
-> ProtocolParameters
-> ExceptT ShelleyQueryCmdError IO ()
writeProtocolParameters mOutFile' pparams =
case mOutFile' of
Expand Down Expand Up @@ -199,7 +198,7 @@ runQueryTip (AnyConsensusModeParams cModeParams) network mOutFile = do
case mOutFile of
Just (OutputFile fpath) -> liftIO $ LBS.writeFile fpath output
Nothing -> liftIO $ LBS.putStrLn output

where
tuple3Fst :: (a, b, c) -> a
tuple3Fst (a, _, _) = a
Expand Down
36 changes: 19 additions & 17 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ runTxCalculateMinFee (TxBodyFile txbodyFile) nw protocolParamsSourceSpec

liftIO $ putStrLn $ (show fee :: String) <> " Lovelace"
ParamsFromFile pParamFile -> do
pparams <- readProtocolParameters sbe pParamFile
pparams <- readProtocolParameters pParamFile
let tx = makeSignedTransaction [] txbody
Lovelace fee = estimateTransactionFee sbe
(fromMaybe Mainnet nw)
Expand All @@ -711,7 +711,7 @@ runTxCalculateMinFee (TxBodyFile txbodyFile) nw protocolParamsSourceSpec


getProtocolParametersFromGen :: GenesisFile
-> ExceptT ShelleyTxCmdError IO (ProtocolParameters ShelleyEra)
-> ExceptT ShelleyTxCmdError IO ProtocolParameters
getProtocolParametersFromGen (GenesisFile f) = do
shelleyGen <- firstExceptT ShelleyTxCmdGenesisCmdError (readShelleyGenesis f identity)
firstExceptT ShelleyTxCmdProtocolParametersError . hoistEither
Expand All @@ -729,25 +729,29 @@ runTxCalculateMinValue protocolParamsSourceSpec (TxBodyFile txbodyFile) = do
InAnyCardanoEra era apiTxBody <- readFileTxBody txbodyFile
case cardanoEraStyle era of
LegacyByronEra -> txFeatureMismatch era TxFeatureMultiAssetOutputs
ShelleyBasedEra ShelleyBasedEraMary ->
ShelleyBasedEra sbe ->
case protocolParamsSourceSpec of
ParamsFromGenesis _ -> panic "runTxCalculateMinValue: Currently \
\not possible to get protocol params from genesis file/"
-- TODO: Investigate this again since era was removed from ProtocolParameters
-- We need evidence that we are at least in the Mary era in order to use multi assets.
-- readShelleyGenesis defaults to the shelley era. TODO: make `readShelleyGenesis` era independent.
-- This error currently isn't possible as we only parse `ParamsFromFile` in the cli currently.
ParamsFromFile f -> do
pp <- readProtocolParameters ShelleyBasedEraMary f
pp <- readProtocolParameters f
case protocolParamMinUTxOValue pp of
Just minUTxOVal -> do
let (ShelleyTxBody _ txbody _ _) = apiTxBody
(ShelleyMA.TxBody _ _ _ _ _ _ _ _ mint) = txbody
value = fromMaryValue mint
let minDeposit = calcMinimumDeposit value minUTxOVal
liftIO $ IO.print minDeposit
Just minUTxOVal ->
case sbe of
ShelleyBasedEraMary -> do
let (ShelleyTxBody _ txbody _ _) = apiTxBody
(ShelleyMA.TxBody _ _ _ _ _ _ _ _ mint) = txbody
value = fromMaryValue mint
let minDeposit = calcMinimumDeposit value minUTxOVal
liftIO $ IO.print minDeposit
wrongera ->
left $ ShelleyTxCmdMultiAssetNotAvailableInEra
(AnyCardanoEra $ anyCardanoEraShelleyBased wrongera) txbodyFile
Nothing -> left $ ShelleyTxCmdMinUTxOValueNotSpecifiedInPParams f
ShelleyBasedEra wrongera ->
left $ ShelleyTxCmdMultiAssetNotAvailableInEra (anyCardanoEraShelleyBased wrongera) txbodyFile

runTxCreatePolicyId :: ScriptFile -> ExceptT ShelleyTxCmdError IO ()
runTxCreatePolicyId (ScriptFile sFile) = do
Expand All @@ -756,11 +760,9 @@ runTxCreatePolicyId (ScriptFile sFile) = do

--TODO: eliminate this and get only the necessary params, and get them in a more
-- helpful way rather than requiring them as a local file.
readProtocolParameters :: IsCardanoEra era
=> ShelleyBasedEra era
-> ProtocolParamsFile
-> ExceptT ShelleyTxCmdError IO (ProtocolParameters era)
readProtocolParameters _ (ProtocolParamsFile fpath) = do
readProtocolParameters :: ProtocolParamsFile
-> ExceptT ShelleyTxCmdError IO ProtocolParameters
readProtocolParameters (ProtocolParamsFile fpath) = do
pparams <- handleIOExceptT (ShelleyTxCmdReadFileError . FileIOError fpath) $ LBS.readFile fpath
firstExceptT (ShelleyTxCmdAesonDecodeProtocolParamsError fpath . Text.pack) . hoistEither $
Aeson.eitherDecode' pparams
Expand Down

0 comments on commit 32a1c83

Please sign in to comment.