- 第 1 章 区块链
- 1.2. 什么是智能合约?
- 1.3. 我们应该怎么做?
- 1.4. 如何学习区块链
- 1.5. 币圈与链圈
- 1.6. 区块链能做什么
- 1.7. 区块链不能解决的问题
- 1.8. 理解去中心化
- 1.9. 理解不可撰改
- 1.10. 理解分布式记账
- 1.11. 安全问题
- 1.12. 区块链落地面临的问题
- 1.13. 区块链未来
- 1.14. 区块链的六层模型
- 1.15. 共识机制
- 1.16. SHA-256
- 1.17. Base58编码
- 1.18. Merkle
- 1.19. BIP39协议:使用助记词生成确定性钱包
- 1.20. Ethereum vs Hypterledger Fabic vs EOS 对比
- 1.21. 区块链探索
- 第 2 章 以太坊
- 第 3 章 以太坊私链入门
- 第 4 章 以太坊网络
- 第 5 章 geth v1.8.16 命令详解
- 第 6 章 Wallet
- 第 7 章 Token
- 第 8 章 智能合约语言 Solidity v0.5.0
- 8.2. solc 命令
- 8.3. 智能合约入门演示
- 8.4. Helloworld Example
- 8.5. 数据类型
- 8.6. 单位
- 8.7. 变量
- 8.8. 函数
- 8.9. 事件
- 8.10. 面向对象编程
- 8.11. 合约调用
- 8.12. 合约接收 ETH
- 8.13. 合约中实例化一个接口
- 8.14. 合约中实例化另一个合约
- 8.15. Solidity 安全问题
- 8.16. solidity example
- 8.17. Zeppelin Solidity - OpenZeppelin is a library for writing secure Smart Contracts on Ethereum.
- 第 9 章 Truffle v4.1.8 开发框架
- 第 10 章 web3.js - 1.0.0
- 第 11 章 web3j v3.4.0 - Jave Client
- 11.2. 启动以太坊
- 11.3. Maven pom.xml 文件
- 11.4. Java 与 Solidity 数据类型映射关系
- 11.5. 常量
- 11.6. 连接到服务器获取版本号
- 11.7. 获得以太坊状态信息
- 11.8. 单位转换
- 11.9. 账号管理
- 11.10. Credentials
- 11.11. 交易
- 11.12. 钱包
- 11.13. 智能合约
- 11.14. ERC20合约
- 11.15. Infura
- 11.16. 助记词
- 11.17. 过滤器 (Filter)
- 11.18. Subscription
- 11.19. 解锁账号
- 11.20. IBAN (International Bank Account Number)
- 11.21. Springboot with Ethereum (web3j)
- 第 12 章 web3.py - A python interface for interacting with the Ethereum blockchain and ecosystem.
- 第 14 章 Ethereum Developer APIs
- 第 15 章 infura
- 第 16 章 以太坊案例
- 第 17 章 FAQ
- 17.3. Error: authentication needed: password or unlock
- 17.4. 新增节点后不生效
- 17.5. Unhandled rejection Error: Returned error: The method personal_unlockAccount does not exist/is not available
- 17.6. Error: exceeds block gas limit
- 17.7. Migrations.sol:11:3: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(…) { … }" instead.
- 17.8. Exception in thread "main" rx.exceptions.OnErrorNotImplementedException: Invalid response received: okhttp3.internal.http.RealResponseBody@6c25e6c4
- 17.9. 旧版本 Remix(browser-solidity) 本地安装
- 第 18 章 Hyperledger Fabric v2.0.0
- 第 19 章 Hyperledger Fabric 运维
- 第 20 章 Chaincode 链码(智能合约)
- 第 21 章 Hyperledger Fabric Client SDK for Node.js
- 第 22 章 fabric-sdk-java
- 第 24 章 已知 Hyperledger 落地案例
- 第 25 章 Fabric Command
- 第 26 章 Fabric FAQ
- 第 27 章 IPFS(InterPlanetary File System,星际文件系统)
- 第 28 章 IPFS 命令
- 第 29 章 IPFS WebUI
- 第 30 章 IPFS 集群配置
- 第 31 章 IPFS API
- 第 32 章 IPFS Faq
- 第 33 章 EOS
- 第 34 章 EOS 安装
- 第 35 章 CLEOS
- 第 36 章 智能合约开发
- 第 37 章 EOS Dapp 开发
- 第 38 章 FAQ
- 第 39 章 BaaS (Blockchain as a Service) 平台
- 第 40 章 BitCoin
- 第 41 章 其他区块链相关
- 附录 1. 附录
3.2. 创世区块
3.2. 创世区块
cd ~ mkdir -p ethereum cd ethereum
3.2.1. 初始化创世区块
创建文件 genesis.json
{ "nonce": "0x0000000000000042", "difficulty": "0x020000", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x0000000000000000000000000000000000000000", "timestamp": "0x00", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa", "gasLimit": "0x4c4b40", "config": { "chainId": 15, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "alloc": { } }
mixhash: 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。.
nonce: nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。
difficulty: 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
alloc: 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以。
coinbase: 矿工的账号,随便填
timestamp: 设置创世块的时间戳
parentHash: 上一个区块的hash值,因为是创世块,所以这个值是0
extraData: 附加信息,随便填,可以填你的个性信息
gasLimit: 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大。
初始化创世区块
neo@netkiller ~/ethereum % geth init genesis.json WARN [01-19|17:35:17] No etherbase set and no accounts found as default INFO [01-19|17:35:17] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=16 handles=16 INFO [01-19|17:35:17] Writing custom genesis block INFO [01-19|17:35:17] Successfully wrote genesis state database=chaindata hash=611596…424d04 INFO [01-19|17:35:17] Allocated cache and file handles database=/home/neo/.ethereum/geth/lightchaindata cache=16 handles=16 INFO [01-19|17:35:18] Writing custom genesis block INFO [01-19|17:35:18] Successfully wrote genesis state database=lightchaindata hash=611596…424d04
默认目录是 /home/neo/.ethereum/ 你可以通过 --datadir 参数指定目录
neo@netkiller ~/ethereum % geth --datadir data init genesis.json WARN [01-19|17:38:16] No etherbase set and no accounts found as default INFO [01-19|17:38:16] Allocated cache and file handles database=/home/neo/ethereum/data/geth/chaindata cache=16 handles=16 INFO [01-19|17:38:17] Writing custom genesis block INFO [01-19|17:38:17] Successfully wrote genesis state database=chaindata hash=611596…424d04 INFO [01-19|17:38:17] Allocated cache and file handles database=/home/neo/ethereum/data/geth/lightchaindata cache=16 handles=16 INFO [01-19|17:38:17] Writing custom genesis block INFO [01-19|17:38:17] Successfully wrote genesis state database=lightchaindata hash=611596…424d04 neo@netkiller ~/ethereum % find data data data/keystore data/geth data/geth/chaindata data/geth/chaindata/LOCK data/geth/chaindata/LOG data/geth/chaindata/MANIFEST-000000 data/geth/chaindata/CURRENT data/geth/chaindata/000001.log data/geth/lightchaindata data/geth/lightchaindata/LOCK data/geth/lightchaindata/LOG data/geth/lightchaindata/MANIFEST-000000 data/geth/lightchaindata/CURRENT data/geth/lightchaindata/000001.log
目录结构
data ├── geth │ ├── chaindata │ │ ├── 000001.log │ │ ├── CURRENT │ │ ├── LOCK │ │ ├── LOG │ │ └── MANIFEST-000000 │ └── lightchaindata │ ├── 000001.log │ ├── CURRENT │ ├── LOCK │ ├── LOG │ └── MANIFEST-000000 └── keystore
3.2.2. 创建主账号
neo@netkiller ~/ethereum % geth account new Your new account is locked with a password. Please give a password. Do not forget this password. Passphrase: Repeat passphrase: Address: {39211a30bfe33d4b12fcbd786472c8a552b93389}
提示 | |
---|---|
密码可以直接回车,但是后面 Ethereum Wallet 遇到了麻烦, Ethereum Wallet 要求密码必须是8为数 |
3.2.3. 启动节点
neo@netkiller ~/ethereum % geth --networkid 123456 --rpc --rpccorsdomain "*" --nodiscover console WARN [01-19|17:47:06] No etherbase set and no accounts found as default INFO [01-19|17:47:06] Starting peer-to-peer node instance=Geth/v1.8.10-stable-4bb3c89d/linux-amd64/go1.9.5 INFO [01-19|17:47:06] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=128 handles=1024 INFO [01-19|17:47:06] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}" INFO [01-19|17:47:06] Disk storage enabled for ethash caches dir=/home/neo/.ethereum/geth/ethash count=3 INFO [01-19|17:47:06] Disk storage enabled for ethash DAGs dir=/home/neo/.ethash count=2 INFO [01-19|17:47:06] Initialising Ethereum protocol versions="[63 62]" network=123456 INFO [01-19|17:47:06] Loaded most recent local header number=0 hash=611596…424d04 td=131072 INFO [01-19|17:47:06] Loaded most recent local full block number=0 hash=611596…424d04 td=131072 INFO [01-19|17:47:06] Loaded most recent local fast block number=0 hash=611596…424d04 td=131072 INFO [01-19|17:47:06] Loaded local transaction journal transactions=0 dropped=0 INFO [01-19|17:47:06] Regenerated local transaction journal transactions=0 accounts=0 INFO [01-19|17:47:06] Starting P2P networking INFO [01-19|17:47:06] RLPx listener up self="enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@[::]:30303?discport=0" INFO [01-19|17:47:06] IPC endpoint opened: /home/neo/.ethereum/geth.ipc INFO [01-19|17:47:06] HTTP endpoint opened: http://127.0.0.1:8545 Welcome to the Geth JavaScript console! instance: Geth/v1.8.10-stable-4bb3c89d/linux-amd64/go1.9.5 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 > INFO [01-19|17:47:09] Mapped network port proto=tcp extport=30303 intport=30303 interface="UPNP IGDv1-IP1"
identity: 区块链的标示,随便填写,用于标示目前网络的名字
init: 指定创世块文件的位置,并创建初始块
datadir: 设置当前区块链网络数据存放的位置
port: 网络监听端口
rpc: 启动rpc通信,可以进行智能合约的部署和调试
rpcapi: 设置允许连接的rpc的客户端,一般为db,eth,net,web3
networkid: 设置当前区块链的网络ID,用于区分不同的网络,是一个数字
console: 启动命令行模式,可以在Geth中执行命令
3.2.4. 使用节点进行挖矿
3.2.4.1. 启动矿工开始挖矿
> miner.start(1)
这里的1表示只使用一个线程运行,第一次运行时将开始创建DAG文件,只需等待进度条到100,则将开始挖矿。 实际你看到的挖矿速度很快,这是因为我们已经在初始化创世区块时配置为:"nonce": "0x0000000000000042"。 “0x42”难度能让你在私有测试网链上快速挖以太币。
提示 | |
---|---|
挖矿时必然有矿工账户,而系统默认使用创建的第一个账号。 |
> miner.start(1) INFO [01-19|21:06:43] Updated mining threads threads=1 INFO [01-19|21:06:43] Transaction pool price threshold updated price=18000000000 INFO [01-19|21:06:43] Starting mining operation null > INFO [01-19|21:06:43] Commit new mining work number=1 txs=0 uncles=0 elapsed=717.552µs INFO [01-19|21:06:46] Generating ethash verification cache epoch=0 percentage=91 elapsed=3.000s INFO [01-19|21:06:46] Generated ethash verification cache epoch=0 elapsed=3.273s INFO [01-19|21:06:51] Generating DAG in progress epoch=0 percentage=0 elapsed=5.056s INFO [01-19|21:06:56] Generating DAG in progress epoch=0 percentage=1 elapsed=10.140s INFO [01-19|21:07:01] Generating DAG in progress epoch=0 percentage=2 elapsed=15.119s INFO [01-19|21:07:06] Generating DAG in progress epoch=0 percentage=3 elapsed=19.924s INFO [01-19|21:07:11] Generating DAG in progress epoch=0 percentage=4 elapsed=24.739s INFO [01-19|21:07:16] Generating DAG in progress epoch=0 percentage=5 elapsed=29.473s INFO [01-19|21:07:22] Generating DAG in progress epoch=0 percentage=6 elapsed=35.641s INFO [01-19|21:07:26] Generating DAG in progress epoch=0 percentage=7 elapsed=40.374s INFO [01-19|21:07:31] Generating DAG in progress epoch=0 percentage=8 elapsed=45.134s INFO [01-19|21:07:36] Generating DAG in progress epoch=0 percentage=9 elapsed=49.908s INFO [01-19|21:07:41] Generating DAG in progress epoch=0 percentage=10 elapsed=54.633s ...... ...... ...... INFO [01-19|21:22:43] Generated ethash verification cache epoch=0 elapsed=15m57.328s INFO [01-19|21:22:47] Generating ethash verification cache epoch=1 percentage=17 elapsed=3.031s INFO [01-19|21:22:50] Generating ethash verification cache epoch=1 percentage=34 elapsed=6.056s INFO [01-19|21:22:53] Generating ethash verification cache epoch=1 percentage=49 elapsed=9.562s INFO [01-19|21:22:57] Generating ethash verification cache epoch=1 percentage=70 elapsed=13.115s INFO [01-19|21:23:00] Generating ethash verification cache epoch=1 percentage=90 elapsed=16.123s INFO [01-19|21:23:01] Generated ethash verification cache epoch=1 elapsed=17.576s INFO [01-19|21:23:19] Generating DAG in progress epoch=1 percentage=0 elapsed=18.198s INFO [01-19|21:23:32] Successfully sealed new block number=1 hash=e2b5b9…9b1bfe INFO [01-19|21:23:32] mined potential block number=1 hash=e2b5b9…9b1bfe INFO [01-19|21:23:32] Commit new mining work number=2 txs=0 uncles=0 elapsed=1.188ms INFO [01-19|21:23:37] Generating DAG in progress epoch=1 percentage=1 elapsed=35.913s INFO [01-19|21:23:41] Successfully sealed new block number=2 hash=62db3f…e27b50 INFO [01-19|21:23:41] mined potential block number=2 hash=62db3f…e27b50 INFO [01-19|21:23:41] Commit new mining work number=3 txs=0 uncles=0 elapsed=772.239µs INFO [01-19|21:23:43] Successfully sealed new block number=3 hash=34384b…c387f2 INFO [01-19|21:23:43] mined potential block number=3 hash=34384b…c387f2 INFO [01-19|21:23:43] Commit new mining work number=4 txs=0 uncles=0 elapsed=1.002ms INFO [01-19|21:23:55] Generating DAG in progress epoch=1 percentage=2 elapsed=53.757s INFO [01-19|21:24:13] Generating DAG in progress epoch=1 percentage=3 elapsed=1m11.561s INFO [01-19|21:24:30] Generating DAG in progress epoch=1 percentage=4 elapsed=1m28.986s INFO [01-19|21:24:30] Successfully sealed new block number=4 hash=681970…462135 INFO [01-19|21:24:30] mined potential block number=4 hash=681970…462135 INFO [01-19|21:24:30] Commit new mining work number=5 txs=0 uncles=0 elapsed=833.629µs INFO [01-19|21:24:36] Successfully sealed new block number=5 hash=7b058b…d2f07a INFO [01-19|21:24:36] mined potential block number=5 hash=7b058b…d2f07a INFO [01-19|21:24:36] Commit new mining work number=6 txs=0 uncles=0 elapsed=897.815µs INFO [01-19|21:24:43] Successfully sealed new block number=6 hash=a5fc3d…b1221e INFO [01-19|21:24:43] block reached canonical chain number=1 hash=e2b5b9…9b1bfe INFO [01-19|21:24:43] mined potential block number=6 hash=a5fc3d…b1221e INFO [01-19|21:24:43] Commit new mining work number=7 txs=0 uncles=0 elapsed=758.061µs INFO [01-19|21:24:47] Successfully sealed new block number=7 hash=003b02…e886fd INFO [01-19|21:24:47] block reached canonical chain number=2 hash=62db3f…e27b50 INFO [01-19|21:24:47] mined potential block number=7 hash=003b02…e886fd INFO [01-19|21:24:47] Commit new mining work number=8 txs=0 uncles=0 elapsed=920.862µs INFO [01-19|21:24:48] Generating DAG in progress epoch=1 percentage=5 elapsed=1m46.827s INFO [01-19|21:25:06] Generating DAG in progress epoch=1 percentage=6 elapsed=2m4.338s INFO [01-19|21:25:23] Successfully sealed new block number=8 hash=fd23c9…361c65 INFO [01-19|21:25:23] block reached canonical chain number=3 hash=34384b…c387f2 INFO [01-19|21:25:23] mined potential block number=8 hash=fd23c9…361c65 INFO [01-19|21:25:23] Commit new mining work number=9 txs=0 uncles=0 elapsed=825.737µs INFO [01-19|21:25:23] Generating DAG in progress epoch=1 percentage=7 elapsed=2m22.061s
3.2.4.2. 停止挖矿
> miner.stop() true >
3.2.4.3. 查看所挖金额
> eth.getBalance(eth.accounts[0]) 70000000000000000000
3.2.5. 在创世链中制定矿工账号并为它充值
"alloc": { "0xe8abf98484325fd6afc59b804ac15804b978e607": { "balance": "300000" }, "0x013b5e735e1b48421dd3de3b931d6f03e769e22b": { "balance": "400000" } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论