您当前的位置: 首页 > 

mutourend

暂无认证

  • 2浏览

    0关注

    661博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Cosmos SDK

mutourend 发布时间:2022-01-04 15:57:03 ,浏览量:2

1. 引言

Cosmos SDK 为开源框架,可用于构建:

  • 1)multi-asset public Proof-of-Stake (PoS) blockchains:如Cosmos Hub。
  • 2)permissioned Proof-of-Authority (PoA) blockchains。

基于Cosmos SDK构建的blockchains通常称为application-specific blockchains。

Cosmos SDK主要优势为:

  • 1)Cosmos SDK内的共识机制为:Tendermint Core——为当前最成熟的BFT共识机制。已广泛应用,被认为是构建PoS系统的黄金共识标准。
  • 2)开源模块化设计。随着开源生态模块的增长,将越来越容易构建复杂的去中心化平台。
  • 3)Cosmos SDK的灵感来自基于功能的安全性,并得益于多年来与区块链状态机的斗争。这使得Cosmos SDK成为构建区块链的非常安全的环境。
  • 4)已用于构建多个应用链,如:Cosmos Hub, IRIS Hub, Binance Chain, Terra, Kava等等。
2. 应用链

应用链为blockchains customized to operate a single application。

                ^  +-------------------------------+  ^
                |  |                               |  |   Built with Cosmos SDK
                |  |  State-machine = Application  |  |
                |  |                               |  v
                |  +-------------------------------+
                |  |                               |  ^
Blockchain node |  |           Consensus           |  |
                |  |                               |  |
                |  +-------------------------------+  |   Tendermint Core
                |  |                               |  |
                |  |           Networking          |  |
                |  |                               |  |
                v  +-------------------------------+  v

共识引擎中主要处理2种任务:

  • 1)网络逻辑:主要包含gossiping block parts,transactions 和 consensus votes。
  • 2)共识逻辑:以确定 the deterministic ordering of transactions in the form of blocks。

共识引擎中既不定义state,也不定义交易的有效性。 共识引擎中处理的交易是以[]bytes形式存在的,共识引擎会将交易通过ABCI relay到application,由application来decode和process。在网络和共识逻辑中(如beginning of a block, commit of a block, reception of an unconfirmed transaction等),共识引擎会emit ABCI messages for the state-machine to act on。 基于Cosmos SDK的开发者无需自己实现ABCI,因为BaseApp中内置了一个ABCI的实现。

相比于基于Virtual-machine blockchain开发智能合约,基于Cosmos SDK的应用链主要优势有:

  • 1)灵活性。不受限于EVM编程语言及有限的功能。
    • state-machine通过ABCI接口连接底层的共识机制。该接口可封装为任意编程语言,即意味着开发者可基于其开发语言构建state-machine。除了Cosmos SDK(Go语言)框架之外,还可选择Lotion(Go语言),Weave(JavaScript)。
    • 开发者可自由explore the full spectrum of tradeoffs,如:validator数量 VS 交易吞吐量,安全性 VS 异步可用性等,以及可选择不同的设计方案,如:DB or IAVL tree for storage,UTXO or account model等。
    • 开发者可实现automatic execution of code。在Cosmos SDK中,logic可在每个区块的开始和结束时自动触发。同时,也可自由选择密码学库,而不必受限于底层虚拟机所使用的密码学库。
  • 2)性能。不必竞争同一虚拟机的资源。采用Tendermint BFT共识,相比于PoW共识,其具有更高的交易吞吐量。应用链仅为单一应用服务,相比于虚拟机机制,不存在不同应用之间的计算和存储资源竞争问题。交易吞吐量的真正瓶颈在于state-machine,随着虚拟机需要解析的交易数量增加,相应的计算复杂度也会增加。
  • 3)自治。支持多种角色,如用户、开发者、第三方服务商等等。可实现应用管理 与 网络管理 同步。
  • 4)安全性。安全很难量化,且不同平台之间差异很大。开发者可选择成熟的编程语言来开发应用链。
3. 区块链架构 3.1 state machine

区块链的核心为:a replicated deterministic state machine。

state machine为a computer science concept,为a machine can have multiple states,但任意某时刻,仅有一种状态。 state:描述系统当前状态。 transactions:触发状态变化。

如已知状态S和交易T,state machine将返回新的状态S‘:

+--------+                 +--------+
|        |                 |        |
|   S    +---------------->+   S'   |
|        |    apply(T)     |        |
+--------+                 +--------+

实际,为了效率,会将区块内的交易打包处理。如已知状态S和a block of transactions B,state machine将返回新状态S’:

+--------+                              +--------+
|        |                              |        |
|   S    +----------------------------> |   S'   |
|        |   For each T in B: apply(T)  |        |
+--------+                              +--------+

在区块链上下文中,state machine是deterministic的,即意味着:若a node is started at a given state and replays the same sequence of transactions,it will always end up with the same final state。

Cosmos SDK为开发者提供了极大的灵活性来定义:

  • the state of their application
  • transaction types
  • state transition functions

可基于Tendermint来实现state-machine。

3.2 Tendermint

通过Cosmos SDK,开发者仅需定义state machine,Tendermint将负责处理replication over the network。

                ^  +-------------------------------+  ^
                |  |                               |  |   Built with Cosmos SDK
                |  |  State-machine = Application  |  |
                |  |                               |  v
                |  +-------------------------------+
                |  |                               |  ^
