go语言Lotus项目开发实战(Lotus设计、命令行、节点API、源码,urfave/cli、jaeger、mux路由……)

go语言Lotus项目开发实战

  • 〇、Lotus设计文档及节点使用
    • 1、文档
      • 1.1、官方文档
      • 1.2、文档导读、解读
    • 2、名词解释
      • 2.1、CID是什么意思?
      • 2.2、StateTree是什么意思?
      • 2.3、Actor是什么意思?
      • 2.4、StateTree和Actor的关系是什么?
      • 2.5、DAG是什么意思?
      • 2.6、什么是CAR文件(.car)?
      • 2.7、什么是HAMT?
      • 2.8、如何理解TipSet
    • 3、节点使用
    • 4、filecoin数据结构
  • 一、命令行urfave/cli
    • 1、快速入门
    • 2、结合Lotus源码看cli包的运用
  • 二、Jaeger
  • 三、Uber包与依赖注入
  • 四、其他
    • 1、空对象

〇、Lotus设计文档及节点使用

1、文档

1.1、官方文档

参考:Lotus设计文档
参考:Filecoin官方文档(Lotus编译部署使用文档)

1.2、文档导读、解读

区块链- 干货 | 一文了解Lotus网络内消息运转流程

区块链- Filecoin逻辑梳理及源代码导读
上文中的状态机严格说应该为状态树。状态机的状态是针对扇区的。关于状态机,详见:
挖矿 -lotus 源码 —— Sector 状态管理逻辑

挖矿 -深入理解filecoin存储管理
挖矿 -winningPoSt逻辑介绍
挖矿 -IPFS & FileCoin - PoRep和PoSt算法

钱包 -Filecoin 手续费模型-EIP1559

2、名词解释

2.1、CID是什么意思?

Self-describing content-addressed identifiers for distributed systems
分布式系统中的自描述的内容寻址标识符
CID官方文档

个人理解:就是其他链的哈希值。是一笔交易、一个区块的指纹,唯一标识。

2.2、StateTree是什么意思?

参考:设计文档第2.3.2节

The State Tree is the output of the execution of any operation applied on the Filecoin Blockchain.
状态树是Filecoin区块链上实现的任何操作执行后的输出。

The on-chain (i.e., VM) state data structure is a map (in the form of a Hash Array Mapped Trie - HAMT) that binds addresses to actor states.

链上状态数据结构是一个map(形式为哈希数组地图树-HAMT)用于绑定地址到actor的状态。

The current State Tree function is called by the VM upon every actor method invocation.

当前状态树函数被虚拟机调用在每一个actor方法实施时。

//lotus/chain/state/statetree.go文件中定义了:
//StateTree stores actors state by their ID.
type StateTree struct {
	root        adt.Map
	version     types.StateTreeVersion
	info        cid.Cid
	Store       cbor.IpldStore
	lookupIDFun func(address.Address) (address.Address, error)

	snaps *stateSnaps
}

2.3、Actor是什么意思?

参考:设计文档1.3节

Actors are virtual entities embodied in the state of the Filecoin VM. Protocol actors are analogous to participants in smart contracts; an actor carries a FIL currency balance and can interact with other actors via the operations of the VM, but does not necessarily correspond to any particular node or software component.

Actor是体现在Filecoin VM状态中的虚拟实体。协议参与者类似于智能合约的参与者。一个参与者具有FIL货币余额,并且可以通过VM的操作与其他参与者进行交互,但不一定对应于任何特定的节点或软件组件。

//Lotus/chain/types/actor.go
type Actor struct {
	// Identifies the type of actor (string coded as a CID), see `chain/actors/actors.go`.
	Code    cid.Cid
	Head    cid.Cid
	Nonce   uint64
	Balance BigInt//余额
}

5.2、Actor
The Actor is the Filecoin equivalent of the smart contract in Ethereum.

Filecoin上的Actor相当于Ethereum以太坊上的智能合约。

An actor is an on-chain object with its own state and set of methods. An actor’s state is persisted in the on-chain state tree, keyed by its address.

一个Actor是一个链上对象,它有自己的状态和设置方法。参与者的状态被持久化在链上状态树中,以其地址为键值。(一个Actor的状态保存在链上状态树,我们通过Actor的地址作为key找到Actor的状态。)

All actors (miner actors, the storage market actor, account actors) have an address. Actor’s methods are invoked by crafting messages and getting miners to include them in blocks.

所有Actor(矿工Actor、存储市场Actor、帐户Actor)都有一个地址。Actor的方法是通过创建消息并让矿工将它们包含在块中来调用的。

There eleven (11) builtin System Actors in total in the Filecoin System.

Filecoin系统中总共有11个内置系统Actor。

2.4、StateTree和Actor的关系是什么?

Filecoin区块链作为一个分布式系统,各个节点都会保有全网状态StateTree的一个副本,节点间的状态需要彼此进行同步,同步的方式就是发送和执行消息。Filecoin中的消息来源有两种:一是节点自身发出、二是从P2P网络中同步其它节点发出的消息。

