Skip to content

Commit 4dce3b3

Browse files
author
Ninjatrappeur
authored
Merge pull request #6 from input-output-hk/kderme/stable-parameter
Introduce stable parameter
2 parents dd531b9 + 79746b5 commit 4dce3b3

File tree

13 files changed

+53
-25
lines changed

13 files changed

+53
-25
lines changed

morpho-checkpoint-node/src/Morpho/Config/Types.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ data NodeConfiguration
5050
ncNetworkMagic :: Word32,
5151
ncSystemStart :: Maybe SystemStart,
5252
ncSecurityParameter :: Word64,
53+
ncStableLedgerDepth :: Int,
5354
ncLoggingSwitch :: Bool,
5455
ncTraceOpts :: !TraceOptions,
5556
ncLogMetrics :: Bool,
@@ -78,6 +79,7 @@ instance FromJSON NodeConfiguration where
7879
networkMagic <- v .: "NetworkMagic"
7980
systemStart <- v .:? "SystemStart"
8081
securityParam <- v .: "SecurityParam"
82+
stableLedgerDepth <- v .: "StableLedgerDepth"
8183
loggingSwitch <- v .: "TurnOnLogging"
8284
traceOptions <- traceConfigParser v
8385
vMode <- v .: "ViewMode"
@@ -106,6 +108,7 @@ instance FromJSON NodeConfiguration where
106108
networkMagic
107109
systemStart
108110
securityParam
111+
stableLedgerDepth
109112
loggingSwitch
110113
traceOptions
111114
logMetrics
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE FlexibleContexts #-}
12
{-# LANGUAGE GADTs #-}
23
{-# LANGUAGE RankNTypes #-}
34
{-# LANGUAGE ScopedTypeVariables #-}
@@ -9,32 +10,40 @@ module Morpho.Ledger.SnapshotTimeTravel
910
where
1011

1112
import Cardano.Prelude hiding (Handle, ReadMode, atomically, to, withFile)
13+
import qualified Data.FingerTree.Strict as FT
14+
import Morpho.Ledger.Block
1215
import Ouroboros.Consensus.Ledger.Basics
1316
import Ouroboros.Consensus.Ledger.Extended
1417
import Ouroboros.Consensus.Storage.ChainDB.API
1518
import Ouroboros.Consensus.Util.IOLike
16-
import Ouroboros.Network.AnchoredFragment (anchorPoint)
19+
import Ouroboros.Network.AnchoredFragment (unanchorFragment)
1720
import Ouroboros.Network.Block hiding (Tip)
21+
import qualified Ouroboros.Network.ChainFragment as CF
1822

19-
newtype TimeTravelError blk = LedgerStateNotFoundAt (Point blk)
23+
data TimeTravelError blk
24+
= LedgerStateNotFoundAt (Point blk)
25+
| ChainNotLongEnough Int Int
2026
deriving (Show)
2127

2228
getLatestStableLedgerState ::
23-
(MonadIO m, MonadSTM m) =>
29+
(MonadIO m, MonadSTM m, HasHeader (Header blk)) =>
2430
ChainDB m blk ->
31+
Int ->
2532
m (Either (TimeTravelError blk) (LedgerState blk))
26-
getLatestStableLedgerState chainDB = go (5 :: Int)
33+
getLatestStableLedgerState chainDB offset = do
34+
chainFragment <- unanchorFragment <$> atomically (getCurrentChain chainDB)
35+
let result = CF.lookupByIndexFromEnd chainFragment offset
36+
case result of
37+
FT.Position _ h _ -> getLedger h
38+
_ -> return $ Left $ ChainNotLongEnough offset (CF.length chainFragment)
2739
where
28-
go n = do
29-
immDbTip <- atomically $ castPoint . anchorPoint <$> getCurrentChain chainDB
30-
-- There is a race condition here. If the ledger db advances between the two
31-
-- calls, the ledger state will not be found and 'Nothing' will return.
32-
-- In this case we try again to get a stable ledger state and
33-
-- hopefully succeed this time.
34-
-- Newer ouroboros-consensus make past ledgerState available in STM
35-
-- transactions and can eliminate this race condition.
36-
mstate <- getPastLedger chainDB immDbTip
37-
case (ledgerState <$> mstate, n) of
38-
(Nothing, 0) -> return $ Left $ LedgerStateNotFoundAt immDbTip
39-
(Nothing, _) -> go $ n - 1
40-
(Just st, _) -> return $ Right st
40+
getLedger hdr = do
41+
let pnt = blockPoint hdr
42+
-- There is a race condition here. If the ledger db advances a lot between
43+
-- the two chainDB api calls, the ledger state will not be found and 'Nothing'
44+
-- will return. Newer ouroboros-consensus make past ledgerState available
45+
-- in STM transactions and can eliminate this race condition.
46+
mstate <- getPastLedger chainDB (castPoint pnt)
47+
case ledgerState <$> mstate of
48+
Nothing -> return $ Left $ LedgerStateNotFoundAt (castPoint pnt)
49+
Just st -> return $ Right st

morpho-checkpoint-node/src/Morpho/Node/Run.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ publishStableCheckpoint ::
292292
publishStableCheckpoint nc nodeTracers metrics chainDB ledgerState = do
293293
traceWith (extractStateTracer nodeTracers) (MorphoStateTrace $ morphoLedgerState ledgerState)
294294
set (ledgerStateToBlockNum ledgerState) $ mMorphoStateUnstableCheckpoint metrics
295-
mst <- getLatestStableLedgerState chainDB
295+
mst <- getLatestStableLedgerState chainDB (ncStableLedgerDepth nc)
296296
case mst of
297297
Left err -> traceWith (timeTravelErrorTracer nodeTracers) err
298298
Right stableLedgerState -> do

morpho-checkpoint-node/src/Morpho/Tracing/TracingOrphanInstances.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ instance (HashAlgorithm h, BftCrypto c) => ToObject (TimeTravelError (MorphoBloc
249249
"error" .= String "LedgerStateNotFoundAt",
250250
"point" .= showPoint verb point
251251
]
252+
toObject _verb (ChainNotLongEnough offset len) =
253+
mkObject
254+
[ "kind" .= String "TimeTravelError",
255+
"error" .= String "ChainNotLongEnough",
256+
"offset" .= show offset,
257+
"length" .= show len
258+
]
252259

253260
instance (BftCrypto c, HashAlgorithm h) => ToObject (GenTx (MorphoBlock h c)) where
254261
toObject _verb (MorphoGenTx tx txid) =

morpho-checkpoint-node/tests/Test/Morpho/Examples.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ exampleNodeConfig =
181181
ncNetworkMagic = 12345,
182182
ncSystemStart = Just $ SystemStart $ posixSecondsToUTCTime $ realToFrac (1234566789 :: Integer),
183183
ncSecurityParameter = 3,
184+
ncStableLedgerDepth = 2,
184185
ncLoggingSwitch = True,
185186
ncTraceOpts = exampleTraceOptions,
186187
ncLogMetrics = True,

morpho-checkpoint-node/tests/configuration/Golden/Config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ RequiresNetworkMagic: RequiresMagic
1010
NetworkMagic : 12345
1111
SystemStart: "2009-02-13T23:13:09Z"
1212
SecurityParam : 3
13+
StableLedgerDepth : 2
1314
TurnOnLogging: True
1415
ViewMode: SimpleView
1516
TurnOnLogMetrics: True

morpho-checkpoint-node/tests/configuration/QSM/prop_1/config-0.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Protocol: MockedBFT
88
NumCoreNodes: 1
99
RequiresNetworkMagic: RequiresMagic
1010
NetworkMagic : 12345
11-
SecurityParam : 3
11+
SecurityParam : 5
12+
StableLedgerDepth : 3
1213
TurnOnLogging: True
1314
ViewMode: SimpleView
1415
TurnOnLogMetrics: False

morpho-checkpoint-node/tests/configuration/QSM/prop_2/config-0.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ NumCoreNodes: 2
99
RequiresNetworkMagic: RequiresMagic
1010
NetworkMagic : 12345
1111
SystemStart: "2020-11-12T04:07:09Z"
12-
SecurityParam : 3
12+
SecurityParam : 5
13+
StableLedgerDepth : 3
1314
TurnOnLogging: True
1415
ViewMode: SimpleView
1516
TurnOnLogMetrics: False

morpho-checkpoint-node/tests/configuration/QSM/prop_2/config-1.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ NumCoreNodes: 2
99
RequiresNetworkMagic: RequiresMagic
1010
NetworkMagic : 12345
1111
SystemStart: "2020-11-12T04:07:09Z"
12-
SecurityParam : 3
12+
SecurityParam : 5
13+
StableLedgerDepth : 3
1314
TurnOnLogging: True
1415
ViewMode: SimpleView
1516
TurnOnLogMetrics: False

morpho-checkpoint-node/tests/configuration/QSM/prop_3/config-0.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ NumCoreNodes: 2
99
RequiresNetworkMagic: RequiresMagic
1010
NetworkMagic : 12345
1111
SystemStart: "2020-11-22T04:45:09Z"
12-
SecurityParam : 3
12+
SecurityParam : 5
13+
StableLedgerDepth : 3
1314
TurnOnLogging: True
1415
ViewMode: SimpleView
1516
TurnOnLogMetrics: False

0 commit comments

Comments
 (0)