diff options
author | joe <joe@jerkface.net> | 2014-08-10 18:56:41 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-08-10 18:56:41 -0400 |
commit | 251204cbf876106dcc67bd2c2ca9bd11450f63fe (patch) | |
tree | b77686b641fde052b687c78b3db53aa90562927c | |
parent | 291b5b43374cf956fdd6a94639aac7969adae7f7 (diff) |
initiateSybilCheck
-rw-r--r-- | SybilLimit.hs | 31 |
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 #-} |
2 | module SybilLimit where | 2 | module SybilLimit where |
3 | 3 | ||
4 | import Data.List ( foldl', minimumBy ) | 4 | import Data.List ( foldl', minimumBy ) |
5 | import Data.Maybe ( fromMaybe ) | 5 | import Data.Maybe ( fromMaybe ) |
6 | import Data.Ord ( comparing ) | 6 | import Data.Ord ( comparing ) |
7 | import Data.Tuple ( swap ) | ||
7 | import Data.IntMap.Strict ( IntMap, (!) ) | 8 | import Data.IntMap.Strict ( IntMap, (!) ) |
8 | import qualified Data.IntMap.Strict as IntMap | 9 | import qualified Data.IntMap.Strict as IntMap |
9 | import Data.Traversable ( sequenceA ) | 10 | import 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. |
49 | data PendingSybilCheck = PendingSybilCheck | 50 | data 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 | ||
79 | data SuspectCredentials | ||
80 | = SuspectCredentials { suspectId :: NodeId | ||
81 | , suspectTails :: [(NodeId,NodeId)] | ||
82 | } | ||
83 | |||
79 | friendNode :: NodeId -> FriendNode | 84 | friendNode :: NodeId -> FriendNode |
80 | friendNode nid = FriendNode nid IntMap.empty IntMap.empty Nothing | 85 | friendNode 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 | |||
232 | initiateSybilCheck :: SuspectCredentials -> ThisNode -> MessageReaction | ||
233 | initiateSybilCheck 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) | ||