filecoin技術(shù)架構(gòu)分析之十:filecoin源碼分析之支撐包分析(2/2)

本文作者:楊尉;原創(chuàng)作品,轉(zhuǎn)載請注明出處

[上一篇鏈接] filecoin技術(shù)架構(gòu)分析之九:filecoin源碼分析之支撐包分析(1/2)

[下一篇鏈接] filecoin技術(shù)架構(gòu)分析之十一:filecoin源碼分析之內(nèi)部接口層api包分析

目錄

  • 10 filecoin源碼分析之支撐包分析(2/2)
    • 10.1 repo
    • 10.2 proofs和sectorbuilder
    • 10.3 type
    • 10.4 abi
    • 10.5 pubsub

本章續(xù)上一章的支撐包介紹,主要為便于后面章節(jié)的源碼理解

10.1 repo

  • 提供功能
    • 實例化fs資源或者mem資源
    • 提供讀取、設置API地址方法
    • 提供存儲已被校驗區(qū)塊的方法
    • 提供階段密封數(shù)據(jù)存儲方法
    • 提供密封完成數(shù)據(jù)存儲方法
    • 提供讀取配置方法
    • 提供通用數(shù)據(jù)存儲方法
    • 提供交易數(shù)據(jù)存儲方法
    • 提供錢包信息存儲方法
    • 提供存儲密鑰方法
    • 提供快照配置存儲方法
    • 提供版本號讀取方法
▼ package
    repo

? imports

▼ constants
    // 當前為1,可以cat ~/.filecoin/version確認
   +Version : uint

▼+Datastore : interface
    [embedded]
    // 包含datastore的read、write、batch
   +datastore.Batching

    // Repo接口分別由fsrepo及memrepo實現(xiàn)
▼+Repo : interface
    [methods]
    // 讀取API地址
   +APIAddr() : string, error
    // 存儲已被校驗過的區(qū)塊數(shù)據(jù)
   +ChainDatastore() : Datastore
    // 關(guān)閉
   +Close() : error
    // 讀取配置,對應上一章中的config
   +Config() : *config.Config
    // 存儲通用數(shù)據(jù)
   +Datastore() : Datastore
    // 交易數(shù)據(jù)存儲
   +DealsDatastore() : Datastore
    // 存儲密鑰相關(guān)
   +Keystore() : keystore.Keystore
    // 存儲倒數(shù)第二個配置
   +ReplaceConfig(cfg *config.Config) : error
    // 存儲密封扇區(qū)
   +SealedDir() : string
    // 設置API地址
   +SetAPIAddr(string) : error
    // 存儲分段密封扇區(qū)
   +StagingDir() : string
    // 讀取版本號
   +Version() : uint
    // 存儲錢包信息
   +WalletDatastore() : Datastore
location: repo/fsrepo.go

▼ package
    repo

▼ constants
    // api文件
   +APIFile
    // chain目錄:chain
   -chainDatastorePrefix
    // 配置文件名稱,對應上一章中的config
   -configFilename
    // 交易目錄:deals
   -dealsDatastorePrefix
    // 資源目錄鎖文件:repo.lock
   -lockFile
    // 快照文件前綴名 snapshot
   -snapshotFilenamePrefix
    // 快照目錄;配置快照
   -snapshotStorePrefix
    // 臨時配置文件名稱
   -tempConfigFilename
    // version文件名稱
   -versionFilename
    // 錢包目錄名稱wallet
   -walletDatastorePrefix

▼ variables
   -log

