DataLayer RPC
Note about Windows command escaping
This document will use Linux/MacOS RPC syntax. When running rpc commands on Windows, you'll need to escape all quotes with backslashes.
For example, here is a typical RPC command on Linux and MacOS:
chia rpc data_layer create_data_store '{"fee":"1000"}'
To run the same command on Windows, you need to escape the quotes with backslashes. In other words, add a \ before each double quote, such that:
"fee"
becomes\"fee\"
"1000"
becomes\"1000\"
- etc
Intro
This page includes a comprehensive list of Chia's DataLayer RPC API.
We also have documented the DataLayer CLI commands for interacting with the DataLayer.
By default, the DataLayer RPC API is exposed on port 8562. This is configured in ~/.chia/mainnet/config/config.yaml
under data_layer:rpc_port
. This port must not be exposed publicly for security concerns.
Commands that modify the blockchain include an optional fee. This fee can be specified in two ways:
- The "fee" parameter can be configured explicitly in the command, as several of the examples in this document show
- If the fee option is not explicitly specified, then the
data_layer:fee
setting in~/.chia/mainnet/config/config.yaml
will be used. By default, this is set to 1 billion mojos (0.001 XCH) - If neither of these options is set, then no fee will be used
For commands that change the state of the DataLayer singleton (ie all on-chain commands), you will need to wait for confirmation before running another command. If you run a command before the previous transaction has been confirmed, you will receive this error: Request failed: {'error': 'Already have a pending root waiting for confirmation.', 'success': False}
Note: Hashes used as arguments for the commands documented here may or may not be prefixed with 0x
. Both forms are valid syntax.
Reference
add_mirror
Functionality: Add a new mirror from an owned or subscribed data store. Triggers a Chia transaction
Usage: chia rpc data_layer [OPTIONS] add_mirror [REQUEST]
Options:
Short Command | Long Command | Type | Required | Description |
---|---|---|---|---|
-j | --json-file | FILENAME | False | Optionally instead of REQUEST you can provide a json file containing the request data |
-h | --help | None | False | Show a help message and exit |
Request Parameters:
Flag | Type | Required | Description |
---|---|---|---|
id | TEXT | True | The hexadecimal ID of the store to mirror |
urls | TEXT | True | A list of URLs where the mirror will reside |
amount | INTEGER | True | The number of mojos to spend to create the mirror. In theory, mirrors with a higher amount will be prioritized |
fee | TEXT | False | Set the fee for the transaction, in mojos |
Example
Create one mirror with multiple URLs:
chia rpc data_layer add_mirror '{"id":"0x1ad0908e248f48cc3e9b3cf8f68c748d2e3c5a2a933765032d3222086231ea5e", "urls":["http://www.example.com:8575", "http://www.example2.com:8575"], "amount":1000000}'
Response:
{
"success": true
}
add_missing_files
Functionality: Use the database to restore all files for one or more owned data stores
Note: For subscribed stores, this command will do nothing. Use unsubscribe and subscribe instead
Usage: chia rpc data_layer [OPTIONS] add_missing_files [REQUEST]
Options:
Short Command | Long Command | Type | Required | Description |
---|---|---|---|---|
-j | --json-file | FILENAME | False | Optionally instead of REQUEST you can provide a json file containing the request data |
-h | --help | None | False | Show a help message and exit |
Request Parameters:
Flag | Type | Required | Description |
---|---|---|---|
ids | TEXT | False | A list of hexadecimal store IDs to restore (default: all subscribed stores) |
override | BOOLEAN | False | If True , will overwrite files that already exist (default: False ) |
foldername | TEXT | False | The name of the folder where the files to be restored are located (default: ~/.chia/mainnet/data_layer/db/server_files_location_<network> ) |
Example
For this example, there is one owned store:
ls ~/.chia/mainnet/data_layer/db/server_files_location_testnet10/
Response:
1ad0908e248f48cc3e9b3cf8f68c748d2e3c5a2a933765032d3222086231ea5e-b5420e65846ded936d1e4855c066247fc461a3b281cd9e0e69f3cfa4df529ba2-delta-1-v1.0.dat
1ad0908e248f48cc3e9b3cf8f68c748d2e3c5a2a933765032d3222086231ea5e-b5420e65846ded936d1e4855c066247fc461a3b281cd9e0e69f3cfa4df529ba2-full-1-v1.0.dat
Intentionally move the files and create an empty folder. This will simulate file corruption:
mv ~/.chia/mainnet/data_layer/db/server_files_location_testnet10 ~/.chia/mainnet/data_layer/db/server_files_location_testnet10_bak
mkdir ~/.chia/mainnet/data_layer/db/server_files_location_testnet10/
Next, restore the files:
chia rpc data_layer add_missing_files
Response:
{
"success": true
}
Finally, verify that the files have been restored:
ls ~/.chia/mainnet/data_layer/db/server_files_location_testnet10/
Response:
1ad0908e248f48cc3e9b3cf8f68c748d2e3c5a2a933765032d3222086231ea5e-b5420e65846ded936d1e4855c066247fc461a3b281cd9e0e69f3cfa4df529ba2-delta-1-v1.0.dat
1ad0908e248f48cc3e9b3cf8f68c748d2e3c5a2a933765032d3222086231ea5e-b5420e65846ded936d1e4855c066247fc461a3b281cd9e0e69f3cfa4df529ba2-full-1-v1.0.dat
batch_update
Functionality: Apply multiple updates to a data store with a given changelist. Triggers a Chia transaction
Usage: chia rpc data_layer [OPTIONS] batch_update [REQUEST]
Options:
Short Command | Long Command | Type | Required | Description |
---|---|---|---|---|
-j | --json-file | FILENAME | False | Optionally instead of REQUEST you can provide a json file containing the request data |
-h | --help | None | False | Show a help message and exit |
Request Parameters:
Flag | Type | Required | Description |
---|---|---|---|
id | TEXT | True | The hexadecimal store ID |
changelist | TEXT | True | A string representing the changelist |
submit_on_chain | BOOLEAN | False | Specify whether to submit the update to the blockchain (True ), or to store it locally (False ). Default: True (See info box for details) |
fee | TEXT | False | Set the fee for the transaction, in mojos |
The submit_on_chain
flag was added in Chia version 2.3.0. In order to support the new functionality (submit an update off-chain), the schema for your root
table will be upgraded automatically upon starting the DataLayer service. This upgrade takes about three minutes to complete.
A few notes on the changelist
option:
- The entire list must be formatted as a JSON array
- There are two actions allowed:
insert
anddelete
insert
requireskey
andvalue
flagsdelete
requires akey
flag only- Keys and values must be written in hex format. Values can be derived from text or binary.
- Labels, keys and values must all be enclosed in double quotes
- Multiple inserts and deletes are allowed
- The size of a single
value
flag is limited to 100 MiB. However, adding anything close to that size has not been tested and could produce unexpected results
The following examples will show the basic functionality of this command.
Example 1 -- Insert a single key/value pair
chia rpc data_layer batch_update '{"id":"0x1163ac212bd5fe00efa86f8d3c4958cda08924870800d72dc332f508a1b2e35a", "changelist":[{"action":"insert", "key":"0003", "value":"abc123"}]}'