-
Notifications
You must be signed in to change notification settings - Fork 155
/
Utxo.hs
186 lines (178 loc) · 6.73 KB
/
Utxo.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE RecordWildCards #-}
-- | Specs necessary to generate, environment, state, and signal
-- for the UTXO rule
module Test.Cardano.Ledger.Constrained.Conway.Utxo where
import Cardano.Ledger.Babbage.TxOut
import Cardano.Ledger.BaseTypes
import Cardano.Ledger.Conway.PParams
import Cardano.Ledger.Mary.Value
import Cardano.Ledger.Shelley.API.Types
import Cardano.Ledger.UTxO
import Data.Foldable
import Data.Map qualified as Map
import Data.Maybe
import Data.Set qualified as Set
import Data.Word
import Lens.Micro
import Constrained
import Cardano.Ledger.Conway (ConwayEra)
import Cardano.Ledger.Conway.Core (EraTx (..), ppMaxCollateralInputsL)
import Cardano.Ledger.Crypto (StandardCrypto)
import Test.Cardano.Ledger.Constrained.Conway.Instances
import Test.Cardano.Ledger.Constrained.Conway.PParams
utxoEnvSpec :: IsConwayUniv fn => Specification fn (UtxoEnv (ConwayEra StandardCrypto))
utxoEnvSpec =
constrained $ \utxoEnv ->
match utxoEnv $
\_ueSlot
uePParams
_ueCertState ->
[ satisfies uePParams pparamsSpec
, match uePParams $ \cpp ->
match cpp $
\_cppMinFeeA
_cppMinFeeB
_cppMaxBBSize
cppMaxTxSize
_cppMaxBHSize
_cppKeyDeposit
_cppPoolDeposit
_cppEMax
_cppNOpt
_cppA0
_cppRho
_cppTau
_cppProtocolVersion
_cppMinPoolCost
_cppCoinsPerUTxOByte
_cppCostModels
_cppPrices
_cppMaxTxExUnits
_cppMaxBlockExUnits
_cppMaxValSize
_cppCollateralPercentage
_cppMaxCollateralInputs
_cppPoolVotingThresholds
_cppDRepVotingThresholds
_cppCommitteeMinSize
_cppCommitteeMaxTermLength
_cppGovActionLifetime
_cppGovActionDeposit
_cppDRepDeposit
_cppDRepActivity
_cppMinFeeRefScriptCoinsPerByte ->
-- NOTE: this is for testing only! We should figure out a nicer way
-- of splitting generation and checking constraints here!
[ assert $ lit (THKD 3000) ==. cppMaxTxSize
]
]
utxoStateSpec ::
IsConwayUniv fn =>
UtxoEnv (ConwayEra StandardCrypto) ->
Specification fn (UTxOState (ConwayEra StandardCrypto))
utxoStateSpec _env =
constrained $ \utxoState ->
match utxoState $
\utxosUtxo
_utxosDeposited
_utxosFees
_utxosGovState
_utxosStakeDistr
_utxosDonation ->
[ assert $ utxosUtxo /=. lit mempty
, match utxosUtxo $ \utxoMap ->
forAll (rng_ utxoMap) correctAddrAndWFCoin
]
utxoTxSpec ::
IsConwayUniv fn =>
UtxoEnv (ConwayEra StandardCrypto) ->
UTxOState (ConwayEra StandardCrypto) ->
Specification fn (Tx (ConwayEra StandardCrypto))
utxoTxSpec env st =
constrained $ \tx ->
match tx $ \bdy _wits isValid _auxData ->
[ match isValid assert
, match bdy $
\ctbSpendInputs
ctbCollateralInputs
_ctbReferenceInputs
ctbOutputs
ctbCollateralReturn
_ctbTotalCollateral
_ctbCerts
ctbWithdrawals
ctbTxfee
ctbVldt
_ctbReqSignerHashes
_ctbMint
_ctbScriptIntegrityHash
_ctbAdHash
ctbTxNetworkId
_ctbVotingProcedures
ctbProposalProcedures
_ctbCurrentTreasuryValue
ctbTreasuryDonation ->
[ assert $ ctbSpendInputs /=. lit mempty
, assert $ ctbSpendInputs `subset_` lit (Map.keysSet $ unUTxO $ utxosUtxo st)
, match ctbWithdrawals $ \withdrawalMap ->
forAll' (dom_ withdrawalMap) $ \net _ ->
net ==. lit Testnet
, -- TODO: we need to do this for collateral as well?
match ctbProposalProcedures $ \proposalsList ->
match ctbOutputs $ \outputList ->
[ (reify ctbSpendInputs)
( \actualInputs ->
fold
[ c | i <- Set.toList actualInputs, BabbageTxOut _ (MaryValue c _) _ _ <- maybeToList . txinLookup i . utxosUtxo $ st
]
)
$ \totalValueConsumed ->
[ -- TODO: the yuck here needs to be the selector Fn for the
-- spending field in the outputs. Need to make selectors work!
let outputSum =
foldMap_
( composeFn toGenericFn $
composeFn fstFn $
composeFn sndFn $
composeFn toGenericFn $
composeFn fstFn $
toGenericFn
)
outputList
depositSum =
foldMap_
(composeFn fstFn toGenericFn)
proposalsList
in outputSum + depositSum + ctbTxfee + ctbTreasuryDonation ==. totalValueConsumed
]
, forAll outputList (flip onSized correctAddrAndWFCoin)
]
, match ctbVldt $ \before after ->
[ onJust' before (<=. lit (ueSlot env))
, onJust' after (lit (ueSlot env) <.)
]
, onJust' ctbTxNetworkId (==. lit Testnet)
, onJust' ctbCollateralReturn $ flip onSized correctAddrAndWFCoin
, assert $ size_ ctbCollateralInputs <=. lit (fromIntegral $ uePParams env ^. ppMaxCollateralInputsL)
]
]
correctAddrAndWFCoin ::
IsConwayUniv fn =>
Term fn (TxOut (ConwayEra StandardCrypto)) ->
Pred fn
correctAddrAndWFCoin txOut =
match txOut $ \addr v _ _ ->
[ match v $ \c -> [0 <. c, c <=. fromIntegral (maxBound :: Word64)]
, (caseOn addr)
(branch $ \n _ _ -> n ==. lit Testnet)
( branch $ \bootstrapAddr ->
match bootstrapAddr $ \_ nm _ ->
(caseOn nm)
(branch $ \_ -> False)
(branch $ \_ -> True)
)
]