▼+FSRepo : struct
    [fields]
   -cfg : *config.Config
   -chainDs : Datastore
   -dealsDs : Datastore
   -ds : Datastore
   -keystore : keystore.Keystore
   -lk : sync.RWMutex
   -lockfile : io.Closer
    // 資源目錄路徑
   -path : string
    // 資源目錄版本
   -version : uint
   -walletDs : Datastore
    [methods]
   +APIAddr() : string, error
   +ChainDatastore() : Datastore
   +Close() : error
   +Config() : *config.Config
   +Datastore() : Datastore
   +DealsDatastore() : Datastore
   +Keystore() : keystore.Keystore
   +ReplaceConfig(cfg *config.Config) : error
   +SealedDir() : string
   +SetAPIAddr(maddr string) : error
    // 快照存儲
   +SnapshotConfig(cfg *config.Config) : error
   +StagingDir() : string
   +Version() : uint
   +WalletDatastore() : Datastore
   -loadConfig() : error
   -loadFromDisk() : error
   -loadVersion() : uint, error
   -openChainDatastore() : error
   -openDatastore() : error
   -openDealsDatastore() : error
   -openKeystore() : error
   -openWalletDatastore() : error
   -removeAPIFile() : error
   -removeFile(path string) : error
    [functions]
    // 打開已被初始化過的資源目錄
   +OpenFSRepo(p string) : *FSRepo, error

▼+NoRepoError : struct
    [fields]
   +Path : string
    [methods]
   +Error() : string

▼ functions
    // 從文件中讀取api file
   +APIAddrFromFile(apiFilePath string) : string, error
    // 初始化資源目錄
   +InitFSRepo(p string, cfg *config.Config) : error
   -checkWritable(dir string) : error
   -fileExists(file string) : bool
   -genSnapshotFileName() : string
   -initConfig(p string, cfg *config.Config) : error
   -initVersion(p string, version uint) : error
   -isInitialized(p string) : bool, error
▼ package
    repo

▼ imports

▼+MemRepo : struct
    [fields]
   +C : *config.Config
   +Chain : Datastore
   +D : Datastore
   +DealsDs : Datastore
   +Ks : keystore.Keystore
   +W : Datastore
   -apiAddress : string
   -lk : sync.RWMutex
   -sealedDir : string
   -stagingDir : string
   -version : uint
    [methods]
   +APIAddr() : string, error
   +ChainDatastore() : Datastore
   +CleanupSectorDirs()
   +Close() : error
   +Config() : *config.Config
   +Datastore() : Datastore
   +DealsDatastore() : Datastore
   +Keystore() : keystore.Keystore
   +ReplaceConfig(cfg *config.Config) : error
   +SealedDir() : string
   +SetAPIAddr(addr string) : error
   +StagingDir() : string
   +Version() : uint
   +WalletDatastore() : Datastore
    [functions]
    // 實例化內(nèi)存資源接口,會調(diào)用NewInMemoryRepoWithSectorDirectories
   +NewInMemoryRepo() : *MemRepo
    // 實例化內(nèi)存資源接口,指定階段密封和最終密封目錄
   +NewInMemoryRepoWithSectorDirectories(staging, sealedDir string) : *MemRepo

10.2 proofs和sectorbuilder

  • proofs提供功能
    • 校驗時空證明的方法
    • 校驗密封證明的方法
    • 更細節(jié)的注釋見如下代碼筆者增加的注釋
    • rustverifier實現(xiàn)具體的方法
location: proofs/types.go

▼ package
    proofs

▼ constants
    // merkle根長度
   +CommitmentBytesLen : uint
    // 時空證明挑戰(zhàn)參數(shù)長度:32bytes
   +PoStChallengeSeedBytesLen : uint
    // 密封復制證明長度:384bytes
   +SealBytesLen : uint
    // 時空證明長度:192bytes
   +SnarkBytesLen : uint

    // 原始數(shù)據(jù)的merkle根,由PoRep輸出
 +CommD : []byte

    // 副本數(shù)據(jù)的merkle根,由PoRep輸出
 +CommR : []byte

    // 中間層的merkle根,由PoRep輸出
 +CommRStar : []byte

    // 挑戰(zhàn)隨機參數(shù),32bytes,256bits,PoSt的輸入
 +PoStChallengeSeed : []byte

    // 時空證明輸出,192bytes
 +PoStProof : []byte

    // 密封復制證明,384bytes
 +SealProof : []byte
location: proofs/interface.go

