From d474b630d67d6548172874cfe7a1c87e3c616927 Mon Sep 17 00:00:00 2001 From: Damian Nadales Date: Fri, 27 Sep 2019 19:22:32 +0200 Subject: [PATCH] Add to cbor instances and HasTypeReps for ShortHash (#890) - Add a `ToCBOR` instance for `VKey` - Add a `HasTypeReps` instance for `Hash ShortHash a`. - Import Natural explicitly - Add a `HasTypeReps` instance for `ShortHash`. - Bump `cardano-prelude`. - Bump `iohk-nix`. - Bump `cardano-base` version. --- byron/ledger/executable-spec/cs-ledger.cabal | 2 + .../ledger/executable-spec/src/Ledger/Core.hs | 10 ++-- .../executable-spec/small-steps.cabal | 2 + .../executable-spec/src/Data/AbstractSize.hs | 49 +++++++++++++------ nix/.stack.nix/cardano-binary.nix | 4 +- nix/.stack.nix/cardano-crypto-class.nix | 4 +- nix/.stack.nix/cardano-prelude.nix | 5 +- nix/.stack.nix/cs-ledger.nix | 1 + nix/.stack.nix/small-steps.nix | 1 + nix/iohk-nix-src.json | 6 +-- stack.yaml | 6 +-- 11 files changed, 58 insertions(+), 32 deletions(-) diff --git a/byron/ledger/executable-spec/cs-ledger.cabal b/byron/ledger/executable-spec/cs-ledger.cabal index 86427148d41..16a7b47981e 100644 --- a/byron/ledger/executable-spec/cs-ledger.cabal +++ b/byron/ledger/executable-spec/cs-ledger.cabal @@ -49,6 +49,8 @@ library , lens , template-haskell , Unique >= 0.4.7.6 + -- IOHK deps + , cardano-binary -- Local deps , small-steps default-language: Haskell2010 diff --git a/byron/ledger/executable-spec/src/Ledger/Core.hs b/byron/ledger/executable-spec/src/Ledger/Core.hs index f2857ddedfe..562cd275e60 100644 --- a/byron/ledger/executable-spec/src/Ledger/Core.hs +++ b/byron/ledger/executable-spec/src/Ledger/Core.hs @@ -33,6 +33,8 @@ import Data.Word (Word64, Word8) import GHC.Generics (Generic) import Numeric.Natural (Natural) +import Cardano.Binary (ToCBOR) + import Data.AbstractSize import Test.Goblin (AddShrinks (..), GeneOps, Goblin (..), SeedGoblin (..), @@ -59,7 +61,7 @@ class HasHash a where newtype Owner = Owner { unOwner :: Natural } deriving stock (Show, Generic, Data, Typeable) - deriving newtype (Eq, Ord, Hashable) + deriving newtype (Eq, Ord, Hashable, ToCBOR) deriving anyclass (HasTypeReps) class HasOwner a where @@ -77,7 +79,7 @@ instance HasOwner SKey where -- |Verification Key. newtype VKey = VKey Owner deriving stock (Show, Generic, Data, Typeable) - deriving newtype (Eq, Ord, Hashable) + deriving newtype (Eq, Ord, Hashable, ToCBOR) deriving anyclass (HasTypeReps) instance HasHash VKey where @@ -87,7 +89,7 @@ instance HasOwner VKey where owner (VKey o) = o -- | A genesis key is a specialisation of a generic VKey. -newtype VKeyGenesis = VKeyGenesis { unVKeyGenesis :: VKey} +newtype VKeyGenesis = VKeyGenesis { unVKeyGenesis :: VKey } deriving stock (Show, Generic, Data, Typeable) deriving newtype (Eq, Ord, Hashable, HasHash) deriving anyclass (HasTypeReps) @@ -128,7 +130,7 @@ data Sig a = Sig a Owner deriving (Show, Eq, Ord, Generic, Hashable, Typeable, D -- 'typeReps' to compute 'abstractSize', this would mean the size of -- 'Sig a' would include the size of 'a' (e.g. 'Tx'). This would create an -- artificial coupling between the size of a type and it's "signature". -instance HasTypeReps a => HasTypeReps (Sig a) where +instance Typeable a => HasTypeReps (Sig a) where typeReps x = typeOf x Seq.<| Seq.empty -- |Produce a digital signature diff --git a/byron/semantics/executable-spec/small-steps.cabal b/byron/semantics/executable-spec/small-steps.cabal index 04e427f6f62..dcbf4a26211 100644 --- a/byron/semantics/executable-spec/small-steps.cabal +++ b/byron/semantics/executable-spec/small-steps.cabal @@ -40,6 +40,8 @@ library , lens , mtl , transformers >= 0.5 + -- IOHK deps + , cardano-crypto-class hs-source-dirs: src default-language: Haskell2010 ghc-options: -Wall diff --git a/byron/semantics/executable-spec/src/Data/AbstractSize.hs b/byron/semantics/executable-spec/src/Data/AbstractSize.hs index e5e62edac3b..34a18b10105 100644 --- a/byron/semantics/executable-spec/src/Data/AbstractSize.hs +++ b/byron/semantics/executable-spec/src/Data/AbstractSize.hs @@ -1,8 +1,8 @@ {-# LANGUAGE DefaultSignatures #-} -{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE OverloadedLists #-} -{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE OverloadedLists #-} +{-# LANGUAGE TypeOperators #-} -- | An approach to computing the abstract size of data using 'TypeRep'. -- @@ -14,16 +14,19 @@ module Data.AbstractSize , Size ) where -import qualified Crypto.Hash as Crypto +import qualified Crypto.Hash as Crypto import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map -import Data.Sequence (Seq, empty, (<|), (><)) -import Data.Set (Set) -import Data.Typeable (TypeRep, Typeable, typeOf) -import Data.Word (Word64) -import GHC.Generics ((:*:) ((:*:)), (:+:) (L1, R1), Generic, - K1 (K1), M1 (M1), Rep, U1 (U1), from) -import GHC.Natural +import Data.Sequence (Seq, empty, (<|), (><)) +import Data.Set (Set) +import Data.Typeable (TypeRep, Typeable, typeOf) +import Data.Word (Word64) +import GHC.Generics ((:*:) ((:*:)), (:+:) (L1, R1), Generic, K1 (K1), M1 (M1), Rep, + U1 (U1), from) +import GHC.Natural (Natural) + +import Cardano.Crypto.Hash (Hash) +import Cardano.Crypto.Hash.Short (ShortHash) -- | @abstractSize m a@ computes the abstract size of @a@, using the accounting -- map @m@. The map @m@ determines the abstract size of each 'TypeRep' @@ -96,10 +99,14 @@ type AccountingMap = Map TypeRep Size -- >>> typeReps $ Foo [1, 2] ('a', 'b') -- fromList [Foo,[Int],Int,Int,(Char,Char),Char,Char] -- -class Typeable a => HasTypeReps a where +class HasTypeReps a where typeReps :: a -> Seq TypeRep - default typeReps :: (Generic a, GHasTypeReps (Rep a)) => a -> Seq TypeRep + default typeReps + :: ( Generic a + , GHasTypeReps (Rep a) + , Typeable a + ) => a -> Seq TypeRep typeReps a = typeOf a <| gTypeReps (from a) class GHasTypeReps f where @@ -134,13 +141,17 @@ instance (HasTypeReps a) => GHasTypeReps (K1 i a) where -- HasTypeReps instances -------------------------------------------------------------------------------- -instance HasTypeReps a => HasTypeReps [a] where +instance (Typeable a, HasTypeReps a) => HasTypeReps [a] where typeReps xs = typeOf xs <| foldMap typeReps xs -instance HasTypeReps a => HasTypeReps (Set a) where +instance (Typeable a, HasTypeReps a) => HasTypeReps (Set a) where typeReps xs = typeOf xs <| foldMap typeReps xs -instance (HasTypeReps a, HasTypeReps b) => HasTypeReps (a, b) where +instance ( Typeable a + , Typeable b + , HasTypeReps a + , HasTypeReps b + ) => HasTypeReps (a, b) where typeReps t@(a, b) = typeOf t <| (typeReps a >< typeReps b) instance HasTypeReps Bool where @@ -166,3 +177,9 @@ instance HasTypeReps Word64 where instance HasTypeReps (Crypto.Digest Crypto.SHA256) where typeReps x = [typeOf x] + +instance HasTypeReps ShortHash where + typeReps x = [typeOf x] + +instance Typeable a => HasTypeReps (Hash ShortHash a) where + typeReps x = [typeOf x] diff --git a/nix/.stack.nix/cardano-binary.nix b/nix/.stack.nix/cardano-binary.nix index 61c236910ae..2636d366e46 100644 --- a/nix/.stack.nix/cardano-binary.nix +++ b/nix/.stack.nix/cardano-binary.nix @@ -59,8 +59,8 @@ } // { src = (pkgs.lib).mkDefault (pkgs.fetchgit { url = "https://github.com/input-output-hk/cardano-base"; - rev = "1c9e91cc8cc1deecdd7696d0f7b0b74b8984aa93"; - sha256 = "13f6dy3r4x2arh6zyayz9g05wj3m9lq60d266jppx520z4i5xcl6"; + rev = "80f25cde254d523f34d9804e6e009925d9775adb"; + sha256 = "1c5f43fh9s0b8jmy9b0yc11bbjz0zc9fydjj2nh66jivnl04wwjm"; }); postUnpack = "sourceRoot+=/binary; echo source root reset to \$sourceRoot"; } \ No newline at end of file diff --git a/nix/.stack.nix/cardano-crypto-class.nix b/nix/.stack.nix/cardano-crypto-class.nix index 22f16b2676d..e1c6d46ad78 100644 --- a/nix/.stack.nix/cardano-crypto-class.nix +++ b/nix/.stack.nix/cardano-crypto-class.nix @@ -47,8 +47,8 @@ } // { src = (pkgs.lib).mkDefault (pkgs.fetchgit { url = "https://github.com/input-output-hk/cardano-base"; - rev = "1c9e91cc8cc1deecdd7696d0f7b0b74b8984aa93"; - sha256 = "13f6dy3r4x2arh6zyayz9g05wj3m9lq60d266jppx520z4i5xcl6"; + rev = "80f25cde254d523f34d9804e6e009925d9775adb"; + sha256 = "1c5f43fh9s0b8jmy9b0yc11bbjz0zc9fydjj2nh66jivnl04wwjm"; }); postUnpack = "sourceRoot+=/cardano-crypto-class; echo source root reset to \$sourceRoot"; } \ No newline at end of file diff --git a/nix/.stack.nix/cardano-prelude.nix b/nix/.stack.nix/cardano-prelude.nix index 7a3fc677aba..035b44f21d3 100644 --- a/nix/.stack.nix/cardano-prelude.nix +++ b/nix/.stack.nix/cardano-prelude.nix @@ -60,6 +60,7 @@ (hsPkgs.pretty-show) (hsPkgs.QuickCheck) (hsPkgs.quickcheck-instances) + (hsPkgs.random) (hsPkgs.text) (hsPkgs.template-haskell) (hsPkgs.time) @@ -70,7 +71,7 @@ } // { src = (pkgs.lib).mkDefault (pkgs.fetchgit { url = "https://github.com/input-output-hk/cardano-prelude"; - rev = "12ab51e27539c9cce042ded0c89efc0ccae6137a"; - sha256 = "1j8ac1286grk3c9j10i7az30q6f605r7d302hnd22c9pzj7c0lhy"; + rev = "96e8dcb29dc3c29eee99c0d020152fad6071af6d"; + sha256 = "1ayb5h048qmwvmgzm4ckzg7x8pcscd8znlhrj0438vv8wbvx77rl"; }); } \ No newline at end of file diff --git a/nix/.stack.nix/cs-ledger.nix b/nix/.stack.nix/cs-ledger.nix index 657cfc4bab7..ad760f87aa5 100644 --- a/nix/.stack.nix/cs-ledger.nix +++ b/nix/.stack.nix/cs-ledger.nix @@ -28,6 +28,7 @@ (hsPkgs.lens) (hsPkgs.template-haskell) (hsPkgs.Unique) + (hsPkgs.cardano-binary) (hsPkgs.small-steps) ]; }; diff --git a/nix/.stack.nix/small-steps.nix b/nix/.stack.nix/small-steps.nix index 69a23d98c74..d6eb5cdb79d 100644 --- a/nix/.stack.nix/small-steps.nix +++ b/nix/.stack.nix/small-steps.nix @@ -27,6 +27,7 @@ (hsPkgs.lens) (hsPkgs.mtl) (hsPkgs.transformers) + (hsPkgs.cardano-crypto-class) ]; }; tests = { diff --git a/nix/iohk-nix-src.json b/nix/iohk-nix-src.json index 4aefa58c7b4..863afb4d795 100644 --- a/nix/iohk-nix-src.json +++ b/nix/iohk-nix-src.json @@ -1,7 +1,7 @@ { "url": "https://github.com/input-output-hk/iohk-nix", - "rev": "aa26de40d6bb226fade6e3f6e80ef6e0ef70925b", - "date": "2019-07-31T11:56:57+00:00", - "sha256": "0aqvhm5whr2bzcxbzn69q31i00cdsqqbpay00z69a0vxkv2568rz", + "rev": "11ff3539c21665f9ac20cdf165af632312372b45", + "date": "2019-09-26T16:58:22+00:00", + "sha256": "0bvxjmx341f0ppazcikn9qmdi6nkqgnmw9gj3dkhz9dn6hw838mg", "fetchSubmodules": false } diff --git a/stack.yaml b/stack.yaml index 3419e3409e1..0756e41548e 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,4 +1,4 @@ -resolver: https://raw.githubusercontent.com/input-output-hk/cardano-prelude/12ab51e27539c9cce042ded0c89efc0ccae6137a/snapshot.yaml +resolver: https://raw.githubusercontent.com/input-output-hk/cardano-prelude/96e8dcb29dc3c29eee99c0d020152fad6071af6d/snapshot.yaml packages: - shelley/chain-and-ledger/executable-spec @@ -14,10 +14,10 @@ extra-deps: - bimap-0.4.0 - git: https://github.com/input-output-hk/cardano-prelude - commit: 12ab51e27539c9cce042ded0c89efc0ccae6137a + commit: 96e8dcb29dc3c29eee99c0d020152fad6071af6d - git: https://github.com/input-output-hk/cardano-base - commit: 1c9e91cc8cc1deecdd7696d0f7b0b74b8984aa93 + commit: 80f25cde254d523f34d9804e6e009925d9775adb subdirs: - binary - cardano-crypto-class