Skip to content

Commit

Permalink
Should not try to apply decommit tx again
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed Jul 31, 2024
1 parent 4f5d98b commit ef7c99b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
34 changes: 22 additions & 12 deletions hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,22 @@ onOpenNetworkReqSn env ledger st otherParty sv sn requestedTxIds mDecommitTx =
unseen -> wait $ WaitOnTxs unseen

requireApplicableDecommitTx cont =
case mDecommitTx of
Nothing -> cont (confirmedUTxO, Nothing)
Just decommitTx ->
-- Spec: require ̅S.𝑈 ◦ txω /= ⊥
case applyTransactions ledger currentSlot confirmedUTxO [decommitTx] of
Left (_, err) ->
Error $ RequireFailed $ SnapshotDoesNotApply sn (txId decommitTx) err
Right newConfirmedUTxO -> do
-- Spec: 𝑈_active ← ̅S.𝑈 ◦ txω \ outputs(txω)
let utxoToDecommit = utxoFromTx decommitTx
let activeUTxO = newConfirmedUTxO `withoutUTxO` utxoToDecommit
cont (activeUTxO, Just utxoToDecommit)
-- NOTE: if decommit tx was already applied when we constructed the previous
-- snapshot we should not try to apply it again
if confUTxOToDecommit == decommitUTxO
then cont (confirmedUTxO, decommitUTxO)
else case mDecommitTx of
Nothing -> cont (confirmedUTxO, Nothing)
Just decommitTx ->
-- Spec: require ̅S.𝑈 ◦ txω /= ⊥
case applyTransactions ledger currentSlot confirmedUTxO [decommitTx] of
Left (_, err) ->
Error $ RequireFailed $ SnapshotDoesNotApply sn (txId decommitTx) err
Right newConfirmedUTxO -> do
-- Spec: 𝑈_active ← ̅S.𝑈 ◦ txω \ outputs(txω)
let utxoToDecommit = utxoFromTx decommitTx
let activeUTxO = newConfirmedUTxO `withoutUTxO` utxoToDecommit
cont (activeUTxO, Just utxoToDecommit)

-- NOTE: at this point we know those transactions apply on the localUTxO because they
-- are part of the localTxs. The snapshot can contain less transactions than the ones
Expand Down Expand Up @@ -512,6 +516,12 @@ onOpenNetworkReqSn env ledger st otherParty sv sn requestedTxIds mDecommitTx =
InitialSnapshot{} -> 0
ConfirmedSnapshot{snapshot = Snapshot{number}} -> number

decommitUTxO = utxoFromTx <$> mDecommitTx

confUTxOToDecommit = case confirmedSnapshot of
InitialSnapshot{} -> Nothing
ConfirmedSnapshot{snapshot = Snapshot{utxoToDecommit}} -> utxoToDecommit

seenSn = seenSnapshotNumber seenSnapshot

confirmedUTxO = case confirmedSnapshot of
Expand Down
20 changes: 5 additions & 15 deletions hydra-node/test/Hydra/BehaviorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -460,19 +460,6 @@ spec = parallel $ do

waitUntil [n1, n2] $ DecommitFinalized{headId = testHeadId, decommitTxId = 1}

it "can close with decommit in flight" $
shouldRunInSim $ do
withSimulatedChainAndNetwork $ \chain ->
withHydraNode aliceSk [bob] chain $ \n1 -> do
withHydraNode bobSk [alice] chain $ \n2 -> do
openHead chain n1 n2
let decommitTx = SimpleTx 1 (utxoRef 2) (utxoRef 42)
send n2 (Decommit{decommitTx})
send n1 Close
waitUntil [n1, n2] $ ReadyToFanout{headId = testHeadId}
send n1 Fanout
waitUntil [n1, n2] $ HeadIsFinalized{headId = testHeadId, utxo = utxoRefs [1, 2]}

it "fanout utxo is correct after a decommit" $
shouldRunInSim $ do
withSimulatedChainAndNetwork $ \chain ->
Expand Down Expand Up @@ -633,7 +620,7 @@ waitUntilMatch nodes predicate = do
unless (predicate msg) $
match seenMsgs n

oneMonth = 3600 * 24 * 30
oneMonth = 60 -- 3600 * 24 * 30

-- | Wait for an output matching the predicate and extracting some value. This
-- will loop forever until a match has been found.
Expand Down Expand Up @@ -733,7 +720,10 @@ simulatedChainAndNetwork initialChainState = do
Chain
{ postTx = \tx -> do
now <- getCurrentTime
createAndYieldEvent nodes history localChainState $ toOnChainTx now tx
-- Only observe "after one block"
void . async $ do
threadDelay blockTime
createAndYieldEvent nodes history localChainState $ toOnChainTx now tx
, draftCommitTx = \_ -> error "unexpected call to draftCommitTx"
, submitTx = \_ -> error "unexpected call to submitTx"
}
Expand Down

0 comments on commit ef7c99b

Please sign in to comment.