以太坊学习3--私链的部署使用

导语:

主链和测试链同步速度太慢了,所以说初次学习最好在自己的电脑上搭建私链去学习它的基本用法。以太坊支持自定义创世区块,要运行私有链,我们就需要定义自己的创世区块,配置自己的创世块是为了区分公有链,同一个网络中,创世块必须是一样的,否则无法联通。在D:\blockchain目录下放置初始化创世块文件名字为genesis.json

{
"config": {
        "chainId": 123456,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
},
"nonce": "0x0000000000000042",
"difficulty": "0x020000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x4c4b40",
"alloc": {}
}

个别参数说明:
difficulty:挖矿难度
gasLimit:每块的最大数量
 
进入D:\blockchain目录,新建一个data文件夹,不创建也可以,不创建的话下面的datadir参数后面跟的就是”./”,这里是将新生成的文件都放到data目录下方便管理。
 

初始化:

geth --datadir data init genesis.json

个别参数说明:
init:表示初始化区块,后面跟着创世块的配置文件genesis.json
datadir:数据存放的位置
 

启动节点:

geth --datadir data --networkid 123456 --rpc --rpccorsdomain "*" --nodiscover console

个别参数说明:
network:设置当前区块链的网络ID,用于区分不同的网络,1表示公链
rpc:表示启动rpc通信,可以进行智能合约的部署和调试
nodiscover:别人不能发现连接你
console:表示启动命令行模式,可以在Geth中执行命令
也可以指定端口号启动:

geth --datadir D:\blockchain\30304 --port 30304 --ipcpath "D:\blockchain\30304\geth.ipc"

D:\blockchain\30304:代表指定的存储目录。需要注意的时,每次运行geth命令都需要指定目录,否则geth会调用默认的C盘目录。
–port 30304:代表指定的端口,默认是30303端口。
–ipcpath “D:\blockchain\30304\geth.ipc”:代表指定的IPC,默认的是\geth.ipc。
通过以上的命令,可以实现一台主机开启多个geth。
 

创建新账号:

> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xa9ab17e577fba6ddd1c27d1785add6f48f678a8b"

或者

> personal.newAccount("123456")
"0xedf21069693886b60f56a51dae075e7385e4b959"

注:括号里的是账户密码,这个一定要记住,下面解锁账号的时候会用到
 

查看节点信息:

> admin.nodeInfo
{
  enode: "enode://9e46c339f5862c129bb0349e568fa9d384e59deb6b947c289347b4aec900145832ef1756f1e880bbe3db1e30658d4a21ba2f23270e4aeec4985a7a624cedbbe8@[::]:30303?discport=0",
  id: "9e46c339f5862c129bb0349e568fa9d384e59deb6b947c289347b4aec900145832ef1756f1e880bbe3db1e30658d4a21ba2f23270e4aeec4985a7a624cedbbe8",
  ip: "::",
  listenAddr: "[::]:30303",
  name: "Geth/v1.8.11-stable-dea1ce05/windows-amd64/go1.10.2",
  ports: {
    discovery: 0,
    listener: 30303
  },
  protocols: {
    eth: {
      config: {
        chainId: 123456,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        homesteadBlock: 0
      },
      difficulty: 131072,
      genesis: "0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04",
      head: "0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04",
      network: 123456
    }
  }
}

查看账户余额:

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

或者

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

查看当前矿工账号(挖矿所得会到这个账号里,默认为第一个账户):

> eth.coinbase
"0xa9ab17e577fba6ddd1c27d1785add6f48f678a8b"

挖矿:

开始挖矿 miner.start(1)

> miner.start(1)
INFO [07-01|15:26:22] Updated mining threads                   threads=1
INFO [07-01|15:26:22] Transaction pool price threshold updated price=18000000000
INFO [07-01|15:26:22] Starting mining operation
INFO [07-01|15:26:22] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=0s
INFO [07-01|15:26:24] Successfully sealed new block            number=1 hash=9ed59f…01cb1c
INFO [07-01|15:26:24] �� mined potential block                  number=1 hash=9ed59f…01cb1c
INFO [07-01|15:26:24] Commit new mining work                   number=2 txs=0 uncles=0 elapsed=0s
INFO [07-01|15:26:28] Successfully sealed new block            number=2 hash=1f7e1c…03cd35

说明:括号里的1的意思就是用1个CPU的内核去挖矿
停止挖矿 miner.stop()
 

修改矿工账号:

miner.setEtherbase(eth.accounts[1])

查看账户信息:

eth.accounts[0]

转账:

eth.sendTransaction({from:eth.accounts[0],to:"0xedf21069693886b60f56a51dae075e7385e4b959",value:web3.toWei(1,"ether")})

或者

eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(1,"ether")})

说明:web3.toWei(1,”ether”)方法为单位转换,就是把1eth转换为多少为
可能会报错:

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 0xa9ab17e577fba6ddd1c27d1785add6f48f678a8b
Passphrase:
true

再执行转账命令:

> eth.sendTransaction({from:eth.accounts[0],to:"0xedf21069693886b60f56a51dae075e7385e4b959",value:web3.toWei(1,"ether")})
INFO [07-01|15:40:18] Submitted transaction                    fullhash=0xf05fc26979159eebab2b6d491672039261f73912e62bf71eaf56cde25ef17b54 recipient=0xEdf21069693886B60f56A51dAE075e7385e4b959
"0xf05fc26979159eebab2b6d491672039261f73912e62bf71eaf56cde25ef17b54"

注:执行完转账命令后这笔交易还并没有成功,需要等待别人挖矿去把这笔交易记录下来
 

查看交易状态:

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

查看两个账户的余额是没有变化的:

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

 
我们再启动挖矿:miner.start(2)
当挖到矿再查看交易状态的时候pending已经为0说明交易成功了,这时候我们就可以停止挖矿了:

> txpool.status
{
  pending: 0,
  queued: 0
}
miner.stop()

我们再查两个账户的余额会有变化:

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

注:余额会比一开始的还要大是因为刚才又执行了挖矿,则挖矿所得又会给这个账户

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

查看区块数据:

> eth.blockNumber
183

通过区块号查看区块:

> eth.getBlock(1)
{
  difficulty: 131072,
  extraData: "0xda8301080b846765746888676f312e31302e328777696e646f7773",
  gasLimit: 4995119,
  gasUsed: 0,
  hash: "0x9ed59f854b3eb22be71c94a8260b7bc643f556db947ae2ce4ba267eaf501cb1c",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xa9ab17e577fba6ddd1c27d1785add6f48f678a8b",
  mixHash: "0xb6114b20b28de686952d2775f2ca534d978b1ec926812505fa671bbd25ba9f69",
  nonce: "0x4745c6e4eb5bc837",
  number: 1,
  parentHash: "0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 538,
  stateRoot: "0x68dadec0e6352f2f1819a462d6b734caa0415c029d764a8904931aadb6919ccf",
  timestamp: 1530429982,
  totalDifficulty: 262144,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

注:我们主要还是在测试链上开发测试,因为私链上一些数据还不是很全,不完整,所以最好还是在测试链上开发,在主链上进行生产部署

猜你喜欢

转载自blog.csdn.net/m0_37739193/article/details/81056921