1. 首页
  2. 挖矿

Filecoin技术架构分析之十一:内部接口层api包分析

目录11.filecoin源码分析之内部接口层api包分析

11.1 api

11.2 actor

11.3 address

11.4 client

11.5 config

11.6 daemon

11.7 dag

11.8 id

11.11.log

11.10 miner

11.11 mining

11.12 ping

11.13 retrieval_client

11.14 swarm


api包提供内部接口,供协议层、command/REST使用

较大程度依赖node包

分析版本,go-filecoin版本:master 2c87fd59 (2019.3.7)
11.1 api11.1.1 api的接口定义
如下所示,包含了一系列子接口
typeAPIinterface{

Actor()ActorAddress()AddressClient()ClientDaemon()DaemonDag()DagID()IDLog()LogMiner()MinerMining()MiningPaych()PaychPing()PingRetrievalClient()RetrievalClientSwarm()SwarmVersion()Version

}

11.1.2 api的接口实现▼package

impl

▶imports//nodeAPI来实现其接口定义▼-nodeAPI:struct

[fields]

//合约

-actor:*nodeActor//地址

-address:*nodeAddress//客户端

-client:*nodeClient//daemon

-daemon:*nodeDaemon//dag

-dag:*nodeDag//节点ID

-id:*nodeID//日志

-log:*nodeLog//日志

-logger:logging.EventLogger//矿工

-miner:*nodeMiner//挖矿

-mining:*nodeMining//节点

-node:*node.Node//支付通道

-paych:*nodePaych//ping

-ping:*nodePing//检索客户端

-retrievalClient:*nodeRetrievalClient//swarm

-swarm:*nodeSwarm//版本

-version:*nodeVersion

[methods]//如下为实现API接口

+Actor():api.Actor

+Address():api.Address

+Client():api.Client

+Daemon():api.Daemon

+Dag():api.Dag

+ID():api.ID

+Log():api.Log

+Miner():api.Miner

+Mining():api.Mining

+Paych():api.Paych

+Ping():api.Ping

+RetrievalClient():api.RetrievalClient

+Swarm():api.Swarm

+Version():api.Version

▼functions//实例化API

//1获取高层APIporcelainAPI指针,miner与paych有用到

//2调用各子系统的实例化函数逐一实例化

+New(node*node.Node):api.API

11.2 actor11.2.1 actor的接口定义▼package

api

▶imports

▼+ActorView:struct

[fields]

//actor类型

+ActorType:string

//actor地址

+Address:string

//actor余额

+Balance:*types.AttoFIL//actor代码-CID

+Code:cid.Cid//导出符号集合

+Exports:ReadableExports//表征actor实例的状态

+Head:cid.Cid//消息计数器,仅为accountactors与外部发生交互的时候计算

+Nonce:uint64//导出符号集合

+ReadableExports:map[string]*ReadableFunctionSignature

▼+ReadableFunctionSignature:struct

[fields]

//参数

+Params:[]string

//返回

+Return:[]string▼+Actor:interface//目前接口只有查看功能,返回合约的具体信息

[methods]

+Ls(ctxcontext.Context):[]*ActorView,error

11.2.2 actor的接口实现▼package

impl

▶imports//使用nodeActor来实现Actor接口▼-nodeActor:struct

[fields]

-api:*nodeAPI

[methods]//调用ls方法实现查询功能

+Ls(ctxcontext.Context):[]*api.ActorView,error

[functions]//实例化nodeActor,由api实现代码中调用

-newNodeActor(api*nodeAPI):*nodeActor

▼functions//获取合约类型

//1accountactor

//2存储市场actor

//3支付通道actor

//4矿工actor

//4BootstrapMineractor

-getActorType(actTypeexec.ExecutableActor):string

//查询合约状态

-ls(ctxcontext.Context,fcn*node.Node,actorGetterstate.GetAllActorsFunc):[]*api.ActorView,error

-makeActorView(act*actor.Actor,addrstring,actTypeexec.ExecutableActor):*api.ActorView

-makeReadable(f*exec.FunctionSignature):*api.ReadableFunctionSignature

-presentExports(eexec.Exports):api.ReadableExports

11.3 address提供功能

地址显示方法

地址查找方法

创建地址方法

导出地址方法

导入地址方法

▼package

api▶imports▼+Address:interface

[methods]

+Addrs():Addrs

+Export(ctxcontext.Context,addrs[]address.Address):[]*types.KeyInfo,error

+Import(ctxcontext.Context,ffiles.File):[]address.Address,error▼+Addrs:interface

[methods]

+Lookup(ctxcontext.Context,addraddress.Address):peer.ID,error

+Ls(ctxcontext.Context):[]address.Address,error

+New(ctxcontext.Context):address.Address,error

11.4 client提供如下功能

查询piece数据(DAG格式)

导入数据(相当于ipfs add)

列出所有订单

支付

发起存储交易

