Skip to content

Commit

Permalink
Add postCloseTx to SimulatedChainNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch authored and ch1bo committed Feb 19, 2024
1 parent b0a91e2 commit ad3f55b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions hydra-node/test/Hydra/BehaviorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ data SimulatedChainNetwork tx m = SimulatedChainNetwork
, tickThread :: Async m ()
, rollbackAndForward :: Natural -> m ()
, simulateCommit :: (Party, UTxOType tx) -> m ()
, postCloseTx :: Party -> m ()
}

dummySimulatedChainNetwork :: SimulatedChainNetwork tx m
Expand All @@ -557,6 +558,7 @@ dummySimulatedChainNetwork =
, tickThread = error "tickThread"
, rollbackAndForward = \_ -> error "rollbackAndForward"
, simulateCommit = \_ -> error "simulateCommit"
, postCloseTx = \_ -> error "postCloseTx"
}

-- | With-pattern wrapper around 'simulatedChainAndNetwork' which does 'cancel'
Expand Down Expand Up @@ -622,6 +624,7 @@ simulatedChainAndNetwork initialChainState = do
, rollbackAndForward = rollbackAndForward nodes history localChainState
, simulateCommit = \(party, committed) ->
createAndYieldEvent nodes history localChainState $ OnCommitTx{headId = testHeadId, party, committed}
, postCloseTx = error "unexpected call to postCloseTx"
}
where
-- seconds
Expand Down
28 changes: 26 additions & 2 deletions hydra-node/test/Hydra/Model/MockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import Hydra.BehaviorSpec (
SimulatedChainNetwork (..),
)
import Hydra.Cardano.Api.Pretty (renderTxWithUTxO)
import Hydra.Chain (Chain (..), initHistory)
import Hydra.Chain (
Chain (..),
PostChainTx (CloseTx, confirmedSnapshot, headId, headParameters),
initHistory,
)
import Hydra.Chain.Direct.Fixture (testNetworkId)
import Hydra.Chain.Direct.Handlers (
ChainSyncHandler (..),
Expand All @@ -51,16 +55,18 @@ import Hydra.Chain.Direct.Tx (verificationKeyToOnChainId)
import Hydra.Chain.Direct.Wallet (TinyWallet (..))
import Hydra.Crypto (HydraKey)
import Hydra.HeadLogic (
Environment (Environment, participants, party),
Environment (Environment, party),
Event (..),
defaultTTL,
)
import Hydra.HeadLogic.State (
ClosedState (..),
CoordinatedHeadState (..),
HeadState (..),
IdleState (..),
InitialState (..),
OpenState (..),
participants,
)
import Hydra.Ledger (ChainSlot (..), Ledger (..), ValidationError (..), collectTransactions)
import Hydra.Ledger.Cardano (adjustUTxO, fromChainSlot, genTxOutAdaOnly)
Expand Down Expand Up @@ -105,6 +111,7 @@ mockChainAndNetwork tr seedKeys commits = do
, tickThread
, rollbackAndForward = rollbackAndForward nodes chain
, simulateCommit = simulateCommit nodes
, postCloseTx = postCloseTx nodes
}
where
initialUTxO = initUTxO <> commits <> registryUTxO scriptRegistry
Expand Down Expand Up @@ -204,6 +211,23 @@ mockChainAndNetwork tr seedKeys commits = do
Left e -> throwIO e
Right tx -> submitTx tx

postCloseTx nodes party = do
hydraNodes <- readTVarIO nodes
case find (matchingParty party) hydraNodes of
Nothing -> error "postCloseTx: Could not find matching HydraNode"
Just
MockHydraNode
{ node = HydraNode{oc = Chain{postTx}, nodeState = NodeState{queryHeadState}}
} -> do
hs <- atomically queryHeadState
case hs of
Idle IdleState{} -> error "Cannot post Close tx when in Initial state"
Initial InitialState{} -> error "Cannot post Close tx when in Initial state"
Open OpenState{headId = openHeadId, parameters = headParameters, coordinatedHeadState = CoordinatedHeadState{confirmedSnapshot}} -> do
let closeTx = CloseTx{headId = openHeadId, headParameters, confirmedSnapshot}
postTx closeTx
Closed ClosedState{} -> error "Cannot post Close tx when in Closed state"

matchingParty us MockHydraNode{node = HydraNode{env = Environment{party}}} =
party == us

Expand Down

0 comments on commit ad3f55b

Please sign in to comment.