cardano-ledger-core-1.16.0.0: Core components of Cardano ledgers from the Shelley release on.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cardano.Ledger.SafeHash

Description

In cardano-ledger, hashing a type X is based upon the serialization of X. Serialization is based upon the EncCBOR and DecCBOR type classes, and the values produced by EncCBOR instances for a particular type, are not necessarily unique. For this reason, when an X object comes over the network in serialized form, we must preserve the original bytes that arrived over the network, otherwise when the system hashes that object, the hash in the ledger, and the hash of that object from the other side of the network may not agree. The module SafeHash introduces the SafeToHash type class that ensures that types with a (SafeToHash X) instance store the original bytes that arrived over the network for the value of X. The recommended way to store the original bytes is to use the type MemoBytes, although there are a few types that store their original bytes in other ways. In order to encourage the use of MemoBytes newtypes defined as a MemoBytes get the to derive SafeToHash instances for free.

Synopsis

SafeHash and SafeToHash

data SafeHash c index Source #

A SafeHash is a hash of something that is safe to hash. Such types store their own serialisation bytes. The prime example is (MemoBytes t), but other examples are things that consist of only ByteStrings (i.e. they are their own serialization) or for some other reason store their original bytes.

We do NOT export the constructor SafeHash, but instead export other functions such as 'hashWithCrypto, hashAnnotated and extractHash which have constraints that limit their application to types which preserve their original serialization bytes.

Instances

Instances details
Crypto c ⇒ HeapWords (StrictMaybe (DataHash c)) Source # 
Instance details

Defined in Cardano.Ledger.Plutus.Data

