Skip to content

Commit e691278

Browse files
Use applyTx from cardano-wallet-agda
1 parent 8486b89 commit e691278

File tree

1 file changed

+10
-47
lines changed
  • lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure

1 file changed

+10
-47
lines changed

lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs

+10-47
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
module Cardano.Wallet.Deposit.Pure.Balance
44
( balance
55
, availableUTxO
6+
, IsOurs
67
, applyBlock
78
) where
89

910
import Prelude
1011

11-
import Cardano.Wallet.Deposit.Pure.UTxO
12+
import Cardano.Wallet.Deposit.Pure.UTxO.DeltaUTxO
1213
( 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
1421
, balance
1522
, excluding
1623
)
@@ -21,11 +28,8 @@ import Data.Set
2128
( Set
2229
)
2330

24-
import qualified Cardano.Wallet.Deposit.Pure.UTxO.DeltaUTxO as DeltaUTxO
25-
import qualified Cardano.Wallet.Deposit.Pure.UTxO.UTxO as UTxO
2631
import qualified Cardano.Wallet.Deposit.Read as Read
2732
import qualified Cardano.Wallet.Deposit.Write as Write
28-
import qualified Cardano.Wallet.Read.Tx as Tx
2933

3034
{-----------------------------------------------------------------------------
3135
Wallet Balance
@@ -48,59 +52,18 @@ availableUTxO u pending =
4852
{-----------------------------------------------------------------------------
4953
Applying Blocks
5054
------------------------------------------------------------------------------}
51-
type IsOurs addr = addr -> Bool
52-
5355
-- | Apply a 'Block' to the 'UTxO'.
5456
--
5557
-- Returns both a delta and the new value.
5658
applyBlock
5759
:: IsOurs Read.Address -> Read.Block -> UTxO -> (DeltaUTxO, UTxO)
5860
applyBlock isOurs block u0 =
5961
(mconcat $ reverse dus, u1)
60-
where
62+
where
6163
(dus, u1) =
6264
mapAccumL' (applyTx isOurs) u0
6365
$ Read.transactions block
6466

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-
10467
{-----------------------------------------------------------------------------
10568
Helpers
10669
------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)