Skip to content

Commit

Permalink
Remove translateTimeForPlutusScripts hardfork switch
Browse files Browse the repository at this point in the history
since it was verified that removing it creates the same ledger state,
proving the switch unnecessary.
  • Loading branch information
teodanciu committed Nov 21, 2022
1 parent dc5331f commit 38e2f80
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 59 deletions.
Expand Up @@ -165,7 +165,7 @@ collectTwoPhaseScriptInputs ei sysS pp tx utxo =
(Right [])
where
scriptsAvailable = txscripts utxo tx
txinfo lang = txInfo pp lang ei sysS utxo tx
txinfo lang = txInfo lang ei sysS utxo tx
AlonzoScriptsNeeded scriptsNeeded' = getScriptsNeeded utxo (tx ^. bodyTxL)
neededAndConfirmedToBePlutus =
mapMaybe (knownToNotBe1Phase scriptsAvailable) scriptsNeeded'
Expand Down
2 changes: 1 addition & 1 deletion eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tools.hs
Expand Up @@ -163,7 +163,7 @@ evaluateTransactionExecutionUnitsWithLogs ::
Either (TranslationError (EraCrypto era)) (RedeemerReportWithLogs (EraCrypto era))
evaluateTransactionExecutionUnitsWithLogs pp tx utxo ei sysS costModels = do
let getInfo :: Language -> Either (TranslationError (EraCrypto era)) VersionedTxInfo
getInfo lang = txInfo pp lang ei sysS utxo tx
getInfo lang = txInfo lang ei sysS utxo tx
ctx <- sequence $ Map.fromSet getInfo languagesUsed
pure $
Map.mapWithKey
Expand Down
44 changes: 13 additions & 31 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxInfo.hs
Expand Up @@ -99,7 +99,6 @@ import Cardano.Ledger.Keys (KeyHash (..), hashKey)
import Cardano.Ledger.Language (Language (..))
import Cardano.Ledger.Mary.Value (AssetName (..), MaryValue (..), MultiAsset (..), PolicyID (..))
import Cardano.Ledger.SafeHash (SafeHash, extractHash, hashAnnotated)
import qualified Cardano.Ledger.Shelley.HardForks as HardForks
import Cardano.Ledger.Shelley.TxBody
( DCert (..),
DelegCert (..),
Expand Down Expand Up @@ -131,7 +130,6 @@ import Data.Coders
(!>),
(<!),
)
import Data.Fixed (HasResolution (resolution))
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe)
Expand All @@ -142,7 +140,6 @@ import Data.Time.Clock (nominalDiffTimeToSeconds)
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import Data.Typeable (Proxy (..), Typeable)
import GHC.Generics (Generic)
import GHC.Records (HasField (..))
import Lens.Micro
import NoThunks.Class (NoThunks)
import Numeric.Natural (Natural)
Expand Down Expand Up @@ -267,42 +264,30 @@ transTxOutAddr txOut = do
Nothing -> transAddr (txOut ^. addrTxOutL)

slotToPOSIXTime ::
HasField "_protocolVersion" (PParams era) ProtVer =>
PParams era ->
EpochInfo (Either Text) ->
SystemStart ->
SlotNo ->
Either Text PV1.POSIXTime
slotToPOSIXTime pp ei sysS s = do
PV1.POSIXTime . transTime . nominalDiffTimeToSeconds . utcTimeToPOSIXSeconds
slotToPOSIXTime ei sysS s = do
PV1.POSIXTime . (truncate . (* 1000)) . nominalDiffTimeToSeconds . utcTimeToPOSIXSeconds
<$> epochInfoSlotToUTCTime ei sysS s
where
transTime =
if HardForks.translateTimeForPlutusScripts pp
then
truncate
-- Convert to milliseconds
. (* 1000)
else resolution

