Skip to content

Commit 318c729

Browse files
UnisayPhilip DiSarro
andauthored
Add ScriptContext Builder testlib and refactor LinearVesting tests (#7643)
* Add ScriptContext Builder testlib and refactor LinearVesting tests Introduce a ScriptContext builder module in plutus-ledger-api testlib that provides composable combinators for constructing ScriptContext values in tests. This replaces hand-crafted script contexts with a declarative builder pattern using lenses. Key changes: - Add PlutusLedgerApi.Test.ScriptContextBuilder.Builder with combinators for minting, spending, certifying, rewarding, and proposing contexts - Add PlutusLedgerApi.Test.ScriptContextBuilder.Lenses with TH-generated lenses for all V3 ledger types - Refactor LinearVesting benchmark tests to use the new builder - Fix AsData destructSum-manual test to use manual IsData instances instead of asData, regenerating golden files Based on work from PR #7562. Co-authored-by: Philip DiSarro <philip-disarro@users.noreply.github.com> * Replace redundant helpers with existing API functions - Replace local isPubKeyAddress/isScriptAddress with toPubKeyHash/toScriptHash from PlutusLedgerApi.V1.Address - Replace negateValue with PlutusTx.negate (Value has AdditiveGroup) - Export and relocate currencySymbolFromHex and singleCurrencySymbol to a dedicated Helpers section at the bottom of Builder.hs Co-authored-by: Philip DiSarro <philip-disarro@users.noreply.github.com> * Regenerate golden files for LinearVesting and CallTrace tests --------- Co-authored-by: Philip DiSarro <philip-disarro@users.noreply.github.com>
1 parent 8337e0b commit 318c729

File tree

10 files changed

+943
-56
lines changed

10 files changed

+943
-56
lines changed

plutus-benchmark/linear-vesting/src/LinearVesting/Test.hs

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,62 @@
99
module LinearVesting.Test where
1010

1111
import PlutusTx
12-
import PlutusTx.Prelude
12+
import PlutusTx.Prelude hiding ((<>))
1313

1414
import LinearVesting.Validator (VestingDatum (..), VestingRedeemer (..), validatorCode)
15-
import PlutusLedgerApi.Data.V3
15+
import PlutusLedgerApi.Data.V3 qualified as PV3D
16+
import PlutusLedgerApi.Test.ScriptContextBuilder.Builder
17+
( buildScriptContext
18+
, withAddress
19+
, withInlineDatum
20+
, withOutRef
21+
, withSigner
22+
, withSpendingScript
23+
, withValidRange
24+
)
1625
import PlutusLedgerApi.V1.Data.Value (assetClass)
17-
import PlutusTx.Data.AssocMap qualified as Map
18-
import PlutusTx.Data.List qualified as List
26+
import PlutusLedgerApi.V3 qualified as PV3
27+
import Prelude ((<>))
1928

2029
validatorCodeFullyApplied :: CompiledCode BuiltinUnit
2130
validatorCodeFullyApplied =
2231
validatorCode `unsafeApplyCode` liftCodeDef (toBuiltinData testScriptContext)
2332

24-
testScriptContext :: ScriptContext
33+
testScriptContext :: PV3.ScriptContext
2534
testScriptContext =
26-
ScriptContext
27-
{ scriptContextTxInfo = txInfo
28-
, scriptContextRedeemer
29-
, scriptContextScriptInfo
30-
}
35+
buildScriptContext
36+
( withValidRange
37+
( PV3.Interval
38+
(PV3.LowerBound (PV3.Finite 110) True)
39+
(PV3.UpperBound (PV3.Finite 1100) True)
40+
)
41+
<> withSigner testBeneficiaryPKH
42+
<> withSpendingScript
43+
(toBuiltinData FullUnlock)
44+
( withOutRef (PV3.TxOutRef txOutRefId txOutRefIdx)
45+
<> withAddress (PV3.Address (PV3.ScriptCredential scriptHash) Nothing)
46+
<> withInlineDatum (toBuiltinData testVestingDatum)
47+
)
48+
)
3149
where
32-
txInfo =
33-
TxInfo
34-
{ txInfoInputs = mempty
35-
, txInfoReferenceInputs = mempty
36-
, txInfoOutputs = mempty
37-
, txInfoTxCerts = mempty
38-
, txInfoRedeemers = Map.empty
39-
, txInfoVotes = Map.empty
40-
, txInfoProposalProcedures = mempty
41-
, txInfoCurrentTreasuryAmount = Nothing
42-
, txInfoTreasuryDonation = Nothing
43-
, txInfoFee = 0
44-
, txInfoMint = emptyMintValue
45-
, txInfoWdrl = Map.empty
46-
, txInfoValidRange =
47-
Interval
48-
(LowerBound (Finite 110) True)
49-
(UpperBound (Finite 1100) True)
50-
, txInfoSignatories = List.singleton testBeneficiaryPKH
51-
, txInfoData = Map.empty
52-
, txInfoId = "058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145"
53-
}
54-
55-
scriptContextRedeemer :: Redeemer
56-
scriptContextRedeemer = Redeemer (toBuiltinData FullUnlock)
57-
58-
scriptContextScriptInfo :: ScriptInfo
59-
scriptContextScriptInfo =
60-
SpendingScript (TxOutRef txOutRefId txOutRefIdx) (Just datum)
61-
where
62-
txOutRefId = "058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145"
63-
txOutRefIdx = 0
64-
datum :: Datum
65-
datum = Datum (toBuiltinData testVestingDatum)
50+
txOutRefId :: PV3.TxId
51+
txOutRefId = "058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145"
52+
txOutRefIdx :: Integer
53+
txOutRefIdx = 0
54+
scriptHash :: PV3.ScriptHash
55+
scriptHash = PV3.ScriptHash "deadbeef"
6656

6757
testVestingDatum :: VestingDatum
6858
testVestingDatum =
6959
VestingDatum
70-
{ beneficiary = Address (PubKeyCredential testBeneficiaryPKH) Nothing
71-
, vestingAsset = assetClass (CurrencySymbol "$") (TokenName "test-asset")
60+
{ beneficiary = PV3D.Address (PV3D.PubKeyCredential testBeneficiaryPKH) Nothing
61+
, vestingAsset = assetClass (PV3D.CurrencySymbol "$") (PV3D.TokenName "test-asset")
7262
, totalVestingQty = 1000
7363
, vestingPeriodStart = 0
7464
, vestingPeriodEnd = 100
7565
, firstUnlockPossibleAfter = 10
7666
, totalInstallments = 10
7767
}
7868

79-
testBeneficiaryPKH :: PubKeyHash
80-
testBeneficiaryPKH = PubKeyHash ""
69+
testBeneficiaryPKH :: PV3.PubKeyHash
70+
testBeneficiaryPKH = PV3.PubKeyHash ""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CPU: 30_405_131
22
Memory: 128_919
33
AST Size: 1_854
4-
Flat Size: 2_351
4+
Flat Size: 2_490
55

66
(con unit ())

plutus-benchmark/linear-vesting/test/9.6/main.golden.pir

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,24 @@
14781478
{all dead. dead})
14791479
(Constr 0
14801480
[ Constr 0
1481-
[ List []
1481+
[ List
1482+
[ Constr 0
1483+
[ Constr 0
1484+
[ B #058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145
1485+
, I 0 ]
1486+
, Constr 0
1487+
[ Constr 0 [Constr 1 [B #6465616462656566], Constr 1 []]
1488+
, Map []
1489+
, Constr 2
1490+
[ Constr 0
1491+
[ Constr 0 [Constr 0 [B #], Constr 1 []]
1492+
, Constr 0 [B #24, B #746573742d6173736574]
1493+
, I 1000
1494+
, I 0
1495+
, I 100
1496+
, I 10
1497+
, I 10 ] ]
1498+
, Constr 1 [] ] ] ]
14821499
, List []
14831500
, List []
14841501
, I 0
@@ -1489,9 +1506,14 @@
14891506
[ Constr 0 [Constr 1 [I 110], Constr 1 []]
14901507
, Constr 0 [Constr 1 [I 1100], Constr 1 []] ]
14911508
, List [B #]
1509+
, Map
1510+
[ ( Constr 1
1511+
[ Constr 0
1512+
[ B #058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145
1513+
, I 0 ] ]
1514+
, Constr 1 [] ) ]
14921515
, Map []
1493-
, Map []
1494-
, B #058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145
1516+
, B #6465616462656566
14951517
, Map []
14961518
, List []
14971519
, Constr 1 []

plutus-benchmark/linear-vesting/test/9.6/main.golden.uplc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,25 @@
12071207
(\s -> f (\x -> s s x)))
12081208
(Constr 0
12091209
[ Constr 0
1210-
[ List []
1210+
[ List
1211+
[ Constr 0
1212+
[ Constr 0
1213+
[ B #058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145
1214+
, I 0 ]
1215+
, Constr 0
1216+
[ Constr 0
1217+
[Constr 1 [B #6465616462656566], Constr 1 []]
1218+
, Map []
1219+
, Constr 2
1220+
[ Constr 0
1221+
[ Constr 0 [Constr 0 [B #], Constr 1 []]
1222+
, Constr 0 [B #24, B #746573742d6173736574]
1223+
, I 1000
1224+
, I 0
1225+
, I 100
1226+
, I 10
1227+
, I 10 ] ]
1228+
, Constr 1 [] ] ] ]
12111229
, List []
12121230
, List []
12131231
, I 0
@@ -1218,9 +1236,14 @@
12181236
[ Constr 0 [Constr 1 [I 110], Constr 1 []]
12191237
, Constr 0 [Constr 1 [I 1100], Constr 1 []] ]
12201238
, List [B #]
1239+
, Map
1240+
[ ( Constr 1
1241+
[ Constr 0
1242+
[ B #058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145
1243+
, I 0 ] ]
1244+
, Constr 1 [] ) ]
12211245
, Map []
1222-
, Map []
1223-
, B #058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145
1246+
, B #6465616462656566
12241247
, Map []
12251248
, List []
12261249
, Constr 1 []

plutus-benchmark/plutus-benchmark.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,9 @@ library linear-vesting-internal
778778
LinearVesting.Validator
779779

780780
build-depends:
781-
, base >=4.9 && <5
781+
, base >=4.9 && <5
782782
, plutus-ledger-api
783+
, plutus-ledger-api:plutus-ledger-api-testlib
783784
, plutus-tx
784785
, plutus-tx-plugin
785786

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Added
2+
3+
- Added `ScriptContextBuilder` test utility (`PlutusLedgerApi.Test.ScriptContextBuilder`) to `plutus-ledger-api-testlib` for constructing realistic `ScriptContext` values in tests using a composable builder pattern.

plutus-ledger-api/plutus-ledger-api.cabal

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ library plutus-ledger-api-testlib
161161
PlutusLedgerApi.Test.Common.EvaluationContext
162162
PlutusLedgerApi.Test.EvaluationEvent
163163
PlutusLedgerApi.Test.Examples
164+
PlutusLedgerApi.Test.ScriptContextBuilder.Builder
165+
PlutusLedgerApi.Test.ScriptContextBuilder.Lenses
166+
PlutusLedgerApi.Test.ScriptContextBuilder.Lenses.TH
164167
PlutusLedgerApi.Test.Scripts
165168
PlutusLedgerApi.Test.V1.Data.EvaluationContext
166169
PlutusLedgerApi.Test.V1.Data.Value
@@ -180,13 +183,15 @@ library plutus-ledger-api-testlib
180183
, base64-bytestring
181184
, bytestring
182185
, containers
186+
, lens
183187
, plutus-core ^>=1.59
184188
, plutus-core:plutus-core-testlib
185189
, plutus-ledger-api ^>=1.59
186190
, plutus-tx ^>=1.59
187191
, prettyprinter
188192
, QuickCheck
189193
, serialise
194+
, template-haskell
190195
, text
191196

192197
test-suite plutus-ledger-api-test

0 commit comments

Comments
 (0)