▼ package
    proofs

▼ constants
   +Live
   +Test

 +SectorStoreType : int

    // 校驗時空證明校驗請求
▼+VerifyPoSTRequest : struct
    [fields]
    // 挑戰(zhàn)參數(shù)
   +ChallengeSeed : PoStChallengeSeed
   +CommRs : []CommR
   +Faults : []uint64
   +Proof : PoStProof
   +StoreType : SectorStoreType

▼+VerifyPoSTResponse : struct
    [fields]
   +IsValid : bool

    // 向特定礦工&特定扇區(qū)發(fā)起密封校驗請求
▼+VerifySealRequest : struct
    [fields]
    // 來自于密封的返回參數(shù)
   +CommD : CommD
   +CommR : CommR
   +CommRStar : CommRStar
   +Proof : SealProof
    // 礦工標識
   +ProverID : [31]byte
    // 扇區(qū)ID
   +SectorID : [31]byte
    // 用于控制密封校驗效率
   +StoreType : SectorStoreType

▼+VerifySealResponse : struct
    [fields]
   +IsValid : bool

▼+Verifier : interface
    [methods]
    // 校驗時空證明
   +VerifyPoST(VerifyPoSTRequest) : VerifyPoSTResponse, error
    // 校驗密封證明
   +VerifySeal(VerifySealRequest) : VerifySealResponse, error
location: proofs/rustverifier.go

▼ package
    proofs

? imports

▼ variables
   -log

    // RustVerifier 實現(xiàn)VerifyPoST與VerifySeal接口
▼+RustVerifier : struct
    [methods]
   +VerifyPoST(req VerifyPoSTRequest) : VerifyPoSTResponse, error
   +VerifySeal(req VerifySealRequest) : VerifySealResponse, error

▼ functions
   +CSectorStoreType(cfg SectorStoreType) : *C.ConfiguredStore, error
   -cUint64s(src []uint64) : *C.uint64_t, C.size_t
   -elapsed(what string) : func()
  • sectorbuilder
    • 提供向unsealed扇區(qū)寫入pieces的方法
    • 提供生成時空證明的方法
    • 提供從特定扇區(qū)讀取特定pieces的方法
    • 提供密封完成通知的方法
    • 提供批量密封所有未完成的分段扇區(qū)
    • 與rust-fil-proof交互,更深入的邏輯需要參見rust
location: proofs/sectorbuilder/interface.go

package sectorbuilder

? imports

    // 生成生成時空證明請求
▼+GeneratePoSTRequest : struct
    [fields]
   +ChallengeSeed : proofs.PoStChallengeSeed
   +CommRs : []proofs.CommR

    // 生成生成時空證明響應
▼+GeneratePoSTResponse : struct
    [fields]
   +Faults : []uint64
   +Proof : proofs.PoStProof

▼+PieceInfo : struct
    [fields]
   +Ref : cid.Cid
   +Size : uint64

    // 密封元數(shù)據(jù)
▼+SealedSectorMetadata : struct
    [fields]
   +CommD : proofs.CommD
    // 副本哈希后續(xù)將被刪除
   +CommR : proofs.CommR
   +CommRStar : proofs.CommRStar
    // Pieces后續(xù)將被刪除
   +Pieces : []*PieceInfo
   +Proof : proofs.SealProof
   +SectorID : uint64

    // 密封結(jié)果
▼+SectorSealResult : struct
    [fields]
   +SealingErr : error
   +SealingResult : *SealedSectorMetadata
   +SectorID : uint64

    // SectorBuilder提供相關(guān)功能
    // 1 寫入、密封pieces至扇區(qū)
    // 2 unseal、讀取pieces
