Skip to content

Commit

Permalink
Wait on applicable decommit tx
Browse files Browse the repository at this point in the history
If there is another decommit _in flight_ wait for TTL to expire before
erroring out.

Also remove the red color from the spec
  • Loading branch information
v0d1ch committed May 7, 2024
1 parent 384f923 commit cc883ed
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
38 changes: 22 additions & 16 deletions hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -684,25 +684,31 @@ onOpenClientDecommit env headId ledger currentSlot coordinatedHeadState decommit
onOpenNetworkReqDec ::
IsTx tx =>
Environment ->
TTL ->
OpenState tx ->
tx ->
Outcome tx
onOpenNetworkReqDec env openState decommitTx =
case mExistingDecommitTx of
Just existingDecommitTx ->
Error $ RequireFailed $ DecommitTxInFlight{decommitTx = existingDecommitTx}
Nothing ->
let decommitUTxO = utxoFromTx decommitTx
in newState (DecommitRecorded decommitTx)
<> causes
[ ClientEffect $ ServerOutput.DecommitRequested headId decommitUTxO
]
<> if isLeader parameters party nextSn
then
causes
[NetworkEffect (ReqSn nextSn (txId <$> localTxs) (Just decommitTx))]
else Error $ RequireFailed $ ReqSnNotLeader{requestedSn = nextSn, leader = party}
onOpenNetworkReqDec env ttl openState decommitTx =
waitOnApplicableDecommit $
let decommitUTxO = utxoFromTx decommitTx
in StateChanged (DecommitRecorded decommitTx)
<> Effects
[ ClientEffect $ ServerOutput.DecommitRequested headId decommitUTxO
]
<> if isLeader parameters party nextSn
then
Effects
[NetworkEffect (ReqSn nextSn (txId <$> localTxs) (Just decommitTx))]
else Error $ RequireFailed $ ReqSnNotLeader{requestedSn = nextSn, leader = party}
where
waitOnApplicableDecommit cont =
case mExistingDecommitTx of
Nothing -> cont
Just existingDecommitTx
| ttl > 0 ->
Wait $ WaitOnNotApplicableDecommitTx decommitTx
| otherwise ->
Error $ RequireFailed $ DecommitTxInFlight{decommitTx = existingDecommitTx}
Environment{party} = env

Snapshot{number} = getSnapshot confirmedSnapshot
Expand Down Expand Up @@ -906,7 +912,7 @@ update env ledger st ev = case (st, ev) of
(Open OpenState{headId, coordinatedHeadState, currentSlot}, ClientInput Decommit{decommitTx}) -> do
onOpenClientDecommit env headId ledger currentSlot coordinatedHeadState decommitTx
(Open openState, NetworkInput _ _ (ReqDec{transaction})) ->
onOpenNetworkReqDec env openState transaction
onOpenNetworkReqDec env ttl openState transaction
( Open OpenState{headId = ourHeadId}
, ChainInput Observation{observedTx = OnDecrementTx{headId}}
)
Expand Down
1 change: 1 addition & 0 deletions hydra-node/src/Hydra/HeadLogic/Outcome.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ data WaitReason tx
| WaitOnSeenSnapshot
| WaitOnTxs {waitingForTxIds :: [TxIdType tx]}
| WaitOnContestationDeadline
| WaitOnNotApplicableDecommitTx { waitingOnDecommitTx :: tx}
deriving stock (Generic)

deriving stock instance IsTx tx => Eq (WaitReason tx)
Expand Down
6 changes: 3 additions & 3 deletions hydra-node/test/Hydra/HeadLogicSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ spec =
NetworkEffect reqDec' -> reqDec' == reqDec
_ -> False

it "cannot request decommit when another one is in flight" $ do
it "wait for second decommit when another one is in flight" $ do
let decommitTx1 = SimpleTx 1 mempty (utxoRef 1)
decommitTx2 = SimpleTx 2 mempty (utxoRef 2)
reqDec1 = ReqDec{transaction = decommitTx1, decommitRequester = alice}
Expand All @@ -174,8 +174,8 @@ spec =
let outcome = update bobEnv ledger s1 reqDecEvent2

outcome `shouldSatisfy` \case
Error (RequireFailed DecommitTxInFlight{decommitTx = decommitTx''}) ->
decommitTx1 == decommitTx''
Wait (WaitOnNotApplicableDecommitTx{waitingOnDecommitTx = decommitTx''}) ->
decommitTx2 == decommitTx''
_ -> False

it "updates decommitTx on valid ReqDec" $ do
Expand Down
2 changes: 0 additions & 2 deletions spec/fig_offchain_prot.tex
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@

%%% REC DEC
\On{$(\mathtt{reqDec},\tx)$ from $\party_j$}{
\textcolor{red}{
\Wait{$\tx_\omega = \bot ~ \land ~ \barmU \applytx \tx \not= \bot$}{
\textcolor{black}{
$\tx_\omega \gets \tx$ \;
Expand All @@ -147,7 +146,6 @@
}
}
}
}
}

\vspace{12pt}
Expand Down

0 comments on commit cc883ed

Please sign in to comment.