一步步教你搭建以太坊私有链

本文将介绍如何在Windows平台下搭建以太坊么有链,其他平台大同小异。

安装以太坊客户端

下载地址:http://ethfans.org/wikis/Ethereum-Geth-Mirror
下载完成后直接点击安装,默认安装在C:\Program Files\Geth
打开命令提示窗口,输入:

geth --help

检查是否安装成功。

搭建私有链

创建一个初始账户,用于接受挖矿奖励。

geth --datadir chain account new

这里写图片描述
自定义创世区块:

{
  "config": {
        "chainId": 10, 
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {},
  "coinbase"   : "0xe0c713662fb4e81b6a5f5c1fc9d3bc6cb9d935c1",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

启动私有链节点

geth –targetgaslimit 4294967295 –rpc –rpcaddr “192.168.1.5” –rpcport “8101” –port “30301” –rpcapi “eth,web3,personal” –networkid 20180401 –identity 20180401 -nodiscover -maxpeers 5 –datadir “chain” -unlock 0 -rpccorsdomain*” –mine console 2>geth.log
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中执行命令

Javascript Console命令

执行以上命令后即进入Javascript console执行环境,其内置了以下Javascript对象:

eth:包含一些跟操作区块链相关的方法
net:包含以下查看p2p网络状态的方法
admin:包含一些与管理节点相关的方法
miner:包含启动&停止挖矿的一些方法
personal:主要包含一些管理账户的方法
txpool:包含一些查看交易内存池的方法
web3:包含了以上对象,还包含一些单位换算的方法

启动/停止挖矿

启动挖矿:

miner.start(2)

停止挖矿:

miner.stop()

创建账户

查看已有账户:

eth.accounts

输出:
[“0xe0c713662fb4e81b6a5f5c1fc9d3bc6cb9d935c1”](我们在初始时创建的账户)
再创建一个新账户:

personal.newAccount()

输入密码后,账户创建成功:
“0x8fa760c27ec1ea2173d11ed98ab5c62a6c967c1f”
再次查看账户,已经有两个账户了:

> eth.accounts
["0xe0c713662fb4e81b6a5f5c1fc9d3bc6cb9d935c1", "0x8fa760c27ec1ea2173d11ed98ab5c62a6c967c1f"]

查看账户余额

> eth.getBalance(eth.accounts[0])
745000000000000000000
> eth.getBalance(eth.accounts[1])
0

由于前面我们已经启动了挖矿,所以账户0中已经有挖矿所得的以太币了。
getBalance()返回值的单位是wei,wei是以太币的最小单位,1个以太币=10的18次方个wei。要查看有多少个以太币,可以用web3.fromWei()将返回值换算成以太币:

> web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
745
>

说明已经成功挖到745个以太币。

挖矿账户更改

> eth.coinbase
"0xe0c713662fb4e81b6a5f5c1fc9d3bc6cb9d935c1"

现在的coinbase是账户0,要想使挖矿奖励进入其他账户,通过miner.setEtherbase()将其他账户设置成coinbase即可:

> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0x8fa760c27ec1ea2173d11ed98ab5c62a6c967c1f"

发送交易

查看当前账户余额,账户1余额为0:

> eth.getBalance(eth.accounts[0])
745000000000000000000
> eth.getBalance(eth.accounts[1])
0
>

下面我们将从账户0转给账户1以太币10 个:

> amount = web3.toWei(10,'ether')
"10000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
Error: authentication needed: password or unlock
    at web3.js:3143:20
    at web3.js:6347:15
    at web3.js:5081:36
    at <anonymous>:1:1

这里报错了,原因是账户每隔一段时间就会被锁住,要发送交易,必须先解锁账户,由于我们要从账户0发送交易,所以要解锁账户0:

> personal.unlockAccount(eth.accounts[0])
Unlock account 0xe0c713662fb4e81b6a5f5c1fc9d3bc6cb9d935c1
Passphrase:
true

重新发送交易:

> amount = web3.toWei(10,'ether')
"10000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
"0xd43bc37ff7ebdbf87438f572e0022c9e8f343f2316a51a494321c623013d41fe"

我们去查看账户1中的余额:

> eth.getBalance(eth.accounts[1])
0

发现还没转过去,此时交易已经提交到区块链,但还未被处理,这可以通过查看txpool来验证:

> txpool.status
{
  pending: 1,
  queued: 0
}

其中有一条pending的交易,pending表示已提交但还未被处理的交易。

要使交易被处理,必须要挖矿。这里我们启动挖矿,然后等待挖到一个区块之后就停止挖矿:

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

当miner.stop()返回true后,txpool中pending的交易数量应该为0了,说明交易已经被处理了,而账户1应该收到币了:

> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')
10

查看交易和区块

查看当前区块总数:

查看当前区块总数:

通过区块号查看区块,我们以第0个区块为例:

> eth.getBlock(0)
{
  difficulty: 131072,
  extraData: "0x",
  gasLimit: 3141592,
  gasUsed: 0,
  hash: "0x3bb1cd1e7054c44bd978c8a85a3a9e2a7f641630c98367d6d972fbcf098b1bee",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xe0c713662fb4e81b6a5f5c1fc9d3bc6cb9d935c1",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000042",
  number: 0,
  parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 507,
  stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  timestamp: 0,
  totalDifficulty: 131072,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

通过交易hash查看交易:

> eth.getTransaction("0xd43bc37ff7ebdbf87438f572e0022c9e8f343f2316a51a494321c623013d41fe")
{
  blockHash: "0x6d0e5b8978e6cf82787604b0dfc6d68264f96c94c7efaa6361e9404c9c062891",
  blockNumber: 150,
  from: "0xe0c713662fb4e81b6a5f5c1fc9d3bc6cb9d935c1",
  gas: 90000,
  gasPrice: 18000000000,
  hash: "0xd43bc37ff7ebdbf87438f572e0022c9e8f343f2316a51a494321c623013d41fe",
  input: "0x",
  nonce: 1,
  r: "0xd33da9fd395daed7e68bca0f5c01bdbdfb49ff677796787664f01e6b0fb72291",
  s: "0x331513846c2b55ef28661ee0f149fdfb4042e5f683ee0eb4d7f8d6aedc18d576",
  to: "0x8fa760c27ec1ea2173d11ed98ab5c62a6c967c1f",
  transactionIndex: 1,
  v: "0x37",
  value: 10000000000000000000
}

猜你喜欢

转载自blog.csdn.net/anda0109/article/details/79783064