Skip to main content

system_syncState - JSON-RPC Method

Description#

Returns the current synchronization state of the Moonbase Alpha node, including the starting block, current block, and highest known block. This method is essential for monitoring node synchronization progress, determining if a node is fully synced, and tracking the blockchain sync status. Applications can use this information to decide whether to proceed with queries or wait for synchronization to complete.

Parameters#

This method does not require any parameters.

Returns#

FieldTypeDescription
startingBlocknumberBlock number where synchronization started
currentBlocknumberCurrent block the node has synchronized to
highestBlocknumberHighest known block in the network

Use Cases#

  1. Sync Progress Monitoring: Display real-time synchronization progress in node dashboards
  2. Readiness Checks: Determine if a node is fully synced before accepting queries
  3. Health Monitoring: Track sync lag to identify performance or connectivity issues
  4. Load Balancing: Route queries to fully synced nodes in multi-node setups
  5. Alert Systems: Trigger notifications when nodes fall behind synchronization
  6. Development Tools: Wait for local nodes to sync before running integration tests

Request Example#

curl -X POST https://api-moonbase-alpha.n.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"system_syncState","params":[]}'

Response Example#

{
"jsonrpc": "2.0",
"result": {
"startingBlock": 1000000,
"currentBlock": 1500000,
"highestBlock": 1500050
},
"id": 1
}

Code Examples#

JavaScript (polkadot.js)#

import { ApiPromise, WsProvider } from '@polkadot/api';

const api = await ApiPromise.create({
provider: new WsProvider('wss://api-moonbase-alpha.n.dwellir.com/YOUR_API_KEY')
});
const syncState = await api.rpc.system.syncState();
const current = syncState.currentBlock.toNumber();
const highest = syncState.highestBlock.toNumber();
const progress = ((current / highest) * 100).toFixed(2);
console.log(`Sync progress: ${progress}% (${current}/${highest})`);

Python#

import requests
import json

def get_sync_state():
url = "https://api-moonbase-alpha.n.dwellir.com/YOUR_API_KEY"
payload = {
"jsonrpc": "2.0",
"method": "system_syncState",
"params": [],
"id": 1
}
response = requests.post(url, json=payload)
return response.json()["result"]

sync_state = get_sync_state()
current = sync_state["currentBlock"]
highest = sync_state["highestBlock"]
blocks_behind = highest - current
progress = (current / highest) * 100

print(f"Current block: {current}")
print(f"Highest block: {highest}")
print(f"Blocks behind: {blocks_behind}")
print(f"Sync progress: {progress:.2f}%")

# Check if fully synced (within 10 blocks)
if blocks_behind <= 10:
print("Node is fully synced!")
else:
print(f"Node is syncing... {blocks_behind} blocks behind")

Node.js (fetch)#

async function waitForSync(threshold = 10) {
const response = await fetch('https://api-moonbase-alpha.n.dwellir.com/YOUR_API_KEY', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'system_syncState',
params: [],
id: 1
})
});
const data = await response.json();
const { currentBlock, highestBlock } = data.result;
const isSynced = (highestBlock - currentBlock) <= threshold;
return { isSynced, currentBlock, highestBlock };
}

const { isSynced, currentBlock, highestBlock } = await waitForSync();
console.log(`Node synced: ${isSynced} (${currentBlock}/${highestBlock})`);

Best Practices#

  • Consider a node "synced" when within 5-10 blocks of the highest block
  • Poll sync state periodically (every 5-10 seconds) during synchronization
  • Implement exponential backoff when waiting for synchronization
  • Display sync progress to users when operations require a synced node
  • Set appropriate timeouts for sync-dependent operations in production
  • Use sync state to determine node health in monitoring systems