3
3
module Cardano.Wallet.Deposit.Pure.Balance
4
4
( balance
5
5
, availableUTxO
6
+ , IsOurs
6
7
, applyBlock
7
8
) where
8
9
9
10
import Prelude
10
11
11
- import Cardano.Wallet.Deposit.Pure.UTxO
12
+ import Cardano.Wallet.Deposit.Pure.UTxO.DeltaUTxO
12
13
( DeltaUTxO
13
- , UTxO
14
+ )
15
+ import Cardano.Wallet.Deposit.Pure.UTxO.Tx
16
+ ( IsOurs
17
+ , applyTx
18
+ )
19
+ import Cardano.Wallet.Deposit.Pure.UTxO.UTxO
20
+ ( UTxO
14
21
, balance
15
22
, excluding
16
23
)
@@ -21,11 +28,8 @@ import Data.Set
21
28
( Set
22
29
)
23
30
24
- import qualified Cardano.Wallet.Deposit.Pure.UTxO.DeltaUTxO as DeltaUTxO
25
- import qualified Cardano.Wallet.Deposit.Pure.UTxO.UTxO as UTxO
26
31
import qualified Cardano.Wallet.Deposit.Read as Read
27
32
import qualified Cardano.Wallet.Deposit.Write as Write
28
- import qualified Cardano.Wallet.Read.Tx as Tx
29
33
30
34
{- ----------------------------------------------------------------------------
31
35
Wallet Balance
@@ -48,59 +52,18 @@ availableUTxO u pending =
48
52
{- ----------------------------------------------------------------------------
49
53
Applying Blocks
50
54
------------------------------------------------------------------------------}
51
- type IsOurs addr = addr -> Bool
52
-
53
55
-- | Apply a 'Block' to the 'UTxO'.
54
56
--
55
57
-- Returns both a delta and the new value.
56
58
applyBlock
57
59
:: IsOurs Read. Address -> Read. Block -> UTxO -> (DeltaUTxO , UTxO )
58
60
applyBlock isOurs block u0 =
59
61
(mconcat $ reverse dus, u1)
60
- where
62
+ where
61
63
(dus, u1) =
62
64
mapAccumL' (applyTx isOurs) u0
63
65
$ Read. transactions block
64
66
65
- -- | Apply a transactions to the 'UTxO'.
66
- --
67
- -- Returns both a delta and the new value.
68
- applyTx
69
- :: IsOurs Read. Address -> Read. Tx -> UTxO -> (DeltaUTxO , UTxO )
70
- applyTx isOurs tx u0 =
71
- if isUnchangedUTxO
72
- then (mempty , u0)
73
- else (du, u)
74
- where
75
- (du, u) = (du21 <> du10, u2)
76
-
77
- (du10, u1) = spendTxD tx u0
78
- receivedUTxO = UTxO. filterByAddress isOurs (Read. utxoFromEraTx tx)
79
- (du21, u2) = DeltaUTxO. receiveD u1 receivedUTxO
80
-
81
- -- NOTE: Performance.
82
- -- 'applyTx' is part of a tight loop that inspects all transactions
83
- -- (> 30M Txs as of Feb 2022).
84
- -- Thus, we make a small performance optimization here.
85
- -- Specifically, we want to reject a transaction as soon as possible
86
- -- if it does not change the 'UTxO' set. The test
87
- isUnchangedUTxO = UTxO. null receivedUTxO && DeltaUTxO. null du10
88
- -- allocates slightly fewer new Set/Map than the definition
89
- -- isUnchangedUTxO = mempty == du
90
-
91
- {- ----------------------------------------------------------------------------
92
- UTxO utilities
93
- ------------------------------------------------------------------------------}
94
- -- | Remove unspent outputs that are consumed by the given transaction.
95
- spendTxD :: Read. Tx -> UTxO -> (DeltaUTxO , UTxO )
96
- spendTxD tx ! u =
97
- u `DeltaUTxO.excludingD` inputsToExclude
98
- where
99
- inputsToExclude =
100
- case Tx. getScriptValidity tx of
101
- Tx. IsValidC True -> Tx. getInputs tx
102
- Tx. IsValidC False -> Tx. getCollateralInputs tx
103
-
104
67
{- ----------------------------------------------------------------------------
105
68
Helpers
106
69
------------------------------------------------------------------------------}
0 commit comments