windows系统下搭建以太坊私有链环境

windows系统下搭建以太坊私有链环境

最近对区块链比较感兴趣,在网上查了查资料,整理一下,供大家参考,本篇文章主要是在windows环境下搭建起eth私有链环境,后面会继续更新ethwallet以太钱包的使用等系列文章,欢迎大家一起交流,下面来看看笔记:

0、环境准备:windows10.0_X64

ETH的geth客户端:

pan.baidu.com/s/1kU9IUu 密码: 59sg

(其他版本:ethereum/go-ethereum)

1、解压客户端到任意目录,最好不要有中文目录,例如我的测试目录(D:\blockchaindev\blockchain)

2、在改文件下创建创世区块的初始化文件genesis.json (文件名固定),文件内容如下:

{ 
“difficulty”: “0x40000”, 
“extraData”: “Genesis Block”, 
“gasLimit”: “0xffffffff” 
}

备注:“difficulty”: “0x40000” //难度,该值越大,出块越慢。

“extraData”: “first block info” //附加信息,随意填写

“gasLimit”: “0xffffffff”//每个块所能容载的gas上限

3、进入cmd命令行,cd到解压目录(我的是:D:\blockchaindev\blockchain)

geth –help 查看geth命令是否正常,以及geth的使用帮助

将创世块信息初始化

geth –datadir “%cd%\chain” init genesis.json 

打开geth的控制台

geth –datadir “%cd%\chain” console  

创建用户 其中abcdefg为用户的密码,改用户为私有链主账户,后续启动私有链使用,不用着急,跟着说明来就好。

personal.newAccount(‘abcdefg’) 

退出geth控制台

exit 

启动私有链节点

-->geth –targetgaslimit 4294967295 –rpc –rpcaddr “192.168.1.107” –rpcport “8101” –port “30301” –rpcapi “eth,web3,personal” –networkid 2017 –identity 2017 -nodiscover -maxpeers 5 –datadir “%cd%\chain” -unlock 0 -rpccorsdomain “*” –mine console

注释

(targetgaslimit –每个块的gas上限,这里可以暂时理解为容量

rpc –启动rpc通信,可以进行智能合约的部署和调试

rpcaddr –rpc接口的地址

rpcport –rpc接口的端口号

port –网络监听端口,用于节点之间通信

rpcapi –设置rpc的范围,

暂时开启eth,web3,personal足够

networkid –设置当前区块链的网络ID,是一个数字,可以随便写

identity –区块链的标示,随便填写,用于标示目前网络的名字

nodiscover 禁止被网络中其它节点发现,需要手动添加该节点到网络

maxpeers 最大节点数量

datadir –设置当前区块链网络数据存放的位置

unlock –解锁某用户(此处用用户坐标来控制,解锁后的用户调用接口发起交易时,不要需要提供密码)

rpccorsdomain 限制rpc访问源的ip,代表不限制 mine 允许挖矿

console –启动命令行模式,可以在Geth中执行命令*)

键入回车
命令行中出现

Passphrase: 

键入 刚才创建用户是定义的密码
然后等待出现类似如下信息时:

I1208 17:49:49.023354 ethash.go:291] Generating DAG: 0% 
I1208 17:49:55.636941 ethash.go:291] Generating DAG: 1% 
......
I1208 17:49:55.636941 ethash.go:291] Generating DAG: 99% 
I1208 17:49:55.636941 ethash.go:291] Generating DAG: 100% 

则证明开始创建创世块,等待进度至100%后,便会开始挖矿了。

4、以下为执行过程中的具体体现:

D:\blockchaindev\blockchain>geth -help
NAME:
  geth - the go-ethereum command line interface
USAGE:
  geth [options] command [command options] [arguments...]
VERSION:
  1.4.11-stable
