主页 > imtoken官网注册 > Ubuntu16.04搭建以太坊私有链

Ubuntu16.04搭建以太坊私有链

imtoken官网注册 2023-02-01 06:06:14

一、说明

我想尝试发布基于以太坊的应用或Token,但我需要使用以太币进行正常的网络测试。 购买麻烦,每一步都需要消耗以太币。 但是,我们可以建立私有链进行开发。 话不多说,下面开始搭建我们自己的开发测试私链。

2. 所需工具

以太坊客户端

以太坊客户端用于访问以太坊网络并执行与账户管理、交易、挖矿和智能合约相关的操作。 目前有多种语言实现的客户端。 常用的是Go语言实现的go-ethereum客户端Geth。 支持接入以太坊网络成为一个完整的节点,也可以作为HTTP-RPC服务器对外提供JSON-RPC接口。 .

其他客户有:智能合约编译器

以太坊支持两种智能合约编程语言:Solidity 和 Serpent。 目前主流的是Solidity,学习教程可以参考本站。

现在以太坊提供了更方便的在线IDE——Remix 使用Remix,无需安装

溶胶

而编译过程,可以直接提供部署合约所需的二进制代码和ABI。

以太坊钱包

Ethereum 提供了一个 GUI 钱包 Ethereum Wallet 和一个 Mist Dapp 浏览器。 钱包的功能是Mist的一个子集,可以用来管理账户和交易; Mist还可以基于钱包运行智能合约。 为了演示合约部署过程,本文使用Geth控制台操作,而不是Mist。 当然,使用 Mist 会更简单。

三、安装

我们使用 Ubuntu 16.04 作为服务器。

首先安装必要的工具

apt install software-properties-common

再次添加以太坊源

add-apt-repository -y ppa:ethereum/ethereum
apt update

以太坊环境搭建_以太坊私链搭建_以太坊搭建私链

最后安装go-ethereum

apt install ethereum

安装完成后,可以运行命令查看是否成功

geth version

展示

Geth
Version: 1.8.2-stable
Git Commit: b8b9f7f4476a30a0aaf6077daade6ae77f969960
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.4
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.9

4、私链建设

1.创建初始化配置文件和存放数据目录

要运行以太坊私有链,需要定义自己的创世块,这是一个json格式的配置文件。 我们可以创建一个新文件 genesis.json

以太坊搭建私链_以太坊私链搭建_以太坊环境搭建

{
"config": {
    "chainId": 411,
    "homesteadBlock": 0,
    "eip155Block": 0,
    "eip158Block": 0
  },
  "nonce": "0x0000000000000033",
  "timestamp": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x8000000",
  "difficulty": "0x100",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": {
        "0x1C83C95473e1e93A2C8560c73976dAFA9C3f0a79":{"balance":"1000000"}
}
}

以太坊搭建私链_以太坊私链搭建_以太坊环境搭建

其中 chainID 指定区块链网络 ID。

而且我的地址有配额。其他参数可以参考网站

这里存放数据目录我用/home/ubuntu/ethereum/data0

genesis.json 存放在 /home/ubuntu/ethereum/

然后可以执行命令来执行初始化操作。

cd ethereum
geth --datadir data0 init genesis.json

结果

INFO [04-11|07:51:14] Maximum peer count                       ETH=25 LES=0 total=25
INFO [04-11|07:51:14] Allocated cache and file handles         database=/home/ubuntu/ethereum/data0/geth/chaindata cache=16 handles=16
INFO [04-11|07:51:14] Writing custom genesis block 
INFO [04-11|07:51:14] Persisted trie from memory database      nodes=1 size=195.00B time=35.174碌s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|07:51:14] Successfully wrote genesis state         database=chaindata                                  hash=b924d6Ωfdceb
INFO [04-11|07:51:14] Allocated cache and file handles         database=/home/ubuntu/ethereum/data0/geth/lightchaindata cache=16 handles=16
INFO [04-11|07:51:14] Writing custom genesis block 
INFO [04-11|07:51:14] Persisted trie from memory database      nodes=1 size=195.00B time=460.134碌s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|07:51:14] Successfully wrote genesis state         database=lightchaindata                                  hash=b924d6Ωfdceb

