Ubuntu下DFINITY公链网络部署
Posted Jude00000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu下DFINITY公链网络部署相关的知识,希望对你有一定的参考价值。
Ubuntu下DFINITY公链网络部署(一)
一、前提条件
1.1 安装nodejs
$ sudo apt install nodejs
$ sudo apt install npm
测试是否安装完成
$ node -v
v10.19.0
$ npm -v
6.14.4
1.2 安装DFINITY Canister SDK软件包
浏览器输入以下网址下载sdk执行文件
https://sdk.dfinity.org/install.sh
然后把install.sh文件传入linux中或者新建install.sh文件,内容复制粘贴
用以下命令安装sdk
$ sh install.sh
测试是否安装成功
$ dfx --version
dfx 0.6.26
通过以下命令查看dfx的用法
$ dfx --help
二、创建一个新项目
- 建立一个项目文件夹(文件名自拟)
$ mkdir myProjects
$ cd myProjects
- 建立一个新项目(项目名自拟)
$ dfx new Hello
显示以下信息
Creating new project "Hello"...
CREATE Hello/README.md (1.14KB)...
CREATE Hello/dfx.json (464B)...
CREATE Hello/.gitignore (165B)...
CREATE Hello/src/Hello/main.mo (107B)...
CREATE Hello/src/Hello_assets/assets/sample-asset.txt (24B)...
CREATE Hello/webpack.config.js (2.15KB)...
CREATE Hello/src/Hello_assets/public/index.js (137B)...
CREATE Hello/package.json (284B)...
进入项目根目录
$ cd Hello
三、启动本地网络
- 通过以下命令启动互联网计算网络
$ dfx start
打印以下信息表示启动成功
May 13 14:12:48.106 WARN s:fscpm-uiaaa-aaaaa-aaaap-yai/n:megrz-c3s6h-oxanp-fejfh-ojuny-4yayd-w6f45-ogssj-t4452-4lulr-gae/ic_http_handler/ic_http_handler NNS subnet not found in network topology. Skipping fetching the delegation.
Starting webserver on port 35421 for replica at "http://localhost:35421"
binding to: V4(127.0.0.1:8000)
replica(s): http://localhost:35421/
- 保持打开互联网计算网络的终端窗口,再打开另一个终端窗口,进入项目Hello根目录
$ cd myProject/Hello
四、注册、构建和部署应用程序
- 通过以下命令确保node模块可用
$ npm install
如果出现以下信息,则在package.json里添加"private": true(加在中间需要逗号)
npm WARN Hello_assets@0.1.0 No repository field.
npm WARN Hello_assets@0.1.0 No license field.
$ vi package.json
{
"name": "Hello_assets",
"version": "0.1.0",
"private": true,
"description": "",
"keywords": [],
"scripts": {
"build": "webpack"
},
"devDependencies": {
"@dfinity/agent": "0.6.26",
"terser-webpack-plugin": "2.2.2",
"webpack": "4.41.3",
"webpack-cli": "3.3.10"
}
}
- 通过运行以下命令来注册,构建和部署您的第一个应用程序
$ dfx deploy
成功会打印以下信息
Deploying all canisters.
Creating canisters...
Creating canister "Hello"...
Creating the canister using the wallet canister...
Creating a wallet canister on the local network.
The wallet canister on the "local" network for user "default" is "rwlgt-iiaaa-aaaaa-aaaaa-cai"
"Hello" canister created with canister id: "rrkah-fqaaa-aaaaa-aaaaq-cai"
Creating canister "Hello_assets"...
Creating the canister using the wallet canister...
"Hello_assets" canister created with canister id: "ryjl3-tyaaa-aaaaa-aaaba-cai"
Building canisters...
Building frontend...
Installing canisters...
Installing code for canister Hello, with canister_id rrkah-fqaaa-aaaaa-aaaaq-cai
Installing code for canister Hello_assets, with canister_id ryjl3-tyaaa-aaaaa-aaaba-cai
Authorizing our identity (default) to the asset canister...
Uploading assets to asset canister...
Deployed canisters.
- 通过运行以下命令来调用Hello容器和预定义greet函数
$ dfx canister call Hello greet everyone
输出以下信息
("Hello, everyone!")
五、测试前端应用程序
通过之前绑定的网址在linux下的浏览器访问
http://127.0.0.1:8000/?canisterId=ryjl3-tyaaa-aaaaa-aaaba-cai
浏览器显示以下界面
如果忘记了自己的canisterId,可以通过如下命令找回,资产容器是通过项目名生成的,这里是Hello_assets
$ dfx canister id Hello_assets
显示以下信息
ryjl3-tyaaa-aaaaa-aaaba-cai
六、关闭互联网计算网络
$ dfx stop
本期完
以上是关于Ubuntu下DFINITY公链网络部署的主要内容,如果未能解决你的问题,请参考以下文章
Dfinity挖矿系列:编译和构建网络计算机(ICP)核心服务
加密货币初创公司Dfinity将推出与亚马逊网络服务竞争的互联网区块链