首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

以太坊·单机多实例演示

摘要: 这是一篇演示如何使用一台服务器,运行多个以太坊客户端,并且将节点互联,完成两个节点之间的转账操作。

本文节选自电子书《Netkiller Blockchain 手札》

Netkiller Blockchain 手札

Mr. Neo Chan, 陈景峯(BG7NYT)

文档始创于2018-02-10

版权 2018 Netkiller(Neo Chan). All rights reserved.

版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

$Data$

内容摘要

这一部关于区块链开发及运维的电子书。

为什么会写区块链电子书?因为2018年是区块链年。

这本电子书是否会出版(纸质图书)? 不会,因为互联网技术更迭太快,纸质书籍的内容无法实时更新,一本书动辄百元,很快就成为垃圾,你会发现目前市面的上区块链书籍至少是一年前写的,内容已经过时,很多例子无法正确运行。所以我不会出版,电子书的内容会追逐技术发展,及时跟进软件版本的升级,做到内容最新,至少是主流。

这本电子书与其他区块链书籍有什么不同?市面上大部分区块链书籍都是用2/3去讲区块链原理,只要不到 1/3 的干货,干货不够理论来凑,通篇将理论或是大谈特谈区块链行业,这些内容更多是头脑风暴,展望区块链,均无法落地实施。本书与那些书籍完全不同,不讲理论和原理,面向应用落地,注重例子,均是干货。

电子书更新频率?每天都会有新内容加入,更新频率最迟不会超过一周,更新内容请关注 https://github.com/netkiller/netkiller.github.io/commits/master

http://www.netkiller.cn/blockchain/index.html

2.3. 单机多实例演示

在没有条件安装虚拟机也没有多台服务器的情况下我们可以使用一台服务器运行多个实例的方法也可以实现多个节点运行环境。

cd~mkdir-p ethereumcdethereummkdirdata

创建文件 genesis.json

{"nonce":"0x0000000000000042","difficulty":"0x020000","mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","timestamp":"0x00","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","extraData":"0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa","gasLimit":"0x4c4b40","config": {"chainId":15,"homesteadBlock":,"eip155Block":,"eip158Block":},"alloc": { }}

2.3.1. 实例一

geth --datadir ~/ethereum/data1 init genesis.jsongeth --datadir="~/ethereum/data1"--networkid123456--port30301--rpc --rpcaddr="0.0.0.0"--rpccorsdomain"*"--rpcport8541

启动后终端输出

neo@netkiller ~/ethereum % geth--datadir ~/ethereum/data1 init genesis.jsonWARN [02-0222:09:56] No etherbasesetandnoaccountsfoundasdefaultINFO [02-0222:09:56] Allocatedcacheandfilehandlesdatabase=/home/neo/ethereum/data1/geth/chaindatacache=16handles=16INFO [02-0222:09:56] Writing custom genesisblockINFO [02-0222:09:56] Successfully wrote genesis statedatabase=chaindatahash=611596…424d04INFO [02-0222:09:56] Allocatedcacheandfilehandlesdatabase=/home/neo/ethereum/data1/geth/lightchaindatacache=16handles=16INFO [02-0222:09:57] Writing custom genesisblockINFO [02-0222:09:57] Successfully wrote genesis statedatabase=lightchaindatahash=611596…424d04neo@netkiller ~ % geth--datadir="~/ethereum/data1" --networkid 123456 --port 30301 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" -rpcport 8541WARN [02-0222:36:02]NoetherbasesetandnoaccountsfoundasdefaultINFO [02-0222:36:02]Startingpeer-to-peer nodeinstance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1INFO [02-0222:36:02] Allocatedcacheandfilehandlesdatabase=/home/neo/ethereum/data1/geth/chaindatacache=128handles=1024INFO [02-0222:36:02] Initialisedchainconfiguration config=""INFO [02-0222:36:02] Diskstorageenabledforethash caches dir=/home/neo/ethereum/data1/geth/ethashcount=3INFO [02-0222:36:02] Diskstorageenabledforethash DAGs dir=/home/neo/.ethashcount=2INFO [02-0222:36:02] Initialising Ethereum protocolversions="[63 62]"network=123456INFO [02-0222:36:02] Loaded most recentlocalheadernumber=hash=611596…424d04 td=131072INFO [02-0222:36:02] Loaded most recentlocalfullblocknumber=hash=611596…424d04 td=131072INFO [02-0222:36:02] Loaded most recentlocalfastblocknumber=hash=611596…424d04 td=131072INFO [02-0222:36:02] Loadedlocaltransactionjournal transactions=dropped=INFO [02-0222:36:02] Regeneratedlocaltransactionjournal transactions=accounts=INFO [02-0222:36:02]StartingP2P networking INFO [02-0222:36:05] UDP listener upself=enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@14.103.209.119:30301INFO [02-0222:36:05]HTTPendpoint opened:http://0.0.0.0:8541INFO [02-0222:36:05] IPC endpoint opened: /home/neo/ethereum/data1/geth.ipc INFO [02-0222:36:05] RLPx listener upself=enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@14.103.209.119:30301INFO [02-0222:36:05] Mapped network port proto=udp extport=30301intport=30301interface="UPNP IGDv1-IP1"INFO [02-0222:36:07] Mapped network port proto=tcp extport=30301intport=30301interface="UPNP IGDv1-IP1"

