/
Network.hs
1399 lines (1289 loc) · 53 KB
/
Network.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoMonoLocalBinds #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
-- |
-- Copyright: © 2020 IOHK
-- License: Apache-2.0
--
-- Network Layer for talking to Haskell re-written nodes.
--
-- Good to read before / additional resources:
--
-- - Module's documentation in `ouroboros-network/typed-protocols/src/Network/TypedProtocols.hs`
-- - Data Diffusion and Peer Networking in Shelley (see: https://raw.githubusercontent.com/wiki/input-output-hk/cardano-wallet/data_diffusion_and_peer_networking_in_shelley.pdf)
-- - In particular sections 4.1, 4.2, 4.6 and 4.8
module Cardano.Wallet.Shelley.Network
( -- * Top-Level Interface
pattern Cursor
, withNetworkLayer
, Observer (query,startObserving,stopObserving)
, newObserver
, ObserverLog (..)
-- * Logging
, NetworkLayerLog (..)
) where
import Prelude
import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Data.Tracer
( HasPrivacyAnnotation (..), HasSeverityAnnotation (..) )
import Cardano.Launcher.Node
( CardanoNodeConn, nodeSocketFile )
import Cardano.Wallet.Byron.Compatibility
( byronCodecConfig, protocolParametersFromUpdateState )
import Cardano.Wallet.Logging
( BracketLog (..), bracketTracer, produceTimings )
import Cardano.Wallet.Network
( Cursor, ErrPostTx (..), NetworkLayer (..), mapCursor )
import Cardano.Wallet.Primitive.Slotting
( TimeInterpreter
, TimeInterpreterLog
, currentRelativeTime
, mkTimeInterpreter
)
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..), SyncTolerance )
import Cardano.Wallet.Shelley.Compatibility
( AnyCardanoEra (..)
, CardanoEra (..)
, StandardCrypto
, fromCardanoHash
, fromChainHash
, fromNonMyopicMemberRewards
, fromPoolDistr
, fromShelleyCoin
, fromShelleyPParams
, fromStakeCredential
, fromTip
, fromTip'
, nodeToClientVersion
, optimumNumberOfPools
, slottingParametersFromGenesis
, toCardanoEra
, toPoint
, toShelleyCoin
, toStakeCredential
, unsealShelleyTx
)
import Control.Applicative
( liftA3 )
import Control.Monad
( forever, guard, unless, void, when, (>=>) )
import Control.Monad.Class.MonadAsync
( MonadAsync )
import Control.Monad.Class.MonadST
( MonadST )
import Control.Monad.Class.MonadSTM
( MonadSTM
, STM
, TMVar
, TQueue
, TVar
, atomically
, isEmptyTMVar
, modifyTVar'
, newEmptyTMVar
, newTMVarIO
, newTQueue
, newTVar
, putTMVar
, readTMVar
, readTVar
, takeTMVar
, tryReadTMVar
, writeTVar
)
import Control.Monad.Class.MonadThrow
( MonadThrow )
import Control.Monad.Class.MonadTimer
( MonadTimer, threadDelay )
import Control.Monad.IO.Unlift
( MonadIO, MonadUnliftIO, liftIO )
import Control.Monad.Trans.Except
( ExceptT (..), runExceptT, throwE )
import Control.Retry
( RetryPolicyM, RetryStatus (..), capDelay, fibonacciBackoff, recovering )
import Control.Tracer
( Tracer (..), contramap, nullTracer, traceWith )
import Data.ByteArray.Encoding
( Base (..), convertToBase )
import Data.ByteString.Lazy
( ByteString )
import Data.Function
( (&) )
import Data.Functor.Identity
( runIdentity )
import Data.List
( isInfixOf )
import Data.Map
( Map, (!) )
import Data.Maybe
( fromMaybe )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Percentage )
import Data.Set
( Set )
import Data.Text
( Text )
import Data.Text.Class
( ToText (..) )
import Data.Time.Clock
( DiffTime )
import Data.Void
( Void )
import Fmt
( Buildable (..), fmt, listF, mapF, pretty )
import GHC.Stack
( HasCallStack )
import Network.Mux
( MuxError (..), MuxErrorType (..), WithMuxBearer (..) )
import Ouroboros.Consensus.Cardano
( CardanoBlock )
import Ouroboros.Consensus.Cardano.Block
( CardanoApplyTxErr
, CardanoEras
, CodecConfig (..)
, GenTx (..)
, Query (..)
)
import Ouroboros.Consensus.HardFork.Combinator
( EraIndex (..), QueryAnytime (..), QueryHardFork (..), eraIndexToInt )
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras
( MismatchEraInfo )
import Ouroboros.Consensus.HardFork.History.Qry
( Interpreter, PastHorizonException (..) )
import Ouroboros.Consensus.Network.NodeToClient
( ClientCodecs, Codecs' (..), DefaultCodecs, clientCodecs, defaultCodecs )
import Ouroboros.Consensus.Node.NetworkProtocolVersion
( HasNetworkProtocolVersion (..), SupportedNetworkProtocolVersion (..) )
import Ouroboros.Consensus.Shelley.Ledger.Config
( CodecConfig (..), getCompactGenesis )
import Ouroboros.Network.Block
( Point
, SlotNo (..)
, Tip (..)
, blockPoint
, genesisPoint
, getPoint
, pointHash
, pointSlot
)
import Ouroboros.Network.Client.Wallet
( ChainSyncCmd (..)
, ChainSyncLog (..)
, LSQ (..)
, LocalStateQueryCmd (..)
, LocalTxSubmissionCmd (..)
, chainSyncFollowTip
, chainSyncWithBlocks
, localStateQuery
, localTxSubmission
, mapChainSyncLog
, send
)
import Ouroboros.Network.CodecCBORTerm
( CodecCBORTerm )
import Ouroboros.Network.Driver.Simple
( TraceSendRecv, runPeer, runPipelinedPeer )
import Ouroboros.Network.Mux
( MuxMode (..)
, MuxPeer (..)
, OuroborosApplication (..)
, RunMiniProtocol (..)
)
import Ouroboros.Network.NodeToClient
( ConnectionId (..)
, Handshake
, LocalAddress
, NetworkConnectTracers (..)
, NodeToClientProtocols (..)
, NodeToClientVersion
, NodeToClientVersionData (..)
, connectTo
, localSnocket
, nodeToClientProtocols
, withIOManager
)
import Ouroboros.Network.Point
( WithOrigin (..), fromWithOrigin )
import Ouroboros.Network.Protocol.ChainSync.Client
( chainSyncClientPeer )
import Ouroboros.Network.Protocol.ChainSync.ClientPipelined
( chainSyncClientPeerPipelined )
import Ouroboros.Network.Protocol.Handshake.Version
( simpleSingletonVersions )
import Ouroboros.Network.Protocol.LocalStateQuery.Client
( localStateQueryClientPeer )
import Ouroboros.Network.Protocol.LocalStateQuery.Type
( LocalStateQuery )
import Ouroboros.Network.Protocol.LocalTxSubmission.Client
( localTxSubmissionClientPeer )
import Ouroboros.Network.Protocol.LocalTxSubmission.Type
( LocalTxSubmission, SubmitResult (..) )
import System.IO.Error
( isDoesNotExistError )
import UnliftIO.Async
( Async, async, asyncThreadId, cancel, link )
import UnliftIO.Compat
( coerceHandlers )
import UnliftIO.Concurrent
( ThreadId )
import UnliftIO.Exception
( Handler (..), IOException )
import qualified Cardano.Ledger.Core as SL.Core
import qualified Cardano.Wallet.Primitive.SyncProgress as SyncProgress
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.Coin as W
import qualified Cardano.Wallet.Primitive.Types.Hash as W
import qualified Cardano.Wallet.Primitive.Types.RewardAccount as W
import qualified Cardano.Wallet.Primitive.Types.Tx as W
import qualified Codec.CBOR.Term as CBOR
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Ouroboros.Consensus.Byron.Ledger as Byron
import qualified Ouroboros.Consensus.Shelley.Ledger as Shelley
import qualified Ouroboros.Network.Point as Point
import qualified Shelley.Spec.Ledger.API as SL
import qualified Shelley.Spec.Ledger.LedgerState as SL
{- HLINT ignore "Use readTVarIO" -}
{- HLINT ignore "Use newTVarIO" -}
{- HLINT ignore "Use newEmptyTMVarIO" -}
-- | Network layer cursor for Shelley. Mostly useless since the protocol itself is
-- stateful and the node's keep track of the associated connection's cursor.
data instance Cursor = Cursor
(Async ())
(Point (CardanoBlock StandardCrypto))
(TQueue IO (ChainSyncCmd (CardanoBlock StandardCrypto) IO))
-- | Create an instance of the network layer
withNetworkLayer
:: HasCallStack
=> Tracer IO NetworkLayerLog
-- ^ Logging of network layer startup
-> W.NetworkParameters
-- ^ Initial blockchain parameters
-> CardanoNodeConn
-- ^ Socket for communicating with the node
-> (NodeToClientVersionData, CodecCBORTerm Text NodeToClientVersionData)
-- ^ Codecs for the node's client
-> SyncTolerance
-> (NetworkLayer IO (CardanoBlock StandardCrypto) -> IO a)
-- ^ Callback function with the network layer
-> IO a
withNetworkLayer tr np conn ver tol action = do
trTimings <- traceQueryTimings tr
withNetworkLayerBase (tr <> trTimings) np conn ver tol action
withNetworkLayerBase
:: HasCallStack
=> Tracer IO NetworkLayerLog
-> W.NetworkParameters
-> CardanoNodeConn
-> (NodeToClientVersionData, CodecCBORTerm Text NodeToClientVersionData)
-> SyncTolerance
-> (NetworkLayer IO (CardanoBlock StandardCrypto) -> IO a)
-> IO a
withNetworkLayerBase tr np conn (versionData, _) tol action = do
-- NOTE: We keep client connections running for accessing the node tip,
-- submitting transactions, querying parameters and delegations/rewards.
--
-- It is safe to retry when the connection is lost here because this client
-- doesn't really do anything but sending messages to get the node's tip. It
-- doesn't rely on the intersection to be up-to-date.
let handlers = retryOnConnectionLost tr
-- FIXME: Would be nice to remove these multiple vars.
-- Not as trivial as it seems, since we'd need to preserve the @debounce@
-- behaviour.
(readNodeTip, networkParamsVar, interpreterVar, eraVar, localTxSubmissionQ)
<- connectNodeTipClient handlers
queryRewardQ <- connectDelegationRewardsClient handlers
rewardsObserver <- newRewardBalanceFetcher tr readNodeTip queryRewardQ
let readCurrentNodeEra = atomically $ readTMVar eraVar
action $ NetworkLayer
{ currentNodeTip =
fromTip getGenesisBlockHash <$> atomically readNodeTip
, currentNodeEra =
-- NOTE: Is not guaranteed to be consistent with @currentNodeTip@
readCurrentNodeEra
, watchNodeTip = do
_watchNodeTip readNodeTip
, nextBlocks =
_nextBlocks
, initCursor =
_initCursor
, destroyCursor =
_destroyCursor
, cursorSlotNo =
_cursorSlotNo
, currentProtocolParameters =
fst <$> atomically (readTMVar networkParamsVar)
, currentSlottingParameters =
snd <$> atomically (readTMVar networkParamsVar)
, postTx = \sealed -> do
era <- liftIO readCurrentNodeEra
_postTx localTxSubmissionQ era sealed
, stakeDistribution =
_stakeDistribution queryRewardQ
, getCachedRewardAccountBalance =
_getCachedAccountBalance rewardsObserver
, fetchRewardAccountBalances =
fetchRewardAccounts tr queryRewardQ
, timeInterpreter =
_timeInterpreter (contramap MsgInterpreterLog tr) interpreterVar
, syncProgress = _syncProgress interpreterVar
}
where
gp@W.GenesisParameters
{ getGenesisBlockHash
, getGenesisBlockDate
} = W.genesisParameters np
sp = W.slottingParameters np
cfg = codecConfig sp
-- Put if empty, replace if not empty.
repsertTMVar var x = do
e <- isEmptyTMVar var
unless e $ void $ takeTMVar var
putTMVar var x
connectNodeTipClient
:: HasCallStack
=> RetryHandlers
-> IO ( STM IO (Tip (CardanoBlock StandardCrypto))
, TMVar IO (W.ProtocolParameters, W.SlottingParameters)
, TMVar IO (CardanoInterpreter StandardCrypto)
, TMVar IO AnyCardanoEra
, TQueue IO (LocalTxSubmissionCmd
(GenTx (CardanoBlock StandardCrypto))
(CardanoApplyTxErr StandardCrypto)
IO)
)
connectNodeTipClient handlers = do
localTxSubmissionQ <- atomically newTQueue
networkParamsVar <- atomically newEmptyTMVar
interpreterVar <- atomically newEmptyTMVar
eraVar <- atomically newEmptyTMVar
(nodeTipClient, readTip) <- mkTipSyncClient tr np
localTxSubmissionQ
(curry (atomically . repsertTMVar networkParamsVar))
(atomically . repsertTMVar interpreterVar)
(atomically . repsertTMVar eraVar)
link =<< async (connectClient tr handlers nodeTipClient versionData conn)
pure (readTip, networkParamsVar, interpreterVar, eraVar, localTxSubmissionQ)
connectDelegationRewardsClient
:: HasCallStack
=> RetryHandlers
-> IO (TQueue IO
(LocalStateQueryCmd (CardanoBlock StandardCrypto) IO))
connectDelegationRewardsClient handlers = do
cmdQ <- atomically newTQueue
let cl = mkDelegationRewardsClient tr cfg cmdQ
link =<< async (connectClient tr handlers cl versionData conn)
pure cmdQ
_initCursor :: HasCallStack => [W.BlockHeader] -> IO Cursor
_initCursor headers = do
chainSyncQ <- atomically newTQueue
client <- mkWalletClient (contramap MsgChainSyncCmd tr) cfg gp chainSyncQ
let handlers = failOnConnectionLost tr
thread <- async (connectClient tr handlers client versionData conn)
link thread
let points = reverse $ genesisPoint :
(toPoint getGenesisBlockHash <$> headers)
let findIt = chainSyncQ `send` CmdFindIntersection points
traceWith tr $ MsgFindIntersection headers
res <- findIt
case res of
Just intersection -> do
traceWith tr
$ MsgIntersectionFound
$ fromChainHash getGenesisBlockHash
$ pointHash intersection
pure $ Cursor thread intersection chainSyncQ
_ -> fail $ unwords
[ "initCursor: intersection not found? This can't happen"
, "because we always give at least the genesis point."
, "Here are the points we gave: " <> show headers
]
_destroyCursor (Cursor thread _ _) = do
liftIO $ traceWith tr $ MsgDestroyCursor (asyncThreadId thread)
cancel thread
_nextBlocks (Cursor thread _ chainSyncQ) = do
let toCursor point = Cursor thread point chainSyncQ
liftIO $ mapCursor toCursor <$> chainSyncQ `send` CmdNextBlocks
_cursorSlotNo (Cursor _ point _) = do
fromWithOrigin (SlotNo 0) $ pointSlot point
-- NOTE1: only shelley transactions can be submitted like this, because they
-- are deserialised as shelley transactions before submitting.
--
-- NOTE2: It is not ideal to query the current era again here because we
-- should in practice use the same era as the one used to construct the
-- transaction. However, when turning transactions to 'SealedTx', we loose
-- all form of type-level indicator about the era. The 'SealedTx' type
-- shouldn't be needed anymore since we've dropped jormungandr, so we could
-- instead carry a transaction from cardano-api types with proper typing.
_postTx localTxSubmissionQ era tx = do
liftIO $ traceWith tr $ MsgPostTx tx
case era of
AnyCardanoEra ByronEra ->
throwE $ ErrPostTxProtocolFailure "Invalid era: Byron"
AnyCardanoEra ShelleyEra -> do
let cmd = CmdSubmitTx $ unsealShelleyTx GenTxShelley tx
result <- liftIO $ localTxSubmissionQ `send` cmd
case result of
SubmitSuccess -> pure ()
SubmitFail err -> throwE $ ErrPostTxBadRequest $ T.pack (show err)
AnyCardanoEra AllegraEra -> do
let cmd = CmdSubmitTx $ unsealShelleyTx GenTxAllegra tx
result <- liftIO $ localTxSubmissionQ `send` cmd
case result of
SubmitSuccess -> pure ()
SubmitFail err -> throwE $ ErrPostTxBadRequest $ T.pack (show err)
AnyCardanoEra MaryEra -> do
let cmd = CmdSubmitTx $ unsealShelleyTx GenTxMary tx
result <- liftIO $ localTxSubmissionQ `send` cmd
case result of
SubmitSuccess -> pure ()
SubmitFail err -> throwE $ ErrPostTxBadRequest $ T.pack (show err)
_stakeDistribution queue coin = do
liftIO $ traceWith tr $ MsgWillQueryRewardsForStake coin
let qry :: LSQ (CardanoBlock StandardCrypto) IO (Maybe W.StakePoolsSummary)
qry = liftA3 (liftA3 W.StakePoolsSummary)
getNOpt
queryNonMyopicMemberRewards
stakeDistr
mres <- bracketQuery "stakePoolsSummary" tr $
queue `send` (SomeLSQ qry )
-- The result will be Nothing if query occurs during the byron era
traceWith tr $ MsgFetchStakePoolsData mres
case mres of
Just res@W.StakePoolsSummary{rewards,stake} -> do
liftIO $ traceWith tr $ MsgFetchStakePoolsDataSummary
(Map.size stake)
(Map.size rewards)
return res
Nothing -> pure $ W.StakePoolsSummary 0 mempty mempty
where
stakeDistr
:: LSQ (CardanoBlock StandardCrypto) IO
(Maybe (Map W.PoolId Percentage))
stakeDistr = shelleyBased
(fromPoolDistr <$> LSQry Shelley.GetStakeDistribution)
getNOpt :: LSQ (CardanoBlock StandardCrypto) IO (Maybe Int)
getNOpt = shelleyBased $
optimumNumberOfPools <$> LSQry Shelley.GetCurrentPParams
queryNonMyopicMemberRewards
:: LSQ (CardanoBlock StandardCrypto) IO
(Maybe (Map W.PoolId W.Coin))
queryNonMyopicMemberRewards = shelleyBased $
(getRewardMap . fromNonMyopicMemberRewards)
<$> LSQry (Shelley.GetNonMyopicMemberRewards stake)
where
stake :: Set (Either SL.Coin a)
stake = Set.singleton $ Left $ toShelleyCoin coin
fromJustRewards = fromMaybe
(error "stakeDistribution: requested rewards not included in response")
getRewardMap
:: Map
(Either W.Coin W.RewardAccount)
(Map W.PoolId W.Coin)
-> Map W.PoolId W.Coin
getRewardMap =
fromJustRewards . Map.lookup (Left coin)
_watchNodeTip readTip cb = do
observeForever readTip $ \tip -> do
let header = fromTip getGenesisBlockHash tip
bracketTracer (contramap (MsgWatcherUpdate header) tr) $ cb header
-- TODO(#2042): Make wallets call manually, with matching
-- stopObserving.
_getCachedRewardAccountBalance rewardsObserver k = do
startObserving rewardsObserver k
fromMaybe (W.Coin 0) <$> query rewardsObserver k
_timeInterpreter
:: HasCallStack
=> Tracer IO TimeInterpreterLog
-> TMVar IO (CardanoInterpreter sc)
-> TimeInterpreter (ExceptT PastHorizonException IO)
_timeInterpreter tr' var = do
let readInterpreter = liftIO $ atomically $ readTMVar var
mkTimeInterpreter tr' getGenesisBlockDate readInterpreter
_syncProgress
:: TMVar IO (CardanoInterpreter sc)
-> SlotNo
-> IO SyncProgress
_syncProgress var slot = do
liftIO (atomically $ tryReadTMVar var) >>= \case
-- If the wallet has been started, but not yet been able to connect
-- to the node, we don't have an interpreter summary, and can't
-- calculate the syncProgress using a @SlotNo@.
--
-- If we want to guarantee the availability of @SyncProgress@, we
-- could consider storing @UTCTime@ along with the follower tip in
-- question, but that would make chain-following dependent on
-- a TimeInterpreter.
Nothing -> return NotResponding
Just i -> do
let ti = mkTimeInterpreter nullTracer getGenesisBlockDate (pure i)
time <- currentRelativeTime ti
let res = SyncProgress.syncProgress tol ti slot time
case runIdentity $ runExceptT res of
-- Getting a past horizon error here should be unlikely, but
-- could happen if we switch from a in-sync node to a
-- not-in-sync node, either by restarting the wallet, or
-- restarting the node using the same socket but different
-- db.
Left _pastHorizon -> return NotResponding
Right p -> return p
--------------------------------------------------------------------------------
--
-- Network Client
-- | Type representing a network client running two mini-protocols to sync
-- from the chain and, submit transactions.
type NetworkClient m = OuroborosApplication
'InitiatorMode
-- Initiator ~ Client (as opposed to Responder / Server)
LocalAddress
-- Address type
ByteString
-- Concrete representation for bytes string
m
-- Underlying monad we run in
Void
-- Return type of a network client. Void indicates that the client
-- never exits.
Void
-- Irrelevant for initiator. Return type of 'ResponderMode' application.
-- | Construct a network client with the given communication channel, for the
-- purposes of syncing blocks to a single wallet.
mkWalletClient
:: (MonadThrow m, MonadST m, MonadTimer m, MonadAsync m)
=> Tracer m (ChainSyncLog Text Text)
-> CodecConfig (CardanoBlock StandardCrypto)
-> W.GenesisParameters
-- ^ Static blockchain parameters
-> TQueue m (ChainSyncCmd (CardanoBlock StandardCrypto) m)
-- ^ Communication channel with the ChainSync client
-> m (NetworkClient m)
mkWalletClient tr cfg gp chainSyncQ = do
stash <- atomically newTQueue
pure $ nodeToClientProtocols (const $ return $ NodeToClientProtocols
{ localChainSyncProtocol =
InitiatorProtocolOnly $ MuxPeerRaw $ \channel ->
runPipelinedPeer nullTracer (cChainSyncCodec $ codecs cfg) channel
$ chainSyncClientPeerPipelined
$ chainSyncWithBlocks tr' (fromTip' gp) chainSyncQ stash
, localTxSubmissionProtocol =
doNothingProtocol
, localStateQueryProtocol =
doNothingProtocol
})
nodeToClientVersion
where
tr' = contramap (mapChainSyncLog showB showP) tr
showB = showP . blockPoint
showP p = case (getPoint p) of
Origin -> "Origin"
At blk -> mconcat
[ "(slotNo "
, T.pack $ show $ unSlotNo $ Point.blockPointSlot blk
, ", "
, pretty $ fromCardanoHash $ Point.blockPointHash blk
, ")"
]
-- | Construct a network client with the given communication channel, for the
-- purposes of querying delegations and rewards.
mkDelegationRewardsClient
:: forall m. (MonadThrow m, MonadST m, MonadTimer m, MonadIO m)
=> Tracer m NetworkLayerLog
-- ^ Base trace for underlying protocols
-> CodecConfig (CardanoBlock StandardCrypto)
-> TQueue m (LocalStateQueryCmd (CardanoBlock StandardCrypto) m)
-- ^ Communication channel with the LocalStateQuery client
-> NetworkClient m
mkDelegationRewardsClient tr cfg queryRewardQ =
nodeToClientProtocols (const $ return $ NodeToClientProtocols
{ localChainSyncProtocol =
doNothingProtocol
, localTxSubmissionProtocol =
doNothingProtocol
, localStateQueryProtocol =
InitiatorProtocolOnly $ MuxPeerRaw
$ \channel -> runPeer tr' codec channel
$ localStateQueryClientPeer
$ localStateQuery queryRewardQ
})
nodeToClientVersion
where
tr' = contramap (MsgLocalStateQuery DelegationRewardsClient) tr
codec = cStateQueryCodec (serialisedCodecs cfg)
{-------------------------------------------------------------------------------
Codecs
-------------------------------------------------------------------------------}
codecVersion :: BlockNodeToClientVersion (CardanoBlock StandardCrypto)
codecVersion = verMap ! nodeToClientVersion
where verMap = supportedNodeToClientVersions (Proxy @(CardanoBlock StandardCrypto))
codecConfig :: W.SlottingParameters -> CodecConfig (CardanoBlock c)
codecConfig sp = CardanoCodecConfig
(byronCodecConfig sp)
ShelleyCodecConfig
ShelleyCodecConfig
ShelleyCodecConfig
-- | A group of codecs which will deserialise block data.
codecs
:: MonadST m
=> CodecConfig (CardanoBlock StandardCrypto)
-> ClientCodecs (CardanoBlock StandardCrypto) m
codecs cfg = clientCodecs cfg codecVersion nodeToClientVersion
-- | A group of codecs which won't deserialise block data. Often only the block
-- headers are needed. It's more efficient and easier not to deserialise.
serialisedCodecs
:: MonadST m
=> CodecConfig (CardanoBlock StandardCrypto)
-> DefaultCodecs (CardanoBlock StandardCrypto) m
serialisedCodecs cfg = defaultCodecs cfg codecVersion nodeToClientVersion
{-------------------------------------------------------------------------------
Tip sync
-------------------------------------------------------------------------------}
type CardanoInterpreter sc = Interpreter (CardanoEras sc)
-- | Construct a network client with the given communication channel, for the
-- purpose of:
--
-- * Submitting transactions
-- * Tracking the node tip
-- * Tracking the latest protocol parameters state.
-- * Querying the history interpreter as necessary.
mkTipSyncClient
:: forall m. (HasCallStack, MonadUnliftIO m, MonadThrow m, MonadST m, MonadTimer m)
=> Tracer m NetworkLayerLog
-- ^ Base trace for underlying protocols
-> W.NetworkParameters
-- ^ Initial blockchain parameters
-> TQueue m
(LocalTxSubmissionCmd
(GenTx (CardanoBlock StandardCrypto))
(CardanoApplyTxErr StandardCrypto)
m)
-- ^ Communication channel with the LocalTxSubmission client
-> (W.ProtocolParameters -> W.SlottingParameters -> m ())
-- ^ Notifier callback for when parameters for tip change.
-> (CardanoInterpreter StandardCrypto -> m ())
-- ^ Notifier callback for when time interpreter is updated.
-> (AnyCardanoEra -> m ())
-- ^ Notifier callback for when the era is updated
-> m (NetworkClient m, STM m (Tip (CardanoBlock StandardCrypto)))
mkTipSyncClient tr np localTxSubmissionQ onPParamsUpdate onInterpreterUpdate onEraUpdate = do
(localStateQueryQ :: TQueue m (LocalStateQueryCmd (CardanoBlock StandardCrypto) m))
<- atomically newTQueue
tipVar <- atomically $ newTVar (Just $ AnyCardanoEra ByronEra, TipGenesis)
(onPParamsUpdate' :: (W.ProtocolParameters, W.SlottingParameters) -> m ()) <-
debounce $ \(pp, sp) -> do
traceWith tr $ MsgProtocolParameters pp sp
onPParamsUpdate pp sp
let queryParams = do
eraBounds <- W.EraInfo
<$> LSQry (QueryAnytimeByron GetEraStart)
<*> LSQry (QueryAnytimeShelley GetEraStart)
<*> LSQry (QueryAnytimeAllegra GetEraStart)
<*> LSQry (QueryAnytimeMary GetEraStart)
sp <- byronOrShelleyBased
(pure $ W.slottingParameters np)
((slottingParametersFromGenesis . getCompactGenesis)
<$> LSQry Shelley.GetGenesisConfig)
pp <- byronOrShelleyBased
(protocolParametersFromUpdateState eraBounds
<$> LSQry Byron.GetUpdateInterfaceState)
(fromShelleyPParams eraBounds
<$> LSQry Shelley.GetCurrentPParams)
return (pp, sp)
let queryInterpreter = LSQry (QueryHardFork GetInterpreter)
let cfg = codecConfig (W.slottingParameters np)
-- NOTE: These are updated every block. This is far more often than
-- necessary.
--
-- By blocking (with `send`) we ensure we don't queue multiple queries.
let onTipUpdate _tip = do
let qry = (,,) <$> queryParams <*> queryInterpreter <*> currentEra
(pparams, int, era) <- localStateQueryQ `send` (SomeLSQ qry)
onPParamsUpdate' pparams
onInterpreterUpdate int
onEraUpdate era
link =<< async (observeForever (readTVar tipVar) onTipUpdate)
let client = nodeToClientProtocols (const $ return $ NodeToClientProtocols
{ localChainSyncProtocol =
let
codec = cChainSyncCodec $ codecs cfg
in
InitiatorProtocolOnly $ MuxPeerRaw
$ \channel -> runPeer nullTracer codec channel
$ chainSyncClientPeer
$ chainSyncFollowTip toCardanoEra (curry (atomically . writeTVar tipVar))
, localTxSubmissionProtocol =
let
tr' = contramap MsgTxSubmission tr
codec = cTxSubmissionCodec $ serialisedCodecs cfg
in
InitiatorProtocolOnly $ MuxPeerRaw
$ \channel -> runPeer tr' codec channel
$ localTxSubmissionClientPeer
$ localTxSubmission localTxSubmissionQ
, localStateQueryProtocol =
let
tr' = contramap (MsgLocalStateQuery TipSyncClient) tr
codec = cStateQueryCodec $ serialisedCodecs cfg
in
InitiatorProtocolOnly $ MuxPeerRaw
$ \channel -> runPeer tr' codec channel
$ localStateQueryClientPeer
$ localStateQuery localStateQueryQ
})
nodeToClientVersion
return (client, snd <$> readTVar tipVar)
-- FIXME: We can remove the era from the tip sync client now.
-- Reward Account Balances
-- | Monitors values for keys, and allows clients to @query@ them.
--
-- Designed to be used for observing reward balances, where we want to cache the
-- balances of /all/ the wallets' accounts on tip change, and allow wallet
-- workers to @query@ the cache later, often, and whenever they want.
--
-- NOTE: One could imagine replacing @query@ getter with a push-based approach.
data Observer m key value = Observer
{ startObserving :: key -> m ()
, stopObserving :: key -> m ()
, query :: key -> m (Maybe value)
}
newRewardBalanceFetcher
:: Tracer IO NetworkLayerLog
-- ^ Used to convert tips for logging
-> STM IO (Tip (CardanoBlock StandardCrypto))
-- ^ STM action for observing the current tip
-> TQueue IO (LocalStateQueryCmd (CardanoBlock StandardCrypto) IO)
-> IO (Observer IO W.RewardAccount W.Coin)
newRewardBalanceFetcher tr readNodeTip queryRewardQ = do
(ob, refresh) <- newObserver (contramap MsgObserverLog tr) fetch
link =<< async (observeForever readNodeTip refresh)
return ob
where
fetch
:: Tip (CardanoBlock StandardCrypto)
-> Set W.RewardAccount
-> IO (Maybe (Map W.RewardAccount W.Coin))
fetch _tip accounts | Set.null accounts = pure (Just mempty)
fetch _tip accounts = do
-- NOTE: We no longer need the tip to run LSQ queries. The local state
-- query client will automatically acquire the latest tip.
Just <$> fetchRewardAccounts tr queryRewardQ accounts
fetchRewardAccounts
:: Tracer IO NetworkLayerLog
-> TQueue IO (LocalStateQueryCmd (CardanoBlock StandardCrypto) IO)
-> Set W.RewardAccount
-> IO (Map W.RewardAccount W.Coin)
fetchRewardAccounts tr queryRewardQ accounts = do
liftIO $ traceWith tr $
MsgFetchRewardAccountBalance accounts
let qry = byronOrShelleyBased (pure (byronValue, [])) $
fmap fromBalanceResult
. LSQry
. Shelley.GetFilteredDelegationsAndRewardAccounts
$ Set.map toStakeCredential accounts
(res,logs) <- bracketQuery "queryRewards" tr (send queryRewardQ (SomeLSQ qry))
liftIO $ mapM_ (traceWith tr) logs
return res
where
byronValue :: Map W.RewardAccount W.Coin
byronValue = Map.fromList . map (, minBound) $ Set.toList accounts
fromBalanceResult
:: ( Map (SL.Credential 'SL.Staking crypto)
(SL.KeyHash 'SL.StakePool crypto)
, SL.RewardAccounts crypto
)
-> (Map W.RewardAccount W.Coin, [NetworkLayerLog])
fromBalanceResult (deleg, rewardAccounts) =
( Map.mapKeys fromStakeCredential $
Map.map fromShelleyCoin rewardAccounts
, [MsgAccountDelegationAndRewards deleg rewardAccounts]
)
data ObserverLog key value
= MsgWillFetch (Set key)
| MsgDidFetch (Map key value)
| MsgDidChange (Map key value)
| MsgAddedObserver key
| MsgRemovedObserver key
deriving (Eq, Show)
instance (Ord key, Buildable key, Buildable value)
=> ToText (ObserverLog key value) where
toText (MsgWillFetch keys) = mconcat
[ "Will fetch values for keys "
, fmt $ listF keys
]
toText (MsgDidFetch m) = mconcat
[ "Did fetch values "
, fmt $ mapF m
]
toText (MsgDidChange m) = mconcat
[ "New values: "
, fmt $ mapF m
]
toText (MsgAddedObserver key) = mconcat
[ "Started observing values for key "
, pretty key
]
toText (MsgRemovedObserver key) = mconcat
[ "Stopped observing values for key "
, pretty key
]
-- | Given a way to fetch values for a set of keys, create:
-- 1. An @Observer@ for consuming values
-- 2. A refresh action
--
-- The @env@ parameter can be used to pass in information needed for refreshing,
-- like the current tip when fetching rewards.
--
-- If the given @fetch@ function returns @Nothing@ the the cache will not be
-- updated.
--
-- If it returns @Just values@, the cache will be set to @values@.
newObserver
:: forall m key value env. (MonadSTM m, Ord key, Eq value)
=> Tracer m (ObserverLog key value)
-> (env -> Set key -> m (Maybe (Map key value)))
-> m (Observer m key value, env -> m ())
newObserver tr fetch = do
cacheVar <- atomically $ newTVar Map.empty
toBeObservedVar <- atomically $ newTVar Set.empty
return (observer cacheVar toBeObservedVar, refresh cacheVar toBeObservedVar)
where
observer
:: TVar m (Map key value)
-> TVar m (Set key)
-> Observer m key value
observer cacheVar observedKeysVar =
Observer
{ startObserving = \k -> do
wasAdded <- atomically $ do
notAlreadyThere <- Set.notMember k <$> readTVar observedKeysVar
modifyTVar' observedKeysVar (Set.insert k)
return notAlreadyThere
when wasAdded $ traceWith tr $ MsgAddedObserver k
, stopObserving = \k -> do
atomically $ do
modifyTVar' observedKeysVar (Set.delete k)
modifyTVar' cacheVar (Map.delete k)
traceWith tr $ MsgRemovedObserver k
, query = \k -> do
m <- atomically (readTVar cacheVar)
return $ Map.lookup k m
}
refresh
:: TVar m (Map key value)
-> TVar m (Set key)
-> env
-> m ()
refresh cacheVar observedKeysVar env = do
keys <- atomically $ readTVar observedKeysVar
oldValues <- atomically $ readTVar cacheVar
traceWith tr $ MsgWillFetch keys
mvalues <- fetch env keys
case mvalues of
Nothing -> pure ()
Just values -> do
traceWith tr $ MsgDidFetch values
when (oldValues /= values) $
traceWith tr $ MsgDidChange values
atomically $ writeTVar cacheVar values
-- | Return a function to run an action only if its single parameter has changed
-- since the previous time it was called.
debounce :: (Eq a, MonadSTM m) => (a -> m ()) -> m (a -> m ())
debounce action = do
mvar <- newTMVarIO Nothing
pure $ \cur -> do
prev <- atomically $ takeTMVar mvar
unless (Just cur == prev) $ action cur
atomically $ putTMVar mvar (Just cur)
-- | Convenience function to trace around a local state query.
-- See 'addTimings'.