-- | translate a validity interval to POSIX time
transVITime ::
HasField "_protocolVersion" (PParams era) ProtVer =>
PParams era ->
EpochInfo (Either Text) ->
SystemStart ->
ValidityInterval ->
Either Text PV1.POSIXTimeRange
transVITime _ _ _ (ValidityInterval SNothing SNothing) = pure PV1.always
transVITime pp ei sysS (ValidityInterval (SJust i) SNothing) = do
t <- slotToPOSIXTime pp ei sysS i
transVITime _ _ (ValidityInterval SNothing SNothing) = pure PV1.always
transVITime ei sysS (ValidityInterval (SJust i) SNothing) = do
t <- slotToPOSIXTime ei sysS i
pure $ PV1.from t
transVITime pp ei sysS (ValidityInterval SNothing (SJust i)) = do
t <- slotToPOSIXTime pp ei sysS i
transVITime ei sysS (ValidityInterval SNothing (SJust i)) = do
t <- slotToPOSIXTime ei sysS i
pure $ PV1.to t
transVITime pp ei sysS (ValidityInterval (SJust i) (SJust j)) = do
t1 <- slotToPOSIXTime pp ei sysS i
t2 <- slotToPOSIXTime pp ei sysS j
transVITime ei sysS (ValidityInterval (SJust i) (SJust j)) = do
t1 <- slotToPOSIXTime ei sysS i
t2 <- slotToPOSIXTime ei sysS j
pure $
PV1.Interval
(PV1.lowerBound t1)
Expand Down Expand Up @@ -440,7 +425,6 @@ class ExtendedUTxO era where
-- Compute a Digest of the current transaction to pass to the script
-- This is the major component of the valContext function.
txInfo ::
PParams era ->
Language ->
EpochInfo (Either Text) ->
SystemStart ->
Expand Down Expand Up @@ -473,18 +457,16 @@ alonzoTxInfo ::
( EraTx era,
AlonzoEraTxBody era,
Value era ~ MaryValue (EraCrypto era),
TxWits era ~ AlonzoTxWits era,
HasField "_protocolVersion" (PParams era) ProtVer
TxWits era ~ AlonzoTxWits era
) =>
PParams era ->
Language ->
EpochInfo (Either Text) ->
SystemStart ->
UTxO era ->
Tx era ->
Either (TranslationError (EraCrypto era)) VersionedTxInfo
alonzoTxInfo pp lang ei sysS utxo tx = do
timeRange <- left TimeTranslationPastHorizon $ transVITime pp ei sysS interval
alonzoTxInfo lang ei sysS utxo tx = do
timeRange <- left TimeTranslationPastHorizon $ transVITime ei sysS interval
-- We need to do this as a separate step
let lookupTxOut txIn =
case Map.lookup txIn (unUTxO utxo) of
Expand Down
1 change: 0 additions & 1 deletion eras/alonzo/test-suite/cardano-ledger-alonzo-test.cabal
Expand Up @@ -112,7 +112,6 @@ test-suite cardano-ledger-alonzo-test
cardano-slotting,
cborg,
containers,
data-default-class,
plutus-ledger-api:{plutus-ledger-api,plutus-ledger-api-testlib} ^>=1.1,
QuickCheck,
small-steps,
Expand Down
Expand Up @@ -20,7 +20,6 @@ import qualified Cardano.Ledger.Val as Val
import Cardano.Slotting.EpochInfo (EpochInfo, fixedEpochInfo)
import Cardano.Slotting.Slot (EpochSize (..))
import Cardano.Slotting.Time (SystemStart (..), mkSlotLength)
import Data.Default.Class (def)
import qualified Data.Map.Strict as Map
import qualified Data.Sequence.Strict as StrictSeq
import qualified Data.Set as Set
Expand Down Expand Up @@ -88,15 +87,15 @@ silentlyIgnore tx =
Right _ -> pure ()
Left e -> assertFailure $ "no translation error was expected, but got: " <> show e
where
ctx = txInfo def PlutusV1 ei ss utxo tx
ctx = txInfo PlutusV1 ei ss utxo tx

expectTranslationError :: Language -> Tx Alonzo -> TranslationError StandardCrypto -> Assertion
expectTranslationError lang tx expected =
case ctx of
Right _ -> error "This translation was expected to fail, but it succeeded."
Left e -> e @?= expected
where
ctx = txInfo def lang ei ss utxo tx
ctx = txInfo lang ei ss utxo tx

