Skip to content

Commit 2011c13

Browse files
doc: Updated section for Generating Examples in Schema
1 parent ceeb19b commit 2011c13

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

doc/contribute-to-core-lightning/coding-style-guidelines/writing-json-schemas.md

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,58 @@ To add conditional fields:
6262
it can be defined as `"dependentUpon": { "index": ["start", "limit"] }` in the json and it will
6363
generate the Markdown syntax as `[*index* [*start*] [*limit*]]`.
6464

65-
### Re-generate examples listed in rpc schemas (doc/schemas/lightning-*.json)
66-
67-
1. The `autogenerate-rpc-examples.py` script regenerates RPC examples for methods listed in `doc/schemas/lightning-*.json` files.
68-
2. It uses our pre-existing pytest suite to perform this task.
69-
3. The script runs a test named `test_generate_examples`, which sets up test nodes, records RPC requests, and captures responses. If you have added an RPC, make sure to add its use to the scripts here.
70-
4. To prevent accidental overwriting of examples with other tests, set the environment variable `GENERATE_EXAMPLES=True` before running the script.
71-
5. By default, all methods are regenerated. To specify which methods to regenerate, set the `REGENERATE` environment variable with a comma-separated list of method names. Eg. `REGENERATE='getinfo,connect'` will only regenerate examples for the getinfo and connect RPCs.
72-
6. The dev-bitcoind-poll is set to 3 seconds. Ensure the `TIMEOUT` environment variable is set to more than 3 seconds to avoid test failures due to a short waiting time for bitcoind responses.
73-
7. To run the script using Poetry, use the following command:
65+
## Generating Examples in Schema
66+
The `tests/autogenerate-rpc-examples.py` test script regenerates RPC examples for methods defined
67+
in `doc/schemas/lightning-*.json`. These examples are located at the end of each schema page,
68+
detailing `shell` and `json` request formats along with their corresponding `json` responses.
69+
The script utilizes the pytest suite to automate this task by running a test, `test_generate_examples`,
70+
that sets up test nodes, records RPC requests, and captures responses. Any new RPC command's examples
71+
should also be included in this scripts. This test only executes example generation if `GENERATE_EXAMPLES=True`
72+
is set, preventing accidental overwrites from unrelated tests.
73+
74+
### Adding New Examples
75+
1. Define a New Function (if needed):
76+
- If adding multiple examples for the same feature (e.g., `askrene`), create a new function. Otherwise, use an existing relevant function.
77+
2. Add the update_example Method:
78+
- Define examples using `update_example` with parameters: `node method params [res] [description]`.
79+
`node`: Specifies the node to execute the RPC.
80+
`method`: The RPC method name.
81+
`params`: RPC parameters in JSON or list format.
82+
`response (optional)`: Specify for wait commands or pre-recorded responses.
83+
`description (optional)`: Brief explanation of the example.
84+
3. Update the Ignore List:
85+
- Remove the RPC method name from `IGNORE_RPCS_LIST` to include it in the example generation.
86+
4. Run and Refine:
87+
- Run the test multiple times to detect variable values in responses:
88+
```bash
89+
rm -rf /tmp/ltests* && make -s && VALGRIND=0 TIMEOUT=40 TEST_DEBUG=1 pytest -vvv -n 6 tests/autogenerate-rpc-examples.py
90+
```
91+
- Identify changing values, and add them to `REPLACE_RESPONSE_VALUES`:
92+
```bash
93+
REPLACE_RESPONSE_VALUES.extend([
94+
{'data_keys': ['xyz'], 'original_value': l1.info['xyz'], 'new_value': NEW_VALUES_LIST['xyz_value_1']}
95+
])
96+
```
97+
- If `xyz_value_1` already does not exist in the list, add it to `NEW_VALUES_LIST`.
98+
99+
100+
### Avoiding Missing Example Errors (MissingExampleError)
101+
- If an RPC is in progress and lacks examples, add it to `IGNORE_RPCS_LIST` to bypass the auto-generation requirement.
102+
103+
104+
### Manually Regenerating Specific Examples
105+
1. By default, all methods are regenerated. To specify which methods to regenerate, set the `REGENERATE`
106+
environment variable with a comma-separated list of method names. Eg. `REGENERATE='getinfo,connect'` will
107+
only regenerate examples for the `getinfo` and `connect` RPCs.
108+
2. To regenerate specific examples, set the REGENERATE environment variable:
109+
```bash
110+
REGENERATE='getinfo,connect' VALGRIND=0 TIMEOUT=10 TEST_DEBUG=1 pytest -vvv -n 6 tests/autogenerate-rpc-examples.py
74111
```
75-
rm -rf /tmp/ltests* && REGENERATE='getinfo,connect' VALGRIND=0 TIMEOUT=10 TEST_DEBUG=1 pytest -vvv -s -p no:logging -n 6 tests/autogenerate-rpc-examples.py
76-
```
77-
8. The script saves logs in a file named `autogenerate-examples-status.log`, located in the root directory.
78-
9. After running the script, execute `make` to ensure that the schema has been updated in all relevant locations, such as `...msggen/schema.json`.
112+
3. Logs are saved in `tests/autogenerate-examples-status.log`, and JSON data is in `tests/autogenerate-examples.json`.
113+
4. Run `make` after the script completes to ensure schema updates are applied in other places too, such as `...msggen/schema.json`.
114+
79115

80-
### JSON Drinking Game!
116+
## JSON Drinking Game!
81117

82118
1. Sip whenever you have an additional comma at the end of a sequence.
83119
2. Sip whenever you omit a comma in a sequence because you cut & paste.

0 commit comments

Comments
 (0)