Skip to content

Commit

Permalink
Use observed contestationDeadline from OnContestTx
Browse files Browse the repository at this point in the history
This also adds HeadContested state changed event to update the
contestation deadline in the HeadState. By updating it on contest
observations, the fanout transaction creation will use the right correct
deadline and passes tests.
  • Loading branch information
ch1bo committed Feb 20, 2024
1 parent 1f0cb79 commit c9f0e72
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
39 changes: 25 additions & 14 deletions hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -621,20 +621,26 @@ onOpenChainCloseTx openState newChainState closedSnapshotNumber contestationDead
-- __Transition__: 'ClosedState' → 'ClosedState'
onClosedChainContestTx ::
ClosedState tx ->
-- | New chain state.
ChainStateType tx ->
SnapshotNumber ->
-- | Contestation deadline.
UTCTime ->
Outcome tx
onClosedChainContestTx closedState snapshotNumber
| snapshotNumber < number (getSnapshot confirmedSnapshot) =
Effects
[ ClientEffect ServerOutput.HeadIsContested{snapshotNumber, headId}
, OnChainEffect{postChainTx = ContestTx{headId, headParameters, confirmedSnapshot}}
]
| snapshotNumber > number (getSnapshot confirmedSnapshot) =
-- TODO: A more recent snapshot number was succesfully contested, we will
-- not be able to fanout! We might want to communicate that to the client!
Effects [ClientEffect ServerOutput.HeadIsContested{snapshotNumber, headId}]
| otherwise =
Effects [ClientEffect ServerOutput.HeadIsContested{snapshotNumber, headId}]
onClosedChainContestTx closedState newChainState snapshotNumber contestationDeadline =
StateChanged HeadContested{chainState = newChainState, contestationDeadline}
<> if
| snapshotNumber < number (getSnapshot confirmedSnapshot) ->
Effects
[ ClientEffect ServerOutput.HeadIsContested{snapshotNumber, headId}
, OnChainEffect{postChainTx = ContestTx{headId, headParameters, confirmedSnapshot}}
]
| snapshotNumber > number (getSnapshot confirmedSnapshot) ->
-- TODO: A more recent snapshot number was succesfully contested, we will
-- not be able to fanout! We might want to communicate that to the client!
Effects [ClientEffect ServerOutput.HeadIsContested{snapshotNumber, headId}]
| otherwise ->
Effects [ClientEffect ServerOutput.HeadIsContested{snapshotNumber, headId}]
where
ClosedState{parameters = headParameters, confirmedSnapshot, headId} = closedState

Expand Down Expand Up @@ -734,9 +740,9 @@ update env ledger st ev = case (st, ev) of
(Open{}, PostTxError{postChainTx = CollectComTx{}}) ->
Effects []
-- Closed
(Closed closedState@ClosedState{headId = ourHeadId}, OnChainEvent Observation{observedTx = OnContestTx{headId, snapshotNumber}})
(Closed closedState@ClosedState{headId = ourHeadId}, OnChainEvent Observation{observedTx = OnContestTx{headId, snapshotNumber, contestationDeadline}, newChainState})
| ourHeadId == headId ->
onClosedChainContestTx closedState snapshotNumber
onClosedChainContestTx closedState newChainState snapshotNumber contestationDeadline
| otherwise ->
Error NotOurHead{ourHeadId, otherHeadId = headId}
(Closed ClosedState{contestationDeadline, readyToFanoutSent, headId}, OnChainEvent Tick{chainTime})
Expand Down Expand Up @@ -883,6 +889,10 @@ aggregate st = \case
, headSeed
}
_otherState -> st
HeadContested{chainState, contestationDeadline} ->
case st of
Closed c -> Closed c{chainState, contestationDeadline}
_otherState -> st
HeadFannedOut{chainState} ->
case st of
Closed _ ->
Expand Down Expand Up @@ -993,6 +1003,7 @@ recoverChainStateHistory initialChainState =
PartySignedSnapshot{} -> history
SnapshotConfirmed{} -> history
HeadClosed{chainState} -> pushNewState chainState history
HeadContested{chainState} -> pushNewState chainState history
HeadIsReadyToFanout -> history
HeadFannedOut{chainState} -> pushNewState chainState history
ChainRolledBack{chainState} ->
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 @@ -75,6 +75,7 @@ data StateChanged tx
| PartySignedSnapshot {snapshot :: Snapshot tx, party :: Party, signature :: Signature (Snapshot tx)}
| SnapshotConfirmed {snapshot :: Snapshot tx, signatures :: MultiSignature (Snapshot tx)}
| HeadClosed {chainState :: ChainStateType tx, contestationDeadline :: UTCTime}
| HeadContested {chainState :: ChainStateType tx, contestationDeadline :: UTCTime}
| HeadIsReadyToFanout
| HeadFannedOut {chainState :: ChainStateType tx}
| ChainRolledBack {chainState :: ChainStateType tx}
Expand Down
3 changes: 3 additions & 0 deletions hydra-node/src/Hydra/HeadLogic/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ instance Arbitrary Environment where
-- Note that rollbacks are currently not fully handled in the head logic and
-- only this internal chain state gets replaced with the "rolled back to"
-- version.
--
-- XXX: chainState would actualy not be needed in the HeadState anymore as we do
-- not persist the 'HeadState' and not access it in the HeadLogic either.
data HeadState tx
= Idle (IdleState tx)
| Initial (InitialState tx)
Expand Down

0 comments on commit c9f0e72

Please sign in to comment.