Blockchain node |  |           Consensus           |  |
                |  |                               |  |
                |  +-------------------------------+  |   Tendermint Core
                |  |                               |  |
                |  |           Networking          |  |
                |  |                               |  |
                v  +-------------------------------+  v

Tendermint为应用不可知引擎,负责处理区块链中的networking layer和consensus layer。即意味着,Tendermint负责广播和排序transaction bytes。Tendermint core基于BFT算法来对交易顺序达成共识。

Tendermint 共识算法 有一组名为Validators的特殊节点。Validators负责adding blocks of transactions to the blockchain。 在这里插入图片描述

3.3 ABCI

Tendermint通过ABCI接口将交易发送给应用,应用必须实现ABCI接口。

              +---------------------+
              |                     |
              |     Application     |
              |                     |
              +--------+---+--------+
                       ^   |
                       |   | ABCI
                       |   v
              +--------+---+--------+
              |                     |
              |                     |
              |     Tendermint      |
              |                     |
              |                     |
              +---------------------+

注意:Tendermint仅处理transaction bytes,其并不知道这些bytes的含义。Tendermint能做的仅是order these transaction bytes deterministically。 Tendermint会将这些bytes通过ABCI发送给application,然后等待a return code来inform it if the messages contained in the transactions are successfully processed or not。

ABCI 中包含的重要消息有:

  • 1)CheckTx:当Tendermint Core收到交易,会将交易发送给application来check if a few basic requirements are met。CheckTx将保护全节点的mempool,预防垃圾交易。名为AnteHanlder的handler将用于运行a series of validation steps,如验证是否有足够的fee 以及 验签。若check通过,则会将该交易添加到mempool 并relay给peer 节点。注意,CheckTx时,交易并未处理,即state并未修改。因为此时,交易并未打包进块。
  • 2)DeliverTx:当Tendermint Core收到a valid block,会通过DeliverTx将区块链内的每笔交易发送给application,由application来处理交易。此时,会存在state变化。AnteHandler会随着交易内每个message的实际 Msg RPC服务 再次运行。
  • 3)BeginBlock/EndBlock:这些消息会在每个区块的开始和结束时运行,不管该区块内是否包含交易。It is useful to trigger automatic execution of logic。但是要谨慎使用,因为计算昂贵的loop将slow down your blockchain,而无限死循环将冻结整个链。

任何基于Tendermint的应用都需要实现ABCI接口,以和底层的Tendermint引擎进行通讯。 幸运地是,你无需自己实现ABCI接口,Cosmos SDK以 baseapp 的形式提供了一个样板。

4. Cosmos SDK核心元素

Cosmos SDK为ABCI接口基于Golang语言实现的一个样板。其采用multistore来persist data,采用router来处理交易。

当application收到由DeliverTx发来的valid block时,application会做如下处理:

  • 1)对交易进行解码。Tendermint处理的格式仅为[]bytes
  • 2)从交易中提取messages,并进行basic sanity checks。
  • 3)将每个message 路由至合适的模块,以便对这些message进行处理。
  • 4)Commit状态变化。
                                      +
                                      |
                                      |  Transaction relayed from the full-node's
                                      |  Tendermint engine to the node's application
                                      |  via DeliverTx
                                      |
                                      |
                +---------------------v--------------------------+
                |                 APPLICATION                    |
                |                                                |
                |     Using baseapp's methods: Decode the Tx,    |
                |     extract and route the message(s)           |
                |                                                |
                +---------------------+--------------------------+
                                      |
                                      |
                                      |
                                      +---------------------------+
                                                                  |
                                                                  |
                                                                  |  Message routed to
                                                                  |  the correct module
                                                                  |  to be processed
                                                                  |
                                                                  |
+----------------+  +---------------+  +----------------+  +------v----------+
|                |  |               |  |                |  |                 |
|  AUTH MODULE   |  |  BANK MODULE  |  | STAKING MODULE |  |   GOV MODULE    |
|                |  |               |  |                |  |                 |
|                |  |               |  |                |  | Handles message,|
|                |  |               |  |                |  | Updates state   |
|                |  |               |  |                |  |                 |
+----------------+  +---------------+  +----------------+  +------+----------+
                                                                  |
                                                                  |
                                                                  |
                                                                  |
                                       +--------------------------+
                                       |
                                       | Return result to Tendermint
                                       | (0=Ok, 1=Err)
                                       v
5. Cosmos SDK成熟模块

目前Cosmos SDK成熟模块有:

  • 1)Authz:授权账号代表其他账号执行操作。
  • 2)Bank:Token transfer functionalities。
  • 3)Capability:Object capability implementation。
  • 4)Crisis:Halting the blockchain under certain circumstances(如某invariant is broken)。
  • 5)Distribution:Fee distribution,以及 staking token provision distribution。
  • 6)Epoching:允许modules queue messages for execution at a certain block height。
  • 7)Evidence:Evidence handling for double signing, misbehaviour等等。
  • 8)Feegrant:授予执行交易的费用津贴。
  • 9)Governance:On-chain proposals and voting。
  • 10)Mint:Creation of new units of staking token。
  • 11)Params:Globally available parameter store。
  • 12)Slashing:Validator punishment mechanisms。
  • 13)Staking:公有链的PoS层。
  • 14)Upgrade:Software upgrades handling and coordination。
参考资料

[1] Cosmos SDK官方文档

关注
打赏
1664532908
查看更多评论
立即登录/注册

微信扫码登录

0.2156s