packages feed

haskoin-node-2.7.0: src/Haskoin/Node/Peer.hs

-- |
-- Module      : Haskoin.Node.Peer
-- Copyright   : (c) Haskoin Developers
-- License     : MIT
-- Maintainer  : jprupp@protonmail.ch
-- Stability   : experimental
-- Portability : POSIX
--
-- A single connection to a peer on the Bitcoin network.
--
-- Each peer runs as its own process. It reads messages from the network,
-- parses them, and publishes them as 'PeerEvent's. It also accepts messages to
-- send to the peer through its mailbox. Peers are normally started and managed
-- by "Haskoin.Node.PeerMgr"; applications mostly use 'sendMessage',
-- 'getBlocks', 'getTxs' and 'getData' on peers obtained from
-- 'Haskoin.Node.PeerMgr.getPeers'.
--
-- = Defensive by design
--
-- A peer is anyone on the Internet. The code here assumes the worst of it:
--
-- * The 24-byte header of each message is read and checked first: network
--   magic, and payload size against the limit for that command. Only then is
--   the payload read, so a peer cannot make the node buffer a huge message it
--   would refuse anyway.
--
-- * A payload is read in time and memory linear in its size, however small
--   the pieces it arrives in. The pieces are collected and joined once. An
--   earlier version appended each piece to the bytes read so far, so a peer
--   that sent a large message in small TCP segments made the node copy it
--   over and over, with a cost that grew with the square of the size.
--
-- * Anything that does not parse closes the connection with a
--   'PeerException' saying why. There is no attempt to recover, because a
--   peer that sends garbage once is not worth the risk.
--
-- * A block whose transactions are not the ones its header commits to closes
--   the connection too ('MutatedBlock'). The header carries the proof of
--   work, but anyone can attach other transactions to it. The check is
--   "Haskoin.Node.Block", the same as Bitcoin Core runs on every block it
--   receives. It is done here, once, so that 'getBlocks', 'getData' and the
--   'PeerMessage' events all see only blocks that passed it.
--
-- * Every message on its way out is judged first by a model of the node at
--   the other end ("Haskoin.Node.Peer.Remote"). One that the node would
--   punish this node for is not sent, and is logged with the upstream line
--   of the rule. A request in 'getData' that the node would drop without an
--   answer fails at once.
--
-- * Reading pauses when the node falls behind ('maxPendingMessages'), so the
--   speed of the node, not the speed of the peer, sets the pace.
--
-- = Threads and the state they share
--
-- A peer process is two threads: the writer, which takes commands from the
-- mailbox and writes them to the socket, and the reader, which reads the
-- socket and publishes what it reads. Other threads touch the 'Peer' handle:
-- whoever sends messages, the router of "Haskoin.Node", and requests. Each
-- piece of state is changed in one place or in one transaction:
--
-- * The model of the peer (@remote@) is judged and changed in the
--   transaction that queues a message ('sendMessage'), and by the reader in
--   the transaction that counts a version message it read. The model keeps
--   its own clock, so threads that read the time in another order than they
--   commit cannot make it go back ("Haskoin.Node.Peer.Remote").
--
-- * The allowance of pending messages (@pending@) goes up in the reader, in
--   one transaction with the wait for room, and down once for each message
--   published, in the router ('peerMessageDone').
--
-- * Whether the peer is closing (@closing@) is set by 'killPeer' and when the
--   process ends, and the process races its connection against it. A writer
--   blocked in a write to a peer that stopped reading never looks at its
--   mailbox, so a request to disconnect cannot wait behind it there.
--
-- * Requests ('getData') run one at a time on a peer, under a lock, and the
--   nonce of the ping that ends the last one (@owed@) is set as that ping is
--   queued and cleared by the reader, in the transaction that counts its
--   pong. A new request knows from it whether answers to an earlier one may
--   still come ("Haskoin.Node.Peer.Request").
--
-- docs\/concurrency.md in the repository has the whole picture.
module Haskoin.Node.Peer
  ( PeerConfig (..),
    PeerEvent (..),
    PeerException (..),
    Conduits (..),
    WithConnection,
    Peer (..),
    PeerMessage (..),
    peer,
    newPeer,
    sendMessage,
    killPeer,
    getBlocks,
    getTxs,
    getData,
    pingPeer,
    judgeMessage,
    Owner,
    newOwner,
    ownerName,
    reservePeer,
    releasePeer,
    peerHolder,
    getBusy,
    setBusy,
    setFree,
    peerMessageDone,
    peerMessageDoneSTM,
    maxPendingMessages,
  )
