1, Build a private Ethereum network, pure dry goods

Keywords: Blockchain Ethereum web3

preface

Operating system: macbook pro (16 inch, 2019)
Geth Version: 1.10.8-stable

1, Why use private chains

Deploying smart contracts on the public chain of Ethereum requires Ethereum to initiate transactions. By modifying the configuration, an Ethereum private chain can be built locally, because it has nothing to do with the public chain, that is, it does not synchronize the huge data of the public chain and does not need to spend money to buy Ethereum, which well meets the requirements of smart contract development and testing. The developed smart contract can also easily switch the interface and deploy to the Ethereum public chain.

2, Open source tools and languages that need to be installed

    1. install Go compiler
brew install go
  • 2.Ethereum node installation
brew tap ethereum/ethereum

brew install ethereum
    1. solidit Ethereum smart contract language
brew install solidity

3, Establish private chain

Initialize the genesis node and set the data directory

Create Genesis block configuration file genesis.json file

genesis.json is a configuration file used by the Geth tool to create the genesis block and blockchain. genesis.json is not the genesis block itself

{
  "config": {
    "chainId": 666,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {}
  },
  "nonce": "0x0",
  "timestamp": "0x5ddf8f3e",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x47b760",
  "difficulty": "0x00002",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": { },
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}


Initialize the genesis node through genesis.json and set the data directory:

geth init "/Users/jingbao/IdeaProjects/smart-contract/privateDemo/genesis.json" --datadir "/Users/jingbao/IdeaProjects/smart-contract/privateDemo/data2"

/Users / Jingbao / ideaprojects / smart contract / privatedemo / genesis.json this is where I store genesis.json. Don't forget to change your address

Load using geth

geth --datadir "/Users/jingbao/IdeaProjects/smart-contract/privateDemo/data2" --ws --ws.api "eth,web3,miner,admin,personal,net,txpool" --ws.origins "*" --rpc --rpcapi "eth,web3,miner,admin,personal,net,txpool" --rpcport "8545" --rpccorsdomain "*" --nodiscover --networkid 15 --allow-insecure-unlock --ipcpath ~/Library/Ethereum/geth.ipc

Several parameters are explained in the summary:

  • – identity: Specifies the node ID;
  • – RPC: indicates that the HTTP-RPC service is enabled;
  • – rpcaddr: http-rpc service ip address;
  • – rpcport: Specifies the HTTP-RPC service listening port number (8545 by default);
  • – datal: specify the storage location of blockchain data;
  • – port: Specifies the port number used to connect with other nodes (30303 by default);
  • – nodiscover: turn off the node discovery mechanism to prevent the addition of unfamiliar nodes with the same initial breeding;

Specifically Official documents

The command runs continuously in the form of service. You need to open another command line window and enter

geth attach rpc:~/Library/Ethereum/geth.ipc

This is an interactive JavaScript execution environment, in which JavaScript code can be executed, in which > is the command prompt. In this environment, there are also built-in JavaScript objects used to operate Ethereum, which can be used directly. These objects mainly include:

web3.js common operation commands

web3.js Chinese document

Get all current accounts

> eth.accounts
["0x88d2a3c6575dc35e9014fd13f530dd326b254399", "0xa22be3a54be02c0a57bc23af3295099a0a06e2ab", "0xdd482f7cc5a3ced6a29bed498309f4d0ebad39f4"]

perhaps

> personal.listAccounts
["0x88d2a3c6575dc35e9014fd13f530dd326b254399", "0xa22be3a54be02c0a57bc23af3295099a0a06e2ab", "0xdd482f7cc5a3ced6a29bed498309f4d0ebad39f4"]

Create account

personal.newAccount("my pasword")
  • View balance
eth.getBalance(eth.accounts[0])

Equivalent to

eth.getBalance("0x88d2a3c6575dc35e9014fd13f530dd326b254399")

mining

  • mining

Start mining

miner.start();

Stop mining

miner.stop();

Account transfer

  • Unlock account
personal.unlockAccount(eth.accounts[0])

Enter the password and press enter

  • Convert ether to wei
amount = web3.toWei(1,"ether")
eth.sendTransaction({from:personal.listAccounts[0],to:personal.listAccounts[1],value:amount})

View current to-do list

txpool.status

or

eth.getBlock("pending")

Next article 02. Deploying smart contracts over private Ethereum networks

Like follow subscription continuous update

Click follow to subscribe to continuous updates

Click follow to subscribe to continuous updates

Please leave a message if there are problems or articles that need to be corrected
Or pwn2ownyearn@gmail.com Mailbox

Posted by gregsmith on Sun, 19 Sep 2021 17:51:24 -0700