Skip to content

Commit

Permalink
Serialize commits as CBOR instead of JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Jan 18, 2022
1 parent dd72b36 commit d857425
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Hydra.Chain.Direct.Tx where
import Hydra.Prelude

import Cardano.Api (NetworkId)
import Cardano.Binary (serialize)
import Cardano.Binary (serialize, serialize')
import Cardano.Ledger.Address (Addr (Addr))
import Cardano.Ledger.Alonzo (Script)
import Cardano.Ledger.Alonzo.Data (Data, DataHash, getPlutusData, hashData)
Expand Down Expand Up @@ -53,13 +53,11 @@ import qualified Hydra.Contract.Initial as Initial
import Hydra.Data.ContestationPeriod (contestationPeriodFromDiffTime, contestationPeriodToDiffTime)
import Hydra.Data.Party (partyFromVerKey, partyToVerKey)
import qualified Hydra.Data.Party as OnChain
import Hydra.Data.Utxo (fromByteString)
import qualified Hydra.Data.Utxo as OnChain
import Hydra.Ledger.Cardano (
CardanoTx,
PaymentKey,
Utxo,
Utxo' (Utxo),
VerificationKey (PaymentVerificationKey),
fromLedgerTx,
fromPlutusScript,
Expand Down Expand Up @@ -280,13 +278,11 @@ commitTx networkId party utxo (initialInput, vkh) =

mkCommitDatum :: Party -> Maybe (Api.TxIn, Api.TxOut Api.CtxUTxO Api.Era) -> Plutus.Datum
mkCommitDatum (partyFromVerKey . vkey -> party) utxo =
Commit.datum (party, commitUtxo)
Commit.datum (party, OnChain.Utxo (toBuiltin commitUtxo))
where
commitUtxo = fromByteString $
toStrict $
Aeson.encode $ case utxo of
Nothing -> mempty
Just (i, o) -> Utxo $ Map.singleton i o
commitUtxo = case utxo of
Nothing -> mempty
Just (_, o) -> serialize' (toLedgerTxOut o)

-- | Create a transaction collecting all "committed" utxo and opening a Head,
-- i.e. driving the Head script state.
Expand Down Expand Up @@ -319,6 +315,9 @@ collectComTx networkId utxo (Api.fromLedgerTxIn -> headInput, Api.fromLedgerData
headRedeemer =
Api.mkRedeemerForTxIn $
Head.CollectCom{utxoHash}
-- FIXME: We need to hash tx outs in an order that is recoverable on-chain.
-- The simplest thing to do, is to make sure commit inputs are in the same
-- order as their corresponding committed utxo.
utxoHash = toBuiltin $ hashTxOuts $ toList utxo
headOutput =
Api.TxOut
Expand Down

0 comments on commit d857425

Please sign in to comment.