私链搭建

环境搭建

环境说明

虚拟机

作用

BC01

Bootnode, Wallet

BC02

Wallet

BC03

Wallet, Miner

下载文件

#下载geth-alltools-linux-amd64-1.8.4-2423ae01.tar.gz
#拷贝到各节点
#解压

开启Bootnode

#BC01
./bootnode --genkey=boot.key
./bootnode --nodekey=boot.key 1>bootstrap.log 2>&1 &
#记录下面地址,并修改为正确IP
UDP listener up self=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[::]:30301

新建账号

./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 account new

#BC01
Password: patient1
Address: {c230847d2669091b77053df0cbc7f533a0a4cf1c}

#BC02
Password: patient2
Address: {c9fec0bd5aab52ba08a826af70bdecd2963bf5c9}

#BC03
Password: patient3
Address: {328399ffc312cca5e6955d0dc3986313b68236c8}

初始文件准备

//genesis.json
//拷贝到每台机器
//修改chainId及nonce确保只有自己认证的节点可以加入
//修改alloc给各用户预分配ETH
{
  "config": {
        "chainId": 1,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {
        "c230847d2669091b77053df0cbc7f533a0a4cf1c": {"balance": "1000000000000000000000"},
        "c9fec0bd5aab52ba08a826af70bdecd2963bf5c9": {"balance": "1000000000000000000000"},
        "328399ffc312cca5e6955d0dc3986313b68236c8": {"balance": "1000000000000000000000"}
    },
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0x20000",
  "nonce"      : "0x0000000000009527",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

节点初始化

#每个节点
./geth --datadir=~/eth001 init genesis.json

基本操作

基本查询操作

#BC01
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

#获取区块链高度
eth.blockNumber

#获取区块内容
eth.getBlock(0)

#换算单位
web3.toWei(1,"ether")

转账操作

#BC01
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

#查看BC01账号余额
web3.fromWei(eth.getBalance(eth.coinbase))
web3.fromWei(eth.getBalance('c230847d2669091b77053df0cbc7f533a0a4cf1c'))
1000

#查看BC02账号余额
web3.fromWei(eth.getBalance('c9fec0bd5aab52ba08a826af70bdecd2963bf5c9'))
1000

#解锁BC01账号
personal.unlockAccount('c230847d2669091b77053df0cbc7f533a0a4cf1c','patient1',3600)

#向账号BC02发起转账
eth.sendTransaction({from: 'c230847d2669091b77053df0cbc7f533a0a4cf1c', to: 'c9fec0bd5aab52ba08a826af70bdecd2963bf5c9', value: web3.toWei(1)})

INFO [04-23|16:00:57] Submitted transaction                    fullhash=0xb14ee153be478e1ba208a30eae4a4d3cc34a8b21116d1b17e627cb53ff6a82e6 recipient=0xC9fEC0bd5AAB52bA08a826Af70BdeCD2963BF5c9

挖矿确认交易

#BC03
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 --mine --minerthreads=1 --etherbase=0x0000000000000000000000000000000000000000 

#BC03可以看到区块产生
#BC01和BC02上,开启geth console,可以看到

查询交易结果

#BC01或BC02
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

web3.fromWei(eth.getBalance('c230847d2669091b77053df0cbc7f533a0a4cf1c'))
998.999622
#虽然只转账了1ETH,但是交易需要燃烧gas,所以余额小于999

web3.fromWei(eth.getBalance('c9fec0bd5aab52ba08a826af70bdecd2963bf5c9'))
1001
#收到了转账

最后更新于