Skip to content

Commit 31a1a40

Browse files
authored
Merge pull request #5 from pragma-org/read-ledgerdb-config
Read ledgerdb config
2 parents e675dfe + 903a775 commit 31a1a40

4 files changed

Lines changed: 219 additions & 11 deletions

File tree

.github/workflows/haskell.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ jobs:
2929
id: tag
3030
shell: bash
3131
run: |
32-
if [ "${GITHUB_REF/refs\/tags\//}" == "refs/heads/main" ]; then
33-
echo "value=$GIT_SHA" >> $GITHUB_OUTPUT
34-
else
35-
echo "value=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
36-
fi
32+
echo "value=$GIT_SHA" >> $GITHUB_OUTPUT
3733
env:
3834
GIT_SHA: ${{ github.sha }}
3935

@@ -57,10 +53,6 @@ jobs:
5753
${{ runner.os }}-${{ runner.arch }}-build-
5854
${{ runner.os }}-${{ runner.arch }}-
5955
60-
- name: ❓ Check cache
61-
run: |
62-
find ~/.local
63-
6456
- name: 🚄 Prepare OS
6557
if: ${{ matrix.os == 'macos' }}
6658
run: |

src/Cardano/Tools/DB.hs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
{-# LANGUAGE ViewPatterns #-}
1313
{-# OPTIONS_GHC -Wno-orphans #-}
1414

15+
1516
module Cardano.Tools.DB
1617
( StandardBlock,
1718
StandardPoint,
@@ -20,6 +21,8 @@ module Cardano.Tools.DB
2021
DBError (..),
2122
DB,
2223
DBTrace,
24+
LedgerDbBackend(..),
25+
LedgerDbConfig(..),
2326
SlotNo,
2427
Hash,
2528
asInteger,
@@ -34,6 +37,7 @@ module Cardano.Tools.DB
3437
makeSlot,
3538
mkPoint,
3639
listBlocks,
40+
parseLedgerDbConfig,
3741
toBytestring,
3842
)
3943
where
@@ -45,7 +49,7 @@ import Cardano.Slotting.Slot (SlotNo (..), WithOrigin (..))
4549
import Cardano.Tools.DBAnalyser.Block.Cardano (Args (CardanoBlockArgs))
4650
import Cardano.Tools.DBAnalyser.HasAnalysis (mkProtocolInfo)
4751
import Control.Tracer (Tracer (..))
48-
import Data.Aeson (FromJSON (..), ToJSON, object, toJSON, withObject, (.:), (.=))
52+
import Data.Aeson (FromJSON (..), ToJSON, object, toJSON, withObject, (.:), (.=), eitherDecodeFileStrict', Value (Object))
4953
import qualified Data.ByteString.Base16 as Hex
5054
import qualified Data.ByteString.Lazy as LBS
5155
import Data.String (IsString (fromString))
@@ -76,6 +80,9 @@ import Ouroboros.Consensus.Util.ResourceRegistry (withRegistry)
7680
import Ouroboros.Network.AnchoredSeq (lookupByMeasure)
7781
import qualified Ouroboros.Network.AnchoredSeq as Seq
7882
import Text.Read (readMaybe)
83+
import qualified Data.Aeson.Types as Aeson
84+
import qualified Data.Aeson.KeyMap as KeyMap
85+
import Data.Aeson.Types (fromJSON)
7986

8087
type StandardBlock = CardanoBlock StandardCrypto
8188

@@ -241,3 +248,53 @@ getSnapshot db slot = do
241248
pure $ Found $ serialize (toEnum 10) $ shelleyLedgerState state
242249
_other -> pure (Err UnknownStateType)
243250
_other -> pure (Err NotFound)
251+
252+
253+
-- # LedgerDb Configuration
254+
-- We need to read the node's config file to get its LedgerDB options
255+
-- There is no way to parse the node config without pulling in cardano-node code and dependencies,
256+
-- which defeats the purpose of this code.
257+
258+
data LedgerDbConfig = LedgerDbConfig
259+
{ backend :: LedgerDbBackend,
260+
numOfDiskSnapshots :: Int,
261+
queryBatchSize :: Int,
262+
snapshotInterval :: Int
263+
}
264+
deriving (Eq, Show)
265+
266+
data LedgerDbBackend = V2InMemory
267+
deriving (Eq, Show)
268+
269+
newtype LedgerDbConfigError = LedgerDbConfigError String
270+
deriving (Eq, Show)
271+
272+
instance FromJSON LedgerDbConfig where
273+
parseJSON = withObject "LedgerDbConfig" $ \v ->
274+
LedgerDbConfig
275+
<$> v .: "Backend"
276+
<*> v .: "NumOfDiskSnapshots"
277+
<*> v .: "QueryBatchSize"
278+
<*> v .: "SnapshotInterval"
279+
280+
instance FromJSON LedgerDbBackend where
281+
parseJSON "V2InMemory" = pure V2InMemory
282+
parseJSON _ = fail "Unsupported LedgerDbBackend"
283+
284+
parseLedgerDbConfig :: FilePath -> IO (Either LedgerDbConfigError LedgerDbConfig)
285+
parseLedgerDbConfig nodeConfig = do
286+
eitherDecodeFileStrict' nodeConfig >>= \case
287+
Left _ ->
288+
undefined
289+
Right (Object config) ->
290+
case KeyMap.lookup "LedgerDB" config of
291+
Nothing ->
292+
undefined
293+
Just value ->
294+
case fromJSON value of
295+
Aeson.Error err ->
296+
fail err
297+
Aeson.Success v ->
298+
pure (Right v)
299+
Right _ ->
300+
undefined
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
{
2+
"AlonzoGenesisFile": "mainnet-alonzo-genesis.json",
3+
"AlonzoGenesisHash": "7e94a15f55d1e82d10f09203fa1d40f8eede58fd8066542cf6566008068ed874",
4+
"ByronGenesisFile": "mainnet-byron-genesis.json",
5+
"ByronGenesisHash": "5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb",
6+
"CheckpointsFile": "mainnet-checkpoints.json",
7+
"CheckpointsFileHash": "3e6dee5bae7acc6d870187e72674b37c929be8c66e62a552cf6a876b1af31ade",
8+
"ConsensusMode": "PraosMode",
9+
"ConwayGenesisFile": "mainnet-conway-genesis.json",
10+
"ConwayGenesisHash": "15a199f895e461ec0ffc6dd4e4028af28a492ab4e806d39cb674c88f7643ef62",
11+
"EnableP2P": true,
12+
"LastKnownBlockVersion-Alt": 0,
13+
"LastKnownBlockVersion-Major": 3,
14+
"LastKnownBlockVersion-Minor": 0,
15+
"LedgerDB": {
16+
"Backend": "V2InMemory",
17+
"NumOfDiskSnapshots": 2,
18+
"QueryBatchSize": 100000,
19+
"SnapshotInterval": 4320
20+
},
21+
"MaxKnownMajorProtocolVersion": 2,
22+
"MinNodeVersion": "10.4.0",
23+
"PeerSharing": false,
24+
"Protocol": "Cardano",
25+
"RequiresNetworkMagic": "RequiresNoMagic",
26+
"ShelleyGenesisFile": "mainnet-shelley-genesis.json",
27+
"ShelleyGenesisHash": "1a3be38bcbb7911969283716ad7aa550250226b76a61fc51cc9a9a35d9276d81",
28+
"TargetNumberOfKnownPeers": 100,
29+
"TargetNumberOfRootPeers": 100,
30+
"TraceOptionForwarder": {
31+
"connQueueSize": 64,
32+
"disconnQueueSize": 128,
33+
"maxReconnectDelay": 30
34+
},
35+
"TraceOptionMetricsPrefix": "cardano.node.metrics.",
36+
"TraceOptionPeerFrequency": 2000,
37+
"TraceOptionResourceFrequency": 1000,
38+
"TraceOptions": {
39+
"": {
40+
"backends": [
41+
"EKGBackend",
42+
"Forwarder",
43+
"PrometheusSimple suffix 127.0.0.1 12798",
44+
"Stdout HumanFormatColoured"
45+
],
46+
"detail": "DNormal",
47+
"severity": "Notice"
48+
},
49+
"BlockFetch.Client.CompletedBlockFetch": {
50+
"maxFrequency": 2.0
51+
},
52+
"BlockFetch.Decision": {
53+
"severity": "Silence"
54+
},
55+
"ChainDB": {
56+
"severity": "Info"
57+
},
58+
"ChainDB.AddBlockEvent.AddBlockValidation": {
59+
"severity": "Silence"
60+
},
61+
"ChainDB.AddBlockEvent.AddBlockValidation.ValidCandidate": {
62+
"maxFrequency": 2.0
63+
},
64+
"ChainDB.AddBlockEvent.AddedBlockToQueue": {
65+
"maxFrequency": 2.0
66+
},
67+
"ChainDB.AddBlockEvent.AddedBlockToVolatileDB": {
68+
"maxFrequency": 2.0
69+
},
70+
"ChainDB.CopyToImmutableDBEvent.CopiedBlockToImmutableDB": {
71+
"maxFrequency": 2.0
72+
},
73+
"ChainDB.LedgerEvent.Forker": {
74+
"severity": "Silence"
75+
},
76+
"ChainSync.Client": {
77+
"severity": "Warning"
78+
},
79+
"Forge.Loop": {
80+
"severity": "Info"
81+
},
82+
"Forge.StateInfo": {
83+
"severity": "Info"
84+
},
85+
"Mempool": {
86+
"severity": "Silence"
87+
},
88+
"Mempool.AttemptAdd": {
89+
"severity": "Silence"
90+
},
91+
"Mempool.LedgerFound": {
92+
"severity": "Silence"
93+
},
94+
"Mempool.LedgerNotFound": {
95+
"severity": "Silence"
96+
},
97+
"Mempool.SyncNotNeeded": {
98+
"severity": "Silence"
99+
},
100+
"Net.ConnectionManager.Remote": {
101+
"severity": "Info"
102+
},
103+
"Net.ConnectionManager.Remote.ConnectionManagerCounters": {
104+
"severity": "Silence"
105+
},
106+
"Net.ErrorPolicy": {
107+
"severity": "Info"
108+
},
109+
"Net.ErrorPolicy.Local": {
110+
"severity": "Info"
111+
},
112+
"Net.InboundGovernor": {
113+
"severity": "Warning"
114+
},
115+
"Net.InboundGovernor.Remote": {
116+
"severity": "Info"
117+
},
118+
"Net.Mux.Remote": {
119+
"severity": "Info"
120+
},
121+
"Net.PeerSelection": {
122+
"severity": "Silence"
123+
},
124+
"Net.Subscription.DNS": {
125+
"severity": "Info"
126+
},
127+
"Net.Subscription.IP": {
128+
"severity": "Info"
129+
},
130+
"Resources": {
131+
"severity": "Silence"
132+
},
133+
"Startup.DiffusionInit": {
134+
"severity": "Info"
135+
}
136+
},
137+
"TurnOnLogMetrics": true,
138+
"TurnOnLogging": true,
139+
"UseTraceDispatcher": true,
140+
"defaultBackends": [],
141+
"defaultScribes": [],
142+
"minSeverity": "Critical",
143+
"options": {},
144+
"setupBackends": [],
145+
"setupScribes": []
146+
}

test/Cardano/Tools/DBServer/OptionsSpec.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module Cardano.Tools.DBServer.OptionsSpec where
22

3+
import Cardano.Tools.DB
4+
( LedgerDbConfig(..), LedgerDbBackend(..), parseLedgerDbConfig )
35
import Cardano.Tools.DBServer.Options (Options (..), ServeOptions (..), parseArgs)
46
import Data.String (IsString (..))
57
import Network.Wai.Handler.Warp (HostPreference)
6-
import Test.Hspec (Spec, describe, it, shouldBe)
8+
import Test.Hspec (Spec, describe, it, shouldBe, shouldReturn)
79
import Test.Hspec.QuickCheck (prop)
810
import Test.QuickCheck (Arbitrary (..), elements, (===))
911

@@ -20,6 +22,17 @@ spec = do
2022
show (fromString "*" :: HostPreference) `shouldBe` "HostAny"
2123
show (fromString "0.0.0.0" :: HostPreference) `shouldBe` "Host \"0.0.0.0\""
2224

25+
describe "Config files" $ do
26+
it "Can read configuration file" $ do
27+
let ledgerDbConfig = LedgerDbConfig {
28+
backend = V2InMemory,
29+
numOfDiskSnapshots = 2,
30+
queryBatchSize = 100000,
31+
snapshotInterval = 4320
32+
}
33+
parseLedgerDbConfig "test-data/config/mainnet-config-bp.json" `shouldReturn` Right ledgerDbConfig
34+
35+
2336
newtype SomeHost = SomeHost String
2437
deriving (Show, Eq)
2538

0 commit comments

Comments
 (0)