From 7612f5532ac4a04be2c54d8c8b7f7559a685184c Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Sat, 13 Jun 2020 14:47:40 +0200 Subject: [PATCH 01/13] create a test for parts of #48 --- test/FediChordSpec.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/FediChordSpec.hs b/test/FediChordSpec.hs index b289c33..ab9f1b2 100644 --- a/test/FediChordSpec.hs +++ b/test/FediChordSpec.hs @@ -148,6 +148,27 @@ spec = do it "does not fail on nodes without neighbours (initial state)" $ do (FORWARD nodeset) <- queryLocalCache <$> exampleLocalNode <*> cacheWith4Entries <*> pure 3 <*> pure (toNodeID 11) Set.map (nid . remoteNode ) nodeset `shouldBe` Set.fromList [nid4, nid2, nid3] + describe "successors and predecessors do not disturb the ring characteristics of EpiChord operations (see #48)" $ do + let + emptyCache = initCache + -- implicitly relies on kNieghbours to be <= 3 + thisNid = toNodeID 1000 + thisNode = setNid thisNid <$> exampleLocalNode + nid2 = toNodeID 1003 + node2 = exampleNodeState { nid = nid2} + nid3 = toNodeID 1010 + node3 = exampleNodeState { nid = nid3} + nid4 = toNodeID 1020 + node4 = exampleNodeState { nid = nid4} + nid5 = toNodeID 1025 + node5 = exampleNodeState { nid = nid5} + allRemoteNodes = [node2, node3, node4, node5] + it "lookups also work for slices larger than 1/2 key space" $ do + node <- setSuccessors allRemoteNodes . setPredecessors allRemoteNodes <$> thisNode + -- do lookup on empty cache but with successors for a key > 1/2 key space + -- succeeding the node + queryLocalCache node emptyCache 1 (nid5 + 10) `shouldBe` FOUND (toRemoteNodeState node) + describe "Messages can be encoded to and decoded from ASN.1" $ do -- define test messages From b179357ab0f3de877543e6f95de101c758af56ba Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Sat, 13 Jun 2020 21:41:23 +0200 Subject: [PATCH 02/13] generalise NodeCache implementation to make it usable for neighbour nodes as well contributes to #48 --- src/Hash2Pub/FediChordTypes.hs | 112 +++++++++++++++++++++++---------- 1 file changed, 79 insertions(+), 33 deletions(-) diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index a599739..d775f9f 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -150,6 +150,7 @@ data LocalNodeState = LocalNodeState } deriving (Show, Eq) +-- | for concurrent access, LocalNodeState is wrapped in a TVar type LocalNodeStateSTM = TVar LocalNodeState -- | class for various NodeState representations, providing @@ -224,22 +225,39 @@ setSuccessors succ' ns = ns {successors = take (kNeighbours ns) . nub . sortBy ( setPredecessors :: [RemoteNodeState] -> LocalNodeState -> LocalNodeState setPredecessors pred' ns = ns {predecessors = take (kNeighbours ns) . nub . sortBy (flip localCompare `on` getNid) . filter ((== GT) . (localCompare `on` getNid) (toRemoteNodeState ns)) $ pred'} -type NodeCache = Map.Map NodeID CacheEntry +-- | Class for all types that can be identified via an EpiChord key. +-- Used for restricting the types a 'RingMap' can store +class HasKeyID a where + getKeyID :: a -> NodeID + +instance HasKeyID RemoteNodeState where + getKeyID = getNid + +instance HasKeyID CacheEntry where + getKeyID (CacheEntry _ ns _) = getNid ns + +type NodeCache = RingMap CacheEntry + +-- | generic data structure for holding elements with a key and modular lookup +newtype RingMap a = RingMap { getRingMap :: HasKeyID a => Map.Map NodeID (RingEntry a) } deriving (Show, Eq) -- | An entry of the 'nodeCache' can hold 2 different kinds of data. -- Type variable @a@ should be of type class 'NodeState', but I do not want to use GADTs here. -data CacheEntry = NodeEntry Bool RemoteNodeState POSIXTime - | ProxyEntry (NodeID, ProxyDirection) (Maybe CacheEntry) +data RingEntry a = KeyEntry a + | ProxyEntry (NodeID, ProxyDirection) (Maybe (RingEntry a)) deriving (Show, Eq) --- | as a compromise, only NodeEntry components are ordered by their NodeID --- while ProxyEntry components should never be tried to be ordered. -instance Ord CacheEntry where +-- | 'RingEntry' type for usage as a node cache +data CacheEntry = CacheEntry Bool RemoteNodeState POSIXTime + +-- | as a compromise, only KeyEntry components are ordered by their NodeID +-- while ProxyEntry components should never be tried to be ordered. +instance Ord RingEntry where a `compare` b = compare (extractID a) (extractID b) where - extractID (NodeEntry _ eState _) = getNid eState - extractID (ProxyEntry _ _) = error "proxy entries should never appear outside of the NodeCache" + extractID (KeyEntry e) = getKeyID e + extractID ProxyEntry{} = error "proxy entries should never appear outside of the RingMap" data ProxyDirection = Backwards | Forwards @@ -254,32 +272,48 @@ instance Enum ProxyDirection where --- useful function for getting entries for a full cache transfer cacheEntries :: NodeCache -> [CacheEntry] -cacheEntries ncache = mapMaybe extractNodeEntries $ Map.elems ncache +cacheEntries = mapMaybe extractNodeEntries . Map.elems . getRingMap where extractNodeEntries (ProxyEntry _ possibleEntry) = possibleEntry + extractNodeEntries (KeyEntry entry) = Just entry -- | An empty @NodeCache@ needs to be initialised with 2 proxy entries, -- linking the modular name space together by connecting @minBound@ and @maxBound@ -initCache :: NodeCache -initCache = Map.fromList $ proxyEntry <$> [(maxBound, (minBound, Forwards)), (minBound, (maxBound, Backwards))] +initRMap :: HasKeyID a => RingMap a +initRMap = RingMap . Map.fromList . proxyEntry <$> [(maxBound, (minBound, Forwards)), (minBound, (maxBound, Backwards))] where proxyEntry (from,to) = (from, ProxyEntry to Nothing) --- | Maybe returns the cache entry stored at given key -cacheLookup :: NodeID -- ^lookup key - -> NodeCache -- ^lookup cache - -> Maybe CacheEntry -cacheLookup key cache = case Map.lookup key cache of +initCache :: NodeCache +initCache = initRingMap + +-- | Maybe returns the entry stored at given key +rMapLookup :: HasKeyID a + => NodeID -- ^lookup key + -> RingMap a -- ^lookup cache + -> Maybe a +rMapLookup key rmap = case Map.lookup key $ getRingMap rmap of Just (ProxyEntry _ result) -> result res -> res +cacheLookup :: NodeID -- ^lookup key + -> NodeCache -- ^lookup cache + -> Maybe CacheEntry +cacheLookup = rMapLookup + -- | a wrapper around lookup functions, making the lookup redirectable by a @ProxyEntry@ -- to simulate a modular ring -lookupWrapper :: (NodeID -> NodeCache -> Maybe (NodeID, CacheEntry)) -> (NodeID -> NodeCache -> Maybe (NodeID, CacheEntry)) -> ProxyDirection -> NodeID -> NodeCache -> Maybe CacheEntry -lookupWrapper f fRepeat direction key cache = - case f key cache of +lookupWrapper :: HasKeyID a + => (NodeID -> Map.Map NodeID a -> Maybe (NodeID, a)) + -> (NodeID -> Map.Map NodeID a -> Maybe (NodeID, a)) + -> ProxyDirection + -> NodeID + -> RingMap a + -> Maybe a +lookupWrapper f fRepeat direction key rmap = + case f key $ getRingMap rmap of -- the proxy entry found holds a - Just (_, ProxyEntry _ (Just entry@NodeEntry{})) -> Just entry + Just (_, ProxyEntry _ (Just entry@KeyEntry{})) -> Just entry -- proxy entry holds another proxy entry, this should not happen Just (_, ProxyEntry _ (Just (ProxyEntry _ _))) -> Nothing -- proxy entry without own entry is a pointer on where to continue @@ -288,38 +322,50 @@ lookupWrapper f fRepeat direction key cache = let newKey = if pointerDirection == direction then pointerID else foundKey + (fromInteger . toInteger . fromEnum $ direction) - in if cacheNotEmpty cache - then lookupWrapper fRepeat fRepeat direction newKey cache + in if rMapNotEmpty rmap + then lookupWrapper fRepeat fRepeat direction newKey rmap else Nothing -- normal entries are returned - Just (_, entry@NodeEntry{}) -> Just entry + Just (_, entry@KeyEntry{}) -> Just entry Nothing -> Nothing where - cacheNotEmpty :: NodeCache -> Bool - cacheNotEmpty cache' = (Map.size cache' > 2) -- there are more than the 2 ProxyEntries - || isJust ( cacheLookup minBound cache') -- or one of the ProxyEntries holds a node - || isJust (cacheLookup maxBound cache') + rMapNotEmpty :: RingMap a -> Bool + rMapNotEmpty rmap' = (Map.size (getRingMap rmap') > 2) -- there are more than the 2 ProxyEntries + || isJust (rMapLookup minBound rmap') -- or one of the ProxyEntries holds a node + || isJust (rMapLookup maxBound rmap') --- | find the successor node to a given key on a modular EpiChord ring cache. +-- | find the successor node to a given key on a modular EpiChord ring. -- Note: The EpiChord definition of "successor" includes the node at the key itself, -- if existing. +rMapLookupSucc :: HasKeyID a + => NodeID -- ^lookup key + -> RingMap a -- ^ring cache + -> Maybe a +rMapLookupSucc = lookupWrapper Map.lookupGE Map.lookupGE Forwards + cacheLookupSucc :: NodeID -- ^lookup key -> NodeCache -- ^ring cache -> Maybe CacheEntry -cacheLookupSucc = lookupWrapper Map.lookupGE Map.lookupGE Forwards +cacheLookupSucc = rMapLookupSucc + +-- | find the predecessor node to a given key on a modular EpiChord ring. +rMapLookupPred :: HasKeyID a + => NodeID -- ^lookup key + -> RingMap a -- ^ring cache + -> Maybe a +rMapLookupPred = lookupWrapper Map.lookupLT Map.lookupLE Backwards --- | find the predecessor node to a given key on a modular EpiChord ring cache. cacheLookupPred :: NodeID -- ^lookup key -> NodeCache -- ^ring cache -> Maybe CacheEntry -cacheLookupPred = lookupWrapper Map.lookupLT Map.lookupLE Backwards +cacheLookupPred = rMapLookupPred -- clean up cache entries: once now - entry > maxAge -- transfer difference now - entry to other node -- | return the @NodeState@ data from a cache entry without checking its validation status -cacheGetNodeStateUnvalidated :: CacheEntry -> RemoteNodeState -cacheGetNodeStateUnvalidated (NodeEntry _ nState _) = nState +cacheGetNodeStateUnvalidated :: RingEntry CacheEntry -> RemoteNodeState +cacheGetNodeStateUnvalidated (KeyEntry (CacheEntry _ nState _)) = nState cacheGetNodeStateUnvalidated (ProxyEntry _ (Just entry)) = cacheGetNodeStateUnvalidated entry cacheGetNodeStateUnvalidated _ = error "trying to pure empty node state, please report a bug" From 6a98b5c6daa2284c65acd6fb00f9e9d550afaefe Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Mon, 15 Jun 2020 13:52:52 +0200 Subject: [PATCH 03/13] fix RingMap function types --- src/Hash2Pub/FediChordTypes.hs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index d775f9f..f887095 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -2,6 +2,7 @@ {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RankNTypes #-} module Hash2Pub.FediChordTypes ( NodeID -- abstract, but newtype constructors cannot be hidden @@ -239,7 +240,7 @@ instance HasKeyID CacheEntry where type NodeCache = RingMap CacheEntry -- | generic data structure for holding elements with a key and modular lookup -newtype RingMap a = RingMap { getRingMap :: HasKeyID a => Map.Map NodeID (RingEntry a) } deriving (Show, Eq) +newtype RingMap a = RingMap { getRingMap :: HasKeyID a => Map.Map NodeID (RingEntry a) } -- | An entry of the 'nodeCache' can hold 2 different kinds of data. -- Type variable @a@ should be of type class 'NodeState', but I do not want to use GADTs here. @@ -253,7 +254,7 @@ data CacheEntry = CacheEntry Bool RemoteNodeState POSIXTime -- | as a compromise, only KeyEntry components are ordered by their NodeID -- while ProxyEntry components should never be tried to be ordered. -instance Ord RingEntry where +instance (HasKeyID a, Eq a) => Ord (RingEntry a) where a `compare` b = compare (extractID a) (extractID b) where extractID (KeyEntry e) = getKeyID e @@ -274,18 +275,20 @@ instance Enum ProxyDirection where cacheEntries :: NodeCache -> [CacheEntry] cacheEntries = mapMaybe extractNodeEntries . Map.elems . getRingMap where - extractNodeEntries (ProxyEntry _ possibleEntry) = possibleEntry - extractNodeEntries (KeyEntry entry) = Just entry + extractNodeEntries :: RingEntry CacheEntry -> Maybe CacheEntry + extractNodeEntries (ProxyEntry _ (Just (KeyEntry entry))) = Just entry + extractNodeEntries (KeyEntry entry) = Just entry + extractNodeEntries _ = Nothing -- | An empty @NodeCache@ needs to be initialised with 2 proxy entries, -- linking the modular name space together by connecting @minBound@ and @maxBound@ initRMap :: HasKeyID a => RingMap a -initRMap = RingMap . Map.fromList . proxyEntry <$> [(maxBound, (minBound, Forwards)), (minBound, (maxBound, Backwards))] +initRMap = RingMap . Map.fromList $ proxyEntry <$> [(maxBound, (minBound, Forwards)), (minBound, (maxBound, Backwards))] where proxyEntry (from,to) = (from, ProxyEntry to Nothing) initCache :: NodeCache -initCache = initRingMap +initCache = initRMap -- | Maybe returns the entry stored at given key rMapLookup :: HasKeyID a @@ -293,8 +296,9 @@ rMapLookup :: HasKeyID a -> RingMap a -- ^lookup cache -> Maybe a rMapLookup key rmap = case Map.lookup key $ getRingMap rmap of - Just (ProxyEntry _ result) -> result - res -> res + Just (ProxyEntry _ (Just (KeyEntry result))) -> Just result + Just (KeyEntry res) -> Just res + _ -> Nothing cacheLookup :: NodeID -- ^lookup key -> NodeCache -- ^lookup cache @@ -304,8 +308,8 @@ cacheLookup = rMapLookup -- | a wrapper around lookup functions, making the lookup redirectable by a @ProxyEntry@ -- to simulate a modular ring lookupWrapper :: HasKeyID a - => (NodeID -> Map.Map NodeID a -> Maybe (NodeID, a)) - -> (NodeID -> Map.Map NodeID a -> Maybe (NodeID, a)) + => (NodeID -> Map.Map NodeID (RingEntry a) -> Maybe (NodeID, RingEntry a)) + -> (NodeID -> Map.Map NodeID (RingEntry a) -> Maybe (NodeID, RingEntry a)) -> ProxyDirection -> NodeID -> RingMap a @@ -313,7 +317,7 @@ lookupWrapper :: HasKeyID a lookupWrapper f fRepeat direction key rmap = case f key $ getRingMap rmap of -- the proxy entry found holds a - Just (_, ProxyEntry _ (Just entry@KeyEntry{})) -> Just entry + Just (_, ProxyEntry _ (Just (KeyEntry entry))) -> Just entry -- proxy entry holds another proxy entry, this should not happen Just (_, ProxyEntry _ (Just (ProxyEntry _ _))) -> Nothing -- proxy entry without own entry is a pointer on where to continue @@ -326,10 +330,10 @@ lookupWrapper f fRepeat direction key rmap = then lookupWrapper fRepeat fRepeat direction newKey rmap else Nothing -- normal entries are returned - Just (_, entry@KeyEntry{}) -> Just entry + Just (_, (KeyEntry entry)) -> Just entry Nothing -> Nothing where - rMapNotEmpty :: RingMap a -> Bool + rMapNotEmpty :: (HasKeyID a) => RingMap a -> Bool rMapNotEmpty rmap' = (Map.size (getRingMap rmap') > 2) -- there are more than the 2 ProxyEntries || isJust (rMapLookup minBound rmap') -- or one of the ProxyEntries holds a node || isJust (rMapLookup maxBound rmap') From 061bce2b08370feff699893526ec146ecaa6f26b Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Mon, 15 Jun 2020 15:14:00 +0200 Subject: [PATCH 04/13] adjust types to refactored RingMap NodeCache --- src/Hash2Pub/DHTProtocol.hs | 23 +++++++++++++---------- src/Hash2Pub/FediChordTypes.hs | 3 +++ src/Hash2Pub/ProtocolTypes.hs | 9 +++------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Hash2Pub/DHTProtocol.hs b/src/Hash2Pub/DHTProtocol.hs index 6469e1c..48f1a19 100644 --- a/src/Hash2Pub/DHTProtocol.hs +++ b/src/Hash2Pub/DHTProtocol.hs @@ -54,10 +54,12 @@ import System.Timeout import Hash2Pub.ASN1Coding import Hash2Pub.FediChordTypes (CacheEntry (..), + CacheEntry (..), LocalNodeState (..), LocalNodeStateSTM, NodeCache, NodeID, NodeState (..), RemoteNodeState (..), + RingEntry (..), RingMap (..), cacheGetNodeStateUnvalidated, cacheLookup, cacheLookupPred, cacheLookupSucc, localCompare, @@ -83,7 +85,7 @@ queryLocalCache ownState nCache lBestNodes targetID preds = predecessors ownState closestSuccessor :: Set.Set RemoteCacheEntry - closestSuccessor = maybe Set.empty Set.singleton $ toRemoteCacheEntry =<< cacheLookupSucc targetID nCache + closestSuccessor = maybe Set.empty (Set.singleton . toRemoteCacheEntry) $ cacheLookupSucc targetID nCache closestPredecessors :: Set.Set RemoteCacheEntry closestPredecessors = closestPredecessor (lBestNodes-1) $ getNid ownState @@ -94,10 +96,11 @@ queryLocalCache ownState nCache lBestNodes targetID | otherwise = let result = cacheLookupPred lastID nCache in - case toRemoteCacheEntry =<< result of + case toRemoteCacheEntry <$> result of Nothing -> Set.empty Just nPred@(RemoteCacheEntry ns ts) -> Set.insert nPred $ closestPredecessor (remainingLookups-1) (nid ns) + -- cache operations -- | update or insert a 'RemoteCacheEntry' into the cache, @@ -118,22 +121,22 @@ addCacheEntryPure now (RemoteCacheEntry ns ts) cache = let -- TODO: limit diffSeconds to some maximum value to prevent malicious nodes from inserting entries valid nearly until eternity timestamp' = if ts <= now then ts else now - newCache = Map.insertWith insertCombineFunction (nid ns) (NodeEntry False ns timestamp') cache - insertCombineFunction newVal@(NodeEntry newValidationState newNode newTimestamp) oldVal = + newCache = Map.insertWith insertCombineFunction (nid ns) (KeyEntry (CacheEntry False ns timestamp')) $ getRingMap cache + insertCombineFunction newVal@(KeyEntry (CacheEntry newValidationState newNode newTimestamp)) oldVal = case oldVal of ProxyEntry n _ -> ProxyEntry n (Just newVal) - NodeEntry oldValidationState _ oldTimestamp -> NodeEntry oldValidationState newNode (max oldTimestamp newTimestamp) + KeyEntry (CacheEntry oldValidationState _ oldTimestamp) -> KeyEntry (CacheEntry oldValidationState newNode (max oldTimestamp newTimestamp)) in - newCache + RingMap newCache -- | delete the node with given ID from cache deleteCacheEntry :: NodeID -- ^ID of the node to be deleted -> NodeCache -- ^cache to delete from -> NodeCache -- ^cache without the specified element -deleteCacheEntry = Map.update modifier +deleteCacheEntry nid = RingMap . Map.update modifier nid . getRingMap where modifier (ProxyEntry idPointer _) = Just (ProxyEntry idPointer Nothing) - modifier NodeEntry {} = Nothing + modifier KeyEntry {} = Nothing -- | Mark a cache entry as verified after pinging it, possibly bumping its timestamp. markCacheEntryAsVerified :: Maybe POSIXTime -- ^ the (current) timestamp to be @@ -141,9 +144,9 @@ markCacheEntryAsVerified :: Maybe POSIXTime -- ^ the (current) timestamp to -> NodeID -- ^ which node to mark -> NodeCache -- ^ current node cache -> NodeCache -- ^ new NodeCache with the updated entry -markCacheEntryAsVerified timestamp = Map.adjust adjustFunc +markCacheEntryAsVerified timestamp nid = RingMap . Map.adjust adjustFunc nid . getRingMap where - adjustFunc (NodeEntry _ ns ts) = NodeEntry True ns $ fromMaybe ts timestamp + adjustFunc (KeyEntry (CacheEntry _ ns ts)) = KeyEntry (CacheEntry True ns $ fromMaybe ts timestamp) adjustFunc (ProxyEntry _ (Just entry)) = adjustFunc entry adjustFunc entry = entry diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index f887095..c09c02b 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -16,8 +16,11 @@ module Hash2Pub.FediChordTypes ( , setPredecessors , NodeCache , CacheEntry(..) + , RingEntry(..) + , RingMap(..) , cacheGetNodeStateUnvalidated , initCache + , cacheEntries , cacheLookup , cacheLookupSucc , cacheLookupPred diff --git a/src/Hash2Pub/ProtocolTypes.hs b/src/Hash2Pub/ProtocolTypes.hs index afb72d2..15cb863 100644 --- a/src/Hash2Pub/ProtocolTypes.hs +++ b/src/Hash2Pub/ProtocolTypes.hs @@ -89,15 +89,12 @@ data RemoteCacheEntry = RemoteCacheEntry RemoteNodeState POSIXTime instance Ord RemoteCacheEntry where (RemoteCacheEntry ns1 _) `compare` (RemoteCacheEntry ns2 _) = nid ns1 `compare` nid ns2 --- | Extracts a 'RemoteCacheEntry' from the indirections of a 'CacheEntry', if it holds one -toRemoteCacheEntry :: CacheEntry -> Maybe RemoteCacheEntry -toRemoteCacheEntry (NodeEntry _ ns ts) = Just $ RemoteCacheEntry ns ts -toRemoteCacheEntry (ProxyEntry _ (Just entry@NodeEntry{})) = toRemoteCacheEntry entry -toRemoteCacheEntry _ = Nothing +toRemoteCacheEntry :: CacheEntry -> RemoteCacheEntry +toRemoteCacheEntry (CacheEntry _ ns ts) = RemoteCacheEntry ns ts -- | a list of all entries of a 'NodeCache' as 'RemoteCacheEntry', useful for cache transfers toRemoteCache :: NodeCache -> [RemoteCacheEntry] -toRemoteCache cache = mapMaybe toRemoteCacheEntry $ Map.elems cache +toRemoteCache cache = toRemoteCacheEntry <$> cacheEntries cache -- | extract the 'NodeState' from a 'RemoteCacheEntry' remoteNode :: RemoteCacheEntry -> RemoteNodeState From 22a6becf6bcac9601c0561b60d39c240bbf1c2b1 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Mon, 15 Jun 2020 16:41:03 +0200 Subject: [PATCH 05/13] fix all previously working tests --- src/Hash2Pub/FediChordTypes.hs | 23 ++++++++++++++++++----- test/FediChordSpec.hs | 15 ++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index c09c02b..bd5db0e 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -18,6 +18,7 @@ module Hash2Pub.FediChordTypes ( , CacheEntry(..) , RingEntry(..) , RingMap(..) + , rMapSize , cacheGetNodeStateUnvalidated , initCache , cacheEntries @@ -39,7 +40,8 @@ import Control.Exception import Data.Function (on) import Data.List (delete, nub, sortBy) import qualified Data.Map.Strict as Map -import Data.Maybe (fromMaybe, isJust, mapMaybe) +import Data.Maybe (fromMaybe, isJust, isNothing, + mapMaybe) import qualified Data.Set as Set import Data.Time.Clock.POSIX import Network.Socket @@ -253,6 +255,7 @@ data RingEntry a = KeyEntry a -- | 'RingEntry' type for usage as a node cache data CacheEntry = CacheEntry Bool RemoteNodeState POSIXTime + deriving (Show, Eq) -- | as a compromise, only KeyEntry components are ordered by their NodeID @@ -308,6 +311,18 @@ cacheLookup :: NodeID -- ^lookup key -> Maybe CacheEntry cacheLookup = rMapLookup +-- | returns number of present 'KeyEntry' in a properly initialised 'RingMap' +rMapSize :: (HasKeyID a, Integral i) + => RingMap a + -> i +rMapSize rmap = fromIntegral $ Map.size innerMap - oneIfEntry minBound - oneIfEntry maxBound + where + innerMap = getRingMap rmap + oneIfEntry :: Integral i => NodeID -> i + oneIfEntry nid + | isNothing (rMapLookup nid rmap) = 1 + | otherwise = 0 + -- | a wrapper around lookup functions, making the lookup redirectable by a @ProxyEntry@ -- to simulate a modular ring lookupWrapper :: HasKeyID a @@ -371,10 +386,8 @@ cacheLookupPred = rMapLookupPred -- transfer difference now - entry to other node -- | return the @NodeState@ data from a cache entry without checking its validation status -cacheGetNodeStateUnvalidated :: RingEntry CacheEntry -> RemoteNodeState -cacheGetNodeStateUnvalidated (KeyEntry (CacheEntry _ nState _)) = nState -cacheGetNodeStateUnvalidated (ProxyEntry _ (Just entry)) = cacheGetNodeStateUnvalidated entry -cacheGetNodeStateUnvalidated _ = error "trying to pure empty node state, please report a bug" +cacheGetNodeStateUnvalidated :: CacheEntry -> RemoteNodeState +cacheGetNodeStateUnvalidated (CacheEntry _ nState _) = nState -- | converts a 'HostAddress6' IP address to a big-endian strict ByteString ipAddrAsBS :: HostAddress6 -> BS.ByteString diff --git a/test/FediChordSpec.hs b/test/FediChordSpec.hs index ab9f1b2..0ac0ea9 100644 --- a/test/FediChordSpec.hs +++ b/test/FediChordSpec.hs @@ -2,11 +2,11 @@ module FediChordSpec where import Control.Exception -import Data.ASN1.Parse (runParseASN1) -import qualified Data.ByteString as BS -import qualified Data.Map.Strict as Map -import Data.Maybe (fromJust, isJust) -import qualified Data.Set as Set +import Data.ASN1.Parse (runParseASN1) +import qualified Data.ByteString as BS +import qualified Data.Map.Strict as Map +import Data.Maybe (fromJust, isJust) +import qualified Data.Set as Set import Data.Time.Clock.POSIX import Network.Socket import Test.Hspec @@ -14,6 +14,7 @@ import Test.Hspec import Hash2Pub.ASN1Coding import Hash2Pub.DHTProtocol import Hash2Pub.FediChord +import Hash2Pub.FediChordTypes spec :: Spec spec = do @@ -79,8 +80,8 @@ spec = do newCache = addCacheEntryPure 10 (RemoteCacheEntry exampleNodeState 10) (addCacheEntryPure 10 (RemoteCacheEntry anotherNode 10) emptyCache) exampleID = nid exampleNodeState it "entries can be added to a node cache and looked up again" $ do - -- the cache includes 2 additional proxy elements right from the start - Map.size newCache - Map.size emptyCache `shouldBe` 2 + rMapSize emptyCache `shouldBe` 0 + rMapSize newCache `shouldBe` 2 -- normal entry lookup nid . cacheGetNodeStateUnvalidated <$> cacheLookup anotherID newCache `shouldBe` Just anotherID nid . cacheGetNodeStateUnvalidated <$> cacheLookup (anotherID+1) newCache `shouldBe` Nothing From 6142ee61d724717e6c08b2ca47ddf8d804972caf Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Tue, 16 Jun 2020 23:15:05 +0200 Subject: [PATCH 06/13] WIP: implement adding, setting and taking RingMap entries. contributes to #48 --- src/Hash2Pub/DHTProtocol.hs | 4 +- src/Hash2Pub/FediChordTypes.hs | 104 +++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 13 deletions(-) diff --git a/src/Hash2Pub/DHTProtocol.hs b/src/Hash2Pub/DHTProtocol.hs index 48f1a19..2fe41eb 100644 --- a/src/Hash2Pub/DHTProtocol.hs +++ b/src/Hash2Pub/DHTProtocol.hs @@ -121,13 +121,13 @@ addCacheEntryPure now (RemoteCacheEntry ns ts) cache = let -- TODO: limit diffSeconds to some maximum value to prevent malicious nodes from inserting entries valid nearly until eternity timestamp' = if ts <= now then ts else now - newCache = Map.insertWith insertCombineFunction (nid ns) (KeyEntry (CacheEntry False ns timestamp')) $ getRingMap cache + newCache = addRMapEntryWith insertCombineFunction (KeyEntry (CacheEntry False ns timestamp')) cache insertCombineFunction newVal@(KeyEntry (CacheEntry newValidationState newNode newTimestamp)) oldVal = case oldVal of ProxyEntry n _ -> ProxyEntry n (Just newVal) KeyEntry (CacheEntry oldValidationState _ oldTimestamp) -> KeyEntry (CacheEntry oldValidationState newNode (max oldTimestamp newTimestamp)) in - RingMap newCache + newCache -- | delete the node with given ID from cache deleteCacheEntry :: NodeID -- ^ID of the node to be deleted diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index bd5db0e..ca7e3d5 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -19,6 +19,10 @@ module Hash2Pub.FediChordTypes ( , RingEntry(..) , RingMap(..) , rMapSize + , addRMapEntry + , addRMapEntryWith + , takeRMapPredecessors + , takeRMapSuccessors , cacheGetNodeStateUnvalidated , initCache , cacheEntries @@ -37,11 +41,12 @@ module Hash2Pub.FediChordTypes ( ) where import Control.Exception +import Data.Foldable (foldr') import Data.Function (on) import Data.List (delete, nub, sortBy) import qualified Data.Map.Strict as Map -import Data.Maybe (fromMaybe, isJust, isNothing, - mapMaybe) +import Data.Maybe (fromJust, fromMaybe, isJust, + isNothing, mapMaybe) import qualified Data.Set as Set import Data.Time.Clock.POSIX import Network.Socket @@ -141,9 +146,9 @@ data LocalNodeState = LocalNodeState -- ^ EpiChord node cache with expiry times for nodes , cacheWriteQueue :: TQueue (NodeCache -> NodeCache) -- ^ cache updates are not written directly to the 'nodeCache' but queued and - , successors :: [RemoteNodeState] -- could be a set instead as these are ordered as well + , successors :: RingMap RemoteNodeState -- could be a set instead as these are ordered as well -- ^ successor nodes in ascending order by distance - , predecessors :: [RemoteNodeState] + , predecessors :: RingMap RemoteNodeState -- ^ predecessor nodes in ascending order by distance , kNeighbours :: Int -- ^ desired length of predecessor and successor list @@ -233,7 +238,7 @@ setPredecessors pred' ns = ns {predecessors = take (kNeighbours ns) . nub . sort -- | Class for all types that can be identified via an EpiChord key. -- Used for restricting the types a 'RingMap' can store -class HasKeyID a where +class (Eq a, Show a) => HasKeyID a where getKeyID :: a -> NodeID instance HasKeyID RemoteNodeState where @@ -247,8 +252,14 @@ type NodeCache = RingMap CacheEntry -- | generic data structure for holding elements with a key and modular lookup newtype RingMap a = RingMap { getRingMap :: HasKeyID a => Map.Map NodeID (RingEntry a) } --- | An entry of the 'nodeCache' can hold 2 different kinds of data. --- Type variable @a@ should be of type class 'NodeState', but I do not want to use GADTs here. +instance Eq (RingMap a) where + a == b = getRingMap a == getRingMap b + +instance Show (RingMap a) where + show rmap = shows (getRingMap rmap) "RingMap " + +-- | entry of a 'RingMap' that holds a value and can also +-- wrap around the lookup direction at the edges of the name space. data RingEntry a = KeyEntry a | ProxyEntry (NodeID, ProxyDirection) (Maybe (RingEntry a)) deriving (Show, Eq) @@ -286,15 +297,15 @@ cacheEntries = mapMaybe extractNodeEntries . Map.elems . getRingMap extractNodeEntries (KeyEntry entry) = Just entry extractNodeEntries _ = Nothing --- | An empty @NodeCache@ needs to be initialised with 2 proxy entries, +-- | An empty 'RingMap' needs to be initialised with 2 proxy entries, -- linking the modular name space together by connecting @minBound@ and @maxBound@ -initRMap :: HasKeyID a => RingMap a -initRMap = RingMap . Map.fromList $ proxyEntry <$> [(maxBound, (minBound, Forwards)), (minBound, (maxBound, Backwards))] +emptyRMap :: HasKeyID a => RingMap a +emptyRMap = RingMap . Map.fromList $ proxyEntry <$> [(maxBound, (minBound, Forwards)), (minBound, (maxBound, Backwards))] where proxyEntry (from,to) = (from, ProxyEntry to Nothing) initCache :: NodeCache -initCache = initRMap +initCache = emptyRMap -- | Maybe returns the entry stored at given key rMapLookup :: HasKeyID a @@ -382,6 +393,77 @@ cacheLookupPred :: NodeID -- ^lookup key -> Maybe CacheEntry cacheLookupPred = rMapLookupPred +addRMapEntryWith :: HasKeyID a + => (RingEntry a -> RingEntry a -> RingEntry a) + -> a + -> RingMap a + -> RingMap a +addRMapEntryWith combineFunc entry = RingMap + . Map.insertWith combineFunc (getKeyID entry) (KeyEntry entry) + . getRingMap + +addRMapEntry :: HasKeyID a + => a + -> RingMap a + -> RingMap a +addRMapEntry = addRMapEntryWith insertCombineFunction + where + insertCombineFunction newVal oldVal = + case oldVal of + ProxyEntry n _ -> ProxyEntry n (Just newVal) + KeyEntry _ -> newVal + + +addRMapEntries :: (Foldable t, HasKeyID a) + => t a + -> RingMap a + -> RingMap a +addRMapEntries entries rmap = foldr' addRMapEntry rmap entries + +setRMapEntries :: (Foldable t, HasKeyID a) + => t a + -> RingMap a +setRMapEntries entries = addRMapEntries entries emptyRMap + +-- | takes up to i entries from a 'RingMap' by calling a getter function on a +-- *startAt* value and after that on the previously returned value. +-- Stops once i entries have been taken or an entry has been encountered twice +-- (meaning the ring has been traversed completely). +-- Forms the basis for 'takeRMapSuccessors' and 'takeRMapPredecessors'. +takeRMapEntries_ :: (HasKeyID a, Integral i) + => (NodeID -> RingMap a -> Maybe a) + -> NodeID + -> i + -> RingMap a + -> [a] +-- TODO: might be more efficient with dlists +takeRMapEntries_ getterFunc startAt num rmap = reverse $ + case getterFunc startAt rmap of + Nothing -> [] + Just anEntry -> takeEntriesUntil (getKeyID anEntry) (getKeyID anEntry) (num-1) [anEntry] + where + -- TODO: figure out correct type signature once it compiles + --takeEntriesUntil :: (HasKeyID b, Integral i) => NodeID -> NodeID -> i -> [b] -> [b] + takeEntriesUntil havingReached previousEntry remaining takeAcc + | remaining <= 0 = takeAcc + | getKeyID (fromJust $ getterFunc previousEntry rmap) == havingReached = takeAcc + | otherwise = let (Just gotEntry) = getterFunc (getKeyID previousEntry) rmap + in takeEntriesUntil (getKeyID havingReached) (getKeyID gotEntry) (remaining-1) (gotEntry:takeAcc) + +takeRMapPredecessors :: (HasKeyID a, Integral i) + => NodeID + -> i + -> RingMap a + -> [a] +takeRMapPredecessors = takeRMapEntries_ rMapLookupPred + +takeRMapSuccessors :: (HasKeyID a, Integral i) + => NodeID + -> i + -> RingMap a + -> [a] +takeRMapSuccessors = takeRMapEntries_ rMapLookupSucc + -- clean up cache entries: once now - entry > maxAge -- transfer difference now - entry to other node From 2269357ed043393a03fd053e39951729e7e5c9ce Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Wed, 17 Jun 2020 02:21:27 +0200 Subject: [PATCH 07/13] deleting RingMap entries, list conversion --- src/Hash2Pub/FediChordTypes.hs | 36 ++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index ca7e3d5..b41e3dd 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -23,6 +23,9 @@ module Hash2Pub.FediChordTypes ( , addRMapEntryWith , takeRMapPredecessors , takeRMapSuccessors + , deleteRMapEntry + , rMapFromList + , rMapToList , cacheGetNodeStateUnvalidated , initCache , cacheEntries @@ -288,14 +291,15 @@ instance Enum ProxyDirection where fromEnum Backwards = - 1 fromEnum Forwards = 1 +-- | helper function for getting the a from a RingEntry a +extractRingEntry :: HasKeyID a => RingEntry a -> Maybe a +extractRingEntry (KeyEntry entry) = Just entry +extractRingEntry (ProxyEntry _ (Just (KeyEntry entry))) = Just entry +extractRingEntry _ = Nothing + --- useful function for getting entries for a full cache transfer cacheEntries :: NodeCache -> [CacheEntry] -cacheEntries = mapMaybe extractNodeEntries . Map.elems . getRingMap - where - extractNodeEntries :: RingEntry CacheEntry -> Maybe CacheEntry - extractNodeEntries (ProxyEntry _ (Just (KeyEntry entry))) = Just entry - extractNodeEntries (KeyEntry entry) = Just entry - extractNodeEntries _ = Nothing +cacheEntries = mapMaybe extractRingEntry . Map.elems . getRingMap -- | An empty 'RingMap' needs to be initialised with 2 proxy entries, -- linking the modular name space together by connecting @minBound@ and @maxBound@ @@ -312,10 +316,7 @@ rMapLookup :: HasKeyID a => NodeID -- ^lookup key -> RingMap a -- ^lookup cache -> Maybe a -rMapLookup key rmap = case Map.lookup key $ getRingMap rmap of - Just (ProxyEntry _ (Just (KeyEntry result))) -> Just result - Just (KeyEntry res) -> Just res - _ -> Nothing +rMapLookup key rmap = extractRingEntry =<< Map.lookup key (getRingMap rmap) cacheLookup :: NodeID -- ^lookup key -> NodeCache -- ^lookup cache @@ -425,6 +426,21 @@ setRMapEntries :: (Foldable t, HasKeyID a) -> RingMap a setRMapEntries entries = addRMapEntries entries emptyRMap +deleteRMapEntry :: (HasKeyID a) + => NodeID + -> RingMap a + -> RingMap a +deleteRMapEntry nid = RingMap . Map.update modifier nid . getRingMap + where + modifier (ProxyEntry idPointer _) = Just (ProxyEntry idPointer Nothing) + modifier KeyEntry {} = Nothing + +rMapToList :: (HasKeyID a) => RingMap a -> [a] +rMapToList = mapMaybe extractRingEntry . Map.elems . getRingMap + +rMapFromList :: (HasKeyID a) => [a] -> RingMap a +rMapFromList = setRMapEntries + -- | takes up to i entries from a 'RingMap' by calling a getter function on a -- *startAt* value and after that on the previously returned value. -- Stops once i entries have been taken or an entry has been encountered twice From 7e08250f8c0775061087fff82731cb6f430ffeb4 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Wed, 17 Jun 2020 14:29:19 +0200 Subject: [PATCH 08/13] refactor setting successors and predecessors --- src/Hash2Pub/FediChordTypes.hs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index b41e3dd..47b2004 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -149,9 +149,9 @@ data LocalNodeState = LocalNodeState -- ^ EpiChord node cache with expiry times for nodes , cacheWriteQueue :: TQueue (NodeCache -> NodeCache) -- ^ cache updates are not written directly to the 'nodeCache' but queued and - , successors :: RingMap RemoteNodeState -- could be a set instead as these are ordered as well + , successors :: [RemoteNodeState] -- could be a set instead as these are ordered as well -- ^ successor nodes in ascending order by distance - , predecessors :: RingMap RemoteNodeState + , predecessors :: [RemoteNodeState] -- ^ predecessor nodes in ascending order by distance , kNeighbours :: Int -- ^ desired length of predecessor and successor list @@ -231,13 +231,14 @@ instance Typeable a => Show (TVar a) where instance Typeable a => Show (TQueue a) where show x = show (typeOf x) + -- | convenience function that updates the successors of a 'LocalNodeState' setSuccessors :: [RemoteNodeState] -> LocalNodeState -> LocalNodeState -setSuccessors succ' ns = ns {successors = take (kNeighbours ns) . nub . sortBy (localCompare `on` getNid) . filter ((== LT) . (localCompare `on` getNid) (toRemoteNodeState ns)) $ succ'} +setSuccessors succs ns = ns {successors = takeRMapSuccessors (getNid ns) (kNeighbours ns) . rMapFromList $ succs} -- | convenience function that updates the predecessors of a 'LocalNodeState' setPredecessors :: [RemoteNodeState] -> LocalNodeState -> LocalNodeState -setPredecessors pred' ns = ns {predecessors = take (kNeighbours ns) . nub . sortBy (flip localCompare `on` getNid) . filter ((== GT) . (localCompare `on` getNid) (toRemoteNodeState ns)) $ pred'} +setPredecessors preds ns = ns {predecessors = takeRMapPredecessors (getNid ns) (kNeighbours ns) . rMapFromList $ preds} -- | Class for all types that can be identified via an EpiChord key. -- Used for restricting the types a 'RingMap' can store @@ -463,8 +464,8 @@ takeRMapEntries_ getterFunc startAt num rmap = reverse $ takeEntriesUntil havingReached previousEntry remaining takeAcc | remaining <= 0 = takeAcc | getKeyID (fromJust $ getterFunc previousEntry rmap) == havingReached = takeAcc - | otherwise = let (Just gotEntry) = getterFunc (getKeyID previousEntry) rmap - in takeEntriesUntil (getKeyID havingReached) (getKeyID gotEntry) (remaining-1) (gotEntry:takeAcc) + | otherwise = let (Just gotEntry) = getterFunc previousEntry rmap + in takeEntriesUntil havingReached (getKeyID gotEntry) (remaining-1) (gotEntry:takeAcc) takeRMapPredecessors :: (HasKeyID a, Integral i) => NodeID From fb164dea0a6e407444ff5b713c7b52ce1c266b2e Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Wed, 17 Jun 2020 14:32:26 +0200 Subject: [PATCH 09/13] fix instance declaration of RingMap --- src/Hash2Pub/FediChordTypes.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index 47b2004..27d5f32 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -256,10 +256,10 @@ type NodeCache = RingMap CacheEntry -- | generic data structure for holding elements with a key and modular lookup newtype RingMap a = RingMap { getRingMap :: HasKeyID a => Map.Map NodeID (RingEntry a) } -instance Eq (RingMap a) where +instance (HasKeyID a) => Eq (RingMap a) where a == b = getRingMap a == getRingMap b -instance Show (RingMap a) where +instance (HasKeyID a) => Show (RingMap a) where show rmap = shows (getRingMap rmap) "RingMap " -- | entry of a 'RingMap' that holds a value and can also From 43e4ab184e51a4632e92860b628155ceb3b6a4f7 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Wed, 17 Jun 2020 14:53:36 +0200 Subject: [PATCH 10/13] adjust cache entry insertion to usage of RingMap #48 --- src/Hash2Pub/DHTProtocol.hs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Hash2Pub/DHTProtocol.hs b/src/Hash2Pub/DHTProtocol.hs index 2fe41eb..3c6cd6c 100644 --- a/src/Hash2Pub/DHTProtocol.hs +++ b/src/Hash2Pub/DHTProtocol.hs @@ -60,6 +60,7 @@ import Hash2Pub.FediChordTypes (CacheEntry (..), NodeID, NodeState (..), RemoteNodeState (..), RingEntry (..), RingMap (..), + addRMapEntryWith, cacheGetNodeStateUnvalidated, cacheLookup, cacheLookupPred, cacheLookupSucc, localCompare, @@ -114,14 +115,14 @@ addCacheEntry entry cache = do -- | pure version of 'addCacheEntry' with current time explicitly specified as argument addCacheEntryPure :: POSIXTime -- ^ current time - -> RemoteCacheEntry -- ^ a remote cache entry received from network - -> NodeCache -- ^ node cache to insert to - -> NodeCache -- ^ new node cache with the element inserted + -> RemoteCacheEntry -- ^ a remote cache entry received from network + -> NodeCache -- ^ node cache to insert to + -> NodeCache -- ^ new node cache with the element inserted addCacheEntryPure now (RemoteCacheEntry ns ts) cache = let -- TODO: limit diffSeconds to some maximum value to prevent malicious nodes from inserting entries valid nearly until eternity timestamp' = if ts <= now then ts else now - newCache = addRMapEntryWith insertCombineFunction (KeyEntry (CacheEntry False ns timestamp')) cache + newCache = addRMapEntryWith insertCombineFunction (CacheEntry False ns timestamp') cache insertCombineFunction newVal@(KeyEntry (CacheEntry newValidationState newNode newTimestamp)) oldVal = case oldVal of ProxyEntry n _ -> ProxyEntry n (Just newVal) From f27812bcf31170edca4af31a9d5d9ce577ec5c29 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Wed, 17 Jun 2020 15:13:49 +0200 Subject: [PATCH 11/13] give up on providing type signature for takeEntriesUntil --- src/Hash2Pub/FediChordTypes.hs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index 27d5f32..06f0fc5 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -459,8 +459,6 @@ takeRMapEntries_ getterFunc startAt num rmap = reverse $ Nothing -> [] Just anEntry -> takeEntriesUntil (getKeyID anEntry) (getKeyID anEntry) (num-1) [anEntry] where - -- TODO: figure out correct type signature once it compiles - --takeEntriesUntil :: (HasKeyID b, Integral i) => NodeID -> NodeID -> i -> [b] -> [b] takeEntriesUntil havingReached previousEntry remaining takeAcc | remaining <= 0 = takeAcc | getKeyID (fromJust $ getterFunc previousEntry rmap) == havingReached = takeAcc From da0b8626cbb9b0a92289383bb8a1b938ce4ad2d8 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Thu, 18 Jun 2020 23:06:43 +0200 Subject: [PATCH 12/13] critical bug fix: use target ID for predecessor query lookup --- src/Hash2Pub/DHTProtocol.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hash2Pub/DHTProtocol.hs b/src/Hash2Pub/DHTProtocol.hs index 3c6cd6c..3a019e7 100644 --- a/src/Hash2Pub/DHTProtocol.hs +++ b/src/Hash2Pub/DHTProtocol.hs @@ -89,7 +89,7 @@ queryLocalCache ownState nCache lBestNodes targetID closestSuccessor = maybe Set.empty (Set.singleton . toRemoteCacheEntry) $ cacheLookupSucc targetID nCache closestPredecessors :: Set.Set RemoteCacheEntry - closestPredecessors = closestPredecessor (lBestNodes-1) $ getNid ownState + closestPredecessors = closestPredecessor (lBestNodes-1) targetID closestPredecessor :: (Integral n, Show n) => n -> NodeID -> Set.Set RemoteCacheEntry closestPredecessor 0 _ = Set.empty closestPredecessor remainingLookups lastID From 3f42f984438bb49d97bc2d91ab34bb65ba8ba097 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Thu, 18 Jun 2020 23:08:20 +0200 Subject: [PATCH 13/13] adjust lookup to RingMap, fix #48 - change default lookup result when not joined to FOUND - fix determining own responsibility #48 - adjust tests --- src/Hash2Pub/DHTProtocol.hs | 20 +++++++++++++++++++- src/Hash2Pub/FediChordTypes.hs | 11 ++++++++++- test/FediChordSpec.hs | 34 ++++++++++++++++++---------------- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/Hash2Pub/DHTProtocol.hs b/src/Hash2Pub/DHTProtocol.hs index 3a019e7..5daa1c8 100644 --- a/src/Hash2Pub/DHTProtocol.hs +++ b/src/Hash2Pub/DHTProtocol.hs @@ -60,11 +60,16 @@ import Hash2Pub.FediChordTypes (CacheEntry (..), NodeID, NodeState (..), RemoteNodeState (..), RingEntry (..), RingMap (..), + HasKeyID(..), addRMapEntryWith, + addRMapEntry, cacheGetNodeStateUnvalidated, cacheLookup, cacheLookupPred, cacheLookupSucc, localCompare, localCompare, setPredecessors, + getKeyID, rMapFromList, + rMapLookupPred, + rMapLookupSucc, setSuccessors) import Hash2Pub.ProtocolTypes @@ -77,7 +82,7 @@ import Debug.Trace (trace) queryLocalCache :: LocalNodeState -> NodeCache -> Int -> NodeID -> QueryResponse queryLocalCache ownState nCache lBestNodes targetID -- as target ID falls between own ID and first predecessor, it is handled by this node - | (targetID `localCompare` ownID) `elem` [LT, EQ] && maybe False (\p -> targetID `localCompare` p == GT) (getNid <$> headMay preds) = FOUND . toRemoteNodeState $ ownState + | isInOwnResponsibilitySlice ownState targetID = FOUND . toRemoteNodeState $ 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 = FORWARD $ closestSuccessor `Set.union` closestPredecessors @@ -101,6 +106,19 @@ queryLocalCache ownState nCache lBestNodes targetID Nothing -> Set.empty Just nPred@(RemoteCacheEntry ns ts) -> Set.insert nPred $ closestPredecessor (remainingLookups-1) (nid ns) +-- | Determines whether a lookup key is within the responsibility slice of a node, +-- as it falls between its first predecessor and the node itself. +-- Looks up the successor of the lookup key on a 'RingMap' representation of the +-- predecessor list with the node itself added. If the result is the same as the node +-- itself then it falls into the responsibility interval. +isInOwnResponsibilitySlice :: HasKeyID a => LocalNodeState -> a -> Bool +isInOwnResponsibilitySlice ownNs lookupTarget = (getKeyID <$> rMapLookupSucc (getKeyID lookupTarget) predecessorRMap) == pure (getNid ownNs) + where + predecessorList = predecessors ownNs + -- add node itself to RingMap representation, to distinguish between + -- responsibility of own node and predecessor + predecessorRMap = addRMapEntry (toRemoteNodeState ownNs) $ rMapFromList predecessorList + closestPredecessor = headMay predecessorList -- cache operations diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index 06f0fc5..601ca63 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -18,12 +18,18 @@ module Hash2Pub.FediChordTypes ( , CacheEntry(..) , RingEntry(..) , RingMap(..) + , HasKeyID + , getKeyID , rMapSize + , rMapLookup + , rMapLookupPred + , rMapLookupSucc , addRMapEntry , addRMapEntryWith , takeRMapPredecessors , takeRMapSuccessors , deleteRMapEntry + , setRMapEntries , rMapFromList , rMapToList , cacheGetNodeStateUnvalidated @@ -251,6 +257,9 @@ instance HasKeyID RemoteNodeState where instance HasKeyID CacheEntry where getKeyID (CacheEntry _ ns _) = getNid ns +instance HasKeyID NodeID where + getKeyID = id + type NodeCache = RingMap CacheEntry -- | generic data structure for holding elements with a key and modular lookup @@ -260,7 +269,7 @@ instance (HasKeyID a) => Eq (RingMap a) where a == b = getRingMap a == getRingMap b instance (HasKeyID a) => Show (RingMap a) where - show rmap = shows (getRingMap rmap) "RingMap " + show rmap = shows "RingMap " (show $ getRingMap rmap) -- | entry of a 'RingMap' that holds a value and can also -- wrap around the lookup direction at the edges of the name space. diff --git a/test/FediChordSpec.hs b/test/FediChordSpec.hs index 0ac0ea9..dbb8e8b 100644 --- a/test/FediChordSpec.hs +++ b/test/FediChordSpec.hs @@ -127,28 +127,30 @@ spec = do node3 = exampleNodeState { nid = nid3} nid4 = toNodeID 2^(9::Integer)+100 node4 = exampleNodeState { nid = nid4} - cacheWith2Entries :: IO NodeCache - cacheWith2Entries = addCacheEntryPure 10 <$> (RemoteCacheEntry <$> (toRemoteNodeState <$> node1) <*> pure 10) <*> pure (addCacheEntryPure 10 (RemoteCacheEntry node2 10) emptyCache) - cacheWith4Entries = addCacheEntryPure 10 (RemoteCacheEntry node3 10) <$> (addCacheEntryPure 10 (RemoteCacheEntry node4 10) <$> cacheWith2Entries) - it "works on an empty cache" $ do - queryLocalCache <$> exampleLocalNode <*> pure emptyCache <*> pure 3 <*> pure (toNodeID 2^(9::Integer)+5) `shouldReturn` FORWARD Set.empty - queryLocalCache <$> exampleLocalNode <*> pure emptyCache <*> pure 1 <*> pure (toNodeID 2342) `shouldReturn` FORWARD Set.empty + nid5 = toNodeID 2^(25::Integer)+100 + node5 = exampleNodeState { nid = nid5} + cacheWith2Entries :: NodeCache + cacheWith2Entries = addCacheEntryPure 10 (RemoteCacheEntry node5 10) (addCacheEntryPure 10 (RemoteCacheEntry node2 10) emptyCache) + cacheWith4Entries = addCacheEntryPure 10 (RemoteCacheEntry node3 10) (addCacheEntryPure 10 (RemoteCacheEntry node4 10) cacheWith2Entries) + it "nodes not joined provide the default answer FOUND" $ do + exampleLocalNodeAsRemote <- toRemoteNodeState <$> exampleLocalNode + queryLocalCache <$> exampleLocalNode <*> pure emptyCache <*> pure 3 <*> pure (toNodeID 2^(9::Integer)+5) `shouldReturn` FOUND exampleLocalNodeAsRemote + queryLocalCache <$> exampleLocalNode <*> pure cacheWith4Entries <*> pure 1 <*> pure (toNodeID 2342) `shouldReturn` FOUND exampleLocalNodeAsRemote + it "joined nodes do not fall back to the default" $ + queryLocalCache <$> node1 <*> pure emptyCache <*> pure 1 <*> pure (toNodeID 3) `shouldReturn` FORWARD Set.empty it "works on a cache with less entries than needed" $ do - (FORWARD nodeset) <- queryLocalCache <$> exampleLocalNode <*> cacheWith2Entries <*> pure 4 <*> pure (toNodeID 2^(9::Integer)+5) - Set.map (nid . remoteNode) nodeset `shouldBe` Set.fromList [ nid1, nid2 ] + (FORWARD nodeset) <- queryLocalCache <$> node1 <*> pure cacheWith2Entries <*> pure 4 <*> pure (toNodeID 2^(9::Integer)+5) + Set.map (nid . remoteNode) nodeset `shouldBe` Set.fromList [ nid5, nid2 ] it "works on a cache with sufficient entries" $ do - (FORWARD nodeset1) <- queryLocalCache <$> exampleLocalNode <*> cacheWith4Entries <*> pure 3 <*> pure (toNodeID 2^(9::Integer)+5) - (FORWARD nodeset2) <- queryLocalCache <$> exampleLocalNode <*> cacheWith4Entries <*> pure 1 <*> pure (toNodeID 2^(9::Integer)+5) - Set.map (nid . remoteNode) nodeset1 `shouldBe` Set.fromList [nid4, nid2, nid3] + (FORWARD nodeset1) <- queryLocalCache <$> node1 <*> pure cacheWith4Entries <*> pure 3 <*> pure (toNodeID 2^(9::Integer)+5) + (FORWARD nodeset2) <- queryLocalCache <$> node1 <*> pure cacheWith4Entries <*> pure 1 <*> pure (toNodeID 2^(9::Integer)+5) + Set.map (nid . remoteNode) nodeset1 `shouldBe` Set.fromList [nid4, nid2, nid5] Set.map (nid . remoteNode) nodeset2 `shouldBe` Set.fromList [nid4] it "recognises the node's own responsibility" $ do - FOUND selfQueryRes <- queryLocalCache <$> node1 <*> cacheWith4Entries <*> pure 3 <*> pure nid1 + FOUND selfQueryRes <- queryLocalCache <$> node1 <*> pure cacheWith4Entries <*> pure 3 <*> pure nid1 getNid <$> node1 `shouldReturn` getNid selfQueryRes - FOUND responsibilityResult <- queryLocalCache <$> node1 <*> cacheWith4Entries <*> pure 3 <*> pure (toNodeID 2^(22::Integer)) + FOUND responsibilityResult <- queryLocalCache <$> node1 <*> pure cacheWith4Entries <*> pure 3 <*> pure (toNodeID 2^(22::Integer)) getNid <$> node1 `shouldReturn` getNid responsibilityResult - it "does not fail on nodes without neighbours (initial state)" $ do - (FORWARD nodeset) <- queryLocalCache <$> exampleLocalNode <*> cacheWith4Entries <*> pure 3 <*> pure (toNodeID 11) - Set.map (nid . remoteNode ) nodeset `shouldBe` Set.fromList [nid4, nid2, nid3] describe "successors and predecessors do not disturb the ring characteristics of EpiChord operations (see #48)" $ do let emptyCache = initCache