Adopting new bitcoin hash types and crate version
[rust-lightning] / ARCH.md
1 Rust-Lightning is broken into a number of high-level structures with APIs to hook them
2 together, as well as APIs for you, the user, to provide external data.
3
4 The two most important structures which nearly every application of Rust-Lightning will
5 need to use are `ChannelManager` and `ChannelMonitor`. `ChannelManager` holds multiple
6 channels, routes payments between them, and exposes a simple API to make and receive
7 payments. Individual `ChannelMonitor`s monitor the on-chain state of a channel, punish
8 counterparties if they misbehave, and force-close channels if they contain unresolved
9 HTLCs which are near expiration. The `ManyChannelMonitor` API provides a way for you to
10 receive `ChannelMonitorUpdate`s from `ChannelManager` and persist them to disk before the
11 channel steps forward.
12
13 There are two additional important structures that you may use either on the same device
14 as the `ChannelManager` or on a separate one. `Router` handles receiving channel and node 
15 announcements and calculates routes for sending payments. `PeerManager` handles the
16 authenticated and encrypted communication protocol, monitoring for liveness of peers,
17 routing messages to `ChannelManager` and `Router` instances directly, and receiving
18 messages from them via the `EventsProvider` interface.
19
20 These structs communicate with each other using a public API, so that you can easily add
21 a proxy in between for special handling. Further, APIs for key generation, transaction
22 broadcasting, block fetching, and fee estimation must be implemented and the data
23 provided by you, the user.
24
25 The library does not rely on the presence of a runtime environment outside of access to
26 heap, atomic integers, and basic Mutex primitives. This means the library will never
27 spawn threads or take any action whatsoever except when you call into it. Thus,
28 `ChannelManager` and `PeerManager` have public functions which you should call on a timer,
29 network reads and writes are external and provided by you, and the library relies only on
30 block time for current time knowledge.
31
32 At a high level, some of the common interfaces fit together as follows:
33
34
35 ```
36
37                      -----------------
38                      | KeysInterface |  --------------
39                      -----------------  | UserConfig |
40          --------------------       |   --------------
41   /------| MessageSendEvent |       |   |     ----------------
42  |       --------------------       |   |     | FeeEstimator |
43  |   (as MessageSendEventsProvider) |   |     ----------------
44  |                         ^        |   |    /          |      ------------------------
45  |                          \       |   |   /      ---------> | BroadcasterInterface |
46  |                           \      |   |  /      /     |     ------------------------
47  |                            \     v   v v      /      v        ^
48  |    (as                      ------------------       ----------------------
49  |    ChannelMessageHandler)-> | ChannelManager | ----> | ManyChannelMonitor |
50  v               /             ------------------       ----------------------
51 --------------- /                ^         (as EventsProvider)   ^
52 | PeerManager |-                 |              \     /         /
53 ---------------                  |        -------\---/----------
54  |              -----------------------  /        \ /
55  |              | ChainWatchInterface | -          v
56  |              -----------------------        ---------
57  |                            |                | Event |
58 (as RoutingMessageHandler)    v                ---------
59   \                   ----------
60    -----------------> | Router |
61                       ----------
62 ```