Skip to content

Commit

Permalink
Use newtype derived Arbitrary where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo committed Sep 4, 2024
1 parent b19e0b3 commit 3ab8034
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
4 changes: 1 addition & 3 deletions hydra-tx/src/Hydra/Tx/Party.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Hydra.Tx.Crypto (AsType (AsHydraKey), HydraKey)
newtype Party = Party {vkey :: VerificationKey HydraKey}
deriving stock (Eq, Show, Generic)
deriving anyclass (ToJSON, FromJSON)
deriving newtype (Arbitrary)

instance ToJSONKey Party where
toJSONKey = toJSONKeyText (serialiseToRawBytesHexText . vkey)
Expand All @@ -43,9 +44,6 @@ instance Ord Party where
Party{vkey = a} <= Party{vkey = b} =
verificationKeyHash a <= verificationKeyHash b

instance Arbitrary Party where
arbitrary = Party <$> arbitrary

instance FromCBOR Party where
fromCBOR = Party <$> fromCBOR

Expand Down
10 changes: 2 additions & 8 deletions hydra-tx/src/Hydra/Tx/Snapshot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,23 @@ import Test.QuickCheck.Instances.Natural ()
newtype SnapshotNumber
= UnsafeSnapshotNumber Natural
deriving stock (Eq, Ord, Generic)
deriving newtype (Show, ToJSON, FromJSON, ToCBOR, FromCBOR, Real, Num, Enum, Integral)
deriving newtype (Show, ToJSON, FromJSON, ToCBOR, FromCBOR, Real, Num, Enum, Integral, Arbitrary)

-- NOTE: On-chain scripts ensure snapshot number does not become negative.
fromChainSnapshotNumber :: Onchain.SnapshotNumber -> SnapshotNumber
fromChainSnapshotNumber =
UnsafeSnapshotNumber . fromMaybe 0 . integerToNatural

instance Arbitrary SnapshotNumber where
arbitrary = UnsafeSnapshotNumber <$> arbitrary

newtype SnapshotVersion
= UnsafeSnapshotVersion Natural
deriving stock (Eq, Ord, Generic)
deriving newtype (Show, ToJSON, FromJSON, ToCBOR, FromCBOR, Real, Num, Enum, Integral)
deriving newtype (Show, ToJSON, FromJSON, ToCBOR, FromCBOR, Real, Num, Enum, Integral, Arbitrary)

-- NOTE: On-chain scripts ensure snapshot version does not become negative.
fromChainSnapshotVersion :: Onchain.SnapshotVersion -> SnapshotVersion
fromChainSnapshotVersion =
UnsafeSnapshotVersion . fromMaybe 0 . integerToNatural

instance Arbitrary SnapshotVersion where
arbitrary = UnsafeSnapshotVersion <$> arbitrary

-- * Snapshot

data Snapshot tx = Snapshot
Expand Down

0 comments on commit 3ab8034

Please sign in to comment.