ETH私链

更新于:2019-11-07

更多区块链信息可查看:https://blog.csdn.net/ted_sky/article/details/102932065

搭建私链

下载安装

https://gethstore.blob.core.windows.net/builds/geth-windows-amd64-1.8.27-4bcc0a37.exe
生成两个文件
eth_pri_install

准备创世区块配置文件

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 dataeth_pri_init
初始化成功后,会在数据目录 data 中生成 geth 和 keystore 两个文件夹,此时目录结构如下:
eth_pri_path

常用命令

启动geth

geth --datadir "D:\Tool\ETH\data" console --rpc
eth_pri_console

创建账户

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()
eth_minerstart

停止挖矿

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})
eth_sendtrans

查询交易状态

> txpool.status
{
   
	pending: 1,
	queued: 0
}

其中有一条pending的交易,pending表示已提交但还未被处理的交易。

处理交易

要使交易被处理,必须要挖矿。这里我们启动挖矿,然后等待挖到一个区块之后就停止挖矿:
miner.start(1);admin.sleepBlocks(1);miner.stop();
eth_handletrans

获取区块高度

> eth.blockNumber
58

查看区块

eth.getBlock(57)
eth_getblock

代币转账

eth.sendTransaction({
   from:eth.accounts[0], to:"0xbF584dd2094c0807d5FAb99c77c6b9cC7423Ef14", value:0, data:"0xa9059cbb000000000000000000000000bf584dd2094c0807d5fab99c77c6b9cc7423ef1400000000000000000000000000000000000000000000000ad78ebc5ac6200000"})

运行JS控制台

geth attach

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值