初始化成功后,会在data0目录下生成需要的文件。

以太坊环境搭建_以太坊搭建私链_以太坊私链搭建

其中,区块数据存放在geth/chaindata,账户数据存放在keystore。

5.启动私有节点

初始化完成后,你就拥有了自己的私有链,我们可以执行命令启动它

geth --identity "TestNode" --rpc --rpcport "8545" --datadir data0 --port "30303" --nodiscover console

上面命令的主体是geth console,意思是启动节点,进入交互式控制台。

各选项含义如下:

运行上述命令后以太坊私链搭建,区块链节点启动,进入节点控制台:

INFO [04-11|08:05:23] Maximum peer count                       ETH=25 LES=0 total=25
INFO [04-11|08:05:23] Starting peer-to-peer node               instance=Geth/TestNode/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4
INFO [04-11|08:05:23] Allocated cache and file handles         database=/home/ubuntu/ethereum/data0/data0/geth/chaindata cache=768 handles=512
INFO [04-11|08:05:23] Writing default main-net genesis block 
INFO [04-11|08:05:23] Persisted trie from memory database      nodes=12356 size=2.34mB time=47.705254ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|08:05:23] Initialised chain configuration          config="{ChainID: 1 Homestead: 1150000 DAO: 1920000 DAOSupport: true EIP150: 2463000 EIP155: 2675000 EIP158: 2675000 Byzantium: 4370000 Constantinople:  Engine: ethash}"
INFO [04-11|08:05:23] Disk storage enabled for ethash caches   dir=/home/ubuntu/ethereum/data0/data0/geth/ethash count=3
INFO [04-11|08:05:23] Disk storage enabled for ethash DAGs     dir=/root/.ethash                                 count=2
INFO [04-11|08:05:23] Initialising Ethereum protocol           versions="[63 62]" network=1
INFO [04-11|08:05:23] Loaded most recent local header          number=0 hash=d4e567b8fa3 td=17179869184

以太坊私链搭建_以太坊环境搭建_以太坊搭建私链

INFO [04-11|08:05:23] Loaded most recent local full block number=0 hash=d4e567b8fa3 td=17179869184 INFO [04-11|08:05:23] Loaded most recent local fast block number=0 hash=d4e567b8fa3 td=17179869184 INFO [04-11|08:05:23] Regenerated local transaction journal transactions=0 accounts=0 INFO [04-11|08:05:23] Starting P2P networking INFO [04-11|08:05:23] RLPx listener up self="enode://b5e91ca148308d721599ac14b9f2a9aff28c88b258a439fbc19f094d65ecaa9954a6a7d0de29f5737726bcdb20a443db9edeedce0471c1ba5cda043e8601e0d0@[::]:30303?discport=0" INFO [04-11|08:05:23] IPC endpoint opened url=/home/ubuntu/ethereum/data0/data0/geth.ipc INFO [04-11|08:05:23] HTTP endpoint opened url=http://127.0.0.1:8545 cors= vhosts=localhost Welcome to the Geth JavaScript console! instance: Geth/TestNode/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4 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

这是一个交互式 JavaScript 执行环境以太坊私链搭建,可以在其中执行 JavaScript 代码,其中 > 是命令提示符。 在这个环境中,还内置了一些操作以太坊的JavaScript对象,可以直接使用这些对象。 这些对象主要包括:

6.控制台操作

进入以太坊Javascript控制台后,可以使用内置对象进行一些操作。 这些内置对象提供了很多功能,例如查看区块和交易、创建账户、挖矿、发送交易和部署智能合约。

常用的命令有:

这些命令支持Tab键自动补全,具体用法如下。

1. 创建账户

>personal.newAccount()
Passphrase: 
Repeat passphrase: 
"0x914995c9c3c993c9d3fdd63602c91823f932b308"