Skip to content

Commit

Permalink
Move Write.Tx modules to cardano-balance-tx (#4127)
Browse files Browse the repository at this point in the history
- [x] Factor out `std-gen-seed` library
- [x] Move `Write.Tx` modules to `cardano-balance-tx`

### Comments

- Unit tests to be moved in separate PR and/or task.

### Issue Number

ADP-3170
  • Loading branch information
Anviking authored Sep 15, 2023
2 parents ae6e90b + d0f58df commit 8ae7966
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 56 deletions.
5 changes: 5 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ packages:
, lib/numeric/
, lib/primitive/
, lib/read
, lib/std-gen-seed/
, lib/temporary-extra/
, lib/test-utils/
, lib/text-class/
Expand Down Expand Up @@ -246,6 +247,10 @@ package cardano-wallet-test-utils
tests: True
ghc-options: -fwrite-ide-info

package std-gen-seed
tests: True
ghc-options: -fwrite-ide-info

package wai-middleware-logging
tests: True
ghc-options: -fwrite-ide-info
Expand Down
43 changes: 40 additions & 3 deletions lib/balance-tx/cardano-balance-tx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,59 @@ library
import: language, opts-lib
hs-source-dirs: lib
build-depends:
, MonadRandom
, QuickCheck
, base
, bytestring
, cardano-addresses
, cardano-api
, cardano-coin-selection
, cardano-crypto-class
, cardano-ledger-allegra
, cardano-ledger-alonzo
, cardano-ledger-api
, cardano-ledger-babbage
, cardano-ledger-binary
, cardano-ledger-conway
, cardano-ledger-core
, cardano-ledger-mary
, cardano-ledger-shelley
, cardano-slotting
, cardano-strict-containers
, cardano-wallet-primitive
, cardano-wallet-test-utils
, cborg
, containers
, deepseq
, fmt
, generic-lens
, generics-sop
, MonadRandom
, QuickCheck
, int-cast
, lens
, monoid-subclasses
, ouroboros-consensus
, ouroboros-consensus-cardano
, pretty-simple
, random
, serialise
, std-gen-seed
, text
, transformers
exposed-modules:
Cardano.Tx.Balance
Cardano.Tx.Balance.Internal.CoinSelection
Cardano.Tx.Balance.Internal.CoinSelection.Gen
Cardano.Wallet.Shelley.Compatibility.Ledger
Cardano.Wallet.Write.ProtocolParameters
Cardano.Wallet.Write.Tx
Cardano.Wallet.Write.Tx.Balance
Cardano.Wallet.Write.Tx.Balance.TokenBundleSize
Cardano.Wallet.Write.Tx.Gen
Cardano.Wallet.Write.Tx.Redeemers
Cardano.Wallet.Write.Tx.Sign
Cardano.Wallet.Write.Tx.SizeEstimation
Cardano.Wallet.Write.Tx.TimeTranslation
Cardano.Wallet.Write.UTxOAssumptions

test-suite test
import: language, opts-exe
Expand All @@ -62,6 +99,7 @@ test-suite test
hs-source-dirs: test/data test/spec
main-is: run-test-suite.hs
build-depends:
, QuickCheck
, base
, cardano-balance-tx
, cardano-wallet-launcher
Expand All @@ -70,7 +108,6 @@ test-suite test
, generics-sop
, hspec
, hspec-core
, QuickCheck
build-tool-depends: hspec-discover:hspec-discover
other-modules:
Cardano.Tx.Balance.Internal.CoinSelectionSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import Cardano.Address.Script
( KeyHash (..), KeyRole (..), Script (..) )
import Cardano.Crypto.Hash
( hashFromBytes, hashToBytes )
import Cardano.Slotting.Slot
( SlotNo (..) )
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Coin
Expand Down Expand Up @@ -116,7 +118,6 @@ import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Data.Map.Strict as Map
import qualified Data.Sequence.Strict as StrictSeq
import qualified Ouroboros.Network.Block as O

--------------------------------------------------------------------------------
-- Roundtrip conversion between wallet types and ledger specification types
Expand Down Expand Up @@ -378,9 +379,9 @@ toWalletScript tokeyrole = fromLedgerScript
RequireAnyOf $ map fromLedgerScript $ toList contents
fromLedgerScript (Scripts.RequireMOf num contents) =
RequireSomeOf (fromIntegral num) $ fromLedgerScript <$> toList contents
fromLedgerScript (Scripts.RequireTimeExpire (O.SlotNo slot)) =
fromLedgerScript (Scripts.RequireTimeExpire (SlotNo slot)) =
ActiveUntilSlot $ fromIntegral slot
fromLedgerScript (Scripts.RequireTimeStart (O.SlotNo slot)) =
fromLedgerScript (Scripts.RequireTimeStart (SlotNo slot)) =
ActiveFromSlot $ fromIntegral slot

toWalletScriptFromShelley
Expand Down Expand Up @@ -427,8 +428,8 @@ toLedgerTimelockScript s = case s of
Scripts.RequireTimeStart
(convertSlotNo slot)
where
convertSlotNo :: Natural -> O.SlotNo
convertSlotNo x = O.SlotNo $ fromMaybe err $ intCastMaybe x
convertSlotNo :: Natural -> SlotNo
convertSlotNo x = SlotNo $ fromMaybe err $ intCastMaybe x
where
err = error $ unwords
[ "toLedgerTimelockScript:"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ import Cardano.Wallet.Primitive.Types.Tx
( SealedTx, sealedTxFromCardano )
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( TxSize (..), txOutMaxCoin, txOutMaxTokenQuantity )
import Cardano.Wallet.Read.Primitive.Tx.Features.Outputs
( fromCardanoValue )
import Cardano.Wallet.Write.ProtocolParameters
( ProtocolParameters (..) )
import Cardano.Wallet.Write.Tx
Expand Down Expand Up @@ -154,11 +152,11 @@ import Cardano.Wallet.Write.Tx.Redeemers
import Cardano.Wallet.Write.Tx.Sign
( estimateKeyWitnessCount, estimateSignedTxSize )
import Cardano.Wallet.Write.Tx.SizeEstimation
( TxSkeleton (..), estimateTxCost )
( TxSkeleton (..), assumedTxWitnessTag, estimateTxCost )
import Cardano.Wallet.Write.Tx.TimeTranslation
( TimeTranslation )
import Cardano.Wallet.Write.UTxOAssumptions
( UTxOAssumptions (..), assumedInputScriptTemplate, assumedTxWitnessTag )
( UTxOAssumptions (..), assumedInputScriptTemplate )
import Control.Arrow
( left )
import Control.Monad
Expand Down Expand Up @@ -1604,3 +1602,6 @@ validateTxOutputAdaQuantity constraints output
(constraints ^. #computeMinimumAdaQuantity)
(fst output)
(snd output ^. #tokens)

fromCardanoValue :: Cardano.Value -> W.TokenBundle
fromCardanoValue = W.toWalletTokenBundle . Cardano.toMaryValue
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ module Cardano.Wallet.Write.Tx.SizeEstimation
, estimateTxCost
, TxSkeleton (..)

-- ** Needed for balance migration
-- ** TxWitnessTag
, TxWitnessTag (..)
, assumedTxWitnessTag

-- * Needed for balance migration
, txConstraints

-- ** Needed for estimateSignedTxSize
-- * Needed for estimateSignedTxSize
, sizeOf_BootstrapWitnesses

-- ** For the wallet
-- * For the wallet
, _txRewardWithdrawalCost
)

Expand Down Expand Up @@ -65,8 +69,6 @@ import Cardano.Wallet.Primitive.Types.Tx.Constraints
( TxConstraints (..), TxSize (..), txOutMaxCoin )
import Cardano.Wallet.Shelley.Compatibility.Ledger
( Convert (..) )
import Cardano.Wallet.TxWitnessTag
( TxWitnessTag (..) )
import Cardano.Wallet.Write.ProtocolParameters
( ProtocolParameters (..) )
import Cardano.Wallet.Write.Tx
Expand All @@ -82,6 +84,8 @@ import Cardano.Wallet.Write.Tx
)
import Cardano.Wallet.Write.Tx.Sign
( estimateMaxWitnessRequiredPerInput )
import Cardano.Wallet.Write.UTxOAssumptions
( UTxOAssumptions (..) )
import Control.Lens
( (^.) )
import Data.Generics.Internal.VL.Lens
Expand Down Expand Up @@ -697,3 +701,14 @@ mkLedgerTxOut txOutEra address bundle =
RecentEraConway -> W.toConwayTxOut txOut
where
txOut = W.TxOut address bundle

data TxWitnessTag
= TxWitnessByronUTxO
| TxWitnessShelleyUTxO
deriving (Show, Eq)

assumedTxWitnessTag :: UTxOAssumptions -> TxWitnessTag
assumedTxWitnessTag = \case
AllKeyPaymentCredentials -> TxWitnessShelleyUTxO
AllByronKeyPaymentCredentials -> TxWitnessByronUTxO
AllScriptPaymentCredentialsFrom {} -> TxWitnessShelleyUTxO
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module Cardano.Wallet.Write.UTxOAssumptions
-- * UTxOAssumptions
UTxOAssumptions (..)
, assumedInputScriptTemplate
, assumedTxWitnessTag

-- * Validation
, validateAddress
Expand All @@ -21,8 +20,6 @@ import Prelude

import Cardano.Ledger.Shelley.API
( Addr (..), Credential (..) )
import Cardano.Wallet.TxWitnessTag
( TxWitnessTag (..) )
import Cardano.Wallet.Write.Tx
( Address )

Expand Down Expand Up @@ -50,12 +47,6 @@ assumedInputScriptTemplate = \case
AllByronKeyPaymentCredentials -> Nothing
AllScriptPaymentCredentialsFrom scriptTemplate _ -> Just scriptTemplate

assumedTxWitnessTag :: UTxOAssumptions -> TxWitnessTag
assumedTxWitnessTag = \case
AllKeyPaymentCredentials -> TxWitnessShelleyUTxO
AllByronKeyPaymentCredentials -> TxWitnessByronUTxO
AllScriptPaymentCredentialsFrom {} -> TxWitnessShelleyUTxO

validateAddress :: UTxOAssumptions -> Address -> Bool
validateAddress = valid
where
Expand Down
File renamed without changes.
66 changes: 66 additions & 0 deletions lib/std-gen-seed/std-gen-seed.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
cabal-version: 3.0
name: std-gen-seed
version: 0.0.0.0
synopsis: Support for standard random number generator seeds
license: Apache-2.0
author: HAL, Cardano Foundation
maintainer: [email protected]
build-type: Simple
extra-doc-files: CHANGELOG.md

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
exposed-modules:
System.Random.StdGenSeed
hs-source-dirs: src
build-depends:
, base
, random
, MonadRandom
, OddWord
, quiet
, splitmix

test-suite unit
import: language, opts-exe
ghc-options: -with-rtsopts=-M2G -with-rtsopts=-N4
build-depends:
base
, hspec
, QuickCheck
, std-gen-seed
, random
build-tool-depends:
hspec-discover:hspec-discover
type:
exitcode-stdio-1.0
hs-source-dirs:
test/unit
main-is:
std-gen-seed-unit-test.hs
other-modules:
System.Random.StdGenSeedSpec

1 change: 1 addition & 0 deletions lib/std-gen-seed/test/unit/std-gen-seed-unit-test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
16 changes: 2 additions & 14 deletions lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ library
Cardano.Wallet.Registry
Cardano.Wallet.Shelley.BlockchainSource
Cardano.Wallet.Shelley.Compatibility
Cardano.Wallet.Shelley.Compatibility.Ledger
Cardano.Wallet.Shelley.Network
Cardano.Wallet.Shelley.Network.Node
Cardano.Wallet.Shelley.Transaction
Expand All @@ -350,20 +349,9 @@ library
Cardano.Wallet.TokenMetadata
Cardano.Wallet.Transaction
Cardano.Wallet.Transaction.Built
Cardano.Wallet.TxWitnessTag
Cardano.Wallet.TypeLevel
Cardano.Wallet.Version
Cardano.Wallet.Version.TH
Cardano.Wallet.Write.ProtocolParameters
Cardano.Wallet.Write.Tx
Cardano.Wallet.Write.Tx.Balance
Cardano.Wallet.Write.Tx.Balance.TokenBundleSize
Cardano.Wallet.Write.Tx.Gen
Cardano.Wallet.Write.Tx.Redeemers
Cardano.Wallet.Write.Tx.Sign
Cardano.Wallet.Write.Tx.SizeEstimation
Cardano.Wallet.Write.Tx.TimeTranslation
Cardano.Wallet.Write.UTxOAssumptions
Control.Concurrent.Concierge
Control.Monad.Exception.Unchecked
Control.Monad.Fail.Extended
Expand All @@ -376,7 +364,6 @@ library
Database.Persist.PersistValue.Extended
Network.Ntp
Ouroboros.Network.Client.Wallet
System.Random.StdGenSeed
UnliftIO.Compat

other-modules: Paths_cardano_wallet
Expand Down Expand Up @@ -824,6 +811,7 @@ test-suite unit
, si-timers
, sop-core
, splitmix
, std-gen-seed
, string-interpolate
, string-qq
, temporary
Expand Down Expand Up @@ -942,7 +930,6 @@ test-suite unit
Data.Vector.ShuffleSpec
Spec
SpecHook
System.Random.StdGenSeedSpec

test-suite integration
import: language, opts-exe
Expand Down Expand Up @@ -1061,6 +1048,7 @@ benchmark db
, bytestring
, cardano-addresses
, cardano-api
, cardano-balance-tx
, cardano-crypto
, cardano-wallet
, cardano-wallet-application-extras
Expand Down
4 changes: 1 addition & 3 deletions lib/wallet/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,6 @@ import Cardano.Wallet.Transaction
)
import Cardano.Wallet.Transaction.Built
( BuiltTx (..) )
import Cardano.Wallet.TxWitnessTag
( TxWitnessTag (..) )
import Cardano.Wallet.Write.Tx
( recentEra )
import Cardano.Wallet.Write.Tx.Balance
Expand All @@ -523,7 +521,7 @@ import Cardano.Wallet.Write.Tx.Balance
, constructUTxOIndex
)
import Cardano.Wallet.Write.Tx.SizeEstimation
( _txRewardWithdrawalCost )
( TxWitnessTag (..), _txRewardWithdrawalCost )
import Cardano.Wallet.Write.Tx.TimeTranslation
( TimeTranslation )
import Control.Arrow
Expand Down
Loading

0 comments on commit 8ae7966

Please sign in to comment.