Use geth to build a private chain

Use geth to build a private chain

OS: Ubuntu 18.04

A go environment is required! !

Download go-ethereum source code

# /usr/local/目录下:
git clone https://github.com/ethereum/go-ethereum.git

# git地址
https://github.com/ethereum/go-ethereum

compile

make geth

View version number

geth version

initialization

# 创建文件夹
mkdir /usr/local/myChain
# 初始配置文件
vim genesis.json
# /usr/local/go-ethereum
geth --datadir ../myChain/ init ../myChain/genesis.json
#初始化后就不能初始化了 直接移除
geth removedb
//genesis.json
{
    
    
    "config": {
    
    
        "chainId": 27   //链ID 随便起 和主链、测试连不同就OK
        },
    "alloc": {
    
    
        "0xc7c4f9a0Cd0a3e45348c5ed8c3909C69aA9FA8fC":{
    
    "balance":"10000000000000000000000"} //创世块分配的账户及余额
        },
    "difficulty": "2000", //挖矿难度
    "gasLimit": "2100000" //汽油费
}


{
    
    
"config": {
    
    
"chainId": 123,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0
},
"alloc": {
    
    },
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x2000",
"extraData": "",
"gasLimit": "0x2fefd8",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}

start up

#/usr/local/myChain/
geth --datadir . --networkid 27
# 带控制台
geth --datadir . --networkid 27 console
#指定日志目录
geth --datadir . --networkid 27 console 2>output.log # 2代表输出 2> 输出重定向
#
geth --datadir . --networkid 123 --http console 2>output.log 

some commands

#查看账户
eth.accounts
#查看余额
eth.getBalance("0xc7c4f9a0Cd0a3e45348c5ed8c3909C69aA9FA8fC")
eth.getBalance(eth.accounts[0])
#转成ether单位
web3.fromWei(eth.getBalance("0xc7c4f9a0Cd0a3e45348c5ed8c3909C69aA9FA8fC"))
web3.fromWei(eth.getBalance(eth.accounts[0]))
#当前块号
eth.blockNumber
#创建账户
personal.newAccount()
#解锁账户
personal.unlockAccount(eth.accounts[0])
#挖矿
miner.start(1)
#停止挖矿
miner.stop()
#返回接收挖矿回报的账户地址
eth.coinbase
#获取区块信息
eth.getBlock()
#获取交易信息
eth.getTransaction()
#转账
eth.sendTransaction({
    
    from:eth.accounts[0],to:"0xc7c4f9a0Cd0a3e45348c5ed8c3909C69aA9FA8fC",value:web3.toWei(10,'ether')})

0x74a328c888bccd96e24a856964b6e418984451fa
Yanlng0503

# 交易号
0xac6dee174111034ab7a39b6166529cf6918b90ecf89aa64ad37e68822e4d5e05


curl -X POST -H "Content-Type:application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}' http://1.116.123.107:8545

JSON-rpc

root@VM-16-5-ubuntu:/usr/local/myChain# curl -X POST -H "Content-Type:application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":1,"result":"Geth/v1.10.18-unstable-40cfe710-20220420/linux-amd64/go1.18.1"}

geth --datadir . --networkid 123 --http console 2>output.log 

 curl -X POST -H "Content-Type:application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' http://127.0.0.1:8545

Guess you like

Origin blog.csdn.net/qq_43010602/article/details/124351659