blvm-node 0.1.7

Bitcoin Commons BLVM: Minimal Bitcoin node implementation using blvm-protocol and blvm-consensus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# Module System

## Overview

The module system enables optional features (Lightning, merge mining, privacy enhancements) without affecting consensus or base node stability. Modules run in separate processes with IPC communication, providing security through isolation.

## Architecture

### System Architecture

```
┌─────────────────────────────────────────────────────────────────┐
│                    Bitcoin Commons Infrastructure                │
│  ┌────────────────┐  ┌────────────────┐  ┌──────────────────┐ │
│  │ Governance App │  │ Module Registry│  │ Signature Service │ │
│  │  (GitHub App)  │  │   (REST API)   │  │  (Multisig)      │ │
│  └────────────────┘  └────────────────┘  └──────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
                              │ (REST API, Signatures, Governance)
┌─────────────────────────────────────────────────────────────────┐
│                         blvm-node                              │
│  ┌──────────────────────────────────────────────────────────┐ │
│  │                    Module Manager                         │ │
│  │  ┌──────────────┐  ┌──────────────┐  ┌────────────────┐ │ │
│  │  │   Loader     │  │  Discovery   │  │   Registry     │ │ │
│  │  │              │  │              │  │   Client       │ │ │
│  │  └──────────────┘  └──────────────┘  └────────────────┘ │ │
│  └──────────────────────────────────────────────────────────┘ │
│                                                                 │
│  ┌──────────────────────────────────────────────────────────┐ │
│  │                  Security Layer                           │ │
│  │  ┌──────────────┐  ┌──────────────┐  ┌────────────────┐ │ │
│  │  │   Signer     │  │  Validator   │  │   Sandbox      │ │ │
│  │  │  (Verify)    │  │  (Perms)     │  │  (Isolation)   │ │ │
│  │  └──────────────┘  └──────────────┘  └────────────────┘ │ │
│  └──────────────────────────────────────────────────────────┘ │
│                                                                 │
│  ┌──────────────────────────────────────────────────────────┐ │
│  │                    API Hub                                │ │
│  │  ┌──────────────┐  ┌──────────────┐  ┌────────────────┐ │ │
│  │  │ Blockchain   │  │  Governance  │  │  Communication │ │ │
│  │  │     API      │  │     API     │  │      API       │ │ │
│  │  └──────────────┘  └──────────────┘  └────────────────┘ │ │
│  └──────────────────────────────────────────────────────────┘ │
│                                                                 │
│  ┌──────────────────────────────────────────────────────────┐ │
│  │                  IPC Server                               │ │
│  │              (Unix Domain Sockets)                       │ │
│  └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
                              │ (IPC)
┌─────────────────────────────────────────────────────────────────┐
│                    Module Processes (Isolated)                  │
│  ┌──────────────┐  ┌──────────────┐  ┌────────────────────┐  │
│  │   Module A     │  │   Module B    │  │    Module C        │  │
│  │  (Lightning)   │  │ (Merge Mine)  │  │  (Privacy)         │  │
│  └──────────────┘  └──────────────┘  └────────────────────┘  │
│  ┌──────────────┐  ┌──────────────┐                            │
│  │   Module D     │  │   Module E    │                            │
│  │ (Marketplace)   │  │ (Stratum V2)  │                            │
│  └──────────────┘  └──────────────┘                            │
└─────────────────────────────────────────────────────────────────┘
```

## Available Modules

### Core Modules

- **blvm-stratum-v2**: Stratum V2 mining protocol support
- **blvm-merge-mining**: Merge mining for secondary chains (requires blvm-stratum-v2)
- **blvm-marketplace**: Module marketplace and registry (handles module payments)

### Module Dependencies

- **blvm-merge-mining** requires **blvm-stratum-v2** (for mining coordination)
- **blvm-marketplace** is standalone (handles module registry and payments)

### Payment Models

- **blvm-merge-mining**: One-time activation fee + hardcoded revenue share
- **blvm-marketplace**: Receives 15% of all module sales (75% author, 15% marketplace, 10% node)

## Module Lifecycle

```
Discovery → Verification → Loading → Execution → Monitoring
    │            │            │           │            │
    │            │            │           │            │
    ▼            ▼            ▼           ▼            ▼
Registry    Signer      Loader      Process      Monitor
```