2.3.2. 实例二

geth --datadir ~/ethereum/data2 init genesis.jsongeth --datadir="~/ethereum/data2"--networkid123456--port30302--rpc --rpcaddr="0.0.0.0"--rpccorsdomain"*"--rpcport8542

启动后控制台输出与实例一类似

2.3.3. 添加节点

开启一个新终端窗口,运行下面命令查看节点一的 enode 字符串

geth --exec'admin.nodeInfo.enode'attachethereum/data1/geth.ipc"enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@[::]:30301?discport=0"

进入节点二,并连接到节点一。

neo@netkiller~ % geth attach ethereum/data2/geth.ipcWelcome to the Geth JavaScript console!instance:Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1modules:admin:1.0debug:1.0eth:1.0miner:1.0net:1.0personal:1.0rpc:1.0txpool:1.0web3:1.0> admin.addPeer("enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@[::]:30302")true> admin.addPeer("enode://53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062@[::]:30301")true

查看节点

> admin.peers[{caps:["eth/63"],id:"53433417f11d1d9a17f155cbaad2c4ec375af7b141e2989f049b572fc3f856d78f254e58fa82ed6eab48a16b7d625527214522ec0fd3e3af030b5b8dfdadc062",name:"Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1",network:{localAddress:"[::1]:51250",remoteAddress:"[::1]:30301"},protocols:{eth:{difficulty:131072,head:"0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04",version:63} }}]

至此,节点已经添加完毕。

退出

2.3.4. 节点测试

这里我们实现两个节点间的以太币转账。

现在两个节点上都没有任何账号

neo@netkiller~ % geth --exec'personal.listAccounts'attach ethereum/data1/geth.ipc []neo@netkiller~ % geth --exec'personal.listAccounts'attach ethereum/data2/geth.ipc[]

在两个节点上分别创建两个账号,一个是矿工账号,另一个是普通账号。

neo@netkiller~ % geth --exec'personal.newAccount("abc123")'attach ethereum/data1/geth.ipc"0x5ad227e8d7e460713c78eebbe558473571edae72"neo@netkiller~ % geth --exec'personal.newAccount("abc123")'attach ethereum/data1/geth.ipc"0x3e822e05ee975e02be3f15f32b0fddced8d5bdd0"neo@netkiller~ % geth --exec'personal.listAccounts'attach ethereum/data1/geth.ipc ["0x5ad227e8d7e460713c78eebbe558473571edae72","0x3e822e05ee975e02be3f15f32b0fddced8d5bdd0"]neo@netkiller~ % geth --exec'personal.newAccount("abc123")'attach ethereum/data2/geth.ipc"0xa6df3e3c141e27726f4aeb21a5dab2e5c76c9565"neo@netkiller~ % geth --exec'personal.newAccount("abc123")'attach ethereum/data2/geth.ipc"0xa66c7b8b1c26856d284a0b962385babe02caa51d"neo@netkiller~ % geth --exec'personal.listAccounts'attach ethereum/data2/geth.ipc ["0xa6df3e3c141e27726f4aeb21a5dab2e5c76c9565","0xa66c7b8b1c26856d284a0b962385babe02caa51d"]

启动挖矿