COMMANDS:
  import       import a blockchain file
  export       export blockchain into file
  upgradedb    upgrade chainblock database
  removedb     Remove blockchain and state databases
  dump         dump a specific block from storage
  monitor      Geth Monitor: node metrics monitoring and visualization
  account      manage accounts
  wallet       ethereum presale wallet
  console      Geth Console: interactive JavaScript environment
  attach       Geth Console: interactive JavaScript environment (connect to node)
  js           executes the given JavaScript files in the Geth JavaScript VM
  makedag      generate ethash dag (for testing)
  gpuinfo      gpuinfo
  gpubench     benchmark GPU
  version      print ethereum version numbers
  init         bootstraps and initialises a new genesis block (JSON)
  help, h      Shows a list of commands or help for one command
ETHEREUM OPTIONS:
 --datadir "C:\Users\Administrator\AppData\Roaming\Ethereum"   Data directory for the databases and keystore
 --keystore                                                    Directory for the keystore (default = inside the datadir)
 --networkid value                                             Network identifier (integer, 0=Olympic, 1=Frontier, 2=Morden) (default: 1)
 --olympic                                                     Olympic network: pre-configured pre-release test network
 --testnet                                                     Morden network: pre-configured test network with modified starting nonces (replay protection)
 --dev                                                         Developer mode: pre-configured private network with several debugging flags
 --identity value                                              Custom node name
 --fast                                                        Enable fast syncing through state downloads
 --lightkdf                                                    Reduce key-derivation RAM & CPU usage at some expense of KDF strength
 --cache value                                                 Megabytes of memory allocated to internal caching (min 16MB / database forced) (default: 128)
 --blockchainversion value                                     Blockchain version (integer) (default: 3)
ACCOUNT OPTIONS:
 --unlock value        Comma separated list of accounts to unlock
 --password value      Password file to use for non-inteactive password input
API AND CONSOLE OPTIONS:
 --rpc                 Enable the HTTP-RPC server
 --rpcaddr value       HTTP-RPC server listening interface (default: "localhost")
 --rpcport value       HTTP-RPC server listening port (default: 8545)
 --rpcapi value        API's offered over the HTTP-RPC interface (default: "eth,net,web3")
 --ws                  Enable the WS-RPC server
 --wsaddr value        WS-RPC server listening interface (default: "localhost")
 --wsport value        WS-RPC server listening port (default: 8546)
 --wsapi value         API's offered over the WS-RPC interface (default: "eth,net,web3")
 --wsorigins value     Origins from which to accept websockets requests
 --ipcdisable          Disable the IPC-RPC server
 --ipcapi value        API's offered over the IPC-RPC interface (default: "admin,debug,eth,miner,net,personal,shh,txpool,web3")
 --ipcpath "geth.ipc"  Filename for IPC socket/pipe within the datadir (explicit paths escape it)
 --rpccorsdomain value Comma separated list of domains from which to accept cross origin requests (browser enforced)
 --jspath loadScript   JavaScript root path for loadScript and document root for `admin.httpGet` (default: ".")
 --exec value          Execute JavaScript statement (only in combination with console/attach)
 --preload value       Comma separated list of JavaScript files to preload into the console
NETWORKING OPTIONS:
 --bootnodes value     Comma separated enode URLs for P2P discovery bootstrap
 --port value          Network listening port (default: 30303)
 --maxpeers value      Maximum number of network peers (network disabled if set to 0) (default: 25)
 --maxpendpeers value  Maximum number of pending connection attempts (defaults used if set to 0) (default: 0)
 --nat value           NAT port mapping mechanism (any|none|upnp|pmp|extip:<IP>) (default: "any")
 --nodiscover          Disables the peer discovery mechanism (manual peer addition)
 --nodekey value       P2P node key file
 --nodekeyhex value    P2P node key as hex (for testing)