### Lifecycle Events

Modules can subscribe to lifecycle events to react to dependency changes:

- **`ModuleLoaded`**: Published when a module is loaded
  - Payload: `{ module_id, module_name, version }`
  - Use case: Dependent modules can initialize connections when dependencies load

- **`ModuleUnloaded`**: Published when a module is unloaded
  - Payload: `{ module_id, module_name }`
  - Use case: Dependent modules can clean up when dependencies unload

### Discovery

Modules discovered through:
- Local filesystem (`modules/` directory)
- Module registry (REST API)
- Manual installation

### Verification

Each module verified through:
- Hash verification (binary integrity)
- Signature verification (multisig maintainer signatures)
- Permission checking (capability validation)
- Compatibility checking (version requirements)

### Loading

Module loaded into isolated process:
- Sandbox creation (resource limits)
- IPC connection establishment
- API subscription setup
- Config merge: node `[modules.<name>]` overrides module `config.toml`; node values take precedence
- Database backend: inherited from node when not set; module can override via `database_backend` in config

### Execution

Module runs in isolated process:
- Separate memory space
- Resource limits enforced
- IPC communication only
- Event subscription active

### Monitoring

Module health monitored:
- Process status
- Resource usage
- Error tracking
- Crash isolation

## Security Model

### Process Isolation

Modules run in separate processes with isolated memory. Node consensus state is protected and read-only to modules.

```
┌─────────────────────────────────────┐
│         blvm-node Process          │
│  ┌───────────────────────────────┐ │
│  │    Consensus State             │ │
│  │    (Protected, Read-Only)      │ │
│  └───────────────────────────────┘ │
│  ┌───────────────────────────────┐ │
│  │    Module Manager             │ │
│  │    (Orchestration)            │ │
│  └───────────────────────────────┘ │
└─────────────────────────────────────┘
              │ IPC (Unix Sockets)
┌─────────────┴─────────────────────┐
│      Module Process (Isolated)     │
│  ┌───────────────────────────────┐ │
│  │    Module State               │ │
│  │    (Separate Memory Space)    │ │
│  └───────────────────────────────┘ │
│  ┌───────────────────────────────┐ │
│  │    Sandbox                    │ │
│  │    (Resource Limits)          │ │
│  └───────────────────────────────┘ │
└─────────────────────────────────────┘
```

### Security Flow

```
Module Binary
    ├─→ Hash Verification ──→ Integrity Check
    ├─→ Signature Verification ──→ Multisig Check ──→ Maintainer Verification
    ├─→ Permission Check ──→ Capability Validation
    └─→ Sandbox Creation ──→ Resource Limits ──→ Isolation
```

### Permission Model

Modules request capabilities:
- `read_blockchain` - Read-only blockchain access
- `subscribe_events` - Subscribe to node events
- `governance_vote` - Cast governance votes (if authorized)
- `send_transactions` - Submit transactions to mempool (future)

## Module Manifest

Module manifests use TOML format with a clean, hierarchical structure:

```toml
# ============================================================================
# Module Manifest
# ============================================================================

# ----------------------------------------------------------------------------
# Core Identity (Required)
# ----------------------------------------------------------------------------
name = "lightning-network"
version = "1.2.3"
entry_point = "lightning-network"

# ----------------------------------------------------------------------------
# Metadata (Optional)
# ----------------------------------------------------------------------------
description = "Lightning Network payment processor implementation"
author = "Alice <alice@example.com>"

# ----------------------------------------------------------------------------
# Capabilities
# ----------------------------------------------------------------------------
# Permissions this module requires to function
capabilities = [
    "read_blockchain",    # Query blockchain data
    "subscribe_events",   # Receive node events
    "read_lightning",     # Access Lightning Network APIs
]

# ----------------------------------------------------------------------------
# Dependencies
# ----------------------------------------------------------------------------
# Required dependencies (module cannot load without these)
[dependencies]
"blvm-node" = ">=1.0.0"

# Optional dependencies (module can work without these)
[optional_dependencies]
"blvm-mesh" = ">=0.5.0"  # Optional mesh networking support

# ----------------------------------------------------------------------------
# Configuration Schema
# ----------------------------------------------------------------------------
# Descriptions of configuration keys this module accepts
[config_schema]
network = "Network: mainnet, testnet, regtest (default: mainnet)"
fee_rate = "Default fee rate in sat/vB (default: 1)"

# ----------------------------------------------------------------------------
# Advanced Features (Optional)
# ----------------------------------------------------------------------------

# Binary integrity verification
[binary]
hash = "sha256:abc123..."
size = 1234567

# Maintainer signatures (for verified modules)
[signatures]
threshold = "2-of-3"
maintainers = [
    { name = "alice", public_key = "02abc...", signature = "..." },
    { name = "bob", public_key = "03def...", signature = "..." },
]

# Payment configuration (for paid modules)
[payment]
required = true
price_sats = 100000
author_payment_code = "PM8TJ..."
commons_payment_code = "PM8TJ..."
payment_signature = "..."
```

