Windows Ethereum Private Chain Construction Tutorial

If you don't like to waste time on building the development environment, you can use Huizhi's online tutorial:

1. Install the DApp development environment

1.1 Install Node.js

We use the official long-term support version 8.10.0LTS. Click this link to download the 32-bit installation package. The 32-bit installation package can be used for 32-bit systems or 64-bit systems.
If you confirm that your system is 64-bit, you can also download the 64-bit package.
You can install it directly after downloading. After the installation is complete, open a console window and you can use node:

C:\Users\hubwiz> node –v
v8.10.0

1.2 Install Geth

Download the 64-bit or 32-bit Geth installer and install it.
After the installation is complete, open a console and execute the command to verify that the installation was successful:

C:\Users\hubwiz> geth version
Geth
Version: 1.8.3-stable

1.3 Install the solidity compiler

C:\Users\hubwiz> npm install –g solc

After the installation is complete, execute the command to verify that the installation was successful

C:\Users\hubwiz> solcjs –version
0.40.2+commit.3155dd80.Emscripten.clang

1.4 Install web3

The installation process of Web3 uses git, so you need to install the Windows version of the git command line first. Download the 64-bit or 32-bit git installer, and continue installing web3 after installing it locally.

C:\Users\hubwiz> npm install –g [email protected]

Installation verification:

C:\Users\hubwiz> node –p 'require("web3")'
{[Function: Web3]
  providers:{…}}

1.5 Install the truffle framework

Execute the following command to install the truffle development framework:

C:\Users\hubwiz> npm install –g truffle

Verify installation:

C:\Users\hubwiz> truffle.cmd version
Truffle v4.1.3 (core 4.1.3)

1.6 Install webpack

Execute the following command to install webpack:

C:\Users\hubwiz> npm install –g [email protected]

Verify installation

C:\Users\hubwiz> webpack –v
3.11.0

3. Running private chain nodes

2.1 Genesis Block Configuration

Create a node directory node1, and create the private chain's genesis block configuration file in it:

C:\Users\hubwiz> mkdir node1
C:\Users\hubwiz> cd node1
C:\Users\hubwiz\node1> notepad private.json

Then edit the content as follows:

