-
Notifications
You must be signed in to change notification settings - Fork 86
/
TxCost.hs
277 lines (254 loc) · 10.1 KB
/
TxCost.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
{-# LANGUAGE DuplicateRecordFields #-}
module TxCost where
import Hydra.Prelude hiding (catch)
import Cardano.Api.UTxO qualified as UTxO
import Cardano.Binary (serialize)
import Data.ByteString.Lazy qualified as LBS
import Data.Maybe (fromJust)
import Hydra.Cardano.Api (
Coin (..),
ExecutionUnits (..),
Tx,
UTxO,
genTxIn,
)
import Hydra.Cardano.Api.TxOut (toPlutusTxOut)
import Hydra.Chain.Direct.State (
ClosedState (..),
InitialState (..),
OpenState (..),
commit,
ctxContestationPeriod,
ctxHeadParameters,
ctxHydraSigningKeys,
ctxParticipants,
ctxVerificationKeys,
genCloseTx,
genCommits,
genCommits',
genHydraContextFor,
genInitTx,
genStClosed,
genStInitial,
genStOpen,
getContestationDeadline,
getKnownUTxO,
initialize,
observeClose,
pickChainContext,
unsafeAbort,
unsafeClose,
unsafeCollect,
unsafeContest,
unsafeFanout,
unsafeObserveInitAndCommits,
)
import Hydra.Ledger.Cardano (
genOutput,
genUTxOAdaOnlyOfSize,
)
import Hydra.Ledger.Cardano.Evaluate (
estimateMinFee,
evaluateTx,
genPointInTimeBefore,
genValidityBoundsFromContestationPeriod,
maxTxSize,
slotLength,
systemStart,
usedExecutionUnits,
)
import Hydra.Ledger.Cardano.Time (slotNoFromUTCTime)
import Hydra.Plutus.Orphans ()
import Hydra.Snapshot (genConfirmedSnapshot)
import PlutusLedgerApi.V2 (toBuiltinData)
import PlutusTx.Builtins (lengthOfByteString, serialiseData)
import Test.QuickCheck (generate)
computeInitCost :: IO [(NumParties, TxSize, MemUnit, CpuUnit, Coin)]
computeInitCost = do
interesting <- catMaybes <$> mapM compute [1, 2, 3, 5, 10]
limit <- maybeToList . getFirst <$> foldMapM (fmap First . compute) [100, 99 .. 11]
pure $ interesting <> limit
where
compute numParties = do
(tx, knownUtxo) <- generate $ genInitTx' numParties
case checkSizeAndEvaluate tx knownUtxo of
Just (txSize, memUnit, cpuUnit, minFee) ->
pure $ Just (NumParties numParties, txSize, memUnit, cpuUnit, minFee)
Nothing ->
pure Nothing
genInitTx' numParties = do
ctx <- genHydraContextFor numParties
cctx <- pickChainContext ctx
seedInput <- genTxIn
seedOutput <- genOutput =<< arbitrary
let utxo = UTxO.singleton (seedInput, seedOutput)
pure (initialize cctx seedInput (ctxParticipants ctx) (ctxHeadParameters ctx), utxo)
computeCommitCost :: IO [(NumUTxO, TxSize, MemUnit, CpuUnit, Coin)]
computeCommitCost = do
interesting <- catMaybes <$> mapM compute [1, 2, 3, 5, 10]
limit <- maybeToList . getFirst <$> foldMapM (fmap First . compute) [100, 99 .. 11]
pure $ interesting <> limit
where
compute numUTxO = do
utxo <- generate $ genUTxOAdaOnlyOfSize numUTxO
(commitTx, knownUtxo) <- generate $ genCommitTx utxo
case commitTx of
Left _ -> pure Nothing
Right tx ->
case checkSizeAndEvaluate tx (utxo <> knownUtxo) of
Just (txSize, memUnit, cpuUnit, minFee) ->
pure $ Just (NumUTxO $ length utxo, txSize, memUnit, cpuUnit, minFee)
Nothing ->
pure Nothing
genCommitTx utxo = do
-- NOTE: number of parties is irrelevant for commit tx
ctx <- genHydraContextFor 1
(cctx, stInitial) <- genStInitial ctx
let InitialState{headId} = stInitial
knownUTxO = getKnownUTxO stInitial <> getKnownUTxO cctx
blueprintTx <- arbitrary
pure (commit cctx headId knownUTxO utxo blueprintTx, knownUTxO)
computeCollectComCost :: IO [(NumParties, Natural, TxSize, MemUnit, CpuUnit, Coin)]
computeCollectComCost =
catMaybes <$> mapM compute [1 .. 10]
where
compute numParties = do
(utxo, tx, knownUtxo) <- generate $ genCollectComTx numParties
case checkSizeAndEvaluate tx knownUtxo of
Just (txSize, memUnit, cpuUnit, minFee) ->
pure $ Just (NumParties numParties, serializedSize utxo, txSize, memUnit, cpuUnit, minFee)
Nothing ->
pure Nothing
genCollectComTx numParties = do
ctx <- genHydraContextFor numParties
cctx <- pickChainContext ctx
initTx <- genInitTx ctx
commits <- genCommits' (genUTxOAdaOnlyOfSize 1) ctx initTx
let (committedUTxOs, stInitialized) = unsafeObserveInitAndCommits cctx (ctxVerificationKeys ctx) initTx commits
let InitialState{headId} = stInitialized
let utxoToCollect = fold committedUTxOs
let spendableUTxO = getKnownUTxO stInitialized
pure (fold committedUTxOs, unsafeCollect cctx headId (ctxHeadParameters ctx) utxoToCollect spendableUTxO, getKnownUTxO stInitialized <> getKnownUTxO cctx)
computeCloseCost :: IO [(NumParties, TxSize, MemUnit, CpuUnit, Coin)]
computeCloseCost = do
interesting <- catMaybes <$> mapM compute [1, 2, 3, 5, 10]
limit <- maybeToList . getFirst <$> foldMapM (fmap First . compute) [50, 49 .. 11]
pure $ interesting <> limit
where
compute numParties = do
(ctx, st, tx, _sn) <- generate $ genCloseTx numParties
let utxo = getKnownUTxO st <> getKnownUTxO ctx
case checkSizeAndEvaluate tx utxo of
Just (txSize, memUnit, cpuUnit, minFee) ->
pure $ Just (NumParties numParties, txSize, memUnit, cpuUnit, minFee)
Nothing ->
pure Nothing
computeContestCost :: IO [(NumParties, TxSize, MemUnit, CpuUnit, Coin)]
computeContestCost = do
interesting <- catMaybes <$> mapM compute [1, 2, 3, 5, 10]
limit <- maybeToList . getFirst <$> foldMapM (fmap First . compute) [50, 49 .. 11]
pure $ interesting <> limit
where
compute numParties = do
(tx, utxo) <- generate $ genContestTx numParties
case checkSizeAndEvaluate tx utxo of
Just (txSize, memUnit, cpuUnit, minFee) ->
pure $ Just (NumParties numParties, txSize, memUnit, cpuUnit, minFee)
Nothing ->
pure Nothing
genContestTx numParties = do
ctx <- genHydraContextFor numParties
utxo <- arbitrary
(closedSnapshotNumber, _, stClosed@ClosedState{headId}) <- genStClosed ctx utxo
cctx <- pickChainContext ctx
snapshot <- genConfirmedSnapshot headId (succ closedSnapshotNumber) utxo (ctxHydraSigningKeys ctx)
pointInTime <- genPointInTimeBefore (getContestationDeadline stClosed)
let cp = ctxContestationPeriod ctx
let contestUtxo = getKnownUTxO stClosed <> getKnownUTxO cctx
pure (unsafeContest cctx contestUtxo headId cp snapshot pointInTime, contestUtxo)
computeAbortCost :: IO [(NumParties, TxSize, MemUnit, CpuUnit, Coin)]
computeAbortCost =
-- NOTE: We can't even close with one party right now, so no point in
-- determining interesting values
catMaybes <$> forM [1 .. 100] compute
where
compute numParties = do
(tx, utxo) <- generate $ genAbortTx numParties
case checkSizeAndEvaluate tx utxo of
Just (txSize, memUnit, cpuUnit, minFee) -> do
pure $ Just (NumParties numParties, txSize, memUnit, cpuUnit, minFee)
Nothing ->
pure Nothing
genAbortTx numParties = do
ctx <- genHydraContextFor numParties
initTx <- genInitTx ctx
-- NOTE: Commits are more expensive to abort, so let's use all commits
commits <- genCommits ctx initTx
cctx <- pickChainContext ctx
let (committed, stInitialized) = unsafeObserveInitAndCommits cctx (ctxVerificationKeys ctx) initTx commits
let InitialState{seedTxIn} = stInitialized
let spendableUTxO = getKnownUTxO stInitialized <> getKnownUTxO cctx
pure (unsafeAbort cctx seedTxIn spendableUTxO (fold committed), spendableUTxO)
computeFanOutCost :: IO [(NumParties, NumUTxO, Natural, TxSize, MemUnit, CpuUnit, Coin)]
computeFanOutCost = do
interesting <- catMaybes <$> mapM (uncurry compute) [(p, u) | p <- [5], u <- [0, 1, 5, 10, 20, 30, 40, 50]]
limit <-
maybeToList
. getFirst
<$> foldMapM
(\(p, u) -> First <$> compute p u)
[(p, u) | p <- [5], u <- [100, 99 .. 0]]
pure $ interesting <> limit
where
compute parties numElems = do
(utxo, tx, knownUTxO) <- generate $ genFanoutTx parties numElems
let utxoSerializedSize = serializedSize utxo
case checkSizeAndEvaluate tx knownUTxO of
Just (txSize, memUnit, cpuUnit, minFee) ->
pure $ Just (NumParties parties, NumUTxO numElems, utxoSerializedSize, txSize, memUnit, cpuUnit, minFee)
Nothing ->
pure Nothing
-- Generate a fanout with a defined number of outputs.
genFanoutTx numParties numOutputs = do
utxo <- genUTxOAdaOnlyOfSize numOutputs
ctx <- genHydraContextFor numParties
(_committed, stOpen@OpenState{headId, seedTxIn}) <- genStOpen ctx
snapshot <- genConfirmedSnapshot headId 1 utxo [] -- We do not validate the signatures
cctx <- pickChainContext ctx
let cp = ctxContestationPeriod ctx
(startSlot, closePoint) <- genValidityBoundsFromContestationPeriod cp
let closeTx = unsafeClose cctx (getKnownUTxO stOpen) headId (ctxHeadParameters ctx) snapshot startSlot closePoint
stClosed = snd . fromJust $ observeClose stOpen closeTx
deadlineSlotNo = slotNoFromUTCTime systemStart slotLength (getContestationDeadline stClosed)
utxoToFanout = getKnownUTxO stClosed <> getKnownUTxO cctx
pure (utxo, unsafeFanout cctx utxoToFanout seedTxIn utxo deadlineSlotNo, getKnownUTxO stClosed <> getKnownUTxO cctx)
newtype NumParties = NumParties Int
deriving newtype (Eq, Show, Ord, Num, Real, Enum, Integral)
newtype NumUTxO = NumUTxO Int
deriving newtype (Eq, Show, Ord, Num, Real, Enum, Integral)
newtype TxSize = TxSize Natural
deriving newtype (Eq, Show, Ord, Num, Real, Enum, Integral)
newtype MemUnit = MemUnit Natural
deriving newtype (Eq, Show, Ord, Num, Real, Enum, Integral)
newtype CpuUnit = CpuUnit Natural
deriving newtype (Eq, Show, Ord, Num, Real, Enum, Integral)
checkSizeAndEvaluate :: Tx -> UTxO -> Maybe (TxSize, MemUnit, CpuUnit, Coin)
checkSizeAndEvaluate tx knownUTxO = do
guard $ txSize < maxTxSize
case evaluateTx tx knownUTxO of
(Right report) -> do
guard $ all isRight report
let ExecutionUnits
{ executionMemory = usedMemory
, executionSteps = usedCpu
} = usedExecutionUnits report
let minFee = estimateMinFee tx report
Just (TxSize txSize, MemUnit usedMemory, CpuUnit usedCpu, minFee)
_ -> Nothing
where
txSize = fromIntegral $ LBS.length $ serialize tx
serializedSize :: UTxO -> Natural
serializedSize =
fromIntegral
. lengthOfByteString
. foldMap (serialiseData . toBuiltinData . fromJust . toPlutusTxOut)