Ana içeriğe geç

Light Client Nasıl Yerleştirilir?

Fetching the number of the latest block processed by light client​

To fetch the number of the latest block processed by light client, we can perform GET request on /v1/latest_block endpoint.

use reqwest::StatusCode;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LatestBlock {
pub latest_block: u32,
}

const LIGHT_CLIENT_URL: &str = "http://localhost:7000";

let latest_block_url = format!("{LIGHT_CLIENT_URL}/v1/latest_block");
let response = reqwest::get(latest_block_url).await.unwrap();

if response.status() == StatusCode::OK {
let latest_block: LatestBlock =
serde_json::from_str(&response.text().await.unwrap()).unwrap();
println!("{latest_block:?}");
}
// ...error handling...

Fetching the confidence for given block​

To fetch the confidence for specific block, which is already processed by application client, we can perform GET request on /v1/confidence/{block_number} endpoint.

curl "http://localhost:7000/v1/confidence/1"

Response:

{
"block": 1,
"confidence": 93.75,
"serialised_confidence": "5232467296"
}

Fetching decoded application data for given block​

After data is verified, it can be fetched with GET request on /v1/appdata/{block_number} endpoint, by specifying decode=true query parameter.

JSON response​

curl "http://localhost:7000/v1/appdata/1?decode=true"

Response:

{
"block": 46,
"extrinsics": ["ZXhhbXBsZQ=="]
}

Decoded extrinsic​

curl -s "http://127.0.0.1:7000/v1/appdata/1?decode=true" | jq -r '.extrinsics[-1]' | base64 -d

Response:

"example"

Waiting for application data to be verified​

If light client is still processing specific block, we can poll light client with GET request on /v1/appdata/{block_number} endpoint, and wait for data to become available.

curl "http://localhost:7000/v1/appdata/2"

If response status code is 401 and body is:

"Processing block"

we need to poll endpoint until data becomes available.

API reference​

In case of error, endpoints will return response with 500 Internal Server Error status code, and descriptive error message.

GET /v1/mode​

Retrieves the operating mode of the light client. Light client can operate in two different modes, LightClient or AppClient, depending on configuration of application ID.

Responses​

If operating mode is LightClient response is:

Status code: 200 OK

"LightClient"

In case of AppClient mode, response is:

Status code: 200 OK

{"AppClient": {app_id}}

GET /v1/latest_block​

Retrieves the latest block processed by the light client.

Responses​

Status code: 200 OK

{"latest_block":{block_number}}

GET /v1/confidence/{block_number}​

Given a block number, it returns the confidence computed by the light client for that specific block.

Path parameters:

  • block_number - block number (required)

Responses​

In case when confidence is computed:

Status code: 200 OK

{ "block": 1, "confidence": 93.75, "serialised_confidence": "5232467296" }

If confidence is not computed, and specified block is before the latest processed block:

Status code: 400 Bad Request

"Not synced"

If confidence is not computed, and specified block is after the latest processed block:

Status code: 404 Not Found

"Not found"

GET /v1/appdata/{block_number}​

Given a block number, it retrieves the hex-encoded extrinsics for the specified block, if available. Alternatively, if specified by a query parameter, the retrieved extrinsic is decoded and returned as a base64-encoded string.

Path parameters:

  • block_number - block number (required)

Query parameters:

  • decode - true if decoded extrinsics are requested (boolean, optional, default is false)

Responses​

If application data is available, and decode is false or unspecified:

Status code: 200 OK

{
"block": 1,
"extrinsics": [
"0xc5018400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01308e88ca257b65514b7b44fc1913a6a9af6abc34c3d22761b0e425674d68df7de26be1c8533a7bbd01fdb3a8daa5af77df6d3fb0a67cde8241f461f4fe16f188000000041d011c6578616d706c65"
]
}

If application data is available, and decode is true:

Status code: 200 OK

{ "block": 1, "extrinsics": ["ZXhhbXBsZQ=="] }

If application data is not available, and specified block is the latest block:

Status code: 401 Unauthorized

"Processing block"

If application data is not available, and specified block is not the latest block:

Status code: 404 Not Found

"Not found"

GET /v1/status/{block_number}​

Retrieves the status of the latest block processed by the light client.

Path parameters:

  • block_number - block number (required)

Responses​

If latest processed block exists, and app_id is configured (otherwise, app_id is not set):

Status code: 200 OK

{ "block_num": 89, "confidence": 93.75, "app_id": 1 }

If there are no processed blocks:

Status code: 404 Not Found

"Not found"