linux windows 创建 ETH私链

1 安装ETH私有链 (geth)

下载文件私链安装包

https://gethstore.blob.core.windows.net/builds/geth-linux-amd64-1.9.0-52f24617.tar.gz

解压

tar -zxvf geth-linux-amd64-1.9.0-52f24617.tar.gz

2 编写创世块初始化文件

genesis.json 文件名

{
	"config": {
		"chainId": 10,
		"homesteadBlock": 0,
		"eip155Block": 0,
		"eip158Block": 0
	},
	"coinbase": "0x0000000000000000000000000000000000000000",
	"difficulty": "0x20000",
	"extraData": "",
	"gasLimit": "0xffffff",
	"nonce": "0x0000000000000042",
	"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
	"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
	"timestamp": "0x00",
	"alloc": {}
}

创世块参数说明

{
  "config": {
        # 链的id,随便设定,和networkid区别开,不是同一个东西
        "chainId": 7,
        # 不知其意
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  # 直接到账多少个币,单位: wei
  "alloc": {
  "0xe986e4751cbc040ef7c2c66745388cbc9bc9c7e7": {"balance": "111111111"}
  },
  # 币基地址,也就是默认的钱包地址,因为我没有地址,所以全0,为空
  # 后面运行Geth后创建新账户时,如果Geth发现没有币基地址,会默认将第一个账户的地址设置为币基地址
  # 矿工账号
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  # 挖矿难度
  "difficulty" : "0x4cccc8",
  # 附加信息,随便填个文本或不填也行
  "extraData"  : "xgp-remark",
  # gas最高限制,以太坊运行交易,合约等所消耗的gas最高限制,这里设置为最高
  "gasLimit"   : "0xffffffff",
  # 64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊黄皮书中的要求
  # 直接用我这个也可以
  "nonce"      : "0x0000000000000042",
  # 与nonce共同用于挖矿,注意他和nonce的设置需要满足以太坊黄皮书中的要求
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  # 上一个区块的Hash值,因为是创世块,石头里蹦出来的,没有在它前面的,所以是0
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  # 创世块的时间戳,这里给0就好
  "timestamp"  : "0x00"
}

 

3 初始化创世块

./geth init ./genesis.json --datadir "./chain"

 

缺少glibc https://blog.csdn.net/u013216061/article/details/78813805

 

4 启动

./geth --rpc --rpcaddr=127.0.0.1 --rpcport=8545 --datadir ./chain console 2>>eth_output.log

screen 后台启动

screen ./geth --rpc --rpcaddr=127.0.0.1 --rpcport=8545 --datadir ./chain --allow-insecure-unlock console 2>>eth_output.log

--rpcaddr 改为0.0.0.0 运行任何ip访问,较为危险

screen ./geth --rpc --rpcaddr=0.0.0.0 --rpcport=8545 --datadir ./chain console 2>>eth_output.log

参数说明

--rpcaddr=[可以访问的ip地址]

--rpcport=8545(默认端口)

--datadir ./chain(数据文件存放目录)

--allow-insecure-unlock (允许解锁账户)没有这个参数,有时候会出现无法解锁账户的情况

 

 

5 创建账户

web3.personal.newAccount("123456")

123456 为账户的密码

查看当前已创建的账户

web3.eth.accounts

重命名账户

user0 = web3.eth.accounts[0]

查看账户余额

// 通过别名查看账户余额 web3.eth.getBalance(user0) // 通过address 查看账户余额

web3.eth.getBalance("0xdcbde6e4653b9df8e62a298a7516cebdd82f3a31")

// 解锁账号

// 通过别名解锁账户
web3.personal.unlockAccount(user0,"123456")

// 通过地址解锁账户
web3.personal.unlockAccount("0xdcbde6e4653b9df8e62a298a7516cebdd82f3a31","123456")

// 转账申请

web3.eth.sendTransaction({from:user0,to:"0xdcbde6e4653b9df8e62a298a7516cebdd82f3a31",value:web3.toWei(20000,"ether")})

//查询余额

web3.fromWei(eth.getBalance("0x7a11b12e794320eb1a3584baa53f61928f066ca4"),'ether') 

web3.fromWei(eth.getBalance(user0),'ether') //别名查询账户余额

// 转账查询

通过TXID 查询转账详情

eth.getTransaction("0x5df1efa65b9fc82bbfc348329e6d08c24b42e6a28882f14c536e94dd0d622db4")
// 开始挖矿 ,后面的数字为挖矿线程数,数字越大,消耗的资源越多
miner.start(1)

// 停止挖矿 
miner.stop()

 

参考文章:

https://my.oschina.net/u/3050295/blog/2992477

https://www.jianshu.com/p/facab57e7564

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值