Skip to content

Commit 35e3d9a

Browse files
committed
Draft roll forward and backward
Reusing logic from ouroborus client.
1 parent e8a24e3 commit 35e3d9a

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

hydra-chain-observer/src/Hydra/Blockfrost/ChainObserver.hs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import Blockfrost.Client (
1616
)
1717
import Control.Retry (RetryPolicyM, exponentialBackoff, limitRetries)
1818

19-
import Hydra.Cardano.Api (Hash, SlotNo)
19+
import Hydra.Cardano.Api (BlockNo, Hash, NetworkId, SlotNo, Tx, UTxO)
2020
import Hydra.Cardano.Api.Prelude (BlockHeader (..), ChainPoint (..))
21-
import Hydra.ChainObserver.NodeClient (ChainObserverLog (..), NodeClient (..))
21+
import Hydra.Chain.Direct.Handlers (convertObservation)
22+
import Hydra.ChainObserver.NodeClient (ChainObservation (..), ChainObserverLog (..), NodeClient (..), ObserverHandler, logOnChainTx, observeAll)
23+
import Hydra.Tx (txId)
2224

2325
blockfrostClient ::
2426
Tracer IO ChainObserverLog ->
@@ -49,6 +51,41 @@ blockfrostClient tracer = do
4951
pure ()
5052
}
5153

54+
-- TODO! DRY
55+
rollForward ::
56+
Tracer IO ChainObserverLog ->
57+
NetworkId ->
58+
ChainPoint ->
59+
BlockNo ->
60+
UTxO ->
61+
[Tx] ->
62+
ObserverHandler IO ->
63+
IO UTxO
64+
rollForward tracer networkId point blockNo currentUTxO receivedTxs observerHandler = do
65+
let receivedTxIds = txId <$> receivedTxs
66+
traceWith tracer RollForward{point, receivedTxIds}
67+
let (adjustedUTxO, observations) = observeAll networkId currentUTxO receivedTxs
68+
let onChainTxs = mapMaybe convertObservation observations
69+
forM_ onChainTxs (traceWith tracer . logOnChainTx)
70+
let observationsAt = HeadObservation point blockNo <$> onChainTxs
71+
observerHandler $
72+
if null observationsAt
73+
then [Tick point blockNo]
74+
else observationsAt
75+
pure adjustedUTxO
76+
77+
-- TODO! DRY
78+
rollBackward ::
79+
Tracer IO ChainObserverLog ->
80+
ChainPoint ->
81+
b ->
82+
IO b
83+
rollBackward tracer point currentUTxO = do
84+
traceWith tracer Rollback{point}
85+
pure currentUTxO
86+
87+
-- * Helpers
88+
5289
toChainPoint :: Block -> ChainPoint
5390
toChainPoint Block{_blockSlot, _blockHash} =
5491
ChainPoint slotNo headerHash

hydra-explorer/src/Hydra/Explorer.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Hydra.Prelude
55

66
import Control.Concurrent.Class.MonadSTM (modifyTVar', newTVarIO, readTVarIO)
77
import Hydra.API.APIServerLog (APIServerLog (..), Method (..), PathInfo (..))
8-
import Hydra.ChainObserver (ChainObservation)
8+
import Hydra.ChainObserver.NodeClient (ChainObservation)
99
import Hydra.Explorer.ExplorerState (ExplorerState (..), HeadState, TickState, aggregateHeadObservations, initialTickState)
1010
import Hydra.Explorer.Options (Options (..), toArgStartChainFrom)
1111
import Hydra.Logging (Tracer, Verbosity (..), traceWith, withTracer)

hydra-explorer/src/Hydra/Explorer/ExplorerState.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Hydra.Chain (OnChainTx (..))
1010
import Hydra.Chain.Direct.Tx (
1111
headSeedToTxIn,
1212
)
13-
import Hydra.ChainObserver (ChainObservation (..))
13+
import Hydra.ChainObserver.NodeClient (ChainObservation (..))
1414
import Hydra.Tx.ContestationPeriod (ContestationPeriod, toNominalDiffTime)
1515
import Hydra.Tx.HeadParameters (HeadParameters (..))
1616
import Hydra.Tx.OnChainId (OnChainId)

hydra-explorer/test/Hydra/Explorer/ExplorerStateSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Hydra.Explorer.ExplorerStateSpec where
33
import Hydra.Prelude
44
import Test.Hydra.Prelude
55

6-
import Hydra.ChainObserver (ChainObservation (..))
6+
import Hydra.ChainObserver.NodeClient (ChainObservation (..))
77
import Hydra.Explorer.ExplorerState (ExplorerState (..), HeadState (..), aggregateHeadObservations, initialTickState)
88
import Hydra.Tx.HeadId (HeadId)
99
import Test.QuickCheck (forAll, listOf1, (=/=))

0 commit comments

Comments
 (0)