summaryrefslogtreecommitdiff
path: root/Mainline.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-27 01:19:55 -0400
committerjoe <joe@jerkface.net>2017-07-27 01:19:55 -0400
commit51f8d96fe88323753cbbfa455fb41165628f7794 (patch)
tree820406bb9d38aadb33eb59634cc818d9e56e1b3b /Mainline.hs
parent0e20eb6683761362ee282e3188fccdab46b02ee4 (diff)
Mainline queries: implementation (TODO: interface).
Diffstat (limited to 'Mainline.hs')
-rw-r--r--Mainline.hs23
1 files changed, 15 insertions, 8 deletions
diff --git a/Mainline.hs b/Mainline.hs
index 7c54a096..921bea0f 100644
--- a/Mainline.hs
+++ b/Mainline.hs
@@ -92,6 +92,13 @@ instance S.Serialize NodeId where
92instance FiniteBits NodeId where 92instance FiniteBits NodeId where
93 finiteBitSize _ = 160 93 finiteBitSize _ = 160
94 94
95instance Read NodeId where
96 readsPrec _ str
97 | (bs, xs) <- Base16.decode $ Char8.pack str
98 , B.length bs == 20
99 = [ (NodeId bs, drop 40 str) ]
100 | otherwise = []
101
95zeroID :: NodeId 102zeroID :: NodeId
96zeroID = NodeId $ B.replicate 20 0 103zeroID = NodeId $ B.replicate 20 0
97 104
@@ -465,7 +472,7 @@ traced (TableMethods ins del lkup)
465 472
466type MainlineClient = Client String Method TransactionId NodeInfo (Message BValue) 473type MainlineClient = Client String Method TransactionId NodeInfo (Message BValue)
467 474
468newClient :: SockAddr -> IO (MainlineClient, Routing) 475newClient :: SockAddr -> IO (MainlineClient, Routing, SwarmsDatabase)
469newClient addr = do 476newClient addr = do
470 udp <- udpTransport addr 477 udp <- udpTransport addr
471 nid <- NodeId <$> getRandomBytes 20 478 nid <- NodeId <$> getRandomBytes 20
@@ -564,7 +571,7 @@ newClient addr = do
564 (sched6 routing) 571 (sched6 routing)
565 (refreshBucket (nodeSearch client) (routing6 routing) (nodeId tenative_info)) 572 (refreshBucket (nodeSearch client) (routing6 routing) (nodeId tenative_info))
566 573
567 return (client, routing) 574 return (client, routing, swarms)
568 575
569-- | Modifies a purely random 'NodeId' to one that is related to a given 576-- | Modifies a purely random 'NodeId' to one that is related to a given
570-- routable address in accordance with BEP 42. 577-- routable address in accordance with BEP 42.
@@ -969,14 +976,14 @@ ping client addr =
969 976
970-- searchQuery :: ni -> IO (Maybe [ni], [r], tok)) 977-- searchQuery :: ni -> IO (Maybe [ni], [r], tok))
971getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],())) 978getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],()))
972getNodes = mainlineSend (Method "find_node") unwrap $ flip FindNode (Just Want_Both) 979getNodes = mainlineSend (Method "find_node") unwrapNodes $ flip FindNode (Just Want_Both)
973 where 980
974 unwrap (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6,()) 981unwrapNodes (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6,())
975 982
976getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Token)) 983getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Token))
977getPeers = mainlineSend (Method "get_peers") unwrap $ flip GetPeers (Just Want_Both) . coerce 984getPeers = mainlineSend (Method "get_peers") unwrapPeers $ flip GetPeers (Just Want_Both) . coerce
978 where 985
979 unwrap (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, tok) 986unwrapPeers (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, tok)
980 987
981data TriadSlot = SlotA | SlotB | SlotC 988data TriadSlot = SlotA | SlotB | SlotC
982 deriving (Eq,Ord,Enum,Show,Read) 989 deriving (Eq,Ord,Enum,Show,Read)