▼+SectorBuilder : interface
    [methods]
    // 向unsealed扇區(qū)寫入pieces
   +AddPiece(ctx context.Context, pi *PieceInfo) : uint64, error
   +Close() : error
    // 生成時空證明
   +GeneratePoST(GeneratePoSTRequest) : GeneratePoSTResponse, error
   +GetMaxUserBytesPerStagedSector() : uint64, error
    // 從扇區(qū)中讀取特定pieces
   +ReadPieceFromSealedSector(pieceCid cid.Cid) : io.Reader, error
    // 密封所有未完成的分段扇區(qū)
   +SealAllStagedSectors(ctx context.Context) : error
    // 密封完成的通知
   +SectorSealResults() : chan SectorSealResult

▼ functions
   -init()
location: proofs/sectorbuilder/poller.go

// 當pieces加入后,會進行FFI調(diào)用,定時執(zhí)行密封
const SealedSectorPollingInterval = 1 * time.Second

10.3 type

如下對一些主要結(jié)構(gòu)進行簡析

  • AttoFIL(10*-18 FIL)

    • 提供AttoFIL的算數(shù)運算方法
    • 提供AttoFIL的邏輯運算方法
  • Block

    • 區(qū)塊結(jié)構(gòu)
▼+Block : struct
    [fields]
   +Height : Uint64
   +MessageReceipts : []*MessageReceipt
   +Messages : []*SignedMessage
   +Miner : address.Address
   +Nonce : Uint64
   +ParentWeight : Uint64
   +Parents : SortedCidSet
   +Proof : proofs.PoStProof
   +StateRoot : cid.Cid
   +Ticket : Signature
   -cachedBytes : []byte
   -cachedCid : cid.Cid
    [methods]
   +Cid() : cid.Cid
   +Equals(other *Block) : bool
   +IsParentOf(c Block) : bool
   +Score() : uint64
   +String() : string
   +ToNode() : node.Node
    [functions]
   +DecodeBlock(b []byte) : *Block, error
  • BlockHeight
    • 區(qū)塊高度相關(guān)操作方法
▼+BlockHeight : struct
    [fields]
   -val : *big.Int
    [methods]
   +Add(y *BlockHeight) : *BlockHeight
   +AsBigInt() : *big.Int
   +Bytes() : []byte
   +Equal(y *BlockHeight) : bool
   +GreaterEqual(y *BlockHeight) : bool
   +GreaterThan(y *BlockHeight) : bool
   +LessEqual(y *BlockHeight) : bool
   +LessThan(y *BlockHeight) : bool
   +String() : string
   +Sub(y *BlockHeight) : *BlockHeight
    [functions]
   +NewBlockHeight(x uint64) : *BlockHeight
   +NewBlockHeightFromBytes(buf []byte) : *BlockHeight
   +NewBlockHeightFromString(s string, base int) : *BlockHeight, bool
  • BytesAmount (*big.Int)

    • 提供相關(guān)的算數(shù)邏輯運算
  • ChannelID(支付通道結(jié)構(gòu)體)

▼+ChannelID : struct
    [fields]
   -val : *big.Int
    [methods]
   +Bytes() : []byte
   +Equal(y *ChannelID) : bool
   +Inc() : *ChannelID
   +KeyString() : string
   +String() : string
    [functions]
   +NewChannelID(x uint64) : *ChannelID
   +NewChannelIDFromBytes(buf []byte) : *ChannelID
   +NewChannelIDFromString(s string, base int) : *ChannelID, bool
  • 一些變量定義
    • 創(chuàng)建各類actor對象
func init() {
    AccountActorCodeObj = dag.NewRawNode([]byte("accountactor"))
    AccountActorCodeCid = AccountActorCodeObj.Cid()
    StorageMarketActorCodeObj = dag.NewRawNode([]byte("storagemarket"))
    StorageMarketActorCodeCid = StorageMarketActorCodeObj.Cid()
    PaymentBrokerActorCodeObj = dag.NewRawNode([]byte("paymentbroker"))
    PaymentBrokerActorCodeCid = PaymentBrokerActorCodeObj.Cid()
    MinerActorCodeObj = dag.NewRawNode([]byte("mineractor"))
    MinerActorCodeCid = MinerActorCodeObj.Cid()
    BootstrapMinerActorCodeObj = dag.NewRawNode([]byte("bootstrapmineractor"))
    BootstrapMinerActorCodeCid = BootstrapMinerActorCodeObj.Cid()
}
  • Message相關(guān)
    • 消息結(jié)構(gòu)及方法
    • filecoin網(wǎng)絡的交易由一些列的Message組成
