Geth download, install, configure environment and build alliance chain

Introduction to Ethereum Survey Course

For more classroom teaching explanations, refer to open resources. Using what you have learned, create projects and complete the required content. The included features and requirements are as follows:

One: Install and run geth client

1. Download and install geth

First download geth: https://geth.ethereum.org/downloads/

insert image description here

insert image description here
insert image description here
​ Select path ↓

insert image description here

2. Configure environment variables

insert image description here

3. Run geth as shown in the following command:

Check out the geth command. Use geth version to view the geth version number to determine whether geth is successfully installed. As shown in the following command:

`geth version`

insert image description here

You can view the commands and related parameters supported by the geth tool through geth --help, which is convenient for later operations on geth.

As shown in the following command:

geth --help

The result of the operation is as follows:

insert image description here

Two: Build a geth alliance chain network and realize communication between multiple nodes

1. Initialize the genesis block

Add the genesis.json file in the root directory of geth, the code in genesis.json is as follows

insert image description here

{
    
    
   "alloc": {
    
    
      "0x63b47f8abf5d47c9a90bab0ce0c6222b6cb2808a": {
    
    	//使用账户1创建
      "balance": "999000000000000000000"
      }
   },
    "config":{
    
    
        "chainId":10,
        "homesteadBlock":0,
        "eip155Block":0,
        "eip158Block":0
    },
    "nonce":"0x0000000000000042",
    "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
    "difficulty": "0x2000",
    "alloc": {
    
    },
    "coinbase":"0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "",
    "gasLimit":"0xffffffff"
}

geth --datadir chain1 init genesis.jsonInitialize the genesis block using

geth --datadir chain1 init genesis.json

insert image description here

Start cmd in the geth installation directory, enter

geth --datadir chain1 --nodiscover console

Enter geth console mode, where chain1 is the account and block data directory

2. Build an alliance chain,

Win+R opens a second console and enters the following command. (command to initialize the genesis block)

geth --datadir ./data-init2/ init genesis.json

Start and enter the console. (Note that there is one more ipcdiable parameter here. If it is the same as the first console, an error will be reported: Erro starting protocol stack:Access is denied)

Create new accounts on the two consoles and record the addresses

Use eth.accountsthe command to view existing accounts, which are currently empty

personal.newAccount("root")Create 2 accounts using //root as password

Account 1: 0x134a886c1f30bd52febed873054de8b7ae7e92e5

Account 2: 0x751b26bf96544c6bc50c9290a0e64631f7a82ec1

Use eth.getBalancethe () command to view existing accounts, which are currently empty

1. View the peers of the node.

admin.peers

Let the two nodes establish a link by sharing the enode address.

admin.nodeInfo.enode

Copy the enode information of node 2, and execute the following command on the console of node 1.

admin.addPeer ("enode://0bd1b7da689dd574eae04d20484086a5c59d81981b927602ef745233a72eac3280ea1a193a41c24d05fae54ae9e78d4fd76b590ffc356c6786eb6f70b0e6b276@[::]:40404?discport=0")

When mining on console 1 miner.start(), we will find that such log information appears on the console of node 2, and the synchronization is successful

Check the amount of the coinbase address and find that there has been an increase.

Three: Use solidity to write smart contract programs and deploy them on the geth client

Compile a simple smart contract in remix

`pragma solidity ^0.4.18;`

`contract test{`
    `function multiply(uint a)public view returns(uint d){`
        `return a*7;`
    `}`
`}

Compile the above code, and you can see the bytecode and ABI information in the compilation details

{
    --
    "object": "608060405234801561001057600080fd5b5060bb8061001f6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b348015604f57600080fd5b50606c600480360381019080803590602001909291905050506082565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a723058209135a65fdddd7be677810243db99bc4cbe46fcf74ee4ce1a3a8cc7fdbab004ef0029",
    --
}

ABI documents

[
    {
        "constant": true,
        "inputs": [
            {
                "name": "a",
                "type": "uint256"
            }
        ],
        "name": "multiply",
        "outputs": [
            {
                "name": "d",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
]

Create a contract object through ABI, then unlock the account we created before in cmd, and deploy the contract

contractInstance = myContract.new({data: bytecode,gas: 1000000, from: eth.coinbase}, function(e, contract){
  if(!e){
    if(!contract.address){
      console.log("Contract transaction send: Transaction Hash: "+contract.transactionHash+" waiting to be mined...");
    }else{
      console.log("Contract mined! Address: "+contract.address);
      console.log(contract);
    }
  }else{
    console.log(e)
  }
})

Four: Through the terminal operation, complete the contract function call (create an account and test the mining function

Use the first step code to create node 3, get the encode address and link to node 2

Unlock Node 3

Use node two to transfer money to node three

Send transfer order

eth.sendTransaction({from: "0x134a886c1f30bd52febed873054de8b7ae7e92e5", to: "0x0df12fd40b27405558103da6f6938eb8c257d", value: "74000000000000000"})

Query balance is

74

Five: The function of completing transactions between multiple nodes, and setting and estimating gas usage

set gas value

01337036)]

Use node two to transfer money to node three

Send transfer order

eth.sendTransaction({from: "0x134a886c1f30bd52febed873054de8b7ae7e92e5", to: "0x0df12fd40b27405558103da6f6938eb8c257d", value: "74000000000000000"})

Query balance is

74

Guess you like

Origin blog.csdn.net/weixin_54291763/article/details/127730124