This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
forked from froozen/kademlia
/
Config.hs
66 lines (56 loc) · 2.64 KB
/
Config.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
module Network.Kademlia.Config
( KademliaConfig(..)
, WithConfigT (..)
, WithConfig
, getConfig
, usingConfigT
, usingConfig
, defaultConfig
, defaultRoutingSharingN
, defaultK
) where
import Control.Monad.Identity (Identity (runIdentity))
import Control.Monad.Reader (ReaderT (..), ask)
import Control.Monad.Trans (MonadTrans)
import Network.Kademlia.Utils (hour, minute)
data KademliaConfig = KademliaConfig {
k :: !Int -- ^ @k@ nearest heighbours for query. Constant from paper.
, expirationTime :: !Int -- ^ we delete a value after @expirationTime@ seconds has passed
, storeValueTime :: !Int -- ^ we store all values stored in the node in the 'k' closest known nodes every @storeValueTime@ seconds
, pingTime :: !Int -- ^ we ping all known nodes every @pingTime@ seconds to make sure they are still present
, nbLookupNodes :: !Int -- ^ number of nodes to look in parallel during a lookup
-- also known as α in kademlia paper
, msgSizeLimit :: !Int -- ^ upper bound on size of message transfered through
-- network; exceeding messages would be splitted
, storeValues :: !Bool -- ^ when this is False, we don't store anything in this node
, routingSharingN :: !Int -- ^ number of nodes from not closest to include int `returnNodes` responses (see [CSL-260])
, cacheSize :: !Int -- ^ cache size used by Node Storage Tree
, pingLimit :: !Int -- ^ after unsuccessfull @pingLimit@ pings node will be throwed out from bucket
}
newtype WithConfigT m a = WithConfigT
{ getWithConfigT :: ReaderT KademliaConfig m a
} deriving (Functor, Applicative, Monad, MonadTrans)
type WithConfig a = WithConfigT Identity a
getConfig :: Monad m => WithConfigT m KademliaConfig
getConfig = WithConfigT ask
usingConfigT :: WithConfigT m a -> KademliaConfig -> m a
usingConfigT f cfg = flip runReaderT cfg $ getWithConfigT f
usingConfig :: WithConfig a -> KademliaConfig -> a
usingConfig f cfg = runIdentity $ usingConfigT f cfg
defaultK :: Int
defaultK = 7
defaultRoutingSharingN :: Int
defaultRoutingSharingN = uncurry (+) $ defaultK `divMod` 2
defaultConfig :: KademliaConfig
defaultConfig = KademliaConfig
{ k = defaultK
, expirationTime = hour 1
, storeValueTime = hour 1
, pingTime = minute 5
, nbLookupNodes = 3
, msgSizeLimit = 1200
, storeValues = True
, routingSharingN = defaultRoutingSharingN
, cacheSize = 5
, pingLimit = 4
}