Skip to content

Commit

Permalink
Start adding cardano keys to initTx from bottom-up
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo committed Oct 28, 2021
1 parent 08832f2 commit e1a0c10
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/Chain/Direct.hs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ txSubmissionClient tracer queue callback headState TinyWallet{getUtxo, sign, cov
InitTx p -> do
txIns <- keys <$> getUtxo
case txIns of
(seedInput : _) -> pure . Just $ initTx p seedInput
(seedInput : _) -> pure . Just $ initTx undefined p seedInput
[] -> error "cannot find a seed input to pass to Init transaction"
AbortTx _utxo -> do
readTVar headState >>= \case
Expand Down
17 changes: 12 additions & 5 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ policyId :: MintingPolicyHash

-- | Create the init transaction from some 'HeadParameters' and a single TxIn
-- which will be used as unique parameter for minting NFTs.
initTx :: HeadParameters -> TxIn StandardCrypto -> ValidatedTx Era
initTx HeadParameters{contestationPeriod, parties} txIn =
initTx ::
-- | Participant's cardano public keys.
[VerificationKey] ->
HeadParameters ->
TxIn StandardCrypto ->
ValidatedTx Era
initTx cardanoKeys HeadParameters{contestationPeriod, parties} txIn =
mkUnsignedTx body dats (Redeemers mempty) mempty
where
body =
Expand Down Expand Up @@ -138,9 +143,9 @@ initTx HeadParameters{contestationPeriod, parties} txIn =
(contestationPeriodFromDiffTime contestationPeriod)
(map (partyFromVerKey . vkey) parties)

initials = map mkInitial parties
initials = map mkInitial cardanoKeys

mkInitial party = TxOut @Era initialAddress initialValue (SJust $ initialDatumHash party)
mkInitial = TxOut @Era initialAddress initialValue . SJust . initialDatumHash

initialAddress = scriptAddr $ plutusScript MockInitial.validatorScript

Expand All @@ -149,7 +154,9 @@ initTx HeadParameters{contestationPeriod, parties} txIn =

initialDatumHash = hashData @Era . initialDatum

initialDatum _party = error "undefined"
initialDatum vkey =
let pubKeyHash = transKeyHash $ hashKey @StandardCrypto $ VKey vkey
in Data . toData $ MockInitial.datum pubKeyHash

-- | Craft a commit transaction which includes the "committed" utxo as a datum.
-- TODO(SN): Eventually, this might not be necessary as the 'Utxo tx' would need
Expand Down
20 changes: 10 additions & 10 deletions hydra-node/test/Hydra/Chain/Direct/TxSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,36 @@ spec =
-- NOTE(SN): We are relying in the inclusion of the datum in the "posting
-- tx" in order to 'observeTx'. This test is here to make this a bit more
-- explicit than the above general property.
prop "contains HeadParameters as datums" $ \txIn params ->
let ValidatedTx{wits} = initTx params txIn
prop "contains HeadParameters as datums" $ \txIn params cardanoKeys ->
let ValidatedTx{wits} = initTx cardanoKeys params txIn
dats = txdats wits
HeadParameters{contestationPeriod, parties} = params
onChainPeriod = contestationPeriodFromDiffTime contestationPeriod
onChainParties = map (partyFromVerKey . vkey) parties
datum = Head.Initial onChainPeriod onChainParties
in Map.elems (unTxDats dats) === [Data . toData $ toBuiltinData datum]

prop "is observed" $ \txIn cperiod (party :| parties) ->
prop "is observed" $ \txIn cperiod (party :| parties) cardanoKeys ->
let params = HeadParameters cperiod (party : parties)
tx = initTx params txIn
tx = initTx cardanoKeys params txIn
observed = observeInitTx @SimpleTx party tx
in case observed of
Just (octx, _) -> octx === OnInitTx cperiod (party : parties)
_ -> property False
& counterexample ("Observed: " <> show observed)

prop "is not observed if not invited" $ \txIn cperiod (NonEmpty parties) ->
prop "is not observed if not invited" $ \txIn cperiod (NonEmpty parties) cardanoKeys ->
forAll (elements parties) $ \notInvited ->
let invited = nub parties \\ [notInvited]
tx = initTx (HeadParameters cperiod invited) txIn
tx = initTx cardanoKeys (HeadParameters cperiod invited) txIn
in isNothing (observeInitTx notInvited tx)
& counterexample ("observing as: " <> show notInvited)
& counterexample ("invited: " <> show invited)

prop "updates on-chain state to 'Initial'" $ \txIn cperiod (party :| others) ->
prop "updates on-chain state to 'Initial'" $ \txIn cperiod (party :| others) cardanoKeys ->
let params = HeadParameters cperiod parties
parties = party : others
tx = initTx params txIn
tx = initTx cardanoKeys params txIn
res = observeInitTx @SimpleTx party tx
in case res of
Just (_, Initial{initials, threadOutput = (_txin, _txout, tt, ps)}) ->
Expand Down Expand Up @@ -176,8 +176,8 @@ spec =
& counterexample ("Input utxo: " <> show utxo)

prop "cover fee correctly handles redeemers" $
withMaxSuccess 60 $ \txIn walletUtxo params (NonEmpty initials) ->
let ValidatedTx{body = initTxBody} = initTx params txIn
withMaxSuccess 60 $ \txIn walletUtxo params (NonEmpty initials) cardanoKeys ->
let ValidatedTx{body = initTxBody} = initTx cardanoKeys params txIn
txInitIn = TxIn (TxId $ SafeHash.hashAnnotated initTxBody) 0
-- FIXME(AB): fromJust is partial
txInitOut = fromJust $ Seq.lookup 0 (outputs initTxBody)
Expand Down

0 comments on commit e1a0c10

Please sign in to comment.