Skip to content

Commit 3fe73a2

Browse files
authored
Merge pull request #4879 from IntersectMBO/lehins/improve-mkAddr
Improve `mkAddr` and `mkCred` interface
2 parents 8b2df7d + 4df915b commit 3fe73a2

File tree

34 files changed

+211
-250
lines changed

34 files changed

+211
-250
lines changed

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Imp/UtxowSpec/Invalid.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
module Test.Cardano.Ledger.Alonzo.Imp.UtxowSpec.Invalid (spec) where
1111

12-
import Cardano.Ledger.Address (Addr (..))
1312
import Cardano.Ledger.Allegra.Scripts (AllegraEraScript (..))
1413
import Cardano.Ledger.Alonzo (AlonzoEra)
1514
import Cardano.Ledger.Alonzo.Core
@@ -20,7 +19,7 @@ import Cardano.Ledger.Alonzo.Rules (
2019
)
2120
import Cardano.Ledger.Alonzo.Scripts (eraLanguages)
2221
import Cardano.Ledger.Alonzo.TxWits (Redeemers (..), TxDats (..), unRedeemers)
23-
import Cardano.Ledger.BaseTypes (Mismatch (..), Network (..), StrictMaybe (..), natVersion)
22+
import Cardano.Ledger.BaseTypes (Mismatch (..), StrictMaybe (..), natVersion)
2423
import Cardano.Ledger.Coin (Coin (..))
2524
import Cardano.Ledger.Credential (Credential (..), StakeReference (..))
2625
import Cardano.Ledger.Keys (asWitness, witVKeyHash)
@@ -139,10 +138,9 @@ spec = describe "Invalid transactions" $ do
139138
testHashMismatch SNothing
140139

141140
it "UnspendableUTxONoDatumHash" $ do
142-
let scriptHash = redeemerSameAsDatumHash
143-
144141
txIn <- impAnn "Produce script at a txout with a missing datahash" $ do
145-
let addr = Addr Testnet (ScriptHashObj scriptHash) StakeRefNull
142+
let scriptHash = redeemerSameAsDatumHash
143+
let addr = mkAddr scriptHash StakeRefNull
146144
let tx =
147145
mkBasicTx mkBasicTxBody
148146
& bodyTxL . outputsTxBodyL .~ [mkBasicTxOut addr mempty]

