12.以太坊搭建私链及链接钱包——2019年12月12日


title: 如何搭建一条以太坊私链,并使用Ethereum Wallet链接?
date: "2019-10-18 10:17:16"
tags: Dapp开发
categories: 技术驿站


1.开发环境介绍

  1. Geth(go-ethereum)版本 ——1.8.27

    Go-ethereum客户端通常被称为Geth,它是个命令行界面,执行在Go上实现的完整以太坊节点。

    通过Geth,可以实现以太坊的各种功能,如账户的新建删除,挖矿,ether币的转移,智能合约的部署和执行等等。

  2. go版本——go version go1.9.2 darwin/amd64

    image-20191018122933436

  3. mac开发环境

  4. Ethereum wallet钱包

    image-20191018124111560

2.安装Geth

使用brew安装

//不更新homebrew  Homebrew是使用ruby开发的Mac的软件包管理器.
export HOMEBREW_NO_AUTO_UPDATE=true

//如果你在核心仓库没有找到你需要的软件,那么你就需要安装第三方的仓库去安装你需要的软件
//tap命令的仓库源默认来至于Github
brew tap ethereum/ethereum
brew install ethereum

//检查是都安装成功
geth --help
ryks-MacBook-Pro-4:~ apple$ geth --help
USAGE:
   geth [options] command [command options] [arguments...]
   
VERSION:
   1.8.27-stable
   
COMMANDS:
   account           Manage accounts
   attach            Start an interactive JavaScript environment (connect to node)
   bug               opens a window to report a bug on the geth repo
   console           Start an interactive JavaScript environment
   copydb            Create a local chain from a target chaindata folder
   dump              Dump a specific block from storage
   dumpconfig        Show configuration values
   export            Export blockchain into file
   export-preimages  Export the preimage database into an RLP stream
   import            Import a blockchain file
   import-preimages  Import the preimage database from an RLP stream
   init              Bootstrap and initialize a new genesis block
   js                Execute the specified JavaScript files
   license           Display license information
   makecache         Generate ethash verification cache (for testing)
   makedag           Generate ethash mining DAG (for testing)
   monitor           Monitor and visualize node metrics
   removedb          Remove blockchain and state databases
   version           Print version numbers
   wallet            Manage Ethereum presale wallets
   help, h           Shows a list of commands or help for one command
          

3.搭建私有链

3.1 自定义创世区块信息

节点启动之初必须载入正确的创世区块信息,且不得任意修改。

在一个全英文路径下创建一个genesis,json文件(中文路径会报错)

json文件内容如下:

config, difficulty, gasLimit, alloc创始区块文件中,这几个文件是必须的。

