Skip to content

Commit

Permalink
Move Compatibility.{Ledger,LedgerSpec} to conversions package. (#…
Browse files Browse the repository at this point in the history
…4153)

## Issue

ADP-3185

## Description

This PR moves the `Compatibility.{Ledger,LedgerSpec}` modules to a new
package called `cardano-wallet-conversions`.

Before this PR:
- The `Compatibility.Ledger` module was located in `cardano-balance-tx`.
- The `Compatibility.LedgerSpec` module was located in `cardano-wallet`.

This PR 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 PR also clones the
definition of a single QuickCheck generator
(`Cardano.Wallet.Gen.genScript`).

## Potential future work

- Consider moving the `Cardano.Wallet.Shelley.Compatibility` module (or
a subset of its functions) to this package.
- Adjust the module hierarchy / namespace to something more suitable.
(For the moment, we avoid changing module names in order to avoid
conflicts with other in-flight PRs.)
  • Loading branch information
jonathanknowles authored Oct 12, 2023
2 parents 735efa0 + 07b7ad9 commit 630c270
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 6 deletions.
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
2 changes: 1 addition & 1 deletion lib/balance-tx/cardano-balance-tx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ library
, cardano-ledger-shelley
, cardano-slotting
, cardano-strict-containers
, cardano-wallet-conversions
, cardano-wallet-primitive
, cardano-wallet-test-utils
, cborg
Expand All @@ -77,7 +78,6 @@ library
, text
, transformers
exposed-modules:
Cardano.Wallet.Shelley.Compatibility.Ledger
Cardano.Write.ProtocolParameters
Cardano.Write.Tx
Cardano.Write.Tx.Balance
Expand Down
86 changes: 86 additions & 0 deletions lib/conversions/cardano-wallet-conversions.cabal
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/conversions/test/spec/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-}
6 changes: 6 additions & 0 deletions lib/conversions/test/spec/SpecHook.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module SpecHook where

import Test.Hspec

hook :: Spec -> Spec
hook = parallel
13 changes: 13 additions & 0 deletions lib/conversions/test/spec/run-test-suite.hs
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 630c270

Please sign in to comment.