【 Please indicate the source of reprint 】https://blog.csdn.net/qq_25870633/article/details/82027510
<https://blog.csdn.net/qq_25870633/article/details/82027510>

Reference from : https://www.zybuluo.com/zhenxi/note/912913
<https://www.zybuluo.com/zhenxi/note/912913>

Now we all know that many of the underlying blockchain support the storage of small amounts of data 【 It's too expensive , There is one in the yellow book gas Can refer to the consumption regulations of . General account deposit (storage)
take 0 Value to non 0 Need to consume 2W Of gas, Modify non 0 Value needs to be consumed 5K Of gas, But it will not 0 Change to 0 You can get it 1.5W Of gas; and memory Space per 32 byte consume 3gas
】, So we want to make a big file , for example : picture , What about uploading videos and so on to the chain ?【 In Ethereum, you can save the data in the log , Log cost per byte 8 individual Gas, And contract storage is
each 32 Bytes 20,000 individual Gas】 At present, the market is relatively hot at the bottom IPFS < Interstellar file system > It should meet our needs . therefore , Our theme today is IPFS .

first , We're talking IPFS Before principle , Let's play first IPFS.

install IPFS:

My demo environment is

Ubuntu 16.04.4 LTS

git   2.7.4

go   go1.10.3 linux/amd64

node  v10.6.0

npm  6.1.0

docker  1.13.1

docker-compose  1.8.0

Open it first IPFS Official website , The official website will automatically display the corresponding download version according to the platform of the current browser . After we download it , For example, mine is in the catalog :



I'm used to putting everything that needs to be installed /usr/local Under the table of contents , And the habit of the need to install things divided into directories 【 I am here win It's the same habit 】, So it is :



Start decompressing :



After decompressing, we will see one in the current directory go-ipfs Table of contents :



Here we need to put ipfs Move to   /usr/local/bin Under the table of contents :

【 be careful 】 Don't put it wrong , Don't put it in /usr/bin Go down .

/usr/bin  The following are executable programs that are pre installed on the system , It will change as the system upgrades
/usr/local/bin A directory is a place for users to place their own executable programs , It's recommended here , The file with the same name will not be updated by the system .



Then we can open a window at will :



OK, Get to this location IPFS Has been installed successfully , Very convenient , Decompress is what you want .

We can see by looking at the subcommands , Input at terminal :ipfs init You can initialize a IPFS Node of .

such as , I'm in mine /home/gavin Directory :

initialization IPFS node :ipfs init

 



QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv

At this time , The initialization of the node is successful , We'll see one in the current directory .ipfs The hidden directory of :



Enter the directory , We can see that :



as a result of : End of execution ipfs init After order , A .ipfs The folder stores node data for ..ipfs The default storage space of the node is 10 individual G.

If you want to modify the node default storage space yourself , You can open the terminal and execute the following command :

Change configuration items in nodes :export EDITOR=/usr/bin/vim && ipfs config edit



Open the file and change the red box to the size you want :



 

All right, we'll come back and see before End of execution ipfs init The file we were prompted to view later ( Remember ? Turn it up ),

View local IPFS File for :ipfs cat /ipfs/ A string of documents Hash



We've been taught how to use it quickly   IPFS Yeah ! We follow the above instructions :



We can see that there are some examples , Through this, we can basically learn a lot about IPFS It's working 【 Isn't it written in it ?】

see Of the current local node ID:ipfs id



Start one IPFS Node service :ipfs daemon

【 be careful 】 If it doesn't start , All operations are local , Including adding files , Add folder, etc , It's just going to be local , It's not going to be broadcast all over the network .

According to the above qick-start Demo in , We can use it in another window : The foreground starts a daemon , start-up IPFS Node services for < Interact with the whole network >



At this time, according to qick-start Tips for , input :http://localhost:5001/webui
<http://localhost:5001/webui>  You can see that



 

View other nodes connected to yourself :ipfs swarm peers

【 be careful 】: This step has to be stay ipfs daemon Namely It can only be viewed when the local service is on , Because it's connected to other nodes in the network



obtain IPFS The underlying structure of the object :ipfs object get A string of documents Hash