{
    "config": {
        "chainId": 7878,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "difficulty": "200",
    "gasLimit": "2100000",
    "alloc": {
        "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
        "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
    }
}
  • config.chainIdUsed to declare the Ethereum network number, choose a number greater than 10.
  • difficultyIt is used to declare the mining difficulty. The smaller the value, the lower the difficulty and the faster the block generation.

2.2 Initialize the private chain node

Execute the init command of geth to initialize the private chain node:

C:\Users\hubwiz\node1> geth --datadir .\data init private.json

This will create the data directory in the current directory to store block data and account information:

C:\Users\hubwiz\node1> dir
data private.json

The above command can be written into a script init.cmd, so as to avoid entering so many unremembered things every time. The contents of the file are as follows:

geth --datadir .\data init private.json

When deploying the next node, you can directly execute this script for initialization. For example, on another machine:

C:\Users\hubwiz\node1> init.cmd

2.3 Start the private chain node

Start from the specified private chain data directory and set a different network number to start the node:

C:\Users\hubwiz\node1> geth --rpc --datadir .\data --networkid 7878 console

Similarly, you can use a script console.cmd to simplify the input when starting the node, the content of the file is as follows:

geth --rpc \
      --rpcaddr 0.0.0.0 \
      --rpccorsdomain "*" \
      --datadir ./data \
      --networkid 7878 \
      console
  • rpcaddrThe parameter is used to declare the listening address of the node's RPC API. If it is set to 0.0.0.0, the API can be accessed from other machines;
  • rpccorsdomainThe parameter is to solve the security limitation of web3 cross-domain calls from the browser.
    To start the node later, just execute this script directly:
C:\Users\hubwiz\node1> console.cmd

2.4 Account Management

2.4.1 View Account List

In the geth console, use the accounts property of the eth object to view the current list of accounts:

> eth.accounts
[]

Since we haven't created an account yet, this list is still empty.

2.4.2 Create a new account

In the geth console, use the newAccount() method of the personal object to create a new account with a password of your choice:

> personal.newAccount('78787878')
0xd8bcf1324d566cbec5d3b67e6e14485b06a41d49

The output is the newly created account address (public key), your output will not be the same as the example above. geth will be saved to the keystore file in the data directory. The password must be remembered by yourself, and you will need to use it later.

2.4.3 Check account balance

In the geth console, use the getBalance() method of the personal object to get the balance of the specified account. The parameter is the account address:

> eth.getBalance(eth.accounts[0])
0

Or enter the account address directly:

> eth.getBalance('0xd8bcf1324d566cbec5d3b67e6e14485b06a41d49')
0

The newly created account has a balance of 0.

2.4.4 Mining

An account with no money can do nothing and needs to mine to make some money.
Execute the start() method of the miner object in the geth console to start mining:

> miner.start(1)

After a few minutes, check the account balance:

> eth.getBalance(eth.accounts[0])
2.695e+21

There is a lot of money, 2695ETH, and the current market value is nearly 5 million yuan, ha.
Execute the stop() method of the miner object to stop mining:

> miner.stop()

2.4.5 Unlock Account

An unlocked account is required when deploying the contract. Use the unlockAccount() method of the personal object in the geth console to unlock the specified account. The parameters are the account address and account password (the password specified when creating the account):

> eth.unlockAccount(eth.accounts[0],'78787878')
true

3. Build a sample project

3.1 Create a new DApp project

Execute the following command to create the project directory and enter it:

C:\Users\hubwiz> mkdir demo
C:\Users\hubwiz> cd demo

Then initialize the project skeleton structure with the webpack template:

C:\Users\hubwiz\demo> truffle.cmd unbox webpack
Downloading…
Unpacking…
Setting up…
Unbox successful. Sweet!

3.2 Install the NPM packages that the project depends on

Execute the following command to install the nmp package:

C:\Users\hubwiz\demo$ npm install

3.3 Modify truffle configuration

If you use the graphical version of ganache, you don't need to modify the truffle.js configuration file. Otherwise, you need to modify the port to 8545 in truffle.js, because ganache-cli listens on port 8545:

module.exports = {
  networks:{
    development: {
      port: 8545
    }
  }
}

3.4 Start the node

Execute the following command to start the node emulator in order to deploy the contract and execute the transaction:

C:\Users\hubwiz\node1> console.cmd

Note: In order to deploy the contract on the node, don't forget to unlock the account after starting geth:

> personal.unlockAcount(eth.accounts[0],'78787878')
true

3.5 Compiling the contract

Execute the following command to compile the project contract:

C:\Users\hubwiz\demo> truffle.cmd compile

3.6 Deploy the contract

Execute the following command to deploy the contract:

C:\Users\hubwiz\demo> truffle.cmd migrate

If you forgot to unlock the account in the geth console before, you will see the following error, please refer to the previous instructions to unlock it:

Error: authentication needed: password or unlock

If the account has been properly unlocked, you will see the deployment process stop in the following state:

Replacing Migrations…
… 0x3088762a5bc9…

This is because truffle is waiting for the deployment transaction to commit, but we haven't started mining in the private chain yet.
Now switch back to the geth terminal window and check the status of the transaction pool:

> txpool.status
{
  pending:1,
  queued:0
}

Sure enough there is a pending transaction! Just start mining:

> miner.start(1)

After a while, check the status of the transaction pool:

> txpool.status
{
  pending:0,
  queued:0
}

The transaction has been successfully submitted. We can stop mining because it is too CPU intensive:

> miner.stop()

Now switch back to the truffle terminal, and the deployment process has completed correctly.

3.7 Start DApp

Execute the following command to start the DApp:

C:\Users\hubwiz\demo> npm run dev

Just visit http://localhost:8080 in your browser

If you want your DApp to be accessible from other machines, modify package.json:

{
  scripts:{
    "dev": "webpack-dev-server –-host 0.0.0.0"
  }
}

Related Tutorials

Guess you like

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