haskoin-node: P2P library for Bitcoin and Bitcoin Cash
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 downloaded from connected peers. See the README on GitHub at https://github.com/jprupp/haskoin-node#readme.
[Skip to Readme]
Modules
[Index] [Quick Jump]
Downloads
- haskoin-node-2.7.0.tar.gz [browse] (Cabal source package)
- Package description (as included in the package)
Maintainer's Corner
For package maintainers and hackage trustees
Candidates
- No Candidates
| Versions [RSS] | 0.2.0, 0.3.0, 0.3.1, 0.4.0, 0.4.1, 0.4.2, 0.5.0, 0.5.1, 0.5.2, 0.6.0, 0.6.1, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.8.0, 0.8.1, 0.8.2, 0.9.0, 0.9.1, 0.9.2, 0.9.3, 0.9.4, 0.9.5, 0.9.6, 0.9.7, 0.9.8, 0.9.9, 0.9.10, 0.9.11, 0.9.12, 0.9.13, 0.9.14, 0.9.15, 0.9.16, 0.9.17, 0.9.18, 0.9.19, 0.9.20, 0.9.21, 0.10.0, 0.10.1, 0.11.0, 0.11.1, 0.11.2, 0.11.3, 0.12.0, 0.13.0, 0.14.0, 0.14.1, 0.15.0, 0.16.0, 0.17.0, 0.17.1, 0.17.2, 0.17.9, 0.17.10, 0.17.11, 0.17.12, 0.17.13, 0.17.14, 0.18.0, 0.18.1, 0.19.0, 1.0.0, 1.0.1, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.2.0, 1.3.0, 1.3.1, 1.4.0, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 2.0.0, 2.1.0, 2.1.1, 2.1.2, 2.2.0, 2.3.0, 2.3.1, 2.4.0, 2.5.0, 2.5.1, 2.6.0, 2.6.1, 2.7.0 |
|---|---|
| Change log | CHANGELOG.md |
| Dependencies | async (>=2.2 && <2.3), base (>=4.20 && <5), bytestring (>=0.11 && <0.13), cereal (>=0.5 && <0.6), conduit (>=1.3 && <1.4), conduit-extra (>=1.3 && <1.4), containers (>=0.6 && <0.8), fast-logger (>=3.2 && <3.4), haskoin-core (>=3.0 && <3.1), network (>=3.1 && <3.3), nqe (>=0.7.0 && <0.8), random (>=1.2 && <1.4), rocksdb-haskell-jprupp (>=2.6.1 && <2.9), rocksdb-query (>=0.6.1 && <0.8), stm (>=2.5 && <2.6), streaming-commons (>=0.2 && <0.3), string-conversions (>=0.4 && <0.5), text (>=2.0 && <2.2), time (>=1.12 && <1.15), transformers (>=0.6 && <0.7) [details] |
| Tested with | ghc ==9.12.4 |
| License | MIT |
| Copyright | 2020 Haskoin Developers |
| Author | JP Rupp |
| Maintainer | jprupp@protonmail.ch |
| Uploaded | by jprupp at 2026-11-25T23:58:12Z |
| Category | Bitcoin, Finance, Network |
| Home page | https://github.com/jprupp/haskoin-node#readme |
| Bug tracker | https://github.com/jprupp/haskoin-node/issues |
| Source repo | head: git clone https://github.com/jprupp/haskoin-node |
| Distributions | Stackage:2.7.0 |
| Reverse Dependencies | 2 direct, 1 indirect [details] |
| Downloads | 31598 total (175 in the last 30 days) |
| Rating | (no votes yet) [estimated by Bayesian average] |
| Your Rating |
|
| Status | Docs uploaded by user Build status unknown [no reports yet] |
Readme for haskoin-node-2.7.0
[back to package description]Haskoin Node
Haskoin Node is a peer-to-peer networking library for Bitcoin (BTC) and Bitcoin Cash (BCH), written in Haskell. It connects to the network, keeps a pool of healthy peers, and syncs block headers into a RocksDB database.
It does not download or validate blocks and transactions by itself. It publishes everything that happens as events, and gives you the peers to ask for the data you need. It is the networking layer of Haskoin Store.
Contents
- Why Haskoin Node
- Design principles
- Features
- Installation
- Quick start
- Tutorial: a block watcher
- Concepts
- Usage guide
- Configuration
- Module overview
- Security notes
- Development
- Related projects
- License
Why Haskoin Node
A program that wants Bitcoin data has two usual options. It can run a full node such as Bitcoin Core next to it and talk to its RPC interface, which means operating a second large piece of software. Or it can speak the peer-to-peer protocol itself, which means solving the same problems every node solves: finding peers, keeping good ones, dropping bad ones, parsing untrusted messages safely, and following the header chain through reorganisations.
Haskoin Node solves those problems once, as a library. It gives a Haskell program a direct, well-behaved presence on the network, and stops there: what to download, what to validate and what to store is up to the program. It is the networking layer of Haskoin Store, which indexes the whole Bitcoin and Bitcoin Cash block chains this way without a full node.
Design principles
- Do one job. Keep connected and keep the header chain current. Blocks, transactions, validation and storage of anything but headers belong to the application, because every application wants something different.
- Headers are the source of truth. Headers are small (80 bytes) and carry the proof of work. With them, the node knows which chain is real without downloading blocks, and can tell the application which blocks to fetch.
- Tell, don't ask. Everything the node learns becomes an event on a publisher. The application subscribes and reacts; the node never needs to know who is listening.
- Peers are strangers. Message sizes are checked before payloads are read, a payload costs time linear in its size however it is split, anything invalid ends the connection, a block must hold the transactions its header commits to, relayed addresses are filtered and spread over network groups, and the node reads a peer only as fast as it handles its messages. Your subscribers are not throttled: one that falls behind holds memory.
- Prefer good peers, but keep moving. Peers are ranked by measured latency so the fastest are used first, and every connection is eventually replaced so the node never depends on the same few peers forever.
- Small, isolated components. The peer manager, the header chain and each connection are separate NQE processes with their own state. One misbehaving peer dies alone.
- Explicit protocol states. What the node expects from each peer, during the handshake, while syncing headers and while receiving requested data, is written down as a pure state machine. A message that does not fit the current state is refused instead of handled by accident, and every rule is tested without a network.
- Races ruled out by construction. Every concurrent protocol, from the threads of one connection to the routing of events, is a pure state machine; the threads only carry out what it decides. Each piece of state has one owner, or changes in one STM transaction per decision, or keeps its order through a single queue. docs/concurrency.md says which, protocol by protocol.
- The rules of the other node, not guesses. Bitcoin Core and Bitcoin Cash Node decide what a peer may send, and much of it is in no BIP. Every message to a peer is first judged by a model of the node at the other end, the state machines that haskoin-core translates from the source of both nodes. A message that the node would punish is not sent, and the log cites the upstream line of the rule. See What a peer allows.
- Plain
IO. Every function runs inIO, with no monad transformers and noMonadIOorMonadUnliftIOconstraints. Types say exactly what runs, GHC has nothing to specialise, and long-running loops cannot leak through an unspecialised dictionary. From your own monad, call the node withliftIO. - Keep moving. Every hint that a peer has blocks the node lacks makes it ask that peer for headers, at once even while another peer syncs, and a quiet period makes it ask every peer, so a missed announcement cannot leave the chain behind. A syncing peer loses its turn when it sends nothing new, and when it runs past the deadline that Bitcoin Core gives its initial headers sync.
Features
| Area | What you get |
|---|---|
| Networks | Every network of haskoin-core: Bitcoin and Bitcoin Cash mainnet, testnets and regtest |
| Peer discovery | DNS seeds and addresses relayed by peers, or a static list of peers |
| Peer management | Version handshake, ping-based latency, peers sorted by median ping, rotation after a maximum age, retry backoff and bans for bad addresses, random choice among learned addresses with one connection per network group; configured peers kept: never rotated, tried again within ten seconds for ever, with a slot kept for each while it is away |
| Header sync | Headers downloaded from one peer at a time, with a deadline per turn, connected to the chain with the most work, stored in RocksDB; new blocks followed whether peers announce them with headers or inventory, announcing peers asked at once, and peers polled after a quiet period |
| Events | Best header changes, sync completion, peer connections and every message received |
| Data requests | Fetch blocks and transactions from a peer, with a timeout; every block checked against its header's Merkle root and witness commitment |
| Protocol model | Every outgoing message judged by a model of the node at the other end (Bitcoin Core 32.1 or Bitcoin Cash Node 29.2.0, from haskoin-core): what the node would punish is not sent, and a request it would drop fails at once |
| Protection | Strict parsing, per-command size limits, linear-time reads, back-pressure, bounded address pool, explicit handshake states, connect timeout |
What is not included
- No block or transaction validation, and no mempool. Haskoin Store does this on top of the events.
- No inbound connections: the node only connects out.
- No compact blocks, Bloom filters (BIP-37) or compact block filters (BIP-157).
Installation
System libraries
Haskoin Node needs RocksDB and
libsecp256k1 0.2.0 or later, with
pkg-config to find them.
# Debian and Ubuntu
sudo apt-get install librocksdb-dev libsecp256k1-dev pkg-config
# Fedora
sudo dnf install rocksdb-devel libsecp256k1-devel pkgconf
# macOS with Homebrew
brew install rocksdb secp256k1 pkg-config
# Nix
nix-shell -p rocksdb secp256k1 pkg-config
Some older distributions ship a libsecp256k1 below 0.2.0. See the haskoin-core installation notes for how to build it.
Check the libraries are found
pkg-config --modversion libsecp256k1 # 0.2.0 or later
ls /usr/include/rocksdb/c.h # or $(brew --prefix)/include/rocksdb/c.h
Compiler
It needs GHC 9.2 or later, because it uses OverloadedRecordDot. It is built
and tested with the GHC of the Stackage nightly in stack.yaml.
With Cabal
build-depends:
base
, data-default
, haskoin-core
, haskoin-node
, nqe
, rocksdb-haskell-jprupp
You open the database with rocksdb-haskell-jprupp and create the event
publisher with nqe.
With Stack
Recent Stackage nightly snapshots include Haskoin Node. With an older resolver,
add it and its dependencies to stack.yaml:
extra-deps:
- haskoin-node-2.6.0
- haskoin-core-2.0.0
- secp256k1-haskell-3.0.0
- base16-2.0
- rocksdb-haskell-jprupp-2.8.0
- rocksdb-query-0.7.0
- nqe-0.7.0
Step by step with Stack
-
Install Stack and the libraries above.
-
Create a project:
stack new node-demo simple cd node-demo -
Add the packages listed under With Cabal to
build-dependsinnode-demo.cabal, plusnetwork. -
If your resolver is older than the packages, add the
extra-depsshown under With Stack tostack.yaml. -
Put the quick start program in
src/Main.hsand run it:stack runWithin a few seconds it has peers and starts printing header heights. A full header sync of Bitcoin usually takes minutes and on the order of a hundred megabytes of disk; it resumes where it stopped if you interrupt it.
Quick start
This program connects to Bitcoin, syncs headers, and prints what happens:
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedRecordDot #-}
import Control.Monad (forever)
import Data.Default (def)
import Database.RocksDB (Config (..), withDB)
import Haskoin (BlockNode (..), NetworkAddress (..), btc, sockToHostAddress)
import Haskoin.Node
import NQE (receive, withPublisher, withSubscription)
import Network.Socket (SockAddr (..))
main :: IO ()
main =
withLogger LevelInfo $ \logger ->
withDB "/tmp/haskoin-node" def {createIfMissing = True} $ \db ->
withPublisher $ \pub ->
withSubscription pub $ \events -> do
let cfg =
NodeConfig
{ maxPeers = 8,
db = db,
cf = Nothing,
peers = [],
discover = True,
address = NetworkAddress 0 (sockToHostAddress (SockAddrInet 0 0)),
net = btc,
pub = pub,
timeout = 120,
maxPeerLife = 48 * 3600,
connect = withConnection,
logger = logger,
permissions = mempty
}
withNode cfg $ \_node -> forever $
receive events >>= \case
ChainEvent (ChainBestBlock bn) ->
putStrLn ("Best header: " <> show bn.height)
ChainEvent (ChainSynced _) ->
putStrLn "Headers in sync"
PeerEvent (PeerConnected p) ->
putStrLn ("Connected: " <> show p)
_ -> return ()
Subscribe to the publisher before calling withNode, as here, or you may miss
the first events.
Tutorial: a block watcher
This builds a program that prints every new Bitcoin block with its number of transactions. It shows the intended division of labour: the node follows headers and tells you about them, and you fetch what you need.
1. Wait until the headers are synced
At start-up the node downloads hundreds of thousands of headers, each one a
ChainBestBlock event. Reacting to each of those would mean fetching every
historical block. Wait for ChainSynced first:
waitSynced :: Inbox NodeEvent -> IO ()
waitSynced events =
receive events >>= \case
ChainEvent (ChainSynced _) -> return ()
_ -> waitSynced events
2. React to new best headers
After that, each ChainBestBlock is a new block at the tip, or the new tip
after a reorganisation:
watch :: Network -> Node -> Inbox NodeEvent -> IO ()
watch net node events = forever $
receive events >>= \case
ChainEvent (ChainBestBlock bn) -> fetch net node bn
_ -> return ()
3. Fetch the block from the fastest peer
getPeers lists online peers, fastest first. Ask one, and try the next if it
fails or times out:
fetch :: Network -> Node -> BlockNode -> IO ()
fetch net node bn = getPeers node.peerMgr >>= go
where
hash = headerHash bn.header
go [] = putStrLn ("No peer could send block " <> show bn.height)
go (op : ops) =
getBlocks net 30 op.mailbox [hash] >>= \case
Just [b] ->
putStrLn (show bn.height <> ": " <> show (length b.txs) <> " transactions")
_ -> go ops
4. Put it together
Use the configuration from the quick start, and run the watcher
inside withNode. Besides the imports of the quick start, the watcher needs
Block (..), Network and headerHash from Haskoin, and Inbox from NQE:
withNode cfg $ \node -> do
waitSynced events
watch btc node events
What the node did for you
While this runs, the node keeps eight connections, measures and ranks them, drops silent or misbehaving peers and finds replacements, stores headers so a restart is quick, and switches to a heavier chain if one appears. The watcher is only twenty lines because none of that is its concern.
Concepts
Components
withNode starts two processes, built with NQE:
- The peer manager (
Haskoin.Node.PeerMgr) finds peer addresses, connects to them, does the version handshake, pings them, and drops those that are slow, silent, misbehaving or too old. Configured peers are tried first; learned addresses are picked at random, at most one per network group. An address whose connection failed is tried again later each time, and one whose peer misbehaved waits a day. - The chain (
Haskoin.Node.Chain) asks one peer at a time for headers, connects them to the headers it has, follows the chain with the most work, and stores the headers in RocksDB. It asks a peer for headers whenever the peer announces blocks it does not know, with headers or with inventory, at once if another peer holds the turn, and asks every peer when it has heard nothing fortimeoutseconds. A turn ends when the peer has nothing more, sends a full batch with nothing new, or runs past its deadline.
The rules for talking to peers are pure state machines, each in its own module:
| Module | Rules |
|---|---|
Haskoin.Node.PeerMgr.Lifecycle |
Life of a connection: handshake, pings, timers, disconnection |
Haskoin.Node.PeerMgr.Handshake |
The version handshake, the first stage of a connection |
Haskoin.Node.PeerMgr.Addresses |
Which address to try next, and when to retry one |
Haskoin.Node.Chain.Sync |
Which peer to ask for headers, and when headers count as synced |
Haskoin.Node.Peer.Request |
Receiving requested blocks and transactions |
Haskoin.Node.Peer.Lock |
Who may hold a peer for a long conversation |
Haskoin.Node.Router |
The order of each peer's events: announced before its messages |
The processes store the state and carry out what a transition says. Time is an input to the transitions, so every timing rule is tested without waiting. docs/concurrency.md has, for every thread, what it owns and why no two threads get in each other's way.
Haskoin.Node.Block is a pure check of another kind: whether the
transactions of a block are the ones its header commits to. Every block a peer
sends passes through it.
Each peer connection is its own process (Haskoin.Node.Peer), run by a
supervisor inside the peer manager.
Events
Everything is published to the Publisher NodeEvent in the configuration:
| Event | When |
|---|---|
ChainEvent (ChainBestBlock node) |
The best header changed, including on start |
ChainEvent (ChainSynced node) |
Headers caught up with the network (once) |
PeerEvent (PeerConnected peer) |
A peer finished the handshake |
PeerEvent (PeerDisconnected peer) |
An online peer went away |
PeerEvent (PeerMessage peer msg) |
An online peer sent a message: MInv, MBlock, MTx and so on |
Messages are published only from peers that are online, except version and
verack, which are the handshake itself. For each peer, PeerConnected comes
before any other message of the peer, and PeerDisconnected after all of
them, even when the peer sends right behind its handshake: the node holds such
messages until the announcement has gone out (Haskoin.Node.Router). 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 its parent.
Nothing else about it is checked, and a transaction in an MTx message is not
checked at all.
"In sync" means the best header is less than two hours old, has at least the minimum chain work of the network, and no peer is left to sync from.
Back-pressure
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. 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.
Database layout
Headers are stored in the column family you configure, or the default one:
| Key prefix | Value |
|---|---|
0x90 |
header (BlockNode) by hash |
0x91 |
best header |
0x92 |
data version |
You can keep your own data in the same database, as Haskoin Store does, as long
as your keys do not start with these bytes. Haskoin.Node.Chain.Database has
the code.
Usage guide
Picking a peer and requesting data
ps <- getPeers node.peerMgr -- online peers, fastest first
case ps of
op : _ -> do
let p = op.mailbox
mblocks <- getBlocks btc 30 p [blockHash] -- Maybe [Block]
mtxs <- getTxs btc 30 p [txHash] -- Maybe [Tx]
sendMessage MMempool p
[] -> return ()
getBlocks, getTxs and getData return Nothing if the peer does not send
everything, in order, before the timeout in seconds. Unrelated messages that the
peer sends in between are ignored. If the peer disconnects, they return
Nothing at once. Requests on one peer run one after the other, and the
timeout counts the wait: two at once would take each other's blocks for their
own. The late answers of a request that timed out do not disturb the next one.
pingPeer measures a round trip, and gives False at once if the peer
disconnects.
A block that getBlocks returns holds the transactions its header commits to.
A peer that sends another block under the requested header is disconnected,
and the request gives Nothing. The transactions are not
validated otherwise. A transaction from getTxs is matched by its id, which
does not cover its witness, so treat its witness data as unauthenticated until
you find the transaction in a block.
getPeers leaves out peers that are being disconnected, even before their
connection closes. killPeer closes a connection at once, without sending the
messages still queued for it, even when the peer has stopped reading.
For a conversation that spans many messages, such as downloading a long run of blocks, reserve the peer first so that no other task uses it at the same time. The header chain does the same while it syncs. Only the owner that reserved a peer can release it:
owner <- newOwner "downloader"
reserved <- reservePeer owner p
when reserved $ do
-- ... long conversation with p ...
void (releasePeer owner p)
setBusy and setFree still work, but are deprecated: they act for a single
owner shared by all their callers.
Querying headers
best <- chainGetBest node.chain
mnode <- chainGetBlock node.chain someHash
isMain <- chainBlockMain node.chain someHash
fork <- chainGetSplitBlock node.chain nodeA nodeB
parents <- chainGetParents node.chain height top
synced <- chainIsSynced node.chain
chainRunHeaders runs the header functions of haskoin-core, such as getAncestor,
against the database of the chain.
Static peers
cfg {peers = ["node1.example.com", "10.0.0.5:8333", "[2001:db8::1]:8333"], discover = False}
A peer without a port uses the default port of the network.
Addresses from DNS seeds must be public, like relayed addresses. On regtest the
seed of haskoin-core is localhost, which is not, so list local peers in
peers instead of relying on discovery.
Custom connections
connect decides how to open a connection. withConnection opens a plain TCP
socket, or a Unix socket for a peer written unix:PATH, and gives up if it
does not open within five seconds (connectTimeout, as in Bitcoin Core).
Replace it to go through a proxy, or to plug in scripted peers in tests, as the
test suite does.
Peers on Unix sockets
A peer in peers can be a Unix socket, written unix:PATH, as Bitcoin Core
writes the socket of a proxy. Neither Bitcoin Core nor Bitcoin Cash Node
listens on a Unix socket, so something joins the socket to a node: socat, or
the bridge of a test that runs its node in a container, such as
podman-test. Tests that run at once
then reach their nodes through sockets of their own, with no port of the
machine taken. The version message gives such a peer the zero address, as
Bitcoin Core does when it does not know one. A peer or a DNS seed can never
give the node a Unix address: relayed addresses are IPv4 or IPv6.
What a peer allows
Bitcoin Core and Bitcoin Cash Node punish a peer for things that no BIP
mentions. Ask either for its mempool without being whitelisted, and unless
it offers Bloom filters it closes the connection. haskoin-core translates
both nodes into pure state machines (Haskoin.Network.Protocol), and every
peer runs one, as a model of the node at the other end
(Haskoin.Node.Peer.Remote). Each message is judged on its way out:
Allowed: the node takes it, and answers it if it is a request.Unanswered: the node drops it without a word.getData,getBlocksandgetTxsgiveNothingat once rather than wait for their timeout.Refused: the node would add to this node's ban score, discourage it, ban it, or close the connection. The peer process does not send it, and logs a warning with the reason and the upstream line, such asnot sending cmd "mempool": the node would disconnect (ProtocolViolation): mempool request with bloom filters disabled (src/net_processing.cpp:5195).
To ask before sending, use judgeMessage:
judgeMessage peer MMempool >>= \case
Allowed -> MMempool `sendMessage` peer
verdict -> putStrLn ("Not asking: " <> T.unpack (judgementText verdict))
The model learns the options of the node from its version message: whether
it offers Bloom filters, whether it is pruned, whether it relays
transactions. What the node does not say is which permissions it gave this
node with -whitelist or -whitebind. If you run the nodes of peers, say
it in permissions:
cfg {peers = ["127.0.0.1:8335"], permissions = Set.fromList [PermMempool, PermNoBan]}
The permissions apply to the configured peers only, since a peer found through discovery has granted none.
The model knows the protocol exactly and the chain of the node only as well
as this node does. It takes the node to have every block it is asked for, so
it never fails a request on a guess that the node lacks a block. It also
does not refuse a message over a disconnection that depends on the state of
the node, such as an upload limit it reached. The haskoin-core report
doc/protocol
describes the rules of both nodes in prose.
Logging
withLogger level logs to standard error, one line per message, such as
[Info#Chain] Best block header at height 870000. Build a Logger with your
own push function to log elsewhere.
| Level | What it is for |
|---|---|
| Debug | Everything: each peer that connects or leaves, each message |
| Info | Milestones: the best block, headers in sync, peers again after none |
| Warn | Something unusual that the node dealt with: a misbehaving peer, no peers left |
| Error | Something wrong with this node. Nothing that a peer does is an error |
An alert on errors is then an alert about the node, not about the network.
Configuration
| Field | Meaning |
|---|---|
maxPeers |
Maximum number of peers to connect to |
db |
RocksDB handle for the headers |
cf |
Column family for the headers, or Nothing for the default one |
peers |
Static peers, as host, host:port, [ipv6]:port or unix:PATH |
discover |
Use DNS seeds and addresses relayed by peers |
address |
This node's address, sent in version messages |
net |
Network constants from haskoin-core (btc, bch, btcTest, ...) |
pub |
Publisher that receives every NodeEvent |
timeout |
Seconds of silence, without a pong, or without completing the handshake, before a peer is dropped; also how long the syncing peer may take to send headers, and the quiet period after which every peer is asked for headers |
maxPeerLife |
Seconds after which a peer is dropped, so that connections rotate. Not the peers of peers: a configured peer is kept |
connect |
How to open a connection; usually withConnection |
logger |
Where to log; see withLogger |
permissions |
Permissions that the nodes of peers grant this node with -whitelist or -whitebind, such as PermMempool; see What a peer allows |
Module overview
Haskoin.Node re-exports everything a program needs:
| Part | Contents |
|---|---|
| Node | NodeConfig, NodeEvent, Node, withNode, withConnection |
| Peers | Peer, PeerEvent, PeerException, sendMessage, judgeMessage, getBlocks, getTxs, getData, pingPeer, killPeer, newOwner, reservePeer, releasePeer, newPeer |
| Models of peers | Judgement, judgementText, expectsReject, Permission, Permissions |
| Blocks | blockMutation, BlockMutation, witnessCheck, WitnessCheck, segWitHeight |
| Peer manager | PeerMgr, OnlinePeer, PeerState, Stage, DisconnectReason, getPeers, getOnlinePeer, toSockAddr, isRoutable |
| Chain | Chain, ChainEvent, chainGetBest, chainGetBlock, chainGetAncestor, chainGetParents, chainGetSplitBlock, chainBlockMain, chainIsSynced, chainHasHeaders, chainRunHeaders |
| Logging | Logger, LogLevel, withLogger, withSource, logDebug ... logError |
The lower-level functions that feed messages to the peer manager and the chain are exported too, for programs that wire the components themselves.
Every module is exposed and can be imported on its own:
| Module | Purpose |
|---|---|
Haskoin.Node.Peer |
The process that talks to one peer, and data requests |
Haskoin.Node.Peer.Request |
State machine for receiving requested blocks and transactions |
Haskoin.Node.Peer.Remote |
The model of the node at the other end, which judges each message sent to it |
Haskoin.Node.Peer.Lock |
State machine for reserving a peer |
Haskoin.Node.Router |
State machine for the order of each peer's events |
Haskoin.Node.Block |
Whether a block holds the transactions its header commits to |
Haskoin.Node.PeerMgr |
Finds, connects, ranks and drops peers |
Haskoin.Node.PeerMgr.Lifecycle |
State machine for the life of a connection |
Haskoin.Node.PeerMgr.Handshake |
State machine for the version handshake |
Haskoin.Node.PeerMgr.Addresses |
State machine for the address book |
Haskoin.Node.Chain |
Keeps the header chain in sync |
Haskoin.Node.Chain.Sync |
State machine that decides which peer to ask for headers, and when the chain is synced |
Haskoin.Node.Chain.Database |
Storage of headers in RocksDB |
Haskoin.Node.Address |
Parsing peer addresses, network groups and opening connections |
Haskoin.Node.Logger |
Logging |
Security notes
- Every message header is checked before its payload is read. The payload limit
is the
maxPayloadSizeof the command on the network: 4,000,000 bytes on Bitcoin, and on Bitcoin Cash 2 MiB for everything but block messages. - Reading a payload costs time and memory linear in its size, however small the pieces it arrives in.
- List messages are refused as soon as their count is over the limit, such as 2000 headers, 1000 addresses, 50000 inventory entries or 101 locator hashes.
- A peer that sends anything invalid is disconnected, with a
PeerExceptionsaying why. - A peer that sends a block whose transactions are not the ones its header
commits to is disconnected (
MutatedBlock). The check is Bitcoin Core'sIsBlockMutated: the Merkle root, a tree that repeats transactions (CVE-2012-2459), and, once SegWit is active at the block's height, the witness commitment. The header itself is not marked invalid. When the node does not know the parent of a block, its witness data is not checked. - Messages from a peer that has not completed the handshake are not acted on or published, except the handshake messages.
- On Bitcoin, a peer must offer witness data (
NODE_WITNESS) as well as the full chain, as Bitcoin Core requires of its outbound peers. - A peer that sends a second version or verack, or does not complete the
handshake within
timeoutseconds even while sending other messages, is disconnected. - A peer that sends invalid headers, or does not answer a request for headers in time, is never asked for headers again before it disconnects.
- A syncing peer whose full batch of headers brings nothing new loses its turn. One that is still syncing after 15 minutes plus 1 millisecond for each block expected since the best header is disconnected when another peer can take over, as Bitcoin Core does.
- Addresses relayed by peers and returned by DNS seeds are only used if they are public unicast addresses, so a peer or a resolver cannot make the node connect to services on its own host or network. Static peers are not checked.
- Learned addresses are picked at random, and at most one connection is open to each network group (a /16 of IPv4, a /32 of IPv6), so one operator cannot take every slot. Static peers are exempt.
- At most 4096 relayed addresses and 4096 seed addresses are kept, apart, so gossip cannot crowd out the seeds. When the room for one is full, a new address replaces one that has failed to connect.
- A connection that does not open within five seconds is given up.
- A peer that stops reading cannot keep its slot: a connection told to close
closes at once, even with a write blocked, and the peer manager cancels the
process of one still there
timeoutseconds later. - An address whose connection fails waits twice as long after each failure before it is tried again, up to ten minutes. A relayed address whose peer broke the protocol is not tried again for a day. Static peers are never banned, and wait at most ten minutes.
- Headers are not reported as synced until they have the minimum chain work of the network. Above that work, forks from far below the tip on a chain with less work are refused.
- Below that work, during the initial sync, every valid header is stored. A peer can make the node store a fork of low difficulty, at the cost of its proof of work. Bitcoin Core first checks that a peer's headers reach the minimum work before it stores any; this node does not yet.
- The node trusts the chain with the most work. It checks that a block holds the transactions its header commits to, but does not validate the block: check the transactions you get from peers before relying on them.
Development
git clone https://github.com/jprupp/haskoin-node.git
cd haskoin-node
stack build # build the library
stack test # run the tests
stack haddock --no-haddock-deps # build the API documentation
On NixOS, or with stack --nix, the nix section of stack.yaml provides
RocksDB and libsecp256k1.
These commands build the library as it is released, with optimisation. For
the time between two commits there is a faster loop: haskell-dev, from
jprupp/tools, on your PATH.
haskell-dev build # build without optimisation
haskell-dev test PeerMgr # run the tests in several processes at once
haskell-dev docs # build the documentation and check its comments
haskell-dev test runs every test, those against real nodes too, in about
50 seconds. haskell-dev.toml tells it which spec modules are slow, so that it shares them between the
test processes. Do not mix the two kinds of build in one folder: the flags
differ, so each switch compiles everything again. Never time code with
haskell-dev.
package.yaml is the source of the package description. Change it rather than
haskoin-node.cabal, which hpack generates.
The package also builds with Cabal
(cabal build, cabal test).
The tests are one suite, spec, using hspec and
QuickCheck, and
stack test runs all of them:
-
The unit tests (
test/unit) test the state machines with properties, and run the peer, peer manager, chain and node against peers scripted in memory, which break the rules as no real node does: wrong magic, oversized messages, forged blocks, a secondverack. They need no Internet access, and name their peers by Unix sockets, never by a port. What an honest node does, the regtest tests show with real ones. -
The regtest tests (
Haskoin.Node.RegtestSpec) run the node against Bitcoin Core and Bitcoin Cash Node in regression test mode. The nodes run in a Podman container built fromtest/regtest/Containerfile, whose scripttest/regtest/install-nodes.shinstalls the official release binaries on Ubuntu after checking their SHA-256 hashes. The image is built the first time the tests run, which needs Internet access. The containers run through podman-test, each in a pod with no network: the peer-to-peer port of each node is bridged to a Unix socket of the pod, and the node under test connects to it as the peerunix:PATH. So runs never collide, and no port of the machine is taken. A peer that hangs is a node stopped withSIGSTOP. A container may use 2 GiB of memory. The tests are skipped if Podman is missing, or ifHASKOIN_NODE_SKIP_REGTESTorPODMAN_TEST_SKIPis set.stack test --ta '--match /Haskoin.Node.Regtest/' # only these HASKOIN_NODE_SKIP_REGTEST=1 stack test # all but these
Set HASKOIN_TEST_LOG to see the node's logs while tests run.
CI builds and tests every change with the OS libraries and with
stack --nix, through the workflow that the packages of this family share in
jprupp/tools, without the regtest tests,
and runs those in a job of its own. You can run its checks locally: check-stack-paths, from
jprupp/tools, fails if stack.yaml points at a package outside the
repository, which CI cannot build, and scripts/install-system-libraries.sh
installs the C libraries on Ubuntu.
Contributing
Pull requests are welcome.
- Format the code with Ormolu:
haskell-format, from jprupp/tools. - Add tests for the behaviour you change.
- Keep lines of code, including scripts, workflows and the examples in the documentation, shorter than 120 columns.
- Put multi-line scripts in their own files rather than embedding them in a workflow or a Containerfile.
- Describe the change under a new version in
CHANGELOG.md. The file follows Keep a Changelog, and versions follow Semantic Versioning.
Related projects
- haskoin-core has the Bitcoin data types and protocol that this library speaks.
- haskoin-store indexes blocks and transactions on top of this node and serves them over a REST API.
- nqe provides the processes, supervisors and publishers.
- rocksdb-query stores the headers.
License
Haskoin Node is released under the MIT License.