经过本文所述办法和项目中的脚本,咱们能够快速的建立好自己的私有链进行区块链开发测试,本文基于以太坊技能进行建立,分两个部分,一个是Ubuntu下建立办法,另一个是Windwos下建立办法,关于以太坊相关的基础知识,能够看我原先宣布的一些文章,地址如下:http://blog.csdn.net/sportshark

一、Ubuntu下装置Geth客户端
之所以采用Ubuntu,是由于以太坊的官方对Ubuntu支撑的很好,是在各个linux体系中装置最简略。
Geth官方装置指南:
https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum
进入ubuntu指令行,履行如下指令
sudo apt-get update
sudo apt-get installsoftware-properties-common
sudo add-apt-repository -yppa:ethereum/ethereum
sudo add-apt-repository -yppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install ethereum
体系联网履行后,即完成了装置以太坊客户端,其中包括geth,bootnode, evm, disasm, rlpdump,ethtest
此时假如输入Geth指令,会呈现发动以太坊发动的画面
二、装置Windows下Geth客户端
Windows有必要64位体系,从官方网站下载编译好的win64客户端,解压缩即可运转,下载地址如下:https://github.com/ethereum/go-ethereum/releases/
下载后,只要一个Geth.exe的文件。
装置图像化客户端Mist,依然是从官方地址下载编译好的客户端即可,下载地址:https://github.com/ethereum/mist/releases/
下载解压缩后,Ethereum-Wallet即为以太坊图形化界面。
三、预备创世块文件
装备自己的创世块是为了区分公有链,同一个网络中,创世块有必要是相同的,否则无法联通,此办法在windows和Ubuntu下通用。
新建文件piccgenesis.json,输入如下内容并保存
https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
"nonce":"0x0000000000000042",
"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x4000",
"alloc": https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg},
"coinbase":"0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "PICC GenesisBlock",
"gasLimit":"0xffffffff"
}
解释一下各个参数的效果:
mixhash
与nonce合作用于挖矿,由上一个区块的一部分生成的hash。留意他和nonce的设置需求满意以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。.
nonce
nonce便是一个64位随机数,用于挖矿,留意他和mixhash的设置需求满意以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。
difficulty
设置当时区块的难度,假如难度过大,cpu挖矿就很难,这儿设置较小难度
alloc
用来预置账号以及账号的以太币数量,由于私有链挖矿比较简单,所以咱们不需求预置有币的账号,需求的时分自己创立即能够。
coinbase
矿工的账号,随意填
timestamp
设置创世块的时间戳
parentHash
上一个区块的hash值,由于是创世块,所以这个值是0
extraData
附加信息,随意填,能够填你的特性信息
gasLimit
该值设置对GAS的消耗总量约束,用来约束区块能包括的买卖信息总和,由于咱们是私有链,所以填最大。
四、发动私有链节点
发动Geth即能够发动以太坊的区块链,为了构建私有链 ,需求在Geth发动时加入一些参数,Geth参数含义如下:
identity
区块链的标明,随意填写,用于标明现在网络的姓名
init
指定创世块文件的方位,并创立初始块
datadir
设置当时区块链网络数据寄存的方位
port
网络监听端口
rpc
发动rpc通讯,能够进行智能合约的布置和调试
rpcapi
设置允许衔接的rpc的客户端,一般为db,eth,net,web3
networkid
设置当时区块链的网络ID,用于区分不同的网络,是一个数字
console
发动指令行形式,能够在Geth中履行指令
1、在Ubuntu发动区块链节点
在Ubuntu下,首要切换到打算运转的目录,目录下应该有装备好的piccgenesis.json文件,履行如下指令
basepath=$(cd `dirname $0`; pwd)
获取当时的目录
geth --datadir "$basepath/chain" init piccgenesis.json
创立数据寄存地址并初始化创世块
geth --identity"PICCetherum" --rpc --rpccorsdomain "*" --datadir "$basepath/chain" --port "30303" --rpcapi "db,eth,net,web3"--networkid 95518 console
发动后界面如下,光标停留在最终的指令行上,能够履行以太坊指令。
I0707 00:45:43.680087 ethdb/database.go:82]Alloted 128MB cache and 1024 file handles to /home/lihe/桌面/chain/chaindata
I0707 00:45:43.726008ethdb/database.go:169] closed db:/home/lihe/桌面/chain/chaindata
I0707 00:45:43.728913 ethdb/database.go:82]Alloted 128MB cache and 1024 file handles to /home/lihe/桌面/chain/chaindata
I0707 00:45:43.908795 ethdb/database.go:82]Alloted 16MB cache and 16 file handles to /home/lihe/桌面/chain/dapp
I0707 00:45:43.969506 core/genesis.go:92]Genesis block already in chain. Writing canonical number
I0707 00:45:43.980337 eth/backend.go:274]Successfully wrote custom genesis block:6e92f8b23bcdfdf34dc813cfaf1d84b71beac80530506b5d63a2df10fe23a660
I0707 00:45:43.980618 eth/backend.go:184]Protocol Versions: [63 62], Network Id: 95518
I0707 00:45:43.981567core/blockchain.go:204] Last header: #81 [6193c4b0…] TD=10836704
I0707 00:45:43.981645core/blockchain.go:205] Last block: #81 [6193c4b0…] TD=10836704
I0707 00:45:43.981677core/blockchain.go:206] Fast block: #81 [6193c4b0…] TD=10836704
I0707 00:45:43.985253 p2p/server.go:313]Starting Server
I0707 00:45:45.834488p2p/discover/udp.go:217] Listening,enode://134881790e54c803955715e3661c27f91caaf499be813e29c9f986e2eac62d47e02b13a8e51776c1caea554655614ed26ce0185d84e626da7ac48a83a60113ff@[::]:30303
I0707 00:45:45.835853 node/node.go:366]HTTP endpoint opened: http://localhost:8545
I0707 00:45:45.848008 p2p/server.go:556]Listening on [::]:30303
I0707 00:45:45.849731 node/node.go:296] IPCendpoint opened: /home/lihe/桌面/chain/geth.ipc
Welcome to the Geth JavaScript console!
instance:Geth/v1.5.0-unstable/linux/go1.5.1/PICCetherum
coinbase:0x93509a2f4b2b974b07ef0b52e07c3992601f5de1
at block: 81 (Tue, 05 Jul 2016 21:02:25CST)
datadir: /home/lihe/桌面/chain
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>
能够看到Listening on [::]:30303和Welcome to the Geth JavaScript console!的提示,阐明现已发动成功
留意:假如想将Ubuntu作为永久区块链节点运用,当运用nohup指令时,Geth发动参数console有必要去掉,否则Geth会主动中止。
2、在windows发动区块链节点
进入Windows下Geth的目录 ,放置装备好的piccgenesis.json文件,履行如下指令:
geth --datadir "%cd%\chain" init piccgenesis.json
创立数据寄存地址并初始化创世块
geth--identity "PICCetherum" --rpc--rpccorsdomain "*" --datadir "%cd%\chain" --port"30303" --rpcapi"db,eth,net,web3" --networkid 95518 console
当看到Listening on [::]:30303和Welcome to the Geth JavaScript console!的提示,阐明现已发动成功
五、运用节点创立账号
发动节点成功后,会进入Geth的指令行形式,输入如下指令
personal.newAccount()
体系会提示你输入账号密码,并确认,最终会显现一个新生成的账号。
六、发动Windows下私有链图形节点
首要按上面的过程发动Geth并创立了账号,然后解压缩Ethereum-Wallet,运转Ethereum-Wallet.exe,即发动成功,假如区块链正常的话,会在右上角显现“PRIVATE-NET”,点击“LAUNCH APPLICATION”进入图形界面即可。
七、衔接其他节点
首要要知道自己的节点信息,在Geth指令行界面下输入指令,留意大小写
admin.nodeInfo
体系会显现
enode:"enode://1e3c1727cd3bee9f25edeb5dbb3b880e03e41f8eec99566557f3ee0422734a8fcad17c161aa93d61bdbfb28ed152c143c7eb501db58bc63502a104a84b62d742@0.0.0.0:30303“
其中
enode://1e3c1727cd3bee9f25edeb5dbb3b880e03e41f8eec99566557f3ee0422734a8fcad17c161aa93d61bdbfb28ed152c143c7eb501db58bc63502a104a84b62d742@0.0.0.0:30303
便是自己节点的信息,留意要把“0.0.0.0“换成你自己的IP。将这个信息发送给其他节点,在其他节点的指令行中输入:
admin.addPeer(‘enode://1e3c1727cd3bee9f25edeb5dbb3b880e03e41f8eec99566557f3ee0422734a8fcad17c161aa93d61bdbfb28ed152c143c7eb501db58bc63502a104a84b62d742@192.168.1.101:30303’)
假如添加成功,输入admin.peers会显现出新添加的节点。
八、运用节点进行挖矿
在Geth指令行界面下,输入miner.start()即发动挖矿,挖矿后,会不停刷屏,输入miner.stop()即中止,不必管刷屏导致的指令不全,指令会正常履行。
到这一步,现已组建一个私有链的网络,能够像其他区块链相同不停的扩大这个网络,下一篇文章,我会介绍如安在私有链上编写、调试和布置智能合约。
参阅文章:
1. http://tech.lab.carl.pro/kb/ethereum/testnet_setup
2. http://www.ethdocs.org/en/latest/network/test-networks.html#setting-up-a-local-private-testnet
3. https://github.com/ethereum/go-ethereum/wiki/Connecting-to-the-network
4. https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console
5. https://github.com/ethereum/go-ethereum/wiki/Mining
6. https://github.com/ethereum/go-ethereum/wiki/Managing-your-accounts
7. https://github.com/janx/ethereum-bootstrap

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注