Skip to content

Commit

Permalink
Use rdptr instead of direct constructor
Browse files Browse the repository at this point in the history
The redeemers are indexed to the input they apply to, assuming the
inputs are sorted by (TxId, TxIx), so we cannot naively use a counter
to build those. The rdptr function retrieves the correct index for the
pointer.
  • Loading branch information
abailly-iohk committed Oct 12, 2021
1 parent f649159 commit 0063017
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Expand Up @@ -15,10 +15,10 @@ import Cardano.Binary (serialize)
import Cardano.Ledger.Address (Addr (Addr))
import Cardano.Ledger.Alonzo (AlonzoEra, Script)
import Cardano.Ledger.Alonzo.Data (Data (Data), hashData)
import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), Script (PlutusScript), Tag (Spend))
import Cardano.Ledger.Alonzo.Tx (IsValid (IsValid), ValidatedTx (..))
import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), Script (PlutusScript))
import Cardano.Ledger.Alonzo.Tx (IsValid (IsValid), ScriptPurpose (Spending), ValidatedTx (..), rdptr)
import Cardano.Ledger.Alonzo.TxBody (TxBody (..), TxOut (TxOut))
import Cardano.Ledger.Alonzo.TxWitness (RdmrPtr (RdmrPtr), Redeemers (..), TxDats (..), TxWitness (..), unTxDats)
import Cardano.Ledger.Alonzo.TxWitness (RdmrPtr, Redeemers (..), TxDats (..), TxWitness (..), unTxDats)
import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Era (hashScript)
import qualified Cardano.Ledger.SafeHash as SafeHash
Expand Down Expand Up @@ -154,16 +154,20 @@ abortTx (txIn, token, HeadParameters{contestationPeriod, parties}) initInputs =
-- TODO(SN): dummy exUnits, balancing overrides them?
redeemers =
Map.fromList $
(RdmrPtr Spend 0, (headRedeemer, ExUnits 0 0)) :
initialRedeemers
foldl' hasRdmrPtr [] $
(rdptr body (Spending txIn), (headRedeemer, ExUnits 0 0)) :
initialRedeemers

hasRdmrPtr acc = \case
(SNothing, _) -> acc
(SJust v, ex) -> (v, ex) : acc

headRedeemer = Data $ toData Head.Abort

initialRedeemers =
zipWith
(\rd n -> (RdmrPtr Spend n, (rd, ExUnits 0 0)))
(map (const $ Data $ toData ()) initInputs)
[1 ..]
map
(\(txin, _) -> (rdptr body (Spending txin), (Data $ toData (), ExUnits 0 0)))
initInputs

scripts = Map.fromList $ map (\s -> (hashScript @Era s, s)) [initialScript, headScript]

Expand Down
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/Chain/Direct/TxSpec.hs
Expand Up @@ -101,7 +101,7 @@ spec =
utxo = UTxO $ Map.fromList $ (txIn, txOut) : map toTxOut initials

results = validateTxScriptsUnlimited tx utxo
in 1 == length (rights $ Map.elems results)
in 1 + length initials == length (rights $ Map.elems results)
& counterexample ("Evaluation results: " <> show results)
& counterexample ("Tx: " <> show tx)
& counterexample ("Input utxo: " <> show utxo)
Expand Down

0 comments on commit 0063017

Please sign in to comment.