Skip to content

Commit 25f9f72

Browse files
authored
Persistence works with committing empty UTxO (#1741)
### Why To prove that we can load from persistence when we commit empty UTxO (reported by hydra-doom team) --- <!-- Consider each and tick it off one way or the other --> * [x] CHANGELOG updated or not needed * [x] Documentation updated or not needed * [x] Haddocks updated or not needed * [x] No new TODOs introduced or explained herafter
2 parents 264d68f + 07c960a commit 25f9f72

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

hydra-cluster/src/Hydra/Cluster/Scenarios.hs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Control.Concurrent.Async (mapConcurrently_)
2121
import Control.Lens ((^..), (^?))
2222
import Data.Aeson (Value, object, (.=))
2323
import Data.Aeson qualified as Aeson
24-
import Data.Aeson.Lens (key, values, _JSON)
24+
import Data.Aeson.Lens (key, values, _JSON, _String)
2525
import Data.Aeson.Types (parseMaybe)
2626
import Data.ByteString (isInfixOf)
2727
import Data.ByteString qualified as B
@@ -469,6 +469,44 @@ singlePartyCommitsScriptBlueprint tracer workDir node hydraScriptsTxId =
469469

470470
RunningNode{networkId, nodeSocket, blockTime} = node
471471

472+
persistenceCanLoadWithEmptyCommit ::
473+
Tracer IO EndToEndLog ->
474+
FilePath ->
475+
RunningNode ->
476+
TxId ->
477+
IO ()
478+
persistenceCanLoadWithEmptyCommit tracer workDir node hydraScriptsTxId =
479+
(`finally` returnFundsToFaucet tracer node Alice) $ do
480+
refuelIfNeeded tracer node Alice 20_000_000
481+
aliceChainConfig <- chainConfigFor Alice workDir nodeSocket hydraScriptsTxId [] $ UnsafeContestationPeriod 100
482+
let hydraNodeId = 1
483+
let hydraTracer = contramap FromHydraNode tracer
484+
headId <- withHydraNode hydraTracer aliceChainConfig workDir hydraNodeId aliceSk [] [1] $ \n1 -> do
485+
send n1 $ input "Init" []
486+
headId <- waitMatch (10 * blockTime) n1 $ headIsInitializingWith (Set.fromList [alice])
487+
488+
requestCommitTx n1 mempty >>= submitTx node
489+
waitFor hydraTracer (10 * blockTime) [n1] $
490+
output "HeadIsOpen" ["utxo" .= object mempty, "headId" .= headId]
491+
pure headId
492+
let persistenceState = workDir </> "state-" <> show hydraNodeId </> "state"
493+
stateContents <- readFileBS persistenceState
494+
let headOpened = BSC.pack $ List.last (List.lines $ BSC.unpack stateContents)
495+
case headOpened ^? key "stateChanged" . key "tag" . _String of
496+
Nothing -> error "Failed to find HeadIsOpened in the state file"
497+
Just headIsOpen -> do
498+
headIsOpen `shouldBe` "HeadOpened"
499+
withHydraNode hydraTracer aliceChainConfig workDir hydraNodeId aliceSk [] [1] $ \n1 -> do
500+
waitFor hydraTracer (10 * blockTime) [n1] $
501+
output "HeadIsOpen" ["utxo" .= object mempty, "headId" .= headId]
502+
503+
send n1 $ input "GetUTxO" []
504+
505+
waitFor hydraTracer 10 [n1] $
506+
output "GetUTxOResponse" ["headId" .= headId, "utxo" .= (mempty :: UTxO)]
507+
where
508+
RunningNode{nodeSocket, blockTime} = node
509+
472510
-- | Single hydra-node where the commit is done from a raw transaction
473511
-- blueprint.
474512
singlePartyCommitsFromExternalTxBlueprint ::

hydra-cluster/test/Test/EndToEndSpec.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import Hydra.Cluster.Scenarios (
6464
canSubmitTransactionThroughAPI,
6565
headIsInitializingWith,
6666
initWithWrongKeys,
67+
persistenceCanLoadWithEmptyCommit,
6768
refuelIfNeeded,
6869
restartedNodeCanAbort,
6970
restartedNodeCanObserveCommitTx,
@@ -217,6 +218,11 @@ spec = around (showLogsOnFailure "EndToEndSpec") $ do
217218
withCardanoNodeDevnet (contramap FromCardanoNode tracer) tmpDir $ \node ->
218219
publishHydraScriptsAs node Faucet
219220
>>= singlePartyCommitsScriptBlueprint tracer tmpDir node
221+
it "persistence can load with empty commit" $ \tracer -> do
222+
withClusterTempDir $ \tmpDir -> do
223+
withCardanoNodeDevnet (contramap FromCardanoNode tracer) tmpDir $ \node ->
224+
publishHydraScriptsAs node Faucet
225+
>>= persistenceCanLoadWithEmptyCommit tracer tmpDir node
220226

221227
describe "three hydra nodes scenario" $ do
222228
it "does not error when all nodes open the head concurrently" $ \tracer ->

0 commit comments

Comments
 (0)