Skip to content

Commit

Permalink
Find seenTxs' and seenUTxO' in one fold
Browse files Browse the repository at this point in the history
This circumvents the "should not happen" case where a transaction
`canApply`, but then it fails when we determine the new seenUTxO'.
  • Loading branch information
ch1bo committed Feb 22, 2023
1 parent 7366ed8 commit fe854de
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ onOpenNetworkReqSn ::
-- | List of transactions to snapshot.
[tx] ->
Outcome tx
onOpenNetworkReqSn env ledger st otherParty sn txs =
onOpenNetworkReqSn env ledger st otherParty sn requestedTxs =
-- TODO: Verify the request is signed by (?) / comes from the leader
-- (Can we prove a message comes from a given peer, without signature?)

Expand All @@ -696,16 +696,11 @@ onOpenNetworkReqSn env ledger st otherParty sn txs =
-- Spec: wait U̅ ◦ T ̸= ⊥ combined with Û ← Ū̅ ◦ T
waitApplyTxs $ \u -> do
-- NOTE: confSn == seenSn == sn here
let nextSnapshot = Snapshot (confSn + 1) u txs
let nextSnapshot = Snapshot (confSn + 1) u requestedTxs
-- Spec: σᵢ
let snapshotSignature = sign signingKey nextSnapshot
-- Prune transactions
-- TODO: filter/fold once
let seenTxs' = filter ((== Valid) . canApply ledger u) seenTxs
let seenUTxO' =
case applyTransactions ledger u seenTxs' of
Left (_, err) -> Hydra.Prelude.error $ "SHOULD NOT HAPPEN: " <> show err
Right u' -> u'
-- Spec: T̂ ← {tx | ∀tx ∈ T̂ , Û ◦ tx ≠ ⊥} and L̂ ← Û ◦ T̂
let (seenTxs', seenUTxO') = pruneTransactions u
NewState
( Open
st
Expand All @@ -730,13 +725,21 @@ onOpenNetworkReqSn env ledger st otherParty sn txs =
else Wait $ WaitOnSnapshotNumber seenSn

waitApplyTxs cont =
case applyTransactions ledger confirmedUTxO txs of
case applyTransactions ledger confirmedUTxO requestedTxs of
Left (_, err) ->
-- FIXME: this will not happen, as we are always comparing against the
-- confirmed snapshot utxo in NewTx?
Wait $ WaitOnNotApplicableTx err
Right u -> cont u

pruneTransactions utxo = do
foldr go ([], utxo) seenTxs
where
go tx (txs, u) =
case applyTransactions ledger u [tx] of
Left (_, _) -> (txs, u)
Right u' -> (txs <> [tx], u')

confSn = case confirmedSnapshot of
InitialSnapshot{} -> 0
ConfirmedSnapshot{snapshot = Snapshot{number}} -> number
Expand Down

0 comments on commit fe854de

Please sign in to comment.