MINER OPTIONS:
 --mine                        Enable mining
 --minerthreads value          Number of CPU threads to use for mining (default: 4)
 --minergpus value             List of GPUs to use for mining (e.g. '0,1' will use the first two GPUs found)
 --autodag                     Enable automatic DAG pregeneration
 --etherbase value             Public address for block mining rewards (default = first account created) (default: "0")
 --targetgaslimit value        Target gas limit sets the artificial target gas floor for the blocks to mine (default: "4712388")
 --gasprice value              Minimal gas price to accept for mining a transactions (default: "20000000000")
 --extradata value             Block extra data set by the miner (default = client version)
GAS PRICE ORACLE OPTIONS:
 --gpomin value        Minimum suggested gas price (default: "20000000000")
 --gpomax value        Maximum suggested gas price (default: "500000000000")
 --gpofull value       Full block threshold for gas price calculation (%) (default: 80)
 --gpobasedown value   Suggested gas price base step down ratio (1/1000) (default: 10)
 --gpobaseup value     Suggested gas price base step up ratio (1/1000) (default: 100)
 --gpobasecf value     Suggested gas price base correction factor (%) (default: 110)
VIRTUAL MACHINE OPTIONS:
 --jitvm               Enable the JIT VM
 --forcejit            Force the JIT VM to take precedence
 --jitcache value      Amount of cached JIT VM programs (default: 64)
LOGGING AND DEBUGGING OPTIONS:
 --metrics                     Enable metrics collection and reporting
 --fakepow                     Disables proof-of-work verification
 --verbosity value             Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=detail (default: 3)
 --vmodule value               Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=6,p2p=5)
 --backtrace value             Request a stack trace at a specific logging statement (e.g. "block.go:271") (default: :0)
 --pprof                       Enable the pprof HTTP server
 --pprofport value             pprof HTTP server listening port (default: 6060)
 --memprofilerate value        Turn on memory profiling with the given rate (default: 524288)
 --blockprofilerate value      Turn on block profiling with the given rate (default: 0)
 --cpuprofile value            Write CPU profile to the given file
 --trace value                 Write execution trace to the given file
EXPERIMENTAL OPTIONS:
 --shh         Enable Whisper
 --natspec     Enable NatSpec confirmation notice
MISCELLANEOUS OPTIONS:
 --solc value          Solidity compiler command to be used (default: "solc")
 --support-dao-fork    Updates the chain rules to support the DAO hard-fork
 --oppose-dao-fork     Updates the chain rules to oppose the DAO hard-fork
 --help, -h            show help

初始化创世区块内容

D:\blockchaindev\blockchain>geth -datadir "%cd%\chain" init genesis.json
I1223 10:08:43.809936 ethdb/database.go:82] Alloted 16MB cache and 16 file handles to D:\blockchaindev\blockchain\chain\chaindata
I1223 10:08:43.991765 cmd/geth/main.go:299] successfully wrote genesis block and/or chain rule set: ed2df46a5b327fadb593c7f4ff0746d97f4fecb6373b48c57a4f93d3d0c03435
D:\blockchaindev\blockchain>geth -datadir "%cd%\chain" console
I1223 10:09:28.036526 ethdb/database.go:82] Alloted 128MB cache and 1024 file handles to D:\blockchaindev\blockchain\chain\chaindata
I1223 10:09:28.192817 ethdb/database.go:169] closed db:D:\blockchaindev\blockchain\chain\chaindata
I1223 10:09:28.192817 cmd/utils/flags.go:592] WARNING: No etherbase set and no accounts found as default
I1223 10:09:28.192817 ethdb/database.go:82] Alloted 128MB cache and 1024 file handles to D:\blockchaindev\blockchain\chain\chaindata
I1223 10:09:28.302193 eth/backend.go:621] upgrading db log bloom bins
I1223 10:09:28.302193 eth/backend.go:629] upgrade completed in 0
I1223 10:09:28.302193 ethdb/database.go:82] Alloted 16MB cache and 16 file handles to D:\blockchaindev\blockchain\chain\dapp
I1223 10:09:28.369501 eth/backend.go:172] Protocol Versions: [63 62], Network Id: 1
I1223 10:09:28.369501 eth/backend.go:201] Blockchain DB Version: 3
I1223 10:09:28.370505 core/blockchain.go:206] Last header: #0 [ed2df46a…] TD=262144
I1223 10:09:28.370505 core/blockchain.go:207] Last block: #0 [ed2df46a…] TD=262144
I1223 10:09:28.370505 core/blockchain.go:208] Fast block: #0 [ed2df46a…] TD=262144
I1223 10:09:28.371688 p2p/server.go:313] Starting Server
I1223 10:09:30.273071 p2p/nat/nat.go:111] mapped network port udp:30303 -> 30303 (ethereum discovery) using UPNP IGDv1-IP1
I1223 10:09:30.319900 p2p/discover/udp.go:217] Listening, enode://bb8f35cfc7aef69ee06a542ca9430c3419ec43b74a0e4a430542a6fbaa7e52457c325aba026f7f5c4e3a5a917edfc792018aa5f338e47a4b78282ac5f179cd3c@223.20.249.137:30303
I1223 10:09:30.319900 p2p/server.go:556] Listening on [::]:30303
I1223 10:09:30.319900 node/node.go:296] IPC endpoint opened: \\.\pipe\geth.ipc
I1223 10:09:30.381014 p2p/nat/nat.go:111] mapped network port tcp:30303 -> 30303 (ethereum p2p) using UPNP IGDv1-IP1
Welcome to the Geth JavaScript console!
instance: Geth/v1.4.11-stable/windows/go1.6.2
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

