neo blockchain book
  • NEOHOPE's Blockchain Book
  • 概述
    • 本书内容
    • 读者定位
    • 本书约定
    • 项目对比
    • 名词释义
  • 基本原理
    • 区块链
    • 智能合约
    • 交易所
    • 系统架构
  • 基础算法
    • 默克尔树
    • Bloom过滤器
    • 公钥私钥及椭圆曲线
  • 共识算法
    • 分布式系统
    • Paxos与Raft算法
    • 拜占庭容错算法
  • Bitcoin
    • 整体架构
    • 状态转换
    • 区块确认
    • 分叉处理
    • P2P
    • 脚本
    • 钱包
    • 使用
    • 编译(Ubuntu)
    • 私链搭建(Linux)
    • 私链搭建(Docker)
  • Ethereum
    • 整体架构
    • 状态转换
    • 区块确认
    • 使用
    • Remix IDE环境搭建(Ubuntu16)
    • 智能合约编写01
    • 智能合约编写02
    • 智能合约编写03
    • 智能合约编写04
    • 编译(MacOS)
    • 私链搭建
  • EOS
    • 整体架构
    • 使用
    • 智能合约使用(单机单节点单钱包)
    • 智能合约使用(单机单节点多钱包)
    • 编写简单的智能合约
    • 编写有数据存储的智能合约
    • 编译(Linux)
    • 私链搭建(单机多节点)
    • 私链搭建(多机多节点)
  • Fabric
    • 整体架构
    • 身份管理
    • 共识达成
    • 使用
    • 智能合约介绍
    • 智能合约使用01
    • 智能合约使用02
    • 编译(MacOS)
    • 私链搭建(Ubuntu)
    • 在私链中添加一个机构
  • Cosmos
    • 整体架构
    • Zone与Hub
    • 使用
  • IPFS
    • 整体架构
    • 使用
    • 编译(MacOS)
    • 私链搭建
  • 然后呢?
由 GitBook 提供支持
在本页
  • 环境搭建
  • 环境说明
  • 下载文件
  • 开启Bootnode
  • 新建账号
  • 初始文件准备
  • 节点初始化
  • 基本操作
  • 基本查询操作
  • 转账操作
  • 挖矿确认交易
  • 查询交易结果
  1. Ethereum

私链搭建

环境搭建

环境说明

虚拟机

作用

BC01

Bootnode, Wallet

BC02

Wallet

BC03

Wallet, Miner

下载文件

#下载geth-alltools-linux-amd64-1.8.4-2423ae01.tar.gz
#拷贝到各节点
#解压

开启Bootnode

#BC01
./bootnode --genkey=boot.key
./bootnode --nodekey=boot.key 1>bootstrap.log 2>&1 &
#记录下面地址,并修改为正确IP
UDP listener up self=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[::]:30301

新建账号

./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 account new

#BC01
Password: patient1
Address: {c230847d2669091b77053df0cbc7f533a0a4cf1c}

#BC02
Password: patient2
Address: {c9fec0bd5aab52ba08a826af70bdecd2963bf5c9}

#BC03
Password: patient3
Address: {328399ffc312cca5e6955d0dc3986313b68236c8}

初始文件准备

//genesis.json
//拷贝到每台机器
//修改chainId及nonce确保只有自己认证的节点可以加入
//修改alloc给各用户预分配ETH
{
  "config": {
        "chainId": 1,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {
        "c230847d2669091b77053df0cbc7f533a0a4cf1c": {"balance": "1000000000000000000000"},
        "c9fec0bd5aab52ba08a826af70bdecd2963bf5c9": {"balance": "1000000000000000000000"},
        "328399ffc312cca5e6955d0dc3986313b68236c8": {"balance": "1000000000000000000000"}
    },
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0x20000",
  "nonce"      : "0x0000000000009527",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

节点初始化

#每个节点
./geth --datadir=~/eth001 init genesis.json

基本操作

基本查询操作

#BC01
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

#获取区块链高度
eth.blockNumber

#获取区块内容
eth.getBlock(0)

#换算单位
web3.toWei(1,"ether")

转账操作

#BC01
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

#查看BC01账号余额
web3.fromWei(eth.getBalance(eth.coinbase))
web3.fromWei(eth.getBalance('c230847d2669091b77053df0cbc7f533a0a4cf1c'))
1000

#查看BC02账号余额
web3.fromWei(eth.getBalance('c9fec0bd5aab52ba08a826af70bdecd2963bf5c9'))
1000

#解锁BC01账号
personal.unlockAccount('c230847d2669091b77053df0cbc7f533a0a4cf1c','patient1',3600)

#向账号BC02发起转账
eth.sendTransaction({from: 'c230847d2669091b77053df0cbc7f533a0a4cf1c', to: 'c9fec0bd5aab52ba08a826af70bdecd2963bf5c9', value: web3.toWei(1)})

INFO [04-23|16:00:57] Submitted transaction                    fullhash=0xb14ee153be478e1ba208a30eae4a4d3cc34a8b21116d1b17e627cb53ff6a82e6 recipient=0xC9fEC0bd5AAB52bA08a826Af70BdeCD2963BF5c9

挖矿确认交易

#BC03
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 --mine --minerthreads=1 --etherbase=0x0000000000000000000000000000000000000000 

#BC03可以看到区块产生
#BC01和BC02上,开启geth console,可以看到

查询交易结果

#BC01或BC02
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

web3.fromWei(eth.getBalance('c230847d2669091b77053df0cbc7f533a0a4cf1c'))
998.999622
#虽然只转账了1ETH,但是交易需要燃烧gas,所以余额小于999

web3.fromWei(eth.getBalance('c9fec0bd5aab52ba08a826af70bdecd2963bf5c9'))
1001
#收到了转账

上一页编译(MacOS)下一页EOS

最后更新于7年前