where

import Conduit
import Control.Concurrent.Async (link, race_, withAsync)
import Control.Concurrent.STM
import Control.Exception (Exception, bracket_, finally, throwIO)
import Control.Monad
import Data.Bool (bool)
import Data.ByteString (ByteString)
import Data.ByteString qualified as B
import Data.Function (on)
import Data.Int (Int64)
import Data.Map.Strict qualified as Map
import Data.Maybe (catMaybes, isJust)
import Data.Serialize (decode, runGet, runPut)
import Data.String.Conversions (cs)
import Data.Text (Text)
import Data.Time.Clock.POSIX (getPOSIXTime)
import Data.Word (Word32, Word64)
import Haskoin
import Haskoin.Node.Block
import Haskoin.Node.Logger
import Haskoin.Node.Loop (loopForever)
import Haskoin.Node.Peer.Lock
import Haskoin.Node.Peer.Remote
import Haskoin.Node.Peer.Request
import NQE
import System.Random
import System.Timeout (timeout)

-- | The two directions of a network connection, as conduits.
--
-- Connections are abstract conduits rather than sockets, so that the same peer
-- code runs over TCP, through a proxy, or against an in-memory mock in tests.
data Conduits = Conduits
  { -- | bytes received from the peer
    inboundConduit :: ConduitT () ByteString IO (),
    -- | bytes to send to the peer
    outboundConduit :: ConduitT ByteString Void IO ()
  }

-- | A function that opens a connection, passes its 'Conduits' to the given
-- action, and closes the connection when the action returns.
type WithConnection = (Conduits -> IO ()) -> IO ()

-- | Configuration of a peer process.
data PeerConfig = PeerConfig
  { -- | peer events are published here
    pub :: !(Publisher PeerEvent),
    -- | network constants
    net :: !Network,
    -- | name of the peer in logs, usually its address
    label :: !Text,
    -- | how to open the connection to the peer
    connect :: !WithConnection,
    -- | log messages here
    logger :: !Logger,
    -- | look up a block header that the node has stored, or give 'Nothing'.
    -- A block from the peer is checked against its parent's height, to know
    -- whether its witness data must match the commitment in its coinbase
    -- (see 'witnessCheck'). "Haskoin.Node" passes 'Haskoin.Node.chainGetBlock'.
    -- A function that always gives 'Nothing' still checks the Merkle root of
    -- every block, but not its witness data.
    lookupBlock :: !(BlockHash -> IO (Maybe BlockNode)),
    -- | the best block header that the node has stored, which the model of
    -- the peer takes for the tip of the peer ("Haskoin.Node.Peer.Remote").
    -- "Haskoin.Node" passes 'Haskoin.Node.Chain.chainGetBest'.
    bestBlock :: !(IO BlockNode),
    -- | the model of the peer when the connection opens, from 'newRemote'
    remote :: !Remote
  }

-- | Events published about peers.
data PeerEvent
  = -- | the peer completed the version handshake and is online
    PeerConnected !Peer
  | -- | the peer disconnected
    PeerDisconnected !Peer
  | -- | the peer sent a message. A block in an 'MBlock' message has passed
    -- 'blockMutation': its transactions hash to the Merkle root of its
    -- header, and its witness data matches its coinbase when the node knows
    -- the parent of the block. Nothing else about it is checked. A
    -- transaction in an 'MTx' message is not checked at all.
    PeerMessage !Peer !Message
  deriving (Eq)

-- | Why a peer process stopped with an error. The peer manager logs it and
-- forgets the peer.
--
-- These used to be calls to 'error' with a message. A type lets callers and
-- tests tell the reasons apart without parsing text.
data PeerException
  = -- | the peer closed the connection in the middle of a message
    ConnectionClosed
  | -- | the 24-byte message header could not be parsed
    InvalidMessageHeader
  | -- | the message is for another network; holds its magic bytes
    WrongNetwork !Word32
  | -- | the payload is larger than allowed for the command; holds its size
    MessageTooLarge !MessageCommand !Word32
  | -- | the payload could not be parsed; holds the reason
    InvalidPayload !MessageCommand !String
  | -- | a block whose transactions are not the ones its header commits to;
    -- holds the hash of the block and the reason (see 'blockMutation')
    MutatedBlock !BlockHash !BlockMutation
  deriving stock (Show)

