原文链接:filecoin-project.github.io/specs/#syst…
本文由星际联盟Boni Liu翻译,转载请注明出处。
星际联盟提供对Filecoin Spec 的全文翻译,便于Filecoin项目广大中国参与者理解Filecoin的深层原理。文章将定期更新章节,请持续关注"IPFS星际联盟"&"星际联盟Filecoin"公众号。
Filecoin的主要目的是存储客户的文件和数据。 本节将详细介绍与处理文件、分块、编码、图形表示、Pieces
(分片)、存储抽象等相关的数据结构和工具。
// Path is an opaque locator for a file (e.g. in a unix-style filesystem).
type Path string
// File is a variable length data container.
// The File interface is modeled after a unix-style file, but abstracts the
// underlying storage system.
type File interface {
Path() Path
Size() int
Close() error
// Read reads from File into buf, starting at offset, and for size bytes.
Read(offset int, size int, buf Bytes) struct {size int, e error}
// Write writes from buf into File, starting at offset, and for size bytes.
Write(offset int, size int, buf Bytes) struct {size int, e error}
}
FileStore
是一种抽象概念,用来指代Filecoin会存入数据的任何底层系统或设备。它基于Unix文件系统的语义,又包含Paths
(路径) 的概念。在这里使用这种抽象是为了确保Filecoin的实现,使终端用户可以轻松地使用符合他们需求的存储系统来替换底层存储系统。FileStore
最简单的版本是只有主机操作系统的文件系统。
// FileStore is an object that can store and retrieve files by path.
type FileStore struct {
Open(p Path) union {f File, e error}
Create(p Path) union {f File, e error}
Store(p Path, f File) error
Delete(p Path) error
// maybe add:
// Copy(SrcPath, DstPath)
}
Filecoin用户的需求差异很大,许多用户(尤其是矿工)将依附和围绕Filecoin来实现复杂的存储架构。FileStore
这种抽象化存在的目的,就是便于满足不同的需求。 Filecoin协议中对所有文件和扇区本地数据的存储,都是通过这个FileStore
接口定义的,这使得可交换变得易于实现,并且终端用户可以轻松选择所需要的系统。
FileStore
接口可以通过多种支持备份数据的存储系统来实现。 例如:
Implementation应当实现对主机OS文件系统的支持。Implementation能够实现对其他存储系统的支持。
Piece
(分片) 代表File
(文件) 的整体或一部分,在Deals
(交易) 中被Clients
(客户) 和Miners
(矿工) 使用。 客户需雇用矿工来存储分片。
分片结构的设计用于证明任意IPLD图表和客户端数据的存储。下图显示了分片的详细组成及其证明树,包括完整的和带宽最大化的分片结构。
分片,证明树,分片的数据结构(在新标签页中打开)
import abi "github.com/filecoin-project/specs-actors/actors/abi"
// PieceInfo is an object that describes details about a piece, and allows
// decoupling storage of this information from the piece itself.
type PieceInfo struct {
ID PieceID
Size abi.PieceSize
// TODO: store which algorithms were used to construct this piece.
}
// Piece represents the basic unit of tradeable data in Filecoin. Clients
// break files and data up into Pieces, maybe apply some transformations,
// and then hire Miners to store the Pieces.
//
// The kinds of transformations that may ocurr include erasure coding,
// encryption, and more.
//
// Note: pieces are well formed.
type Piece struct {
Info PieceInfo
// tree is the internal representation of Piece. It is a tree
// formed according to a sequence of algorithms, which make the
// piece able to be verified.
tree PieceTree
// Payload is the user's data.
Payload() Bytes
// Data returns the serialized representation of the Piece.
// It includes the payload data, and intermediate tree objects,
// formed according to relevant storage algorithms.
Data() Bytes
}
// // LocalPieceRef is an object used to refer to pieces in local storage.
// // This is used by subsystems to store and locate pieces.
// type LocalPieceRef struct {
// ID PieceID
// Path file.Path
// }
// PieceTree is a data structure used to form pieces. The algorithms involved
// in the storage proofs determine the shape of PieceTree and how it must be
// constructed.
//
// Usually, a node in PieceTree will include either Children or Data, but not
// both.
//
// TODO: move this into filproofs -- use a tree from there, as that's where
// the algorightms are defined. Or keep this as an interface, met by others.
type PieceTree struct {
Children [PieceTree]
Data Bytes
}
PieceStore
可以从一些本地存储中存储和检索分片。 PieceStore
还会额外提供对分片的索引。
import ipld "github.com/filecoin-project/specs/libraries/ipld"
type PieceID UVarint
// PieceStore is an object that stores pieces into some local storage.
// it is internally backed by an IpldStore.
type PieceStore struct {
Store ipld.GraphStore
Index {PieceID: Piece}
Get(i PieceID) struct {p Piece, e error}
Put(p Piece) error
Delete(i PieceID) error
}
数据传输是一种系统,用于交易产生时通过网络传输完整或部分的分片。
下图展示了Data Transfer(数据传输) 和它的模块如何适应存储和检索市场。 需要特别关注的是,来自市场的Data Transfer Request Validators(数据传输请求验证器) ,是如何插入到Data Transfer(数据传输) 模块中,又能保证他们的代码是属于市场系统的。
数据传输 - 推送流程(在新标签中打开)
所有的数据传输都有两个基本阶段:
请注意,“协商”和“转移”阶段可以在单独的往返过程中进行,也可能在相同的往返过程中进行。在此过程中,请求方通过发送请求隐式地表示同意,而响应方可以直接同意并立即发送或接收数据。
数据传输 - 推送流程图(在新标签中打开)
推送流程是存储交易的理想选择,客户一旦确认交易已签署并处于链上,就会启动推送
数据传输 - 合并流程(在新标签页中打开)
合并流程是检索交易的理想选择,一旦交易达成,客户将启动合并。
[ID:ipfsunion6]
扫描二维码
关注官方公众号