IntersectMBO / IntersectMBO/ouroboros-consensus

Avoid recomputing the header hash when deserialising from storage

Open
#680 0 comments 0 reactions 0 assignees View on GitHub
🏎️ performance
Dominant language
Haskell
Stars
67
Forks
43
Avg merge
5d 13h
Merged PRs (30d)
43

Description

When deserialising a block or header from disk, we use [`mkByronBlock`](https://github.com/input-output-hk/ouroboros-network/blob/38c50e8060f4debef3a50606b4b84179801403f1/ouroboros-consensus-byron/src/Ouroboros/Consensus/Byron/Ledger/Block.hs#L99) or [`mkByronHeader`](https://github.com/input-output-hk/ouroboros-network/blob/38c50e8060f4debef3a50606b4b84179801403f1/ouroboros-consensus-byron/src/Ouroboros/Consensus/Byron/Ledger/Block.hs#L168), and [`mkShelleyBlock`](https://github.com/input-output-hk/ouroboros-network/blob/e482fc9e48db5857a0773df5d686ec52f586cf35/ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Ledger/Block.hs#L101) or [`mkShelleyHeader`](https://github.com/input-output-hk/ouroboros-network/blob/e482fc9e48db5857a0773df5d686ec52f586cf35/ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Ledger/Block.hs#L129). Those functions recompute the hash of the header ([here](https://github.com/input-output-hk/ouroboros-network/blob/38c50e8060f4debef3a50606b4b84179801403f1/ouroboros-consensus-byron/src/Ouroboros/Consensus/Byron/Ledger/Block.hs#L70) and [here](https://github.com/input-output-hk/ouroboros-network/blob/e482fc9e48db5857a0773df5d686ec52f586cf35/ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Ledger/Block.hs#L104)) each time.

When deserialising from the network, this makes sense. If we just received the hash over the network, we'd still have to recompute it to see whether the one we received is correct. But for deserialising from disk, we already have checksums of blocks to detect corruption (although we don't have one for the header), and we already have the hash easily available.

To avoid this computation when deserialising a block from disk, we could change the `DecodeDisk blk blk` constraint to `DecodeDisk blk (HeaderHash blk -> blk)` and pass in the `HeaderHash blk` from the index (secondary index for the ImmutableDB, the in-memory index for the VolatileDB). Similarly for headers.

Note that when serving blocks or headers to other nodes, we already do binary streaming, so we don't deserialise them. This optimisation would thus only help when we need to deserialise a block because we need it in chain selection/ledger validation.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.