Skip to content

Commit

Permalink
Fix incorrect handling of timelocks in impAllegraSatisfyNativeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
neilmayhew committed Nov 18, 2024
1 parent 26fab88 commit 521f1c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
18 changes: 11 additions & 7 deletions eras/allegra/impl/testlib/Test/Cardano/Ledger/Allegra/ImpTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Cardano.Ledger.Allegra (AllegraEra)
import Cardano.Ledger.Allegra.Core
import Cardano.Ledger.Allegra.Scripts (
AllegraEraScript,
evalTimelock,
pattern RequireTimeExpire,
pattern RequireTimeStart,
)
Expand Down Expand Up @@ -55,15 +56,18 @@ instance
fixupTx = shelleyFixupTx

impAllegraSatisfyNativeScript ::
AllegraEraScript era =>
( AllegraEraScript era
, AllegraEraTxBody era
) =>
Set.Set (KeyHash 'Witness (EraCrypto era)) ->
TxBody era ->
NativeScript era ->
ImpTestM era (Maybe (Map.Map (KeyHash 'Witness (EraCrypto era)) (KeyPair 'Witness (EraCrypto era))))
impAllegraSatisfyNativeScript providedVKeyHashes script = do
impAllegraSatisfyNativeScript providedVKeyHashes txBody script = do
impState <- get
let
keyPairs = impState ^. impKeyPairsG
prevSlotNo = impState ^. impLastTickG
vi = txBody ^. vldtTxBodyL
satisfyMOf m Empty
| m <= 0 = Just mempty
| otherwise = Nothing
Expand All @@ -82,10 +86,10 @@ impAllegraSatisfyNativeScript providedVKeyHashes script = do
RequireAllOf ss -> satisfyMOf (length ss) ss
RequireAnyOf ss -> satisfyMOf 1 ss
RequireMOf m ss -> satisfyMOf m ss
RequireTimeExpire slotNo
| slotNo < prevSlotNo -> Just mempty
lock@(RequireTimeStart _)
| evalTimelock mempty vi lock -> Just mempty
| otherwise -> Nothing
RequireTimeStart slotNo
| slotNo > prevSlotNo -> Just mempty
lock@(RequireTimeExpire _)
| evalTimelock mempty vi lock -> Just mempty
| otherwise -> Nothing
pure $ satisfyScript script
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ class
impSatisfyNativeScript ::
-- | Set of Witnesses that have already been satisfied
Set.Set (KeyHash 'Witness (EraCrypto era)) ->
-- | The transaction body that the script will be applied to
TxBody era ->
NativeScript era ->
ImpTestM era (Maybe (Map (KeyHash 'Witness (EraCrypto era)) (KeyPair 'Witness (EraCrypto era))))

Expand Down Expand Up @@ -714,7 +716,7 @@ instance
startEpochNo = impEraStartEpochNo @(ShelleyEra c)
pure $ translateToShelleyLedgerStateFromUtxo transContext startEpochNo Byron.empty

impSatisfyNativeScript providedVKeyHashes script = do
impSatisfyNativeScript providedVKeyHashes _txBody script = do
keyPairs <- gets impKeyPairs
let
satisfyMOf m Empty
Expand Down Expand Up @@ -863,7 +865,8 @@ updateAddrTxWits tx = impAnn "updateAddrTxWits" $ do
addrWitHashes = curAddrWitHashes <> Set.map witVKeyHash extraAddrVKeyWits
-- Shelley Based Native Script Witnesses
scriptsRequired <- impNativeScriptsRequired tx
nativeScriptsKeyPairs <- mapM (impSatisfyNativeScript addrWitHashes) (Map.elems scriptsRequired)
nativeScriptsKeyPairs <-
mapM (impSatisfyNativeScript addrWitHashes txBody) (Map.elems scriptsRequired)
let extraNativeScriptVKeyWits =
mkWitnessesVKey txBodyHash $ Map.elems (mconcat (catMaybes nativeScriptsKeyPairs))
-- Byron Based Witessed
Expand Down Expand Up @@ -903,7 +906,7 @@ impNativeScriptKeyPairs tx = do
scriptsRequired <- impNativeScriptsRequired tx
let nativeScripts = Map.elems scriptsRequired
curAddrWits = Set.map witVKeyHash $ tx ^. witsTxL . addrTxWitsL
keyPairs <- mapM (impSatisfyNativeScript curAddrWits) nativeScripts
keyPairs <- mapM (impSatisfyNativeScript curAddrWits $ tx ^. bodyTxL) nativeScripts
pure . mconcat $ catMaybes keyPairs

fixupTxOuts :: (ShelleyEraImp era, HasCallStack) => Tx era -> ImpTestM era (Tx era)
Expand Down

0 comments on commit 521f1c4

Please sign in to comment.