更新于:2019-11-07
ETH私链
更多区块链信息可查看:https://blog.csdn.net/ted_sky/article/details/102932065
搭建私链
下载安装
https://gethstore.blob.core.windows.net/builds/geth-windows-amd64-1.8.27-4bcc0a37.exe
生成两个文件
准备创世区块配置文件
genesis.json
{
"config": {
"chainId": 22222,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x01",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {
}
}
初始化命令 | 作用 |
---|---|
mixhash | 与nonce配合用于挖矿,由上一个区块的一部分生成的hash |
nonce | nonce就是一个64位随机数,用于挖矿 |
difficulty | 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度 |
alloc | 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以 |
coinbase | 矿工的账号,随便填 |
timestamp | 设置创世块的时间戳 |
parentHash | 上一个区块的hash值,因为是创世块,所以这个值是0 |
extraData | 附加信息,随便填,可以填你的个性信息 |
gasLimit | 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大 |
初始化创世区块
geth --datadir "D:\Tool\ETH\data" init "D:\Tool\ETH\genesis.json" console
如果在执行上面的命令报错,先删除原来的创世块geth removedb --datadir data
初始化成功后,会在数据目录 data 中生成 geth 和 keystore 两个文件夹,此时目录结构如下:
常用命令
启动geth
geth --datadir "D:\Tool\ETH\data" console --rpc
创建账户
personal.newAccount('Your Password')
> personal.newAccount("1")
"0x12a97a88c217890aea39918b989b7ae1027295ad"
查看地址列表
> eth.accounts
["0x12a97a88c217890aea39918b989b7ae1027295ad", "0x0dd6f494848afd8cb6e9bb29335edbc341cac7b9"]
查询账户余额
eth.getBalance(eth.accounts[0])
> eth.getBalance(eth.accounts[0])
0
挖矿
miner.start()
停止挖矿
miner.stop()
解锁账户
personal.unlockAccount(eth.accounts[0])
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x12a97a88c217890aea39918b989b7ae1027295ad
Passphrase:
true
转账
eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:1000})
查询交易状态
> txpool.status
{
pending: 1,
queued: 0
}
其中有一条pending的交易,pending表示已提交但还未被处理的交易。
处理交易
要使交易被处理,必须要挖矿。这里我们启动挖矿,然后等待挖到一个区块之后就停止挖矿:
miner.start(1);admin.sleepBlocks(1);miner.stop();
获取区块高度
> eth.blockNumber
58
查看区块
eth.getBlock(57)
代币转账
eth.sendTransaction({
from:eth.accounts[0], to:"0xbF584dd2094c0807d5FAb99c77c6b9cC7423Ef14", value:0, data:"0xa9059cbb000000000000000000000000bf584dd2094c0807d5fab99c77c6b9cc7423ef1400000000000000000000000000000000000000000000000ad78ebc5ac6200000"})
运行JS控制台
geth attach ipc:\\.\pipe\geth.ipc
部署合约
已存在的合约
获取合约内容:https://etherscan.io/address/0x8e870d67f660d95d5be530380d0ec0bd388289e1#