Skip to content

Commit

Permalink
Wait for needed UTxO some time
Browse files Browse the repository at this point in the history
If two RollbackAndForward actions are generated next to each other
then submitTx fails. Waiting for a small amount of time to pass so
that the needed utxo is present fixes this problem.

Ideally the fix would be: Correctly implemented rollbacks in the mock
chain but perhaps this is enough for now.
  • Loading branch information
v0d1ch committed Feb 19, 2024
1 parent f7c07b0 commit f976751
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions hydra-node/test/Hydra/Model/MockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ mockChainAndNetwork tr seedKeys commits = do
let vks = getVerificationKey . signingKey . snd <$> seedKeys
env{participants = verificationKeyToOnChainId <$> vks}

-- In case of two consecutive rollback actions it can happen that the
-- tx can't be evaluated to be correct. Waiting for some time to
-- see the needed inputs helps resolve these errors
waitToEvaluateTx n chain tx = do
(_, _, _, utxo) <- readTVarIO chain
let result = evaluateTx tx utxo
if n == 0
then pure (result, utxo)
else
case result of
Left _ -> threadDelay 0.1 >> waitToEvaluateTx (n - 1) chain tx
Right report
| any isLeft report -> threadDelay 0.1 >> waitToEvaluateTx (n - 1) chain tx
| otherwise -> pure (result, utxo)

connectNode nodes chain queue node = do
localChainState <- newLocalChainState (initHistory initialChainState)
let Environment{party = ownParty} = env node
Expand All @@ -140,29 +155,30 @@ mockChainAndNetwork tr seedKeys commits = do
}
let getTimeHandle = pure $ fixedTimeHandleIndefiniteHorizon `generateWith` 42
let HydraNode{eq = EventQueue{putEvent}} = node

-- Validate transactions on submission and queue them for inclusion if valid.
let submitTx tx =
atomically $ do
(_, _, _, utxo) <- readTVar chain
-- TODO: dry with block tx validation
case evaluateTx tx utxo of
Left err ->
throwSTM . userError . toString $
unlines
[ "MockChain: Invalid tx submitted"
, "Tx: " <> toText (renderTxWithUTxO utxo tx)
, "Error: " <> show err
]
Right report
| any isLeft report ->
throwSTM . userError . toString $
unlines
[ "MockChain: Invalid tx submitted"
, "Tx: " <> toText (renderTxWithUTxO utxo tx)
, "Error: " <> show (lefts . toList $ report)
]
| otherwise ->
writeTQueue queue tx
let submitTx tx = do
(result, utxo) <- waitToEvaluateTx (2 :: Int) chain tx
-- TODO: dry with block tx validation
case result of
Left err ->
atomically . throwSTM . userError . toString $
unlines
[ "MockChain: Invalid tx submitted"
, "Tx: " <> toText (renderTxWithUTxO utxo tx)
, "Error: " <> show err
]
Right report
| any isLeft report ->
atomically . throwSTM . userError . toString $
unlines
[ "MockChain: Invalid tx submitted"
, "Tx: " <> toText (renderTxWithUTxO utxo tx)
, "Error: " <> show (lefts . toList $ report)
]
| otherwise ->
atomically $ writeTQueue queue tx

let chainHandle =
createMockChain
tr
Expand Down

0 comments on commit f976751

Please sign in to comment.