removed bytestring pinning from Peer

This commit is contained in:
Dmitry Zuikov 2023-01-27 07:04:54 +03:00
parent d19777660c
commit eaa2c41827
4 changed files with 33 additions and 45 deletions

View File

@ -33,8 +33,8 @@ outputs = { self, nixpkgs, haskell-flake-utils, ... }@inputs:
packagePostOverrides = { pkgs }: with pkgs; with haskell.lib; [ packagePostOverrides = { pkgs }: with pkgs; with haskell.lib; [
enableExecutableProfiling disableExecutableProfiling
enableLibraryProfiling disableLibraryProfiling
dontBenchmark dontBenchmark
dontCoverage dontCoverage
dontDistribute dontDistribute

View File

@ -17,7 +17,6 @@ import HBS2.Prelude.Plated
import HBS2.Storage import HBS2.Storage
import Control.Monad.Trans.Maybe import Control.Monad.Trans.Maybe
import Codec.Serialise hiding (encode,decode)
import Control.Concurrent.Async import Control.Concurrent.Async
import Control.Monad.Reader import Control.Monad.Reader
import Data.ByteString.Lazy (ByteString) import Data.ByteString.Lazy (ByteString)
@ -47,6 +46,9 @@ instance (IsKey HbSync, Key HbSync ~ Hash HbSync, Block ByteString ~ ByteString)
getChunk (AnyStorage s) = getChunk s getChunk (AnyStorage s) = getChunk s
hasBlock (AnyStorage s) = hasBlock s hasBlock (AnyStorage s) = hasBlock s
data AnyMessage e = AnyMessage Integer (Encoded e)
deriving stock (Generic)
class Monad m => HasOwnPeer e m where class Monad m => HasOwnPeer e m where
ownPeer :: m (Peer e) ownPeer :: m (Peer e)
@ -56,20 +58,15 @@ class Monad m => HasPeerLocator e m where
class HasStorage m where class HasStorage m where
getStorage :: m AnyStorage getStorage :: m AnyStorage
data Fabriq e = forall bus . (Serialise (Encoded e), Messaging bus e ByteString) => Fabriq bus data Fabriq e = forall bus . (Messaging bus e (AnyMessage e)) => Fabriq bus
class HasFabriq e m where class HasFabriq e m where
getFabriq :: m (Fabriq e) getFabriq :: m (Fabriq e)
instance HasPeer e => Messaging (Fabriq e) e ByteString where instance (HasPeer e) => Messaging (Fabriq e) e (AnyMessage e) where
sendTo (Fabriq bus) = sendTo bus sendTo (Fabriq bus) = sendTo bus
receive (Fabriq bus) = receive bus receive (Fabriq bus) = receive bus
data AnyMessage e = AnyMessage Integer (Encoded e)
deriving stock (Generic)
instance Serialise (Encoded e) => Serialise (AnyMessage e)
data AnyProtocol e m = forall p . ( HasProtocol e p data AnyProtocol e m = forall p . ( HasProtocol e p
, Response e p m , Response e p m
@ -197,14 +194,13 @@ instance ( MonadIO m
instance ( MonadIO m instance ( MonadIO m
, HasProtocol e p , HasProtocol e p
, HasFabriq e (PeerM e m) , HasFabriq e (PeerM e m)
, Serialise (Encoded e)
) => Request e p (PeerM e m) where ) => Request e p (PeerM e m) where
request p msg = do request p msg = do
let proto = protoId @e @p (Proxy @p) let proto = protoId @e @p (Proxy @p)
pipe <- getFabriq @e pipe <- getFabriq @e
me <- ownPeer @e me <- ownPeer @e
let bs = serialise (AnyMessage @e proto (encode msg)) sendTo pipe (To p) (From me) (AnyMessage @e proto (encode msg))
sendTo pipe (To p) (From me) bs
instance ( HasProtocol e p instance ( HasProtocol e p
, Typeable (EventHandler e p (PeerM e IO)) , Typeable (EventHandler e p (PeerM e IO))
@ -320,7 +316,6 @@ runProto :: forall e m . ( MonadIO m
, HasOwnPeer e m , HasOwnPeer e m
, HasFabriq e m , HasFabriq e m
, HasPeer e , HasPeer e
, Serialise (Encoded e)
) )
=> [AnyProtocol e (ResponseM e m)] => [AnyProtocol e (ResponseM e m)]
-> m () -> m ()
@ -337,23 +332,16 @@ runProto hh = do
messages <- receive pipe (To me) messages <- receive pipe (To me)
for_ messages $ \(From pip, bs) -> do for_ messages $ \(From pip, AnyMessage n msg) -> do
case deserialiseOrFail @(AnyMessage e) bs of case Map.lookup n disp of
Nothing -> pure ()
Left _-> pure () Just (AnyProtocol { protoDecode = decoder
, handle = h
Right (AnyMessage n msg) -> do }) -> maybe (pure ()) (runResponseM pip . h) (decoder msg)
case Map.lookup n disp of
Nothing -> pure ()
Just (AnyProtocol { protoDecode = decoder
, handle = h
}) -> maybe (pure ()) (runResponseM pip . h) (decoder msg)
instance ( HasProtocol e p instance ( HasProtocol e p
, Serialise (Encoded e)
, MonadTrans (ResponseM e) , MonadTrans (ResponseM e)
, HasStorage (PeerM e IO) , HasStorage (PeerM e IO)
, Pretty (Peer e) , Pretty (Peer e)
@ -372,9 +360,7 @@ instance ( HasProtocol e p
who <- asks (view answTo) who <- asks (view answTo)
self <- lift $ ownPeer @e self <- lift $ ownPeer @e
fab <- lift $ getFabriq @e fab <- lift $ getFabriq @e
let bs = serialise (AnyMessage @e proto (encode msg)) sendTo fab (To who) (From self) (AnyMessage @e proto (encode msg))
sendTo fab (To who) (From self) bs
instance ( MonadIO m instance ( MonadIO m
, HasProtocol e p , HasProtocol e p

View File

@ -52,7 +52,7 @@ instance ( (HasPeer proto, Hashable (Peer proto))
atomically $ Chan.writeTChan chan (who, msg) atomically $ Chan.writeTChan chan (who, msg)
receive bus (To me) = liftIO do receive bus (To me) = liftIO do
readChan =<< getChan bus me -- Cache.fetchWithCache (fakeP2p bus) me (const newTChanIO) readChan =<< getChan bus me
where where
readChan | blocking bus = atomically . (List.singleton <$>) . Chan.readTChan readChan | blocking bus = atomically . (List.singleton <$>) . Chan.readTChan

View File

@ -55,6 +55,8 @@ import Data.Hashable
import Type.Reflection import Type.Reflection
import Data.Fixed import Data.Fixed
import Data.Dynamic
import System.Random.MWC import System.Random.MWC
import qualified Data.Vector.Unboxed as U import qualified Data.Vector.Unboxed as U
@ -93,9 +95,9 @@ instance Pretty (Peer Fake) where
instance HasProtocol Fake (BlockInfo Fake) where instance HasProtocol Fake (BlockInfo Fake) where
type instance ProtocolId (BlockInfo Fake) = 1 type instance ProtocolId (BlockInfo Fake) = 1
type instance Encoded Fake = ByteString type instance Encoded Fake = Dynamic
decode = either (const Nothing) Just . deserialiseOrFail decode = fromDynamic
encode = serialise encode = toDyn
-- FIXME: 3 is for debug only! -- FIXME: 3 is for debug only!
instance Expires (EventKey Fake (BlockInfo Fake)) where instance Expires (EventKey Fake (BlockInfo Fake)) where
@ -109,16 +111,15 @@ instance Expires (EventKey Fake (BlockAnnounce Fake)) where
instance HasProtocol Fake (BlockChunks Fake) where instance HasProtocol Fake (BlockChunks Fake) where
type instance ProtocolId (BlockChunks Fake) = 2 type instance ProtocolId (BlockChunks Fake) = 2
type instance Encoded Fake = ByteString type instance Encoded Fake = Dynamic
decode = either (const Nothing) Just . deserialiseOrFail decode = fromDynamic
encode = serialise encode = toDyn
instance HasProtocol Fake (BlockAnnounce Fake) where instance HasProtocol Fake (BlockAnnounce Fake) where
type instance ProtocolId (BlockAnnounce Fake) = 3 type instance ProtocolId (BlockAnnounce Fake) = 3
type instance Encoded Fake = ByteString type instance Encoded Fake = Dynamic
decode = either (const Nothing) Just . deserialiseOrFail decode = fromDynamic
encode = serialise encode = toDyn
type instance SessionData e (BlockInfo e) = BlockSizeSession e type instance SessionData e (BlockInfo e) = BlockSizeSession e
type instance SessionData e (BlockChunks e) = BlockDownload type instance SessionData e (BlockChunks e) = BlockDownload
@ -223,9 +224,9 @@ instance Typeable (SessionKey e (Stats e)) => Hashable (SessionKey e (Stats e))
instance HasProtocol Fake (Stats Fake) where instance HasProtocol Fake (Stats Fake) where
type instance ProtocolId (Stats Fake) = 0xFFFFFFFE type instance ProtocolId (Stats Fake) = 0xFFFFFFFE
type instance Encoded Fake = ByteString type instance Encoded Fake = Dynamic
decode = either (const Nothing) Just . deserialiseOrFail decode = fromDynamic
encode = serialise encode = toDyn
newtype Speed = Speed (Fixed E1) newtype Speed = Speed (Fixed E1)
deriving newtype (Ord, Eq, Num, Real, Fractional, Show) deriving newtype (Ord, Eq, Num, Real, Fractional, Show)
@ -265,7 +266,7 @@ updateStats updTime blknum = do
blockDownloadLoop :: forall e m . ( m ~ PeerM e IO blockDownloadLoop :: forall e m . ( m ~ PeerM e IO
-- , e ~ Fake -- , e ~ Fake
, Serialise (Encoded e) -- , Serialise (Encoded e)
, MonadIO m , MonadIO m
, Request e (BlockInfo e) m , Request e (BlockInfo e) m
, Request e (BlockAnnounce e) m , Request e (BlockAnnounce e) m
@ -283,6 +284,7 @@ blockDownloadLoop :: forall e m . ( m ~ PeerM e IO
, Num (Peer e) , Num (Peer e)
, Pretty (Peer e) , Pretty (Peer e)
, Block ByteString ~ ByteString , Block ByteString ~ ByteString
-- , Encoded e ~ ByteString
-- , Key HbSync ~ Hash HbSync -- , Key HbSync ~ Hash HbSync
) )
=> ChunkWriter HbSync IO -> m () => ChunkWriter HbSync IO -> m ()