StateTree维护了一个大的Map,上面记录了所有Actor的地址和状态。

2.5、DAG是什么意思?

2.6、什么是CAR文件(.car)?

Content Addressable aRchives
可内容寻址文件
CAR官方文档

2.7、什么是HAMT?

HAMT是状态树的树状数据结构。

文档6.2.2

See the draft IPLD hash map specfor details on implementing the HAMT used for the global state tree map and throughout the actor code.

有关详细信息,请参阅IPLD散列映射规范草案实施HAMT用于全局状态树映射和整个Actor的代码。

2.8、如何理解TipSet

TipSet是区块集的意思。filecoin区块链上可能有多个区块处于同一高度,他们有共同的父区块,这样的一组区块组成一个TipSet。

3、节点使用

Lotus官方API文档
Lotus官方API-token

证明参数路径:
/var/tmp/filecoin-proof-parameters
chaindata路径:
~/.lotus/datastore/chain

1、生成地址并查看

lotus wallet new bls
生成了三个测试网上的钱包地址,用lotus wallet list查看:
Address                                                                                 Balance  Nonce  Default  
t3suzgpfqm6ofdk4olqoszlyqckfkn7himxdqphn44kjcen7kilzxg2q6l4hkqpndejebmgnjglcolrnrkkgwa  0 FIL    0               
t3tb2womxekgq5mha2yzgujndi5x6fy5pocsf4xjt6bldpajrc362kjqblog43hf4h5gbtwajipifiury5szwa  0 FIL    0      X        
t3wzcvnv7w3m7scgyzhkbxc2bgpzjk73ve6labtg323bcixn7es4f63tyvzaz5krgydqquyv7ekxm34coilrda  0 FIL    0  

2、向水龙头申请币

开启VPN
地址t3tb2womxekgq5mha2yzgujndi5x6fy5pocsf4xjt6bldpajrc362kjqblog43hf4h5gbtwajipifiury5szwa
向水龙头申请币send fund之后:收到的cid
bafy2bzaceaz6uet7qimrqtpkdw55vrmcwebvlmowrea52zervg4ka5rwr4zoq


3、查询余额

用balance命令查余额:
lotus wallet balance t3tb2womxekgq5mha2yzgujndi5x6fy5pocsf4xjt6bldpajrc362kjqblog43hf4h5gbtwajipifiury5szwa
5000fil


4、转账

root@iZ8vbfh73l8663gu8trbveZ:~# lotus send --from t3tb2womxekgq5mha2yzgujndi5x6fy5pocsf4xjt6bldpajrc362kjqblog43hf4h5gbtwajipifiury5szwa t3wzcvnv7w3m7scgyzhkbxc2bgpzjk73ve6labtg323bcixn7es4f63tyvzaz5krgydqquyv7ekxm34coilrda 3.1415
bafy2bzacecr2poxbw2xvkn2qvrkycb2ii25i3bkflejjvf3fntw7ebxiyo7iu

5、导出私钥

lotus wallet export t3rxcf2xuohit4it6jtymwetwfeh4u2lrcjdqnc2t5obxoeatldtj6hfk7ouoskygjgrucexjbz7szw6q7saga
7b2254797065223a22626c73222c22507269766174654b6579223a2247664c4d784f51376c594c6b4a3262397047746667456a55627430464e376a694d59386b46572f6130544d3d227d

lotus wallet export t3tb2womxekgq5mha2yzgujndi5x6fy5pocsf4xjt6bldpajrc362kjqblog43hf4h5gbtwajipifiury5szwa
7b2254797065223a22626c73222c22507269766174654b6579223a2277536c4e78654632636b7256365444732b682b34794f524f30626c32716568537a6f56627255764248416b3d227d



6、查链上数据

