practice collation (1) Basic knowledge of
practice finishing (2) Geth client
practice finishing (3) Remix development and deployment of smart contracts
practice finishing (4) Truffle smart contract development framework
practice finishing (5) DApp development process record (part 1)
practice finishing (5) DApp development process record (Part 2)
Ethereum practice sorting (6) file decentralized storage
The previously implemented DAPP realizes the decentralization of runtime rules through open source code and smart contracts. But the front-end page always comes from a centralized Web server, and the centralized server has absolute control over the displayed content, so decentralization is not pure enough. I need to decentralize the storage of the entire front-end packaging directory.
IPFS protocol
The goal of IPFS is to replace HTTP to build a better decentralized Web. The existing network service, based on IP addressing, is that IP finds the server where the content is located, and then interacts with the server. In the IPFS network, it is based on content addressing. The files we upload to IPFS will generate hash values. You don't need to know where the file is stored. You can find the file through the hash value.
IPFS installation
$ tar go-ipfs_v0.9.1_darwin-amd64.tar
$ cd go-ipfs
$ sudo ./install.sh
IPFS initialization
$ ipfs init
generating ED25519 keypair...done
peer identity: 12D3KooWBsfJCNoZBNLd825gcAnhSMqHJoFmE3PSpd86v49PjJSQ
initializing IPFS node at /Users/zhutx/.ipfs
to get started, enter:
ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme
After the above command is executed, a key pair is generated and the corresponding node identity id (peer identity) is generated. At the same time, a hidden directory of .ipfs is generated under the user's home directory, and IPFS related data is here. The IPFS configuration file is .ipfs/config, the synchronization file database is in the .ipfs/blocks directory, and the key is in the .ipfs/keystore directory.
upload file
Create a file:
$ echo "Hello World" >> hello.txt
Add to IPFS:
$ ipfs add hello.txt
added QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u hello.txt
12 B / 12 B [=========================================================] 100.00%
View the content of the file on IPFS:
$ ipfs cat QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u
Hello World
upload directory
Create a directory and move files into:
$ mkdir upload
$ mv hello.txt upload
Upload the entire directory to IPFS:
$ ipfs add -r upload
added QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u upload/hello.txt
added QmXnF4uF4hG8pANVHxXoL9z8v87xinNwcdMMnBnqRnrSiW upload
12 B / 12 B [============================================================================================] 100.00%
The above command returns the directory hash and the hash of each file. The content of the file has not changed, so it is still the previous hash value
// 可以通过【目录哈希值/文件名】的方式,访问文件内容
$ ipfs cat QmXnF4uF4hG8pANVHxXoL9z8v87xinNwcdMMnBnqRnrSiW/hello.txt
Hello World
// 当然也是可以直接文件哈希值访问文件内容
$ ipfs cat QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u
Hello World
synchronized to the network
The files and directories we added above only exist on the local IPFS node. We need to synchronize the data to the IPFS network, and we need to open the daemon service:
$ ipfs daemon
Initializing daemon...
go-ipfs version: 0.9.1
Repo version: 11
System version: amd64/darwin
Golang version: go1.16.6
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/127.0.0.1/udp/4001/quic
Swarm listening on /ip4/192.168.31.175/tcp/4001
Swarm listening on /ip4/192.168.31.175/udp/4001/quic
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /ip6/::1/udp/4001/quic
Swarm listening on /p2p-circuit
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/127.0.0.1/udp/4001/quic
Swarm announcing /ip4/192.168.31.175/tcp/4001
Swarm announcing /ip4/192.168.31.175/udp/4001/quic
Swarm announcing /ip6/::1/tcp/4001
Swarm announcing /ip6/::1/udp/4001/quic
API server listening on /ip4/127.0.0.1/tcp/5001
WebUI: http://127.0.0.1:5001/webui
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready
After opening the daemon, it will try to connect to other nodes to synchronize data. At the same time, the API service and Web gateway service will be enabled locally (default port 8080).
API Service Console
The API service is equipped with an IPFS Web version of the management console, you can upload files directly on the console, check the node connection, and so on.
It can be accessed through http://localhost:5001/webui:
through the gateway
Because the current browser does not support IPFS protocol (ipfs://) to access files, you need to use the Web gateway service to obtain files on the IPFS network. The file just uploaded can be accessed through the gateway as follows:
http://127.0.0.1:8080/ipfs/QmXnF4uF4hG8pANVHxXoL9z8v87xinNwcdMMnBnqRnrSiW/hello.txt
The files have been synchronized to the IPFS network, and external users can access them through the official IPFS gateway: https://ipfs.io/ipfs/QmXnF4uF4hG8pANVHxXoL9z8v87xinNwcdMMnBnqRnrSiW/hello.txt
The official IPFS gateway service may need to go over the wall to connect. You can find a third-party public IPFS gateway service here:
https://ipfs.github.io/public-gateway-checker/
By uploading the catalog, we can upload the entire DAPP front-end catalog to the IPFS network to achieve the decentralization of the front-end. However, please note that you need to use relative paths to introduce resources such as css and js in the code, otherwise the corresponding files will not be found when accessing index.html through IPFS.
What is the relationship between IPFS and filecoin
IPFS is a network protocol, and Filecoin is a decentralized storage project based on IPFS. The Filecoin project is the incentive layer of the IPFS network. The Filecoin project is mainly to motivate everyone to contribute hard disk resources, because IPFS mining is essentially sharing its own storage space.
We can understand the Filecoin project as an incentive system running on the IPFS network. IPFS is a point-to-point distributed network. IPFS requires more nodes to participate. The way to participate is to access the IPFS network to share hard drives and bandwidth. However, if there is no good incentive mechanism, who is willing to contribute their own hard disk and bandwidth resources? So at this time, IPFS needs an incentive mechanism like filecoin.
practice collation (1) Basic knowledge of
practice finishing (2) Geth client
practice finishing (3) Remix development and deployment of smart contracts
practice finishing (4) Truffle smart contract development framework
practice finishing (5) DApp development process record (
practice finishing (5) DApp development process record (Part 2)
Ethereum practice sorting (6) file decentralized storage
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。