手把手教你用ETHEREUM构建自己的虚拟货币
区块链(Blockchain)作为比特币的底层技术受到了越来越多的关注,其去中心化的分布式数据股票储结构有着广阔的应用场景。ITgo一直关注区块链技术,那么本次关于区块链的话题就让我们一起来用Ethereum动手实践一下虚拟货币的账号管理、挖矿、转账等操作。
単位 | ether |
---|---|
wei(最小) | 0.000000000000000001ether |
kwei | 0.000000000000001ether |
mwei | 0.000000000001ether |
gwei(shannon) | 0.000000001ether |
szabo | 0.000001ether |
finney | 0.001ether |
ether | 1ether |
kether | 1000ether |
mether | 1000000ether |
gether | 1000000000ether |
tether | 1000000000000ether |
本文以读者了解虚拟货币运作的基本概念为前提,比如“挖矿”等术语在此不做解释。
转账时需要支付给矿工的手续费叫做“gas”。在Ethereum平台上运行程序需要燃料故名为“gas”。支付gas时根据“gas limit”和“gas price”计算费用。比如:转账时gas limit需要2100gas,1gas按照20gwei计算,总共需要花费2100 × 20gwe = 42,000 gwei。
Ethereum客户端有几种,我们在这里使用Geth。Geth是“Go Ethereum”的略称,是Go语言编写的Ethereum节点操作的命令行客户端。
01. 初始化
(1)Geth安装
读者使用的PC环境五花八门,我们主要讲Mac和Windows环境的安装和执行。
Mac系统可以使用Homebrew进行简单安装。
1$ brew tap ethereum/ethereum
2$ brew install ethereum
Windows从下面的网址下载后点击安装。
https://geth.ethereum.org/downloads/
(2)初始设定文件genesis.json做成。
因为是在本地PC执行,并不是连接到真正的Ethereum网络,所以开始还不存在任何区块,需要建立第一个区块。
1mkdir mycoin
在mycoin目录下做一个配置文件,文件名:genesis.json,内容如下。
1{
2 "config": {
3 "chainId": 30,
4 "homesteadBlock": 0,
5 "eip155Block": 0,
6 "eip158Block": 0
7 },
8 "nonce": "0x0000000000000042",
9 "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
10 "difficulty": "0x400",
11 "alloc": {},
12 "coinbase": "0x3333333333333333333333333333333333333333",
13 "timestamp": "0x0",
14 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
15 "extraData": "0x",
16 "gasLimit": "0x8000000"
17}
(3)初始区块生成
做好配置文件后,初始化区块链,我们自己的虚拟货币就可以开始营业了。
1geth --datadir mycoin/ init mycoin/genesis.json
02. 账号管理
(1)进入控制台
1geth --networkid "30" --nodiscover --datadir "mycoin" console 2>> mycoin/err.log
networkid:可以指定为0~3以外的任何整数。
nodiscover:不探索其他节点,因为是本地网络不需要寻找其他节点,所以使用了该参数。
console: 启动geth控制台参数。
(2)查看第一个区块
确认Genesis区块和配置文件genesis.json的内容是否一致。
1> eth.getBlock(0)
2
3difficulty: 1024,
4 extraData: "0x",
5 gasLimit: 134217728,
6 gasUsed: 0,
7 hash: "0x6231b02ac967dff9b0c799e956094408959de862d720d08776302ffefba0300b",
8 logsBloom: "0x0000000000000000000000000000000000000000000000000000000000000...
9 ...
10
(3)做成账户
查看是否有账户存在。
1> eth.accounts
2[] <--账户为空
可以用personal.newAccount(“password”)命令做成账号。这里我们做成两个账户,一个用于付款,一个用于收款。
注意:括号中的参数是密码,不是用户名。因为虚拟货币具有匿名性,账号不用用户名表示而是一长串的“地址”。
1# 付款账户
2> personal.newAccount("password1")
3"0xe9c73048df2f545f7b76749a510f58421dd32a9c"
4
5# 收款账户
6> personal.newAccount("password2")
7"0x096a92fe38f83717754a6955127c7232f00a5e9f"
8
9# 账户确认(是否存在两个账户)
10> eth.accounts
11["0xe9c73048df2f545f7b76749a510f58421dd32a9c", "0x096a92fe38f83717754a6955127c7232f00a5e9f"]
03. 余额确认及挖矿
coinbase是挖矿时取得报酬的“地址”。初始状态coinbase指向第一个账户的地址。
1# 确认挖矿使用的地址
2> eth.coinbase
3"0xe9c73048df2f545f7b76749a510f58421dd32a9c"
4
5# 确认付款账户的余额
6> eth.getBalance(eth.accounts[0])
70
挖矿开始和结束命令如下。eth.hashrate是每秒计算哈希值的次数,这个值大于0的话表示挖矿进行中。
1# 挖矿开始
2> miner.start()
3null
4
5# 查看Hash Rate
6> miner.getHashrate()
716695
8
9# 停止挖矿
10> miner.stop()
11null
04. 转账
(1)账户解锁
1> personal.unlockAccount("0xe9c73048df2f545f7b76749a510f58421dd32a9c", "password1")
2true
3> personal.unlockAccount("0x096a92fe38f83717754a6955127c7232f00a5e9f", "password2")
4true
(2)余额确认
1> eth.getBalance(eth.accounts[0])
28.85e+21
3> eth.getBalance(eth.accounts[1])
40
(3)转账
1> eth.sendTransaction({from: "0xe9c73048df2f545f7b76749a510f58421dd32a9c", to: "0x096a92fe38f83717754a6955127c7232f00a5e9f", value: web3.toWei(1, "ether")})
2"0x5017b80c25688fcc961a252c4259a1533ca8c996642a44e9a3fbcafdecde0bf0"
(4)查看等待的交易
gas:表示处理交易的计算量
gasPrice:处理交易时1gas的手续费,默认单位是wei。
1> eth.pendingTransactions
2[{
3 blockHash: null,
4 blockNumber: null,
5 from: "0xe9c73048df2f545f7b76749a510f58421dd32a9c",
6 gas: 90000,
7 gasPrice: 18000000000,
8 hash: "0x5017b80c25688fcc961a252c4259a1533ca8c996642a44e9a3fbcafdecde0bf0",
9 input: "0x",
10 nonce: 0,
11 r: "0x4ddf23d16c78915ccf32524113c58b278ecf0a840ee7b6f25dafed73f27af058",
12 s: "0x40fb5bba1610055b7aadbaddd39e55431a27a4a6d7aa466a3bcf93af4abe888a",
13 to: "0x096a92fe38f83717754a6955127c7232f00a5e9f",
14 transactionIndex: 0,
15 v: "0x60",
16 value: 1000000000000000000
17}]
(5)挖矿
1# 开始挖矿
2> miner.start()
3null
4
5# 确认挖矿状态(Hash Rate不为0的情况表示矿机正在工作)
6> eth.hashrate
76945
8
9# 停止挖矿
10> miner.stop()
11null
(6)查看余额
1> eth.getBalance(eth.accounts[0])
28.879e+21
3> eth.getBalance(eth.accounts[1])
41000000000000000000
5
6# 用其它单位查看
7> web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
8> web3.fromWei(eth.getBalance(eth.accounts[1]),"finney")
9> web3.fromWei(eth.getBalance(eth.accounts[1]),"szabo")
交易成功至此我们完成了虚拟货币的一整套虚拟货币的交易