新建账户

> personal.newAccount( 'abcdefg' )
"0x55261e32008f9a65f86de9895544eaa8868c7fcd"

退出控制台

> exit
I1223 10:14:59.151943 node/node.go:328] IPC endpoint closed: \\.\pipe\geth.ipc
I1223 10:14:59.151943 core/blockchain.go:565] Chain manager stopped
I1223 10:14:59.151943 eth/handler.go:224] Stopping ethereum protocol handler...
I1223 10:14:59.151943 eth/handler.go:245] Ethereum protocol handler stopped
I1223 10:14:59.151943 core/tx_pool.go:163] Transaction pool stopped
I1223 10:14:59.151943 eth/backend.go:500] Automatic pregeneration of ethash DAG OFF (ethash dir: C:\Users\Administrator\AppData\Ethash)
I1223 10:14:59.152900 ethdb/database.go:169] closed db:D:\blockchaindev\blockchain\chain\chaindata
I1223 10:14:59.152900 ethdb/database.go:169] closed db:D:\blockchaindev\blockchain\chain\dapp

启动私有链节点

D:\blockchaindev\blockchain>geth -targetgaslimit 4294967295 -rpc -rpcaddr "192.168.1.107" -rpcport "8101" -port "30301" -rpcapi "eth,web3,personal" -networkid 2017 -identity 2017 -nodiscover -maxpeers 5 -datadir "%cd%\chain" -unlock 0 -rpccorsdomain "*" -mine console
I1223 10:27:48.136134 ethdb/database.go:82] Alloted 128MB cache and 1024 file handles to D:\blockchaindev\blockchain\chain\chaindata
I1223 10:27:48.308053 ethdb/database.go:169] closed db:D:\blockchaindev\blockchain\chain\chaindata
I1223 10:27:48.308053 ethdb/database.go:82] Alloted 128MB cache and 1024 file handles to D:\blockchaindev\blockchain\chain\chaindata
I1223 10:27:48.417402 ethdb/database.go:82] Alloted 16MB cache and 16 file handles to D:\blockchaindev\blockchain\chain\dapp
I1223 10:27:48.464300 eth/backend.go:172] Protocol Versions: [63 62], Network Id: 2017
I1223 10:27:48.464300 eth/backend.go:201] Blockchain DB Version: 3
I1223 10:27:48.464300 core/blockchain.go:206] Last header: #0 [ed2df46a…] TD=262144
I1223 10:27:48.464300 core/blockchain.go:207] Last block: #0 [ed2df46a…] TD=262144
I1223 10:27:48.464300 core/blockchain.go:208] Fast block: #0 [ed2df46a…] TD=262144
I1223 10:27:48.464300 p2p/server.go:313] Starting Server
I1223 10:27:48.464300 node/node.go:366] HTTP endpoint opened: http://192.168.1.107:8101
Unlocking account 0 | Attempt 1/3
Passphrase: (此处输入之前的abcdefg密码)
I1223 10:28:28.741460 cmd/geth/accountcmd.go:189] Unlocked account 55261e32008f9a65f86de9895544eaa8868c7fcd
I1223 10:28:28.755470 miner/miner.go:119] Starting mining operation (CPU=4 TOT=5)
I1223 10:28:28.769498 miner/worker.go:573] commit new work on block 1 with 0 txs & 0 uncles. Took 14.0287ms
I1223 10:28:28.781490 ethash.go:259] Generating DAG for epoch 0 (size 1073739904) (0000000000000000000000000000000000000000000000000000000000000000)
Welcome to the Geth JavaScript console!
instance: Geth/v1.4.11-stable/windows/go1.6.2/2017
coinbase: 0x55261e32008f9a65f86de9895544eaa8868c7fcd
at block: 0 (Thu, 01 Jan 1970 08:00:00 CST)
datadir: D:\blockchaindev\blockchain\chain
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

