diff options
-rw-r--r-- | OnionRouter.hs | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/OnionRouter.hs b/OnionRouter.hs index f1b0b41c..ad6323fe 100644 --- a/OnionRouter.hs +++ b/OnionRouter.hs | |||
@@ -17,6 +17,8 @@ import Control.Monad | |||
17 | import Crypto.Random | 17 | import Crypto.Random |
18 | import Data.Array.MArray | 18 | import Data.Array.MArray |
19 | import Data.Bits | 19 | import Data.Bits |
20 | import Data.Bool | ||
21 | import Data.List | ||
20 | import qualified Data.ByteString as B | 22 | import qualified Data.ByteString as B |
21 | import qualified Data.HashMap.Strict as HashMap | 23 | import qualified Data.HashMap.Strict as HashMap |
22 | ;import Data.HashMap.Strict (HashMap) | 24 | ;import Data.HashMap.Strict (HashMap) |
@@ -251,6 +253,20 @@ selectTrampolines or = do | |||
251 | myThreadId >>= flip labelThread ("OnionRouter") | 253 | myThreadId >>= flip labelThread ("OnionRouter") |
252 | return ns | 254 | return ns |
253 | 255 | ||
256 | choose3 :: (Integral a, DRG drg) => drg -> a -> ([a], drg) | ||
257 | choose3 drg0 cnt = ([a,b,c], drg) | ||
258 | where | ||
259 | (a, drg1) = randomR (0,cnt - 1) drg0 | ||
260 | (b0, drg2) = randomR (0,cnt - 2) drg1 | ||
261 | (c0, drg ) = randomR (0,cnt - 3) drg2 | ||
262 | b | b0 < a = b0 | ||
263 | | otherwise = b0 + 1 | ||
264 | [ac,bc] = sort [a,b] | ||
265 | c1 | c0 < ac = c0 | ||
266 | | otherwise = c0 + 1 | ||
267 | c | c1 < bc = c1 | ||
268 | | otherwise = c1 + 1 | ||
269 | |||
254 | -- Select 3 indices into the trampolineNodes set and returns the associated | 270 | -- Select 3 indices into the trampolineNodes set and returns the associated |
255 | -- nodes provided they are suitable for use in an onion route. Otherwise, it | 271 | -- nodes provided they are suitable for use in an onion route. Otherwise, it |
256 | -- returns Left with the nodes that were selected. | 272 | -- returns Left with the nodes that were selected. |
@@ -262,15 +278,7 @@ selectTrampolines' or = do | |||
262 | cnt <- readTVar (trampolineCount or) | 278 | cnt <- readTVar (trampolineCount or) |
263 | ts <- readTVar (trampolineNodes or) | 279 | ts <- readTVar (trampolineNodes or) |
264 | drg0 <- readTVar (onionDRG or) | 280 | drg0 <- readTVar (onionDRG or) |
265 | let (a, drg1) = randomR (0,cnt - 1) drg0 | 281 | let ([a,b,c],drg) = choose3 drg0 cnt |
266 | (b0, drg2) = randomR (0,cnt - 2) drg1 | ||
267 | (c0, drg ) = randomR (0,cnt - 3) drg2 | ||
268 | b | b0 < a = b0 | ||
269 | | otherwise = b0 + 1 | ||
270 | c1 | c0 < a = c0 | ||
271 | | otherwise = c0 + 1 | ||
272 | c | c1 < b = c1 | ||
273 | | otherwise = c1 + 1 | ||
274 | ns = mapMaybe (\n -> IntMap.lookup n ts) [a,b,c] | 282 | ns = mapMaybe (\n -> IntMap.lookup n ts) [a,b,c] |
275 | ns' <- case ns of | 283 | ns' <- case ns of |
276 | [an,bn,cn] | distinct3by nodeClass an bn cn | 284 | [an,bn,cn] | distinct3by nodeClass an bn cn |