Publish local files to IPFS In the network :ipfs name publish A string of documents Hash



Download files from any node :ipfs get A string of documents Hash



The most common commands are :
init initialization ipfs Local configuration add <path> Add the specified file to IPFS cat <ref> Displays the specified IPFS Object data get <ref>
Download the specified IPFS object ls <ref> List displays links to the specified object refs <ref> List displays the link hash for the specified object
All right, that's right ipfs Some operations of , Detailed operation reference : http://cw.hubwiz.com/card/c/ipfs/1/12/2/
<http://cw.hubwiz.com/card/c/ipfs/1/12/2/>

Let's go deeper and deeper through some conceptual knowledge IPFS The underlying mechanism of :

Repo:

first , Need to know ipfs Node local is similar to repo The concept of existence . We can see it by command Statistics of local warehouse :

ipfs repo stat 



You can see that , There's an act inside : RepoPath: /home/gavin/.ipfs You can know , .ipfs The table of contents is ipfs Local warehouse directory for , The directory is implement
ipfs init The command is generated , Subsequent documents ,block And so on, and everything will be stored here :



stay IPFS in , We think ipfs When a file is requested , It's from this repo Find in , stay repo
The data in the catalog is divided into two parts :metadata( metadata ),block( Real data ).

Now let's add one locally file , Then check repo Statistical results of :



We can see that NumObjects  and RepoSize The value of the item becomes larger , Because we want to be local repo Added gavin This document

Bootstrap:

Then let's understand a concept : Bootstrap < Lead item >

What is? Bootstrap What about ? stay ipfs Of config We'll see in the file :



This is where we are ipfs init after , Generated config The file is configured by default Some by ipfs Official maintenance and operation ipfs
node , Is to facilitate us to address and download files , It is recommended not to delete it . Of course, we can also pass orders :ipfs bootstrap list see :

 

.

therefore ,bootstrap The use of , according to ipfs default The node list is provided to us when we request a file , Because there is no local warehouse , And can pull files from these nodes first
.【 The downloaded files will be saved locally repo Medium 】

Pinning:

ipfs There is a fairly aggressive caching mechanism , You can execute any ipfs Keep the object local for a short time after the operation , But these objects may be garbage cleaned up on a regular basis .
To prevent garbage collection, simply fix the hash you care about , The fixed way is Pinning 
Pinning yes ipfs A very important concept in . because ipfs Try to make it feel like every single object is local , Fixed is allowing you to tell ipfs A mechanism to always keep a given object local .

View local fixed content :ipfs pin ls



ipfs By default, the local uploaded files will be fixed :【 Remote download will not be fixed , It's stored locally for a short time repo in 】 use : ipfs repo gc It can be emptied
repo Cache files in

Want to delete a locally uploaded file , Need to contact first Fixed and then in the gc operation :ipfs pin rm -r file Hash



then , again : ipfs repo gc that will do :



【 be careful 】: We can test it , First add a file locally , And then publish it , Finally, I deleted the local file , And then it was executed ipfs cat To view the previous files , See if you can find it on the Internet :

I've tried it myself. It doesn't seem to work , And use it ipfs get  file hash Also cannot obtain before publishes in the network with Hash file , Who tried to talk to me if you could .. But I found out
Regardless of the same file Hash release N The results are the same :



All right, let's get to the point , Let's go on to the bottom , Reference from :https://www.jianshu.com/p/e4703ad30f65
<https://www.jianshu.com/p/e4703ad30f65>

IPFS Workflow of getting files

【 The first step 】: Query first Pinning without , Step 2  
【 Step two 】: Query local repo , without , Step 3  
【 Step three 】: according to bootstrap list Find other nodes , without , There is no such thing

IPFS Object of :

The following references are from :https://blog.csdn.net/yichigo/article/details/79655922
<https://blog.csdn.net/yichigo/article/details/79655922>

IPFS It's essentially a search and sharing tool IPFS Object's P2P system . One IPFS An object is a data structure with two fields :

* Data : Size less than 256 kB Unstructured data block of (blob)
* Links  :  One Link An array of structs . It contains Link Point to others IPFS object .
and Link Structure has three data fields :

