区块链以太坊学习二:实操搭建私链

用 Geth 搭建以太坊私链

安装Geth 

一、apt-get

$ sudo apt-get install software-properties-common

$ sudo add-apt-repository -y ppa:ethereum/ethereum

$ sudo apt-get update

$ sudo apt-get install ethereum

二、github源码安装

$ git clone https://github.com/ethereum/go-ethereum.git

$ cd go-ethereum

$ make geth

搭建私链

genesis.json

 "config": {
 "chainId": 15
 },
 "difficulty": "2000",
 "gasLimit": "2100000",
 "alloc": {
 "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
 "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
 }
}

创建一条以它作为创世块的区块链,使用命令:

geth --datadir path/to/custom/data/folder init genesis.json

在当前目录下运行 geth,就会启动这条私链,注意要将 networked 设置为与创世块配置里的 chainId 一致。

geth --datadir path/to/custom/data/folder --networkid 15

 

Geth 控制台命令

Geth Console 是一个交互式的 JavaScript 执行环境,里面内置了一些用来操作以太坊的 JavaScript 对象,我们可以直接调用这些对象来获取区块链上的相关信息。这些对象主要包括:

eth:主要包含对区块链进行访问和交互相关的方法;

net:主要包含查看 p2p 网络状态的方法;

admin:主要包含与管理节点相关的方法;

miner:主要包含挖矿相关的一些方法;

personal:包含账户管理的方法;

txpool:包含查看交易内存池的方法;

web3:包含以上所有对象,还包含一些通用方法。

常用命令有:

personal.newAccount():创建账户;

personal.unlockAccount():解锁账户;

eth.accounts:列出系统中的账户;

eth.getBalance():查看账户余额,返回值的单位是 Wei;

eth.blockNumber:列出当前区块高度;

eth.getTransaction():获取交易信息;

eth.getBlock():获取区块信息;

miner.start():开始挖矿;

miner.stop():停止挖矿;

web3.fromWei():Wei 换算成以太币;

web3.toWei():以太币换算成 Wei;

txpool.status:交易池中的状态;

其他 :npm install ganache-cli web3@0.20.1 solc

truffle develop

posted @ 2022-09-14 14:12  caolingyi  阅读(141)  评论(0编辑  收藏  举报