geth --exec'miner.start(1)'attachethereum/data1/geth.ipc geth --exec'miner.start(1)'attachethereum/data2/geth.ipc

如果正常运行,两个节点上的矿工账号都会有一定的以太币。而普通账号额度应该为零。

neo@netkiller~ % geth --exec'eth.getBalance(eth.accounts[0])'attach ethereum/data1/geth.ipc299438256000000000000neo@netkiller~ % geth --exec'eth.getBalance(eth.accounts[1])'attach ethereum/data1/geth.ipcneo@netkiller~ % geth --exec'eth.getBalance(eth.accounts[1])'attach ethereum/data1/geth.ipc298000056000000000000neo@netkiller~ % geth --exec'eth.getBalance(eth.accounts[1])'attach ethereum/data2/geth.ipc

我们尝试从节点一矿工账号向节点二上的普通用户转账。

neo@netkiller~ % geth attach ethereum/data1/geth.ipc Welcome to the Geth JavaScript console!instance:Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1coinbase:0x5ad227e8d7e460713c78eebbe558473571edae72atblock:144(Fri,02Feb201823:24:35HST)datadir:/home/neo/ethereum/data1modules:admin:1.0debug:1.0eth:1.0miner:1.0net:1.0personal:1.0rpc:1.0txpool:1.0web3:1.0> personal.listAccounts["0x5ad227e8d7e460713c78eebbe558473571edae72"]> personal.unlockAccount(eth.accounts[],"abc123")true> eth.sendTransaction({from:"0x5ad227e8d7e460713c78eebbe558473571edae72",to:"0xa66c7b8b1c26856d284a0b962385babe02caa51d",value:web3.toWei(1,"ether")})"0x87c059d0769c8a74499ddd08c04a10f23b7681651615a28098d73ec63a943684"> eth.pendingTransactions[{blockHash:null,blockNumber:null,from:"0x5ad227e8d7e460713c78eebbe558473571edae72",gas:90000,gasPrice:18000000000,hash:"0x87c059d0769c8a74499ddd08c04a10f23b7681651615a28098d73ec63a943684",input:"0x",nonce:2,r:"0xce004f964f268a00e90cadd4e8a685131aa34f37144f7e2e47dc7fe4ec784e55",s:"0x412209c18513a28422e62c4bdb85a223f190e133cf71990a87c570a3a53ae093",to:"0xa66c7b8b1c26856d284a0b962385babe02caa51d",transactionIndex:,v:"0x41",value:1000000000000000000}]

稍后一会,当使用 eth.pendingTransactions 查看挂起交易为空的时候,表示已经处理完毕。这时退出控制台。

> eth.pendingTransactions[]> exit

现在查看节点二上的第二个普通账号余额

neo@netkiller~ % geth --exec'eth.getBalance(eth.accounts[1])'attach ethereum/data2/geth.ipc1000000000000000000

转账成功

现在我们从节点二上的普通用户向节点一上的普通用户转账。

neo@netkiller~ % geth attach ethereum/data2/geth.ipc Welcome to the Geth JavaScript console!instance:Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1coinbase:0xa6df3e3c141e27726f4aeb21a5dab2e5c76c9565atblock:319(Fri,02Feb201823:50:07HST)datadir:/home/neo/ethereum/data2modules:admin:1.0debug:1.0eth:1.0miner:1.0net:1.0personal:1.0rpc:1.0txpool:1.0web3:1.0> personal.unlockAccount(eth.accounts[1],"abc123")true> eth.sendTransaction({from:"0xa66c7b8b1c26856d284a0b962385babe02caa51d",to:"0x3e822e05ee975e02be3f15f32b0fddced8d5bdd0",value:web3.toWei(0.1,"ether")})"0x951bd161dfd000ff825379cb0644c4acd4afd4d3e1ac4f4c1c6009b3c2a1d366"> eth.pendingTransactions[]> exit

查看两个普通账号的余额

neo@netkiller~ % geth --exec'eth.getBalance(eth.accounts[1])'attach ethereum/data1/geth.ipc100000000000000000neo@netkiller~ % geth --exec'eth.getBalance(eth.accounts[1])'attach ethereum/data2/geth.ipc899622000000000000

Donations (打赏)

We accept PayPal through:

https://www.paypal.me/netkiller

http://www.netkiller.cn/home/donations.html

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180222G0DT8900?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券