0%

DAPP本地环境部署

DAPP

dApp在私有测试区块链环境下的部署过程,过程中使用了他人的智能合约及前端程序。

环境准备

GO环境

访问Golang官网并下载:https://golang.org/doc/install

1
2
3
https://golang.org/dl/go1.15.7.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.15.7.linux-amd64.tar.gz # sudo if needed
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc

GO <1.13无法从源码安装go-ethereum

安装geth

  1. 下载go-ethereum:
    1
    2
    3
    4
    git clone https://github.com/ethereum/go-ethereum.git
    cd go-ethereum
    make geth # make all
    echo 'export PATH=$PATH:<PWD>/build/bin' >> ~/.bashrc

创世

首先要创建一个配置文件,使用puppeth,在加入节点时无需创建此文件,只需要使用第一个节点创建时生成的json文件即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
michael@michael-ubuntu:~/blockchain$ mkdir -p ChainSkills/private                                                                                           
michael@michael-ubuntu:~/blockchain$ cd ChainSkills/private/
michael@michael-ubuntu:~/blockchain/ChainSkills/private$ puppeth
+-----------------------------------------------------------+
| Welcome to puppeth, your Ethereum private network manager |
| |
| This tool lets you create a new Ethereum network down to |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail. |
| |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset. |
+-----------------------------------------------------------+

Please specify a network name to administer (no spaces, hyphens or capital letters please)
> cfca

Sweet, you can set this via --network=cfca next time!

INFO [01-21|09:32:09.988] Administering Ethereum network name=cfca
WARN [01-21|09:32:09.988] No previous configurations found path=/home/michael/.puppeth/cfca
What would you like to do? (default = stats)
1. Show network stats
2. Configure new genesis
3. Track new remote server
4. Deploy network components
> 2

What would you like to do? (default = create)
1. Create new genesis from scratch
2. Import already existing genesis
> 1

Which consensus engine to use? (default = clique)
1. Ethash - proof-of-work
2. Clique - proof-of-authority
> 1

Which accounts should be pre-funded? (advisable at least one)
> 0x

Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)
>

Specify your chain/network ID if you want an explicit one (default = random)
> 6996
INFO [01-21|09:33:22.523] Configured new genesis block

What would you like to do? (default = stats)
1. Show network stats
2. Manage existing genesis
3. Track new remote server
4. Deploy network components
> 2

1. Modify existing configurations
2. Export genesis configurations
3. Remove genesis configuration
> 2

Which folder to save the genesis specs into? (default = current)
Will create cfca.json, cfca-aleth.json, cfca-harmony.json, cfca-parity.json
>
INFO [01-21|09:34:23.364] Saved native genesis chain spec path=cfca.json
INFO [01-21|09:34:23.365] Saved genesis chain spec client=aleth path=cfca-aleth.json
INFO [01-21|09:34:23.366] Saved genesis chain spec client=parity path=cfca-parity.json
INFO [01-21|09:34:23.367] Saved genesis chain spec client=harmony path=cfca-harmony.json

What would you like to do? (default = stats)
1. Show network stats
2. Manage existing genesis
3. Track new remote server
4. Deploy network components
> ^C
michael@michael-ubuntu:~/blockchain/ChainSkills/private$ ls
cfca-aleth.json cfca-harmony.json cfca.json cfca-parity.json
michael@michael-ubuntu:~/blockchain/ChainSkills/private$ geth --datadir . init cfca.json
INFO [01-21|09:42:29.231] Maximum peer count ETH=50 LES=0 total=50
INFO [01-21|09:42:29.231] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [01-21|09:42:29.231] Set global gas cap cap=25000000
INFO [01-21|09:42:29.231] Allocated cache and file handles database=/home/michael/blockchain/ChainSkills/private/geth/chaindata cache=16.00MiB handles=16
INFO [01-21|09:42:29.318] Writing custom genesis block
INFO [01-21|09:42:29.325] Persisted trie from memory database nodes=354 size=50.23KiB time="778.382µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-21|09:42:29.325] Successfully wrote genesis state database=chaindata hash="747b68…5aa3aa"
INFO [01-21|09:42:29.325] Allocated cache and file handles database=/home/michael/blockchain/ChainSkills/private/geth/lightchaindata cache=16.00MiB handles=16
INFO [01-21|09:42:29.452] Writing custom genesis block
INFO [01-21|09:42:29.467] Persisted trie from memory database nodes=354 size=50.23KiB time=4.035487ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [01-21|09:42:29.467] Successfully wrote genesis state database=lightchaindata hash="747b68…5aa3aa"
michael@michael-ubuntu:~/blockchain/ChainSkills/private$

