以太坊私链入门

链客,专为开发者而生,有问必答!

此文章来自区块链技术社区,未经允许拒绝转载
在这里插入图片描述
目录

  1. 背景
  2. 软件安装与配置
    2.1. Ubuntu
    2.1.1. 安装 geth
    2.1.2. 安装 solc
    2.2. Windows
    2.3. Mac OS
    2.4. 编译安装
  3. 创世区块
    3.1. 初始化创世区块
    3.2. 启动节点
    3.2.1. rpcaddr
    3.3. 使用节点进行挖矿
    3.3.1. 启动矿工开始挖矿
    3.3.2. 停止挖矿
    3.3.3. 查看所挖金额
  4. 管理
    4.1. 控制台
    4.2. 连接控制台
    4.3. 账号管理
    4.3.1. 新建账号
    4.3.2. 查看账号
    4.4. 运行JS
  5. JavaScript Console
    5.1. personal 管理
    5.1.1. 创建账号
    5.1.2. 显示账号
    5.2. eth 管理
    5.2.1. 矿工账号
    5.2.2. 余额
    5.2.2.1. 单位转换
    5.2.3. 转账
    5.3. admin 管理
    5.3.1. 显示节点
    5.3.2. 查看池
    5.3.3. 添加节点
    5.4. miner 挖矿管理
    5.4.1. 开始挖矿
    5.4.2. 停止挖矿
  6. 总结
  7. FAQ
    7.1. Error: authentication needed: password or unlock
  8. 背景
    区块链是什么?一句话,它是一种特殊的(非关系型)分布式数据库,这种数据库只能做插入和查找操作,并且没有管理员。

首先,区块链的主要作用是储存信息。任何需要保存的信息,都可以写入区块链,也可以从里面读取,所以它是数据库。

其次,任何人都可以架设服务器,加入区块链网络,成为一个节点。区块链的世界里面,没有中心节点,每个节点都是平等的,都保存着整个数据库。你可以向任何一个节点,写入/读取数据,因为所有节点最后都会同步,保证区块链一致。

  1. 软件安装与配置
    2.1. Ubuntu
    2.1.1. 安装 geth
    安装环境

Ubuntu 17.10

sudo apt upgrade -y
sudo apt install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt update
sudo apt install ethereum
neo@netkiller ~ % geth version
Geth
Version: 1.7.3-stable
Git Commit: 4bb3c89d44e372e6a9ab85a8be0c9345265c763a
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.1
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.9
2.1.2. 安装 solc
sudo apt install solc -y
neo@netkiller ~ % solc --version
solc, the solidity compiler commandline interface
Version: 0.4.19+commit.c4cbbb05.Linux.g++
2.2. Windows
访问 https://geth.ethereum.org/downloads/
下载并安装 Geth for Windows
2.3. Mac OS
brew tap ethereum/ethereum
brew install ethereum
2.4. 编译安装
git clone https://github.com/ethereum/go-ethereum
sudo apt-get install -y build-essential golang
cd go-ethereum
make geth
3. 创世区块
cd ~
mkdir -p ethereum
cd ethereum
3.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
dat

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值