▼+Message : struct
    [fields]
   +From : address.Address
   +Method : string
   +Nonce : Uint64
   +Params : []byte
   +To : address.Address
   +Value : *AttoFIL
    [methods]
   +Cid() : cid.Cid, error
   +Marshal() : []byte, error
   +String() : string
   +Unmarshal(b []byte) : error
    [functions]
   +NewMessage(from, to address.Address, nonce uint64, value *AttoFIL, method string, params []byte) : *Message
▼+MessageReceipt : struct
    [fields]
   +ExitCode : uint8
   +GasAttoFIL : *AttoFIL
   +Return : [][]byte
▼+MeteredMessage : struct
    [fields]
   +GasLimit : GasUnits
   +GasPrice : AttoFIL
    [embedded]
   +Message : Message
    [methods]
   +Marshal() : []byte, error
   +Unmarshal(b []byte) : error
    [functions]
   +NewMeteredMessage(msg Message, gasPrice AttoFIL, gasLimit GasUnits) : *MeteredMessage
▼+SignedMessage : struct
    [fields]
   +Signature : Signature
    [embedded]
   +MeteredMessage : MeteredMessage
    [methods]
   +Cid() : cid.Cid, error
   +Marshal() : []byte, error
   +RecoverAddress(r Recoverer) : address.Address, error
   +String() : string
   +Unmarshal(b []byte) : error
   +VerifySignature() : bool
    [functions]
   +NewSignedMessage(msg Message, s Signer, gasPrice AttoFIL, gasLimit GasUnits) : *SignedMessage, error
  • TipSet
    • 區(qū)塊集合
 +Tip : Block

▼+TipSet : map[cid.Cid]*Tip
    [methods]
   +AddBlock(b *Block) : error
   +Clone() : TipSet
   +Equals(ts2 TipSet) : bool
   +Height() : uint64, error
   +MinTicket() : Signature, error
   +ParentWeight() : uint64, error
   +Parents() : SortedCidSet, error
   +String() : string
   +ToSlice() : []*Block
   +ToSortedCidSet() : SortedCidSet

10.4 abi

  • abi
    • 對filecoin中的各類數(shù)據(jù)定義數(shù)據(jù)類型
    • 提供abi編解碼操作方法

10.5 pubsub

  • 提供功能
    • 提供訂閱實例化以及訂閱方法
    • 提供發(fā)布實例化以及發(fā)布方法
▼ package
    pubsub

? imports

▼+Subscriber : struct
    [fields]
   -pubsub : *libp2p.PubSub
    [methods]
   +Subscribe(topic string) : Subscription, error
    [functions]
   +NewSubscriber(sub *libp2p.PubSub) : *Subscriber

▼-subscriptionWrapper : struct
    [embedded]
   +*libp2p.Subscription : *libp2p.Subscription
    [methods]
   +Next(ctx context.Context) : Message, error

▼+Message : interface
    [methods]
   +GetData() : []byte
   +GetFrom() : peer.ID

▼+Subscription : interface
    [methods]
   +Cancel()
   +Next(ctx context.Context) : Message, error
   +Topic() : string
▼ package
    pubsub

? imports

▼+Publisher : struct
    [fields]
   -pubsub : *pubsub.PubSub
    [methods]
   +Publish(topic string, data []byte) : error
    [functions]
   +NewPublisher(sub *pubsub.PubSub) : *Publisher

[上一篇鏈接] filecoin技術(shù)架構(gòu)分析之九:filecoin源碼分析之支撐包分析(1/2)

[下一篇鏈接] filecoin技術(shù)架構(gòu)分析之十一:filecoin源碼分析之內(nèi)部接口層api包分析

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容