From 07b7ad9d62eb5c84e31a6d5bc7ff5d4f4c5e2e29 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Wed, 11 Oct 2023 07:14:06 +0000 Subject: [PATCH] Move `Compatibility.{Ledger,LedgerSpec}` to `conversions` package. The `Compatibility.Ledger` module was located in `cardano-balance-tx`. The `Compatibility.LedgerSpec` module was located in `cardano-wallet`. This commit co-locates both modules within a new package called `cardano-wallet-conversions`. This makes it possible for both `cardano-wallet` and `cardano-balance-tx` to depend upon these conversion functions without having to export them from the public interface of `cardano-balance-tx`. Note that in order to perform this relocation, this commit also clones the definition of a single QuickCheck generator: `Cardano.Wallet.Gen.genScript`. --- cabal.project | 1 + lib/balance-tx/cardano-balance-tx.cabal | 2 +- .../cardano-wallet-conversions.cabal | 86 +++++++++++++++++++ .../Wallet/Shelley/Compatibility/Ledger.hs | 0 .../Shelley/Compatibility/LedgerSpec.hs | 37 +++++++- lib/conversions/test/spec/Spec.hs | 1 + lib/conversions/test/spec/SpecHook.hs | 6 ++ lib/conversions/test/spec/run-test-suite.hs | 13 +++ lib/wallet/cardano-wallet.cabal | 4 +- 9 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 lib/conversions/cardano-wallet-conversions.cabal rename lib/{balance-tx => conversions}/lib/Cardano/Wallet/Shelley/Compatibility/Ledger.hs (100%) rename lib/{wallet/test/unit => conversions/test/spec}/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs (80%) create mode 100644 lib/conversions/test/spec/Spec.hs create mode 100644 lib/conversions/test/spec/SpecHook.hs create mode 100644 lib/conversions/test/spec/run-test-suite.hs diff --git a/cabal.project b/cabal.project index 3d32bdcc86f..646b36720c9 100644 --- a/cabal.project +++ b/cabal.project @@ -63,6 +63,7 @@ packages: , lib/cardano-api-extra/ , lib/crypto-hash-extra/ , lib/coin-selection/ + , lib/conversions/ , lib/delta-store/ , lib/delta-table , lib/delta-types/ diff --git a/lib/balance-tx/cardano-balance-tx.cabal b/lib/balance-tx/cardano-balance-tx.cabal index 640e237760a..ed086faf61e 100644 --- a/lib/balance-tx/cardano-balance-tx.cabal +++ b/lib/balance-tx/cardano-balance-tx.cabal @@ -55,6 +55,7 @@ library , cardano-ledger-shelley , cardano-slotting , cardano-strict-containers + , cardano-wallet-conversions , cardano-wallet-primitive , cardano-wallet-test-utils , cborg @@ -77,7 +78,6 @@ library , text , transformers exposed-modules: - Cardano.Wallet.Shelley.Compatibility.Ledger Cardano.Write.ProtocolParameters Cardano.Write.Tx Cardano.Write.Tx.Balance diff --git a/lib/conversions/cardano-wallet-conversions.cabal b/lib/conversions/cardano-wallet-conversions.cabal new file mode 100644 index 00000000000..af07663873c --- /dev/null +++ b/lib/conversions/cardano-wallet-conversions.cabal @@ -0,0 +1,86 @@ +cabal-version: 3.0 +name: cardano-wallet-conversions +version: 2023.7.18 +synopsis: Miscellaneous conversion functions. +description: Please see README.md. +homepage: https://github.com/cardano-foundation/cardano-wallet +author: Cardano Foundation (High Assurance Lab) +maintainer: hal@cardanofoundation.org +copyright: 2018-2022 IOHK, 2023 Cardano Foundation +license: Apache-2.0 +category: Blockchain, Cardano +build-type: Simple + +common language + default-language: Haskell2010 + default-extensions: + NoImplicitPrelude + OverloadedStrings + +common opts-lib + ghc-options: -Wall -Wcompat -fwarn-redundant-constraints + + if flag(release) + ghc-options: -O2 -Werror + +common opts-exe + ghc-options: -threaded -rtsopts -Wall + + if flag(release) + ghc-options: -O2 -Werror + +flag release + description: Enable optimization and `-Werror` + default: False + manual: True + +library + import: language, opts-lib + hs-source-dirs: lib + build-depends: + , base + , bytestring + , cardano-addresses + , cardano-crypto-class + , cardano-ledger-allegra + , cardano-ledger-alonzo + , cardano-ledger-babbage + , cardano-ledger-core + , cardano-ledger-mary + , cardano-ledger-shelley + , cardano-ledger-shelley-ma + , cardano-slotting + , cardano-strict-containers + , cardano-wallet-primitive + , containers + , fmt + , generic-lens + , int-cast + , ouroboros-consensus-cardano + exposed-modules: + Cardano.Wallet.Shelley.Compatibility.Ledger + +test-suite test + import: language, opts-exe + ghc-options: -with-rtsopts=-M2G -with-rtsopts=-N4 + type: exitcode-stdio-1.0 + hs-source-dirs: test/spec + main-is: run-test-suite.hs + build-depends: + , base + , bytestring + , cardano-addresses + , cardano-ledger-allegra:{cardano-ledger-allegra, testlib} + , cardano-wallet-conversions + , cardano-wallet-launcher + , cardano-wallet-primitive + , cardano-wallet-test-utils + , hspec + , hspec-core + , ouroboros-consensus-cardano + , QuickCheck + build-tool-depends: hspec-discover:hspec-discover + other-modules: + Cardano.Wallet.Shelley.Compatibility.LedgerSpec + Spec + SpecHook diff --git a/lib/balance-tx/lib/Cardano/Wallet/Shelley/Compatibility/Ledger.hs b/lib/conversions/lib/Cardano/Wallet/Shelley/Compatibility/Ledger.hs similarity index 100% rename from lib/balance-tx/lib/Cardano/Wallet/Shelley/Compatibility/Ledger.hs rename to lib/conversions/lib/Cardano/Wallet/Shelley/Compatibility/Ledger.hs diff --git a/lib/wallet/test/unit/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs b/lib/conversions/test/spec/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs similarity index 80% rename from lib/wallet/test/unit/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs rename to lib/conversions/test/spec/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs index ae1b884be0c..e66053e55fe 100644 --- a/lib/wallet/test/unit/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs +++ b/lib/conversions/test/spec/Cardano/Wallet/Shelley/Compatibility/LedgerSpec.hs @@ -10,9 +10,7 @@ module Cardano.Wallet.Shelley.Compatibility.LedgerSpec import Prelude import Cardano.Address.Script - ( KeyHash (..), KeyRole (..), Script ) -import Cardano.Wallet.Gen - ( genScript ) + ( KeyHash (..), KeyRole (..), Script (..) ) import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) import Cardano.Wallet.Primitive.Types.TokenBundle @@ -48,7 +46,19 @@ import Test.Hspec import Test.Hspec.Core.QuickCheck ( modifyMaxSuccess ) import Test.QuickCheck - ( Arbitrary (..), elements, property, vectorOf, (===) ) + ( Arbitrary (..) + , Gen + , Positive (Positive) + , arbitrarySizedNatural + , choose + , elements + , oneof + , property + , scale + , sized + , vectorOf + , (===) + ) import qualified Data.ByteString as BS @@ -128,6 +138,25 @@ instance Arbitrary (Script KeyHash) where arbitrary = do keyHashes <- vectorOf 10 arbitrary genScript keyHashes + where + genScript :: [a] -> Gen (Script a) + genScript elems = scale (`div` 3) $ sized scriptTree + where + scriptTree 0 = oneof + [ RequireSignatureOf <$> elements elems + , ActiveFromSlot <$> arbitrarySizedNatural + , ActiveUntilSlot <$> arbitrarySizedNatural + ] + scriptTree n = do + Positive m <- arbitrary + let n' = n `div` (m + 1) + scripts' <- vectorOf m (scriptTree n') + atLeast <- choose (1, fromIntegral m) + elements + [ RequireAllOf scripts' + , RequireAnyOf scripts' + , RequireSomeOf atLeast scripts' + ] instance Arbitrary KeyHash where arbitrary = do diff --git a/lib/conversions/test/spec/Spec.hs b/lib/conversions/test/spec/Spec.hs new file mode 100644 index 00000000000..5416ef6a866 --- /dev/null +++ b/lib/conversions/test/spec/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-} diff --git a/lib/conversions/test/spec/SpecHook.hs b/lib/conversions/test/spec/SpecHook.hs new file mode 100644 index 00000000000..36ff64bdc52 --- /dev/null +++ b/lib/conversions/test/spec/SpecHook.hs @@ -0,0 +1,6 @@ +module SpecHook where + +import Test.Hspec + +hook :: Spec -> Spec +hook = parallel diff --git a/lib/conversions/test/spec/run-test-suite.hs b/lib/conversions/test/spec/run-test-suite.hs new file mode 100644 index 00000000000..126ca339278 --- /dev/null +++ b/lib/conversions/test/spec/run-test-suite.hs @@ -0,0 +1,13 @@ +module Main where + +import Prelude + +import Cardano.Startup + ( withUtf8Encoding ) +import Test.Hspec.Extra + ( hspecMain ) + +import qualified Spec + +main :: IO () +main = withUtf8Encoding $ hspecMain Spec.spec diff --git a/lib/wallet/cardano-wallet.cabal b/lib/wallet/cardano-wallet.cabal index 268474c6a38..bfddd5c0877 100644 --- a/lib/wallet/cardano-wallet.cabal +++ b/lib/wallet/cardano-wallet.cabal @@ -95,6 +95,7 @@ library , cardano-slotting , cardano-strict-containers , cardano-wallet-application-extras + , cardano-wallet-conversions , cardano-wallet-launcher , cardano-wallet-primitive , cardano-wallet-read @@ -397,6 +398,7 @@ library cardano-wallet-api-http , cardano-ledger-shelley , cardano-wallet , cardano-wallet-application-extras + , cardano-wallet-conversions , cardano-wallet-launcher , cardano-wallet-primitive , cardano-wallet-read @@ -729,6 +731,7 @@ test-suite unit , cardano-wallet , cardano-wallet-api-http , cardano-wallet-application-extras + , cardano-wallet-conversions , cardano-wallet-launcher , cardano-wallet-primitive , cardano-wallet-read @@ -911,7 +914,6 @@ test-suite unit Cardano.Wallet.Primitive.Types.Tx.TxSeqSpec Cardano.Wallet.Primitive.TypesSpec Cardano.Wallet.RegistrySpec - Cardano.Wallet.Shelley.Compatibility.LedgerSpec Cardano.Wallet.Shelley.CompatibilitySpec Cardano.Wallet.Shelley.LaunchSpec Cardano.Wallet.Shelley.NetworkSpec