txInfoTests :: TestTree
txInfoTests =
Expand Down
11 changes: 4 additions & 7 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/TxInfo.hs
Expand Up @@ -27,7 +27,7 @@ import Cardano.Ledger.Babbage.TxBody
ShelleyEraTxBody (..),
ShelleyMAEraTxBody (..),
)
import Cardano.Ledger.BaseTypes (ProtVer (..), StrictMaybe (..), isSJust)
import Cardano.Ledger.BaseTypes (StrictMaybe (..), isSJust)
import Cardano.Ledger.Core hiding (TranslationError)
import Cardano.Ledger.Mary.Value (MaryValue (..))
import Cardano.Ledger.SafeHash (hashAnnotated)
Expand All @@ -41,7 +41,6 @@ import Control.Monad (unless, when, zipWithM)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Data.Text (Text)
import GHC.Records (HasField (..))
import Lens.Micro
import qualified PlutusLedgerApi.V1 as PV1
import PlutusLedgerApi.V1.Contexts ()
Expand Down Expand Up @@ -164,18 +163,16 @@ babbageTxInfo ::
( EraTx era,
BabbageEraTxBody era,
Value era ~ MaryValue (EraCrypto era),
TxWits era ~ AlonzoTxWits era,
HasField "_protocolVersion" (PParams era) ProtVer
TxWits era ~ AlonzoTxWits era
) =>
PParams era ->
Language ->
EpochInfo (Either Text) ->
SystemStart ->
UTxO era ->
Tx era ->
Either (TranslationError (EraCrypto era)) VersionedTxInfo
babbageTxInfo pp lang ei sysS utxo tx = do
timeRange <- left TimeTranslationPastHorizon $ Alonzo.transVITime pp ei sysS interval
babbageTxInfo lang ei sysS utxo tx = do
timeRange <- left TimeTranslationPastHorizon $ Alonzo.transVITime ei sysS interval
case lang of
PlutusV1 -> do
let refInputs = txBody ^. referenceInputsTxBodyL
Expand Down
1 change: 0 additions & 1 deletion eras/babbage/test-suite/cardano-ledger-babbage-test.cabal
Expand Up @@ -93,7 +93,6 @@ test-suite cardano-ledger-babbage-test
cardano-slotting,
cborg,
containers,
data-default-class,
plutus-ledger-api,
cardano-strict-containers,
tasty,
Expand Down
Expand Up @@ -27,7 +27,6 @@ import qualified Cardano.Ledger.Val as Val
import Cardano.Slotting.EpochInfo (EpochInfo, fixedEpochInfo)
import Cardano.Slotting.Slot (EpochSize (..))
import Cardano.Slotting.Time (SystemStart (..), mkSlotLength)
import Data.Default.Class (def)
import qualified Data.Map.Strict as Map
import qualified Data.Sequence.Strict as StrictSeq
import qualified Data.Set as Set
Expand Down Expand Up @@ -147,7 +146,7 @@ successfulTranslation lang tx f =
Right info -> assertBool "unexpected transaction info" (f info)
Left e -> assertFailure $ "no translation error was expected, but got: " <> show e
where
ctx = txInfo def lang ei ss utxo tx
ctx = txInfo lang ei ss utxo tx

successfulV2Translation :: AlonzoTx Babbage -> (VersionedTxInfo -> Bool) -> Assertion
successfulV2Translation = successfulTranslation PlutusV2
Expand All @@ -158,7 +157,7 @@ expectTranslationError lang tx expected =
Right _ -> assertFailure "This translation was expected to fail, but it succeeded."
Left e -> e @?= expected
where
ctx = txInfo def lang ei ss utxo tx
ctx = txInfo lang ei ss utxo tx

expectV1TranslationError :: AlonzoTx Babbage -> TranslationError StandardCrypto -> Assertion
expectV1TranslationError = expectTranslationError PlutusV1
Expand Down
9 changes: 0 additions & 9 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/HardForks.hs
Expand Up @@ -7,7 +7,6 @@ module Cardano.Ledger.Shelley.HardForks
allowMIRTransfer,
validatePoolRewardAccountNetID,
allowScriptStakeCredsToEarnRewards,
translateTimeForPlutusScripts,
missingScriptsSymmetricDifference,
forgoRewardPrefilter,
allowOutsideForecastTTL,
Expand Down Expand Up @@ -52,14 +51,6 @@ allowScriptStakeCredsToEarnRewards ::
Bool
allowScriptStakeCredsToEarnRewards pp = pvMajor (getField @"_protocolVersion" pp) > 4

-- | Starting with protocol version 6, we translate slots to time correctly for
-- Plutus scripts.
translateTimeForPlutusScripts ::
(HasField "_protocolVersion" pp ProtVer) =>
pp ->
Bool
translateTimeForPlutusScripts pp = pvMajor (getField @"_protocolVersion" pp) > 5

-- | Starting with protocol version 7, the UTXO rule predicate failure
-- MissingScriptWitnessesUTXOW will not be used for extraneous scripts
missingScriptsSymmetricDifference ::
Expand Down
Expand Up @@ -102,7 +102,6 @@ collectTwoPhaseScriptInputsOutputOrdering =
( fromRight (error "translation error") $
getTxInfo
apf
(pp apf)
PlutusV1
testEpochInfo
testSystemStart
Expand Down Expand Up @@ -177,7 +176,6 @@ collectInputs x = error ("collectInputs Not defined in era " ++ show x)

getTxInfo ::
Proof era ->
PParams era ->
Language ->
EpochInfo (Either Text) ->
SystemStart ->
Expand Down

0 comments on commit 38e2f80

Please sign in to comment.