区块链(二)-私有链的搭建

来源:互联网 发布:python 爬取金融数据 编辑:程序博客网 时间:2024/05/16 12:22

私有链


搭建私有链,首先需要写一个创世块文件,创世块就是我自己链上的第一个区块。

{ "config":{}, "nonce":"0x0000000000000042", "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000", "difficulty": "0x100", "alloc": {}, "coinbase":"0x0000000000000000000000000000000000000000", "timestamp": "0x00", "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x00", "gasLimit":"0xfffffff"}

  1. config:这个参数很重要,如果少了这个参数,挖矿就会一直不成功
  2. nonce: 是一个64位随机数,用于挖矿
  3. mixhash: 与nonce配合用于挖矿,由上一个区块的一部分生成的hash
  4. difficulty: 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
  5. alloc: 用来预置账号以及账号的以太币数量
  6. coinbase: 矿工的账号,默认的初始值为创建的第一个账户
  7. timestamp:设置创世块的时间戳,可计算当前实时时间,以后查询交易时间更方便
  8. parentHash:上一个区块的hash值,因为是创世块,所以这个值是0
  9. extraData:附加信息
  10. gasLimit:该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,以后为了更复杂的交易,我们将其设置为最大值

初始化
(1)在终端输入这两条命令,创建文件夹myPrivate,并进入该文件夹

mkdir myPrivatecd myPrivate

(2)将刚刚的创世块文件genesis.json,放在文件夹下,接着执行命令进行初始化

geth init genesis.json

启动私有链
执行如下命令:

geth  --rpc --rpccorsdomain "*" --rpcapi "personal,web3,eth"  console

这是我总结的最重要的geth启动项,在区块链的前端开发中每一个都不可缺少。

$ geth  --rpc --rpccorsdomain "*" --rpcapi "personal,web3,eth"  --rpcaddr "10.18.25.15" consoleINFO [06-13|10:45:13] instance:Geth/v1.6.0-unstable-105b37f1/darwin/go1.8INFO [06-13|10:45:13] Allocated cache and file handles         database=/Users/xue/Library/Ethereum/geth/chaindata cache=128 handles=1024INFO [06-13|10:45:13] Initialised chain configuration          config="{ChainID: 0 Homestead: <nil> DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: <nil> EIP158: <nil>}"INFO [06-13|10:45:13] Disk storage enabled for ethash caches   dir=/Users/xue/Library/Ethereum/geth/ethash count=3INFO [06-13|10:45:13] Disk storage enabled for ethash DAGs     dir=/Users/xue/.ethash                      count=2INFO [06-13|10:45:13] Initialising Ethereum protocol           versions="[63 62]" network=1INFO [06-13|10:45:13] Loaded most recent local header          number=288 hash=9a47c9…e52fea td=38712958INFO [06-13|10:45:13] Loaded most recent local full block      number=288 hash=9a47c9…e52fea td=38712958INFO [06-13|10:45:13] Loaded most recent local fast block      number=288 hash=9a47c9…e52fea td=38712958INFO [06-13|10:45:13] Starting P2P networkingINFO [06-13|10:45:15] RLPx listener up                         self=enode://c0aa55a66963bc5321dd6633d029e3259458e68a2dea591baee5b0bb96e1194ec7eea9bfd5fc2eaee518bfc58ed2c475a4574e9dae7a569625c6c88d89140ec7@[::]:30303INFO [06-13|10:45:15] IPC endpoint opened: /Users/xue/Library/Ethereum/geth.ipcINFO [06-13|10:45:15] HTTP endpoint opened: http://10.18.25.15:8545Welcome to the Geth JavaScript console!instance: Geth/v1.6.0-unstable-105b37f1/darwin/go1.8coinbase: 0xaae168cfde121cbbb2ceb3c7157f81a7dae82964at block: 288 (Mon, 12 Jun 2017 12:37:45 CST) datadir: /Users/xue/Library/Ethereum 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>
阅读全文
3 0
原创粉丝点击