Skip to content

Commit b06f908

Browse files
committed
Remove stateChangeID from StateChanged and HeadLogic
EventIds are now handled outside of the HeadLogic.
1 parent 8496821 commit b06f908

File tree

6 files changed

+170
-226
lines changed

6 files changed

+170
-226
lines changed

hydra-node/src/Hydra/HeadLogic.hs

+36-53
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ onIdleChainInitTx ::
126126
HeadSeed ->
127127
HeadParameters ->
128128
[OnChainId] ->
129-
Word64 ->
130129
Outcome tx
131-
onIdleChainInitTx env newChainState headId headSeed headParameters participants nextStateChangeID
130+
onIdleChainInitTx env newChainState headId headSeed headParameters participants
132131
| configuredParties == initializedParties
133132
&& party `member` initializedParties
134133
&& configuredContestationPeriod == contestationPeriod
@@ -139,7 +138,6 @@ onIdleChainInitTx env newChainState headId headSeed headParameters participants
139138
, chainState = newChainState
140139
, headId
141140
, headSeed
142-
, stateChangeID = nextStateChangeID
143141
}
144142
<> cause (ClientEffect $ ServerOutput.HeadIsInitializing{headId, parties})
145143
| otherwise =
@@ -179,10 +177,9 @@ onInitialChainCommitTx ::
179177
Party ->
180178
-- | Committed UTxO
181179
UTxOType tx ->
182-
Word64 ->
183180
Outcome tx
184-
onInitialChainCommitTx st newChainState pt utxo nextStateChangeID =
185-
newState CommittedUTxO{party = pt, committedUTxO = utxo, chainState = newChainState, stateChangeID = nextStateChangeID}
181+
onInitialChainCommitTx st newChainState pt utxo =
182+
newState CommittedUTxO{party = pt, committedUTxO = utxo, chainState = newChainState}
186183
<> causes
187184
( notifyClient
188185
: [postCollectCom | canCollectCom]
@@ -231,10 +228,9 @@ onInitialChainAbortTx ::
231228
ChainStateType tx ->
232229
Committed tx ->
233230
HeadId ->
234-
Word64 ->
235231
Outcome tx
236-
onInitialChainAbortTx newChainState committed headId nextStateChangeID =
237-
newState HeadAborted{chainState = newChainState, stateChangeID = nextStateChangeID}
232+
onInitialChainAbortTx newChainState committed headId =
233+
newState HeadAborted{chainState = newChainState}
238234
<> cause (ClientEffect $ ServerOutput.HeadIsAborted{headId, utxo = fold committed})
239235

240236
-- | Observe a collectCom transaction. We initialize the 'OpenState' using the
@@ -247,10 +243,9 @@ onInitialChainCollectTx ::
247243
InitialState tx ->
248244
-- | New chain state
249245
ChainStateType tx ->
250-
Word64 ->
251246
Outcome tx
252-
onInitialChainCollectTx st newChainState nextStateChangeID =
253-
newState HeadOpened{chainState = newChainState, initialUTxO = u0, stateChangeID = nextStateChangeID}
247+
onInitialChainCollectTx st newChainState =
248+
newState HeadOpened{chainState = newChainState, initialUTxO = u0}
254249
<> cause (ClientEffect $ ServerOutput.HeadIsOpen{headId, utxo = u0})
255250
where
256251
u0 = fold committed
@@ -288,24 +283,23 @@ onOpenNetworkReqTx ::
288283
TTL ->
289284
-- | The transaction to be submitted to the head.
290285
tx ->
291-
Word64 ->
292286
Outcome tx
293-
onOpenNetworkReqTx env ledger st ttl tx nextStateChangeID =
287+
onOpenNetworkReqTx env ledger st ttl tx =
294288
-- Spec: Tall ← ̂Tall ∪ { (hash(tx), tx) }
295-
(newState TransactionReceived{tx, stateChangeID = nextStateChangeID} <>) $
289+
(newState TransactionReceived{tx} <>) $
296290
-- Spec: wait L̂ ◦ tx ≠ ⊥ combined with L̂ ← L̂ ◦ tx
297291
waitApplyTx $ \newLocalUTxO ->
298292
-- Spec: if ŝ = s̄ ∧ leader(s̄ + 1) = i
299293
( if not snapshotInFlight && isLeader parameters party nextSn
300294
then
301-
newState TransactionAppliedToLocalUTxO{tx = tx, newLocalUTxO, stateChangeID = nextStateChangeID}
295+
newState TransactionAppliedToLocalUTxO{tx = tx, newLocalUTxO}
302296
<>
303297
-- XXX: This state update has no equivalence in the
304298
-- spec. Do we really need to store that we have
305299
-- requested a snapshot? If yes, should update spec.
306-
newState SnapshotRequestDecided{snapshotNumber = nextSn, stateChangeID = nextStateChangeID}
300+
newState SnapshotRequestDecided{snapshotNumber = nextSn}
307301
<> cause (NetworkEffect $ ReqSn nextSn (txId <$> localTxs'))
308-
else newState TransactionAppliedToLocalUTxO{tx, newLocalUTxO, stateChangeID = nextStateChangeID}
302+
else newState TransactionAppliedToLocalUTxO{tx, newLocalUTxO}
309303
)
310304
<> cause (ClientEffect $ ServerOutput.TxValid headId tx)
311305
where
@@ -371,9 +365,8 @@ onOpenNetworkReqSn ::
371365
SnapshotNumber ->
372366
-- | List of transactions to snapshot.
373367
[TxIdType tx] ->
374-
Word64 ->
375368
Outcome tx
376-
onOpenNetworkReqSn env ledger st otherParty sn requestedTxIds nextStateChangeID =
369+
onOpenNetworkReqSn env ledger st otherParty sn requestedTxIds =
377370
-- TODO: Verify the request is signed by (?) / comes from the leader
378371
-- (Can we prove a message comes from a given peer, without signature?)
379372

@@ -402,7 +395,6 @@ onOpenNetworkReqSn env ledger st otherParty sn requestedTxIds nextStateChangeID
402395
, requestedTxIds
403396
, newLocalUTxO
404397
, newLocalTxs
405-
, stateChangeID = nextStateChangeID
406398
}
407399
where
408400
requireReqSn continue
@@ -484,9 +476,8 @@ onOpenNetworkAckSn ::
484476
Signature (Snapshot tx) ->
485477
-- | Snapshot number of this AckSn.
486478
SnapshotNumber ->
487-
Word64 ->
488479
Outcome tx
489-
onOpenNetworkAckSn Environment{party} openState otherParty snapshotSignature sn nextStateChangeID =
480+
onOpenNetworkAckSn Environment{party} openState otherParty snapshotSignature sn =
490481
-- TODO: verify authenticity of message and whether otherParty is part of the head
491482
-- Spec: require s ∈ {ŝ, ŝ + 1}
492483
requireValidAckSn $ do
@@ -499,7 +490,7 @@ onOpenNetworkAckSn Environment{party} openState otherParty snapshotSignature sn
499490
let multisig = aggregateInOrder sigs' parties
500491
requireVerifiedMultisignature multisig snapshot $
501492
do
502-
newState SnapshotConfirmed{snapshot, signatures = multisig, stateChangeID = nextStateChangeID}
493+
newState SnapshotConfirmed{snapshot, signatures = multisig}
503494
<> cause (ClientEffect $ ServerOutput.SnapshotConfirmed headId snapshot multisig)
504495
& maybeEmitSnapshot
505496
where
@@ -531,7 +522,6 @@ onOpenNetworkAckSn Environment{party} openState otherParty snapshotSignature sn
531522
{ snapshot
532523
, party = otherParty
533524
, signature = snapshotSignature
534-
, stateChangeID = nextStateChangeID
535525
}
536526

537527
requireVerifiedMultisignature multisig msg cont =
@@ -550,7 +540,7 @@ onOpenNetworkAckSn Environment{party} openState otherParty snapshotSignature sn
550540
if isLeader parameters party nextSn && not (null localTxs)
551541
then
552542
outcome
553-
<> newState SnapshotRequestDecided{snapshotNumber = nextSn, stateChangeID = nextStateChangeID}
543+
<> newState SnapshotRequestDecided{snapshotNumber = nextSn}
554544
<> cause (NetworkEffect $ ReqSn nextSn (txId <$> localTxs))
555545
else outcome
556546

@@ -595,10 +585,9 @@ onOpenChainCloseTx ::
595585
SnapshotNumber ->
596586
-- | Contestation deadline.
597587
UTCTime ->
598-
Word64 ->
599588
Outcome tx
600-
onOpenChainCloseTx openState newChainState closedSnapshotNumber contestationDeadline nextStateChangeID =
601-
newState HeadClosed{chainState = newChainState, contestationDeadline, stateChangeID = nextStateChangeID}
589+
onOpenChainCloseTx openState newChainState closedSnapshotNumber contestationDeadline =
590+
newState HeadClosed{chainState = newChainState, contestationDeadline}
602591
<> causes
603592
( notifyClient
604593
: [ OnChainEffect
@@ -634,10 +623,9 @@ onClosedChainContestTx ::
634623
SnapshotNumber ->
635624
-- | Contestation deadline.
636625
UTCTime ->
637-
Word64 ->
638626
Outcome tx
639-
onClosedChainContestTx closedState newChainState snapshotNumber contestationDeadline nextStateChangeID =
640-
newState HeadContested{chainState = newChainState, contestationDeadline, stateChangeID = nextStateChangeID}
627+
onClosedChainContestTx closedState newChainState snapshotNumber contestationDeadline =
628+
newState HeadContested{chainState = newChainState, contestationDeadline}
641629
<> if
642630
| snapshotNumber < number (getSnapshot confirmedSnapshot) ->
643631
cause notifyClients
@@ -681,10 +669,9 @@ onClosedChainFanoutTx ::
681669
ClosedState tx ->
682670
-- | New chain state
683671
ChainStateType tx ->
684-
Word64 ->
685672
Outcome tx
686-
onClosedChainFanoutTx closedState newChainState nextStateChangeID =
687-
newState HeadFannedOut{chainState = newChainState, stateChangeID = nextStateChangeID}
673+
onClosedChainFanoutTx closedState newChainState =
674+
newState HeadFannedOut{chainState = newChainState}
688675
<> cause (ClientEffect $ ServerOutput.HeadIsFinalized{headId, utxo})
689676
where
690677
Snapshot{utxo} = getSnapshot confirmedSnapshot
@@ -698,27 +685,26 @@ update ::
698685
IsChainState tx =>
699686
Environment ->
700687
Ledger tx ->
701-
Word64 ->
702688
-- | Current HeadState to validate the command against.
703689
HeadState tx ->
704690
-- | Input to be processed.
705691
Input tx ->
706692
Outcome tx
707-
update env ledger nextStateChangeID st ev = case (st, ev) of
693+
update env ledger st ev = case (st, ev) of
708694
(Idle _, ClientInput Init) ->
709695
onIdleClientInit env
710696
(Idle _, ChainInput Observation{observedTx = OnInitTx{headId, headSeed, headParameters, participants}, newChainState}) ->
711-
onIdleChainInitTx env newChainState headId headSeed headParameters participants nextStateChangeID
697+
onIdleChainInitTx env newChainState headId headSeed headParameters participants
712698
(Initial initialState@InitialState{headId = ourHeadId}, ChainInput Observation{observedTx = OnCommitTx{headId, party = pt, committed = utxo}, newChainState})
713-
| ourHeadId == headId -> onInitialChainCommitTx initialState newChainState pt utxo nextStateChangeID
699+
| ourHeadId == headId -> onInitialChainCommitTx initialState newChainState pt utxo
714700
| otherwise -> Error NotOurHead{ourHeadId, otherHeadId = headId}
715701
(Initial initialState, ClientInput Abort) ->
716702
onInitialClientAbort initialState
717703
(Initial initialState@InitialState{headId = ourHeadId}, ChainInput Observation{observedTx = OnCollectComTx{headId}, newChainState})
718-
| ourHeadId == headId -> onInitialChainCollectTx initialState newChainState nextStateChangeID
704+
| ourHeadId == headId -> onInitialChainCollectTx initialState newChainState
719705
| otherwise -> Error NotOurHead{ourHeadId, otherHeadId = headId}
720706
(Initial InitialState{headId = ourHeadId, committed}, ChainInput Observation{observedTx = OnAbortTx{headId}, newChainState})
721-
| ourHeadId == headId -> onInitialChainAbortTx newChainState committed headId nextStateChangeID
707+
| ourHeadId == headId -> onInitialChainAbortTx newChainState committed headId
722708
| otherwise -> Error NotOurHead{ourHeadId, otherHeadId = headId}
723709
(Initial InitialState{committed, headId}, ClientInput GetUTxO) ->
724710
cause (ClientEffect . ServerOutput.GetUTxOResponse headId $ fold committed)
@@ -728,18 +714,18 @@ update env ledger nextStateChangeID st ev = case (st, ev) of
728714
(Open{}, ClientInput (NewTx tx)) ->
729715
onOpenClientNewTx tx
730716
(Open openState, NetworkInput ttl _ (ReqTx tx)) ->
731-
onOpenNetworkReqTx env ledger openState ttl tx nextStateChangeID
717+
onOpenNetworkReqTx env ledger openState ttl tx
732718
(Open openState, NetworkInput _ otherParty (ReqSn sn txIds)) ->
733719
-- XXX: ttl == 0 not handled for ReqSn
734-
onOpenNetworkReqSn env ledger openState otherParty sn txIds nextStateChangeID
720+
onOpenNetworkReqSn env ledger openState otherParty sn txIds
735721
(Open openState, NetworkInput _ otherParty (AckSn snapshotSignature sn)) ->
736722
-- XXX: ttl == 0 not handled for AckSn
737-
onOpenNetworkAckSn env openState otherParty snapshotSignature sn nextStateChangeID
723+
onOpenNetworkAckSn env openState otherParty snapshotSignature sn
738724
( Open openState@OpenState{headId = ourHeadId}
739725
, ChainInput Observation{observedTx = OnCloseTx{headId, snapshotNumber = closedSnapshotNumber, contestationDeadline}, newChainState}
740726
)
741727
| ourHeadId == headId ->
742-
onOpenChainCloseTx openState newChainState closedSnapshotNumber contestationDeadline nextStateChangeID
728+
onOpenChainCloseTx openState newChainState closedSnapshotNumber contestationDeadline
743729
| otherwise ->
744730
Error NotOurHead{ourHeadId, otherHeadId = headId}
745731
(Open OpenState{coordinatedHeadState = CoordinatedHeadState{confirmedSnapshot}, headId}, ClientInput GetUTxO) ->
@@ -753,34 +739,31 @@ update env ledger nextStateChangeID st ev = case (st, ev) of
753739
-- Closed
754740
(Closed closedState@ClosedState{headId = ourHeadId}, ChainInput Observation{observedTx = OnContestTx{headId, snapshotNumber, contestationDeadline}, newChainState})
755741
| ourHeadId == headId ->
756-
onClosedChainContestTx closedState newChainState snapshotNumber contestationDeadline nextStateChangeID
742+
onClosedChainContestTx closedState newChainState snapshotNumber contestationDeadline
757743
| otherwise ->
758744
Error NotOurHead{ourHeadId, otherHeadId = headId}
759745
(Closed ClosedState{contestationDeadline, readyToFanoutSent, headId}, ChainInput Tick{chainTime})
760746
| chainTime > contestationDeadline && not readyToFanoutSent ->
761-
newState HeadIsReadyToFanout{stateChangeID = nextStateChangeID}
747+
newState HeadIsReadyToFanout
762748
<> cause (ClientEffect $ ServerOutput.ReadyToFanout headId)
763749
(Closed closedState, ClientInput Fanout) ->
764750
onClosedClientFanout closedState
765751
(Closed closedState@ClosedState{headId = ourHeadId}, ChainInput Observation{observedTx = OnFanoutTx{headId}, newChainState})
766752
| ourHeadId == headId ->
767-
onClosedChainFanoutTx closedState newChainState nextStateChangeID
753+
onClosedChainFanoutTx closedState newChainState
768754
| otherwise ->
769755
Error NotOurHead{ourHeadId, otherHeadId = headId}
770756
-- General
771757
(_, ChainInput Rollback{rolledBackChainState}) ->
772-
newState ChainRolledBack{chainState = rolledBackChainState, stateChangeID}
758+
newState ChainRolledBack{chainState = rolledBackChainState}
773759
(_, ChainInput Tick{chainSlot}) ->
774-
newState TickObserved{chainSlot, stateChangeID}
775-
-- FIXME(Elaine): should this bump the stateChangeID as well?
760+
newState TickObserved{chainSlot}
776761
(_, ChainInput PostTxError{postChainTx, postTxError}) ->
777762
cause . ClientEffect $ ServerOutput.PostTxOnChainFailed{postChainTx, postTxError}
778763
(_, ClientInput{clientInput}) ->
779764
cause . ClientEffect $ ServerOutput.CommandFailed clientInput st
780765
_ ->
781766
Error $ UnhandledInput ev st
782-
where
783-
stateChangeID = nextStateChangeID
784767

785768
-- * HeadState aggregate
786769

hydra-node/src/Hydra/HeadLogic/Outcome.hs

+28-35
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,19 @@ data StateChanged tx
4646
, chainState :: ChainStateType tx
4747
, headId :: HeadId
4848
, headSeed :: HeadSeed
49-
, stateChangeID :: Word64
5049
}
5150
| CommittedUTxO
5251
{ party :: Party
5352
, committedUTxO :: UTxOType tx
5453
, chainState :: ChainStateType tx
55-
, stateChangeID :: Word64
5654
}
57-
| HeadAborted {chainState :: ChainStateType tx, stateChangeID :: Word64}
58-
| HeadOpened {chainState :: ChainStateType tx, initialUTxO :: UTxOType tx, stateChangeID :: Word64}
55+
| HeadAborted {chainState :: ChainStateType tx}
56+
| HeadOpened {chainState :: ChainStateType tx, initialUTxO :: UTxOType tx}
5957
| TransactionAppliedToLocalUTxO
6058
{ tx :: tx
6159
, newLocalUTxO :: UTxOType tx
62-
, stateChangeID :: Word64
6360
}
64-
| SnapshotRequestDecided {snapshotNumber :: SnapshotNumber, stateChangeID :: Word64}
61+
| SnapshotRequestDecided {snapshotNumber :: SnapshotNumber}
6562
| -- | A snapshot was requested by some party.
6663
-- NOTE: We deliberately already include an updated local ledger state to
6764
-- not need a ledger to interpret this event.
@@ -70,22 +67,18 @@ data StateChanged tx
7067
, requestedTxIds :: [TxIdType tx]
7168
, newLocalUTxO :: UTxOType tx
7269
, newLocalTxs :: [tx]
73-
, stateChangeID :: Word64
7470
}
75-
| TransactionReceived {tx :: tx, stateChangeID :: Word64}
76-
| PartySignedSnapshot {snapshot :: Snapshot tx, party :: Party, signature :: Signature (Snapshot tx), stateChangeID :: Word64}
77-
| SnapshotConfirmed {snapshot :: Snapshot tx, signatures :: MultiSignature (Snapshot tx), stateChangeID :: Word64}
78-
| HeadClosed {chainState :: ChainStateType tx, contestationDeadline :: UTCTime, stateChangeID :: Word64}
79-
| HeadContested {chainState :: ChainStateType tx, contestationDeadline :: UTCTime, stateChangeID :: Word64}
80-
| HeadIsReadyToFanout {stateChangeID :: Word64}
81-
| HeadFannedOut {chainState :: ChainStateType tx, stateChangeID :: Word64}
82-
| ChainRolledBack {chainState :: ChainStateType tx, stateChangeID :: Word64}
83-
| TickObserved {chainSlot :: ChainSlot, stateChangeID :: Word64}
71+
| TransactionReceived {tx :: tx}
72+
| PartySignedSnapshot {snapshot :: Snapshot tx, party :: Party, signature :: Signature (Snapshot tx)}
73+
| SnapshotConfirmed {snapshot :: Snapshot tx, signatures :: MultiSignature (Snapshot tx)}
74+
| HeadClosed {chainState :: ChainStateType tx, contestationDeadline :: UTCTime}
75+
| HeadContested {chainState :: ChainStateType tx, contestationDeadline :: UTCTime}
76+
| HeadIsReadyToFanout
77+
| HeadFannedOut {chainState :: ChainStateType tx}
78+
| ChainRolledBack {chainState :: ChainStateType tx}
79+
| TickObserved {chainSlot :: ChainSlot}
8480
deriving stock (Generic)
8581

86-
-- FIXME(Elaine): these stateChangeID fields were added in an attempt to make every StateChanged keep track of its ID
87-
-- it's not clear how to handle the state for this. but for now the field is kept so that the type of putEvent can be kept simple, and shouldn't do harm
88-
8982
deriving stock instance (IsTx tx, Eq (HeadState tx), Eq (ChainStateType tx)) => Eq (StateChanged tx)
9083
deriving stock instance (IsTx tx, Show (HeadState tx), Show (ChainStateType tx)) => Show (StateChanged tx)
9184
deriving anyclass instance (IsTx tx, ToJSON (ChainStateType tx)) => ToJSON (StateChanged tx)
@@ -97,22 +90,22 @@ instance IsChainState tx => Arbitrary (StateChanged tx) where
9790
genStateChanged :: IsChainState tx => Environment -> Gen (StateChanged tx)
9891
genStateChanged env =
9992
oneof
100-
[ HeadInitialized (mkHeadParameters env) <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
101-
, CommittedUTxO party <$> arbitrary <*> arbitrary <*> arbitrary
102-
, HeadAborted <$> arbitrary <*> arbitrary
103-
, HeadOpened <$> arbitrary <*> arbitrary <*> arbitrary
104-
, TransactionAppliedToLocalUTxO <$> arbitrary <*> arbitrary <*> arbitrary
105-
, SnapshotRequestDecided <$> arbitrary <*> arbitrary
106-
, SnapshotRequested <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
107-
, TransactionReceived <$> arbitrary <*> arbitrary
108-
, PartySignedSnapshot <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
109-
, SnapshotConfirmed <$> arbitrary <*> arbitrary <*> arbitrary
110-
, HeadClosed <$> arbitrary <*> arbitrary <*> arbitrary
111-
, HeadContested <$> arbitrary <*> arbitrary <*> arbitrary
112-
, HeadIsReadyToFanout <$> arbitrary
113-
, HeadFannedOut <$> arbitrary <*> arbitrary
114-
, ChainRolledBack <$> arbitrary <*> arbitrary
115-
, TickObserved <$> arbitrary <*> arbitrary
93+
[ HeadInitialized (mkHeadParameters env) <$> arbitrary <*> arbitrary <*> arbitrary
94+
, CommittedUTxO party <$> arbitrary <*> arbitrary
95+
, HeadAborted <$> arbitrary
96+
, HeadOpened <$> arbitrary <*> arbitrary
97+
, TransactionAppliedToLocalUTxO <$> arbitrary <*> arbitrary
98+
, SnapshotRequestDecided <$> arbitrary
99+
, SnapshotRequested <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
100+
, TransactionReceived <$> arbitrary
101+
, PartySignedSnapshot <$> arbitrary <*> arbitrary <*> arbitrary
102+
, SnapshotConfirmed <$> arbitrary <*> arbitrary
103+
, HeadClosed <$> arbitrary <*> arbitrary
104+
, HeadContested <$> arbitrary <*> arbitrary
105+
, pure HeadIsReadyToFanout
106+
, HeadFannedOut <$> arbitrary
107+
, ChainRolledBack <$> arbitrary
108+
, TickObserved <$> arbitrary
116109
]
117110
where
118111
Environment{party} = env

0 commit comments

Comments
 (0)