Page Content

Tutorials

Remote Procedure Call Explained: Calling Network Functions

Remote Procedure Call Explained

Remote Procedure Call Explained
Remote Procedure Call Explained

RPCs are important for programs to communicate with Ethereum network nodes. RPCs let web programs and other tools send transactions, manage accounts, and access the blockchain’s status from an Ethereum node.

Definition and Purpose

RPC is a distributed systems concept that enables a procedure on one computer to be called on another computer as though it were a local procedure call. Ethereum makes heavy use of this technique to facilitate communication between users and Decentralized Applications (DApps) and the blockchain.

It serves as a link between the underlying blockchain services and the application’s user interface (UI), and its functionalities are frequently called from “glue code” at the application level.

JSON-RPC Interface

  • Ethereum nodes mostly use a JSON-RPC interface to present a collection of widely recognised methods. This API calls remote procedures using JSON.
  • Lightweight, machine-parsable, and human-readable JSON. Name-value pairs and sorted lists are used.
  • JSON-RPC is lightweight and stateless. JSON-RPC requests often include:
    • jsonrpc: JSON-RPC protocol version string (“2.0”).
    • method: The method to call (e.g., eth_getTransactionCount or eth_coinbase).
    • params: This includes the values of the parameters used when the method was invoked.
    • id: A request number.
  • The majority of tools, including ethers.js and web3.js, manage the creation of these JSON-RPC calls for the developer.

JSON-RPC Connection Protocols

JSON-RPC Connection Protocols Three distinct protocols can be used by Ethereum nodes to process JSON-RPC messages:

  • HTTP Protocol: With an HTTP-based interface for POSTing JSON messages, this is the most straightforward choice. Basic authentication and HTTPS-encrypted connections may be necessary for certain nodes. For instance, Infura’s HTTP endpoint is straightforward but lacks expensive functions like newFilter.
  • WebSocket Protocol: This allows a client and a server to have a continuous two-way connection. It enables clients to subscribe to real-time changes pushed by the node and make JSON-RPC requests. Only WebSockets or IPC connections not HTTP allow subscriptions.
  • IPC Protocol (Inter-Process Communication): On a computer, this usually operates locally. When an Ethereum client like Geth constructs an IPC pipe on the local filesystem (geth.ipc), programs on the same computer can communicate bidirectionally. Web3.js needs RPC for local communication, unlike Geth and Mist.

web3.js and RPC

  • Web3.js connects to Ethereum blockchain nodes using RPCs. Web apps use it as their main Ethereum blockchain API.
  • After initialising the web3 object in DApps with port and provider, it RPCs to the node.
  • Web3.eth allows access to the eth object and its functionality for account, smart contract, and blockchain data interaction. Web3.js abstracts raw RPC calls with web3.eth.sendTransaction().
  • Connecting to an Ethereum client node requires web3.providers.
  • The basic blockchain protocol is implemented by the web3.core package, transaction broadcasting and receiving networking is handled by web3.net, and DApp communication is handled by web3.shh (Whisper protocol).

Geth and RPC

  • Popular Ethereum client Geth supports RPC.
  • Geth activates the HTTP-RPC server and controls access to eth, net, web3, and personal APIs using –rpc and –rpcapi.
  • TCP port 8545 is default for Geth HTTP-RPC.
  • Developers can directly interface with Geth over JSON-RPC over HTTP using curl for POST requests.
  • RPC-based functionality and interactions Numerous interactions with the Ethereum network are made possible by RPC calls:
  • Getting general network information is known as “querying network information.”
  • Account/Address Information: retrieving codes and balances.
  • Interaction of Smart Contracts:
    • Calling Contracts: Making read-only, or static, calls to smart contracts that don’t alter the status of the blockchain. These are handled by the node that gets the call and don’t cost gas. These calls are usually triggered by Solidity functions that are tagged with view or pure modifiers.
    • Sending Transactions: Sending commands to smart contracts so they can carry out state-changing tasks. These are broadcast to the network for mining and execution, and they cost gas.
  • Monitoring Changes:
    • Polling for New Blocks: Searching the node for fresh blocks on a regular basis.
    • Event Filters: Putting filters on a node to get new events that fit certain criteria.
    • Subscriptions: Real-time direct push notifications of additions or deletions resulting from chain reorganisations to the client using two-way connections (WebSockets/IPC).

Considerations and Security

  • RPC interfaces can present security vulnerabilities, particularly when they are made publicly available. Security breaches may result, for example, if –rpccorsdomain ‘*’ is used on public networks, allowing all external connections to the Ethereum node over the exposed RPC endpoint.
  • RPC is frequently utilised for local testing in conjunction with technologies such as Ganache, which offers a private Ethereum blockchain for smart contract development and testing.
  • For DApp developers, the web3.js framework simplifies these intricate interactions by offering an API to access blockchain node services.
Thota Nithya
Thota Nithyahttps://govindhtech.com/
Hai, Iam Nithya. My role in Govindhtech involves contributing to the platform's mission of delivering the latest news and insights on emerging technologies such as artificial intelligence, cloud computing, computer hardware, and mobile devices.
Index