instance Exception PeerException

-- | Mailbox for a peer. Peers are compared by their mailbox, and shown by their
-- label.
data Peer = Peer
  { -- | send 'PeerMessage's to the peer process here
    mailbox :: !(Mailbox PeerMessage),
    -- | events from this peer are published here
    pub :: !(Publisher PeerEvent),
    -- | name of the peer in logs, usually its address
    label :: !Text,
    -- | who has reserved the peer for a long conversation; see
    -- 'reservePeer'
    busy :: !(TVar (Lock Owner)),
    -- | messages from this peer that the node has not finished handling
    pending :: !(TVar Int),
    -- | the model of the peer, which judges the messages sent to it
    remote :: !(TVar Remote),
    -- | what the node knows that a judgement of a message needs
    facts :: !(Message -> IO Facts),
    -- | whether the process is told to stop, or has stopped; the process
    -- races its connection against it
    closing :: !(TVar Bool),
    -- | held by the request that is running on the peer ('getData'), so that
    -- requests do not see each other's answers
    requestLock :: !(TMVar ()),
    -- | the nonce of the ping that ends the last request on the peer, until
    -- the reader reads its pong
    owed :: !(TVar (Maybe Word64))
  }

instance Eq Peer where
  (==) = (==) `on` (.mailbox)

instance Show Peer where
  show = cs . (.label)

-- | Incoming messages that a peer accepts.
data PeerMessage
  = -- | disconnect from the peer, once the messages queued before it are
    -- sent; 'killPeer' queues it, but does not wait for it
    KillPeer
  | -- | send a network message to the peer
    SendMessage !Message
  | -- | a message that 'sendMessage' did not send, because the model of the
    -- peer said that the peer would punish the node for it; the peer process
    -- logs it, in the order of the messages that it sends
    NotSent !Message !Judgement

-- | A 'Peer' handle for a configuration and the mailbox of its process,
-- with a free lock, no pending messages, the model of the configuration, and
-- no request.
--
-- The handle holds the state that the threads of the process and its users
-- share, so there is one way to make it: the @wrapPeer@ of earlier releases,
-- which took some of that state from the caller, is gone.
newPeer :: PeerConfig -> Mailbox PeerMessage -> IO Peer
newPeer cfg mbox = do
  busy <- newTVarIO Free
  pending <- newTVarIO 0
  remote <- newTVarIO cfg.remote
  closing <- newTVarIO False
  requestLock <- newTMVarIO ()
  owed <- newTVarIO Nothing
  return
    Peer
      { mailbox = mbox,
        pub = cfg.pub,
        label = cfg.label,
        busy = busy,
        pending = pending,
        remote = remote,
        facts = gatherFacts cfg,
        closing = closing,
        requestLock = requestLock,
        owed = owed
      }

-- | What the node knows that a judgement of a message needs: the time, its
-- best header, and its headers of the blocks that the message names, up to
-- 'factsLookups' of them. A block past that, or one that the node does not
-- know, is taken to be at the tip, which changes no judgement of the
-- protocol.
gatherFacts :: PeerConfig -> Message -> IO Facts
gatherFacts cfg m = do
  t <- getPOSIXTime
  best <- cfg.bestBlock
  found <- forM (take factsLookups named) $ \h -> fmap (h,) <$> cfg.lookupBlock h
  return
    Facts
      { now = floor (t * 1000000) :: Int64,
        best = best,
        known = Map.fromList (catMaybes found)
      }
  where
    named = case m of
      MGetData (GetData ivs) -> [BlockHash iv.invHash | iv <- ivs, isBlock iv.invType]
      MGetHeaders gh -> gh.locator
      MGetBlocks gb -> gb.locator
      _ -> []
    isBlock = (`elem` [InvBlock, InvWitnessBlock, InvMerkleBlock, InvCompactBlock])

-- | Most block headers looked up to judge one message.
factsLookups :: Int
factsLookups = 64

