2
2
module Cardano.Wallet.Kernel.BListener (
3
3
-- * Respond to block chain events
4
4
applyBlock
5
- , applyBlocks
6
5
, switchToFork
7
6
-- * Testing
8
7
, observableRollbackUseInTestsOnly
@@ -13,16 +12,15 @@ import Universum hiding (State)
13
12
import Control.Concurrent.MVar (modifyMVar_ )
14
13
import Data.Acid.Advanced (update' )
15
14
16
- import Pos.Core (SlotId )
17
- import Pos.Core.Chrono (OldestFirst )
18
15
import Pos.Crypto (EncryptedSecretKey )
19
16
20
17
import Cardano.Wallet.Kernel.DB.AcidState (ApplyBlock (.. ),
21
- ObservableRollbackUseInTestsOnly (.. ),
22
- RollbackDuringRestoration , SwitchToFork (.. ))
18
+ ObservableRollbackUseInTestsOnly (.. ), SwitchToFork (.. ),
19
+ SwitchToForkError (.. ))
20
+ import Cardano.Wallet.Kernel.DB.BlockContext
23
21
import Cardano.Wallet.Kernel.DB.HdWallet
24
- import Cardano.Wallet.Kernel.DB.InDb
25
- import Cardano.Wallet.Kernel.DB.Resolved ( ResolvedBlock , rbSlotId )
22
+ import Cardano.Wallet.Kernel.DB.Resolved ( ResolvedBlock , rbContext )
23
+ import Cardano.Wallet.Kernel.DB.Spec.Update ( ApplyBlockFailed )
26
24
import Cardano.Wallet.Kernel.DB.TxMeta.Types
27
25
import Cardano.Wallet.Kernel.Internal
28
26
import qualified Cardano.Wallet.Kernel.NodeStateAdaptor as Node
@@ -41,35 +39,31 @@ import Cardano.Wallet.Kernel.Types (WalletId (..))
41
39
-- TODO: Improve performance (CBR-379)
42
40
prefilterBlock' :: PassiveWallet
43
41
-> ResolvedBlock
44
- -> IO ((SlotId , Map HdAccountId PrefilteredBlock ), [TxMeta ])
42
+ -> IO ((BlockContext , Map HdAccountId PrefilteredBlock ), [TxMeta ])
45
43
prefilterBlock' pw b = do
46
44
aux <$> getWalletCredentials pw
47
45
where
48
46
aux :: [(WalletId , EncryptedSecretKey )]
49
- -> ((SlotId , Map HdAccountId PrefilteredBlock ), [TxMeta ])
47
+ -> ((BlockContext , Map HdAccountId PrefilteredBlock ), [TxMeta ])
50
48
aux ws =
51
49
let (conMap, conMeta) = mconcat $ map (uncurry (prefilterBlock b)) ws
52
- in ((b ^. rbSlotId , conMap), conMeta)
50
+ in ((b ^. rbContext , conMap), conMeta)
53
51
54
52
-- | Notify all the wallets in the PassiveWallet of a new block
55
53
applyBlock :: PassiveWallet
56
54
-> ResolvedBlock
57
- -> IO ()
55
+ -> IO (Either ApplyBlockFailed () )
58
56
applyBlock pw@ PassiveWallet {.. } b = do
59
57
k <- Node. getSecurityParameter _walletNode
60
- ((slotId , blocksByAccount), metas) <- prefilterBlock' pw b
58
+ ((ctxt , blocksByAccount), metas) <- prefilterBlock' pw b
61
59
-- apply block to all Accounts in all Wallets
62
- confirmed <- update' _wallets $ ApplyBlock k (InDb slotId) blocksByAccount
63
- modifyMVar_ _walletSubmission $ return . Submission. remPending confirmed
64
- mapM_ (putTxMeta _walletMeta) metas
65
-
66
- -- | Apply multiple blocks, one at a time, to all wallets in the PassiveWallet
67
- --
68
- -- TODO(@matt-noonan) this will be the responsibility of the worker thread (as part of CBR-243: Wallet restoration)
69
- applyBlocks :: PassiveWallet
70
- -> OldestFirst [] ResolvedBlock
71
- -> IO ()
72
- applyBlocks = mapM_ . applyBlock
60
+ mConfirmed <- update' _wallets $ ApplyBlock k ctxt blocksByAccount
61
+ case mConfirmed of
62
+ Left err -> return $ Left err
63
+ Right confirmed -> do
64
+ modifyMVar_ _walletSubmission $ return . Submission. remPending confirmed
65
+ mapM_ (putTxMeta _walletMeta) metas
66
+ return $ Right ()
73
67
74
68
-- | Switch to a new fork
75
69
--
@@ -78,7 +72,7 @@ applyBlocks = mapM_ . applyBlock
78
72
switchToFork :: PassiveWallet
79
73
-> Int -- ^ Number of blocks to roll back
80
74
-> [ResolvedBlock ] -- ^ Blocks in the new fork
81
- -> IO (Either RollbackDuringRestoration () )
75
+ -> IO (Either SwitchToForkError () )
82
76
switchToFork pw@ PassiveWallet {.. } n bs = do
83
77
k <- Node. getSecurityParameter _walletNode
84
78
blocksAndMeta <- mapM (prefilterBlock' pw) bs
@@ -98,7 +92,7 @@ switchToFork pw@PassiveWallet{..} n bs = do
98
92
-- Only used for tests. See 'switchToFork'.
99
93
-- TODO(kde): Do we want tests to deal with metadata?
100
94
observableRollbackUseInTestsOnly :: PassiveWallet
101
- -> IO (Either RollbackDuringRestoration () )
95
+ -> IO (Either SwitchToForkError () )
102
96
observableRollbackUseInTestsOnly PassiveWallet {.. } = do
103
97
res <- update' _wallets $ ObservableRollbackUseInTestsOnly
104
98
case res of
0 commit comments