Skip to content

Commit

Permalink
Do not reset wallet's state on the first rollback after finding inter…
Browse files Browse the repository at this point in the history
…section

  The server actually always reply with a rollback response, to the point of intersection. Thus, we can safely ignore the first rollback after successfully finding an intersection.
  • Loading branch information
KtorZ committed Oct 20, 2021
1 parent eee7671 commit 228ee52
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions hydra-node/src/Hydra/Chain/Direct/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ client tracer tipVar utxoVar address nodeToClientV =
Codecs{cChainSyncCodec, cTxSubmissionCodec, cStateQueryCodec} =
defaultCodecs nodeToClientV

type OnRollback m = ChainSyncClient Block (Point Block) (Tip Block) m ()

-- NOTE: We are fetching PParams only once, when the client first starts. Which
-- means that, if the params change later, we may start producing invalid
-- transactions. In principle, we also expect the Hydra node to monitor the
Expand Down Expand Up @@ -384,24 +386,33 @@ chainSyncClient tracer tipVar utxoVar address =
tip <$ check (tip /= genesisPoint)
pure $ ChainSync.SendMsgFindIntersect [tip] clientStIntersect

onFirstRollback :: ChainSyncClient Block (Point Block) (Tip Block) m ()
onFirstRollback = ChainSyncClient (pure $ clientStIdle reset)

clientStIntersect :: ChainSync.ClientStIntersect Block (Point Block) (Tip Block) m ()
clientStIntersect =
ChainSync.ClientStIntersect
{ ChainSync.recvMsgIntersectNotFound = \_tip -> do
reset
, ChainSync.recvMsgIntersectFound = \_point _tip ->
ChainSyncClient (pure clientStIdle)
ChainSyncClient (pure $ clientStIdle onFirstRollback)
}

clientStIdle :: ChainSync.ClientStIdle Block (Point Block) (Tip Block) m ()
clientStIdle =
ChainSync.SendMsgRequestNext clientStNext (pure clientStNext)

clientStNext :: ChainSync.ClientStNext Block (Point Block) (Tip Block) m ()
clientStNext =
clientStIdle ::
OnRollback m ->
ChainSync.ClientStIdle Block (Point Block) (Tip Block) m ()
clientStIdle onRollback =
ChainSync.SendMsgRequestNext
(clientStNext onRollback)
(pure $ clientStNext onRollback)

clientStNext ::
OnRollback m ->
ChainSync.ClientStNext Block (Point Block) (Tip Block) m ()
clientStNext onRollback =
ChainSync.ClientStNext
{ ChainSync.recvMsgRollBackward = \_point _tip ->
reset
onRollback
, ChainSync.recvMsgRollForward = \block _tip ->
ChainSyncClient $ do
msg <- atomically $ do
Expand All @@ -414,7 +425,7 @@ chainSyncClient tracer tipVar utxoVar address =
else do
pure Nothing
mapM_ (traceWith tracer) msg
pure clientStIdle
pure (clientStIdle reset)
}

stateQueryClient ::
Expand Down

0 comments on commit 228ee52

Please sign in to comment.