-- | What the peer would do with a message if it were queued now, after the
-- messages queued before it, without queuing it.
judgeMessage :: Peer -> Message -> IO Judgement
judgeMessage p m = do
  f <- p.facts m
  fst . judge f m <$> readTVarIO p.remote

-- | Most messages from one peer that may wait for the node to handle them.
-- When a peer gets this far ahead, the node stops reading from its socket
-- until it catches up, so that a peer that sends faster than the node can
-- handle fills its own TCP window instead of the node's memory.
maxPendingMessages :: Int
maxPendingMessages = 16

-- | Tell the peer process that the node has finished handling one of the
-- messages that it published, so that it may read another one.
--
-- Call it exactly once for each 'PeerMessage', from one consumer of the
-- events of the peer: in a node, the router of "Haskoin.Node" does, and the
-- machine it runs ("Haskoin.Node.Router") disposes of every message exactly
-- once. A second call for the same message would let the peer get one more
-- message ahead of the node than 'maxPendingMessages'; a missing call would
-- stop the peer for good once it misses 'maxPendingMessages' of them. The
-- count never goes below zero, so a stray call cannot make room for more
-- than the allowance, but it is a fault of the caller, not a case to rely
-- on. A token in each message to make a second call impossible was
-- considered, and left out: it would change 'PeerEvent' for every consumer,
-- for a rule that one place keeps.
peerMessageDone :: Peer -> IO ()
peerMessageDone = atomically . peerMessageDoneSTM

-- | 'peerMessageDone' in 'STM'.
peerMessageDoneSTM :: Peer -> STM ()
peerMessageDoneSTM p = modifyTVar' p.pending (max 0 . subtract 1)

-- | Run peer process in current thread. The 'Peer' must be the one that
-- 'newPeer' built with the mailbox of this inbox.
--
-- Opens the connection, then reads and publishes messages from the peer while
-- sending the messages that arrive in the inbox. Returns when 'killPeer' is
-- called or the connection is closed, and throws if the peer sends invalid
-- data.
--
-- The connection runs in a race against the @closing@ flag of the peer,
-- which 'killPeer' sets: the process stops at once, even when its writer is
-- blocked in a write to a peer that stopped reading. The flag is set when
-- the process ends, however it ends, so that requests started after that
-- give up at once.
peer ::
  PeerConfig ->
  Peer ->
  Inbox PeerMessage ->
  IO ()
peer cfg@PeerConfig {label, net, pub, logger} p inbox =
  race_ (cfg.connect peer_session) closed
    `finally` atomically (writeTVar p.closing True)
  where
    closed = atomically (readTVar p.closing >>= check)
    -- Commands from the mailbox, as a stream of messages to send.
    go = do
      m <- liftIO $ do
        logDebug logger $ label <> " awaiting event..."
        receive inbox
      dispatchMessage cfg p m >>= bool (return ()) go
    peer_session ad = do
      let ins = ad.inboundConduit
          ons = ad.outboundConduit
          src =
            runConduit $
              ins
                .| inPeerConduit cfg
                .| mapM_C send_msg
          snk = outPeerConduit net .| ons
      withAsync src $ \as -> do
        link as
        runConduit (go .| snk)
    -- Wait until the node has room for another message from this peer.
    send_msg m = do
      atomically $ do
        n <- readTVar p.pending
        check (n < maxPendingMessages)
        writeTVar p.pending (n + 1)
        case m of
          MVersion v -> modifyTVar' p.remote (learnVersion v)
          -- the last request on the peer is no longer owed its pong; this
          -- comes before the pong is published, so a request that reads
          -- @owed@ after it subscribed sees the pong if it still waits
          MPong (Pong k) -> modifyTVar' p.owed (\o -> if o == Just k then Nothing else o)
          _ -> return ()
      publish (PeerMessage p m) pub

-- | Internal function to dispatch peer messages. 'sendMessage' judged each
-- message as it queued it; one that the peer would punish the node for
-- comes as 'NotSent', and is logged.
dispatchMessage ::
  PeerConfig ->
  Peer ->
  PeerMessage ->
  ConduitT i Message IO Bool
dispatchMessage PeerConfig {label, logger} _ (SendMessage m) = do
  liftIO $ logDebug logger (label <> " sending cmd " <> cs (show (msgType m)))
  yield m
  return True