root@iZ8vbfh73l8663gu8trbveZ:~# lotus chain getblock bafy2bzacedzkpx5fbs2ioeozy4hqapxo443gq7wdutn7cogsujpfv4xh6ybes
{
  "Miner": "t01220",
  "Ticket": {
    "VRFProof": "oVQwZUxHSME7i98vuEMkmHAPEym852OvB/JQshXpNKxfhtAGNLN3whuQe2yQIAsnCPHqngCAyvnzLoR0iNFB31bImhIKzIFkCv3M1O3j79pTxAzCgMWr+Ydk9QEiH9ZB"
  },
  "ElectionProof": {
    "WinCount": 1,
    "VRFProof": "sQEBJ/4AB8EE4XIe/EOaaghT2dK3Y0KdmsQNHl4QhnbkSz+/uQzRqWhsjS4VOaV1Cjf5VuL49c2W0wyqh9cmmdGZ4OUpLVB6r0Ovj58BoxdR+qrx5QI49BJlTs5qHDNC"
  },
  "BeaconEntries": [
    {
      "Round": 831373,
      "Data": "ofjINvG0OKahp4rBmFUJE4YSXxFi386IlBufJTtBPqVeMxR+x4GgiztR+lQT9Y/EBozsbvi58NfArId6+5btIjW3kPahBnvyceex6bFzSV5qvdGBePMJHCvD0g77Zxbd"
    }
  ],
  "WinPoStProof": [
    {
      "PoStProof": 3,
      "ProofBytes": "uaQUF2wsOEKr2MiE1hUOIdkQ+eaC97FsEsT0jJZWA3Tc4Iko/iXu7DXrTHUJJTgpq04yVDX4VyhRUlrMfziy53FMATF/P498t3rC1DKpABPQZP3uigKZBmmASG4wHHnuELNJUrDVMR2eUy2gyfonKQ1Bmenm2J57RFhhLPZDOSrTczVIn6fhdrX+/7o39mjVtpJ47SCDPn6xjDiOT7ZI9ABtw+tab0J6LYiLn9FABQ5bXvQuY8V1zEB8MuVQZp/E"
    }
  ],
  "Parents": [
    {
      "/": "bafy2bzacedwgoam5wvghx7x5n6gjz67m3scs5xtvuvtifbgxi5mod5pdh3d2i"
    },
    {
      "/": "bafy2bzacebmbrpicw3qcua4kizkajw6dvogejhcfbkp7owq7qkqjj7fgpgktk"
    }
  ],
  "ParentWeight": "4019853704",
  "Height": 219869,
  "ParentStateRoot": {
    "/": "bafy2bzacecqkaitzs4hrrmnc5qpphfukigav5tbf5tqzjoes5rgbuhb6sec2o"
  },
  "ParentMessageReceipts": {
    "/": "bafy2bzacedswlcz5ddgqnyo3sak3jmhmkxashisnlpq6ujgyhe4mlobzpnhs6"
  },
  "Messages": {
    "/": "bafy2bzacecmda75ovposbdateg7eyhwij65zklgyijgcjwynlklmqazpwlhba"
  },
  "BLSAggregate": {
    "Type": 2,
    "Data": "wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  },
  "Timestamp": 1620372270,
  "BlockSig": {
    "Type": 2,
    "Data": "kyBg94RtHPzxWWv44YBi7jihhMxa9ULEJ5sFnavS4EcZZe1kigwYjuePpeDBUD/2BUFAcIGZdSN5RC0C4h4LJrpqEDJ2ozlyqyOs5ikzNcCoiInsqpv0zxBBE1n5moHV"
  },
  "ForkSignaling": 0,
  "ParentBaseFee": "100",
  "BlsMessages": [],
  "SecpkMessages": [],
  "ParentReceipts": null,
  "ParentMessages": []
}

=== === === ===
root@iZ8vbfh73l8663gu8trbveZ:~# lotus chain getblock bafy2bzacecixmekx6roukx33qhf7dck3fezsfv3hs3pnw75tqib3lmycta7ym
{
  "Miner": "t01002",
  "Ticket": {
    "VRFProof": "he+QLexh/+oZ2w+tDdRneLCzJZ8xdU5KOcnA/nOZzv7auZC2o9I52z6Rt/Wf+rjHB3cSdjnmAxJeJjPGT5LTCSqHRiSyLtKv714dKLnrZs6KKptetWc06h35yp2TcSZd"
  },
  "ElectionProof": {
    "WinCount": 2,
    "VRFProof": "hfxbUuCZgiLcR9ojO+TcA2Q11qmK16EJOyehhewC7hAZUZx4eM1B/TZBRAJIi/yQFLlOA2lu1YahpHHBDYH2xx9836qKHSt8rPuVHnWELYV0c7bRcMIZ0SlhAZptySej"
  },
  "BeaconEntries": [
    {
      "Round": 831399,
      "Data": "gsrC16vVY+bRcpWu92wPqPRfA5Vc9KNO4Ny1U8ZD+XhdpYPPH3gLPyTVS/k/qLhdGSXpkPn/Y72rInW1FXzQ3A8yDkSEBwvbOIX8Bnul4uLtlyV2ZIs7mCTQF/F1d32G"
    }
  ],
  "WinPoStProof": [
    {
      "PoStProof": 3,
      "ProofBytes": "kTSCPzGC0y8BHGgCkF/LoLDBe2yWwRePmzVA729klYhMSQct36EijJacmAb+EaK1sCYWrXYMPlgPmUu0q1Ka68tyPlzBfQZ4Sgyie6X6YahXtuUMJ/ODrtDvEv90KCWHDRkEqVJJsc177iTQez6BjSRi2xxXLO/xOioLVPRJE674/14V6R2wtkC0pjRdhrwyllXuMTFOWz3FKV7xoUzkcEuTLXJ6lXCay/nuSjFA8wK4afFYSbr+U/3a2E/QOGMM"
    }
  ],
  "Parents": [
    {
      "/": "bafy2bzaceaoettifgvquvngbz6ijlbr3wnefzcnwlizbm2ouh5xcqorkyvn74"
    },
    {
      "/": "bafy2bzaced7antoasz4ultdcjhqbuudjaqzfmh44gjwvooup7yxfybnhqit24"
    }
  ],
  "ParentWeight": "4020324091",
  "Height": 219895,
  "ParentStateRoot": {
    "/": "bafy2bzacedr4wsubkve7yjm7tjdxdhoozig74vbqhi2cl656jsrb2gnzfnzj4"
  },
  "ParentMessageReceipts": {
    "/": "bafy2bzacedswlcz5ddgqnyo3sak3jmhmkxashisnlpq6ujgyhe4mlobzpnhs6"
  },
  "Messages": {
    "/": "bafy2bzacecioubc4o7f7ushan3xbkxvghcbd4ck7eodp2oe2iyxefhegygrza"
  },
  "BLSAggregate": {
    "Type": 2,
    "Data": "rXdjE7v00eVu78ly0LAD4UwRbMQ2mbMvNGs/e7uT4bMnz+Wy+UA4DhwC4kF2C4umC3CFbpnIy/lxX6Cc83SK90snRvcFNrWuPvI1jHX1fTcaBVIIWzJox6Ia7izwCUSx"
  },
  "Timestamp": 1620373050,
  "BlockSig": {
    "Type": 2,
    "Data": "sPvm3j06UgylOUEkpgK4e/lqdNbrmqKo/VMEQ+ybNY7t4JTb/p0Id/I6Rfwt6JCvFDZu3bUZD2REsNLaoer0yIFcaH469h2fUawSL4CA+kvfsEQ8sEvSfatQThk6N0ny"
  },
  "ForkSignaling": 0,
  "ParentBaseFee": "100",
  "BlsMessages": [
    {
      "Version": 0,
      "To": "t05",
      "From": "t3v6dlxsu26rnqrrjdenx73knuse5bsb3poiajrvcshljdwxb6pecbf55dy36lcgrtuftubmmwitc64pshio5q",
      "Nonce": 1880,
      "Value": "0",
      "GasLimit": 42812733,
      "GasFeeCap": "100273",
      "GasPremium": "99219",
      "Method": 4,
      "Params": "gYGCi9gqWCgAAYHiA5IgIKn7EHcSjx5Xj2ZoQZX4mVO+CcHR2WXtfHZEiQzWwlkLGUAA9FgxA6vklQoZdiZ/pY5z5WzGYqnqZdpNXdA5HgYjzCjG5iBsSImiIh57PiBkCSVagpyM+0MAxAl4NG1BVldnNUFJZzUyWkZsb1M1akUrOWtVVklGaGczM0kxc0xXZjVvYVdkQmVrb2tuVGp5b1EaAANx7BoADD+nQEYAB9xUoL5AWGECrcebdKcqVCdSbKeAX6/N+Pei25jnR1DzQWVkyzw+5kDBILopYO3algMDJt+BRazPE/nKnVqQHFLbNHwoKyIt1kRp2L0mJHlmMtzRhHv8LkVF3VzV+babLZuMZ/wtywSu",
      "CID": {
        "/": "bafy2bzacean6ykdfirj23rf5epz6f6rctummpfdg6csikxizop2gmjtotr3pq"
      }
    }
  ],
  "SecpkMessages": [],
  "ParentReceipts": null,
  "ParentMessages": []
}

7、根据cid查询交易

root@iZ8vbfh73l8663gu8trbveZ:~# lotus chain getmessage bafy2bzaceaz6uet7qimrqtpkdw55vrmcwebvlmowrea52zervg4ka5rwr4zoq
{
  "Message": {
    "Version": 0,
    "To": "t3tb2womxekgq5mha2yzgujndi5x6fy5pocsf4xjt6bldpajrc362kjqblog43hf4h5gbtwajipifiury5szwa",
    "From": "t1ocoat2yl2pmu2t5i7hlssdlylbp6tp5eapetv5y",
    "Nonce": 5898,
    "Value": "5000000000000000000000",
    "GasLimit": 2729022,
    "GasFeeCap": "100812",
    "GasPremium": "99758",
    "Method": 0,
    "Params": null,
    "CID": {
      "/": "bafy2bzacea7clkwxhajbjq5tqvjhztnrpdtsb2hficcl4rsjwphveq36eclq2"
    }
  },
  "Signature": {
    "Type": 1,
    "Data": "11/MvWQzBhBuRNRgpaApQl9Rdby/NHK+pNHsMQBC41VxszsBDVNYSVDy6K+txpfmSi6ZKxRBpprVn7zWdZUVnAE="
  },
  "CID": {
    "/": "bafy2bzaceaz6uet7qimrqtpkdw55vrmcwebvlmowrea52zervg4ka5rwr4zoq"
  }
}

=== === === === 

root@iZ8vbfh73l8663gu8trbveZ:~# lotus chain getmessage bafy2bzacecr2poxbw2xvkn2qvrkycb2ii25i3bkflejjvf3fntw7ebxiyo7iu
{
  "Version": 0,
  "To": "t3wzcvnv7w3m7scgyzhkbxc2bgpzjk73ve6labtg323bcixn7es4f63tyvzaz5krgydqquyv7ekxm34coilrda",
  "From": "t3tb2womxekgq5mha2yzgujndi5x6fy5pocsf4xjt6bldpajrc362kjqblog43hf4h5gbtwajipifiury5szwa",
  "Nonce": 0,
  "Value": "3141500000000000000",
  "GasLimit": 2657522,
  "GasFeeCap": "101106",
  "GasPremium": "100052",
  "Method": 0,
  "Params": null,
  "CID": {
    "/": "bafy2bzacecr2poxbw2xvkn2qvrkycb2ii25i3bkflejjvf3fntw7ebxiyo7iu"
  }
}

=== === === ===

root@iZ8vbfh73l8663gu8trbveZ:~# lotus chain getmessage bafy2bzacean6ykdfirj23rf5epz6f6rctummpfdg6csikxizop2gmjtotr3pq
{
  "Version": 0,
  "To": "t05",
  "From": "t3v6dlxsu26rnqrrjdenx73knuse5bsb3poiajrvcshljdwxb6pecbf55dy36lcgrtuftubmmwitc64pshio5q",
  "Nonce": 1880,
  "Value": "0",
  "GasLimit": 42812733,
  "GasFeeCap": "100273",
  "GasPremium": "99219",
  "Method": 4,
  "Params": "gYGCi9gqWCgAAYHiA5IgIKn7EHcSjx5Xj2ZoQZX4mVO+CcHR2WXtfHZEiQzWwlkLGUAA9FgxA6vklQoZdiZ/pY5z5WzGYqnqZdpNXdA5HgYjzCjG5iBsSImiIh57PiBkCSVagpyM+0MAxAl4NG1BVldnNUFJZzUyWkZsb1M1akUrOWtVVklGaGczM0kxc0xXZjVvYVdkQmVrb2tuVGp5b1EaAANx7BoADD+nQEYAB9xUoL5AWGECrcebdKcqVCdSbKeAX6/N+Pei25jnR1DzQWVkyzw+5kDBILopYO3algMDJt+BRazPE/nKnVqQHFLbNHwoKyIt1kRp2L0mJHlmMtzRhHv8LkVF3VzV+babLZuMZ/wtywSu",
  "CID": {
    "/": "bafy2bzacean6ykdfirj23rf5epz6f6rctummpfdg6csikxizop2gmjtotr3pq"
  }
}
root@iZ8vbfh73l8663gu8trbveZ:~# 



8、用curl请求更多信息

8.1、官方示例:获取当前最新区块
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(cat ~/.lotus/token)" \
--data '{ "jsonrpc": "2.0", "method": "Filecoin.ChainHead", "params": [], "id": 1 }' \
'http://127.0.0.1:1234/rpc/v0'

8.2、通过消息cid查询消息(一笔交易)

入参:
bafy2bzacecr2poxbw2xvkn2qvrkycb2ii25i3bkflejjvf3fntw7ebxiyo7iu
bafy2bzacecmda75ovposbdateg7eyhwij65zklgyijgcjwynlklmqazpwlhba


curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(cat ~/.lotus/token)" \
--data '{ "jsonrpc": "2.0", "method": "Filecoin.StateSearchMsg", "params": [{ "/": "bafy2bzacecr2poxbw2xvkn2qvrkycb2ii25i3bkflejjvf3fntw7ebxiyo7iu"}], "id": 1 }' \
'http://127.0.0.1:1234/rpc/v0'

结果:
{"jsonrpc":"2.0","result":{"Message":{"/":"bafy2bzacecr2poxbw2xvkn2qvrkycb2ii25i3bkflejjvf3fntw7ebxiyo7iu"},"Receipt":{"ExitCode":0,"Return":null,"GasUsed":2126018},"ReturnDec":null,"TipSet":[{"/":"bafy2bzacebttztgti4auou5k3zsguhq3iukyn7fhhlgnn753ak33qrw6cjsyg"},{"/":"bafy2bzacea6ke6xtf2odiqueomsrtbd3jat2lrbhrtfqqrpv4qfq42jqmp34m"}],"Height":219139},"id":1}



8.3、通过区块高度查询区块集(TipSet)

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(cat ~/.lotus/token)" \
--data '{ "jsonrpc": "2.0", "method": "Filecoin.ChainGetTipSetByHeight", "params": [219139,null], "id": 1 }' \
'http://127.0.0.1:1234/rpc/v0'


结果:
{"jsonrpc":"2.0","result":{"Cids":[{"/":"bafy2bzacebttztgti4auou5k3zsguhq3iukyn7fhhlgnn753ak33qrw6cjsyg"},{"/":"bafy2bzacea6ke6xtf2odiqueomsrtbd3jat2lrbhrtfqqrpv4qfq42jqmp34m"}],"Blocks":[{"Miner":"t01002","Ticket":{"VRFProof":"iO/HB1JXixfwwsGLbWZVktcADmJCR0h5RO20YMekmuc/ohfiX+F+1yWFh6EoGzxiC0ZGLsJTKxN0J9z6xUm5FKF2RKFzcoOWvQk1205Wc5jzy2Ser2bG41jvm8flRZNV"},"ElectionProof":{"WinCount":2,"VRFProof":"rUPTm3ZW17zvV/1Xqis/Qj5S7asvVkdzwmQ3RAroHDzw76MnMpML506L05xVLNyrGBEGi6As/sa4R1N9YIf9SXp5Abhz+J+u2nSSFIR3uibMz2ENZmjEuuDgPT2fEgzh"},"BeaconEntries":[{"Round":830643,"Data":"l2AdE/wUm9wTx3nMHoWUNiwE2RPqOc+oTX3ob0z11K0uUsRX+NKUfkUCYmYtwgWkDeBRXYZPASFC8+oxMHp+nyVJmLvRYuylcOOdzWxBfAOrzRA9F1GZdQnrLPwwKUNK"}],"WinPoStProof":[{"PoStProof":3,"ProofBytes":"rfwDM8itmevUPI/Xear1WEwRTipz/5XLtj3h0IZYRay1NcDx6SF2HNhLMpY1D01korJaECirKt3jz3Q3vmkWL9SAwSorV7BygGCrr2brWkOxlumt1uks536Luw7oHywSDbWMfTvz6VdqrIxxcClBgKmeRCju9oIxekmiD9TKO5z4UXIOf7jyC+9YoH/Lkoq2r1o+GWkC5YnX4mQYvggwjNPUtmgkohZ9zm7TCWsS8TLlWmdMa7+BqjcCTmIQ96u2"}],"Parents":[{"/":"bafy2bzacebbkrm7yhpfhmwlmjcp2byjcnod24cpae6p4ur2z27zug6tsvfjuk"},{"/":"bafy2bzacecewcbb4m6t3aazffwlv2rik66uaycm6b6uwjxjkzqkuvqyg63svk"},{"/":"bafy2bzaceavevvmoz32navv4fw47nhm2u3l7ogqt6nme67t6qwv6r2pbyk3i2"},{"/":"bafy2bzacebfeebawuutmsyg5lm655fp5xrffofiixgmngnlbrzmyy2mrebirg"}],"ParentWeight":"4006662714","Height":219139,"ParentStateRoot":{"/":"bafy2bzaceaq6zrnh2xwypbvpkprymyqx2744nf5teaezl6b7ea3rthwji2ib4"},"ParentMessageReceipts":{"/":"bafy2bzaceaq3rlk5k6waekfjnfottdc3lq4blswd3jtgb3lzt6434nv4xidbm"},"Messages":{"/":"bafy2bzacecmda75ovposbdateg7eyhwij65zklgyijgcjwynlklmqazpwlhba"},"BLSAggregate":{"Type":2,"Data":"wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"},"Timestamp":1620350370,"BlockSig":{"Type":2,"Data":"hOG62MxgNoT+IgPKi5Iq7E38qJM6DY1rSV253dQt6ymvvwaZ7Cowzd4c4qilziFNFmOdok6eks0QcaxWJpSxZsIi3Jpb6AtDuyLQ8f9RFV8zsbzwcYYsOmkC9CmohOXk"},"ForkSignaling":0,"ParentBaseFee":"100"},{"Miner":"t01220","Ticket":{"VRFProof":"h6Z8DTSsXn3w6UEyxW6X5zrALPXg8TzRn5D+YJRy63EojmdmIjnUNvIPSwOo3aoMABk9laVWilSSSUZzrQkQkndDhTh0BVN+r1/jLkMMlETX8xLrud61crskVAW8pmq3"},"ElectionProof":{"WinCount":2,"VRFProof":"r6iyyRoycjLsvazLG+CK9fj7hJ4FidPh5uju1Au6lXz5nc/7Qb8NUlUmrDP6I5t2FC/Az51GFEOqzT3uwBfcJz9Uu3M1Y5J7Yppz6Ty2N7CBc7uFobsKEWSAMv20Kz9M"},"BeaconEntries":[{"Round":830643,"Data":"l2AdE/wUm9wTx3nMHoWUNiwE2RPqOc+oTX3ob0z11K0uUsRX+NKUfkUCYmYtwgWkDeBRXYZPASFC8+oxMHp+nyVJmLvRYuylcOOdzWxBfAOrzRA9F1GZdQnrLPwwKUNK"}],"WinPoStProof":[{"PoStProof":3,"ProofBytes":"jNO6sYCD3hI112tJJIJey98nHuBvL6e5qRmjuI7ZVPqsBXKDYV5+8TRV8TR3SscWi/va2k3fkFVU1S7fpkuicT9agZglnZMpQ2URT576xumXRIJAJPL1vcVnvI1TjROCEFqp2qF3vRgmeNKpxIXU9UROG/i2vBuH5PHLHqdMABhfVDKyLDuIGlF2WvQ82JAKtak55zvZXImieSBEqYEG/HqGscv/zdQtodRO0CVyXd6nBNtJwT99wZvUoGepzzZK"}],"Parents":[{"/":"bafy2bzacebbkrm7yhpfhmwlmjcp2byjcnod24cpae6p4ur2z27zug6tsvfjuk"},{"/":"bafy2bzacecewcbb4m6t3aazffwlv2rik66uaycm6b6uwjxjkzqkuvqyg63svk"},{"/":"bafy2bzaceavevvmoz32navv4fw47nhm2u3l7ogqt6nme67t6qwv6r2pbyk3i2"},{"/":"bafy2bzacebfeebawuutmsyg5lm655fp5xrffofiixgmngnlbrzmyy2mrebirg"}],"ParentWeight":"4006662714","Height":219139,"ParentStateRoot":{"/":"bafy2bzaceaq6zrnh2xwypbvpkprymyqx2744nf5teaezl6b7ea3rthwji2ib4"},"ParentMessageReceipts":{"/":"bafy2bzaceaq3rlk5k6waekfjnfottdc3lq4blswd3jtgb3lzt6434nv4xidbm"},"Messages":{"/":"bafy2bzacecmda75ovposbdateg7eyhwij65zklgyijgcjwynlklmqazpwlhba"},"BLSAggregate":{"Type":2,"Data":"wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"},"Timestamp":1620350370,"BlockSig":{"Type":2,"Data":"tIrJr6M8rwMZ3sZgQlwUjKJR2QPZkIe7kg+HNN7YAtYmYVXonnF1YHwIePmgKQAnGA2u6rDpRw5SoGrU/biVn6Plbe1zQBBfY4xcaPm/47PLtVc5WOG6LXiKD8zifPD9"},"ForkSignaling":0,"ParentBaseFee":"100"}],"Height":219139},"id":1}


8.4、通过区块cid查询区块中所有消息??

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(cat ~/.lotus/token)" \
--data '{ "jsonrpc": "2.0", "method": "Filecoin.ChainGetBlockMessages", "params": [{ "/": "bafy2bzacebfeebawuutmsyg5lm655fp5xrffofiixgmngnlbrzmyy2mrebirg"}], "id": 1 }' \
'http://127.0.0.1:1234/rpc/v0'


结果:
{"jsonrpc":"2.0","result":{"BlsMessages":[{"Version":0,"To":"t3wzcvnv7w3m7scgyzhkbxc2bgpzjk73ve6labtg323bcixn7es4f63tyvzaz5krgydqquyv7ekxm34coilrda","From":"t3tb2womxekgq5mha2yzgujndi5x6fy5pocsf4xjt6bldpajrc362kjqblog43hf4h5gbtwajipifiury5szwa","Nonce":0,"Value":"3141500000000000000","GasLimit":2657522,"GasFeeCap":"101106","GasPremium":"100052","Method":0,"Params":null,"CID":{"/":"bafy2bzacecr2poxbw2xvkn2qvrkycb2ii25i3bkflejjvf3fntw7ebxiyo7iu"}}],"SecpkMessages":[],"Cids":[{"/":"bafy2bzacecr2poxbw2xvkn2qvrkycb2ii25i3bkflejjvf3fntw7ebxiyo7iu"}]},"id":1}



update MySQL.user set authentication_string=password('root') where user='root' ;

8.5、发起一笔转账
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(cat ~/.lotus/token)" \
--data '{ "jsonrpc": "2.0", "method": "Filecoin.MpoolPushMessage", "params": [
  {
    "Version": 0,
    "To": "t3suzgpfqm6ofdk4olqoszlyqckfkn7himxdqphn44kjcen7kilzxg2q6l4hkqpndejebmgnjglcolrnrkkgwa",
    "From": "t3u4dpy2kxtl2t76j2mpnntst6377p5aunpcffqklrqxwndxkq74dhmndjmxyejlm6g44cbsmaacutpxxug7ua",
    "Nonce": 0,
    "Value": "60999999736565481513",
    "GasLimit": 0,
    "GasFeeCap": "0",
    "GasPremium": "0",
    "Method": 0
  },
  null
], "id": 1 }'


