Skip to content

Commit

Permalink
Add to cbor instances and HasTypeReps for ShortHash (#890)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
dnadales committed Sep 27, 2019
1 parent 319613c commit d474b63
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 32 deletions.
2 changes: 2 additions & 0 deletions byron/ledger/executable-spec/cs-ledger.cabal
Expand Up @@ -49,6 +49,8 @@ library
, lens
, template-haskell
, Unique >= 0.4.7.6
-- IOHK deps
, cardano-binary
-- Local deps
, small-steps
default-language: Haskell2010
Expand Down
10 changes: 6 additions & 4 deletions byron/ledger/executable-spec/src/Ledger/Core.hs
Expand Up @@ -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 (..),
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions byron/semantics/executable-spec/small-steps.cabal
Expand Up @@ -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
Expand Down
49 changes: 33 additions & 16 deletions 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'.
--
Expand All @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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]
4 changes: 2 additions & 2 deletions nix/.stack.nix/cardano-binary.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions nix/.stack.nix/cardano-crypto-class.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions nix/.stack.nix/cardano-prelude.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nix/.stack.nix/cs-ledger.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nix/.stack.nix/small-steps.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions 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
}
6 changes: 3 additions & 3 deletions 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
Expand All @@ -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
Expand Down

0 comments on commit d474b63

Please sign in to comment.