@optimystic/reference-peer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,265 @@
1
+ # Optimystic Reference Peer (CLI)
2
+
3
+ A developer-friendly CLI for running an Optimystic peer over libp2p and exercising collections and distributed transactions.
4
+
5
+ This tool supports:
6
+ - Interactive mode (join the network, then issue commands)
7
+ - Single-action mode (start → perform action → optionally stay connected)
8
+ - Single-node (no bootstrap) and multi-node (with bootstrap) flows
9
+ - Memory or file-backed storage
10
+
11
+ ---
12
+
13
+ ## Prerequisites
14
+ - Node.js 20+
15
+ - Yarn 1.x (workspaces)
16
+
17
+ From the repo root:
18
+
19
+ ```sh
20
+ # Build p2p and the CLI (recommended sequence)
21
+ yarn --silent workspace @optimystic/db-p2p build
22
+ yarn --silent workspace @optimystic/reference-peer build
23
+ ```
24
+
25
+ You can also build just the CLI:
26
+
27
+ ```sh
28
+ yarn --silent workspace @optimystic/reference-peer build
29
+ ```
30
+
31
+ ---
32
+
33
+ ## Quick Start
34
+
35
+ After building, the CLI is available as `yarn optimystic-peer` from the workspace root, or you can run `node packages/reference-peer/dist/src/cli.js` directly.
36
+
37
+ ### Start the first node (no bootstrap)
38
+ This starts a libp2p node and drops you into interactive mode.
39
+
40
+ ```sh
41
+ yarn optimystic-peer interactive --port 8011 --network optimystic
42
+ ```
43
+
44
+ You will see listening multiaddrs like:
45
+
46
+ ```
47
+ /ip4/127.0.0.1/tcp/8011/p2p/<PEER_ID>
48
+ ```
49
+
50
+ Share one of these with other peers as a `--bootstrap` address.
51
+
52
+ ### Start a second node (join via bootstrap)
53
+
54
+ ```sh
55
+ yarn optimystic-peer interactive \
56
+ --port 8021 \
57
+ --network optimystic \
58
+ --bootstrap "/ip4/127.0.0.1/tcp/8011/p2p/<PEER_ID>"
59
+ ```
60
+
61
+ `--bootstrap` accepts a comma-separated list for multiple addresses.
62
+
63
+ ---
64
+
65
+ ## Mesh Orchestrator (local multi-node)
66
+
67
+ `packages/reference-peer/src/mesh.ts` (built to `packages/reference-peer/dist/src/mesh.js`) launches a small local mesh of headless service peers and writes their info to `.mesh/node-*.json`. Useful for testing discovery/bootstrapping and for the provided VS Code launch profiles.
68
+
69
+ Run after building:
70
+
71
+ ```sh
72
+ # Defaults: 2 nodes starting at port 8011
73
+ yarn workspace @optimystic/reference-peer mesh
74
+
75
+ # Configure size and base port
76
+ # macOS/Linux
77
+ MESH_NODES=3 MESH_BASE_PORT=8011 yarn workspace @optimystic/reference-peer mesh
78
+ # Windows (cmd)
79
+ set MESH_NODES=3 && set MESH_BASE_PORT=8011 && yarn workspace @optimystic/reference-peer mesh
80
+ ```
81
+
82
+ What happens:
83
+ - Starts node-1 without bootstrap; writes `.mesh/node-1.json` containing `peerId` and `multiaddrs`
84
+ - Starts remaining nodes with `--bootstrap` set to node-1’s addresses
85
+ - Uses in-memory storage (`--storage memory`)
86
+ - Exits with Ctrl+C
87
+
88
+ Output file example (`.mesh/node-1.json`):
89
+
90
+ ```json
91
+ {
92
+ "peerId": "12D3Koo...",
93
+ "multiaddrs": ["/ip4/127.0.0.1/tcp/8011/p2p/12D3Koo..."],
94
+ "port": 8011,
95
+ "networkName": "optimystic",
96
+ "timestamp": 1700000000000,
97
+ "pid": 12345
98
+ }
99
+ ```
100
+
101
+ VS Code integration:
102
+ - "Optimystic: Start Mesh (N=3) then Debug Peer" runs `dist/mesh.js` with `MESH_NODES=3` and base port `8011`.
103
+ - "Optimystic: Debug Interactive Peer (bootstraps to mesh)" launches the CLI with `--bootstrap-file ./.mesh/node-1.json`. The CLI reads that file’s `multiaddrs` and automatically bootstraps.
104
+ - The compound "Optimystic: Mesh + Debug Peer" starts both together.
105
+
106
+ ---
107
+
108
+ ## Modes
109
+
110
+ - Single-node (no `--bootstrap`):
111
+ - Uses an in-process local transactor (no network consensus)
112
+ - Great for local development
113
+ - Distributed (with `--bootstrap`):
114
+ - Uses the network transactor (libp2p services for repo/cluster)
115
+ - Suitable for exercising peer-to-peer coordination
116
+
117
+ Bootstrap discovery is only enabled when `--bootstrap` is provided.
118
+
119
+ ---
120
+
121
+ ## Storage Options
122
+
123
+ - `--storage memory` (default): keeps data in-memory for the process lifetime
124
+ - `--storage file` with `--storage-path <dir>`: persists data to disk
125
+
126
+ Examples:
127
+
128
+ ```sh
129
+ # Memory (default)
130
+ yarn optimystic-peer interactive --port 8011
131
+
132
+ # File-backed
133
+ yarn optimystic-peer interactive \
134
+ --port 8011 \
135
+ --storage file \
136
+ --storage-path "./.optimystic-storage/node-8011"
137
+ ```
138
+
139
+ ---
140
+
141
+ ## Interactive Mode
142
+
143
+ ```sh
144
+ yarn optimystic-peer interactive [options]
145
+ ```
146
+
147
+ Options:
148
+ - `-p, --port <number>`: Port to listen on (default: 0 = auto)
149
+ - `-b, --bootstrap <string>`: Comma-separated list of bootstrap multiaddrs
150
+ - `-i, --id <string>`: Optional peer id
151
+ - `-r, --relay`: Enable relay service
152
+ - `-n, --network <string>`: Network name (default: `optimystic`)
153
+ - `-s, --storage <type>`: `memory` | `file` (default: `memory`)
154
+ - `--storage-path <path>`: Required when `--storage file`
155
+
156
+ Once started, you’ll see a prompt:
157
+
158
+ ```
159
+ 🎮 Interactive mode started. Type "help" for commands, "exit" to quit.
160
+ optimystic>
161
+ ```
162
+
163
+ Available interactive commands:
164
+ - `help` – Show command help
165
+ - `create-diary <name>` – Create a diary collection
166
+ - `add-entry <diary> <content>` – Append an entry
167
+ - `list-diaries` – List created diaries in this session
168
+ - `read-diary <name>` – Stream all entries
169
+ - `exit` / `quit` – Disconnect and exit
170
+
171
+ ---
172
+
173
+ ## Single-Action Mode
174
+
175
+ ```sh
176
+ yarn optimystic-peer run --action <action> [options]
177
+ ```
178
+
179
+ Actions:
180
+ - `create-diary` requires `--diary <name>`
181
+ - `add-entry` requires `--diary <name>` and `--content <text>`
182
+ - `list-diaries`
183
+ - `read-diary` requires `--diary <name>`
184
+
185
+ Common options:
186
+ - `--stay-connected` – remain connected and switch to interactive mode after the action
187
+ - All network/storage options available in interactive mode are supported here, too
188
+
189
+ Examples:
190
+
191
+ ```sh
192
+ # Create a diary and disconnect
193
+ yarn optimystic-peer run \
194
+ --action create-diary \
195
+ --diary my-diary \
196
+ --port 8001
197
+
198
+ # Add an entry
199
+ yarn optimystic-peer run \
200
+ --action add-entry \
201
+ --diary my-diary \
202
+ --content "Hello, Optimystic!" \
203
+ --port 8002
204
+
205
+ # Read entries
206
+ yarn optimystic-peer run \
207
+ --action read-diary \
208
+ --diary my-diary \
209
+ --port 8003
210
+
211
+ # List and then stay connected to keep working
212
+ yarn optimystic-peer run \
213
+ --action list-diaries \
214
+ --stay-connected \
215
+ --port 8004
216
+ ```
217
+
218
+ ---
219
+
220
+ ## Troubleshooting
221
+
222
+ - "Bootstrap requires a list of peer addresses"
223
+ - Omit `--bootstrap` when starting the first node
224
+ - Ensure you’ve rebuilt the packages so bootstrap discovery is conditional
225
+ - "_started not set"
226
+ - Fixed: custom libp2p services now pass only the required `logger` and `registrar`
227
+ - Rebuild `@optimystic/db-p2p` and `@optimystic/reference-peer`
228
+ - No peers found in distributed mode
229
+ - Verify `--bootstrap` addresses are correct and reachable
230
+ - You can provide multiple bootstrap addresses (comma-separated)
231
+ - Data not persisting across restarts
232
+ - Use `--storage file --storage-path <dir)` to persist locally
233
+
234
+ ---
235
+
236
+ ## Notes
237
+ - In single-node mode, operations execute locally (no network coordination). This is ideal for development.
238
+ - In distributed mode, operations are coordinated via libp2p protocols for repo and cluster.
239
+ - Multiaddrs printed at startup can be used as bootstrap addresses for subsequent nodes.
240
+
241
+ ---
242
+
243
+ ## Development
244
+
245
+ Rebuild after making changes:
246
+
247
+ ```sh
248
+ yarn workspace @optimystic/db-p2p build
249
+ yarn workspace @optimystic/reference-peer build
250
+ ```
251
+
252
+ After building, the CLI is available as:
253
+
254
+ ```sh
255
+ # From workspace root (recommended)
256
+ yarn optimystic-peer interactive --port 8011
257
+
258
+ # Or via the start script
259
+ yarn workspace @optimystic/reference-peer start -- interactive --port 8011
260
+ ```
261
+
262
+ ---
263
+
264
+ ## License
265
+ This package is part of the Optimystic repository. See the root project for licensing information.
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":""}