查询存储交易

▼+Ask:struct

[fields]

+Error:error

+Expiry:*types.BlockHeight

+ID:uint64

+Miner:address.Address

+Price:*types.AttoFIL▼+Client:interface

[methods]

+Cat(ctxcontext.Context,ccid.Cid):uio.DagReader,error

+ImportData(ctxcontext.Context,dataio.Reader):ipld.Node,error

+ListAsks(ctxcontext.Context):chanAsk,error

+Payments(ctxcontext.Context,dealCidcid.Cid):[]*paymentbroker.PaymentVoucher,error

+ProposeStorageDeal(ctxcontext.Context,datacid.Cid,mineraddress.Address,askuint64,durationuint64,allowDuplicatesbool):*storage.DealResponse,error

+QueryStorageDeal(ctxcontext.Context,propcid.Cid):*storage.DealResponse,error

11.5 config提供功能

Get配置

Set配置

11.6 daemon提供功能

启动进程相关

具体的业务启动逻辑会调用到node包

▼package

api

▶imports

▼+DaemonInitConfig:struct

[fields]

//如果配置,定期检查并密封staged扇区

+AutoSealIntervalSeconds:uint

+DefaultAddress:address.Address//指定网络

+DevnetNightly:bool

+DevnetTest:bool

+DevnetUser:bool

//创世文件

+GenesisFile:string

+PeerKeyFile:string

//repo目录

+RepoDir:string

//指定矿工

+WithMiner:address.Address

+DaemonInitOpt:func(*DaemonInitConfig)

▼+Daemon:interface

[methods]

+Init(ctxcontext.Context,opts…DaemonInitOpt):error

+Start(ctxcontext.Context):error

+Stop(ctxcontext.Context):error

11.7 dag提供功能

dag查询功能

类似ipfs block get

11.8 id提供功能

ID详细信息

如多地址、协议版本、导出公钥等

▼+IDDetails:struct

[fields]

+Addresses:[]ma.Multiaddr

+AgentVersion:string

+ID:peer.ID

+ProtocolVersion:string

+PublicKey:[]byte

[methods]

+MarshalJSON():[]byte,error

+UnmarshalJSON(data[]byte):error▼+ID:interface

[methods]

+Details():*IDDetails,error▼functions

-decode(iddmap[string]*json.RawMessage,keystring,destinterface{}):error

11.11.log提供日志功能

▼+Log:interface

[methods]

+Tail(ctxcontext.Context):io.Reader

11.10 miner创建矿工

▼+Miner:interface

[methods]

+Create(ctxcontext.Context,fromAddraddress.Address,gasPricetypes.AttoFIL,gasLimittypes.GasUnits,pledgeuint64,pidpeer.ID,collateral*types.AttoFIL):address.Address,error

11.11 mining挖矿控制

启动

停止

▼+Mining:interface

[methods]

+Once(ctxcontext.Context):*types.Block,error

+Start(ctxcontext.Context):error

+Stop(ctxcontext.Context):error

11.12 ping提供ping接口

▼+PingResult:struct

[fields]

+Success:bool

+Text:string

+Time:time.Duration▼+Ping:interface

[methods]

+Ping(ctxcontext.Context,pidpeer.ID,countuint,delaytime.Duration):chan*PingResult,error

11.13 retrieval_client提供检索接口

▼+RetrievalClient:interface

[methods]

+RetrievePiece(ctxcontext.Context,pieceCIDcid.Cid,minerAddraddress.Address):io.ReadCloser,error

11.14 swarm提供节点连接功能

显示连接节点

连接节点

查找节点

▼+SwarmConnInfo:struct

[fields]

+Addr:string

+Latency:string

+Muxer:string

+Peer:string

+Streams:[]SwarmStreamInfo

[methods]

+Len():int

+Less(i,jint):bool

+Swap(i,jint)

▼+SwarmConnInfos:struct

[fields]

+Peers:[]SwarmConnInfo

[methods]

+Len():int

+Less(i,jint):bool

+Swap(i,jint)

▼+SwarmConnectResult:struct

[fields]

+Peer:string

+Success:bool▼+SwarmStreamInfo:struct

[fields]

+Protocol:string▼+Swarm:interface

[methods]

+Connect(ctxcontext.Context,addrs[]string):[]SwarmConnectResult,error

+FindPeer(ctxcontext.Context,peerIDpeer.ID):peerstore.PeerInfo,error

+Peers(ctxcontext.Context,verbose,latency,streamsbool):*SwarmConnInfos,error

Filecoin技术架构分析之十一:内部接口层api包分析

53fhas42u3s.png (84.2 KB, 下载次数: 10)

下载附件

53fhas42u3s.png

2019-4-30 11:51 上传

该内容来自于互联网公开内容,非区块链原创内容,如若转载,请注明出处:https://htzkw.com/archives/31800

联系我们

aliyinhang@gmail.com