如何配置一个私有区块链测试网络

本文使用geth命令配置一个私有以太坊网络,初始化命令是 

geth --identity "MyNodeName" --genesis /path/to/CustomGenesis.json --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "C:\chains\TestChain1" --port "30303" --nodiscover --rpcapi "db,eth,net,web3" --networkid 1999 init /path/to/CustomGenesis.json

配置一个私有网络要做以下4步

  • Custom Genesis File 自定义Genesis文件
  • Custom Data Directory 配置数据文件目录
  • Custom NetworkID 自定义NetworkID
  • (Recommended) Disable Node Discovery 禁用节点发现功能

The genesis file

CustomGenesis.json

{
    "nonce": "0x0000000000000042",     "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x0",     "gasLimit": "0x8000000",     "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",     "alloc": {     }
}

下面解释其它参数

--nodiscover 禁节点发现

--maxpeers 0 允许其它人连起的数量,因是私有节点设为0不允许任何人连结

--rpc 使用RPC进行通讯,默认设置

--rpcapi "db,eth,net,web3" 允许的API使用RPC,这里使用web3。默认允许所有API运行在RPC上,db,eth,net and web3运行在IPC上

--rpcport "8080" RPC通讯的端口

--rpccorsdomain "http://chriseth.github.io/browser-solidity/" 允许什么URL来源的可以使用节点通讯,注意避免使用全局通配符*

--datadir "/home/TestChain1" chain数据保存的目录

--port "30303" 本地网络端口,用于与外部节点通讯的端口

--identity "TestnetMainNode" 你的节点的名称

初始化后执行交互命令

geth --identity "MyNodeName" --genesis /path/to/CustomGenesis.json --rpc --rpcport "8080" --rpccorsdomain "*" --datadir "C:\chains\TestChain1" --port "30303" --nodiscover --rpcapi "db,eth,net,web3" --networkid 1999 console

 

geth完整命令参考

https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console

一些常用命令

#如果要attch到一个已经在运行的实例上
geth attach
#创建新帐号
personal.newAccount("password")
#配置成etherbase
miner.setEtherbase(personal.listAccounts[0])
#开始挖矿
miner.start()

 

一个交互例子

geth --datadir ~/dapps/testing/00/ --port 30310 --rpcport 8110 --networkid 4567890 --nodiscover --maxpeers 0 --vmdebug --verbosity 6 --pprof --pprofport 6110 console 2>> ~/dapp/testint/00/00.log

geth --datadir ~/dapps/testing/00/ --nodiscover --rpc --dev --mine --minerthreads 1 --unlock 0 console 2>>geth.log

Before you can submit any transactions, you need set up your private test chain. See Test Networks.

// create account. will prompt for password
personal.newAccount();
// name your primary account, will often use it
primary = eth.accounts[0];
// check your balance (denominated in ether)
balance = web3.fromWei(eth.getBalance(primary), "ether");
// assume an existing unlocked primary account
primary = eth.accounts[0];

// mine 10 blocks to generate ether

// starting miner
miner.start(4);
// sleep for 10 blocks (this can take quite some time).
admin.sleepBlocks(10);
// then stop mining (just not to burn heat in vain)
miner.stop();
balance = web3.fromWei(eth.getBalance(primary), "ether");

After you create transactions, you can force process them with the following lines:

miner.start(1);
admin.sleepBlocks(1);
miner.stop();

You can check your pending transactions with:

// shows transaction pool
txpool.status
// number of pending txs
eth.getBlockTransactionCount("pending");
// print all pending txs
eth.getBlock("pending", true).transactions

If you submitted contract creation transaction, you can check if the desired code actually got inserted in the current blockchain:

txhash = eth.sendTansaction({from:primary, data: code})
//... mining
contractaddress = eth.getTransactionReceipt(txhash);
eth.getCode(contractaddress)

转载于:https://my.oschina.net/swingcoder/blog/759257

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值