### Manifest Structure

The manifest is organized into logical sections:

1. **Core Identity** (required): `name`, `version`, `entry_point`
2. **Metadata** (optional): `description`, `author`
3. **Capabilities**: List of permissions the module requires
4. **Dependencies**: Required and optional module dependencies
5. **Configuration Schema**: Descriptions of configurable options
6. **Advanced Features** (optional): Signatures, binary verification, payment config

## Module Storage

Each module has its **own separate database** at `data/modules/<name>/db/`. By default, modules use the **same database format** as the node (redb, rocksdb, sled, tidesdb). Configurable via `database_backend` in module config or `[modules.<name>]`; when not set, inherits from the node.

## Config Override

Node config `[modules.<name>]` overrides module `config.toml` when loading. Node values take precedence. Example:

```toml
[modules.selective-sync]
database_backend = "redb"
```

## CLI Flow

Modules register CLI specs on connect via `RegisterCliSpec`. The node stores them in `cli_registry`. blvm fetches specs via `getmoduleclispecs` RPC and dispatches via `runmodulecli` when the user runs a module command (e.g. `blvm sync-policy list`). Node → module invocation uses `ModuleMessage::Invocation` over IPC.

## Data Flow

### Module Installation Flow

```
1. User requests module installation
      ├─→ Query Registry API
   │   │
   │   ├─→ Fetch module metadata
   │   ├─→ Verify maintainer signatures
   │   └─→ Check compatibility
      ├─→ Download module binary
   │   │
   │   ├─→ Verify binary hash
   │   └─→ Verify binary signatures
      ├─→ Resolve dependencies
   │   │
   │   ├─→ Check layer compatibility
   │   └─→ Verify dependency signatures
      ├─→ Install to modules directory
      └─→ Register with Module Manager
```

### Module Execution Flow

```
1. Module Manager loads module
      ├─→ Verify signatures (if not cached)
      ├─→ Check permissions
      ├─→ Create sandbox environment
      ├─→ Spawn module process
      ├─→ Establish IPC connection
      ├─→ Module subscribes to events
      └─→ Module enters main loop
```

## Integration Points

### Registry Integration

Modules discovered and verified through module registry:

```
Module Registry Client
        ├─→ REST API Client
    │   ├─→ Search modules
    │   ├─→ Get module metadata
    │   ├─→ Download binaries
    │   └─→ Verify signatures
        └─→ Local Cache
        ├─→ Cached metadata
        └─→ Signature cache
```

### Governance Integration

Modules participate in governance through governance API:

```
Governance Client
        ├─→ Proposal API
    │   ├─→ Get proposals
    │   ├─→ Cast votes
    │   └─→ Get results
        └─→ Signature Verification
        ├─→ Verify maintainer signatures
        └─→ Multisig validation
```

### Security Integration

Module security handled through security layer:

```
Security Layer
        ├─→ Module Signer
    │   ├─→ Verify manifest signatures
    │   ├─→ Verify binary signatures
    │   └─→ Multisig validation
        ├─→ Permission Validator
    │   ├─→ Check capabilities
    │   ├─→ Tier validation
    │   └─→ Resource limits
        └─→ Sandbox Manager
        ├─→ Process isolation
        ├─→ Resource limits
        └─→ Capability enforcement
```

## Usage

See [modules/README.md](../modules/README.md) for module installation and usage instructions.

## Security

Modules cannot:
- Modify consensus rules
- Modify UTXO set
- Access node private keys
- Bypass security boundaries
- Affect other modules

Module crashes are isolated and do not affect the base node.