-
环境
- 版本为1.7.2-stable-1db4ecdc
- 通过不同端口号来区分节点
- go的客户端geth
- 工作目录
zskMacBook-Pro:ethereum zsk$ tree -L 2 . ├── data │ ├── 00 │ ├── 01 │ ├── 02 │ └── genesis ├── log │ ├── file_to_log_output00 │ ├── file_to_log_output01 │ └── file_to_log_output02 └── start.sh
- start.sh
if [ "$1" == "" ];then echo "node id is empty, please use: start.sh <node_id>"; exit fi # 赋值 no=$1 datadir=data logdir=log # 在当前目录下创建data和log文件夹 mkdir -p $datadir mkdir -p $logdir # 如果启动前需要使用创世块初始化 if [ ! -d "$DIRECTORY" ]; then if [ ! -f $datadir/genesis ];then echo '{"config": {"chainId": 15, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "coinbase" : "0x0000000000000000000000000000000000000000", "difficulty" : "0x40000", "extraData" : "", "gasLimit" : "0xffffffff", "nonce" : "0x0000000000000042", "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp" : "0x00", "alloc": { } }' > $datadir/genesis fi geth --datadir $datadir/$no init ./data/genesis fi # 解析 # --datadir ./data/$no 就是在当前脚本运行的目录下的data文件下创建数据库和密钥存储库的相应文件 # --networkid 11100 网络标识符为11100 # --ipcdisable 禁用IPC-RPC服务器 # --port 619$no 网络监听端口类似61900,$no就是执行这个文件后输入的参数例如:./start.sh 00,$no就是00 # --rpc 使HTTP-RPC服务器 # --rpccorsdomain "*" 逗号分隔的域列表,用于接受跨源请求(浏览器强制执行) # --rpcport 81$no HTTP-RPC服务监听端口8100,参考--port 619$no # console 2>>log/file_to_log_output$no 就是启动console且在log文件下创建file_to_log_output$no文件,用于存储打印输出 # 运行geth,启动console geth --datadir ./data/$no --networkid 11100 --ipcdisable --port 619$no --rpc --rpccorsdomain "*" --rpcport 81$no console 2>>log/file_to_log_output$no
- .sh文件的运行
如果是参数是00,则工作目录是data/00,p2p端口是61900,rpc端口是8100,log日志文件file_to_log_output00zskMacBook-Pro:~ zsk$ cd Desktop/ethereum/ **打开脚本所在的文件 zskMacBook-Pro:ethereum zsk$ chmod 777 start.sh **给脚本权限可以运行 zskMacBook-Pro:ethereum zsk$ ./start.sh 00 **执行脚本,并传参数00进入脚本中
-
基础操作
eth.accounts // 查看当前用户,返回值为一个数组 personal.newAccount('密码') // 创建一个新的用户 user = eth.accounts[0] // 把数组的第一个值赋予user eth.getBalance(user) // 查询这个账户的余额 miner.start() // 开始挖矿 miner.stop() // 停止挖矿 可以直接查询命令支持调用的方式 主要有: admin debug eth miner net personal rpc txpool web3 不懂的可以 > admin 然后会输出这个命令的相关内容
-
交易配置
- 用不同的端口作为不同的节点,来模拟操作。
- 交易之前先配置一个三个节点的私人以太坊网络
1. 首先第一个终端窗口 ./start.sh 00 > user = eth.accounts[0] // 用户赋予变量user "0x79c0afea83c7be28b06125df2b28fd39124ced2f" > eth.getBalance(user) // 查询当前用户的币余额 > admin.nodeInfo.enode // 当前节点的标识 "enode://562006da8c281f7b420c398c3ce3cdb2e336fc864d0a505eb1e693e7376c30fc72fde681e1b4fc109dba5bacefb5e55d0c3d2101c153000e2569e1f29262481c@[::]:61900" > admin.addPeer("enode://42e3c2088e94241266f15cdb45ecf4e0f1738cff0380a6ebda08ab7036b3d2c8df5ed5c03d6eefa84c090636f517c60bb2763f214f0f22dd6ecfc6814dbff553@[::]:61901") true // 添加节点,返回正确 > admin.addPeer("enode://afb86fb86a4140450d1f127a9a066ff3c6d9a70ae55f526cc61332dcde4e25f42dcccd984661074cbbf37a63d5daed41d9ba5cb40c4ed30118a70f4230571ced@[::]:61902") true // 添加节点,返回正确 > admin.peers // 当前的节点 [{ caps: ["eth/62", "eth/63"], id: "42e3c2088e94241266f15cdb45ecf4e0f1738cff0380a6ebda08ab7036b3d2c8df5ed5c03d6eefa84c090636f517c60bb2763f214f0f22dd6ecfc6814dbff553", name: "Geth/v1.7.2-stable-1db4ecdc/darwin-amd64/go1.9.2", network: { localAddress: "[::1]:55074", remoteAddress: "[::1]:61901" }, protocols: { eth: { difficulty: 59328089, head: "0xd8beaf4675756dc5172469c993238d72a77e490ddae6d9a21d87de08a2b07a4c", version: 63 } } }, { caps: ["eth/62", "eth/63"], id: "afb86fb86a4140450d1f127a9a066ff3c6d9a70ae55f526cc61332dcde4e25f42dcccd984661074cbbf37a63d5daed41d9ba5cb40c4ed30118a70f4230571ced", name: "Geth/v1.7.2-stable-1db4ecdc/darwin-amd64/go1.9.2", network: { localAddress: "[::1]:55081", remoteAddress: "[::1]:61902" }, protocols: { eth: { difficulty: 59328089, head: "0xd8beaf4675756dc5172469c993238d72a77e490ddae6d9a21d87de08a2b07a4c", version: 63 } } }] 2. 第二个终端窗口 ./start.sh 01 > user = eth.accounts[0] "0x3332a93d25e509012bdf7d12e05425279bd72915" > eth.getBalance(user) 7999622000000000000 > admin.nodeInfo.enode "enode://42e3c2088e94241266f15cdb45ecf4e0f1738cff0380a6ebda08ab7036b3d2c8df5ed5c03d6eefa84c090636f517c60bb2763f214f0f22dd6ecfc6814dbff553@[::]:61901" > admin.addPeer("enode://afb86fb86a4140450d1f127a9a066ff3c6d9a70ae55f526cc61332dcde4e25f42dcccd984661074cbbf37a63d5daed41d9ba5cb40c4ed30118a70f4230571ced@[::]:61902") 解释下,如果端口1添加了端口2,则端口2也默认添加了端口1,所以端口2只需要添加端口3,这样三个端口就已经互相连接 3. 第三个端口 ./start.sh 02 > user = eth.accounts[0] > eth.getBalance(user) 8800000000000000000 > admin.nodeInfo.enode "enode://afb86fb86a4140450d1f127a9a066ff3c6d9a70ae55f526cc61332dcde4e25f42dcccd984661074cbbf37a63d5daed41d9ba5cb40c4ed30118a70f4230571ced@[::]:61902" 4. 这样这三个节点就互相连接,然后就可以交易了 personal.unlockAccount(user,'abc123') // 先解锁解锁用户 eth.sendTransaction({from: user, to: otherUser, value:web3.toWei(88,'ether')}) // user 向 otherUser 转3个币 // 现在去查询otherUser余额还是为0,每次交易的确认,其实都是需要挖矿的,也就是被其他矿工共识确认,然后才能加入区块链的账本中 // 所以在其他旷工去挖矿就可以确认这笔交易 端口2向端口3交易88个币。然后端口1开始挖矿,这笔交易就会被确认。 最后端口2,除了会支出88个币,还会支出一小部分币,用于支付矿工费。端口3会收到88个币。
- 交易
personal.unlockAccount(user,'123456') // 解锁用户 eth.sendTransaction({from: user, to: otherUser, value:web3.toWei(3,'ether')}) // user 向 otherUser 转3个币
-
后记
bootnode没法使用暂时没有研究明白原因。
补充- 原本以为是新的版本中把bootnode移除了,使用了新的技术,发现too young too simple了。
- 经过一番查找解决方案如下
原理就是用Homebrew安装的ethereum.rb中没有加入安装bootnode的命令 在/usr/local/Homebrew/Library/Taps/ethereum/homebrew-ethereum 找到ethereum.rb 然后加入bin.install 'build/bin/bootnode' 文件内容片段变为 def install ENV["GOROOT"] = "#{HOMEBREW_PREFIX}/opt/go/libexec" system "go", "env" # Debug env system "make", "all" bin.install 'build/bin/evm' bin.install 'build/bin/geth' bin.install 'build/bin/rlpdump' bin.install 'build/bin/puppeth' bin.install 'build/bin/bootnode' // 这个是新加入的 end 最后使用命令重新卸载和安装 brew reinstall ethereum
- 参考icattlecoder的搭建本地以太坊网络,可以继续搭建测试网络
bootnode不工作以及解决方案参考资料
Bootnode command not working #3703
Install all go-ethereum executables #114
参考资料
官方:设置私有网络和本地集群
官方:私有网络
Connecting to the network
Private network
icattlecoder:搭建本地以太坊网络
以太坊开发--geth的使用入门
网友评论