From 3024b35b05d7f520666af20ced8d1f3080837bb2 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 12 Oct 2017 05:36:54 -0400 Subject: Type signatures and Contravariant instance. --- src/Network/Kademlia/Routing.hs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/Network/Kademlia') diff --git a/src/Network/Kademlia/Routing.hs b/src/Network/Kademlia/Routing.hs index 7f76ac77..a52cca73 100644 --- a/src/Network/Kademlia/Routing.hs +++ b/src/Network/Kademlia/Routing.hs @@ -1,5 +1,6 @@ -- | -- Copyright : (c) Sam Truzjan 2013 +-- (c) Joe Crayne 2017 -- License : BSD3 -- Maintainer : pxqr.sta@gmail.com -- Stability : experimental @@ -73,6 +74,7 @@ import Control.Applicative as A import Control.Arrow import Control.Monad import Data.Function +import Data.Functor.Contravariant import Data.Functor.Identity import Data.List as L hiding (insert) import Data.Maybe @@ -95,6 +97,7 @@ import Data.Typeable import Data.Coerce import Data.Hashable + -- | Last time the node was responding to our queries. -- -- Not all nodes that we learn about are equal. Some are \"good\" and @@ -273,6 +276,10 @@ delta = 15 * 60 -- | Should maintain a set of stable long running nodes. -- -- Note: pings are triggerd only when a bucket is full. +updateBucketForInbound :: ( Coercible t1 t + , Alternative f + , Reifies s (Compare t1) + ) => NominalDiffTime -> t1 -> Bucket s t1 -> f ([t], Bucket s t1) updateBucketForInbound curTime info bucket -- Just update timestamp if a node is already in bucket. -- @@ -316,6 +323,8 @@ updateBucketForInbound curTime info bucket map_ns f = bucket { bktNodes = f (bktNodes bucket) } -- map_q f = bucket { bktQ = runIdentity \$ f (bktQ bucket) } +updateBucketForPingResult :: (Applicative f, Reifies s (Compare a)) => + a -> Bool -> Bucket s a -> f ([(a, Maybe (Timestamp, a))], Bucket s a) updateBucketForPingResult bad_node got_response bucket = pure ( map (,Nothing) forgotten ++ map (second Just) replacements @@ -792,7 +801,8 @@ data KademliaSpace nid ni = KademliaSpace , kademliaSample :: forall m. Applicative m => (Int -> m BS.ByteString) -> nid -> (Int,Word8,Word8) -> m nid } -contramapKS f ks = ks - { kademliaLocation = kademliaLocation ks . f - } +instance Contravariant (KademliaSpace nid) where + contramap f ks = ks + { kademliaLocation = kademliaLocation ks . f + } -- cgit v1.2.3