Crypto c ⇒ FromJSON (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

parseJSONValueParser (SafeHash c index) Source #

parseJSONListValueParser [SafeHash c index] Source #

omittedFieldMaybe (SafeHash c index) Source #

Crypto c ⇒ ToJSON (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

toJSONSafeHash c index → Value Source #

toEncodingSafeHash c index → Encoding Source #

toJSONList ∷ [SafeHash c index] → Value Source #

toEncodingList ∷ [SafeHash c index] → Encoding Source #

omitFieldSafeHash c index → Bool Source #

Show (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

showsPrecIntSafeHash c index → ShowS Source #

showSafeHash c index → String Source #

showList ∷ [SafeHash c index] → ShowS Source #

(Typeable index, Crypto c) ⇒ FromCBOR (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

fromCBORDecoder s (SafeHash c index) Source #

labelProxy (SafeHash c index) → Text Source #

(Typeable index, Crypto c) ⇒ ToCBOR (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

toCBORSafeHash c index → Encoding Source #

encodedSizeExpr ∷ (∀ t. ToCBOR t ⇒ Proxy t → Size) → Proxy (SafeHash c index) → Size Source #

encodedListSizeExpr ∷ (∀ t. ToCBOR t ⇒ Proxy t → Size) → Proxy [SafeHash c index] → Size Source #

(Typeable index, Crypto c) ⇒ DecCBOR (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

decCBORDecoder s (SafeHash c index) Source #

dropCBORProxy (SafeHash c index) → Decoder s () Source #

labelProxy (SafeHash c index) → Text Source #

(Typeable index, Crypto c) ⇒ EncCBOR (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

encCBORSafeHash c index → Encoding Source #

encodedSizeExpr ∷ (∀ t. EncCBOR t ⇒ Proxy t → Size) → Proxy (SafeHash c index) → Size Source #

encodedListSizeExpr ∷ (∀ t. EncCBOR t ⇒ Proxy t → Size) → Proxy [SafeHash c index] → Size Source #

HashAlgorithm (HASH c) ⇒ SafeToHash (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

originalBytesSafeHash c index → ByteString Source #

originalBytesSizeSafeHash c index → Int Source #

makeHashWithExplicitProxysHashAlgorithm (HASH c0) ⇒ Proxy c0 → Proxy index0 → SafeHash c index → SafeHash c0 index0 Source #

Crypto c ⇒ Default (SafeHash c i) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

defSafeHash c i Source #

NFData (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

rnfSafeHash c index → () Source #

Eq (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

(==)SafeHash c index → SafeHash c index → Bool Source #

(/=)SafeHash c index → SafeHash c index → Bool Source #

Ord (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

compareSafeHash c index → SafeHash c index → Ordering Source #

(<)SafeHash c index → SafeHash c index → Bool Source #

(<=)SafeHash c index → SafeHash c index → Bool Source #

(>)SafeHash c index → SafeHash c index → Bool Source #

(>=)SafeHash c index → SafeHash c index → Bool Source #

maxSafeHash c index → SafeHash c index → SafeHash c index Source #

minSafeHash c index → SafeHash c index → SafeHash c index Source #

HeapWords (SafeHash c i) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

heapWordsSafeHash c i → Int Source #

NoThunks (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

noThunksContextSafeHash c index → IO (Maybe ThunkInfo) Source #

wNoThunksContextSafeHash c index → IO (Maybe ThunkInfo) Source #

showTypeOfProxy (SafeHash c index) → String Source #

class SafeToHash t where Source #

Only Types that preserve their serialisation bytes are members of the class SafeToHash. There are only a limited number of primitive direct instances of SafeToHash, all but two of them are present in this file. Instead of making explicit instances, we almost always use a newtype (around a type S) where their is already an instance (SafeToHash S). In that case the newtype has its SafeToHash instance derived using newtype deriving. The prime example of s is MemoBytes. The only exceptions are the legacy Shelley types: Metadata and ShelleyTx, that preserve their serialization bytes using a different mechanism than the use of MemoBytes. SafeToHash is a superclass requirement of the classes HashAnnotated and HashWithCrypto (below) which provide more convenient ways to construct SafeHashes than using makeHashWithExplicitProxys.

Minimal complete definition

originalBytes

Methods

originalBytes ∷ t → ByteString Source #

Extract the original bytes from t

originalBytesSize ∷ t → Int Source #

makeHashWithExplicitProxysHashAlgorithm (HASH c) ⇒ Proxy c → Proxy index → t → SafeHash c index Source #

Instances

Instances details
SafeToHash ByteString Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

SafeToHash ShortByteString Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

SafeToHash AnchorData Source # 
Instance details

Defined in Cardano.Ledger.BaseTypes

SafeToHash PlutusBinary Source # 
Instance details

Defined in Cardano.Ledger.Plutus.Language

SafeToHash (BinaryData era) Source # 
Instance details

Defined in Cardano.Ledger.Plutus.Data

SafeToHash (Data era) Source # 
Instance details

Defined in Cardano.Ledger.Plutus.Data

SafeToHash (Plutus l) Source # 
Instance details

Defined in Cardano.Ledger.Plutus.Language

HashAlgorithm c ⇒ SafeToHash (Hash c i) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

originalBytesHash c i → ByteString Source #

originalBytesSizeHash c i → Int Source #

makeHashWithExplicitProxysHashAlgorithm (HASH c0) ⇒ Proxy c0 → Proxy index → Hash c i → SafeHash c0 index Source #

SafeToHash (MemoBytes t era) Source # 
Instance details

Defined in Cardano.Ledger.MemoBytes.Internal

HashAlgorithm (HASH c) ⇒ SafeToHash (SafeHash c index) Source # 
Instance details

Defined in Cardano.Ledger.SafeHash

Methods

originalBytesSafeHash c index → ByteString Source #

originalBytesSizeSafeHash c index → Int Source #

makeHashWithExplicitProxysHashAlgorithm (HASH c0) ⇒ Proxy c0 → Proxy index0 → SafeHash c index → SafeHash c0 index0 Source #

Creating SafeHash

class SafeToHash x ⇒ HashAnnotated x index c | x → index c Source #

Types that are SafeToHash, AND have both of the following two invariants, are made members of the HashAnnotated class. The preconditions are:

  1. The type uniquely determines the index type tag of (SafeHash crypto index)
  2. The type uniquely determines the crypto type of (SafeHash crytop index)

The SafeToHash and the HashAnnotated classes are designed so that their instances can be easily derived (because their methods have default methods when the type is a newtype around a type that is SafeToHash). For example,

  newtype T era = T S
     deriving Eq
     deriving newtype SafeToHash -- Uses {-# LANGUAGE DerivingStrategies #-}

  instance HashAnnotated (T era) Index (Crypto era)

After these declarations. One specialization of hashAnnotated is (hashAnnotated :: Era e => T e -> SafeHash (Crypto e) Index)

hashAnnotated ∷ (HashAnnotated x index c, HashAlgorithm (HASH c)) ⇒ x → SafeHash c index Source #

Create a (SafeHash i crypto), given (Hash.HashAlgorithm (HASH crypto)) and (HashAnnotated x i crypto) instances.

class SafeToHash x ⇒ HashWithCrypto x index | x → index where Source #

Create (SafeHash index crypto) values, used when the type being hashed: x determines the index tag but not the crypto tag of x

Minimal complete definition

Nothing

Methods

hashWithCrypto ∷ ∀ c. HashAlgorithm (HASH c) ⇒ Proxy c → x → SafeHash c index Source #

Create a (SafeHash index crypto) value from x, the proxy determines the crypto.

Instances

Instances details
HashWithCrypto AnchorData AnchorData Source # 
Instance details

Defined in Cardano.Ledger.BaseTypes

unsafeMakeSafeHashHash (HASH c) index → SafeHash c index Source #

Don't use this except in Testing to make Arbitrary instances, etc. Defined here, only because the Constructor is in scope here.

Other operations

castSafeHash ∷ ∀ i j c. SafeHash c i → SafeHash c j Source #

To change the index parameter of SafeHash (which is a phantom type) use castSafeHash

extractHashSafeHash c i → Hash (HASH c) i Source #

Extract the hash out of a SafeHash

indexProxyHashAnnotated x index c ⇒ x → Proxy index Source #