How to create a RPC enabled private network in Ethereum

Let’s create a Geth private network with remote call procedure APIs.

Jayamine Alupotha
hackergirl
3 min readFeb 20, 2018

--

Ethereum is a platform to create decentralized applications which uses Blockchain data structure. Please install the Geth client before going to other sections. You can find the details about the installation here.

After installing, run the following command in your command line terminal.

> geth --help

Geth help command returns the Geth commands and options.

Now create a folder in your home folder. In the following sections, we will call that folder “<chain data folder>”. Make sure to keep that folder in a safe location.

Let’s create a private network by running the following command,

$ geth --identity "<name of your testnet>" --rpc --rpcport "<http port number>" --rpccorsdomain "*" --rpcapi "db,eth,net,web3" --datadir "<chain data folder>" --port "<port>" --nodiscover --networkid <id number> console 

Ex:

$ geth --identity "jay" --rpc --rpcport "8080" --rpccorsdomain "*" --rpcapi "db,eth,net,web3" --datadir "/home/jayamini/.ethereum/geth/chaindata/TestChain1" --port "30303" --nodiscover --networkid 1999 console
  • identity: Custom node name
  • rpc: Enable the HTTP-RPC server
  • rpcport: HTTP-RPC server listening port (default: 8545)
  • rpcapi: API’s offered over the HTTP-RPC interface
  • rpccorsdomain: Comma separated list of domains from which to accept cross origin requests (browser enforced)
  • datadir: Data directory for the databases and keystore
  • port: Network listening port (default: 30303)
  • nodiscover: Disables the peer discovery mechanism (manual peer addition)
  • networkid: Network identifier (integer, 1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby) (default: 1)
  • console: Start an interactive JavaScript environment

( Note: You can find more details about Geth commands and options by running “- -help” command.)

Edit 2022: Please add --allow-insecure-unlock when you create/open the testnet if you are using command line to unlock your accounts.

After running the above command, your Geth client will create a new private network for you.

Let’s create an account for you by running,

> personal.newAccount()

(Note: Make sure to remember the passphrase, because there is no other way to recover the account.)

The address of the account is “0x985a6a949e1790c35f9320a9db5020aac87e7c99”. Let’s unlock the account by running

> personal.unloackAccount(<address>)

Now start to mine coins(ether) by running,

> miner.start()

and stop after some time by running

> miner.stop()

(Note: These coins are not valid outside the private network.)

JSON RPC API

You can access JSON RPC APIs via HTTP requests. Find more details about JSON RPC APIs here.

Curl

$ curl -X POST -H ‘Content-Type: application/json’ — data ‘{“jsonrpc” :”2.0" , “method” : “eth_accounts”, “params” : [] , “id”: 1999 }’ localhost:8080

Ballerina Ethereum Connector

Ballerina is a programming language for integration. Ballerina Ethereum connector provides easy access to the Ethereum RPC APIs. You can find more details here. Let’s run a sample by running the following command,

$ ballerina run sampleDashBoard.bal <rpcaddrs + rpcport> <jsonrpcversion> <networkid>

Ex:

$ ballerina run sampleDashBoard.bal http://localhost:8080 2.0 1999

You can see a simple wallet dash board,

Next articles I’m going to create contracts on our private network.

--

--