现在开始创建创世区块

> I1223 10:28:39.215680 ethash.go:291] Generating DAG: 0%
I1223 10:28:43.918361 ethash.go:291] Generating DAG: 1%
I1223 10:28:48.394782 ethash.go:291] Generating DAG: 2%
I1223 10:28:53.106499 ethash.go:291] Generating DAG: 3%
        ......
I1223 10:36:17.364772 ethash.go:291] Generating DAG: 98%
I1223 10:36:21.974501 ethash.go:291] Generating DAG: 99%
I1223 10:36:26.578553 ethash.go:291] Generating DAG: 100%

这里创世区块创建完成,开始挖矿

I1223 10:36:26.582539 ethash.go:276] Done generating DAG for epoch 0, it took 7m57.8060368s
I1223 10:36:31.944388 miner/worker.go:339] ?  Mined block (#1 / dad26a45). Wait 5 blocks for confirmation
I1223 10:36:31.947390 miner/worker.go:573] commit new work on block 2 with 0 txs & 0 uncles. Took 0
I1223 10:36:31.972409 miner/worker.go:339] ?  Mined stale block (#1 / 9324f1b8).
I1223 10:36:31.975409 miner/worker.go:573] commit new work on block 2 with 0 txs & 0 uncles. Took 24.0175ms
I1223 10:36:31.977412 miner/worker.go:573] commit new work on block 2 with 0 txs & 0 uncles. Took 0
                                                                 ......
I1223 10:36:52.562017 miner/worker.go:573] commit new work on block 17 with 0 txs & 0 uncles. Took 0
I1223 10:36:52.568019 miner/worker.go:573] commit new work on block 17 with 0 txs & 0 uncles. Took 0
I1223 10:36:52.693107 miner/worker.go:339] ?  Mined block (#17 / bb7e4ddb). Wait 5 blocks for confirmation
I1223 10:36:52.699114 miner/worker.go:573] commit new work on block 18 with 0 txs & 0 uncles. Took 0
I1223 10:36:53.212477 miner/worker.go:573] commit new work on block 18 with 0 txs & 0 uncles. Took 0

挖矿开始后,屏幕会不停的刷,此时执行miner.stop()即可停止,不要在意输出的刷屏,只管输入完成后回车即可,如果输入正确,回车后会输出“true”。

备注几个geth的使用命令,后面会详细整理一篇geth操作命令介绍:

eth.accounts   查看账户
miner.start()    启动挖矿
miner.stop()    停止挖矿

参考文档

更多交流,欢迎WX:zhengpeng0725

编辑于 2017-12-23