1. 区块链
目前业界关于区块链尚无权威统一的定义。区块链简单来说,是一种链式的数据结构,区块之间按照时间顺序连接,利用密码学方式保证数据不易串改和不易伪造,并在网络所有节点进行分布式存储的共享账本。
近一年 BTC 价格走势
谈到区块链就不得不提及最近价格经历过山车的比特币(BTC),比特币对社会的贡献和价值主要在于,通过实践验证了背后支撑其运作的区块链技术。了解比特币的基本原理,请移步
一个故事告诉你比特币的原理及运作机制
http://blog.codinglabs.org/articles/bitcoin-mechanism-make-easy.html
目前互联网主要作为“信息”传递的平台,而区块链技术的发展和完善将赋予互联网传递“信任”和“价值”的能力,就目前的环境而言,区块链技术的炒作可能要多于技术本身的落地和创新。区块链技术作为互联网传递“信任”和价值的解决方案,本身也有其相应的缺点和适用场景。由于区块链基于 P2P 网络,要求全网节点保存同一份数据,以效率来换取安全性(执行大量的本质上毫无意义的计算,浪费大量的电力),因此对效率要求很高的场景并不适合区块链技术。
2. 以太坊
以太坊(英语:Ethereum)是一个开源的有智能合约功能的公共区块链平台。通过其专用加密货币以太币( Ether ,又称“以太币”)提供去中心化的虚拟机(称为“以太虚拟机” Ethereum Virtual Machine )来处理点对点合约。
简而言之,以太坊就是一个建立在区块链技术之上的支持去中心化应用开发的基础平台。以太坊与去中心化 App 之间的关系相当于操作系统与运行在该操作系统之上 App 之间的关系。
在以太坊出现之前,区块链应用是这样玩的:拷贝一份比特币代码,然后去改底层代码如加密算法,共识机制,网络协议等等(很多山寨币就是这样诞生的,改改就出来一个新币,然后去 ICO 割韭菜,俗称“空气币”)。以太坊平台对底层区块链技术进行了封装,让区块链应用开发者可以直接基于以太坊平台进行开发,开发者只要专注于应用本身的开发,从而大大降低了难度。
以太坊创始人
下面扒一扒以太坊的创始人。以太坊最初由 Vitalik Buterin 在 2013 年提出,Vitalik Buterin 何许人也,业内称之为 V 神,1994 年生于俄罗斯,黑客背景。 - 17 岁开始研究比特币,创《比特币杂志》; - 19 岁自加拿大滑铁卢大学休学,并在该年 11 月,公布《以太坊白皮书》初版,开始募集开发者; - 20 岁获得提尔奖学金、成立非营利组织以太坊基金会,在迈阿密的比特币会议公开发表以太坊计画,该年 7 月,启动以太坊计画众售募资,募得 3.1 万枚比特币(当时约合 1840 万美元); - 21 岁以太坊最初版本 Frontier 问世、以太币开始在世界各地交易所公开交易; - 22 岁被《财星》杂志评选为 2016 年 40 岁以下的 40 大杰出人物。
3. 搭建私有链
安装以太坊客户端
下载以太坊客户端移步
https://github.com/ethereum/mist/releases
下载对应操作系统的以太坊钱包和 Mist(去中心化应用浏览器)。
以太坊钱包启动后会自动下载以太坊命令行客户端 Geth ,Geth 目前使用非常广泛,基于 GO 语言开发。安装后的路径:
C:UsershaohaoAppDataRoamingEthereumWalletbinariesGethunpacked
将该路径配置为环境变量(注意英文分号)。可以通过 geth --help 查看 Geth 是否安装成功。目前业界关于区块链尚无权威统一的定义。区块链简单来说,是一种链式的数据结构,区块之间按照时间顺序连接,利用密码学方式保证数据不易串改和不易伪造,并在网络所有节点进行分布式存储的共享账本。
定义创世区块
区块链的第一个区块称为创世区块,它是区块链里面所有区块的共同祖先,这意味着你从任一区块,循链向后回溯,最终都将到达创世区块。以太坊支持自定义创世区块,要运行私有链,首先需要定义自己的创世区块,一般用一个 JSON 文件表示创世区块的信息。
在 C 盘创建目录 C:MyChainsT1 ,该目录主要用于存储私有链数据,在该目录下创建 MyGenesis.json 文件,文件内容:
代码语言:javascript复制{
"config": {
"chainId": 2048,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x10",
"extraData" : "",
"gasLimit" : "0x222222",
"nonce" : "0x000000000000001",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
初始化区块链
命令行在 C:MyChainsT1 路径下,关闭以太坊钱包和 Mist ,初始化区块链执行命令:
代码语言:javascript复制geth --identity "dev" --rpc --rpccorsdomain "*" --datadir "C:MyChainsT1" --rpcapi "db,eth,net,web3" --networkid 100 init "./MyGenesis.json"
执行成功后显示:
启动私有链
命令行在 C:MyChainsT1 路径下,关闭以太坊钱包和 Mist ,启动私有链执行命令:
代码语言:javascript复制geth --datadir "C:MyChainsT1" --identity "dev" --rpccorsdomain "*" --networkid 100 console
执行成功后显示:
创建以太坊账户
私有链启动成功后,便会进入以太坊 Geth Console 控制台,以 `>` 符号开始。查看私有链上的账户(目前还没有创建任何账户)。
代码语言:javascript复制> eth.accounts
[]
通过 personal.newAccount() 指令(输入两次密码)创建 2 个新账户。
查看账户余额(目前两个账户余额均为 0 ):
代码语言:javascript复制> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0
要使账户有余额,可以通过转账或者挖矿来获取以太币(ETH)。
挖矿
以太坊通过命令 miner.start([线程数]) 来启动挖矿,这里启动 5 个线程进行挖矿:
当 percentage=100 后,通过命令 miner.stop() 终止挖矿。停止挖矿后,查看账户余额:
PS: getBalance() 返回值的单位是 wei , wei 是以太币的最小单位,1 个以太币 = 10 的 18 次方个 wei 。查看账户有多少个以太币,可以通过命令 web3.fromWei()
转账
目前我们有两个账户,账户 0 有 105 个以太币, 账户 1 有 0 个以太币。现在通过操作账户 0 向账户 1 转 10 个以太币。
首先通过账户 0 的密码对账户 0 进行解锁:
代码语言:javascript复制> personal.unlockAccount(eth.accounts[0])
Unlock account 0x33213084015dab454fee16eb369fa2a8e6e65eee
Passphrase:
true
操作账户 0 向账户 1 转 10 个以太币:
代码语言:javascript复制> count = web3.toWei(10,'ether')
"10000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:count})
INFO [10-02|21:28:11.870] Setting new local account address=0x33213084015DAB454fEe16eb369FA2a8E6E65eEE
INFO [10-02|21:28:11.885] Submitted transaction fullhash=0xa7adb20cd2665d888b64bbccbf29ad5a5a1feb461ffb940
"0xa7adb20cd2665d888b64bbccbf29ad5a5a1feb461ffb9407447c8dc608886a7e"
交易发送后,查看账户 1 的余额,发现还是 0 。
代码语言:javascript复制> eth.getBalance(eth.accounts[1])
0
> txpool.status
{
pending: 1,
queued: 0
}
账户 0 转给账户 1 的以太币还未到账,通过 txpool.status 命令查看,有一笔交易未处理,pending 字段表示交易已提交但还未被处理的交易。为什么交易未被处理呢?因为当前链上没有矿工在挖矿,即该区块链没有算力来处理交易。
通过命令
miner.start(1);admin.sleepBlocks(1);miner.stop();
启动挖矿,并在挖到一个区块后停止挖矿。
代码语言:javascript复制> miner.start(1);admin.sleepBlocks(1);miner.stop();
INFO [10-02|21:29:01.378] Updated mining threads threads=1
INFO [10-02|21:29:01.390] Transaction pool price threshold updated price=1000000000
INFO [10-02|21:29:01.396] Commit new mining work number=22 sealhash=ee23d4…78362e uncles=0 txs=0
INFO [10-02|21:29:01.434] Commit new mining work number=22 sealhash=3ca4dd…c456e1 uncles=0 txs=1
INFO [10-02|21:29:04.772] Generating DAG in progress epoch=1 percentage=0 elapsed=2.123s
INFO [10-02|21:29:06.896] Generating DAG in progress epoch=1 percentage=1 elapsed=4.246s
INFO [10-02|21:29:08.924] Generating DAG in progress epoch=1 percentage=2 elapsed=6.274s
INFO [10-02|21:29:10.851] Generating DAG in progress epoch=1 percentage=3 elapsed=8.201s
INFO [10-02|21:29:12.717] Successfully sealed new block number=22 sealhash=3ca4dd…c456e1 hash=0d293a…f
INFO [10-02|21:29:12.881] ? mined potential block number=22 hash=0d293a…fa74a1
INFO [10-02|21:29:12.892] Commit new mining work number=23 sealhash=e1b40b…1b3631 uncles=0 txs=0
INFO [10-02|21:29:13.023] Generating DAG in progress epoch=1 percentage=4 elapsed=10.373s
null
> INFO [10-02|21:29:14.682] Generating DAG in progress epoch=1 percentage=5 elapsed=12.032s
INFO [10-02|21:29:16.528] Generating DAG in progress epoch=1 percentage=6 elapsed=13.878s
INFO [10-02|21:29:18.519] Generating DAG in progress epoch=1 percentage=7 elapsed=15.869s
INFO [10-02|21:29:20.207] Generating DAG in progress epoch=1 percentage=8 elapsed=17.558s
INFO [10-02|21:29:22.042] Generating DAG in progress epoch=1 percentage=9 elapsed=19.393s
INFO [10-02|21:29:23.899] Generating DAG in progress epoch=1 percentage=10 elapsed=21.249s
INFO [10-02|21:29:25.817] Generating DAG in progress epoch=1 percentage=11 elapsed=23.167s
INFO [10-02|21:29:27.605] Generating DAG in progress epoch=1 percentage=12 elapsed=24.955s
INFO [10-02|21:29:29.529] Generating DAG in progress epoch=1 percentage=13 elapsed=26.879s
INFO [10-02|21:29:31.255] Generating DAG in progress epoch=1 percentage=14 elapsed=28.605s
INFO [10-02|21:29:33.227] Generating DAG in progress epoch=1 percentage=15 elapsed=30.577s
INFO [10-02|21:29:35.041] Generating DAG in progress epoch=1 percentage=16 elapsed=32.391s
INFO [10-02|21:29:36.542] Generating DAG in progress epoch=1 percentage=17 elapsed=33.892s
INFO [10-02|21:29:38.093] Generating DAG in progress epoch=1 percentage=18 elapsed=35.444s
INFO [10-02|21:29:39.928] Generating DAG in progress epoch=1 percentage=19 elapsed=37.279s
INFO [10-02|21:29:41.835] Generating DAG in progress epoch=1 percentage=20 elapsed=39.185s
INFO [10-02|21:29:43.817] Generating DAG in progress epoch=1 percentage=21 elapsed=41.167s
INFO [10-02|21:29:45.506] Generating DAG in progress epoch=1 percentage=22 elapsed=42.856s
INFO [10-02|21:29:47.383] Generating DAG in progress epoch=1 percentage=23 elapsed=44.733s
INFO [10-02|21:29:49.006] Generating DAG in progress epoch=1 percentage=24 elapsed=46.356s
INFO [10-02|21:29:51.061] Generating DAG in progress epoch=1 percentage=25 elapsed=48.411s
INFO [10-02|21:29:52.928] Generating DAG in progress epoch=1 percentage=26 elapsed=50.278s
INFO [10-02|21:29:54.690] Generating DAG in progress epoch=1 percentage=27 elapsed=52.040s
INFO [10-02|21:29:56.612] Generating DAG in progress epoch=1 percentage=28 elapsed=53.963s
INFO [10-02|21:29:58.547] Generating DAG in progress epoch=1 percentage=29 elapsed=55.897s
INFO [10-02|21:30:00.662] Generating DAG in progress epoch=1 percentage=30 elapsed=58.012s
INFO [10-02|21:30:02.853] Generating DAG in progress epoch=1 percentage=31 elapsed=1m0.203s
INFO [10-02|21:30:04.967] Generating DAG in progress epoch=1 percentage=32 elapsed=1m2.317s
INFO [10-02|21:30:07.100] Generating DAG in progress epoch=1 percentage=33 elapsed=1m4.450s
INFO [10-02|21:30:09.305] Generating DAG in progress epoch=1 percentage=34 elapsed=1m6.655s
INFO [10-02|21:30:12.027] Generating DAG in progress epoch=1 percentage=35 elapsed=1m9.377s
INFO [10-02|21:30:15.647] Generating DAG in progress epoch=1 percentage=36 elapsed=1m12.997s
INFO [10-02|21:30:18.841] Generating DAG in progress epoch=1 percentage=37 elapsed=1m16.191s
INFO [10-02|21:30:21.048] Generating DAG in progress epoch=1 percentage=38 elapsed=1m18.398s
INFO [10-02|21:30:22.907] Generating DAG in progress epoch=1 percentage=39 elapsed=1m20.257s
INFO [10-02|21:30:25.220] Generating DAG in progress epoch=1 percentage=40 elapsed=1m22.570s
ERROR[10-02|21:30:25.367] The pipe is being closed.
INFO [10-02|21:30:27.847] Generating DAG in progress epoch=1 percentage=41 elapsed=1m25.197s
INFO [10-02|21:30:30.042] Generating DAG in progress epoch=1 percentage=42 elapsed=1m27.392s
INFO [10-02|21:30:32.096] Generating DAG in progress epoch=1 percentage=43 elapsed=1m29.447s
INFO [10-02|21:30:34.722] Generating DAG in progress epoch=1 percentage=44 elapsed=1m32.072s
INFO [10-02|21:30:36.573] Generating DAG in progress epoch=1 percentage=45 elapsed=1m33.923s
INFO [10-02|21:30:38.233] Generating DAG in progress epoch=1 percentage=46 elapsed=1m35.583s
INFO [10-02|21:30:40.176] Generating DAG in progress epoch=1 percentage=47 elapsed=1m37.526s
INFO [10-02|21:30:41.973] Generating DAG in progress epoch=1 percentage=48 elapsed=1m39.323s
INFO [10-02|21:30:43.541] Generating DAG in progress epoch=1 percentage=49 elapsed=1m40.891s
ERROR[10-02|21:30:45.251] The pipe is being closed.
INFO [10-02|21:30:45.603] Generating DAG in progress epoch=1 percentage=50 elapsed=1m42.953s
INFO [10-02|21:30:47.923] Generating DAG in progress epoch=1 percentage=51 elapsed=1m45.274s
INFO [10-02|21:30:49.805] Generating DAG in progress epoch=1 percentage=52 elapsed=1m47.156s
INFO [10-02|21:30:51.919] Generating DAG in progress epoch=1 percentage=53 elapsed=1m49.269s
INFO [10-02|21:30:53.608] Generating DAG in progress epoch=1 percentage=54 elapsed=1m50.958s
INFO [10-02|21:30:55.495] Generating DAG in progress epoch=1 percentage=55 elapsed=1m52.845s
INFO [10-02|21:30:57.525] Generating DAG in progress epoch=1 percentage=56 elapsed=1m54.875s
INFO [10-02|21:30:59.282] Generating DAG in progress epoch=1 percentage=57 elapsed=1m56.632s
INFO [10-02|21:31:01.103] Generating DAG in progress epoch=1 percentage=58 elapsed=1m58.453s
INFO [10-02|21:31:02.786] Generating DAG in progress epoch=1 percentage=59 elapsed=2m0.136s
INFO [10-02|21:31:04.432] Generating DAG in progress epoch=1 percentage=60 elapsed=2m1.782s
INFO [10-02|21:31:06.310] Generating DAG in progress epoch=1 percentage=61 elapsed=2m3.661s
INFO [10-02|21:31:08.257] Generating DAG in progress epoch=1 percentage=62 elapsed=2m5.607s
INFO [10-02|21:31:10.020] Generating DAG in progress epoch=1 percentage=63 elapsed=2m7.370s
INFO [10-02|21:31:11.977] Generating DAG in progress epoch=1 percentage=64 elapsed=2m9.327s
INFO [10-02|21:31:13.658] Generating DAG in progress epoch=1 percentage=65 elapsed=2m11.008s
INFO [10-02|21:31:15.305] Generating DAG in progress epoch=1 percentage=66 elapsed=2m12.655s
INFO [10-02|21:31:17.153] Generating DAG in progress epoch=1 percentage=67 elapsed=2m14.503s
INFO [10-02|21:31:19.010] Generating DAG in progress epoch=1 percentage=68 elapsed=2m16.360s
INFO [10-02|21:31:20.714] Generating DAG in progress epoch=1 percentage=69 elapsed=2m18.064s
INFO [10-02|21:31:22.771] Generating DAG in progress epoch=1 percentage=70 elapsed=2m20.122s
INFO [10-02|21:31:24.663] Generating DAG in progress epoch=1 percentage=71 elapsed=2m22.014s
INFO [10-02|21:31:26.595] Generating DAG in progress epoch=1 percentage=72 elapsed=2m23.945s
INFO [10-02|21:31:28.383] Generating DAG in progress epoch=1 percentage=73 elapsed=2m25.733s
INFO [10-02|21:31:30.136] Generating DAG in progress epoch=1 percentage=74 elapsed=2m27.486s
INFO [10-02|21:31:31.953] Generating DAG in progress epoch=1 percentage=75 elapsed=2m29.303s
INFO [10-02|21:31:33.914] Generating DAG in progress epoch=1 percentage=76 elapsed=2m31.264s
INFO [10-02|21:31:35.726] Generating DAG in progress epoch=1 percentage=77 elapsed=2m33.076s
INFO [10-02|21:31:37.357] Generating DAG in progress epoch=1 percentage=78 elapsed=2m34.707s
INFO [10-02|21:31:39.145] Generating DAG in progress epoch=1 percentage=79 elapsed=2m36.495s
INFO [10-02|21:31:40.857] Generating DAG in progress epoch=1 percentage=80 elapsed=2m38.208s
INFO [10-02|21:31:42.481] Generating DAG in progress epoch=1 percentage=81 elapsed=2m39.832s
INFO [10-02|21:31:44.210] Generating DAG in progress epoch=1 percentage=82 elapsed=2m41.560s
INFO [10-02|21:31:46.065] Generating DAG in progress epoch=1 percentage=83 elapsed=2m43.415s
INFO [10-02|21:31:48.009] Generating DAG in progress epoch=1 percentage=84 elapsed=2m45.359s
INFO [10-02|21:31:49.535] Generating DAG in progress epoch=1 percentage=85 elapsed=2m46.885s
INFO [10-02|21:31:51.370] Generating DAG in progress epoch=1 percentage=86 elapsed=2m48.720s
INFO [10-02|21:31:53.217] Generating DAG in progress epoch=1 percentage=87 elapsed=2m50.567s
INFO [10-02|21:31:55.001] Generating DAG in progress epoch=1 percentage=88 elapsed=2m52.351s
INFO [10-02|21:31:56.863] Generating DAG in progress epoch=1 percentage=89 elapsed=2m54.213s
INFO [10-02|21:31:58.758] Generating DAG in progress epoch=1 percentage=90 elapsed=2m56.109s
INFO [10-02|21:32:01.015] Generating DAG in progress epoch=1 percentage=91 elapsed=2m58.365s
INFO [10-02|21:32:02.656] Generating DAG in progress epoch=1 percentage=92 elapsed=3m0.006s
INFO [10-02|21:32:04.260] Generating DAG in progress epoch=1 percentage=93 elapsed=3m1.610s
INFO [10-02|21:32:06.299] Generating DAG in progress epoch=1 percentage=94 elapsed=3m3.649s
INFO [10-02|21:32:08.470] Generating DAG in progress epoch=1 percentage=95 elapsed=3m5.820s
INFO [10-02|21:32:10.052] Generating DAG in progress epoch=1 percentage=96 elapsed=3m7.402s
INFO [10-02|21:32:12.307] Generating DAG in progress epoch=1 percentage=97 elapsed=3m9.657s
INFO [10-02|21:32:15.145] Generating DAG in progress epoch=1 percentage=98 elapsed=3m12.496s
INFO [10-02|21:32:19.571] Generating DAG in progress epoch=1 percentage=99 elapsed=3m16.921s
INFO [10-02|21:32:19.586] Generated ethash verification cache epoch=1 elapsed=3m16.936s
在查看未处理的交易数及两个账户的余额:
代码语言:javascript复制> stxpool.status
{
pending: 0,
queued: 0
}
>
> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')
10
> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
95
可以看到未处理交易数为 0 ,账户 0 余额为 10 个以太币。也可以打开以太坊钱包或 Mist 查看账户及余额。