4、filecoin数据结构

//TODO

一、命令行urfave/cli

1、快速入门

打开goland把下面的代码复制到testCli.go文件中,运行go mod init和go mod tidy拉取依赖。

package main

import (
	"fmt"
	"github.com/urfave/cli"
	"log"
	"os"
)

func main() {
	//实例化一个命令行程序
	oApp := cli.NewApp()
	//程序名称
	oApp.Name = "GoTool"
	//程序的用途描述
	oApp.Usage = "To save the world"
	//程序的版本号
	oApp.Version = "1.0.0"

	//设置多个命令处理函数
	oApp.Commands = []cli.Command{
		{
			//命令全称
			Name: "lang",
			//命令简写
			Aliases: []string{"l"},
			//命令详细描述
			Usage: "Setting language",
			//命令处理函数
			Action: func(c *cli.Context) {
				// 通过c.Args().First()获取命令行参数
				fmt.Printf("language=%v \n", c.Args().First())
			},
		},
		{
			Name:    "encode",
			Aliases: []string{"e"},
			Usage:   "Setting encoding",
			Action: func(c *cli.Context) {
				fmt.Printf("encoding=%v \n", c.Args().First())
			},
		},
	}
	//启动
	if err := oApp.Run(os.Args); err != nil {
		log.Fatal(err)
	}
	/*
	 [root@localhost cli]# go run main.go l english
	 language=english
	 [root@localhost cli]# go run main.go e utf8
	 encoding=utf8
	 [root@localhost cli]# go run main.go help
	 NAME:
	 GoTool - To save the world
	 USAGE:
	 main [global options] command [command options] [arguments...]
	 VERSION:
	 1.0.0
	 COMMANDS:
	 lang, l Setting language
	 encode, e Setting encoding
	 help, h Shows a list of commands or help for one command

	 GLOBAL OPTIONS:
	 --help, -h  show help
	 --version, -v print the version
	*/
}

