Skip to content

Commit bfecfe0

Browse files
Design doc cont (#578)
* add design doc * doc: flesh out design doc * doc: fixup config graph * doc: link to design from readme * fixup! doc: flesh out design doc --------- Co-authored-by: Grant <[email protected]>
1 parent 474502d commit bfecfe0

File tree

5 files changed

+321
-22
lines changed

5 files changed

+321
-22
lines changed

DESIGN.md

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# Overview
2+
3+
Warnet is designed for Bitcoin developers, researchers, and enthusiasts who want to safely experiment with and test ideas related to Bitcoin networks. By leveraging Kubernetes, Warnet enables users to deploy and simulate large-scale Bitcoin networks in a controlled and reproducible environment.
4+
5+
## Table of Contents
6+
7+
1. [Overview](#overview)
8+
- [Key Features](#key-features)
9+
- [Target Audience](#target-audience)
10+
- [Benefits](#benefits)
11+
12+
2. [Philosophy](#philosophy)
13+
14+
3. [Kubernetes and Helm in Warnet](#kubernetes-and-helm-in-warnet)
15+
- [Kubernetes](#kubernetes)
16+
- [Helm](#helm)
17+
- [Why Helm is Preferred in Warnet](#why-helm-is-preferred-in-warnet)
18+
19+
4. [Project Structure](#project-structure)
20+
- [Overview of Resources](#overview-of-resources)
21+
- [Overview of src/warnet](#overview-of-srcwarnet)
22+
- [Overview of Test](#overview-of-test)
23+
24+
5. [Operating in the Network with "Scenarios"/Commanders](#operating-in-the-network-with-scenarioscommanders)
25+
26+
6. [The Resources Configuration Pipeline - An Example](#the-resources-configuration-pipeline---an-example)
27+
28+
## Key Features:
29+
- Safe simulation of Bitcoin networks
30+
- Kubernetes-based deployment for scalability and ease of management
31+
- User interaction capabilities through custom scenarios
32+
- Infrastructure as Code (IaC) approach for reproducibility
33+
34+
## Target Audience:
35+
- Bitcoin core developers
36+
- Bitcoin researchers
37+
- Network security specialists
38+
- Bitcoin protocol testers
39+
- Bitcoin application developers
40+
- Lightning developers
41+
- Students of bitcoin
42+
- And more!
43+
44+
## Benefits:
45+
1. Scalability: Simulate networks of various sizes to understand behaviour at scale
46+
2. Reproducibility: Easily recreate test environments using IaC principles
47+
3. Flexibility: Customize network configurations and scenarios to test specific hypotheses
48+
4. Risk-free experimentation: Test new ideas without affecting real Bitcoin networks
49+
5. Educational: Learn about Bitcoin network behaviour in a controlled setting
50+
51+
## Philosophy
52+
53+
The implementation should follow a native Kubernetes (via Helm) approach wherever possible (see [Kubernetes and Helm in Warnet](#kubernetes-and-helm-in-warnet) for more information on these applications). This simplifies development in this project, and offloads responsibility over (often) non-trivial components to the Kubernetes development team. Ideologically this means that all Warnet components should be thought of as "Kubernetes resources which are to be controlled (installed/uninstalled) using Helm", wherever possible.
54+
55+
This philosophy applies to the implementation in this codebase: there are often multiple possible ways of implementing a new feature and developers should first seek out the way this is usually achieved on Kubernetes (natively) and encode this in a Helm chart, before falling back to a custom solution if this is not viable.
56+
57+
For example, a new logging component could be added in multiple possible ways to Warnet:
58+
59+
1. Run a process on the local host which connects in to the cluster via a forwarded port and performs customised logging.
60+
61+
2. Launch a standardised logging application (e.g. Grafana) via executing a `kubectl` command on a *.yaml* file.
62+
63+
```python
64+
subprocess.run("kubectl apply -f grafana.yaml --namespace=grafana")
65+
```
66+
67+
3. Create a helm chart (or use an [already-available](https://github.com/grafana/helm-charts) one) for the Grafana component and install using helm.
68+
69+
```python
70+
subprocess.run("helm upgrade --install --namespace logging promtail grafana/promtail")
71+
```
72+
73+
Out of these three options, the third should be preferred where possible, followed by the second, with the first only being used in extreme cases.
74+
75+
## Kubernetes and Helm in Warnet
76+
77+
<div style="display: flex; justify-content: space-around; align-items: center; margin: 20px 0;">
78+
<img src="/img/kubernetes.svg" alt="Kubernetes logo" style="width: 100px; height: 100px;" />
79+
<img src="/img/helm.svg" alt="Helm logo" style="width: 100px; height: 100px;" />
80+
</div>
81+
82+
Warnet leverages [Kubernetes](https://kubernetes.io/) for deploying and managing simulated Bitcoin networks, with [Helm](https://helm.sh/) serving as the preferred method for managing Kubernetes resources. Understanding the relationship between these technologies is helpful for grasping Warnet's architecture and deployment strategy.
83+
84+
### Kubernetes
85+
[Kubernetes](https://kubernetes.io/) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. In Warnet, Kubernetes provides the underlying infrastructure for running simulated Bitcoin nodes and related services.
86+
87+
Key benefits of using Kubernetes in Warnet:
88+
- Scalability: Easily scale the number of simulated nodes
89+
- Resource management: Efficiently allocate computational resources
90+
- Service discovery: Automatically manage network connections between nodes
91+
92+
### Helm
93+
[Helm](https://helm.sh/) is a package manager for Kubernetes that simplifies the process of defining, installing, and upgrading complex Kubernetes applications. Warnet prefers Helm for managing Kubernetes resources due to its powerful templating and package management capabilities.
94+
95+
Advantages of using Helm in Warnet:
96+
- Templating: Define reusable Kubernetes resource templates
97+
- Packaging: Bundle related Kubernetes resources into a single unit (chart)
98+
- Simplified deployment: Use a single command to deploy complex applications
99+
100+
### Why Helm is Preferred in Warnet
101+
1. Reproducibility: Helm charts ensure consistent deployments across different environments.
102+
2. Customization: Users can easily modify network configurations by adjusting Helm chart values.
103+
3. Modularity: New components can be added to Warnet by creating or integrating existing Helm charts.
104+
4. Simplified management: Helm's release management makes it easy to install, upgrade, rollback, or delete entire simulated networks.
105+
106+
By leveraging Kubernetes with Helm, Warnet achieves a flexible, scalable, and easily manageable architecture for simulating Bitcoin networks. This approach aligns with the project's philosophy of using native Kubernetes solutions and following best practices in cloud-native application deployment.
107+
108+
## Project structure
109+
The Warnet code base has four main sections:
110+
111+
1. *resources* - these items are available during runtime and relate to configuration (crucially, Kubernetes configuration)
112+
2. *src/warnet* - python source code lives here
113+
3. *test* - CI testing files live here
114+
4. *docs* - stores documentation available in the github repository
115+
116+
### Overview of resources
117+
There are four main kinds of *resources*:
118+
119+
1. Kubernetes configuration files - they are the backbone of Stateless Configuration; they are *yaml* files.
120+
> [!NOTE]
121+
> Whilst native Kubernetes *yaml* configuration files can and do exist here, Helm charts are the preferred way to configure Kubernetes resources.
122+
2. scenarios - these are python programs that users can load into the cluster to interact with the simulated Bitcoin network
123+
3. images - the logic for creating bitcoin nodes and also containers for running scenarios are found here; this includes Dockerfiles
124+
4. scripts and other configs - these are like "assets" or "one off" items which appear in Warnet.
125+
126+
### Overview of src/warnet
127+
The python source code found in *src/warnet* serves to give users a way to create and interact with the simulated Bitcoin network as it exists in a Kubernetes cluster.
128+
129+
There are eight categories of python program files in Warnet:
130+
131+
1. Bitcoin images
132+
* *image.py* and *image_build.py* - the logic that helps the user create bitcoin node images
133+
2. Bitcoin interaction
134+
* *bitcoin.py* - make it easy to interact with bitcoin nodes in the simulated network
135+
3. Scenario interaction
136+
* *control.py* - launch scenarios in order to interact with the simulated Bitcoin network
137+
4. Kubernetes
138+
* *k8s.py* - gather Kubernetes configuration data; retrieve Kubernetes resources
139+
* *status.py* - make it easy for the user to see the status of the simulated bitcoin network
140+
5. Resource configuration pipeline
141+
* *admin.py* - copy configurations for *resources* such as namespaces and put them in the user's directory
142+
* *deploy.py* - take configurations for *resources* and put them into the Kubernetes cluster
143+
* *network.py* - copy *resources* to the users Warnet directory
144+
* *namespaces.py* - copy *resources* to the users Warnet directory; interact with namespaces in the cluster
145+
6. User interaction
146+
* *main.py* - provide the interface for the `warnet` command line program
147+
7. Host computer
148+
* *process.py* - provides a way to run commands on the user's host computer
149+
8. Externalized configuration
150+
* *constants.py* - this holds values which occur repeatedly in the code base
151+
152+
### Overview of test
153+
The *test_base.py* file forms the basis of the *test* section. Each test uses *TestBase* which controls the test running framework.
154+
155+
### Operating in the network with "scenarios"/Commanders
156+
Warnet includes the capability to run "scenarios" on the network. These are python files which can be found in *resources/scenarios*, or copied by default into a new project directory.
157+
158+
These scenarios use a base class called `Commander` which ultimately inherits from the Bitcoin Test Framework. This means that scenarios can be written and controlled using the familiar Bitcoin Core functional test language. The `self.nodes[index]` property has been patched to automatically direct commands to a running bitcoin node of the same index in the network.
159+
160+
Once a scenario has been written, it can be loaded into the cluster and run using `warnet run <file>`.
161+
162+
### The resources configuration pipeline - an example
163+
It is important to focus on the pipeline that takes *resources*, copies them into user directories, and translates them into Kubernetes objects. To make this possible and to achieve a more stateless configuration, Warnet uses Helm which provides templating for Kubernetes configuration files.
164+
165+
Looking more closely at the *resources/charts* section, for example, we can focus in on the *bitcoincore* directory. Inside, there is an example *namespaces.yaml* and *namespace-defaults.yaml* file. These configuration files are provided to the user in their project directory when the `warnet init` command is invoked.
166+
167+
This provides the user the opportunity to change those configuration files and modify both configuration defaults for all nodes, along with specific node settings. When `warnet deploy [project-dir]` command is run by the user, it will apply the configuration data to the Helm chart found in the *charts* directory of the *resources* section. The Helm chart acts as a template through which the user's configuration data is applied.
168+
169+
In this way, there is a pipeline which starts with the user's Stateful Data which is then piped through the Helm templating system, and then is applied to the Kubernetes cluster.
170+
171+
> [!TIP]
172+
> Along with the python source code, all resources found in the *resources* directory are also included in the `warnet` python package.
173+
> In this way default resources such as helm charts are available to the CLI application via the `importlib.resources` module.
174+
175+
> [!TIP]
176+
> To learn more about the resources configuration pipeline used in Warnet see the [configuration](docs/config.md) overview.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Monitor and analyze the emergent behaviors of Bitcoin networks.
1313

1414
## Documentation
1515

16+
- [Design](/DESIGN.md)
1617
- [Installation](/docs/install.md)
1718
- [CLI Commands](/docs/warnet.md)
1819
- [Network configuration with yaml files](/docs/config.md)

docs/config.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,39 @@ The process is similar for other modules (e.g. fork-observer), but may differ sl
2323
- Kubernetes processes these manifests and creates or updates the corresponding resources in the cluster.
2424
- The process ends with the resources being deployed or updated in the Kubernetes cluster.
2525

26+
In the flowchart below, boxes with a red outline represent default or user-supplied configuration files, blue signifies files operated on by Helm or Helm operations, and green by Kubernetes.
27+
2628
```mermaid
27-
graph TD
28-
A[Start] --> B[values.yaml]
29-
subgraph User Configuration [user config: bottom overrides top]
30-
C[node-defaults.yaml]
31-
D[network.yaml]
32-
end
33-
B --> C
34-
C --> D
35-
D --> F[Final values]
36-
F --> I[Templates]
37-
I --> J[configmap.yaml]
38-
I --> K[service.yaml]
39-
I --> L[servicemonitor.yaml]
40-
I --> M[pod.yaml]
41-
J --> N[Helm renders templates]
42-
K --> N
43-
L --> N
44-
M --> N
45-
N --> O[Rendered Kubernetes manifests]
46-
O --> P[Helm applies manifests to Kubernetes]
47-
P --> Q[Kubernetes creates/updates resources]
48-
Q --> R[Resources deployed/updated in cluster]
29+
graph TD
30+
A[Start]:::start --> B[values.yaml]:::config
31+
subgraph User Configuration [User configuration]
32+
C[node-defaults.yaml]:::config
33+
D[network.yaml]:::config
34+
end
35+
B --> C
36+
C -- Bottom overrides top ---D
37+
D --> F[Final values]:::config
38+
F --> I[Templates]:::helm
39+
I --> J[configmap.yaml]:::helm
40+
I --> K[service.yaml]:::helm
41+
I --> L[servicemonitor.yaml]:::helm
42+
I --> M[pod.yaml]:::helm
43+
J --> N[Helm renders templates]:::helm
44+
K & L & M --> N
45+
N --> O[Rendered kubernetes
46+
manifests]:::helm
47+
O --> P[Helm applies manifests to
48+
kubernetes]:::helm
49+
P --> Q["Kubernetes
50+
creates/updates resources"]:::k8s
51+
Q --> R["Resources
52+
deployed/updated in cluster"]:::finish
53+
54+
classDef start fill:#f9f,stroke:#333,stroke-width:4px
55+
classDef finish fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
56+
classDef config stroke:#f00
57+
classDef k8s stroke:#0f0
58+
classDef helm stroke:#00f
4959
```
5060

5161
Users should only concern themselves therefore with setting configuration in the `<network_name>/[network|node-defaults].yaml` files.

img/helm.svg

Lines changed: 28 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)