使用geth创建私有链

安装:

官网地址

移除节点:

geth removedb --datadir node1
geth removedb --datadir node2
geth removedb --datadir node3

创建genesis.json:

{
  "config": {
    "chainId": 6666,
    "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": "0xffffffff",
  "difficulty": "0x0200",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": { },
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

创世配置参数说明:

  • nonce:64位随机数,用于挖矿
  • timestamp:创世块的时间戳
  • parentHash:上一个区块的hash值,因为是创世块,所以这个值是0
  • mixHash:与nonce配合用于挖矿,由上一个区块的一部分生成hash
  • extraData:附加信息,任意填写
  • gasLimit:对GAS的消耗总量限制,用来限制区块能包含的交易信息总和
  • difficulty:难度值,越大越难
  • coinbase:矿工账号,第一个区块挖出后将给这个矿工账号发送奖励的以太币
  • alloc:预设账号以及账号的以太币数量,测试链挖矿比较容易可以不配置
  • chainId:指定了独立的区块链网络ID,不同ID网络的节点无法互相连接

初始化节点:

geth --datadir node1 init genesis.json
geth --datadir node2 init genesis.json

初始化创世区块时如果报错:

Failed to write genesis block: unsupported fork ordering: eip150Block not enabled, but eip155Block enabled at 0

需要先移除原来的创世块

geth removedb --datadir node1

再初始化创世区块

启动节点:

geth --datadir "node1" --networkid 6666 --port 10010 --http --http.api "eth,net,web3,miner,admin" --http.addr "localhost" --http.port 8546 console 2>node1.log
geth --datadir "node2" --networkid 6666 --port 10011 --http --http.api "eth,net,web3,miner,admin" --http.addr "localhost" --http.port 8547 console 2>node2.log

查看自己节点的信息:

admin.nodeInfo

添加节点:

admin.addPeer('enode://535bb7b99b8dacd1011484ff3007c77809946b5869e8ab2e30c616bf8eb012d60eecc71e58ebcfbb7c0265b82a4f342e6a107a6b51f6f0ed2d1084c57aba55ae@[::]:10011')

查看节点列表:

admin.peers

查询账户:

读取本地keystore目录下的账户地址信息

eth.accounts
#或
personal.listAccounts

创建账户:

输入参数就是密码,账户地址保存在keystore目录下

personal.newAccount()
#或
personal.newAccount(password)

通过私钥导入账户:

第一个参数是去掉0x的私钥,第二个参数是密码

personal.importRawKey("a290e50c1cde5128229cf9e286593c443084646e65e97c1bcd8afdaddea78fa7","111")

解锁账户:

personal.unlockAccount(account)

执行personal.unlockAccount()时报错: 

geth新版本error:account unlock with HTTP access is forbidden at web3.js……

异常原因:新版本geth,出于安全考虑,默认禁止了HTTP通道解锁账户

解决方案:启动节点命令添加--allow-insecure-unlock

查询余额:

余额默认以最小单位显示:1个ETH=10的18次方个wei

eth.getBalance(account1)
# ETH
web3.fromWei(eth.getBalance(account),'ether')

当前区块:

eth.blockNumber

开始挖 砿:

miner.start()
# x是启动几个核心
miner.start(x)
# 成功挖到一次后就停止
miner.start(1);admin.sleepBlocks(1);miner.stop()

结束挖 砿:

miner.stop()

设置挖 砿账户:

miner.setEtherbase(account)

转账:

转账之前需要先对账户执行personal.unlockAccount()进行解锁

执行miner.start()后转账才会成功

eth.sendTransaction({from:account1,to:account2,value:web3.toWei(5,'ether')})

使用Metamask连接geth网络

猜你喜欢

转载自blog.csdn.net/watson2017/article/details/122719160