美文网首页
IPFS中的Bitswap工作原理

IPFS中的Bitswap工作原理

作者: Purson | 来源:发表于2020-02-22 20:24 被阅读0次

How Bitswap works

IPFS breaks up files into chunks called Blocks, identified by a Content IDentifier (CID). When nodes running the Bitswap protocol want to fetch a file, they send out “want lists” to other peers. A “want list” is a list of CIDs for blocks a peer wants to receive. Each node remembers which blocks its peers want, and each time the node receives a block it checks if any of its peers want the block and sends it to them.

To find out which peers have the blocks that make up a file, a Bitswap node first sends a want for the root block CID to all the peers it is connected to. If the peers don’t have the block, the node queries the Distributed Hash Table (DHT) to ask who has the root block. Any peers that respond with the root block are added to a session. From now on Bitswap only sends wants to peers in the session, so as not to flood the network with requests.

The node sends out a want for each CID to several peers in the session in parallel, because not all peers will have all blocks. If the node starts receiving a lot of duplicate blocks, it sends a want for each CID to fewer peers. If the node gets timeouts waiting for blocks, it sends a want for each CID to more peers. In this way the node tries to maintain a high download speed without too many duplicate blocks.

IPFS将大块文件分成小块(blocks),并通过CID(Content Identifier)将其标识,当运行着Bitswap 协议当节点需要获取一个文件的时候,它们就会把“需求清单”发送到其他节点。“需求清单”上面就是CID的列表,每个节点知道文件小块被放到什么地方,只要节点需要,任何时候都可以拿回。

当某个节点需要找出哪些节点拥有组合文件的区块,一个Bitswap节点首先向所有连接的节点寻找某个文件的根block(应该是一个链表结构,所以需要找寻根节点)。如果这些节点没有根block,那么它们就会请求分布式哈希表(DHT)。一旦有任何节点反馈自己有那个根节点,就会被加入一个会话当中。从那之后,Bitswap就只会把请求发送到这个(会话)session中到节点,而不会分散发布到网络中的其他节点。

需求节点会同时向会话中的节点发送CID列表,因为并不是所有节点都拥有完整的文件块。如果节点接收到多个重复到blocks,需求节点就会少发一些请求到其他节点,如果需求节点长时间没有得到响应,那么他就会发送更多到请求到其他节点。这样,确保了更快到下载速度。

相关文章

网友评论

      本文标题:IPFS中的Bitswap工作原理

      本文链接:https://www.haomeiwen.com/subject/ljxrqhtx.html