The construction of private chain in Ethereum

1. Installation environment

  1. Ubuntu 64-bit Alibaba Cloud Server
  2. geth online installation

    $ sudo add-apt-repository -y ppa:ethereum/ethereum
    $ sudo apt-get update
    $ sudo apt-get install ethereum
    • 1
    • 2
    • 3

two. Prepare the genesis block configuration file

  1. Save the following content in a json format configuration file named genesis.json

    {
    "coinbase"   : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x400", "extraData" : "0x0", "gasLimit" : "0x2fefd8", "nonce" : "0xdeadbeefdeadbeef", "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp" : "0x00", "alloc" : {} }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

3. Initialization: writing to the genesis block

  1. Create a new directory to store blockchain data. Suppose the newly created data directory is ~/privatechain/data0, and genesis.json is stored in ~/privatechain. The directory structure should be as follows:

    privatechain  
    ├── data0  
    └── genesis.json  
    • 1
    • 2
    • 3
  2. Enter the privatechain and execute the initialization command, where the –datadir option is followed by a directory name, here is data0, indicating that the specified data storage directory is data0

    $ cd privatechain  
    $ geth --datadir data0 init genesis.json 
    • 1
    • 2

    When you see the following output, the initialization is successful

    I0322 10:52:44.585282 cmd/geth/chaincmd.go:131] successfully wrote genesis block and/or chain rule set: b240e0678c2a8f87cf350225b528e3d97688aad6d4d84ee84e405c7fc9e37e4e 
    • 1
  3. After the initialization is successful, two folders, geth and keystore, will be generated in the data directory data0. At this time, the directory structure is as follows: The block data is stored in geth/chaindata, and the account data is stored in keystore.

    privatechain  
    ├── data0  
    │   ├── geth  
    │   │   └── chaindata  
    │   │       ├── 000002.log  
    │   │       ├── CURRENT  
    │   │       ├── LOCK  
    │   │       ├── LOG  
    │   │       └── MANIFEST-000003  
    │   └── keystore  
    └── genesis.json  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

4. Start the private chain node

  1. Enter the following command to start the first node, the --datadir option specifies to use data0 as the data directory, and the --networkid option is followed by a number, here is 314590, which indicates the network id that specifies this private chain. In multiple node private networks, the id should be the same.

    geth --datadir data0 --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console
    • 1

    ps: Start with the above command, because of the geth version, when mining later, it will output null, so you can add –dev –dev.period 1 after the start command. Generally not required, you can first check whether there are users in the current system.

  2. When you see the following information, the blockchain node is running successfully

    Welcome to the Geth JavaScript console!  
    instance: Geth/v1.5.6-stable/linux/go1.7.3 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 
    • 1
    • 2
    • 3

V. Account Creation and Management

  1. eth.accounts to see all users on the current node
  2. Create a new account, enter the password, and finally return the id of the new user (public key, account address)

    personal.newAccount()  
    Passphrase:   
    Repeat passphrase:   
    "0xc232e2add308136571bb8f9197ba4ae4e5ba9836"  
    • 1
    • 2
    • 3
    • 4
  3. View account balance eth.getBalance(eth.accounts[0])

6. Start and stop mining

  1. Use miner.start() to start mining, where 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.

    miner.start(1) 
    • 1
  2. Stop mining miner.stop()

  3. Mining a block will reward 5 ethers, and the reward obtained from mining will enter the miner's account, which is called coinbase. By default, coinbase is the first account in the local account. Users who have current mining earnings

    eth.coinbase 
    • 1
    1. To make mining rewards enter other accounts, set other accounts to coinbase through miner.setEtherbase()
    miner.setEtherbase(eth.accounts[1])  
    • 1

7. Send transaction

  1. Create transaction

    amount = web3.toWei(5,'ether')
    eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount}) 
    • 1
    • 2
  2. Account unlock operation, the account will be locked every time, to send a transaction, you must first unlock the account, and then resend the transaction

    personal.unlockAccount(eth.accounts[0])  
    • 1
  3. Looking at the transactions in the blockchain, there is a pending transaction, and pending indicates a transaction that has been submitted but not yet processed.

    txpool.status  
    {  
    pending: 1, queued: 0 } 
    • 1
    • 2
    • 3
    • 4
    • 5
  4. Mining is necessary for transactions to be processed. Here we start mining, and then stop mining after waiting for a block to be mined:

    miner.start(1);admin.sleepBlocks(1);miner.stop();  
    • 1

8. View transactions and blocks

  1. View the current total number of blocks eth.blockNumber
  2. View transactions by transaction hash

    eth.getTransaction("0x0c59f431068937cbe9e230483bc79f59bd7146edc8f f5ec37fea6710adcab825")  
    • 1
  3. View block by block number eth.getBlock(33)

9. Connect to other nodes

  1. View current node information admin.nodeInfo.enode
  2. You can create another node on the same server, under privatechain, create a folder data1, and then enter the initialization and startup commands. –bootndoes is to set the current node to start, directly by setting the value of –bootndoes to link the first node, The value of --bootnoedes can be printed by entering the command: admin.nodeInfo.enode on the command line in the first section.

    geth --datadir data01 init ./genesis.json geth --datadir data01 --networkid 314590 --ipcdisable --port 61911 --rpcport 8101 --bootnodes "enode://ad307e052d0e04af519b8999fa870800df8a7a0cc2a91e6aea30e879b75c344dfa12c773a63a71677c2a3ea1254cf982815817f7ff58bd79e5837ea44d791a2d@192.168.1.2:61910" console
    • 1
    • 2
  3. Another way to add nodes is admin.addPeer() on node one, you can connect to node two

    admin.addPeer("enode://9e86289ea859ca041f235aed87a091d0cd594b377cbe13e1c5f5a08a8a280e62d4019ac54063ed6a1d0e3c3eaedad0b73c40b99a16a176993f0373ffe92be672@127.0.0.1:61910")
    • 1
  4. You can view the number of connected nodes through net.peerCount

  5. If you build a node on a different server, you need to install geth and restart the previous steps

Guess you like

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