dispatchMessage PeerConfig {label, logger} _ (NotSent m j) = do
  liftIO . logWarn logger $
    label <> " not sending cmd " <> cs (show (msgType m)) <> ": " <> judgementText j
  return True
dispatchMessage PeerConfig {label, logger} _ KillPeer = do
  liftIO $ logDebug logger (label <> " disconnecting")
  return False

-- | Internal conduit to parse messages coming from peer. The header is checked
-- before the payload is read, so that a peer cannot make the node buffer a
-- payload that it will reject anyway. The size limit is the 'maxPayloadSize'
-- of the command. On Bitcoin Cash only messages that carry block content may
-- be larger than 2 MiB. A peer that sends anything invalid is disconnected.
--
-- The counts of list messages are not checked here. 'getMessage' fails as
-- soon as it reads a count above the limit of the message. The limits are
-- 2000 headers and 1000 addresses, for example.
--
-- The bytes of a message are collected as the pieces that arrive, and joined
-- once, header and payload together. The cost of reading is linear in the
-- size of the message, whatever the size of the pieces.
--
-- A block must pass 'blockMutation' before it is passed on. Its parent is
-- looked up with 'lookupBlock' to choose the witness rule.
inPeerConduit ::
  PeerConfig -> ConduitT ByteString Message IO ()
inPeerConduit PeerConfig {label, net, logger, lookupBlock} = loopForever $ do
  debug (label <> " awaiting message...")
  hdr <- takePieces 24
  let x = B.concat hdr
  MessageHeader mgc cmd len _ <- case decode x of
    Left _ -> bad InvalidMessageHeader
    Right h -> return h
  when (mgc /= net.magic) $ bad (WrongNetwork mgc)
  when (len > maxPayloadSize net cmd) $ bad (MessageTooLarge cmd len)
  debug (label <> " sent cmd " <> cs (show cmd))
  payload <- takePieces (fromIntegral len)
  case runGet (getMessage net) (B.concat (x : payload)) of
    Left e -> bad (InvalidPayload cmd (firstLine e))
    Right m -> do
      debug (label <> " sent payload for cmd " <> cs (show cmd))
      checkBlock m
      yield m
  where
    debug = liftIO . logDebug logger
    -- Exactly n bytes, as the pieces they arrived in. Joining them is left
    -- to the caller, so that it happens once.
    takePieces n = do
      pieces <- takeCE n .| sinkList
      when (sum (map B.length pieces) /= n) $ bad ConnectionClosed
      return pieces
    checkBlock = \case
      MBlock b -> do
        parent <- liftIO (lookupBlock b.header.prev)
        forM_ (blockMutation (witnessCheck net parent) b) $
          bad . MutatedBlock (headerHash b.header)
      _ -> return ()
    bad :: PeerException -> ConduitT i o IO a
    bad e = liftIO $ do
      -- a peer that hangs up is the ordinary end of a connection; one that
      -- sends what it should not is worth a look, and is not an error of
      -- this node either
      let level = case e of
            ConnectionClosed -> logDebug
            _ -> logWarn
      level logger (label <> ": " <> cs (show e))
      throwIO e
    -- The reason is on the first line of the parser's error text. The lines
    -- after it are call stack noise.
    firstLine = takeWhile (/= '\n')

-- | Outgoing peer conduit to serialize and send messages.
outPeerConduit :: (Monad m) => Network -> ConduitT Message ByteString m ()
outPeerConduit net = awaitForever $ yield . runPut . putMessage net

-- | Tell a peer to disconnect. The peer process stops at once: messages
-- queued before may not be sent.
--
-- It sets the @closing@ flag that the process races its connection against,
-- and queues 'KillPeer' too, in the same transaction, for those that watch
-- the mailbox. It used to queue 'KillPeer' only, which waited behind every
-- message queued before it, and forever behind a write to a peer that
-- stopped reading.
killPeer :: Peer -> IO ()
killPeer p = atomically $ do
  writeTVar p.closing True
  KillPeer `sendSTM` p.mailbox

-- | Send a network message to peer. Returns as soon as the message is queued.
--
-- The model of the peer judges the message as it is queued, in the same
-- transaction, so that the model sees the messages in the order in which
-- they go out ("Haskoin.Node.Peer.Remote"). A message that the peer would
-- punish the node for is not sent; the peer process logs it.
sendMessage :: Message -> Peer -> IO ()
sendMessage m p = do
  f <- p.facts m
  void (atomically (queueMessage f m p))

