Skip to content

Commit e7d3785

Browse files
Merge pull request #683 from lightninglabs/docs-lnd
Update lnd documentation
2 parents 1713d35 + babcd57 commit e7d3785

File tree

6 files changed

+425
-0
lines changed

6 files changed

+425
-0
lines changed

docs/lnd/alloy-models/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Alloy Models
2+
3+
This folder houses "lightweight formal models" written using the
4+
[Alloy](https://alloytools.org/about.html) model checker and language.
5+
6+
Compared to typical formal methods, Alloy is a _bounded_ model checker,
7+
considered a [lightweight
8+
method](https://en.wikipedia.org/wiki/Formal_methods#:~:text=As%20an%20alternative,Tools.%5B31)
9+
to formal analysis. Lightweight formal methods are easier to use than fully
10+
fledged formal methods as rather than attempting to prove the that a model is
11+
_always_ correct (for all instances), they instead operate on an input of a set
12+
of bounded parameters and iterations. These models can then be used to specify
13+
a model, then use the model checker to find counter examples of a given
14+
assertion. If a counter example can't be found, then the model _may_ be valid.
15+
16+
Alloy in particular is an expressive, human readable language for formal
17+
modeling. It also has a nice visualizer which can show counter examples, aiding
18+
in development and understanding.
19+
20+
Alloy is useful as when used during upfront software design (or even after the
21+
fact), one can create a formal model of a software system to gain better
22+
confidence in the _correctness_ of a system. The model can then be translated
23+
to code. Many times, writing the model (either before or after the code) can
24+
help one to better understand a given software system. Models can also be used
25+
to specify protocols in p2p systems such as Lightning (as it [supports temporal
26+
logic](https://alloytools.org/alloy6.html#:~:text=Meaning%20of%20temporal%20connectives),
27+
which enables creation of state machines and other interactive transcripts),
28+
serving as a complement to a normal prosed based specification.
29+
30+
## How To Learn Alloy?
31+
32+
We recommend the following places to learn Alloy:
33+
* [The online tutorial for Alloy 4.0](https://alloytools.org/about.html):
34+
* Even though this is written with Alloy 4.0 (which doesn't support
35+
temporal logic), the tutorial is very useful, as it introduces
36+
fundamental concept of Alloy, using an accessible example based on a file
37+
system.
38+
39+
* [Alloy Docs](https://alloy.readthedocs.io/en/latest/index.html):
40+
* This serves as a one stop shop for reference to the Alloy language. It
41+
explains all the major syntax, tooling, and also how to model time in
42+
Alloy.
43+
44+
* [Formal Software Design with Alloy 6](https://haslab.github.io/formal-software-design/index.html):
45+
* A more in depth tutorial which uses Alloy 6. This tutorial covers more
46+
advanced topics such as [event
47+
reification](https://alloytools.discourse.group/t/modelling-a-state-machine-in-electrum-towards-alloy-6/88).
48+
This tutorial is also very useful, as it includes examples for how to
49+
model interactions in a p2p network, such as one peer sending a message
50+
to another.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Linear Fee Function
2+
3+
This is a model of the default [Linear Fee
4+
Function](https://github.com/lightningnetwork/lnd/blob/b7c59b36a74975c4e710a02ea42959053735402e/sweep/fee_function.go#L66-L109)
5+
fee bumping mechanism in lnd.
6+
7+
This model was inspired by a bug fix, due to an off-by-one error in the
8+
original code: https://github.com/lightningnetwork/lnd/issues/8741.
9+
10+
The bug in the original code was fixed in this PR:
11+
https://github.com/lightningnetwork/lnd/pull/8751.
12+
13+
14+
## Model & Bug Fix Walk-through
15+
16+
The model includes an assertion that captures the essence of the bug:
17+
`max_fee_rate_before_deadline`:
18+
```alloy
19+
// max_fee_rate_before_deadline is the main assertion in this model. This
20+
// captures a model violation for our fee function, but only if the line in
21+
// fee_rate_at_position is uncommented.
22+
//
23+
// In this assertion, we declare that if we have a fee function that has a conf
24+
// target of 4 (we want a few fee bumps), and we bump to the final block, then
25+
// at that point our current fee rate is the ending fee rate. In the original
26+
// code, assertion isn't upheld, due to an off by one error.
27+
assert max_fee_rate_before_deadline {
28+
always req_num_blocks_to_conf[4] => bump_to_final_block => eventually (
29+
all f: LinearFeeFunction | f.position = f.width.sub[1] &&
30+
f.currentFeeRate = f.endingFeeRate
31+
)
32+
}
33+
```
34+
35+
We can modify the model to find the bug described in the original issue.
36+
1. First, we modify the model by forcing a `check` on the
37+
`max_fee_rate_before_deadline` assertion:
38+
```alloy
39+
check max_fee_rate_before_deadline
40+
```
41+
42+
2. Next, we'll modify the `fee_rate_at_position` predicate to re-introduce
43+
the off by one error:
44+
```alloy
45+
p >= f.width => f.endingFeeRate // -- NOTE: Uncomment this to re-introduce the original bug.
46+
```
47+
48+
If we hit `Execute` in the Alloy Analyzer, then we get a counter example:
49+
![Counter Example](counter-example.png)
50+
51+
52+
We can hit `Show` in the analyzer to visualize it:
53+
![Counter Example Show](counter-example-show.png)
54+
55+
We can see that even though we're one block (`position`) before the deadline
56+
(`width`), our fee rate isn't at the ending fee rate yet.
57+
58+
If we modify the `fee_rate_at_position` to have the correct logic:
59+
```alloy
60+
p >= f.width.sub[1] => f.endingFeeRate
61+
```
62+
63+
Then Alloy is unable to find any counterexamples:
64+
![Correct Model](fixed-model.png)
132 KB
Loading
18.7 KB
Loading
17.7 KB
Loading

0 commit comments

Comments
 (0)