Skip to content

Commit

Permalink
Align decommit off-chain spec/impl
Browse files Browse the repository at this point in the history
These checks there are really not "require" statements, as they would
not "detect cheating" as described in the specification.
  • Loading branch information
ch1bo committed Mar 7, 2024
1 parent f24c855 commit 159ce16
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 54 deletions.
86 changes: 37 additions & 49 deletions hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -620,24 +620,49 @@ onOpenClientDecommit ::
tx ->
Outcome tx
onOpenClientDecommit env headId ledger currentSlot coordinatedHeadState decommitTx =
case mExistingDecommitTx of
Just existingDecommitTx ->
checkNoDecommitInFlight $
checkValidDecommitTx $
Effects
[ ClientEffect
ServerOutput.DecommitInvalid
{ headId
, decommitInvalidReason = ServerOutput.DecommitAlreadyInFlight{decommitTx = existingDecommitTx}
}
[ NetworkEffect ReqDec{transaction = decommitTx, decommitRequester = party}
]
Nothing ->
requireValidDecommitTx headId ledger currentSlot coordinatedHeadState decommitTx $
where
checkNoDecommitInFlight continue =
case mExistingDecommitTx of
Just existingDecommitTx ->
Effects
[ NetworkEffect ReqDec{transaction = decommitTx, decommitRequester = party}
[ ClientEffect
ServerOutput.DecommitInvalid
{ headId
, decommitInvalidReason = ServerOutput.DecommitAlreadyInFlight{decommitTx = existingDecommitTx}
}
]
where
Environment{party} = env
Nothing -> continue

checkValidDecommitTx cont =
case applyTransactions ledger currentSlot confirmedUTxO [decommitTx] of
Left (_, err) ->
Effects
[ ClientEffect
ServerOutput.DecommitInvalid
{ headId
, decommitInvalidReason =
ServerOutput.DecommitTxInvalid
{ confirmedUTxO
, decommitTx
, validationError = err
}
}
]
Right _ -> cont

confirmedUTxO = (getSnapshot confirmedSnapshot).utxo

CoordinatedHeadState{confirmedSnapshot} = coordinatedHeadState

CoordinatedHeadState{decommitTx = mExistingDecommitTx} = coordinatedHeadState

Environment{party} = env

-- | Process the request 'ReqDec' to decommit something from the Open head.
--
-- __Transition__: 'OpenState' → 'OpenState'
Expand Down Expand Up @@ -1212,40 +1237,3 @@ recoverState ::
t (StateChanged tx) ->
HeadState tx
recoverState = foldl' aggregate

-- Decommit helpers

-- TODO: Spec: require U̅ ◦ decTx /= ⊥

-- | Prevents further evaluation if Decommit tx is not applicable to the local
-- ledger state. In this case emits 'DecommitInvalid' otherwise it alows
-- continuation to proceed.
requireValidDecommitTx ::
Monoid (UTxOType tx) =>
HeadId ->
Ledger tx ->
ChainSlot ->
CoordinatedHeadState tx ->
tx ->
Outcome tx ->
Outcome tx
requireValidDecommitTx headId ledger currentSlot coordinatedHeadState decommitTx cont =
case applyTransactions ledger currentSlot confirmedUTxO [decommitTx] of
Left (_, err) ->
Effects
[ ClientEffect
ServerOutput.DecommitInvalid
{ headId
, decommitInvalidReason =
ServerOutput.DecommitTxInvalid
{ confirmedUTxO
, decommitTx
, validationError = err
}
}
]
Right _ -> cont
where
confirmedUTxO = (getSnapshot confirmedSnapshot).utxo

CoordinatedHeadState{confirmedSnapshot} = coordinatedHeadState
6 changes: 2 additions & 4 deletions spec/fig_offchain_prot.tex
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@

%%% DECOMMIT
\On{$(\mathtt{decommit},\tx)$ from client}{
\textcolor{red}{
% TODO: check U_\omega is bottom to not have multiple in-flight?
\Req{$\barmU \applytx \tx \not= \bot$} \;
\Multi{} $(\mathtt{reqDec},\tx)$%
\If{\textcolor{blue}{$\tx_{\omega} = \bot \land \barmU \applytx \tx \not= \bot$}}{
\Multi{} $(\mathtt{reqDec},\tx)$%
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/prel.tex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ \subsection{Notation}

\noindent Furthermore, given a set $\mathcal{A}$, let
\begin{itemize}
\item $\mathcal{A}^? = \mathcal{A} \cup \Diamond$ denotes an option: a value from $\mathcal{A}$ or no value at all,
\item $\mathcal{A}^? = \mathcal{A} \cup \Diamond$ denotes an option: a value from $\mathcal{A}$ or no value at all indicated by $\bot$,
\item $\mathcal{A}^n$ be the set of all n-sized sequences over $\mathcal{A}$,
\item $\mathcal{A}^! = \bigcup_{i=1}^{n \in \tyNatural} \mathcal{A}^{i}$ be the set of non-empty sequences over $\mathcal{A}$, and
\item $\mathcal{A}^* = \bigcup_{i=0}^{n \in \tyNatural} \mathcal{A}^{i}$ be
Expand Down

0 comments on commit 159ce16

Please sign in to comment.