RPC command protocol detailed definition.
Includes command line format, options and arguments, JSON-RPC request and response field, errors and examples.
JSON-RPC version is 2.0.
RPC protocol document see
- Response object is required.
- In request object, params can't be omitted.
- Bat request only support HTTP request, command line doesn't support now.
- If type set
arrayin configuration, it's exactly express there may be multiple same type elements in it. Specially in request object, don't allow"params": [1, "2", true], useobjectinstead.
- script/rpc.json: RPC protocol file.
- srcipt/rpc_generator.py: rpc generator script.
- srcipt/rpc_protocol_generator.py: protocol generator script.
- src/mode/auto_rpc.h, auto_rpc.cpp: rpc cpp file.
- src/mode/auto_protocol.h, auto_protocol.cpp: detail protocol cpp file.
- configure script/rpc.json
- run srcipt/generator.py(all generator entry)
- check src/mode/auto_rpc.h.
// contain all protocols
template <typename... T>
CBasicConfig* CreateConfig(const std::string& cmd)
{
if (cmd == "help")
{
return new mode_impl::CCombinConfig<CHelpConfig, T...>;
}
else if (cmd == "stop")
{
return new mode_impl::CCombinConfig<CStopConfig, T...>;
}
...
}
...- build project
"encryptkey": {
"type": "command",
"name": "EncryptKey",
"introduction": "Encrypts the key.",
"desc": [
"Encrypts the key assoiciated with <passphrase>. ",
"For encrypted key, changes the passphrase for [oldpassphrase] to <passphrase>"
],
"request": {
"type": "object",
"content": {
"pubkey": {
"type": "string",
"desc": "public key"
},
"passphrase": {
"type": "string",
"desc": "passphrase of key"
},
"oldpassphrase": {
"type": "string",
"desc": "old passphrase of key",
"required": false
}
}
},
"response": {
"type": "string",
"name": "result",
"desc": "encrypt key result"
},
"example": [
{
"request": "bigbang-cli encryptkey 2e05c9ee45fdf58f7b007458298042fc3d3ad416a2f9977ace16d14164a3e882 123",
"response": "Encrypt key successfully: 2e05c9ee45fdf58f7b007458298042fc3d3ad416a2f9977ace16d14164a3e882"
},
{
"request": "{\"id\":64,\"method\":\"encryptkey\",\"jsonrpc\":\"2.0\",\"params\":{\"pubkey\":\"2e05c9ee45fdf58f7b007458298042fc3d3ad416a2f9977ace16d14164a3e882\",\"passphrase\":\"123\"}}",
"response": "{\"id\":64,\"jsonrpc\":\"2.0\",\"result\":\"Encrypt key successfully: 2e05c9ee45fdf58f7b007458298042fc3d3ad416a2f9977ace16d14164a3e882\"}"
},
"bigbang-cli encryptkey 2e05c9ee45fdf58f7b007458298042fc3d3ad416a2f9977ace16d14164a3e882 456 123"
],
"error": [
"{\"code\":-4,\"message\":\"Unknown key\"}",
"{\"code\":-406,\"message\":\"The passphrase entered was incorrect.\"}"
]
},- Two type struct can be defined in top level.
classandcommand. classis a cpp class for help organize cpp code. It's key is used reference bycommandcommandis a particular RPC command. It's key is JSON-RPC Method- fields
- type in
commandorclass, required. Value isclassorcommand.
// this is a class "templatepubkey": { "type": "class", // this is a command "encryptkey": { "type": "command",
- name, in
classandcommond, optional. means classname inclass, and part of classname incommand. If no name, use key instead.
// class name is TemplatePubKey "templatepubkey": { "name": "TemplatePubKey", // generator at most 3 classes by EncryptKey. CEncryptKeyParam, CEncryptKeyResult, CEncryptKeyConfig // format: 'C' + name + 'Param'|'Result'|'Config' "encryptkey": { "name": "EncryptKey",
- introduction, optional. Showing on summary help. If not be specified, use desc instead.
"introduction": "Get transaction pool info",
- desc in
command, optional. Showing on help information. See - request in
command, required. Define JSON-RPC request field. - response in
command, required. Define JSON-RPC response field. (In this project must define response, although in JSON-RPC standard response is optional.) - example in
command, optional. Showing on help information. Support string or particular object.
"example": [ // use object. Key is "request" or "response", means this example is a request or response { "request": "bigbang-cli addnewtemplate mint '{\"mint\": \"e8e3770e774d5ad84a8ea65ed08cc7c5c30b42e045623604d5c5c6be95afb4f9\", \"spent\": \"1z6taz5dyrv2xa11pc92y0ggbrf2wf36gbtk8wjprb96qe3kqwfm3ayc1\"}'", "response": "20g0b87qxcd52ceh9zmpzx0hy46pjfzdnqbkh8f4tqs4y0r6sxyzyny25" }, // use string. Means this example is a request "bigbang-cli addnewtemplate delegate '{\"delegate\":\"2e05c9ee45fdf58f7b007458298042fc3d3ad416a2f9977ace16d14164a3e882\",\"owner\":\"1gbma6s21t4bcwymqz6h1dn1t7qy45019b1t00ywfyqymbvp90mqc1wmq\"}'", ],
- error in
commmand, optional. Showing on help information. Format is similar to desc
// use string "error": "{\"code\":-6,\"message\":\"Invalid parameters,failed to make template\"}" // use string array "error": [ "{\"code\":-6,\"message\":\"Invalid parameters,failed to make template\"}", "{\"code\":-401,\"message\":\"Failed to add template\"}", ]
- type in
- overview
"request": {
"type": "object",
"content": {
"txid": {
"type": "string",
"desc": "transaction hash"
},
"serialized": {
"type": "bool",
"desc": [
"If serialized=0, returns an Object with information about <txid>.",
"If serialized is non-zero, returns a string that is",
"serialized, hex-encoded data for <txid>."
],
"opt": "s",
"default": false,
"required": false
}
}- fields
- type, required. Means JSON-RPC params type. Optional value is
objectorarray, or a class key. - content, required. see
- type, required. Means JSON-RPC params type. Optional value is
- overview
// one
"response": {
"type": "object",
"name": "location",
"content": {
"fork": {
"type": "string",
"desc": "fork hash"
},
"height": {
"type": "int",
"desc": "block height"
}
}
},
// another
"response": {
"type": "int",
"name": "count",
"desc": "fork count"
},- fields
- type, required. Means JSON-RPC result. Optional value see.
- name, required. Showing on help.
- desc, optional. Showing on help. See
- content, optional. See
Basic type values are int, uint, bool, double, string, object, array.
If type value is other string, it means a reference of class:
"templaterequest": {
"type": "class",
...
}
// reference of class "templaterequest"
"request": {
"type": "templaterequest"
},doubletype reveciedouble,int,uintin it's precision range- Data range is judged by the business level. RPC only is responsible for basic type range.
Support string or string array, and string array will be joined by \n.
"desc": [
"Encrypts the key assoiciated with <passphrase>. ",
"For encrypted key, changes the passphrase for [oldpassphrase] to <passphrase>"
],
"desc": "Returns the number of forks.",If type value is array or object, needs to set content to describe it's elements.
"list": {
"type": "array",
"desc": "transaction pool list",
"required": false,
"condition": "detail=true",
// because "list"-"type" = "array", so needs "content"
"content": {
"pool": {
"type": "object",
"desc": "pool struct",
// because "pool"-"type" = "object", so needs "content"
"content": {
"hex": {
"type": "string",
"desc": "tx pool hex"
},
"size": {
"type": "uint",
"desc": "tx pool size"
}
}
}
}
}- content is a json object. It's element key is param name, value is param attributes.
- If type is
array, content element number must be 1. - content could nest content, it depends on param's type.
- Generator supports nested array, but strongly suggest consider if is nessesary. Because in cpp code, it will appear
vector<vector<vector<...>> - Not support use
objectto mockmap<>in cpp, useobjectinarrayinstead. - If no params, request content should be
{}. Only used in Request
One param is a object, key is name and value is attributes.
- overview
// one
"serialized": {
"type": "bool",
"desc": [
"If serialized=0, returns an Object with information about <txid>.",
"If serialized is non-zero, returns a string that is",
"serialized, hex-encoded data for <txid>."
],
"opt": "s",
"default": false,
"required": false
}
// another
"type": {
"type": "string",
"desc": "template type"
},
"delegate": {
"type": "object",
"desc": "delegate template struct",
"condition": "type=delegate",
"content": {
"delegate": {
"type": "string",
"desc": "delegate public key"
},
"owner": {
"type": "string",
"desc": "owner address"
}
}
},- fields:
- type, see
- desc, see
- opt, optional. A string used for command line option.
In overview, "serialized"-"opt" = "s" and "type" = "bool", means in command line:If "type" != "bool", means:bigbang-cli COMMAND -s
NOTICE: Don't repeat with other optionsbigbang-cli COMMAND -s=value
- default, optional. Used for command line, if not specify this param, use default value to send JSON-RPC.
... "type": "bool", "default": false, ... ... "type": "int", "default": 0, ...
- required, optional, default=true. Used for JSON-RPC, if required param not be specify value or default, error occured.
- condition, optional. String format:
key=value, used in business code, meansif key=value, this param is valid.
keymust be the same level with this param.Specially, if"type": { "type": "string", "desc": "template type" }, "delegate": { "type": "object", "desc": "delegate template struct", // this means if "type" = "delegate", "delegate" param is valid "condition": "type=delegate", "content": { "delegate": { "type": "string", "desc": "delegate public key" }, "owner": { "type": "string", "desc": "owner address" } } },
keynot is the same level with this param, it's only show in help"response": { "type": "object", "name": "data", "content": { "result": { "type": "bool", "desc": "result", "required": false, // there is no "prev" in same level, this only show in help "condition": "prev=matched or block is not generated by POW" },
- put
required=falseparams behindrequired=trueparams, or generator do this. - Suggest if one param has opt, params behind it also have opt to avoid ambiguous.
- request example
curl -d '[{"id":13,"method":"unlockkey","jsonrpc":"2.0","params":{"pubkey":"d716e72ce58e649a57d54751a7707e325b522497da3a69ae8301a2cbec391c07","passphrase":"1234"}},{"id":14,"method":"unlockkey","jsonrpc":"2.0","params":{"pubkey":"2e05c9ee45fdf58f7b007458298042fc3d3ad416a2f9977ace16d14164a3e882","passphrase":"1234"}}]' http://localhost:9902- resonse example
[{"id":13,"jsonrpc":"2.0","result":"Unlock key successfully: d716e72ce58e649a57d54751a7707e325b522497da3a69ae8301a2cbec391c07"},{"id":14,"jsonrpc":"2.0","error":{"code":-409,"message":"Key is already unlocked"}}]In script/template/mode.json, there define help info of modes.
{
"EModeType::MODE_SERVER": { // EModeType in src/mode/mode_type.h
"usage": "bigbang-server (OPTIONS)", // usage info
"desc": "Run bigbang server" // desc info
},
...
}