-- | Judge a message with the given facts and queue it, in one transaction.
-- Gives whether it is sent: 'False' for a message that the peer would punish
-- the node for, which goes to the process as 'NotSent'. The model clamps the
-- time of the facts to the latest it has seen, so facts read before the
-- transaction cannot make its clock go back.
queueMessage :: Facts -> Message -> Peer -> STM Bool
queueMessage f m p = do
  (j, r) <- judge f m <$> readTVar p.remote
  case j of
    Refused _ _ -> NotSent m j `sendSTM` p.mailbox >> return False
    _ -> writeTVar p.remote r >> SendMessage m `sendSTM` p.mailbox >> return True

-- | Try to reserve a peer for a long conversation, such as syncing headers
-- or downloading blocks. Returns 'True' if the peer was free and the owner now
-- holds it, or 'False' if anyone holds it, including the same owner.
--
-- Two tasks holding such a conversation with the same peer at once would
-- confuse each other's replies. "Haskoin.Node.Peer.Lock" has the rules.
reservePeer :: Owner -> Peer -> IO Bool
reservePeer o p = atomically $ do
  lock <- readTVar p.busy
  case acquire o lock of
    Just lock' -> writeTVar p.busy lock' >> return True
    Nothing -> return False

-- | Release a peer. Returns 'False', and changes nothing, if the owner does not
-- hold it, so that a task cannot free a peer in the middle of another task's
-- conversation.
releasePeer :: Owner -> Peer -> IO Bool
releasePeer o p = atomically $ do
  lock <- readTVar p.busy
  case release o lock of
    Just lock' -> writeTVar p.busy lock' >> return True
    Nothing -> return False

-- | Who holds a peer, if anyone.
peerHolder :: Peer -> IO (Maybe Owner)
peerHolder p = holder <$> readTVarIO p.busy

-- | Check whether a peer is reserved by anyone.
getBusy :: Peer -> IO Bool
getBusy p = isJust <$> peerHolder p

