-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonLeaky.hs
More file actions
80 lines (72 loc) · 2.73 KB
/
NonLeaky.hs
File metadata and controls
80 lines (72 loc) · 2.73 KB
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
{-# LANGUAGE RankNTypes #-}
module Main (main) where
import Cardano.Api
import Control.Concurrent
import Control.Monad
import Data.String
import Data.Word
import Ouroboros.Network.Protocol.LocalTxMonitor.Client
import System.Environment
main :: IO ()
main = do
[networkIdStr, socketPath] <- getArgs
putStrLn "Running NonLeaky LocalTxMonitoringMempoolInformation"
-- With this approach the application memory consumption tops out at ~131MiB on local machine
-- according to the Ubuntu 22.04 System Monitor
client <- mkMempoolClient $ connectInfo (read networkIdStr) socketPath
forever $ do
mpc <- getMempoolSizeAndCapacity client
print $ "NonLeaky LocalTxMonitoringMempoolInformation: " ++ show mpc
where
connectInfo ::
Word32 ->
String ->
LocalNodeConnectInfo CardanoMode
connectInfo nwId socketPath =
LocalNodeConnectInfo
{ localConsensusModeParams = CardanoModeParams (EpochSlots 21600)
, localNodeNetworkId = Testnet $ NetworkMagic nwId
, localNodeSocketPath = fromString socketPath
}
data MempoolClient = MempoolClient
{ mpcRequestVar :: !(MVar ())
, mpcResultVar :: !(MVar MempoolSizeAndCapacity)
}
getMempoolSizeAndCapacity ::
MempoolClient ->
IO MempoolSizeAndCapacity
getMempoolSizeAndCapacity mpc = do
putMVar (mpcRequestVar mpc) ()
takeMVar (mpcResultVar mpc)
mkMempoolClient ::
LocalNodeConnectInfo CardanoMode ->
IO MempoolClient
mkMempoolClient nodeConn = do
requestVar <- newEmptyMVar
resultVar <- newEmptyMVar
let prot =
LocalNodeClientProtocols
{ localChainSyncClient = NoLocalChainSyncClient
, localTxSubmissionClient = Nothing
, localStateQueryClient = Nothing
, localTxMonitoringClient = Just $ LocalTxMonitorClient $ pure (clientIdle requestVar resultVar)
}
void $ forkIO $ connectToLocalNode nodeConn prot
pure $ MempoolClient requestVar resultVar
where
clientIdle ::
MVar () ->
MVar MempoolSizeAndCapacity ->
Ouroboros.Network.Protocol.LocalTxMonitor.Client.ClientStIdle txid (TxInMode era) SlotNo IO ()
clientIdle requestVar resultVar = do
SendMsgAcquire $ \slotNo -> do
void $ takeMVar requestVar
pure $ localTxMonitorMempoolInfo requestVar resultVar slotNo
localTxMonitorMempoolInfo ::
MVar () ->
MVar MempoolSizeAndCapacity ->
SlotNo ->
ClientStAcquired txid (TxInMode era) SlotNo IO ()
localTxMonitorMempoolInfo requestVar resultVar _slotNo = SendMsgGetSizes $ \mpc -> do
putMVar resultVar mpc
pure $ SendMsgRelease $ pure (clientIdle requestVar resultVar)