挖矿网

标题: Filecoin技术架构分析之七:协议层存储协议 [打印本页]

作者: 杨尉    时间: 2019-4-24 15:00
标题: Filecoin技术架构分析之七:协议层存储协议
目录7 filecoin源码协议层分析之存储协议
7.3.1 存储矿工
7.3.2 存储客户
7.1 协议概览图
7.2 源码信息
7.3 源码分析
7.1 协议概览图

storage_protocol.png
7.2 源码信息version
master分支 619b0eb1(2019年3月2日)
package
storage
location
protocol/storage
7.3 源码分析7.3.1 存储矿工▼package
storage
▶imports
▼constants//等待密封数据前缀
-dealsAwatingSealDatastorePrefix//存储交易协议名称:"/fil/storage/mk/1.0.0"
-makeDealProtocol//矿工数据存储前缀
-minerDatastorePrefix//存储查询协议名称:"/fil/storage/qry/1.0.0"
-queryDealProtocol//Gas及Gas限制
-submitPostGasLimit
-submitPostGasPrice//支付通道建立等待时间
-waitForPaymentChannelDuration
▼variables
-log▼+Miner:struct
[fields]
//交易集合
-deals:map[cid.Cid]*storageDeal//等待密封结构体
-dealsAwaitingSeal:*dealsAwaitingSealStruct//交易的资源对象
-dealsDs:repo.Datastore//交易锁
-dealsLk:sync.Mutex//存储矿工地址
-minerAddr:address.Address//节点的Owner地址
-minerOwnerAddr:address.Address//节点对象,有定义存储矿工必须实现的接口
-node:node//存储矿工的高层API
-porcelainAPI:minerPorcelain//是否在生成时空证明中,以及对应的锁
-postInProcess:*types.BlockHeight
-postInProcessLk:sync.Mutex//接受交易以及拒绝交易
-proposalAcceptor:func(ctxcontext.Context,m*Miner,p*DealProposal)*DealResponse,error
-proposalRejector:func(ctxcontext.Context,m*Miner,p*DealProposal,reasonstring)*DealResponse,error
[methods]//密封消息提交到区块链时候,所执行的回调函数,在node中执行
//1失败,则调用dealsAwaitingSeal.fail
//2成功,则调用dealsAwaitingSeal.success
//3成功之后,需要保存密封扇区信息,如果失败调用dealsAwaitingSeal.fail
+OnCommitmentAddedToChain(sector*sectorbuilder.SealedSectorMetadata,errerror)//新区块产生的回调,由node调用,它将会触发新的存储证明
//如果时空证明过期,将会在新的周期重新出发时空证明
+OnNewHeaviestTipSet(tstypes.TipSet)//由handleQueryDeal调用,返回查询结果
+Query(ctxcontext.Context,ccid.Cid):*DealResponse//生成时空证明
-generatePoSt(commRs[]proofs.CommR,challengeproofs.PoStChallengeSeed):proofs.PoStProof,[]uint64,error//获取支付通道信息
//1等待支付通道建立完成
//2获取支付通道信息并返回
//3支付信息包括:合约地址、支付者地址、通道信息、支付通道消息cid、支付凭证合集
-getPaymentChannel(ctxcontext.Context,p*DealProposal):*paymentbroker.PaymentChannel,error//获取新的时空证明时间
-getProvingPeriodStart():*types.BlockHeight,error//获取存储矿工的特定交易
-getStorageDeal(ccid.Cid):*storageDeal//获取存储矿工报价
-getStoragePrice():*types.AttoFIL,error//存储交易请求的入口方法,交易请求流的handle函数
//1读取流中交易请求信息
//2调用receiveStorageProposal处理交易请求
//3回复处理回复
-handleMakeDeal(sinet.Stream)//解析具体流信息,处理查询请求,会调用Query请求
-handleQueryDeal(sinet.Stream)//从资源目录中加载交易信息到Miner实例中
-loadDeals():error//加载待密封的信息
-loadDealsAwaitingSeal():error//密封失败,更新响应信息
-onCommitFail(dealCidcid.Cid,messagestring)//密封成功,更新响应信息
//1切换状态至Posted
//2更新证明信息:扇区ID,副本信息,原始数据信息
-onCommitSuccess(dealCidcid.Cid,sector*sectorbuilder.SealedSectorMetadata)//处理存储交易
//1,获取存储交易信息
//2,数据处理,密封
-processStorageDeal(ccid.Cid)//处理交易请求
//1检查签名的正确性
//2检查支付信息正确性,调用validateDealPayment方法
//3不合法调用proposalRejector(rejectProposal)拒绝请求;合法调用proposalAcceptor(acceptProposal)回复
-receiveStorageProposal(ctxcontext.Context,sp*SignedDealProposal):*DealResponse,error//从Miner对象中存储交易信息到资源目录中
-saveDeal(proposalCidcid.Cid):error//存储待密封信息至资源目录
-saveDealsAwaitingSeal():error//提交时空证明
//1产生随机种子
//2根据时空证明输入长度,生成副本切片
//3随机种子+副本切片作为输入生成时空证明
//4调用高层接口发送消息
-submitPoSt(start,end*types.BlockHeight,inputs[]generatePostInput)//更新交易响应消息
-updateDealResponse(proposalCidcid.Cid,ffunc(*DealResponse)):error//检查支付信息的正确性
//1客户出价必须高于矿工报价
//2收款人必须为本节点矿工
//3支付通道总资金必须大于矿工报价
//4必须有交易凭证,且交易凭证总金额必须大于矿工报价
-validateDealPayment(ctxcontext.Context,p*DealProposal):error
[functions]//实例化存储矿工
//1通过node传参赋值
//2指定密封成功失败的回调函数
//3设置交易请求以及交易查询的流handle方法
+NewMiner(ctxcontext.Context,minerAddr,minerOwnerAddraddress.Address,ndnode,dealsDsrepo.Datastore,porcelainAPIminerPorcelain):*Miner,error
▼-dealsAwaitingSealStruct:struct
[fields]
//从扇区id获取失败信息
+FailedSectors:map[uint64]string
//从扇区id获取交易的cid
+SectorsToDeals:map[uint64][]cid.Cid//从扇区id获取sector元数据
+SuccessfulSectors:map[uint64]*sectorbuilder.SealedSectorMetadata
-l:sync.Mutex//失败处理回调,在实例化Miner指向onCommitFail
-onFail:func(dealCidcid.Cid,messagestring)//成功处理回调,在实例化Miner指向onCommitSuccess
-onSuccess:func(dealCidcid.Cid,sector*sectorbuilder.SealedSectorMetadata)
[methods]//对数据进行密封
-add(sectorIDuint64,dealCidcid.Cid)//密封失败处理dealsAwaitingSeal.onFail
-fail(sectorIDuint64,messagestring)//密封成功处理dealsAwaitingSeal.onSuccess
-success(sector*sectorbuilder.SealedSectorMetadata)
▼-generatePostInput:struct
[fields]
//副本merkle根
-commD:proofs.CommD//原始数据merkle根
-commR:proofs.CommR//中间数据merkle根
-commRStar:proofs.CommRStar//扇区ID
-sectorID:uint64
▼-storageDeal:struct
[fields]
//交易请求结构体
+Proposal:*DealProposal//交易请求响应结构体
+Response:*DealResponse//存储矿工高层API▼-minerPorcelain:interface
[methods]//区块高度
+ChainBlockHeight(ctxcontext.Context):*types.BlockHeight,error//获取配置
+ConfigGet(dottedPathstring):interface{},error//发送、查询、等待消息
+MessageQuery(ctxcontext.Context,optFrom,toaddress.Address,methodstring,params...interface{}):[][]byte,*exec.FunctionSignature,error
+MessageSend(ctxcontext.Context,from,toaddress.Address,value*types.AttoFIL,gasPricetypes.AttoFIL,gasLimittypes.GasUnits,methodstring,params...interface{}):cid.Cid,error
+MessageWait(ctxcontext.Context,msgCidcid.Cid,cbfunc(*types.Block,*types.SignedMessage,*types.MessageReceipt)error):error
▼-node:interface
[methods]//区块高度
+BlockHeight():*types.BlockHeight,error//区块服务,存储/查询服务
+BlockService():bserv.BlockService//区块时间
+GetBlockTime():time.Duration//主机信息
+Host():host.Host//扇区创建,具体包含
//1增加、读取piece;
//2密封所有非空分期扇区
//3密封结果通过返回,通过通道channel的方式
//4获取扇区中最大的piece字节大小
//5生成时空证明
+SectorBuilder():sectorbuilder.SectorBuilder
▼functions//存储交易信息之后,调用processStorageDeal处理交易信息
-acceptProposal(ctxcontext.Context,sm*Miner,p*DealProposal):*DealResponse,error//获取具体文件大小
-getFileSize(ctxcontext.Context,ccid.Cid,dservipld.DAGService):uint64,error
-init()//存储交易信息,更新响应消息,并返回
-rejectProposal(ctxcontext.Context,sm*Miner,p*DealProposal,reasonstring):*DealResponse,error
7.3.2 存储客户▼package
storage
▼imports
▼constants
+ChannelExpiryInterval//Gas及Gas限制
+CreateChannelGasLimit
+CreateChannelGasPrice
+ErrDupicateDeal//建立Voucher的周期
+VoucherInterval//存储前缀
-clientDatastorePrefix
▼variables
+Errors
▼+Client:struct
[fields]
//存储客户高层API
-api:clientPorcelainAPI//交易集合
-deals:map[cid.Cid]*clientDeal//交易资源目录对象及锁
-dealsDs:repo.Datastore
-dealsLk:sync.Mutex//存储客户节点
-node:clientNode
[methods]//加载特定交易的凭证
+LoadVouchersForDeal(dealCidcid.Cid):[]*paymentbroker.PaymentVoucher,error//发起存储交易
//1获取文件大小、矿工报价、区块高度、目的地址
//2建立支付通道
//3调用MakeProtocolRequest发起交易请求
//4检查交易响应
//5持久化交易响应并回复
+ProposeDeal(ctxcontext.Context,mineraddress.Address,datacid.Cid,askIDuint64,durationuint64,allowDuplicatesbool):*DealResponse,error//查询交易
//1获取矿工信息,地址、节点ID
//2调用MakeProtocolRequest发起请求
+QueryDeal(ctxcontext.Context,proposalCidcid.Cid):*DealResponse,error//检查交易响应
-checkDealResponse(ctxcontext.Context,resp*DealResponse):error//判断是否为重复交易
-isMaybeDupDeal(p*DealProposal):bool
//加载交易信息
-loadDeals():error//返回目标矿工地址
-minerForProposal(ccid.Cid):address.Address,error//持久化交易响应
-recordResponse(resp*DealResponse,mineraddress.Address,p*DealProposal):error//保存交易信息
-saveDeal(cidcid.Cid):error
[functions]//实例化存储客户
+NewClient(ndclientNode,apiclientPorcelainAPI,dealsDsrepo.Datastore):*Client,error
▼+ClientNodeImpl:struct
[fields]
-blockTime:time.Duration
-dserv:ipld.DAGService
-host:host.Host
[methods]//实现clientNode接口
+GetBlockTime():time.Duration//获取文件大小
+GetFileSize(ctxcontext.Context,ccid.Cid):uint64,error//发起协议请求
//1建立对应的存储交易或者请求的协议流
//2发起请求
+MakeProtocolRequest(ctxcontext.Context,protocolprotocol.ID,peerpeer.ID,requestinterface{},responseinterface{}):error
[functions]//实例化客户节点
+NewClientNodeImpl(dsipld.DAGService,hosthost.Host,bttime.Duration):*ClientNodeImpl
▼-clientDeal:struct
[fields]
//目标矿工,请求及响应
+Miner:address.Address
+Proposal:*DealProposal
+Response:*DealResponse
▼-clientNode:interface//由ClientNodeImpl实现
[methods]
+GetBlockTime():time.Duration
+GetFileSize(context.Context,cid.Cid):uint64,error
+MakeProtocolRequest(ctxcontext.Context,protocolprotocol.ID,peerpeer.ID,requestinterface{},responseinterface{}):error
▼-clientPorcelainAPI:interface
[embedded]
+types.Signer
[methods]//获取区块高度
+ChainBlockHeight(ctxcontext.Context):*types.BlockHeight,error//创建支付通道
//包括源及目的地址,价格,时间,支付间隔,通道超时时间,Gas及限制
+CreatePayments(ctxcontext.Context,configporcelain.CreatePaymentsParams):*porcelain.CreatePaymentsReturn,error//获取目标地址
+GetAndMaybeSetDefaultSenderAddress():address.Address,error//获取矿工报价
+MinerGetAsk(ctxcontext.Context,minerAddraddress.Address,askIDuint64):miner.Ask,error//获取矿工Owner地址
+MinerGetOwnerAddress(ctxcontext.Context,minerAddraddress.Address):address.Address,error//获取矿工节点ID
+MinerGetPeerID(ctxcontext.Context,minerAddraddress.Address):peer.ID,error
▼functions
-init()
(, 下载次数: 35)