-- | Reserve a peer on behalf of a single owner shared by every caller of this
-- function. Use 'reservePeer' with an owner from 'newOwner' instead.
setBusy :: Peer -> IO Bool
setBusy = reservePeer legacyOwner
{-# DEPRECATED setBusy "Use reservePeer with an owner from newOwner" #-}

-- | Release a peer reserved with 'setBusy'. Peers reserved through
-- 'reservePeer' by another owner are left alone.
setFree :: Peer -> IO ()
setFree = void . releasePeer legacyOwner
{-# DEPRECATED setFree "Use releasePeer with the owner that reserved the peer" #-}

-- | Request full blocks from peer. Will return 'Nothing' if the list of blocks
-- returned by the peer is incomplete, comes out of order, or a timeout is
-- reached.
--
-- The 'Int' argument is the timeout in seconds. On networks with SegWit,
-- blocks are requested with their witness data.
--
-- Every block returned has passed 'blockMutation': its transactions hash to
-- the Merkle root of its header, and, when the node knows its parent, its
-- witness data matches the commitment in its coinbase. A peer that sends a
-- block that fails is disconnected, and the request then gives 'Nothing' as
-- soon as its disconnection is published (or at its timeout, for a peer that
-- no peer manager runs, which publishes none). The blocks are not validated otherwise: their
-- transactions may still be invalid.
getBlocks ::
  Network ->
  Int ->
  Peer ->
  [BlockHash] ->
  IO (Maybe [Block])
getBlocks net time p bhs =
  (>>= traverse block) <$> getData time p (GetData ivs)
  where
    block = either (const Nothing) Just
    c
      | net.segWit = InvWitnessBlock
      | otherwise = InvBlock
    ivs = map (InvVector c . (.get)) bhs

-- | Request transactions from peer. Will return 'Nothing' if the list of
-- transactions returned by the peer is incomplete, comes out of order, or a
-- timeout is reached.
--
-- The 'Int' argument is the timeout in seconds. Peers normally only serve
-- transactions that are in their mempool.
--
-- A transaction is matched by its id, which does not cover its witness. The
-- peer can return the right transaction with any witness data, and nothing
-- here can tell. Treat the witness of a transaction from 'getTxs', or from an
-- 'MTx' event, as unauthenticated until the transaction is found in a block.
getTxs ::
  Network ->
  Int ->
  Peer ->
  [TxHash] ->
  IO (Maybe [Tx])
getTxs net time p ths =
  (>>= traverse tx) <$> getData time p (GetData ivs)
  where
    tx = either Just (const Nothing)
    c
      | net.segWit = InvWitnessTx
      | otherwise = InvTx
    ivs = map (InvVector c . (.get)) ths

-- | Request transactions and/or blocks from peer. Return 'Nothing' if any
-- single inventory fails to be retrieved, if they come out of order, if the
-- peer disconnects, or if timeout is reached.
--
-- The first argument is the timeout in seconds, which counts from the call,
-- including any wait for an earlier request on the same peer. Each item is
-- judged by the model of the peer first ("Haskoin.Node.Peer.Remote"): if the
-- peer would refuse the request, or drop one of its items without an answer,
-- the request gives 'Nothing' at once. A ping is sent right after the
-- request; since peers answer in order, receiving the matching pong before
-- all items means that the peer will not send the rest. Unrelated messages
-- that arrive meanwhile are ignored, and so are blocks, transactions and
-- notfound messages for items that the request did not ask for.
-- Transactions are returned as 'Left' and blocks as 'Right', in the order
-- they were requested. "Haskoin.Node.Peer.Request" has the rules. Blocks are
-- checked as 'getBlocks' says, and transactions as 'getTxs' says.
--
-- Requests on one peer run one at a time, under a lock of the peer: two at
-- once would each take the other's blocks for its own and fail. A request
-- that ended before the pong of its ping came back leaves that nonce behind,
-- and the next request ignores what the peer sends until that pong, which
-- answers the earlier request. A request ends as soon as
-- 'PeerDisconnected' comes for its peer, and at once if the peer is
-- closing when it starts.
getData ::
  Int -> Peer -> GetData -> IO (Maybe [Either Tx Block])
getData seconds p gd@(GetData ivs) = do
  whole <- judgeMessage p (MGetData gd)
  items <- mapM (judgeMessage p . MGetData . GetData . pure) ivs
  if all (== Allowed) (whole : items)
    then fmap join . timeout (seconds * 1000 * 1000) . withRequestLock p $
      withSubscription p.pub $ \inb ->
        readTVarIO p.closing >>= \case
          True -> return Nothing
          False -> do
            r <- randomIO
            let request = MGetData gd
                ping = MPing (Ping r)
            fr <- p.facts request
            fp <- p.facts ping
            stale <- atomically $ do
              stale <- readTVar p.owed
              _ <- queueMessage fr request p
              sent <- queueMessage fp ping p
              when sent $ writeTVar p.owed (Just r)
              return stale
            step inb (newRequest stale r ivs)
    else return Nothing
  where
    step inb = \case
      Complete items -> return (Just items)
      Failed _ -> return Nothing
      Continue req -> filterReceive p inb >>= step inb . stepRequest req

-- | Run a request while holding the request lock of a peer.
withRequestLock :: Peer -> IO a -> IO a
withRequestLock p =
  bracket_ (atomically (takeTMVar p.requestLock)) (atomically (putTMVar p.requestLock ()))

-- | Ping a peer and await response. Return 'False' if response not received
-- before timeout, or at once if the peer disconnects or is closing. The
-- first argument is the timeout in seconds.
pingPeer :: Int -> Peer -> IO Bool
pingPeer time p =
  withSubscription p.pub $ \sub ->
    readTVarIO p.closing >>= \case
      True -> return False
      False -> do
        r <- randomIO
        MPing (Ping r) `sendMessage` p
        fmap (== Just True) . receiveMatchS time sub $ \case
          PeerMessage p' (MPong (Pong r'))
            | p == p' && r == r' -> Just True
          PeerDisconnected p'
            | p == p' -> Just False
          _ -> Nothing

-- | Receive the next message from the given peer, or its disconnection,
-- discarding events about other peers.
filterReceive :: Peer -> Inbox PeerEvent -> IO RequestInput
filterReceive p inb =
  receive inb >>= \case
    PeerMessage p' m | p == p' -> return (Received m)
    PeerDisconnected p' | p == p' -> return Disconnected
    _ -> filterReceive p inb