创建账户

每个节点都可以自己创建账户,加入节点前可自行创建账户,方便挖矿。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
michael@michael-ubuntu:~/blockchain/ChainSkills/private$ geth --datadir . account new
INFO [01-21|09:43:57.608] Maximum peer count ETH=50 LES=0 total=50
INFO [01-21|09:43:57.608] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:

Your new key was generated

Public address of the key: 0x133ee7568C6a06155Bfd31CCDd9b4ab5e6194702
Path of the secret key file: keystore/UTC--2021-01-21T01-44-03.500559011Z--133ee7568c6a06155bfd31ccdd9b4ab5e6194702

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!

michael@michael-ubuntu:~/blockchain/ChainSkills/private$ geth --datadir . account new
INFO [01-21|09:44:24.251] Maximum peer count ETH=50 LES=0 total=50
INFO [01-21|09:44:24.251] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:

Your new key was generated

Public address of the key: 0xD7204F8c8dF887dC32C7202FbCEfFA43793eb2AE
Path of the secret key file: keystore/UTC--2021-01-21T01-44-26.870996945Z--d7204f8c8df887dc32c7202fbceffa43793eb2ae

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!

michael@michael-ubuntu:~/blockchain/ChainSkills/private$ geth --datadir . account new
INFO [01-21|09:44:29.059] Maximum peer count ETH=50 LES=0 total=50
INFO [01-21|09:44:29.059] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:

Your new key was generated

Public address of the key: 0x8454b9158EB7f85A9830Ed40259C3fd39Cbce520
Path of the secret key file: keystore/UTC--2021-01-21T01-44-31.766014334Z--8454b9158eb7f85a9830ed40259c3fd39cbce520

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
michael@michael-ubuntu:~/blockchain/ChainSkills/private$ geth --datadir . account list
INFO [01-21|09:45:22.372] Maximum peer count ETH=50 LES=0 total=50
INFO [01-21|09:45:22.372] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [01-21|09:45:22.372] Set global gas cap cap=25000000
Account #0: {133ee7568c6a06155bfd31ccdd9b4ab5e6194702} keystore:///home/michael/blockchain/ChainSkills/private/keystore/UTC--2021-01-21T01-44-03.500559011Z--133ee7568c6a06155bfd31ccdd9b4ab5e6194702
Account #1: {d7204f8c8df887dc32c7202fbceffa43793eb2ae} keystore:///home/michael/blockchain/ChainSkills/private/keystore/UTC--2021-01-21T01-44-26.870996945Z--d7204f8c8df887dc32c7202fbceffa43793eb2ae
Account #2: {8454b9158eb7f85a9830ed40259c3fd39cbce520} keystore:///home/michael/blockchain/ChainSkills/private/keystore/UTC--2021-01-21T01-44-31.766014334Z--8454b9158eb7f85a9830ed40259c3fd39cbce520

启动

启动第一个节点。

1
2
3
4
5
6
7
8
9
10
11
12
13
geth --networkid 6996 --mine --miner.threads 1 --datadir . --nodiscover  --http --http.addr 192.168
.32.69 --http.port 8545 --port 30301 --http.corsdomain "*" --http.api eth,web3,net,personal --ipcpath ~/.ethereum/geth.ipc
# networkid: 与cfca.json中保持一致
# mine: 开始挖矿,观测到默认会使用第一个账户(eth.coinbase == Account #0)
# datadir: 数据目录
# nodiscover: 不开启探测节点
# http: 开启http服务,即JSON-RPC
# http.addr: 指定IP地址,默认监听在127.0.0.1
# http.port: 8545是默认端口
# port: 30301,这个端口还不清楚
# http.corsdomain: CORS设置
# http.api: 在RPC中开启的功能模块
# ipcpath: 指定IPC文件路径,使用`geth attach`时,geth会自动查找~/.ethereum/geth.ipc文件

