Ethereum Private Chain Construction Tutorial

geth client installation

Reference Tutorial 1
Reference Tutorial 2
- Method 1: Installing from PPA

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
  • Method 2: Building from source

The prerequisite is that go has been installed

git clone https://github.com/ethereum/go-ethereum
sudo apt-get install -y build-essential golang
cd go-ethereum
make geth

Build a private chain

  • Prepare the genesis block. Choose the name and location of the
    new folder (such as eth), and create a new .json format file (such as eth.json) in the eth directory. The content of the file is as follows
    If the content of your json file is not as follows, an error may be reported later.
{
  "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x40000",
    "extraData" : "",
    "gasLimit" : "0xffffffff",
    "nonce" : "0x0000000000000042",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp" : "0x00",
    "alloc": { }
}

  • Write a new data directory under the eth folder of the genesis block
eth
  ├── data
  └── eth.json

Open the terminal in the eth directory and enter:

 geth --datadir data init eth.json

--datadir data specifies that the data storage location is in the data directory, and initializes the block data with the eth.json file.

Tip: As mentioned earlier, if the content of the json file in reference to other people's tutorials is inconsistent with this tutorial, an error may be reported in this step!
After the initialization is successful, the directory structure will be as follows. The block data is stored in geth/chaindata, and the account data is stored in keystore.

 eth
   ├── data
   │   ├── geth
   │   │   ├── chaindata
   │   │   │   ├── 000001.log
   │   │   │   ├── CURRENT
   │   │   │   ├── LOCK
   │   │   │   ├── LOG
   │   │   │   └── MANIFEST-000000
   │   │   └── lightchaindata
   │   │       ├── 000001.log
   │   │       ├── CURRENT
   │   │       ├── LOCK
   │   │       ├── LOG
   │   │       └── MANIFEST-000000
   │   └── keystore
   └── eth.json

  • Start a private chain node
geth --datadir data console 2>console.log

Welcome to the Geth JavaScript console!
instance: Geth/v1.8.7-stable-66432f38/linux-amd64/go1.10.1
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

geth console, which means to start the node and enter the interactive console. The --datadir option specifies to use data as the data directory. The --networkid option is followed by a number, here is 110, which means that the network id of this private chain is specified as 110. The network id will be used when connecting to other nodes. The network id of the Ethereum public network is 1. In order not to conflict with the public chain network, you must specify your own network id when running a private chain node.
Wait for a few minutes, and enter the command line interaction after the startup is successful.

测试

Enter eth:

> eth
{
  accounts: [],
  blockNumber: 0,
  coinbase: undefined,
  compile: {
    lll: function(),
    serpent: function(),
    solidity: function()
  },
  defaultAccount: undefined,
  defaultBlock: "latest",
  gasPrice: 18000000000,
  hashrate: 0,
  mining: false,
  pendingTransactions: [],
  protocolVersion: "0x3f",
  syncing: false,
  call: function(),
  contract: function(abi),
  estimateGas: function(),
  filter: function(options, callback, filterCreationErrorCallback),
  getAccounts: function(callback),
  getBalance: function(),
  getBlock: function(),
  getBlockNumber: function(callback),
  getBlockTransactionCount: function(),
  getBlockUncleCount: function(),
  getCode: function(),
  getCoinbase: function(callback),
  getCompilers: function(),
  getGasPrice: function(callback),
  getHashrate: function(callback),
  getMining: function(callback),
  getPendingTransactions: function(callback),
  getProtocolVersion: function(callback),
  getRawTransaction: function(),
  getRawTransactionFromBlock: function(),
  getStorageAt: function(),
  getSyncing: function(callback),
  getTransaction: function(),
  getTransactionCount: function(),
  getTransactionFromBlock: function(),
  getTransactionReceipt: function(),
  getUncle: function(),
  getWork: function(),
  iban: function(iban),
  icapNamereg: function(),
  isSyncing: function(callback),
  namereg: function(),
  resend: function(),
  sendIBANTransaction: function(),
  sendRawTransaction: function(),
  sendTransaction: function(),
  sign: function(),
  signTransaction: function(),
  submitTransaction: function(),
  submitWork: function()
}

  • View Account
 >eth.accounts
 []
  • Create new account
按提示输入密码
> personal.newAccount()
Passphrase: 
Repeat passphrase: 
"0xcf4d7713eb0e26d372308f320617095f25771b37"
  • Check account balance
>eth.getBalance(eth.accounts[0])
0
  • Mining related
    Enter miner to view mining related commands
> miner
{
  getHashrate: function(),
  setEtherbase: function(),
  setExtra: function(),
  setGasPrice: function(),
  start: function(),
  stop: function()
}

mining

> miner.start(1)

The parameter of start indicates the number of threads used for mining. The first time you start mining, the DAG file required for mining will be generated first. This process is a bit slow. After the progress reaches 100%, mining will start. At this time, the screen will be refreshed with mining information.
If you want to stop mining, enter miner.stop() in the js console:

  • send transaction
eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325740295&siteId=291194637