Skip to content

Commit

Permalink
Should not try to apply decommit tx again
Browse files Browse the repository at this point in the history
Signed-off-by: Sasha Bogicevic <[email protected]>
  • Loading branch information
v0d1ch committed Aug 1, 2024
1 parent 4f5d98b commit 3dd31a6
Show file tree
Hide file tree
Showing 2 changed files with 28 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
21 changes: 6 additions & 15 deletions hydra-node/test/Hydra/BehaviorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Control.Concurrent.Class.MonadSTM (
)
import Control.Monad.Class.MonadAsync (Async, MonadAsync (async), cancel, forConcurrently_)
import Control.Monad.IOSim (IOSim, runSimTrace, selectTraceEventsDynamic)
import Data.Aeson (encode)
import Data.List ((!!))
import Data.List qualified as List
import Hydra.API.ClientInput
Expand Down Expand Up @@ -460,19 +461,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 @@ -561,7 +549,7 @@ spec = parallel $ do
simulateCommit chain (alice, utxoRef 1)

logs = selectTraceEventsDynamic @_ @(HydraNodeLog SimpleTx) result

print $ encode logs
logs `shouldContain` [BeginEffect alice 1 0 (ClientEffect $ HeadIsInitializing testHeadId $ fromList [alice])]
logs `shouldContain` [EndEffect alice 1 0]

Expand Down Expand Up @@ -733,7 +721,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 3dd31a6

Please sign in to comment.