Blockchain's Core Communication and Consensus
JSON-RPC in Blockchain
Talking to the Blockchain
Think of a blockchain as a massive, public library. It holds vast amounts of information, like transaction records and account balances, all organized into blocks. But how do you actually get information from this library or add a new record? You can't just walk in and shout. You need a standardized way to communicate with the librarian, who in this case is a blockchain node.
This is where JSON-RPC comes in. It's the language applications use to talk to blockchain nodes.
JSON-RPC is a stateless, lightweight remote procedure call (RPC) protocol.
Let's break that down. "Remote Procedure Call" (RPC) simply means one computer can ask another computer to run a function and send back the result. "JSON" refers to the format of the messages, which is human-readable and easy for machines to parse. Essentially, your application sends a JSON-formatted request to a blockchain node, and the node sends a JSON-formatted response back.
Requests and Responses
Every JSON-RPC interaction follows a simple request-response pattern. Your application, the client, constructs a request that specifies exactly what it wants to do.
{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}
Here’s what each part means:
jsonrpc: Specifies the protocol version, which is almost always "2.0".method: The name of the function to be executed on the node. In this case,eth_blockNumberasks for the number of the most recent block.params: An array of parameters needed by the method. Sinceeth_blockNumberneeds no extra information, the array is empty.id: A unique identifier for the request. This helps the client match incoming responses to the requests it sent.
After the node processes the request, it sends back a response.
{
"jsonrpc": "2.0",
"result": "0x10B332A",
"id": 1
}
The response structure mirrors the request:
jsonrpc: The same version string.result: The data requested. Here, it's the latest block number, returned as a hexadecimal string. This value,0x10B332A, is 17,511,210 in decimal.id: The same ID from the original request, so your application knows which question this response answers.
Common Blockchain Methods
While there are many JSON-RPC methods, a few are fundamental for building most decentralized applications (dApps). They allow you to read data from the blockchain and ask it to perform actions.
| Method | Description |
|---|---|
eth_blockNumber | Gets the number of the most recent block. |
eth_getBalance | Retrieves the Ether balance of a specific account. |
eth_getTransactionByHash | Fetches the details of a transaction given its hash. |
eth_sendRawTransaction | Submits a pre-signed transaction to the network. |
Let's see how you might use one of these. To check the balance of an account, you would use the eth_getBalance method. This method requires two parameters: the wallet address you want to query and a block number (or a tag like "latest") to specify which point in time you're interested in.
{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": [
"0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"latest"
],
"id": 2
}
The node would then respond with the account's balance in Wei (the smallest unit of Ether), again formatted as a hexadecimal string.
By combining these simple, powerful commands, developers can build complex applications that read from and write to the blockchain, all without needing to run a full node themselves. They can connect to public nodes or specialized node-as-a-service providers that handle the underlying infrastructure. JSON-RPC is the universal language that makes this entire ecosystem work.
What is the primary purpose of JSON-RPC in the context of blockchains?
In a JSON-RPC request, which field specifies the function to be executed on the blockchain node?