ethereum rpc

1.Ethereum installation

1.1 Installation environment

ubuntu  20.04.1

1.2 Installation accessories

go environment nodejs

1.3 Source code installation

git clone
cd go-ethereum
make geth
make all //or compile all

This will create a geth executable in the go-ethereum/build/bin folder that you can move to wherever you want to run it. The binary file is self-contained and does not require any other files.

geth version // View version number
geth -h // command help

Other installation methods

geth official installation 

2Build a private chain

2.1 Initialize the genesis block

Every blockchain starts with the genesis block. When Geth is run for the first time with default settings, it commits mainnet genesis to the database. For private networks (non-mainnet), a different genesis block is usually required.

Ethereum allows the genesis block to be initialized through a genesis configuration file, and you can also use a genesis configuration that chooses to use built-in multiple network environments. By default, the Ethereum mainnet genesis configuration is used.

Write the configuration file for the genesis blockgenesis.json

  "config": {
    "chainId": 15,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "clique": {
      "period": 5,
      "epoch": 30000
  "difficulty": "1",
  "gasLimit": "8000000",
  "extradata": "0x00000000000000000000000000000000000000000000000000000000000000007df9a875a174b3bc565e6424a0050ebc1b2d1d820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "alloc": {
    "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
    "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }

It can be divided into three major categories according to configuration purposes:

chain configuration

The config item defines the chain configuration, which will affect the consensus protocol. Although the chain configuration has little impact on creation, the block generation rules of new blocks all depend on the chain configuration.
Genesis block header information configuration
nonce: random number, corresponding to the Nonce field of the genesis block.
timestamp: UTC timestamp, corresponding to the Time field of the genesis block.
extraData: additional data, corresponding to the Extra field of the genesis block.
gasLimit: required, fuel upper limit, corresponding to the GasLimit field of the genesis block.
difficulty: required, difficulty coefficient, corresponding to the Difficulty field of the genesis block. When building a private chain, you need to select an appropriate difficulty value according to the situation in order to adjust the block production.
minHash: A hash value corresponding to the MixDigest field of the genesis block. Together with the nonce value, it proves that sufficient calculations have been performed on the block.
coinbase: an address corresponding to the Coinbase field of the genesis block.
Initial account asset allocation
The alloc item is the initial account asset allocation in genesis. When the genesis block is generated, the account assets in this data set are written into the block, which is equivalent to pre-mining. This is very useful for development testing and private chains. You can directly allocate assets to any number of accounts without mining.
Use command

geth --datadir data init /home/yu/go-ethereum/genesis.json

Initialize the genesis block. The current location where the blockchain network data is stored isdataTable of contents. The effect after startup is as follows:

2.1.1 Ethereum ChainID and NetworkID

ChainId isEIP-155An identifier introduced to distinguish different EVM chains. The main function is to prevent a transaction from being submitted repeatedly on different chains after being signed. Initially, it was mainly to prevent Ethereum transactions from being replayed on the Ethereum Classic network or Ethereum Classic transactions from being replayed on the Ethereum network. On the Ethereum network, it is activated from block 2675000 through the Spurious Dragon hard fork upgrade.

What impact has the introduction of ChainId brought?

When creating a new EVM chain, the ChainId needs to be specified in the genesis file. It is best that this ChainId should not be the same as the ChainId of any existing EVM chain that is already running in public, otherwise a lot of money may be spent by mistake with a single configuration error. ChainIds that have been occupied can be viewed through thisgeth official installation

NetworkId is mainly used to identify the current blockchain network at the network layer. Two nodes with inconsistent NetworkId cannot establish a connection.

NetworkId cannot be specified through the configuration file and can only be specified through the parameter –networkid. So when we start our own private chain node, we need to remember to add this parameter. If this parameter is not added and the network type is not specified, the default NetworkId value is consistent with the Ethereum main network.

  • hainId is used to prevent transaction replay in different Ethereum isomorphic networks. Mainly used when signing and verifying transactions.
  • NetworkId is used to identify the blockchain network. Mainly used when handshaking between nodes and verifying each other.
  • ChainId needs to be specified in the genesis file, and NetworkId needs to be specified in the startup parameters.
  • The values ​​of ChainId and NetworkId do not need to be the same.

2.2 Start the private chain

Use the following command to start the private chain.

geth --datadir data --nodiscover console 2>> geth.log

Parameter explanation:

datadirThe location where the current blockchain data is stored
consoleStart command line mode, you can execute commands in geth
nodiscoverPrivate chain address, turn off the node discovery mechanism to prevent joining unfamiliar nodes with the same initial configuration
2>> geth.logOutput to geth.log

The easiest way to start.

At this point, you have entered the geth console and can execute related commands. Before executing the command, you can usetail -f geth.logMonitor log output.

Create a new account with the password “pwd”: personal.newAccount(“pwd”)
View accounts: eth.accounts
Check the account balance: eth.getBalance(“your account”)
Unlock account: personal.unlockAccount(“your account”)
Check the mining account: eth.coinbase
1 wei converted to ether: web3.fromWei(1, ‘ether’)
1 Convert ether to wei: web3.toWei(1, ‘ether’)
Initiate transaction: eth.sendTransaction({from : “account1”, to : “account2”, value : web3.toWei(1, ‘ether’)})
Check the transaction pool status: txpool.status
Start mining: miner.start()
Stop mining: miner.stop()
Stop mining when n blocks are mined: miner.start();admin.sleepBlocks(n);miner.stop()

2.3 RPC

The Ethereum client provides an API and a set of remote call (RPC) commands, which are encoded into json format and are called JSON-RPC-API. In essence, the JSON-RPC API is an interface that allows the programs we write to use the Ethereum client as a gateway to access data on the Ethereum network and blockchain
RPC serves as an HTTP service, and the port is set to 8545. For security reasons, by default, only connections from localhost are accepted.
To access the JSON-RPC-API, you can use specialized library functions written in a programming language, such as web3.js for javascript
Or you can manually build the request yourself and send/receive JSON encoded requests.

2.3.1 Start rpc at startup

geth --datadir data --networkid 2020 --http --http.addr="" --http.port 8545 --http.api "db,eth,net,web3" --http.corsdomain "*" --http.vhosts "*" --port 30303 --nodiscover console 2>> geth.log

Parameter explanation

networkidThe network ID of the private chain must be consistent when connected to other nodes.
httpEnable RPC communication
http.addrRPC listening address
http.portRPC listening port, default 8545
http.apiThis command indicates which commands are allowed access via RPC. Geth allows web3 by default.
http.corsdomaincross domain request
http.vhostsThe domain name to receive the request
portWorking port, default 30303


Note that due to version reasons, rpc has been enabled, and the rpc command is replaced by http.

Note: The string in –http.api “eth,admin,net,web3,personal,miner” determines that the content can be executed in the js console! If there is an admin, the js console can execute admin.nodeInfo. Otherwise it cannot be executed!

RPC access can be through http 8545 or http 8545

The command runs continuously as a service. You need to open another command line window and enter:

geth attach rpc:

eth: Contains some methods related to operating the blockchain;
net: Contains some methods for viewing p2p network status;
admin: Contains some methods related to management nodes;
miner: Contains some methods for starting and stopping mining;
personal: mainly includes some methods of managing accounts;
txpool: Contains some methods for viewing the transaction memory pool;
web3: Contains the above objects, and also contains some unit conversion methods.

From the command line, use the commandadmin.nodeInfo.enodeView the enode information of node 1.

After replacing the last IP part of the enode of node 1 ( with the real IP of node 1, use the same command to start geth on another machine (called node 2).

Use in the command line of node 2admin.addPeer (enode information of node 1)The command connects to node 1.

able to passadmin.peersCommand to check the connection status:

2.4 RPC calls

After starting the geth console according to the above command, you can remotely call commands through RPC.

curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}'

Another way to call

Use the postman tool, set the request method to POST, and set the value of “Content-Type” in Headers to “application/json”.

Related Posts

Ethereum 2.0 client Prysm builds and runs node records

Application of 300PLCmpi to Ethernet in aluminum profile aging furnace through Xingda Easy Control MPI-ETH-XD1.0

Comparison and selection of Ethereum “consensus layer” client prysm and teku

Ethereum Ghost Protocol

[Programming]Solidity notes on fighting zombies

ETH hovers near $1,700; what’s next?

FISCO-BCOS application practice: Blockchain application development sharing

Have Bitcoin and Ethereum prices bottomed?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>