diff --git a/Hash2Pub/src/Hash2Pub/DHTProtocol.hs b/Hash2Pub/src/Hash2Pub/DHTProtocol.hs index 46b78ad..d2cb74f 100644 --- a/Hash2Pub/src/Hash2Pub/DHTProtocol.hs +++ b/Hash2Pub/src/Hash2Pub/DHTProtocol.hs @@ -37,8 +37,7 @@ data QueryResponse = FORWARD (Set.Set CacheEntry) -- ^return closest nodes from incomingQuery :: NodeState -> NodeCache -> Int -> NodeID -> QueryResponse incomingQuery ownState nCache lBestNodes targetID -- as target ID falls between own ID and first predecessor, it is handled by this node - -- TODO: this fails with an empty predecessor list - | (targetID `localCompare` ownID) `elem` [LT, EQ] && (targetID `localCompare` (head . predecessors) ownState) == GT = FOUND ownState + | (targetID `localCompare` ownID) `elem` [LT, EQ] && not (null . predecessors $ ownState) && (targetID `localCompare` (head . predecessors) ownState) == GT = FOUND ownState -- my interpretation: the "l best next hops" are the l-1 closest preceding nodes and -- the closest succeeding node (like with the p initiated parallel queries | otherwise = trace ("--- Query for " ++ show targetID ++ " wanting " ++ show lBestNodes ++ " results---") $ diff --git a/Hash2Pub/test/FediChordSpec.hs b/Hash2Pub/test/FediChordSpec.hs index ebd730e..49ac188 100644 --- a/Hash2Pub/test/FediChordSpec.hs +++ b/Hash2Pub/test/FediChordSpec.hs @@ -118,7 +118,7 @@ spec = do let emptyCache = nodeCache exampleNodeState nid1 = toNodeID 2^(23::Integer)+1 - node1 = exampleNodeState { nid = nid1} + node1 = exampleNodeState { nid = nid1, predecessors = [nid4]} nid2 = toNodeID 2^(230::Integer)+12 node2 = exampleNodeState { nid = nid2} nid3 = toNodeID 2^(25::Integer)+10 @@ -141,6 +141,15 @@ spec = do (FORWARD nodeset2) = incomingQuery exampleNodeState c4 1 (toNodeID 2^(9::Integer)+5) Set.map (nid . cacheGetNodeStateUnvalidated) nodeset1 `shouldBe` Set.fromList [nid4, nid2, nid3] Set.map (nid . cacheGetNodeStateUnvalidated) nodeset2 `shouldBe` Set.fromList [nid4] + it "recognises the node's own responsibility" $ do + nC <- cacheWith4Entries + incomingQuery node1 nC 3 (toNodeID 2^(22::Integer)) `shouldBe` FOUND node1 + incomingQuery node1 nC 3 nid1 `shouldBe` FOUND node1 + it "does not fail on nodes without neighbours (initial state)" $ do + nC <- cacheWith4Entries + let (FORWARD nodeset) = incomingQuery exampleNodeState nC 3 (toNodeID 11) + Set.map (nid . cacheGetNodeStateUnvalidated ) nodeset `shouldBe` Set.fromList [nid4, nid2, nid3] + -- some example data