2018-07-22 22:12:07 +03:00
# Grin Stratum RPC Protocol
[1.1.0] Merge master into 1.1.0 (#2720)
* cleanup legacy "3 dot" check (#2625)
* Allow to peers behind NAT to get up to preferred_max connections (#2543)
Allow to peers behind NAT to get up to preffered_max connections
If peer has only outbound connections it's mot likely behind NAT and we should not stop it from getting more outbound connections
* Reduce usage of unwrap in p2p crate (#2627)
Also change store crate a bit
* Simplify (and fix) output_pos cleanup during chain compaction (#2609)
* expose leaf pos iterator
use it for various things in txhashset when iterating over outputs
* fix
* cleanup
* rebuild output_pos index (and clear it out first) when compacting the chain
* fixup tests
* refactor to match on (output, proof) tuple
* add comments to compact() to explain what is going on.
* get rid of some boxing around the leaf_set iterator
* cleanup
* [docs] Add switch commitment documentation (#2526)
* remove references to no-longer existing switch commitment hash
(as switch commitments were removed in ca8447f3bd49e80578770da841e5fbbac2c23cde
and moved into the blinding factor of the Pedersen Commitment)
* some rewording (points vs curves) and fix of small formatting issues
* Add switch commitment documentation
* [docs] Documents in grin repo had translated in Korean. (#2604)
* Start to M/W intro translate in Korean
* translate in Korean
* add korean translation on intro
* table_of_content.md translate in Korean.
* table_of_content_KR.md finish translate in Korean, start to translate State_KR.md
* add state_KR.md & commit some translation in State_KR.md
* WIP stat_KR.md translation
* add build_KR.md && stratum_KR.md
* finish translate stratum_KR.md & table_of_content_KR.md
* rename intro.KR.md to intro_KR.md
* add intro_KR.md file path each language's intro.md
* add Korean translation file path to stratum.md & table_of_contents.md
* fix difference with grin/master
* Fix TxHashSet file filter for Windows. (#2641)
* Fix TxHashSet file filter for Windows.
* rustfmt
* Updating regexp
* Adding in test case
* Display the current download rate rather than the average when syncing the chain (#2633)
* When syncing the chain, calculate the displayed download speed using the current rate from the most recent iteration, rather than the average download speed from the entire syncing process.
* Replace the explicitly ignored variables in the pattern with an implicit ignore
* remove root = true from editorconfig (#2655)
* Add Medium post to intro (#2654)
Spoke to @yeastplume who agreed it makes sense to add the "Grin Transactions Explained, Step-by-Step" Medium post to intro.md
Open for suggestions on a better location.
* add a new configure item for log_max_files (#2601)
* add a new configure item for log_max_files
* rustfmt
* use a constant instead of multiple 32
* rustfmt
* Fix the build warning of deprecated trim_right_matches (#2662)
* [DOC] state.md, build.md and chain directory documents translate in Korean. (#2649)
* add md files for translation.
* start to translation fast-sync, code_structure. add file build_KR.md, states_KR.md
* add dandelion_KR.md && simulation_KR.md for Korean translation.
* add md files for translation.
* start to translation fast-sync, code_structure. add file build_KR.md, states_KR.md
* add dandelion_KR.md && simulation_KR.md for Korean translation.
* remove some useless md files for translation. this is rearrange set up translation order.
* add dot end of sentence & translate build.md in korean
* remove fast-sync_KR.md
* finish build_KR.md translation
* finish build_KR.md translation
* finish translation state_KR.md & add phrase in state.md to move other language md file
* translate blocks_and_headers.md && chain_sync.md in Korean
* add . in chain_sync.md , translation finished in doc/chain dir.
* fix some miss typos
* Api documentation fixes (#2646)
* Fix the API documentation for Chain Validate (v1/chain/validate). It was documented as a POST, but it is actually a GET request, which can be seen in its handler ChainValidationHandler
* Update the API V1 route list response to include the headers and merkleproof routes. Also clarify that for the chain/outputs route you must specify either byids or byheight to select outputs.
* refactor(ci): reorganize CI related code (#2658)
Break-down the CI related code into smaller more maintainable pieces.
* Specify grin or nanogrins in API docs where applicable (#2642)
* Set Content-Type in API client (#2680)
* Reduce number of unwraps in chain crate (#2679)
* fix: the restart of state sync doesn't work sometimes (#2687)
* let check_txhashset_needed return true on abnormal case (#2684)
* Reduce number of unwwaps in api crate (#2681)
* Reduce number of unwwaps in api crate
* Format use section
* Small QoL improvements for wallet developers (#2651)
* Small changes for wallet devs
* Move create_nonce into Keychain trait
* Replace match by map_err
* Add flag to Slate to skip fee check
* Fix secp dependency
* Remove check_fee flag in Slate
* Add Japanese edition of build.md (#2697)
* catch the panic to avoid peer thread quit early (#2686)
* catch the panic to avoid peer thread quit before taking the chance to ban
* move catch wrapper logic down into the util crate
* log the panic info
* keep txhashset.rs untouched
* remove a warning
* [DOC] dandelion.md, simulation.md ,fast-sync.md and pruning.md documents translate in Korean. (#2678)
* Show response code in API client error message (#2683)
It's hard to investigate what happens when an API client error is
printed out
* Add some better logging for get_outputs_by_id failure states (#2705)
* Switch commitment doc fixes (#2645)
Fix some typos and remove the use of parentheses in a
couple of places to make the reading flow a bit better.
* docs: update/add new README.md badges (#2708)
Replace existing badges with SVG counterparts and add a bunch of new ones.
* Update intro.md (#2702)
Add mention of censoring attack prevented by range proofs
* use sandbox folder for txhashset validation on state sync (#2685)
* use sandbox folder for txhashset validation on state sync
* rustfmt
* use temp directory as the sandbox instead actual db_root txhashset dir
* rustfmt
* move txhashset overwrite to the end of full validation
* fix travis-ci test
* rustfmt
* fix: hashset have 2 folders including txhashset and header
* rustfmt
*
(1)switch to rebuild_header_mmr instead of copy the sandbox header mmr
(2)lock txhashset when overwriting and opening and rebuild
* minor improve on sandbox_dir
* add Japanese edition of state.md (#2703)
* Attempt to fix broken TUI locale (#2713)
Can confirm that on the same machine 1.0.2 TUI looks great and is broken on
the current master. Bump of `cursive` version fixed it for me.
Fixes #2676
* clean the header folder in sandbox (#2716)
* forgot to clean the header folder in sandbox in #2685
* Reduce number of unwraps in servers crate (#2707)
It doesn't include stratum server which is sufficiently changed in 1.1
branch and adapters, which is big enough for a separate PR.
* rustfmt
* change version to beta
2019-04-01 13:47:48 +03:00
*Read this in other languages: [Korean ](stratum_KR.md ).*
2018-07-22 22:12:07 +03:00
This document describes the current Stratum RPC protocol implemented in Grin.
## Table of Contents
1. [Messages ](#messages )
1. [getjobtemplate ](#getjobtemplate )
2018-10-03 23:31:28 +03:00
1. [job ](#job )
1. [keepalive ](#keepalive )
1. [login ](#login )
1. [status ](#status )
1. [submit ](#submit )
1. [Error Messages ](#error-messages )
1. [Miner Behavior ](#miner-behavior )
1. [Reference Implementation ](#reference-implementation )
2018-07-22 22:12:07 +03:00
2018-10-03 23:31:28 +03:00
## Messages
2018-07-22 22:12:07 +03:00
In this section, we detail each message and the potential response.
At any point, if miner the tries to do one of the following request (except login) and login is required, the miner will receive the following error message.
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :-------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | method sent by the miner |
| error | {"code":-32500,"message":"login first"} |
Example:
```JSON
{
"id":"10",
"jsonrpc":"2.0",
"method":"getjobtemplate",
"error":{
"code":-32500,
"message":"login first"
}
}
```
if the request is not one of the following, the stratum server will give this error response:
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :------------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | method sent by the miner |
| error | {"code":-32601,"message":"Method not found"} |
Example:
```JSON
{
"id":"10",
"jsonrpc":"2.0",
"method":"getgrins",
"error":{
"code":-32601,
"message":"Method not found"
}
}
```
2018-10-17 02:31:57 +03:00
### `getjobtemplate`
2018-07-22 22:12:07 +03:00
A message initiated by the miner.
Miner can request a job with this message.
#### Request
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :----------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "getjobtemplate" |
| params | null |
Example:
``` JSON
{
"id":"2",
"jsonrpc":"2.0",
"method":"getjobtemplate",
"params":null
}
```
#### Response
The response can be of two types:
##### OK response
Example:
``` JSON
{
"id":"0",
"jsonrpc":"2.0",
"method":"getjobtemplate",
"result":{
"difficulty":1,
"height":13726,
"job_id":4,
"pre_pow":"00010000000000003c4d0171369781424b39c81eb39de10cdf4a7cc27bbc6769203c7c9bc02cc6a1dfc6000000005b50f8210000000000395f123c6856055aab2369fe325c3d709b129dee5c96f2db60cdbc0dc123a80cb0b89e883ae2614f8dbd169888a95c0513b1ac7e069de82e5d479cf838281f7838b4bf75ea7c9222a1ad7406a4cab29af4e018c402f70dc8e9ef3d085169391c78741c656ec0f11f62d41b463c82737970afaa431c5cabb9b759cdfa52d761ac451276084366d1ba9efff2db9ed07eec1bcd8da352b32227f452dfa987ad249f689d9780000000000000b9e00000000000009954"
}
}
2018-10-17 02:31:57 +03:00
```
2018-07-22 22:12:07 +03:00
##### Error response
If the node is syncing, it will send the following message:
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :-------------------------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "getjobtemplate" |
| error | {"code":-32701,"message":"Node is syncing - Please wait"} |
Example:
```JSON
{
"id":"10",
"jsonrpc":"2.0",
"method":"getjobtemplate",
"error":{
"code":-32000,
"message":"Node is syncing - Please wait"
}
}
```
2018-10-17 02:31:57 +03:00
### `job`
2018-07-22 22:12:07 +03:00
A message initiated by the Stratum server.
Stratum server will send job automatically to connected miners.
2018-09-03 21:15:18 +03:00
The miner SHOULD interrupt current job if job_id = 0, and SHOULD replace the current job with this one after the current graph is complete.
2018-07-22 22:12:07 +03:00
#### Request
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :------------------------------------------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "job" |
2018-10-17 02:31:57 +03:00
| params | Int `difficulty` , `height` , `job_id` and string `pre_pow` |
2018-07-22 22:12:07 +03:00
Example:
``` JSON
{
"id":"Stratum",
"jsonrpc":"2.0",
"method":"job",
"params":{
"difficulty":1,
"height":16375,
"job_id":5,
"pre_pow":"00010000000000003ff723bc8c987b0c594794a0487e52260c5343288749c7e288de95a80afa558c5fb8000000005b51f15f00000000003cadef6a45edf92d2520bf45cbd4f36b5ef283c53d8266bbe9aa1b8daaa1458ce5578fcb0978b3995dd00e3bfc5a9277190bb9407a30d66aec26ff55a2b50214b22cdc1f3894f27374f568b2fe94d857b6b3808124888dd5eff7e8de7e451ac805a4ebd6551fa7a529a1b9f35f761719ed41bfef6ab081defc45a64a374dfd8321feac083741f29207b044071d93904986fa322df610e210c543c2f95522c9bdaef5f598000000000000c184000000000000a0cf"
}
}
```
#### Response
No response is required for this message.
2018-10-17 02:31:57 +03:00
### `keepalive`
2018-07-22 22:12:07 +03:00
A message initiated by the miner in order to keep the connection alive.
#### Request
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :--------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "keepalive" |
| params | null |
Example:
``` JSON
{
"id":"2",
"jsonrpc":"2.0",
"method":"keepalive",
"params":null
}
```
#### Response
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :----------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "keepalive" |
| result | "ok" |
| error | null |
Example:
``` JSON
{
"id":"2",
"jsonrpc":"2.0",
"method":"keepalive",
"result":"ok",
"error":null
}
```
2018-10-17 02:31:57 +03:00
### `login`
2018-07-22 22:12:07 +03:00
***
A message initiated by the miner.
Miner can log in on a Grin Stratum server with a login, password and agent (usually statically set by the miner program).
#### Request
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :----------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "login" |
| params | Strings: login, pass and agent |
Example:
``` JSON
{
"id":"0",
"jsonrpc":"2.0",
"method":"login",
"params":{
"login":"login",
"pass":"password",
"agent":"grin-miner"
}
}
```
#### Response
The response can be of two types:
##### OK response
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :----------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "login" |
| result | "ok" |
| error | null |
Example:
``` JSON
{
"id":"1",
"jsonrpc":"2.0",
"method":"login",
"result":"ok",
"error":null
}
```
##### Error response
2018-10-17 02:31:57 +03:00
Not yet implemented. Should return error -32500 "Login first" when login is required.
2018-07-22 22:12:07 +03:00
2018-10-17 02:31:57 +03:00
### `status`
2018-07-22 22:12:07 +03:00
A message initiated by the miner.
This message allows a miner to get the status of its current worker and the network.
#### Request
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :--------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "status" |
| params | null |
Example:
``` JSON
{
"id":"2",
"jsonrpc":"2.0",
"method":"status",
"params":null
}
```
#### Response
The response is the following:
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :------------------------------------------------------------------------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "status" |
2018-10-17 02:31:57 +03:00
| result | String `id` . Integers `height` , `difficulty` , `accepted` , `rejected` and `stale` |
2018-07-22 22:12:07 +03:00
| error | null |
Example:
```JSON
{
"id":"5",
"jsonrpc":"2.0",
"method":"status",
"result":{
"id":"5",
"height":13726,
"difficulty":1,
"accepted":0,
"rejected":0,
"stale":0
},
"error":null
}
```
2018-10-17 02:31:57 +03:00
### `submit`
2018-07-22 22:12:07 +03:00
A message initiated by the miner.
When a miner find a share, it will submit it to the node.
#### Request
The miner submit a solution to a job to the Stratum server.
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :-------------------------------------------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "submit" |
2018-10-17 02:31:57 +03:00
| params | Int `edge_bits` ,`nonce`, `height` , `job_id` and array of integers `pow` |
2018-07-22 22:12:07 +03:00
Example:
``` JSON
2018-10-17 02:31:57 +03:00
{
2018-07-22 22:12:07 +03:00
"id":"0",
"jsonrpc":"2.0",
"method":"submit",
2018-10-17 02:31:57 +03:00
"params":{
"edge_bits":29,
2018-07-22 22:12:07 +03:00
"height":16419,
"job_id":0,
"nonce":8895699060858340771,
"pow":[
4210040,10141596,13269632,24291934,28079062,84254573,84493890,100560174,100657333,120128285,130518226,140371663,142109188,159800646,163323737,171019100,176840047,191220010,192245584,198941444,209276164,216952635,217795152,225662613,230166736,231315079,248639876,263910393,293995691,298361937,326412694,330363619,414572127,424798984,426489226,466671748,466924466,490048497,495035248,496623057,502828197, 532838434
]
}
}
```
#### Response
The response can be of three types.
##### OK response
2018-10-17 02:31:57 +03:00
The share is accepted by the Stratum but is not a valid cuck(at)oo solution at the network target difficulty.
2018-07-22 22:12:07 +03:00
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :----------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "submit" |
| result | "ok" |
| error | null |
Example:
``` JSON
{
"id":"2",
"jsonrpc":"2.0",
"method":"submit",
"result":"ok",
"error":null
}
```
##### Blockfound response
2018-10-17 02:31:57 +03:00
The share is accepted by the Stratum and is a valid cuck(at)oo solution at the network target difficulty.
2018-07-22 22:12:07 +03:00
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :----------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "submit" |
| result | "block - " + hash of the block |
| error | null |
Example:
``` JSON
{
"id":"6",
"jsonrpc":"2.0",
"method":"submit",
"result":"blockfound - 23025af9032de812d15228121d5e4b0e977d30ad8036ab07131104787b9dcf10",
"error":null
}
```
##### Error response
The error response can be of two types: stale and rejected.
##### Stale share error response
The share is a valid solution to a previous job not the current one.
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :-------------------------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "submit" |
| error | {"code":-32503,"message":"Solution submitted too late"} |
Example:
```JSON
{
"id":"5",
"jsonrpc":"2.0",
"method":"submit",
"error":{
"code":-32503,
"message":"Solution submitted too late"
}
}
```
##### Rejected share error responses
Two possibilities: the solution cannot be validated or the solution is of too low difficulty.
###### Failed to validate solution error
The submitted solution cannot be validated.
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :-------------------------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "submit" |
| error | {"code":-32502,"message":"Failed to validate solution"} |
Example:
```JSON
{
"id":"5",
"jsonrpc":"2.0",
"method":"submit",
"error":{
"code":-32502,
"message":"Failed to validate solution"
}
}
```
###### Share rejected due to low difficulty error
The submitted solution is of too low difficulty.
| Field | Content |
2018-10-17 02:31:57 +03:00
| :------------ | :--------------------------------------------------------------- |
2018-07-22 22:12:07 +03:00
| id | ID of the request |
| jsonrpc | "2.0" |
| method | "submit" |
| error | {"code":-32501,"message":"Share rejected due to low difficulty"} |
Example:
```JSON
{
"id":"5",
"jsonrpc":"2.0",
"method":"submit",
"error":{
"code":-32501,
"message":"Share rejected due to low difficulty"
}
}
```
2018-10-03 23:31:28 +03:00
## Error Messages
2018-07-22 22:12:07 +03:00
Grin Stratum protocol implementation contains the following error message:
| Error code | Error Message |
2018-10-17 02:31:57 +03:00
| :---------- | :------------------------------------- |
2018-07-22 22:12:07 +03:00
| -32000 | Node is syncing - please wait |
| -32500 | Login first |
| -32501 | Share rejected due to low difficulty |
| -32502 | Failed to validate solution |
| -32503 | Solution Submitted too late |
| -32600 | Invalid Request |
| -32601 | Method not found |
2018-10-03 23:31:28 +03:00
## Miner behavior
2018-07-22 22:12:07 +03:00
Miners SHOULD, MAY or MUST respect the following rules:
2018-12-18 14:12:05 +03:00
- Miners SHOULD randomize the job nonce before starting
2018-07-22 22:12:07 +03:00
- Miners MUST continue mining the same job until the server sends a new one, though a miner MAY request a new job at any time
- Miners MUST NOT send an rpc response to a job request from the server
- Miners MAY set the RPC "id" and expect responses to have that same id
- Miners MAY send a keepalive message
- Miners MAY send a login request (to identify which miner finds shares / solutions in the logs), the login request MUST have all 3 params.
- Miners MUST return the supplied job_id with submit messages.
2018-10-03 23:31:28 +03:00
## Reference Implementation
2018-07-22 22:12:07 +03:00
2018-09-03 21:15:18 +03:00
The current reference implementation is available at [mimblewimble/grin-miner ](https://github.com/mimblewimble/grin-miner/blob/master/src/bin/client.rs ).