Skip to content

Commit

Permalink
Make the MockChain submission behave the same as the Direct one
Browse files Browse the repository at this point in the history
By using the FailedToPostTx exception, hydra nodes will handle this
event and not crash.

Indeed in some situations it is actually expected that the submission
fails (e.g. multiple collect transactions)

Furthermore, this highlights that the MockChain behaves quite different
than the real cardano chain interfaced through a cardano-node (see FIXME)
  • Loading branch information
ch1bo committed Feb 19, 2024
1 parent a2096b2 commit 4a05bcf
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions hydra-node/test/Hydra/Model/MockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ import Data.Sequence (Seq (Empty, (:|>)))
import Data.Sequence qualified as Seq
import Data.Time (secondsToNominalDiffTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import GHC.IO.Exception (userError)
import Hydra.BehaviorSpec (
SimulatedChainNetwork (..),
)
import Hydra.Cardano.Api.Pretty (renderTxWithUTxO)
import Hydra.Chain (Chain (..), initHistory)
import Hydra.Chain (Chain (..), PostTxError (..), initHistory)
import Hydra.Chain.Direct.Fixture (testNetworkId)
import Hydra.Chain.Direct.Handlers (
ChainSyncHandler (..),
Expand Down Expand Up @@ -147,17 +146,17 @@ mockChainAndNetwork tr seedKeys commits = do
-- TODO: dry with block tx validation
case evaluateTx tx utxo of
Left err ->
throwSTM . userError . toString $
throwSTM . FailedToPostTx @Tx $
unlines
[ "Invalid tx submitted"
[ "MockChain: Invalid tx submitted"
, "Tx: " <> toText (renderTxWithUTxO utxo tx)
, "Error: " <> show err
]
Right report
| any isLeft report ->
throwSTM . userError . toString $
throwSTM . FailedToPostTx @Tx $
unlines
[ "Invalid tx submitted"
[ "MockChain: Invalid tx submitted"
, "Tx: " <> toText (renderTxWithUTxO utxo tx)
, "Error: " <> show (lefts . toList $ report)
]
Expand Down Expand Up @@ -239,6 +238,10 @@ mockChainAndNetwork tr seedKeys commits = do
Nothing ->
pure ()

-- FIXME: This should actually work more like a chain fork / switch to longer
-- chain. That is, the ledger switches to the longer chain state right away
-- and we issue rollback and forwards to synchronize clients. However,
-- submission will already validate against the new ledger state.
rollbackAndForward nodes chain numberOfBlocks = do
doRollBackward nodes chain numberOfBlocks
replicateM_ (fromIntegral numberOfBlocks) $
Expand Down

0 comments on commit 4a05bcf

Please sign in to comment.