加入节点

在go-ethereum目录中使用make all进行编译,这样会编译出除了geth之外的工具,如clef,后面会用到。

首先,将上面生成的cfca.json复制到本地,在本地创建一个文件夹来同步区块链数据等:

1
2
mkdir local && cd local
geth --datadir . init cfca.json

创建账户

1
geth --datadir . account new

启动clef

1
clef --keystore ./keystore/ --http --chainid 6996

执行:
geth attach http://192.168.32.69:8545,连接节点,当然你知道其他节点的话,也可以连接其他节点。

然后在控制台中查看区块链所有节点信息:

1
admin.nodeInfo

然后复制enode信息,退出geth,

使用如下命令接入节点:

1
2
3
geth --datadir . --bootnodes="enode://fa634d5e320df416ca87867bdb4bd4d573422021efead8c9ba84d639d352ad67cac3f8d809b3dd1b316a8eb65b059029b9f24e4f2058870d7b93b15d00197355@192.168.32.69:30301" --networkid 6996 --port 30301 --http --http.po
rt 8545 --mine --miner.threads 1 --signer http://127.0.0.1:8550
# 注意将原来enode信息中的`?disport=0`去掉


命令执行后clef会提示你进行确认。


1处为挖矿之后收取奖励的地址,这里应该跟你创建的第一个账户相同。

部署DAPP

下载源码安装

将智能合约及DAPP部署到本地:

1
2
3
4
5
6
git clone https://github.com/dappuniversity/eth_swap.git
cd eth_swap
code . # 使用vscode对代码进行部分修改
npm install
npm truffle migrate
npm run start

使用DAPP

浏览器中安装插件metamask,在本文件夹下有提供一个安装包。
使用教程:https://www.cnblogs.com/wanghui-garcia/p/9506721.html

浏览器本身不支持与区块链交互,我们通过浏览器插件metamask实现此功能。插件已上传nas。

metamask账户

有两种方法:

  1. 使用geth创建的账户
  2. 使用metamask创建账户

第一种方法的创建方式见上文。
可在metamask中导入keystore文件夹下的账户,这个过程可能需要5-6分钟,如果浏览器提示插件未响应,请点击等待。
这种方式创建的好处就是可以将之前进行挖矿的账户导入到metamask,这样你的账户中就有一些ETH来做交易了。
如果你还没有挖过矿,账户中没有ETH,可通过geth创建账户并开始挖矿,并导入到metamask中,这样挖到的资产会及时反馈到metamask中。

如果能过第二种方式创建账户的话,需要在geth中导入metamask中生成的账户,会使用到创建账户时的私钥和密码,请注意保存。

1
2
3
geth account import  --datadir . --password passfile ./key.prv
# passfile: 包含账户密码的文件
# key.prv: 包含私钥的文件

因为我们加入节点时已经开始挖矿了,我们需要做的是将我们探矿的账户导入到metamask中。

首先点击账户,到展开视图中:

选择导入账户:

选择JSON文件:

选择文件,在弹出的文件浏览器中定位到当前目录下的keystore文件夹下,这里就是我们创建的账户的JSON文件,一般选择,输入使用geth创建账户时的密码:

点击导入:

导入过程需要时间,可能会提示这个,点击Wait可等待就好:
v

我大概需要5分钟,导入成功:

为方便识别,我们改个名字:

回到我们的DAPP网站,选择Miner账户:

刷新一下页面就可以看到我们的Miner账户的币了:

我们可以购买100个ETH的ACFC,兑换比例是1:100:

经过确认之后,我们就获得了ACFC币。后面怎么用还没相好。