命令行运行:go run testCli.go lang English
显示结果: language=English

2、结合Lotus源码看cli包的运用

2.1、cli.Context传递了cli命令行中赋予的参数值。
Action中可以从通过ctx取出来。
2.2、Flag规定了命令行的参数。
Flag在Lotus中常用的有StringFlag和BoolFlag两种类型。

Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "actor",
			Usage: "specify the address of an already created miner actor",
		},
		//...
		}

2.3miner中的Flag:
init命令:

Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "actor",
			Usage: "specify the address of an already created miner actor",
		},
		&cli.BoolFlag{
			Name:   "genesis-miner",
			Usage:  "enable genesis mining (DON'T USE ON BOOTSTRAPPED NETWORK)",
			Hidden: true,
		},
		&cli.BoolFlag{
			Name:  "create-worker-key",
			Usage: "create separate worker key",
		},
		&cli.StringFlag{
			Name:    "worker",
			Aliases: []string{"w"},
			Usage:   "worker key to use (overrides --create-worker-key)",
		},
		&cli.StringFlag{
			Name:    "owner",
			Aliases: []string{"o"},
			Usage:   "owner key to use",
		},
		&cli.StringFlag{
			Name:  "sector-size",
			Usage: "specify sector size to use",
			Value: units.BytesSize(float64(policy.GetDefaultSectorSize())),
		},
		&cli.StringSliceFlag{
			Name:  "pre-sealed-sectors",
			Usage: "specify set of presealed sectors for starting as a genesis miner",
		},
		&cli.StringFlag{
			Name:  "pre-sealed-metadata",
			Usage: "specify the metadata file for the presealed sectors",
		},
		&cli.BoolFlag{
			Name:  "nosync",
			Usage: "don't check full-node sync status",
		},
		&cli.BoolFlag{
			Name:  "symlink-imported-sectors",
			Usage: "attempt to symlink to presealed sectors instead of copying them into place",
		},
		&cli.BoolFlag{
			Name:  "no-local-storage",
			Usage: "don't use storageminer repo for sector storage",
		},
		&cli.StringFlag{
			Name:  "gas-premium",
			Usage: "set gas premium for initialization messages in AttoFIL",
			Value: "0",
		},
		&cli.StringFlag{
			Name:  "from",
			Usage: "select which address to send actor creation message from",
		},
	},

