Skip to content

Commit 7de761e

Browse files
authored
db-analyser: print hashes for --show-slot-block-no (#1152)
A concrete use case is to find the intersection between two ChainDBs (eg by diffing the db-analyser output).
2 parents 05a324f + 5cb8d7f commit 7de761e

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

ouroboros-consensus-cardano/README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ Note that these flags do not refer to Ledger validation.
106106

107107
Lastly the user can provide the analysis that should be run on the chain:
108108

109-
* `--show-slot-block-no` prints the slot and block number of each block it
110-
process.
109+
* `--show-slot-block-no` prints the slot and block number and hash of each block
110+
it process.
111111

112112
* `--count-tx-outputs` prints the block and slot number, tx out output for given
113113
block and the cumulative tx out output for all the blocks seen so far.
@@ -245,6 +245,38 @@ gnuplot -e "bench_data='ledger-ops-cost.csv'" \
245245
The plot will be written to a file named `results.png`. See the script file for
246246
more usage options.
247247

248+
#### Finding the intersection between two ChainDBs
249+
250+
Suppose that you have two ChainDBs containing different forks, and you want to
251+
find their intersection. This can be accomplished with db-analyser via the
252+
`--show-slot-block-no` analysis:
253+
254+
First, run the following command for both of your ChainDBs:
255+
256+
```
257+
db-analyser --analyse-from 1234 --db /path/to/dbX --show-slot-block-no \
258+
cardano --config /path/to/config.json | cut -d ' ' -f 2- > dbX.log
259+
```
260+
261+
Note that specificying `--analyse-from` is optional; it means that you are
262+
certain that both ChainDBs still were on the same fork in slot `1234`, in order
263+
to decrease the run time of the command.
264+
265+
Then, you can `diff` the resulting files to find the last common block, or the
266+
`comm` tool like this:
267+
268+
- Get the last few blocks in common (last one is the intersection):
269+
```console
270+
comm -1 -2 db0.log db1.log | tail
271+
```
272+
- Get the first few blocks after the intersection:
273+
```console
274+
comm -3 db0.log db1.log | head
275+
```
276+
277+
> It is possible to do this in logarithmic instead of linear time via a binary
278+
> search; however, this hasn't been necessary so far.
279+
248280
## db-immutaliser
249281

250282
Copy a specific chain from a volatile DB to an immutable DB, such that other

ouroboros-consensus-cardano/app/DBAnalyser/Parsers.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ parseAnalysis :: Parser AnalysisName
7070
parseAnalysis = asum [
7171
flag' ShowSlotBlockNo $ mconcat [
7272
long "show-slot-block-no"
73-
, help "Show slot and block number of all blocks"
73+
, help "Show slot and block number and hash of all blocks"
7474
]
7575
, flag' CountTxOutputs $ mconcat [
7676
long "count-tx-outputs"

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ data TraceEvent blk =
189189
-- ^ triggered when given analysis has started
190190
| DoneEvent
191191
-- ^ triggered when analysis has ended
192-
| BlockSlotEvent BlockNo SlotNo
192+
| BlockSlotEvent BlockNo SlotNo (HeaderHash blk)
193193
-- ^ triggered when block has been found, it holds:
194194
-- * block's number
195195
-- * slot number when the block was forged
@@ -244,9 +244,10 @@ data TraceEvent blk =
244244
instance HasAnalysis blk => Show (TraceEvent blk) where
245245
show (StartedEvent analysisName) = "Started " <> (show analysisName)
246246
show DoneEvent = "Done"
247-
show (BlockSlotEvent bn sn) = intercalate "\t" $ [
247+
show (BlockSlotEvent bn sn h) = intercalate "\t" $ [
248248
show bn
249249
, show sn
250+
, show h
250251
]
251252
show (CountTxOutputsEvent bn sn cumulative count) = intercalate "\t" $ [
252253
show bn
@@ -292,7 +293,7 @@ instance HasAnalysis blk => Show (TraceEvent blk) where
292293

293294

294295
{-------------------------------------------------------------------------------
295-
Analysis: show block and slot number for all blocks
296+
Analysis: show block and slot number and hash for all blocks
296297
-------------------------------------------------------------------------------}
297298

298299
showSlotBlockNo :: forall blk. HasAnalysis blk => Analysis blk StartFromPoint
@@ -301,7 +302,8 @@ showSlotBlockNo AnalysisEnv { db, registry, startFrom, limit, tracer } =
301302
>> pure Nothing
302303
where
303304
process :: Header blk -> IO ()
304-
process hdr = traceWith tracer $ BlockSlotEvent (blockNo hdr) (blockSlot hdr)
305+
process hdr = traceWith tracer $
306+
BlockSlotEvent (blockNo hdr) (blockSlot hdr) (headerHash hdr)
305307

306308
{-------------------------------------------------------------------------------
307309
Analysis: show total number of tx outputs per block
@@ -419,7 +421,7 @@ storeLedgerStateAt slotNo (AnalysisEnv { db, registry, startFrom, cfg, limit, le
419421

420422
issueWarning blk = let event = SnapshotWarningEvent slotNo (blockSlot blk)
421423
in traceWith tracer event
422-
reportProgress blk = let event = BlockSlotEvent (blockNo blk) (blockSlot blk)
424+
reportProgress blk = let event = BlockSlotEvent (blockNo blk) (blockSlot blk) (blockHash blk)
423425
in traceWith tracer event
424426

425427
storeLedgerState ::

0 commit comments

Comments
 (0)