Skip to content
This repository was archived by the owner on Feb 25, 2023. It is now read-only.

Commit b054ff2

Browse files
0xB10CStadicus
authored andcommitted
docs: middleware and RPC calls
1 parent d279ef4 commit b054ff2

17 files changed

Lines changed: 235 additions & 6 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tools/bbbconfgen/test/test-output.conf
3232
/_site
3333
/docs/.sass-cache
3434
/docs/_site
35+
docs/.jekyll-cache/*
3536

3637
# Ignore the binary build outputs (except READMEs)
3738
/bin/go/*

docs/customapps/middleware/base-streaming-service-info-changes-to-frontend_sequencediagram-org.svg

Lines changed: 1 addition & 0 deletions
Loading

docs/customapps/middleware/middleware-communication.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,47 @@ nav_order: 300
77
---
88
## Middleware: Communication
99

10-
(TODO)0xB10C
10+
11+
### BitBoxApp <-> Middleware RPCs
12+
13+
TODO
14+
15+
The Middleware handles the communication between the BitBoxApp and the BitBoxBase.
16+
17+
- minimize traffic
18+
- RPC Client/Server (Go RPC package)
19+
- Why Websockets
20+
- End to End encryption
21+
- Noise paring / handshake
22+
- TLS vs Noise
23+
- JWT Authentication
24+
25+
Specific RPC sequence diagrams:
26+
27+
* [Streaming ServiceInfo changes to App frontend](base-streaming-service-info-changes-to-frontend_sequencediagram-org.svg){:target="_blank"}
28+
29+
30+
### HSM communication
31+
32+
TODO
33+
34+
### IPC notifications
35+
36+
The Middleware is able to receive IPC notifications from other processes running on the BitBoxBase.
37+
IPC notifications are implemented via a Unix named pipe and notifications are formatted in a JSON based protocol.
38+
Using a named pipe allows simple and dependencyless implementation in other Go, Python or Shell Script based processes running on the BitBoxBase.
39+
One draw back is that a named pipe blocks writes until the content is read.
40+
By default the named pipe is located in `/tmp/middleware-notification.pipe` and system level permissions are required to write in the pipe.
41+
42+
![middleware ipc notifications](middleware_ipc_notifications.png)
43+
44+
*Schematic showing multiple scripts writing notifications into the named pipe.*
45+
46+
The JSON based protocol for the notifications is versioned, includes a notification topic and a can contain a payload.
47+
The payload can contain any valid JSON structure.
48+
Since a write operation to a unix pipe only being atomic (i.e. two writes do not interleave) as long as the amount written is smaller than the `PIPE_BUF`, which is 4096 bytes for Linux, the Middleware drops all notifications bigger than 4095 bytes.
49+
50+
*Sample notification:*
51+
```JSON
52+
{"version": 1, "topic": "sampletopic", "payload": {"sampleInt":123,"sampleString": "string", "sampleBool": true}}
53+
```

docs/customapps/middleware/middleware-configuration.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,48 @@ nav_order: 200
77
---
88
## Middleware: Configuration
99

10-
(TODO)0xB10C
10+
11+
### Command line interface parameters
12+
13+
Running `bbbmiddleware -h` prints the configuration options for the Middleware on the command line.
14+
15+
Note: *The argument list below was last updated in December 2019.*
16+
17+
```console
18+
-bbbcmdscript string
19+
Path to the bbb-cmd.sh script that allows executing system commands (default "/opt/shift/scripts/bbb-cmd.sh")
20+
-bbbconfigscript string
21+
Path to the bbb-config.sh script that allows setting system configuration (default "/opt/shift/scripts/bbb-config.sh")
22+
-bbbsystemctlscript string
23+
Path to the bbb-systemctl.sh script that allows starting and stopping services on the Base (default "/opt/shift/scripts/bbb-systemctl.sh")
24+
-datadir string
25+
Directory where the Middleware persistent data, like for example the noise encryption keys, is stored (default ".base")
26+
-electrsport string
27+
Electrs RPC port (default "51002")
28+
-hsmfirmwarefile string
29+
Location of the signed HSM firmware binary (default "/opt/shift/hsm/firmware-bitboxbase.signed.bin")
30+
-hsmserialport string
31+
Serial port used to communicate with the HSM (default "/dev/ttyS0")
32+
-middlewareport string
33+
Port the Middleware listens on (default "8845")
34+
-network string
35+
Indicate wether Bitcoin is running on mainnet or testnet (default "testnet")
36+
-notificationNamedPipePath string
37+
Path where the Middleware creates a named pipe to receive notifications from other processes on the BitBoxBase (default "/tmp/middleware-notification.pipe")
38+
-prometheusurl string
39+
URL of the Prometheus server (default "http://localhost:9090")
40+
-redismock
41+
Flag to use the Redis mock for development instead of connecting to a redis server
42+
-redisport string
43+
Port of the Redis server (default "6379")
44+
-updatehsmfirmware
45+
Set to true to force HSM firmware update
46+
-updateinfourl string
47+
URL to query information about Base image updates from (default "https://shiftcrypto.ch/updates/base.json")
48+
```
49+
50+
### Changing the Middleware systemd file
51+
52+
(TODO)Stadicus
53+
54+
- config in systemd unit (via env variables / config template in /etc/bbbmiddleware)

docs/customapps/middleware/middleware-functionality.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,45 @@ nav_order: 100
77
---
88
## Middleware: Functionality
99

10-
(TODO)0xB10C
10+
11+
The Middleware connects the BitBoxBase system with the BitBoxBase user interfaces, such as the BitBoxApp and the HSM screen.
12+
The task of the Middleware is to respond to synchronous user request and to asynchronously notify the user about changes on the system.
13+
A action from the user could for example toggle _SSH access_ or requests for information about the Bitcoin blockchain synchronization progress.
14+
A notification from the Middleware to the user could be for example about an available software update or the disk running out of space.
15+
16+
### Tasks of the Middleware
17+
18+
The Middleware processes Remote Procedure Calls (RPCs) from the BitBoxApp, keeps a connection to the HSM, queries both Redis and Prometheus servers, executes shell scripts to change system configuration and listens on an inter process communication (IPC) interface for notifications from other processes.
19+
20+
<br>
21+
![Middleware overview](middleware_overview.png)
22+
*Schematic showing the Middleware and it's connections.*
23+
<br>
24+
<br>
25+
26+
#### App connectivity via RPC
27+
The BitBoxApp acts as RPC Client talking to the Middleware's RPC Server.
28+
Available RPCs can for example change system configuration, set the user password, create or restore a backup, update the BitBoxBase software or deliver information on services such as Bitcoin Core or c-lightning running on the Base.
29+
The [BitBoxApp <-> Middleware communication section](middleware-communication.html) includes more detail on design and implementation decisions.
30+
31+
#### HSM connectivity via UART
32+
33+
The Middleware communicates with the HSM over UART.
34+
Messages are serialized as Protobuf messages.
35+
36+
#### Querying Redis and Prometheus
37+
38+
The Redis server on the BitBoxBase acts as a store for system configuration and the Prometheus server holds service specific information.
39+
The Middleware queries information from both.
40+
41+
42+
#### Executing shell scripts
43+
44+
The Middleware calls the [bbb-cmd.sh](../bbb-cmd.html) and [bbb-config.sh](../bbb-config.html) scripts to change system configuration and to call standard commands.
45+
46+
47+
#### Listening for IPC notifications
48+
49+
Processes running on the BitBoxBase can notify the Middleware about changes the user needs to be made aware of.
50+
This is for example used to notify the Middleware about a successful completion of system checks after an update.
51+
The [communication section](middleware-communication.html) includes detailed information about the IPC implementation.
71.9 KB
Loading
41.5 KB
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
title pre-update
2+
3+
participant app
4+
5+
middleware->shiftcrypto.ch/updates/base.json: HTTP GET
6+
middleware<-shiftcrypto.ch/updates/base.json: response
7+
box over middleware: parse JSON
8+
box over middleware: compare base\nversion with \nbase.json version
9+
10+
11+
box over middleware: if newer
12+
middleware -->> app: notify app about newer version avaliable
13+
14+
middleware <- app: GetUpdateVersion
15+
middleware -> app: {\nupdateAvaliable: bool, \nversion: string, \ndescription: string, \nseverity: int\n}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
title Streaming ServiceInfo changes to the Frontend
3+
4+
participant Frontend
5+
participant App
6+
participant Middleware
7+
participant Prometheus
8+
participant Prometheus Scraper
9+
10+
11+
Middleware->Prometheus: query last scrape timestamp
12+
Middleware<-Prometheus: timestamp: 2
13+
14+
note over Middleware: compare with saved timestamp\n\nequal:\ndo nothing
15+
note over Middleware: wait 5 seconds
16+
17+
Prometheus <<-- Prometheus Scraper: write new data\ntimestamp: 3
18+
Middleware->Prometheus: query last scrape timestamp
19+
Middleware<-Prometheus: timestamp: 3
20+
note over Middleware: compare with saved timestamp\n\nnot equal:\n1. safe timestamp = 3\n2. check if ServiceInfo changed
21+
Middleware->Prometheus: query ServiceInfo data\n(multiple querys)
22+
Middleware<-Prometheus: data ServiceInfo
23+
note over Middleware: check if ServiceInfo data changed\n\n- not changed: skip\n- changed: fire event
24+
App<<--Middleware:notify ServiceInfo changed
25+
note over Middleware:wait 5 seconds
26+
Frontend<<--App:notify ServiceInfo changed
27+
28+
Frontend->App: GET /service-info
29+
Prometheus <<-- Prometheus Scraper: write new data\ntimestamp: 4
30+
App->Middleware:GetServiceInfo RPC
31+
App<-Middleware:Return cached\nGetServiceInfoResponse
32+
Frontend<-App:GetServiceInfoResponse
33+
note over Frontend:Redraw
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
title Base Update Process via RPC
2+
3+
note over front-end: user wants to update
4+
front-end->App: POST /update-base <version>
5+
App->Middleware: UpdateBase RPC <version>
6+
Middleware->bbb-cmd.sh: mender-update install <version>
7+
8+
activate bbb-cmd.sh
9+
10+
bbb-cmd.sh -->> Middleware: read from stdin: progress 1%
11+
Middleware -->> App: Event: UpdateProgresChanged
12+
App -->> front-end: Event: UpdateProgressChanged
13+
note over front-end:Am I ready to query new\nupdate progress info?\n\nNo (do nothing).
14+
15+
bbb-cmd.sh -->> Middleware: read from stdin: progress 2%
16+
Middleware -->> App: Event: UpdateProgresChanged
17+
App -->> front-end: Event: UpdateProgressChanged
18+
note over front-end:Am I ready to query new\nupdate progress info?\n\nYes.
19+
20+
21+
front-end->App: GET /base-update-progress
22+
bbb-cmd.sh -->> Middleware: read from stdin: progress 3%
23+
24+
App->Middleware:GetBaseUpdateProgress
25+
Middleware->App: 3%
26+
App->front-end: 3%
27+
28+
29+
==when update finished==
30+
31+
bbb-cmd.sh -> Middleware: (no error)
32+
deactivate bbb-cmd.sh
33+
34+
note over Middleware: set redis keys and restart base
35+
36+
Middleware -> App: ErrorResponse{Success: true}
37+
38+
App ->front-end: {success: true}
39+

0 commit comments

Comments
 (0)