搭建以太坊私有链

2019-03-27  本文已影响0人  jiangfengzh

1.go-ethereum 编译安装

geth version

geth --help

2.搭建私有链

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

3.初始化写入创世区块 注:文件名按照自己的需求自定义去做.

cd privateNetwork
geth --datadir data0 init genesis.json

初始化成功显示信息.png

4.启动私有链节点

geth --datadir data0 --networkid 11 console

补充:启动geth节点客户端方式二:

geth --identity "TestNode1" --datadir "data0" --rpc --rpcapi "db,eth,net,web3" --rpcaddr "127.0.0.1" --rpcport "8486" --port "30304" --networkid "29382" console

--datadir : 指定节点存在位置
--rpc : 启用http-rpc服务器
--rpcapi : 基于http-rpc提供的api接口。eth,net,web3,db
--rpcaddr : http-rpc服务器接口地址:默认“127.0.0.1”
--rpcport : http-rpc 端口(多节点时,不要重复)
--port : 节点端口号(多节点时,不要重复)
--networkid : 网络标识符 随便指定一个id(确保多节点是统一网络,保持一致)

注释说明: 上面命令的主体是geth console,表示启动节点并进入交互式控制台,–datadir选项指定使用data0作为数据目录,–networkid选项后面跟一个数字,这里是11,表示指定这个私有链的网络id为11。网络id在连接到其他节点的时候会用到,以太坊公网的网络id是1,为了不与公有链网络冲突,运行私有链节点的时候要指定自己的网络id,失败之后重启就可以.

Javascript Console操作

创建账户

前面只是搭建了私有链,并没有自己的账户,可以在js console中输入eth.accounts来验证

personal.newAccount("186")
"0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f"

创建账户,查询账户.png

查看账户

eth.accounts

查看账户.png

账户默认会保存在数据目录的keystore文件夹中。查看目录结构,发现data0/keystore中多了两个文件,这两个文件就对应刚才创建的两个账户,这是json格式的文本文件,可以打开查看,里面存的是私钥经过密码加密后的信息


keystore.png

查看账户余额

eth对象提供了查看账户余额的方法

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

目前两个账户的以太币余额都是0,要使账户有余额,可以从其他账户转账过来,或者通过挖矿来获得以太币奖励

启动&停止挖矿

miner.start(10)

注:其中start的参数表示挖矿使用的线程数。第一次启动挖矿会先生成挖矿所需的DAG文件

miner.stop()

挖到一个区块会奖励5个以太币,挖矿所得的奖励会进入矿工的账户,这个账户叫做coinbase,默认情况下coinbase是本地账户中的第一个账户:

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

miner.setEtherbase(eth.accounts[1])

挖矿 获取代币

账户0和1的账户已近有余额了.png

getBalance()返回值的单位是wei,wei是以太币的最小单位,1个以太币=10的18次方个wei。要查看有多少个以太币,可以用web3.fromWei()将返回值换算成以太币.

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

发送交易转账

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
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x476587659b981486f969aed80bba468815e68622
Passphrase:
true
> amount = web3.toWei(10,'ether')
"10000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
INFO [03-27|10:07:12.547] Submitted transaction                    fullhash=0xcd
5306a3de2e639c14a7af9e2a86b84474672bc89d3fffc0b04884c046710a6e recipient=0x97025
f7e01696Be9ef95FEf15471A30532Cd8E0d
"0xcd5306a3de2e639c14a7af9e2a86b84474672bc89d3fffc0b04884c046710a6e"
> eth.getBalance(eth.accounts[0])
115000000000000000000
> eth.getBalance(eth.accounts[1])
65000000000000000000
>
> txpool.status
{
  pending: 4,
  queued: 0
}

其中有四条pending的交易,pending表示已提交但还未被处理的交易。
要使交易被处理,必须要挖矿。这里我们启动挖矿,然后等待挖到一个区块之后就停止挖矿:

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

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

> txpool.status
{
  pending: 0,
  queued: 0
}
> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')
85.00000100000000001
> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
99.99999899999999999
>

查看交易和区块

eth.blockNumber
 eth.getBlock(9)
{
  difficulty: 131584,
  extraData: "0xda83010817846765746888676f312e31312e358777696e646f7773",
  gasLimit: 3169294,
  gasUsed: 0,
  hash: "0x0c9dd8a50b0574d8ce0745d84995de501a5c44650eff4db7136e9991e8bd8588",
  logsBloom: "0x0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000",
  miner: "0x476587659b981486f969aed80bba468815e68622",
  mixHash: "0xdb08505757df373da4b5049a6bce6178ca4237de20a55a8e93d7c2c9658ced49",

  nonce: "0x08ef051d89684312",
  number: 9,
  parentHash: "0x01f7839a9ebdec086c70e0ec7439fe262d155f41e6a9db4b5135f52d8ed25d4
e",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b
421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934
7",
  size: 538,
  stateRoot: "0x06fe0e99e91a885b7c25a0c43361df2c53aebe18b7488cfc3ba393ee8483bbf8
",
  timestamp: 1553587671,
  totalDifficulty: 1202432,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e
363b421",
  uncles: []
}
eth.getTransaction("0xcd5306a3de2e639c14a7af9e2a86b84474672bc89d3fffc0b04884c0
46710a6e")
{
  blockHash: "0x2c4a4205a0bcef85189203fe39c75fcd63be1a0fb17ef6cd83436837c77cff32
",
  blockNumber: 37,
  from: "0x476587659b981486f969aed80bba468815e68622",
  gas: 90000,
  gasPrice: 1000000000,
  hash: "0xcd5306a3de2e639c14a7af9e2a86b84474672bc89d3fffc0b04884c046710a6e",
  input: "0x",
  nonce: 2,
  r: "0x3672eff2f2b6f6e58cdc36d61aa47946af03b35d2f3e85ef1be8c671eeeb25e1",
  s: "0xa89436dc4ec6e6216b3b9df9ddd08416d657653b7c92b734fe72b92e2d1c31e",
  to: "0x97025f7e01696be9ef95fef15471a30532cd8e0d",
  transactionIndex: 2,
  v: "0x49",
  value: 10000000000000000000
}
上一篇 下一篇

猜你喜欢

热点阅读