summaryrefslogtreecommitdiff
path: root/SybilLimit.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-08-10 18:56:41 -0400
committerjoe <joe@jerkface.net>2014-08-10 18:56:41 -0400
commit251204cbf876106dcc67bd2c2ca9bd11450f63fe (patch)
treeb77686b641fde052b687c78b3db53aa90562927c /SybilLimit.hs
parent291b5b43374cf956fdd6a94639aac7969adae7f7 (diff)
initiateSybilCheck
Diffstat (limited to 'SybilLimit.hs')
-rw-r--r--SybilLimit.hs31
1 files changed, 28 insertions, 3 deletions
diff --git a/SybilLimit.hs b/SybilLimit.hs
index 903ccd0..8d053f9 100644
--- a/SybilLimit.hs
+++ b/SybilLimit.hs
@@ -1,9 +1,10 @@
1{-# LANGUAGE StandaloneDeriving #-} 1{-# LANGUAGE StandaloneDeriving, TupleSections #-}
2module SybilLimit where 2module SybilLimit where
3 3
4import Data.List ( foldl', minimumBy ) 4import Data.List ( foldl', minimumBy )
5import Data.Maybe ( fromMaybe ) 5import Data.Maybe ( fromMaybe )
6import Data.Ord ( comparing ) 6import Data.Ord ( comparing )
7import Data.Tuple ( swap )
7import Data.IntMap.Strict ( IntMap, (!) ) 8import Data.IntMap.Strict ( IntMap, (!) )
8import qualified Data.IntMap.Strict as IntMap 9import qualified Data.IntMap.Strict as IntMap
9import Data.Traversable ( sequenceA ) 10import Data.Traversable ( sequenceA )
@@ -47,8 +48,7 @@ data FriendNode = FriendNode
47-- | When 'sybPendingTails' is empty, the intersection condition is passed if and 48-- | When 'sybPendingTails' is empty, the intersection condition is passed if and
48-- only if 'sybVerifiedTails' is not empty. 49-- only if 'sybVerifiedTails' is not empty.
49data PendingSybilCheck = PendingSybilCheck 50data PendingSybilCheck = PendingSybilCheck
50 { sybSuspect :: NodeId -- todo: neccessary? 51 { sybPendingTails :: Map (NodeId,NodeId) [Int]
51 , sybPendingTails :: Map (NodeId,NodeId) [Int]
52 , sybVerifiedTails :: Map (NodeId,NodeId) [Int] 52 , sybVerifiedTails :: Map (NodeId,NodeId) [Int]
53 } 53 }
54 54
@@ -76,6 +76,11 @@ data MessageReaction = MessageReaction
76 , sybilChecks :: [SybilCheck] 76 , sybilChecks :: [SybilCheck]
77 } 77 }
78 78
79data SuspectCredentials
80 = SuspectCredentials { suspectId :: NodeId
81 , suspectTails :: [(NodeId,NodeId)]
82 }
83
79friendNode :: NodeId -> FriendNode 84friendNode :: NodeId -> FriendNode
80friendNode nid = FriendNode nid IntMap.empty IntMap.empty Nothing 85friendNode nid = FriendNode nid IntMap.empty IntMap.empty Nothing
81 86
@@ -223,3 +228,23 @@ balanceCondition (ka,kb) routeNums me = (me',didPass)
223 where 228 where
224 tieBreak EQ = compare ca cb 229 tieBreak EQ = compare ca cb
225 tieBreak x = x 230 tieBreak x = x
231
232initiateSybilCheck :: SuspectCredentials -> ThisNode -> MessageReaction
233initiateSybilCheck cred me = MessageReaction me' msgs []
234 where
235 me' = me { pendingChecks = Map.insert (suspectId cred) p
236 $ pendingChecks me }
237 msgs = map (\(a,b) -> (b, RegistrationQuery (suspectId cred) a))
238 $ Map.keys tmap
239 p = PendingSybilCheck { sybPendingTails = tmap
240 , sybVerifiedTails = Map.empty
241 }
242 tmap = tmap0 `Map.intersection` smap
243 where
244 smap = Map.fromList $ map (,()) (suspectTails cred)
245 tmap0 = foldl' build
246 Map.empty
247 (map swap $ IntMap.toList $ routeTails me)
248 build mp (tl,i) = Map.alter insert tl mp
249 where insert Nothing = Just [i]
250 insert (Just is) = Just (i:is)