{
    "alloc": {},
    "config": {
        "chainID": 72,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "nonce": "0x0000000000000000",
    "difficulty": "0x4000",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
    "gasLimit": "0xffffffff"
}
  1. 链配置
    config 项是定义链配置,会影响共识协议,虽然链配置对创世影响不大,但新区块的出块规则均依赖链配置。
  2. 创世区块头信息配置
    • nonce:随机数,对应创世区块 Nonce 字段。
    • timestamp:UTC 时间戳,对应创世区块 Time 字段。
    • extraData:额外数据,对应创世区块 Extra 字段。
    • gasLimit必填,燃料上限,对应创世区块 GasLimit 字段。
    • difficulty必填,难度系数,对应创世区块 Difficulty 字段。搭建私有链时,需要根据情况选择合适的难度值,以便调整出块。
    • minHash:一个哈希值,对应创世区块的 MixDigest 字段。和 nonce 值一起证明在区块上已经进行了足够的计算。
    • coinbase:一个地址,对应创世区块的 Coinbase 字段。
  3. 初始账户资产配置
    alloc 项是创世中初始账户资产配置。在生成创世区块时,将此数据集中的账户资产写入区块中,相当于预挖矿。这对开发测试和私有链非常好用,不需要挖矿就可以直接为任意多个账户分配资产。(我们这里置空)

3.2 搭建节点并启动

1. geth --datadir "./node1" init blockGensis.json

--datadir参数:指定以太坊运行时数据存放目录

--init参数: 务必要加上,否则可以创建成功,但是后⾯有问题

image-20191012193601158

2. geth --datadir "./node1" --networkid 72 --port 30301 console

--datadir: 指定节点数据存储路径,此处会⾃动创建 node1 ,之所以这样命名,是因为后⾯会创建 node2 ,便于模拟多个节点交互

--networkid: 当前⽹络的id,写在genesis.json中的

--port: 端⼝

--console: 表明同时启动⼀个交互的终端(⾮必要)

image-20191012194527946

启动节点后,出现控制台,有一些内置函数:

- eth:包含一些跟操作区块链相关的方法;
- net:包含一些查看p2p网络状态的方法;
- admin:包含一些与管理节点相关的方法;
- miner:包含启动&停止挖矿的一些方法;
- personal:主要包含一些管理账户的方法;
- txpool:包含一些查看交易内存池的方法;
- web3:包含了以上对象,还包含一些单位换算的方法。

这些命令支持 Tab 键自动补全。

常用命令:

personal.newAccount():创建账户;
personal.unlockAccount():解锁账户;
eth.accounts:枚举系统中的账户;
eth.getBalance():查看账户余额,
    返回值的单位是 Wei(Wei 是以太坊中最小货币面额单位,类似比特币中的聪,
    1 ether = 10^18 Wei);
eth.blockNumber:列出区块总数;
eth.getTransaction():获取交易;
eth.getBlock():获取区块;
miner.start():开始挖矿;
miner.stop():停止挖矿;
web3.fromWei():Wei 换算成以太币;
web3.toWei():以太币换算成 Wei;
txpool.status:交易池中的状态;
admin.addPeer():连接到其他节点;

3.3 创建账号并挖矿

//查web3版本
> web3.version
{
  api: "0.20.1",
  ethereum: "0x3f",
  network: "72",
  node: "Geth/v1.8.27-stable/darwin-amd64/go1.12.4",
  whisper: undefined,
  getEthereum: function(callback),
  getNetwork: function(callback),
  getNode: function(callback),
  getWhisper: function(callback)
}

//查看有没有账户,为空
> eth.accounts
[]
  
//新建账户,参数为密码
> personal.newAccount("1")
"0x43960fd7dcefa6c05daa5de69f31e717b7f57ec0"
> personal.newAccount("2")
"0xa63a6c5915f2a912aa327cca665d2633667067fa"
  
//挖矿
  miner.start()
  
//停止挖矿
  miner.stop()
>  eth.accounts
["0x43960fd7dcefa6c05daa5de69f31e717b7f57ec0", "0xa63a6c5915f2a912aa327cca665d2633667067fa"]

4.新增节点并链接,转账

4.1 新增节点

geth --datadir "./node2" init genesis.json 
geth --datadir "./node2" --networkid 72 --port 30302

新建节点node2,与node1节点创建方式一样,不过端口设置为30302

4.2 创建账户

> personal.newAccount('1')
"0xe51d257963476d4bb1c41fca4b428ac463a0c41a"
> personal.newAccount('1')
"0xecaa48b51d8e441ea78dc888e1a89b09381329ec"

4.3 查看当前节点信息

node2节点信息

> admin.nodeInfo
{
  enode: "enode://24bfcc9c913aee4f38722dc43a41c250063b4f6a8f34bd4a926272ee8f042da77013b721b1fab8a68f0d340d5e9697f0d05be708f7b47c287be2fd3399a66f85@10.21.238.199:30302",
  enr: "0xf896b8402622e2ee861a9e551f8303600c0fab80a388dae84042c4773e8dc0e3662c2e3b4c0c42f75c254f13f41c8c06dd11b3673c40227cfacee394d82e089234f2930d0283636170c6c5836574683f826964827634826970840a15eec789736563703235366b31a10324bfcc9c913aee4f38722dc43a41c250063b4f6a8f34bd4a926272ee8f042da78374637082765e8375647082765e",
  id: "1615e13fbfae24d7724e488619d9d59cef663e7b6e24e0a05f6c79bd6f6e167e",
  ip: "10.21.238.199",
  listenAddr: "[::]:30302",
  name: "Geth/v1.8.27-stable/darwin-amd64/go1.12.4",
  ports: {
    discovery: 30302,
    listener: 30302
  },
  protocols: {
    eth: {
      config: {
        chainId: 72,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        homesteadBlock: 0
      },
      difficulty: 16384,
      genesis: "0x942f596f99dc8879b426b59080824662e1f97587353d087487fea0a0e2a2588a",
      head: "0x942f596f99dc8879b426b59080824662e1f97587353d087487fea0a0e2a2588a",
      network: 72
    }
  }
}

node1节点信息

> admin.nodeInfo
{
  enode: "enode://13b1b29c814089ae9f1c45b5350c2e73e647d78a9722640ba2ae378970f90e747a799e98cf2efdebb4fd4ca3f0a9f5bb32a2fcd8a3bd01a1f6e76a0be05634dc@10.21.238.199:30301",
  enr: "0xf89cb8403e4ae603a565aa7d2f5cacce5ddf96d457a0eb7ed4a8c4c92a636f72b4c4669832284b0c243ffe9ec5f03ef05f1e59394f2fdb70bb5284d58501af6149a52c9f0683636170ccc5836574683ec5836574683f826964827634826970840a15eec789736563703235366b31a10213b1b29c814089ae9f1c45b5350c2e73e647d78a9722640ba2ae378970f90e748374637082765d8375647082765d",
  id: "a3d459c6ca9dd8f8f964db7e3db0e4406a01b27d8079d5ae1653343c7f40caee",
  ip: "10.21.238.199",
  listenAddr: "[::]:30301",
  name: "Geth/v1.8.27-stable/darwin-amd64/go1.12.4",
  ports: {
    discovery: 30301,
    listener: 30301
  },
  protocols: {
    eth: {
      config: {
        chainId: 72,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        homesteadBlock: 0
      },
      difficulty: 541056,
      genesis: "0x942f596f99dc8879b426b59080824662e1f97587353d087487fea0a0e2a2588a",
      head: "0x662e716503e6b737c376511c3ca8afc19220374f9206f14d04bf0c239232e9fc",
      network: 72
    }
  }
}

4.4 node1添加node2

>admin.addPeer("enode://24bfcc9c913aee4f38722dc43a41c250063b4f6a8f34bd4a926272ee8f042da77013b721b1fab8a68f0d340d5e9697f0d05be708f7b47c287be2fd3399a66f85@10.21.238.199:30302")
true

5.连接到wallet

5.1 查看ethereum

ryks-MacBook-Pro-4:~ apple$ ps -ef | grep geth
  501  5608  5602   0  2:46下午 ttys000    0:00.79 geth --datadir ./node1 --ipcpath /Users/apple/Library/Ethereum/geth.ipc
  501  5629  5621   0  2:46下午 ttys001    0:00.00 grep geth

Ethereum客户端每次都要指定特定的geth.ipc,指定--testnet 来和测试⽹络连接,--testnet参数就是 Ropsten⽹络。

我们可以事先拉起⼀个geth服务,其中将存储数据参数指定为我们的私有⽹络,这样Mist在启动时, 就会⾃动连接到我们的服务,⽽不会重新起服务。

5.2 mist连接到私有链

geth --datadir ./node1 --ipcpath /Users/apple/Library/Ethereum/geth.ipc

image-20191018145133590

之后还想尝试用浏览器显示出所有交易等,但是遇到了某些问题,没能运行成功。

END

猜你喜欢

转载自www.cnblogs.com/oneapple/p/12030476.html