haskoin-node 2.6.0 → 2.6.1
raw patch · 7 files changed
+101/−27 lines, 7 filesdep ~haskoin-corePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: haskoin-core
API changes (from Hackage documentation)
+ Haskoin.Node.Chain: chainThen :: Chain -> STM () -> IO ()
+ Haskoin.Node.Peer: peerMessageDoneSTM :: Peer -> STM ()
+ Haskoin.Node.PeerMgr: peerMgrThen :: PeerMgr -> STM () -> IO ()
Files
- CHANGELOG.md +22/−1
- README.md +5/−1
- haskoin-node.cabal +2/−2
- src/Haskoin/Node.hs +48/−22
- src/Haskoin/Node/Chain.hs +11/−0
- src/Haskoin/Node/Peer.hs +6/−1
- src/Haskoin/Node/PeerMgr.hs +7/−0
CHANGELOG.md view
@@ -4,7 +4,28 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -## [Unreleased]+## [2.6.1] - 2026-11-18++### Fixed+- **Headers no longer hold up the messages of other peers.** The loop that+ routes peer messages waited for the chain to import each `headers` message.+ On Bitcoin Cash that took 20 seconds per message, and in that time no+ message of any peer reached the application: blocks and pongs piled up, and+ Haskoin Store took peers that were delivering for stalled ones. The loop now+ hands headers and inventory to the chain and goes on. The rest of the+ handling of that message, publishing it to the application and counting it+ as handled for the peer's back-pressure, runs when the chain is done with+ it. Ping, pong and address messages go to the peer manager the same way.+ The application now gets a peer's header message after the chain has its+ headers, and other messages of the same peer may reach it before.+- Requires haskoin-core 2.0.1, which connects Bitcoin Cash headers about 60+ times as fast.++### Added+- `chainThen` and `peerMgrThen` run an action once the chain or the peer+ manager has handled every message sent to it before, without waiting.+- `peerMessageDoneSTM`: `peerMessageDone` in `STM`.+ ## [2.6.0] - 2026-11-15
README.md view
@@ -392,7 +392,11 @@ A peer may have at most 16 messages published and not yet handled by the node. Past that, the node stops reading its socket, so a fast peer fills its own TCP-window instead of the node's memory. The node does not wait for your+window instead of the node's memory. A header or inventory message counts as+handled once the chain has dealt with it, and reaches your subscriber then. The+node hands it to the chain and goes on with the messages of the other peers, so+a peer that is still delivering blocks is never held up by the headers that+another peer sent. The node does not wait for your subscriber, though: a subscription is an unbounded queue, and one that falls behind holds every event it has not read. Keep reading events, and hand slow work to another thread.
haskoin-node.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: haskoin-node-version: 2.6.0+version: 2.6.1 synopsis: P2P library for Bitcoin and Bitcoin Cash description: Connect to Bitcoin (BTC) and Bitcoin Cash (BCH) peers, keep them healthy, and sync block headers into a RocksDB database. Blocks and transactions can be@@ -68,7 +68,7 @@ , conduit-extra ==1.3.* , containers >=0.6 && <0.8 , fast-logger ==3.2.*- , haskoin-core >=2.0.0 && <2.1+ , haskoin-core >=2.0.1 && <2.1 , network >=3.1 && <3.3 , nqe >=0.7.0 && <0.8 , random >=1.2 && <1.4
src/Haskoin/Node.hs view
@@ -138,6 +138,8 @@ where import Control.Concurrent.Async (link, withAsync)+import Control.Concurrent.STM (atomically)+import Data.Functor ((<&>)) import Data.Time.Clock (NominalDiffTime) import Database.RocksDB (ColumnFamily, DB) import Haskoin@@ -226,6 +228,25 @@ -- being disconnected. Inventory with blocks goes to the chain, which asks for -- headers if it does not know them. --+-- This loop never waits for the chain. The chain can take seconds over a+-- batch of headers, and a loop that waited for it held up the messages of+-- every peer: blocks and pongs piled up behind the headers, and the+-- application took peers that were delivering for stalled ones. A header or+-- inventory message is instead handed to the chain with the rest of its+-- handling, which runs when the chain is done with it (see 'chainThen'):+-- it is published to the application then, and only then counts as handled+-- for the peer's allowance. The backpressure still holds for the peer that+-- sent it, the chain's mailbox holds at most 'maxPendingMessages' messages of+-- each peer, and the messages of the other peers keep flowing. The chain+-- handles its mailbox in order, so the headers of a peer are imported in the+-- order they came. The application gets a header message only once the+-- chain has its headers, so it can look them up; other messages of the same+-- peer may reach it before.+--+-- Messages for the peer manager, which is quick, are handled the same way,+-- except @version@ and @verack@: the loop waits for those, since whether the+-- next message of that peer is taken depends on them.+-- -- Messages reach the application only from online peers, except @version@ -- and @verack@, which are the handshake itself. Bitcoin Core ignores -- anything else a peer sends before the handshake is done, and so does the@@ -238,37 +259,42 @@ case event of PeerConnected p -> do chainPeerConnected ch p- forward event+ atomically (forward event) PeerDisconnected p -> do chainPeerDisconnected ch p- forward event+ atomically (forward event) PeerMessage p m -> do+ ticklePeer mgr p+ online <- isOnline p+ let done+ | handshake m || online = forward event >> peerMessageDoneSTM p+ | otherwise = peerMessageDoneSTM p+ now = atomically done+ later f = f done case m of MVersion v ->- peerMgrVersion mgr p v >> peerMgrSync mgr+ peerMgrVersion mgr p v >> peerMgrSync mgr >> now MVerAck ->- peerMgrVerAck mgr p >> peerMgrSync mgr+ peerMgrVerAck mgr p >> peerMgrSync mgr >> now MPing (Ping n) ->- peerMgrPing mgr p n >> peerMgrSync mgr+ peerMgrPing mgr p n >> later (peerMgrThen mgr) MPong (Pong n) ->- peerMgrPong mgr p n >> peerMgrSync mgr- MAddr (Addr ns) ->- whenOnline p $ peerMgrAddrs mgr p (map snd ns) >> peerMgrSync mgr- MHeaders (Headers hs) ->- whenOnline p $ chainHeaders ch p (map fst hs) >> chainSync ch- MInv (Inv ivs) -> case [BlockHash iv.invHash | iv <- ivs, isBlock iv] of- [] -> return ()- bhs -> whenOnline p $ chainBlocksAnnounced ch p bhs >> chainSync ch- _ -> return ()- ticklePeer mgr p- if handshake m then forward event else whenOnline p (forward event)- peerMessageDone p+ peerMgrPong mgr p n >> later (peerMgrThen mgr)+ MAddr (Addr ns)+ | online -> peerMgrAddrs mgr p (map snd ns) >> later (peerMgrThen mgr)+ MHeaders (Headers hs)+ | online -> chainHeaders ch p (map fst hs) >> later (chainThen ch)+ MInv (Inv ivs)+ | online,+ bhs@(_ : _) <- [BlockHash iv.invHash | iv <- ivs, isBlock iv] ->+ chainBlocksAnnounced ch p bhs >> later (chainThen ch)+ _ -> now where- forward event = publish (PeerEvent event) output- whenOnline p act =- getOnlinePeer mgr p >>= \case- Just o | o.online -> act- _ -> return ()+ forward event = publishSTM (PeerEvent event) output+ isOnline p =+ getOnlinePeer mgr p <&> \case+ Just o -> o.online+ _ -> False isBlock iv = iv.invType `elem` [InvBlock, InvWitnessBlock] handshake = \case MVersion _ -> True
src/Haskoin/Node/Chain.hs view
@@ -92,6 +92,7 @@ chainHeaders, chainBlocksAnnounced, chainSync,+ chainThen, ) where @@ -577,3 +578,13 @@ -- | Wait until the chain process has handled every message sent to it before. chainSync :: Chain -> IO () chainSync ch = ChainSyncMsg `query` ch.mailbox++-- | Run an action once the chain process has handled every message sent to+-- it before, without waiting for that. "Haskoin.Node" uses it to finish a+-- peer's headers or inventory message only when the chain has dealt with it,+-- so that the backpressure of that one peer holds while the messages of all+-- the other peers keep flowing. 'chainSync' would block the caller for as+-- long as the chain is busy, and the chain can be busy for seconds while it+-- imports headers.+chainThen :: Chain -> STM () -> IO ()+chainThen ch act = ChainSyncMsg (const act) `send` ch.mailbox
src/Haskoin/Node/Peer.hs view
@@ -69,6 +69,7 @@ setBusy, setFree, peerMessageDone,+ peerMessageDoneSTM, maxPendingMessages, ) where@@ -226,7 +227,11 @@ -- | Tell the peer process that the node has finished handling one of the -- messages that it published, so that it may read another one. peerMessageDone :: Peer -> IO ()-peerMessageDone p = atomically $ modifyTVar' p.pending (max 0 . subtract 1)+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 -- 'wrapPeer' built with the mailbox of this inbox.
src/Haskoin/Node/PeerMgr.hs view
@@ -89,6 +89,7 @@ peerMgrAddrs, peerMgrVerAck, peerMgrSync,+ peerMgrThen, getPeers, getOnlinePeer, ticklePeer,@@ -627,6 +628,12 @@ -- | Wait until the peer manager has handled every message sent to it before. peerMgrSync :: PeerMgr -> IO () peerMgrSync mgr = Sync `query` mgr.mailbox++-- | Run an action once the peer manager has handled every message sent to it+-- before, without waiting for that. The non-blocking form of 'peerMgrSync'+-- (see 'Haskoin.Node.Chain.chainThen').+peerMgrThen :: PeerMgr -> STM () -> IO ()+peerMgrThen mgr act = Sync (const act) `send` mgr.mailbox -- | Median of a list, or 'Nothing' if it is empty. median :: (Ord a, Fractional a) => [a] -> Maybe a