-
Notifications
You must be signed in to change notification settings - Fork 211
/
Model.hs
209 lines (194 loc) · 6.87 KB
/
Model.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
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeFamilies #-}
module Cardano.Wallet.DB.Store.DeltaUTxO.Model
( -- * Types
UTxOHistory
-- * Accessors
, getTip
, getFinality
, empty
, getUTxO
-- * Changes
, DeltaUTxOHistory (..)
-- * For testing
, getSpent
)
where
import Prelude
import Cardano.Slotting.Slot
( SlotNo, WithOrigin (..) )
import Cardano.Wallet.DB.Store.DeltaUTxO.Model.Internal
( UTxOHistory (..) )
import Cardano.Wallet.Primitive.Types
( Slot )
import Cardano.Wallet.Primitive.Types.Tx.TxIn
( TxIn )
import Cardano.Wallet.Primitive.Types.UTxO
( DeltaUTxO (..), UTxO, dom, excluding )
import Data.Delta
( Delta (..) )
import Data.Foldable
( fold, foldl' )
import Data.Map.Strict
( Map )
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
-- | Changes to the UTxO history.
data DeltaUTxOHistory
= -- | New slot tip, changes within that block.
AppendBlock SlotNo DeltaUTxO
| -- | Rollback tip.
Rollback Slot
| -- | Move finality forward.
Prune SlotNo
-- | An empty UTxO history
empty :: UTxO -> UTxOHistory
empty utxo =
UTxOHistory
{ history = utxo
, creationSlots = Map.singleton Origin $ dom utxo
, spentSlots = mempty
, spentTxIns = mempty
, tip = Origin
, finality = Nothing
}
-- | Returns the UTxO.
getUTxO :: UTxOHistory -> UTxO
getUTxO UTxOHistory {history, spentSlots} = history `excluding` fold spentSlots
-- | Returns the tip slot.
getTip :: UTxOHistory -> Slot
getTip UTxOHistory {tip} = tip
-- | Returns the finality slot.
getFinality :: UTxOHistory -> Maybe SlotNo
getFinality UTxOHistory {finality} = finality
-- | Returns the spent TxIns that can be rolled back.
getSpent :: UTxOHistory -> Map TxIn SlotNo
getSpent UTxOHistory {spentTxIns} = spentTxIns
onNotNull :: Foldable t1 => t1 a -> t2 -> (t1 a -> t2 -> t2) -> t2
onNotNull x d f =
if null x
then d
else f x d
-- how to apply a DeltaUTxOHistory to a UTxOHistory
instance Delta DeltaUTxOHistory where
type Base DeltaUTxOHistory = UTxOHistory
apply
(AppendBlock slot delta)
noop@UTxOHistory
{ history
, spentSlots
, creationSlots
, spentTxIns
, tip
, finality
}
| At slot <= tip = noop
| otherwise =
UTxOHistory
{ history = history <> received delta
, creationSlots = onNotNull
(dom $ received delta)
creationSlots
$ \received' ->
Map.insert
(At slot)
(received' `Set.difference` dom history)
, spentSlots = onNotNull
( (excluded delta `Set.intersection` dom history)
`Set.difference` fold spentSlots
)
spentSlots
$ \excluded' -> Map.insert slot excluded'
, spentTxIns =
foldl'
(\m txIn -> Map.insert txIn slot m)
spentTxIns
(excluded delta)
, tip = At slot
, finality = finality
}
apply
(Rollback slot)
noop@UTxOHistory
{ history
, spentSlots
, creationSlots
, spentTxIns
, tip
, finality
}
| slot >= tip = noop
| otherwise =
let slot' = maybe slot (max slot) $ At <$> finality
(leftCreationSlots, rolledCreatedSlots) =
Map.spanAntitone (<= slot') creationSlots
rolledSpentTxIns = fold $ case slot' of
Origin -> spentSlots
At slot'' ->
Map.dropWhileAntitone
(<= slot'')
spentSlots
rolledCreatedTxIns = fold rolledCreatedSlots
in UTxOHistory
{ history = history `excluding` rolledCreatedTxIns
, spentSlots = case slot' of
Origin -> mempty
At slot'' ->
Map.takeWhileAntitone
(<= slot'')
spentSlots
, creationSlots = leftCreationSlots
, spentTxIns =
Map.withoutKeys
spentTxIns
rolledSpentTxIns
, tip = slot'
, finality = finality
}
apply
(Prune newFinality)
noop@UTxOHistory
{ history
, spentSlots
, creationSlots
, spentTxIns
, tip
, finality
}
| finalityIsOlder finality = noop
| otherwise = constraintFinality tip $ \newFinality' ->
let (prunedSlots, leftSlots) =
Map.spanAntitone
(<= newFinality')
spentSlots
prunedTxIns = fold prunedSlots
fixCreationSlot
(txIn, slotNo) = Map.alter f (At slotNo)
where
f Nothing = Nothing
f (Just txIns) =
let txIns' = Set.delete txIn txIns
in if null txIns'
then Nothing
else Just txIns'
in UTxOHistory
{ history = history `excluding` prunedTxIns
, spentSlots = leftSlots
, creationSlots =
foldl'
(flip fixCreationSlot)
creationSlots
$ Map.assocs
$ Map.restrictKeys spentTxIns prunedTxIns
, spentTxIns =
Map.withoutKeys
spentTxIns
prunedTxIns
, tip = tip
, finality = Just newFinality'
}
where
finalityIsOlder Nothing = False
finalityIsOlder (Just oldFinality) = newFinality <= oldFinality
constraintFinality Origin _ = noop
constraintFinality (At slotNo) f = f $ min slotNo newFinality