eras/alonzo/test-suite/src/Test/Cardano/Ledger/Alonzo/Examples/Consensus.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ exampleTxBodyAlonzo =
9999
(Set.fromList [mkTxInPartial (TxId (mkDummySafeHash 2)) 1]) -- collateral
100100
( StrictSeq.fromList
101101
[ AlonzoTxOut
102-
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
102+
(mkAddr SLE.examplePayKey SLE.exampleStakeKey)
103103
(SLE.exampleMultiAssetValue 2)
104104
(SJust $ mkDummySafeHash 1) -- outputs
105105
]

eras/babbage/test-suite/src/Test/Cardano/Ledger/Babbage/Examples/Consensus.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ ledgerExamplesBabbage =
9494
collateralOutput :: BabbageTxOut BabbageEra
9595
collateralOutput =
9696
BabbageTxOut
97-
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
97+
(mkAddr SLE.examplePayKey SLE.exampleStakeKey)
9898
(MaryValue (Coin 8675309) mempty)
9999
NoDatum
100100
SNothing
@@ -108,7 +108,7 @@ exampleTxBodyBabbage =
108108
( StrictSeq.fromList
109109
[ mkSized (eraProtVerHigh @BabbageEra) $
110110
BabbageTxOut
111-
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
111+
(mkAddr SLE.examplePayKey SLE.exampleStakeKey)
112112
(MarySLE.exampleMultiAssetValue 2)
113113
(Datum $ dataToBinaryData datumExample) -- inline datum
114114
(SJust $ alwaysSucceeds @'PlutusV2 3) -- reference script

eras/conway/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### `testlib`
1212

13+
* Add `sendCoinTo_` and `sendValueTo_`
1314
* Add `genRegTxCert` and `genUnRegTxCert`. #4830
1415
* Add `Arbitrary` instance for `ConwayBbodyPredFailure` and `ConwayMempoolPredFailure`
1516

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Binary/Regression.hs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,7 @@ import Cardano.Ledger.Binary (
2121
)
2222
import Cardano.Ledger.Coin (Coin (..))
2323
import Cardano.Ledger.Conway (ConwayEra)
24-
import Cardano.Ledger.Conway.Core (
25-
BabbageEraTxBody (..),
26-
EraTx (..),
27-
EraTxBody (..),
28-
EraTxOut (..),
29-
EraTxWits (..),
30-
coinTxOutL,
31-
eraProtVerLow,
32-
txIdTx,
33-
)
24+
import Cardano.Ledger.Conway.Core
3425
import Cardano.Ledger.Conway.Rules (
3526
ConwayLedgerPredFailure (..),
3627
ConwayUtxoPredFailure (..),
@@ -43,7 +34,6 @@ import qualified Data.Sequence.Strict as SSeq
4334
import qualified Data.Set as Set
4435
import Lens.Micro ((%~), (&), (.~))
4536
import Test.Cardano.Ledger.Conway.ImpTest
46-
import Test.Cardano.Ledger.Core.KeyPair (mkScriptAddr)
4737
import Test.Cardano.Ledger.Imp.Common
4838
import Test.Cardano.Ledger.Plutus.Examples (redeemerSameAsDatum)
4939

@@ -95,11 +85,11 @@ spec = describe "Regression" $ do
9585
withImpInit @(LedgerSpec ConwayEra) $
9686
it "InsufficientCollateral is not encoded with negative coin #4198" $ do
9787
collateralAddress <- freshKeyAddr_
98-
(_, skp) <- freshKeyPair
88+
stakingKeyHash <- freshKeyHash @'Staking
9989
let
10090
plutusVersion = SPlutusV2
10191
scriptHash = hashPlutusScript $ redeemerSameAsDatum plutusVersion
102-
lockScriptAddress = mkScriptAddr scriptHash skp
92+
lockScriptAddress = mkAddr scriptHash stakingKeyHash
10393
collateralReturnAddr <- freshKeyAddr_
10494
lockedTx <-
10595
submitTxAnn @ConwayEra "Script locked tx" $
@@ -110,7 +100,7 @@ spec = describe "Regression" $ do
110100
, mkBasicTxOut collateralAddress mempty
111101
]
112102
& bodyTxL . collateralReturnTxBodyL
113-
.~ SJust (mkBasicTxOut collateralReturnAddr . inject $ Coin 849070)
103+
.~ SJust (mkBasicTxOut collateralReturnAddr . inject $ Coin 862000)
114104
let
115105
modifyRootCoin = coinTxOutL .~ Coin 989482376
116106
modifyRootTxOut (x SSeq.:<| SSeq.Empty) =

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/RatifySpec.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
module Test.Cardano.Ledger.Conway.Imp.RatifySpec (spec) where
99

10-
import Cardano.Ledger.Address
1110
import Cardano.Ledger.BaseTypes
1211
import Cardano.Ledger.Coin
1312
import Cardano.Ledger.Conway.Core
@@ -628,7 +627,7 @@ votingSpec =
628627
-- Bump up the UTxO delegated
629628
-- to barely make the threshold (65 %! 100)
630629
stakingKP1 <- lookupKeyPair stakingKH1
631-
_ <- sendCoinTo (mkAddr (paymentKP1, stakingKP1)) (inject $ Coin 858_000_000)
630+
sendCoinTo_ (mkAddr paymentKP1 stakingKP1) (inject $ Coin 858_000_000)
632631
passNEpochs 2
633632
-- The same vote should now successfully ratify the proposal
634633
getLastEnactedCommittee `shouldReturn` SJust (GovPurposeId addCCGaid)
@@ -929,10 +928,7 @@ votingSpec =
929928
getLastEnactedCommittee `shouldReturn` SNothing
930929
-- Bump up the UTxO delegated
931930
-- to barely make the threshold (51 %! 100)
932-
_ <-
933-
sendCoinTo
934-
(Addr Testnet delegatorCPayment1 (StakeRefBase delegatorCStaking1))
935-
(Coin 40_900_000)
931+
sendCoinTo_ (mkAddr delegatorCPayment1 delegatorCStaking1) (Coin 40_900_000)
936932
passNEpochs 2
937933
-- The same vote should now successfully ratify the proposal
938934
getLastEnactedCommittee `shouldReturn` SJust (GovPurposeId addCCGaid)

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/UtxoSpec.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import qualified Data.Sequence.Strict as SSeq
3535
import qualified Data.Set as Set
3636
import Lens.Micro ((&), (.~), (^.))
3737
import Test.Cardano.Ledger.Conway.ImpTest
38-
import Test.Cardano.Ledger.Core.KeyPair (mkScriptAddr)
3938
import Test.Cardano.Ledger.Core.Rational ((%!))
4039
import Test.Cardano.Ledger.Core.Utils (txInAt)
4140
import Test.Cardano.Ledger.Imp.Common
@@ -149,11 +148,11 @@ spec =
149148
_ <- impAddNativeScript script
150149
pure script
151150

152-
addScriptAddr :: HasCallStack => NativeScript era -> ImpTestM era Addr
151+
addScriptAddr :: NativeScript era -> ImpTestM era Addr
153152
addScriptAddr script = do
154-
kpStaking1 <- lookupKeyPair =<< freshKeyHash
155153
scriptHash <- impAddNativeScript script
156-
pure $ mkScriptAddr scriptHash kpStaking1
154+
stakingKeyHash <- freshKeyHash @'Staking
155+
pure $ mkAddr scriptHash stakingKeyHash
157156

158157
scriptSize :: Script era -> Int
159158
scriptSize = \case

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/UtxosSpec.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import qualified Data.Set as Set
4040
import Lens.Micro
4141
import qualified PlutusLedgerApi.V1 as P1
4242
import Test.Cardano.Ledger.Conway.ImpTest
43-
import Test.Cardano.Ledger.Core.KeyPair (mkAddr)
4443
import Test.Cardano.Ledger.Imp.Common
4544
import Test.Cardano.Ledger.Plutus (testingCostModels)
4645
import Test.Cardano.Ledger.Plutus.Examples (
@@ -629,7 +628,7 @@ scriptLockedTxOut ::
629628
TxOut era
630629
scriptLockedTxOut shSpending =
631630
mkBasicTxOut
632-
(Addr Testnet (ScriptHashObj shSpending) StakeRefNull)
631+
(mkAddr shSpending StakeRefNull)
633632
mempty
634633
& dataHashTxOutL .~ SJust (hashData @era $ Data spendDatum)
635634

@@ -640,11 +639,10 @@ mkRefTxOut ::
640639
ScriptHash ->
641640
ImpTestM era (TxOut era)
642641
mkRefTxOut sh = do
643-
kpPayment <- lookupKeyPair =<< freshKeyHash
644-
kpStaking <- lookupKeyPair =<< freshKeyHash
642+
addr <- freshKeyAddr_
645643
let mbyPlutusScript = impLookupPlutusScriptMaybe sh
646644
pure $
647-
mkBasicTxOut (mkAddr (kpPayment, kpStaking)) mempty
645+
mkBasicTxOut addr mempty
648646
& referenceScriptTxOutL .~ maybeToStrictMaybe (fromPlutusScript <$> mbyPlutusScript)
649647

650648
setupRefTx ::

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/ImpTest.hs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,12 @@ module Test.Cardano.Ledger.Conway.ImpTest (
136136
delegateSPORewardAddressToDRep_,
137137
) where
138138

139-
import Cardano.Ledger.Address (Addr (..), RewardAccount (..))
139+
import Cardano.Ledger.Address (RewardAccount (..))
140140
import Cardano.Ledger.Allegra.Scripts (Timelock)
141141
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript)
142142
import Cardano.Ledger.BaseTypes (
143143
EpochInterval (..),
144144
EpochNo (..),
145-
Network (..),
146145
ProtVer (..),
147146
ShelleyBase,
148147
StrictMaybe (..),
@@ -185,7 +184,7 @@ import Cardano.Ledger.Conway.Rules (
185184
)
186185
import Cardano.Ledger.Conway.Tx (AlonzoTx)
187186
import Cardano.Ledger.Conway.TxCert (Delegatee (..))
188-
import Cardano.Ledger.Credential (Credential (..), StakeReference (..))
187+
import Cardano.Ledger.Credential (Credential (..))
189188
import Cardano.Ledger.DRep
190189
import Cardano.Ledger.Plutus.Language (Language (..), SLanguage (..), hashPlutusScript)
191190
import Cardano.Ledger.PoolParams (PoolParams (..), ppRewardAccount)
@@ -241,7 +240,6 @@ import Prettyprinter (align, hsep, viaShow, vsep)
241240
import Test.Cardano.Ledger.Babbage.ImpTest
242241
import Test.Cardano.Ledger.Conway.Arbitrary ()
243242
import Test.Cardano.Ledger.Conway.TreeDiff (tableDoc)
244-
import Test.Cardano.Ledger.Core.KeyPair (KeyPair (..), mkCred)
245243
import Test.Cardano.Ledger.Core.Rational (IsRatio (..))
246244
import Test.Cardano.Ledger.Imp.Common
247245
import Test.Cardano.Ledger.Plutus (testingCostModel)
@@ -472,11 +470,7 @@ setupSingleDRep stake = do
472470
let tx =
473471
mkBasicTx mkBasicTxBody
474472
& bodyTxL . certsTxBodyL
475-
.~ SSeq.fromList
476-
[ RegDepositTxCert
477-
(KeyHashObj delegatorKH)
478-
deposit
479-
]
473+
.~ SSeq.fromList [RegDepositTxCert (KeyHashObj delegatorKH) deposit]
480474
submitTx_ tx
481475
spendingKP <-
482476
delegateToDRep (KeyHashObj delegatorKH) (Coin stake) (DRepCredential (KeyHashObj drepKH))
@@ -490,21 +484,13 @@ delegateToDRep ::
490484
ImpTestM era (KeyPair 'Payment)
491485
delegateToDRep cred stake dRep = do
492486
(_, spendingKP) <- freshKeyPair
493-
let addr = Addr Testnet (mkCred spendingKP) (StakeRefBase cred)
487+
494488
submitTxAnn_ "Delegate to DRep" $
495489
mkBasicTx mkBasicTxBody
496490
& bodyTxL . outputsTxBodyL
497-
.~ SSeq.singleton
498-
( mkBasicTxOut
499-
addr
500-
(inject stake)
501-
)
491+
.~ SSeq.singleton (mkBasicTxOut (mkAddr spendingKP cred) (inject stake))
502492
& bodyTxL . certsTxBodyL
503-
.~ SSeq.fromList
504-
[ DelegTxCert
505-
cred
506-
(DelegVote dRep)
507-
]
493+
.~ SSeq.fromList [DelegTxCert cred (DelegVote dRep)]
508494
pure spendingKP
509495

510496
lookupDRepState ::
@@ -534,10 +520,7 @@ setupPoolWithStake delegCoin = do
534520
registerPool khPool
535521
credDelegatorPayment <- KeyHashObj <$> freshKeyHash
536522
credDelegatorStaking <- KeyHashObj <$> freshKeyHash
537-
void $
538-
sendCoinTo
539-
(Addr Testnet credDelegatorPayment (StakeRefBase credDelegatorStaking))
540-
delegCoin
523+
sendCoinTo_ (mkAddr credDelegatorPayment credDelegatorStaking) delegCoin
541524
pp <- getsNES $ nesEsL . curPParamsEpochStateL
542525
submitTxAnn_ "Delegate to stake pool" $
543526
mkBasicTx mkBasicTxBody

eras/conway/test-suite/src/Test/Cardano/Ledger/Conway/Examples/Consensus.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ ledgerExamplesConway =
110110
collateralOutput :: BabbageTxOut ConwayEra
111111
collateralOutput =
112112
BabbageTxOut
113-
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
113+
(mkAddr SLE.examplePayKey SLE.exampleStakeKey)
114114
(MaryValue (Coin 8675309) mempty)
115115
NoDatum
116116
SNothing
@@ -130,7 +130,7 @@ exampleTxBodyConway =
130130
( StrictSeq.fromList
131131
[ mkSized (eraProtVerHigh @ConwayEra) $
132132
BabbageTxOut
133-
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
133+
(mkAddr SLE.examplePayKey SLE.exampleStakeKey)
134134
(MarySLE.exampleMultiAssetValue 2)
135135
(Datum $ dataToBinaryData datumExample) -- inline datum
136136
(SJust $ alwaysSucceeds @'PlutusV2 3) -- reference script

eras/shelley-ma/test-suite/src/Test/Cardano/Ledger/Allegra/Examples/Consensus.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ exampleAllegraTxBody value =
4646
mkBasicTxBody
4747
& inputsTxBodyL .~ exampleTxIns
4848
& outputsTxBodyL
49-
.~ StrictSeq.singleton (mkBasicTxOut (mkAddr (examplePayKey, exampleStakeKey)) value)
49+
.~ StrictSeq.singleton (mkBasicTxOut (mkAddr examplePayKey exampleStakeKey) value)
5050
& certsTxBodyL .~ exampleCerts
5151
& withdrawalsTxBodyL .~ exampleWithdrawals
5252
& feeTxBodyL .~ Coin 3

eras/shelley-ma/test-suite/test/Test/Cardano/Ledger/Mary/Examples/Cast.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ aliceStake = KeyPair vk sk
3939

4040
-- | Alice's base address
4141
aliceAddr :: Addr
42-
aliceAddr = mkAddr (alicePay, aliceStake)
42+
aliceAddr = mkAddr alicePay aliceStake
4343

4444
-- | Bob's payment key pair
4545
bobPay :: KeyPair 'Payment
@@ -55,7 +55,7 @@ bobStake = KeyPair vk sk
5555

5656
-- | Bob's address
5757
bobAddr :: Addr
58-
bobAddr = mkAddr (bobPay, bobStake)
58+
bobAddr = mkAddr bobPay bobStake
5959

6060
-- Carl's payment key pair
6161
carlPay :: KeyPair 'Payment
@@ -71,7 +71,7 @@ carlStake = KeyPair vk sk
7171

7272
-- | Carl's address
7373
carlAddr :: Addr
74-
carlAddr = mkAddr (carlPay, carlStake)
74+
carlAddr = mkAddr carlPay carlStake
7575

7676
-- | Daria's payment key pair
7777
dariaPay :: KeyPair 'Payment
@@ -87,4 +87,4 @@ dariaStake = KeyPair vk sk
8787

8888
-- | Daria's address
8989
dariaAddr :: Addr
90-
dariaAddr = mkAddr (dariaPay, dariaStake)
90+
dariaAddr = mkAddr dariaPay dariaStake

eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Imp/LedgerSpec.hs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import qualified Data.Map.Strict as Map
1313
import qualified Data.Sequence.Strict as SSeq
1414
import qualified Data.Set as Set
1515
import Lens.Micro ((&), (.~), (^.))
16-
import Test.Cardano.Ledger.Core.KeyPair (mkAddr)
1716
import Test.Cardano.Ledger.Core.Utils (txInAt)
1817
import Test.Cardano.Ledger.Imp.Common
1918
import Test.Cardano.Ledger.Shelley.ImpTest
@@ -24,21 +23,19 @@ spec ::
2423
SpecWith (ImpInit (LedgerSpec era))
2524
spec = describe "LEDGER" $ do
2625
it "Transactions update UTxO" $ do
27-
kpPayment1 <- lookupKeyPair =<< freshKeyHash
28-
kpStaking1 <- lookupKeyPair =<< freshKeyHash
26+
addr1 <- freshKeyAddr_
2927
let coin1 = Coin 2000000
3028
tx1 <-
3129
submitTxAnn "First transaction" . mkBasicTx $
3230
mkBasicTxBody
3331
& outputsTxBodyL @era
3432
.~ SSeq.singleton
35-
(mkBasicTxOut (mkAddr (kpPayment1, kpStaking1)) $ inject coin1)
33+
(mkBasicTxOut addr1 $ inject coin1)
3634
UTxO utxo1 <- getUTxO
3735
case Map.lookup (txInAt (0 :: Int) tx1) utxo1 of
3836
Just out1 -> out1 ^. coinTxOutL `shouldBe` coin1
3937
Nothing -> expectationFailure "Could not find the TxOut of the first transaction"
40-
kpPayment2 <- lookupKeyPair =<< freshKeyHash
41-
kpStaking2 <- lookupKeyPair =<< freshKeyHash
38+
addr2 <- freshKeyAddr_
4239
let coin2 = Coin 3000000
4340
tx2 <-
4441
submitTxAnn "Second transaction" . mkBasicTx $
@@ -47,8 +44,7 @@ spec = describe "LEDGER" $ do
4744
.~ Set.singleton
4845
(txInAt (0 :: Int) tx1)
4946
& outputsTxBodyL @era
50-
.~ SSeq.singleton
51-
(mkBasicTxOut (mkAddr (kpPayment2, kpStaking2)) $ inject coin2)
47+
.~ SSeq.singleton (mkBasicTxOut addr2 $ inject coin2)
5248
UTxO utxo2 <- getUTxO
5349
case Map.lookup (txInAt (0 :: Int) tx2) utxo2 of
5450
Just out1 -> do

0 commit comments

Comments
 (0)