summaryrefslogtreecommitdiff
path: root/src/Data/Kademlia
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Kademlia')
-rw-r--r--src/Data/Kademlia/Common.hs48
-rw-r--r--src/Data/Kademlia/Routing/Bucket.hs139
-rw-r--r--src/Data/Kademlia/Routing/Table.hs38
-rw-r--r--src/Data/Kademlia/Routing/Tree.hs56
4 files changed, 281 insertions, 0 deletions
diff --git a/src/Data/Kademlia/Common.hs b/src/Data/Kademlia/Common.hs
new file mode 100644
index 00000000..874120d8
--- /dev/null
+++ b/src/Data/Kademlia/Common.hs
@@ -0,0 +1,48 @@
1{-# OPTIONS -fno-warn-orphans #-}
2{-# LANGUAGE RecordWildCards #-}
3module Data.Kademlia.Common
4 (NodeID, NodeInfo
5 ) where
6
7import Control.Applicative
8import Data.ByteString
9import Network
10import Network.Socket
11import Data.Serialize
12
13
14type NodeID = ByteString
15type Distance = NodeID
16
17-- WARN is the 'system' random suitable for this?
18-- | Generate random NodeID used for the entire session.
19-- Distribution of ID's should be as uniform as possible.
20--
21genNodeID :: IO NodeID
22genNodeID = undefined -- randomIO
23
24instance Serialize PortNumber where
25 get = fromIntegral <$> getWord16be
26 put = putWord16be . fromIntegral
27
28
29data NodeAddr = NodeAddr {
30 nodeIP :: HostAddress
31 , nodePort :: PortNumber
32 } deriving (Show, Eq)
33
34instance Serialize NodeAddr where
35 get = NodeAddr <$> getWord32be <*> get
36 put NodeAddr {..} = do
37 putWord32be nodeIP
38 put nodePort
39
40
41data NodeInfo = NodeInfo {
42 nodeID :: NodeID
43 , nodeAddr :: NodeAddr
44 } deriving (Show, Eq)
45
46instance Serialize NodeInfo where
47 get = NodeInfo <$> getByteString 20 <*> get
48 put NodeInfo {..} = put nodeID >> put nodeAddr
diff --git a/src/Data/Kademlia/Routing/Bucket.hs b/src/Data/Kademlia/Routing/Bucket.hs
new file mode 100644
index 00000000..8d7f3e50
--- /dev/null
+++ b/src/Data/Kademlia/Routing/Bucket.hs
@@ -0,0 +1,139 @@
1-- |
2-- Copyright : (c) Sam T. 2013
3-- License : MIT
4-- Maintainer : pxqr.sta@gmail.com
5-- Stability : experimental
6-- Portability : portable
7--
8-- Bucket is used to
9--
10-- Bucket is kept sorted by time last seen — least-recently seen
11-- node at the head, most-recently seen at the tail. Reason: when we
12-- insert a node into the bucket we first filter nodes with smaller
13-- lifetime since they more likely leave network and we more likely
14-- don't reach list end. This should reduce list traversal, we don't
15-- need to reverse list in insertion routines.
16--
17-- Bucket is also limited in its length — thus it's called k-bucket.
18-- When bucket becomes full we should split it in two lists by
19-- current span bit. Span bit is defined by depth in the routing
20-- table tree. Size of the bucket should be choosen such that it's
21-- very unlikely that all nodes in bucket fail within an hour of
22-- each other.
23--
24{-# LANGUAGE RecordWildCards #-}
25module Data.Kademlia.Routing.Bucket
26 ( Bucket(maxSize, kvs)
27
28 -- * Query
29 , size, isFull, member
30
31 -- * Construction
32 , empty, singleton
33
34 -- * Modification
35 , enlarge, split, insert
36
37 -- * Defaults
38 , defaultBucketSize
39 ) where
40
41import Control.Applicative hiding (empty)
42import Data.Bits
43import Data.List as L hiding (insert)
44
45
46type Size = Int
47
48data Bucket k v = Bucket {
49 -- | We usually use equally sized buckets in the all routing table
50 -- so keeping max size in each bucket lead to redundancy. Altrough
51 -- it allow us to use some interesting schemes in route tree.
52 maxSize :: Size
53
54 -- | Key -> value pairs as described above.
55 -- Each key in a given bucket should be unique.
56 , kvs :: [(k, v)]
57 }
58
59-- | Gives /current/ size of bucket.
60--
61-- forall bucket. size bucket <= maxSize bucket
62--
63size :: Bucket k v -> Size
64size = L.length . kvs
65
66isFull :: Bucket k v -> Bool
67isFull Bucket {..} = L.length kvs == maxSize
68
69member :: Eq k => k -> Bucket k v -> Bool
70member k = elem k . map fst . kvs
71
72empty :: Size -> Bucket k v
73empty s = Bucket (max 0 s) []
74
75singleton :: Size -> k -> v -> Bucket k v
76singleton s k v = Bucket (max 1 s) [(k, v)]
77
78
79-- | Increase size of a given bucket.
80enlarge :: Size -> Bucket k v -> Bucket k v
81enlarge additional b = b { maxSize = maxSize b + additional }
82
83split :: Bits k => Int -> Bucket k v -> (Bucket k v, Bucket k v)
84split index Bucket {..} =
85 let (far, near) = partition spanBit kvs
86 in (Bucket maxSize near, Bucket maxSize far)
87 where
88 spanBit = (`testBit` index) . fst
89
90
91-- move elem to the end in one traversal
92moveToEnd :: Eq k => (k, v) -> Bucket k v -> Bucket k v
93moveToEnd kv@(k, _) b = b { kvs = go (kvs b) }
94 where
95 go [] = []
96 go (x : xs)
97 | fst x == k = xs ++ [kv]
98 | otherwise = x : go xs
99
100insertToEnd :: (k, v) -> Bucket k v -> Bucket k v
101insertToEnd kv b = b { kvs = kvs b ++ [kv] }
102
103-- | * If the info already exists in bucket then move it to the end.
104--
105-- * If bucket is not full then insert the info to the end.
106--
107-- * If bucket is full then ping the least recently seen node.
108-- Here we have a choice:
109--
110-- If node respond then move it the end and discard node
111-- we want to insert.
112--
113-- If not remove it from the bucket and add the
114-- (we want to insert) node to the end.
115--
116insert :: Applicative f => Eq k
117 => (v -> f Bool) -- ^ Ping RPC
118 -> (k, v) -> Bucket k v -> f (Bucket k v)
119
120insert ping new bucket@(Bucket {..})
121 | fst new `member` bucket = pure (new `moveToEnd` bucket)
122 | size bucket < maxSize = pure (new `insertToEnd` bucket)
123 | least : rest <- kvs =
124 let select alive = if alive then least else new
125 mk most = Bucket maxSize (rest ++ [most])
126 in mk . select <$> ping (snd least)
127 where
128-- | otherwise = pure bucket
129 -- WARN: or maybe error "insertBucket: max size should not be 0" ?
130
131lookup :: k -> Bucket k v -> Maybe v
132lookup = undefined
133
134closest :: Int -> k -> Bucket k v -> [(k, v)]
135closest = undefined
136
137-- | Most clients use this value for maximum bucket size.
138defaultBucketSize :: Int
139defaultBucketSize = 20
diff --git a/src/Data/Kademlia/Routing/Table.hs b/src/Data/Kademlia/Routing/Table.hs
new file mode 100644
index 00000000..b79a0a31
--- /dev/null
+++ b/src/Data/Kademlia/Routing/Table.hs
@@ -0,0 +1,38 @@
1-- |
2-- Copyright : (c) Sam T. 2013
3-- License : MIT
4-- Maintainer : pxqr.sta@gmail.com
5-- Stability : experimental
6-- Portability : portable
7--
8-- Routing table used to lookup . Internally it uses not balanced tree
9--
10-- TODO write module synopsis
11module Data.Kademlia.Routing.Table
12 ( Table(nodeID)
13 ) where
14
15import Control.Applicative
16import Data.List as L
17import Data.Maybe
18
19import Data.Kademlia.Routing.Tree
20
21
22data Table k v = Table {
23 routeTree :: Tree k v
24
25 -- | Set degree of parallelism in node lookup calls.
26 , alpha :: Int
27 , nodeID :: k
28 }
29
30--insert :: NodeID -> Table -> Table
31--insert x t = undefined
32
33--closest :: InfoHash -> Table -> [NodeID]
34--closest = undefined
35
36
37-- TODO table serialization: usually we need to save table between
38-- target program executions for bootstrapping
diff --git a/src/Data/Kademlia/Routing/Tree.hs b/src/Data/Kademlia/Routing/Tree.hs
new file mode 100644
index 00000000..522bb0c2
--- /dev/null
+++ b/src/Data/Kademlia/Routing/Tree.hs
@@ -0,0 +1,56 @@
1-- |
2-- Copyright : (c) Sam T. 2013
3-- License : MIT
4-- Maintainer : pxqr.sta@gmail.com
5-- Stability : experimental
6-- Portability : portable
7--
8-- Routing tree should contain key -> value pairs in this way:
9--
10-- * More keys that near to our node key, and less keys that far
11-- from our node key.
12--
13-- * Tree might be saturated. If this happen we can only update
14-- buckets, but we can't add new buckets.
15--
16-- Instead of using ordinary binary tree and keep track is it
17-- following restrictions above (that's somewhat non-trivial) we
18-- store distance -> value keys. This lead to simple data structure
19-- that actually isomorphic to non-empty list. So we first map our
20-- keys to distances using our node ID and store them in tree. When
21-- we need to extract a pair we map distances to keys back, again
22-- using our node ID. This normalization happen in routing table.
23--
24module Data.Kademlia.Routing.Tree
25 ( Tree, empty, insert
26 ) where
27
28import Control.Applicative hiding (empty)
29import Data.Bits
30
31import Data.Kademlia.Routing.Bucket (Bucket, split, isFull)
32import qualified Data.Kademlia.Routing.Bucket as Bucket
33
34
35
36data Tree k v
37 = Tip (Bucket k v)
38 | Bin (Tree k v) (Bucket k v)
39
40empty :: Int -> Tree k v
41empty = Tip . Bucket.empty
42
43insert :: Applicative f
44 => Bits k
45 => (v -> f Bool)
46 -> (k, v) -> Tree k v -> f (Tree k v)
47insert ping (k, v) = go 0
48 where
49 go n (Tip bucket)
50 | isFull bucket, (near, far) <- split n bucket
51 = pure (Tip near `Bin` far)
52 | otherwise = Tip <$> Bucket.insert ping (k, v) bucket
53
54 go n (Bin near far)
55 | k `testBit` n = Bin <$> pure near <*> Bucket.insert ping (k, v) far
56 | otherwise = Bin <$> go (succ n) near <*> pure far