* Name :Link The name of
* Hash :Link directive IPFS Object's hash
* Size 
:Link The cumulative size of the point object , calculation Link The size of the object pointed to , You need to take into account the size of all objects pointed to by the indicated object ( notes : Except for the size, If there's one in it link, Then we have to put link Medium size Add in )
It can be done through : ipfs  object get  file hash To see :



json The format is as follows :
{ "Links":[ { "Name":"AnotherName",
"Hash":"QmVtYjNij3KeyGmcgg7yVXWskLaBtov3UYL9pgcGK3MCWu", "Size":18 }, {
"Name":"SomeName", "Hash":"QmbUSy8HCn8J4TMDRRdxCbK2uCCtkQyZtY6XYv3y7kLgDC",
"Size":58 } ], "Data":"Hello World!" }
Readers may notice , All hashes are based on “Qm” initial . This is because it is actually a multihash, The first two bytes are used to specify the hash function and hash length . In the example above ,
The hexadecimal of the first two bytes is 1220, among 12 It means that this is SHA256 hash function ,20 Represents hash function selection 32 Byte length calculation .

stay ipfs in , Data and named links  【 namely Link There are Name The kind of 】 Constitutive IPFS The collection of objects is Merkle DAG  structure
. seeing the name of a thing one thinks of its function ,DAG This kind of directed acyclic graph is shown , and Merkle This is a data structure of password verification , Using encrypted hashes to address content . Readers can think about why there are no rings in this diagram :

It is shown in the figure below IPFS Object relation , The nodes of the graph represent the data in the data object , The edge representation of a graph points to other IPFS Object links , The name of the link is a label on an edge of the graph



 

file system (Filesystems)

IPFS You can easily represent a file system consisting of files and directories .

Small files (Small Files)

IPFS Object represents a small file (< 256 kb) Time , Object structure data Is the content of the file (
A small section is also appended at the beginning and end of the file data header Data and a footer data ), Object contains no links , Namely Links The array is empty .【 be careful 】
The file name is not IPFS Part of the object , Therefore, two files with different names and the same content will have the same IPFS Object representation , Having the same hash value .



 

Big files (Large Files)

Big files (> 256kb) Is made up of a link (Link) List , Each link in the list points to less than 256 kB File block of ,
Therefore, an object with a small amount of data can represent a large file . Linked to a file block name Field is an empty string .

directory structure (Directory Structures)

A directory that represents a file or other directory by pointing to IPFS Object links (Link) List . Linked name Field is the name of the file and directory

The directory structure is represented as a IPFS Object graph time , It looks like this :



 

【 be careful 】 contain Hello World!\n Will be automatically de duplicated , The data in the file is stored only in the IPFS A logical location in ( By its hash addressing ).

Version file system (Versioned File Systems)

IPFS It can be expressed as Git Data structure used , To support versioned file systems .

IPFS The main properties of the submit object are , It contains one or more names called parent0,parent1 Etc , These links point to previous submissions ; in addition , It also contains a name for object
Link to (Git It's called a tree ), The file system structure that points to the commit reference .

Let's take the previous file system directory structure as an example , Two submissions are shown here (commit): The first commit is the original structure ; In the second submission , We updated the document my_file.txt, The content changed to
Another World! Not the original Hello World!:



 

The above data and Named links Constitute markle DAG 

stay IPFS Of the BlockChain:



Put the state database in the IPFS Shangshi , Duplicate data will be found —— Between two blocks , Only state entries that have changed need to be explicitly stored .

Storing data on the blockchain and hash Differences stored on blockchain .

Ethereum + IPFS When is the time ,
Ethereum (Ethereum) On the platform , To minimize the expansion of the state database (bloat)(“ Blockchain expansion ”), It costs a lot to store the data in the relevant state database . therefore , For larger blocks of data ,Ethereum
Object that stores data in the state database IPFS hash, Instead of storing the data itself , Is a common design pattern .

Here are some links to see :

https://zhuanlan.zhihu.com/p/37455540 <https://zhuanlan.zhihu.com/p/37455540>

https://www.zybuluo.com/zhenxi/note/912913
<https://www.zybuluo.com/zhenxi/note/912913>