IntersectMBO / IntersectMBO/cardano-ledger
Create a `hashTx` function
- Dominant language
- Haskell
- Stars
- 295
- Forks
- 179
- Avg merge
- 4d 7h
- Merged PRs (30d)
- 29
Description
~Blocked on #5912~
Starting with Dijkstra era we will need to have deterministic hash computation (i.e. not susceptible to non-canonical CBOR) for the purpose of identification of transactions as they are to be included in the block needed for the Leios protocol.
Therefore we need a new type class:
```haskell
class AlonzoEraTx era => DijkstraEraTx where
hashTx :: Tx TopTx era -> SafeHash EraIndependentTx
```
The nice part of this is that we do not need the original bytes, we just need to make sure that we have a unique content of full transaction identified. This means that instead of hashing the bytes of full transaction or the bytes of individual component of a transaction, we can hash the concatenation of hashes of those individual components.
In concrete terms we need this functionality for Dijkstra era:
```haskell
instance DijkstraEraTx DijkstraEra where
hashTx =
unsafeMakeSafeHash $ coerce $ hashWith $ \tx ->
hashToBytes (hashAnnotated (tx ^. bodyTxL)) <>
hashToBytes (hashAnnotated (tx ^. witsTxL)) <>
hashToBytes (hashAnnotated (tx ^. auxDataTxL)) <>
serialize' toCBOR (tx ^. isValidTxL)
```
`hashAnnotated` on `MemoBytes` is memoized, so we'll get the benefit of reusing the hashes of those components for other purposes (`txId`, `auxDataHash`)
Time permitting extra optimization would be to not use the usual mechanism of concatenating hashes, but instead to use [`IncrementalHashAlgorithm`](https://github.com/IntersectMBO/cardano-base/blob/720c7b6d0f226077706019d671bdde6eaa17a5b2/cardano-crypto-class/src/Cardano/Crypto/Hash/Class.hs#L395) to compute the final hash. This can be turned into a follow up ticket taht also adds benchmarks showing the difference in perforemance, instead of being handled as part of this ticket.
Contributor guide
Research direction
Resolve the dependency on issue #5912 first, then locate the DijkstraEraTx class and DijkstraEra instance described here. Implement deterministic hashing from the body, witnesses, auxiliary data, and validity flag, and verify that the result is a SafeHash EraIndependentTx; the IncrementalHashAlgorithm optimization is explicitly a possible follow-up.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- blockchain, cryptography
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100