狼爱上狸

我胡汉三又回来了

windows使用geth搭建以太坊私链

第一步 安装geth

下载地址:https://ethereum.github.io/go-ethereum/downloads/

点击安装勾选development tools

1.png

第二步 创建您的帐户

在当前文件夹下创建node1文件夹

创建一个账户(也称为钱包)

Devnet $ geth --datadir node1/ account new
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: 输入你的密码
Repeat passphrase: 确认你的密码
Address: {08a58f09194e403d02a1928a7bf78646cfc260b0}

第三步 geth命令创建您的Genesis文件

生成的文件是用于初始化区块链的文件。第一个块叫做创世块,是根据genesis.json文件中的参数生成的。

Geth安装后目录下有很多可执行文件如puppeth或bootnode。你可以在Geth github上找到完整的列表。
Puppeth可以创建创世区块的json文件。
开始使用Puppeth:

1、win键 + r 进入cmd命令行,进入到安装geth的文件夹执行 puppeth 然后顺序执行下面操作

2、Please specify a network name to administer (no spaces, please)
> devnet(这里随便填写一个网络管理名称即可,如有需要后面可以通过--network重新设置)
What would you like to do? (default = stats)
 1. Show network stats
 2. Configure new genesis
 3. Track new remote server
 4. Deploy network components

> 2(这里选择2,回车,配置新的创世区块)

3、Which consensus engine to use? (default = clique)
   1. Ethash - proof-of-work
   2. Clique - proof-of-authorit

> 1(这里选择pow共识机制)

4、Which accounts should be pre-funded? (advisable at least one)

> 0x1234567890123456789012345678901234567890(这个是设置预分配以太坊的账户,建议设置一个有私钥的地址可以后面测试使用,设置好后再按一次回车)

5、Specify your chain/network ID if you want an explicit one (default = random)
> 666(这里就是链的chainId,可以随意输入也可以不输直接回车默认随机数字)

6、What would you like to do? (default = stats)

  1. Show network stats
  2. Manage existing genesis
  3. Track new remote server
  4. Deploy network components
    > 2(管理已拥有的创世块)

7、 1. Modify existing fork rules
2. Export genesis configuration
3. Remove genesis configuration
> 2(选择导出创世配置)

8、Which file to save the genesis into? (default = devnet.json)
> ./genesis.json(导出的路径及文件名)

OK,到这里创世json文件创建完成了

第四步 geth命令初始化节点

现在我们有了这个genesis.json文件,可以初始化创世块了!

进入到geth安装文件夹下执行

geth --datadir node1/ init genesis.json

第五步 bootnode命令操作

bootnode唯一的目的是帮助节点发现彼此(记住,以太坊区块链是一个对等网络)

初始化 bootnode

bootnode -genkey boot.Key

启动bootnode服务

bootnode -nodekey boot.key -addr:30310

随意使用任何您喜欢的端口,但请避免使用主流端口(例如HTTP)。30303用于公共以太坊网络。

第六步 geth命令启动节点

geth --datadir .\node1\ --syncmode 'full' --port 30311 --rpc --rpcaddr '127.0.0.1' --rpcport 8501 --rpcapi  'personal,db,eth,net,web3,txpool,miner' --bootnodes 'enode://ca88962dbcc8eb0c7587789866f21db68cdf32ad1ea890fe0d9f8fe010f7e9afe2e6a88d5c9d418be61a10b8a31b1e7c55213bb426dab91596ae36bd7d559333@127.0.0.1:30310' --networkid 666

参数解释:

--syncmode 'full' 有助于防止错误丢弃错误的传播块
--port 是node1的端口
--rpcapi 允许RPC调用的模块
--bootnodes 要连接的bootnode
--networkId genesis.json文件中的chainId

第七步 与您的节点进行交互

通过RPC方式

$ cd devnet
devnet$ geth attach 'http://localhost:8501'
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9
coinbase: 0x87366ef81db496edd0ea2055ca605e8686eec1e6
at block: 945 (Sat, 10 Feb 2018 21:16:14 CET)
 modules: eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

使用Geth控制台

>net.version
"1515"
> eth.blockNumber
1910
> eth.coinbase
"0x87366ef81db496edd0ea2055ca605e8686eec1e6"
> eth.sendTransaction({'from':eth.coinbase, 'to':'0x08a58f09194e403d02a1928a7bf78646cfc260b0', 'value':web3.toWei(3, 'ether')})
"0x299a99baa1b39bdee5f02e3c660e19e744f81c2e886b5fc24aa83f92fe100d3f"
>eth.getTransactionReceipt("0x299a99baa1b39bdee5f02e3c660e19e744f81c2e886b5fc24aa83f92fe100d3f")
{
  blockHash: "0x212fb593980bd42fcaf3f6d1e6db2dd86d3764df8cac2d90408f481ae7830de8",
  blockNumber: 2079,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  from: "0x87366ef81db496edd0ea2055ca605e8686eec1e6",
  gasUsed: 21000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  status: "0x1",
  to: "0x08a58f09194e403d02a1928a7bf78646cfc260b0",
  transactionHash: "0x299a99baa1b39bdee5f02e3c660e19e744f81c2e886b5fc24aa83f92fe100d3f",
  transactionIndex: 0
}
> exit(退出)


来自:https://www.haowenbo.com/articles/2019/07/24/1563963176492.html

posted on 2019-11-28 17:08 狼爱上狸 阅读(973) 评论(0)  编辑  收藏 所属分类: Blockchain


只有注册用户登录后才能发表评论。


网站导航: