Skip to content

Commit

Permalink
Derive Keyed instances for some Hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Oct 26, 2021
1 parent 569a103 commit fa20ff3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions libs/cardano-ledger-core/cardano-ledger-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ library
cardano-ledger-byron,
cardano-prelude,
cardano-slotting,
compact-map,
containers,
data-default-class,
deepseq,
Expand Down
5 changes: 5 additions & 0 deletions libs/cardano-ledger-core/src/Cardano/Ledger/AuxiliaryData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Hashes (EraIndependentAuxiliaryData)
import Cardano.Ledger.SafeHash (SafeHash)
import Control.DeepSeq (NFData (..))
import Data.Compact.HashMap (Keyed)
import NoThunks.Class (NoThunks (..))

newtype AuxiliaryDataHash crypto = AuxiliaryDataHash
{ unsafeAuxiliaryDataHash :: SafeHash crypto EraIndependentAuxiliaryData
}
deriving (Show, Eq, Ord, NoThunks, NFData)

deriving newtype instance
CC.Crypto crypto =>
Keyed (AuxiliaryDataHash crypto)

deriving instance
CC.Crypto crypto =>
ToCBOR (AuxiliaryDataHash crypto)
Expand Down
11 changes: 5 additions & 6 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Hashes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Cardano.Ledger.Crypto (ADDRHASH)
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Control.DeepSeq (NFData)
import Data.Aeson
import Data.Compact.HashMap (Keyed)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))

Expand Down Expand Up @@ -75,13 +76,11 @@ newtype ScriptHash crypto
deriving (Show, Eq, Ord, Generic)
deriving newtype (NFData, NoThunks)

deriving newtype instance
CC.Crypto crypto =>
ToCBOR (ScriptHash crypto)
deriving newtype instance CC.Crypto crypto => Keyed (ScriptHash crypto)

deriving newtype instance
CC.Crypto crypto =>
FromCBOR (ScriptHash crypto)
deriving newtype instance CC.Crypto crypto => ToCBOR (ScriptHash crypto)

deriving newtype instance CC.Crypto crypto => FromCBOR (ScriptHash crypto)

deriving newtype instance CC.Crypto crypto => ToJSON (ScriptHash crypto)

Expand Down
3 changes: 3 additions & 0 deletions libs/cardano-ledger-core/src/Cardano/Ledger/SafeHash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Cardano.Prelude (HeapWords (..))
import Control.DeepSeq (NFData)
import Data.ByteString (ByteString)
import Data.ByteString.Short (ShortByteString, fromShort)
import Data.Compact.HashMap (Keyed)
import Data.Foldable (fold)
import Data.MemoBytes (MemoBytes (..))
import Data.Typeable
Expand All @@ -55,6 +56,8 @@ deriving newtype instance
Hash.HashAlgorithm (CC.HASH crypto) =>
SafeToHash (SafeHash crypto index)

deriving newtype instance CC.Crypto crypto => Keyed (SafeHash crypto index)

deriving newtype instance HeapWords (Hash.Hash (CC.HASH c) i) => HeapWords (SafeHash c i)

deriving instance (Typeable index, CC.Crypto c) => ToCBOR (SafeHash c index)
Expand Down
9 changes: 4 additions & 5 deletions libs/cardano-ledger-core/src/Cardano/Ledger/TxIn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ import Cardano.Ledger.Core (TxBody)
import qualified Cardano.Ledger.Crypto as CC
import Cardano.Ledger.Era (Crypto, Era)
import Cardano.Ledger.Hashes (EraIndependentTxBody)
import Cardano.Ledger.SafeHash
( SafeHash,
hashAnnotated,
)
import Cardano.Ledger.SafeHash (SafeHash, hashAnnotated)
import Cardano.Ledger.Serialization (decodeRecordNamed)
import Cardano.Prelude (HeapWords (..), NFData, cborError)
import qualified Cardano.Prelude as HW
Expand Down Expand Up @@ -74,6 +71,8 @@ newtype TxId crypto = TxId {_unTxId :: SafeHash crypto EraIndependentTxBody}
deriving (Show, Eq, Ord, Generic)
deriving newtype (NoThunks, HeapWords)

deriving newtype instance CC.Crypto crypto => Keyed (TxId crypto)

deriving newtype instance CC.Crypto crypto => ToCBOR (TxId crypto)

deriving newtype instance CC.Crypto crypto => FromCBOR (TxId crypto)
Expand All @@ -90,7 +89,7 @@ data TxIn crypto = TxInCompact !(TxId crypto) {-# UNPACK #-} !Int

pattern TxIn ::
TxId crypto ->
Natural -> -- TODO We might want to change this to Word64 generally
Natural -> -- TODO We might want to change this to Int generally
TxIn crypto
pattern TxIn addr index <-
TxInCompact addr (fromIntegral -> index)
Expand Down

0 comments on commit fa20ff3

Please sign in to comment.