二、Jaeger

Lotus源码中在storage-miner的main.go中:

    import "go.opencensus.io/trace"
//...
    jaeger := tracing.SetupJaegerTracing("lotus")
	defer func() {
		if jaeger != nil {
			jaeger.Flush()
		}
	}()

这里的方法调用的是setup.go中的方法

import "contrib.go.opencensus.io/exporter/jaeger"
//...
func SetupJaegerTracing(serviceName string) *jaeger.Exporter {

	if _, ok := os.LookupEnv("LOTUS_JAEGER"); !ok {
		return nil
	}
	agentEndpointURI := os.Getenv("LOTUS_JAEGER")

	je, err := jaeger.NewExporter(jaeger.Options{
		AgentEndpoint: agentEndpointURI,
		ServiceName:   serviceName,
	})
	if err != nil {
		log.Errorw("Failed to create the Jaeger exporter", "error", err)
		return nil
	}

	trace.RegisterExporter(je)
	trace.ApplyConfig(trace.Config{
		DefaultSampler: trace.AlwaysSample(),
	})
	return je
}

问题:Jaeger是什么?它的作用和使用方法?
Jaeger是一个分布式调用链路跟踪和监控的系统

此问题比较复杂,重要但不紧急,可暂时搁置。

三、Uber包与依赖注入

//TODO

四、其他

1、空对象

err := gen.WriteTupleEncodersToFile("./chain/types/cbor_gen.go", "types",
		types.BlockHeader{},
		types.Ticket{},
//...
)
//这里传入的不是结构体,而是实例化后的对象,只不过是空对象。

你可能感兴趣的:(区块链技术,区块链,golang)