美文网首页
2021-10-28 起步 Substrate

2021-10-28 起步 Substrate

作者: buddyCoder | 来源:发表于2021-10-28 16:28 被阅读0次

    Getting Started

    Overview

    本资源的目的是帮助读者了解使用Substrate进行区块链开发的错综复杂的问题。本指南分为几个部分,解释了Substrate所基于的原则和设计决策,以及成为一个有效的Substrate区块链开发者所需的具体技能。

    Information

        为了最大限度地利用Substrate,你应该对计算机科学和基本的区块链概念有良好的了解。头、区块、客户端、哈希、交易和签名等术语应该是熟悉的。Substrate是建立在Rust编程语言上的,它利用新颖的设计模式,使代码的开发安全而快速。尽管你不需要知道Rust就可以开始使用Substrate,但对Rust的良好理解将使你成为一个更好的Substrate开发者。请查看Rust社区提供的优秀资源,以培养你的Rust开发技能。
    

    Substrate采用模块化方法进行区块链开发,并定义了一套丰富的基元,使开发者能够利用强大的、熟悉的编程习惯。

    使用方法

    Substrate被设计为以三种方式中的一种使用

    1. With the Substrate Node: 你可以运行 pre-designed Substrate Node 并配置 configure 其开始区块。在这种情况下,你只需要提供一个JSON文件并启动你自己的区块链。该JSON文件允许你配置组成Substrate Node运行时间的模块的起始状态,例如。Balances, Staking, and Sudo. 你可以在Create Your First Substrate ChainStart a Private Network教程中了解更多关于运行Substrate Node的信息。

    2. With Substrate FRAME: 你可以使用 FRAME(模块化实体的运行时聚合框架)轻松地创建自己的自定义运行时,这也是用于构建底层节点的方法。这为你的区块链逻辑提供了大量的自由,并允许你配置数据类型,从模块库(称为 "托盘")中选择,甚至添加你自己的自定义托盘。对于这样的项目,Substrate开发者中心节点模板是一个有用的起点。要了解更多信息,请看 Build a dApp 和添加Add a Pallet.(添加钱包)的教程。

    3. With Substrate Core: 整个FRAME系统可以被忽略,而运行时间可以从头开始设计和实现。这可以用任何可以针对WebAssembly的语言来完成。如果运行时可以与Substrate节点的抽象块创作逻辑兼容,那么你可以简单地从你的Wasm blob中构建一个新的创世块,然后用现有的基于Rust的Substrate客户端启动你的链。如果不是,那么你将需要改变客户端的块创作逻辑,甚至有可能改变头和块的序列化格式。就开发工作而言,这是迄今为止使用Substrate最困难的方式,但也给了你最大的创新自由。

    Substrate允许开发者在其设计决策的每一步中,在技术自由和开发便利之间做出选择。下图说明了这种灵活性的本质。

    [图片上传失败...(image-e12c20-1635409692997)]

    Next steps

    Learn more

    Examples

    • 遵循我们的教程 tutorials ,学习用Substrate和FRAME构建和运行区块链。
    • 请参考我们的操作指南 how-to guides ,找到基于工作代码的例子,展示常见问题的解决方案。

    References 参考文献

    Architecture (架构)

    [图片上传失败...(image-51083f-1635409692998)]

    Substrate客户端是一个运行基于Substrate的区块链节点的应用程序。它由几个组件组成,包括但不限于以下内容。

    • 存储(Storage):用于持久保持底层区块链的不断变化的状态。区块链网络允许参与者对存储的状态达成无信任consensus 的共识。Substrate提供了一个简单而高效的键值存储机制 key-value storage mechanism.。
    • 运行时(Runtime):定义了如何处理区块的逻辑,包括状态转换逻辑。在Substrate中,运行时代码被编译为 Wasm,并成为区块链存储状态的一部分。这使得基于Substrate的区块链的决定性特征之一成为可能:不费力的运行时升级forkless runtime upgrades. 。底层客户端也可以包括一个 "本地运行时",它是为客户端本身(而不是Wasm)的同一平台而编译的。客户端调度对运行时的调用的组件被称为执行器executor, ,其作用是在本地代码和解释的Wasm之间进行选择。虽然本地运行时可能提供性能优势,但如果Wasm运行时实现了一个较新的版本,执行器将选择加载Wasm运行时。
    • 点对点网络(Peer-to-peer network):允许客户端与其他网络参与者进行通信的能力。Substrate使用Rust实现的libp2p网络栈来实现这一点。
    • 共识(Consensus):允许网络参与者就区块链的状态达成一致的逻辑。Substrate使得提供定制的共识引擎成为可能,并且还配备了几个建立在Web3基金会 Web3 Foundation research研究之上的共识机制。
    • RPC(远程程序调用):允许区块链用户与网络互动的能力。Substrate提供HTTP和WebSocket RPC服务器。
    • 遥测(Telemetry):由嵌入式普罗米修斯 Prometheus 服务器暴露的客户端指标。

    Installation 安装

    本页将指导你完成为Substrate开发准备计算机所需的两个步骤。由于Substrate是用Rust编程语言 the Rust programming language构建的,你需要做的第一件事是为Rust开发准备计算机--这些步骤将根据计算机的操作系统而有所不同。一旦配置好Rust,你将使用其工具链与Rust项目进行交互;Rust的工具链的命令对于所有支持的、基于Unix的操作系统都是一样的。

    1. Build dependencies 构建依赖

    Substrate的开发在基于Unix的操作系统(如macOS或Linux)上是最容易的。Substrate教程和指南中的例子使用Unix风格的终端来演示如何从命令行与Substrate交互。

    详细看链接

    Ubuntu/Debian

    Use a terminal shell to execute the following commands:

    sudo apt update
    
    #May prompt for location information
    
    sudo apt install -y git clang curl libssl-dev llvm libudev-dev
    

    Arch Linux

    Run these commands from a terminal:

    pacman -Syu --needed --noconfirm curl git clang
    

    ......

    macOS

    Information

    The Apple M1 ARM system on a chip is not very well supported yet by rust, and thus you very likely will run into build errors stemming from this. There are reports that using substrate dependancies newer than the June 2021 monthly release are able to work without issue. Here is the ', node template monthly-2021-05 'that you should use as a base for building on M1s. If you decide not to use a monthly release, see this community contributed script (and discussion) for details on extra configuration steps to get things working on v3.0.0 and below.

    苹果M1芯片上的ARM系统还没有得到rust很好的支持,因此你很可能会遇到由此产生的构建错误。有报告称,使用比2021年6月的月度版本更新的底层依赖能够顺利工作。这里是node template monthly-2021-05 ,你应该用它作为在M1上构建的基础。如果你决定不使用每月发布的版本,请参阅这个社区贡献的脚本this community contributed script (和讨论),以了解额外的配置步骤,使事情在v3.0.0及以下版本上运行。

    Open the Terminal application and execute the following commands:

    # Install Homebrew if necessary https://brew.sh/
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    
    # Make sure Homebrew is up-to-date, install openssl
    brew update
    brew install openssl
    

    Windows

    Warning

    Native development of Substrate is not very well supported! It is highly recommend to use Windows Subsystem Linux (WSL) and follow the instructions for Ubuntu/Debian. Please refer to the separate guide for native Windows development.

    警告
    Substrate的本地开发没有得到很好的支持! 强烈建议使用Windows Subsystem Linux(WSL)并遵循Ubuntu/Debian的说明。请参考关于本地Windows开发的单独指南。

    2. Rust developer environment Rust开发者环境

    Automated getsubstrate.io script 自动化脚本

    For most users you can run our script to automate the steps listed below:

    curl https://getsubstrate.io -sSf | bash -s -- --fast
    

    如果这时出现任何错误,请按照以下步骤在你的机器上手动配置Rust。

    Manual Rust configuration 手动配置Rust

    PS:不想翻译这块....为了看里面需要的工具链还是乖乖的翻译吧。

    本指南使用rustup 来帮助管理Rust工具链。首先安装和配置rustup。

    # Install
    curl https://sh.rustup.rs -sSf | sh
    # Configure
    source ~/.cargo/env
    

    配置Rust工具链,默认为最新的稳定版本,添加nightly和nightly wasm标志。

    rustup default stable
    rustup update
    rustup update nightly
    rustup target add wasm32-unknown-unknown --toolchain nightly
    

    Test your set-up 测试安装配置

    现在,确保你已经成功地为Substrate开发准备好一台计算机的最好方法是按照我们第一个教程our first tutorial.中的步骤进行。

    Information

    For more details on why these dependancies are used, and for troubleshooting errors building the template, read on.

    关于为什么使用这些依赖关系的更多细节,以及对构建模板的错误进行故障排除,请继续阅读。

    Substrate构建的故障排除(Troubleshooting Substrate builds )

    Rust configuration check

    要查看你目前使用的是什么Rust工具链,请运行。

    rustup show
    

    这将显示类似这样的(Ubuntu例子)输出。

    Default host: x86_64-unknown-linux-gnu
    rustup home:  /home/user/.rustup
    
    installed toolchains
    --------------------
    
    stable-x86_64-unknown-linux-gnu (default)
    nightly-2020-10-06-x86_64-unknown-linux-gnu
    nightly-x86_64-unknown-linux-gnu
    
    installed targets for active toolchain
    --------------------------------------
    
    wasm32-unknown-unknown
    x86_64-unknown-linux-gnu
    
    active toolchain
    ----------------
    
    stable-x86_64-unknown-linux-gnu (default)
    rustc 1.50.0 (cb75ad5db 2021-02-10)
    

    正如你在上面看到的,默认的工具链是stable的,并且安装了nightly-x86_64-unknown-linux-gnu工具链以及其wasm32-unknown-unknown目标。你还看到nightly-2020-10-06-x86_64-unknown-linux-gnu被安装了,但除非像指定你的nightly版本部分所说明specify your nightly version的那样明确定义,否则不会使用。

    WebAssembly compilation

    Substrate使用WebAssembly (Wasm) 来产生可移植的区块链运行时。你需要将你的Rust编译器配置为使用夜间构建,以允许你将Substrate运行时代码编译为Wasm目标。

    Information

    There are upstream issues in Rust that need to be resolved before all of Substrate can use the stable Rust toolchain. This is our tracking issue if you're curious as to why and how this will be resolved.

    在所有Substrate可以使用稳定的Rust工具链之前,Rust中存在一些上游问题需要解决。如果你想知道为什么以及如何解决这个问题,这是我们的跟踪问题。

    Latest nightly for Substrate master

    构建Substrate本身的开发者应该始终使用Rust stable和nightly的最新无缺陷版本。这是因为Substrate的代码库遵循Rust nightly的提示,这意味着Substrate的变化往往取决于Rust nightly编译器的上游变化。为了确保你的Rust编译器始终是最新的,你应该运行。

    rustup update
    rustup update nightly
    rustup target add wasm32-unknown-unknown --toolchain nightly
    

    Note

    It may be necessary to occasionally rerun rustup update if a change in the upstream Substrate codebase depends on a new feature of the Rust compiler. When you do this, both your nightly and stable toolchains will be pulled to the most recent release, and for nightly, it is generally not expected to compile WASM without error (although it very often does). Be sure to specify your nightly version if you get WASM build errors from rustup and downgrade nightly as needed.

    如果上游Substrate代码库的变化依赖于Rust编译器的新特性,可能有必要偶尔运行rustup update。当你这样做的时候,你的 nightly和stable工具链都会被拉到最新的版本,对于夜间版来说,一般不会在没有错误的情况下编译WASM(尽管它经常这样做)。如果你从rustup得到WASM的构建错误,请确保指定你的夜间版本,并根据需要降级夜间版本。

    Rust nightly toolchain

    如果你想保证在更新Rust和其他依赖关系时,你的构建可以在你的电脑上运行,你应该使用一个特定的Rust夜间版本,该版本已知与他们正在使用的Substrate版本兼容;这个版本会因项目而异,不同的项目可能使用不同的机制来向开发者传达这个版本。例如,Polkadot客户端在其发布说明中指定了这个信息 release notes.。

    # Specify the specific nightly toolchain in the date below:
    rustup install nightly-<yyyy-MM-dd>
    
    Wasm toolchain

    现在,配置夜间版本,使其与Wasm编译目标一起工作

    rustup target add wasm32-unknown-unknown --toolchain nightly-<yyyy-MM-dd>
    

    Specifying nightly version 指定夜间版本

    使用WASM_BUILD_TOOLCHAIN环境变量来指定Substrate项目在Wasm编译时应该使用的Rust nightly版本。

    WASM_BUILD_TOOLCHAIN=nightly-<yyyy-MM-dd> cargo build --release
    

    Information

    Note that this only builds the runtime with the specified nightly. The rest of project will be compiled with your default toolchain, i.e. the latest installed stable toolchain.

    请注意,这只是用指定的nightly构建运行时。项目的其余部分将用你的默认工具链进行编译,即最新安装的稳定工具链。

    Downgrading Rust nightly 降级夜间版本

    如果你的电脑被配置为使用最新的Rust nightly,而你想降级到一个特定的nightly版本,请按照以下步骤进行。

    rustup uninstall nightly
    rustup install nightly-<yyyy-MM-dd>
    rustup target add wasm32-unknown-unknown --toolchain nightly-<yyyy-MM-dd>
    

    Getting Started on Windows Windows系统下安装

    Information

    Native development of substrate is not very well supported! It is highly recommend to use Windows Subsystem Linux (WSL) and follow the instructions for Ubuntu/Debian. Substrate development is easiest on Unix-based operating systems like macOS or Linux. The examples in the Substrate Tutorials and How-to Guides use Unix-style terminals to demonstrate how to interact with Substrate from the command line.

    Substrate 的本地开发并没有得到很好的支持! 强烈建议使用Windows Subsystem Linux (WSL)并遵循Ubuntu/Debian的说明。在基于Unix的操作系统(如macOS或Linux)上,底层开发是最容易的。Substrate教程和指南中的例子使用Unix风格的终端来演示如何从命令行与Substrate交互。

    如果你确实决定尝试使用Windows电脑来原生构建Substrate,请做以下工作:

    下载并安装 "Visual Studio的构建工具“:

            你可以通过这个链接得到它:https://aka.ms/buildtools。
            运行安装文件:vs_buildtools.exe。
            确保在安装Visual C++ Build Tools时包含 "Windows 10 SDK "组件。
            重新启动你的计算机。
    

    安装Rust

        详细说明由 [Rust Book](https://doc.rust-lang.org/book/ch01-01-installation.html#installing-rustup-on-windows)提供。
    
        下载地址:https://www.rust-lang.org/tools/install。
    
            运行安装文件:rustup-init.exe。
    
            选择 "默认安装"。
    
            要开始安装,你需要在PATH环境变量中加入Cargo的bin目录(%USERPROFILE%\.cargo\bin)。
    
            以后的应用程序会自动拥有正确的环境,但你可能需要重新启动你当前的shell。
    

    在命令提示符(CMD)中运行这些命令来设置你的Wasm构建环境。

    rustup update nightly
    rustup update stable
    rustup target add wasm32-unknown-unknown --toolchain nightly
    

    安装LLVM: https://releases.llvm.org/download.html

    使用PowerShell用vcpkg安装OpenSSL。

    mkdir C:\Tools
    cd C:\Tools
    git clone https://github.com/Microsoft/vcpkg.git --depth=1
    cd vcpkg
    .\bootstrap-vcpkg.bat
    .\vcpkg.exe install openssl:x64-windows-static
    

    使用PowerShell将OpenSSL添加到你的系统变量中。

    $env:OPENSSL_DIR = 'C:\Tools\vcpkg\installed\x64-windows-static'
    $env:OPENSSL_STATIC = 'Yes'
    [System.Environment]::SetEnvironmentVariable('OPENSSL_DIR', $env:OPENSSL_DIR, [System.EnvironmentVariableTarget]::User)
    [System.Environment]::SetEnvironmentVariable('OPENSSL_STATIC', $env:OPENSSL_STATIC, [System.EnvironmentVariableTarget]::User)
    

    安装cmake: https://cmake.org/download/

    安装make

    choco install make
    

    Glossary词汇表

    传送门

    本页定义并解释了Substrate生态系统中常见的许多特定领域的术语。即使是最有经验的Substrate开发者,这也是一个有用的资源。

    Adaptive Quorum Biasing (AQB)

    根据选民投票率来指定公投的通过门槛的一种方法。正的投票率偏差是指随着投票人数的增加,通过门槛会降低;也就是说,较高的投票率会积极地增加公投通过的可能性。负的投票率偏差是指,投票率越高,通过门槛越高。负投票率偏差也被称为 "默认携带",因为如果有一个冷漠的投票机构,公投就会默认通过。中性投票率偏差规定了一个简单的多数通过门槛。AQB消除了对严格的法定人数的需要,因为法定人数是任意的,并创造了不理想的治理机制。AQB在民主托盘中实现,它为一些链上机构(如集体或任何代币持有人)提供了接口,以调用具有积极、中立或消极偏见的公投。

    AfG

    Al's Finality Gadget "的内部代号,它是以发明它的Alistair Stewart命名的。AfG是GRANDPA的同义词。

    Aggregation

    FRAME的上下文中,"pallet aggregation"是指将多个运行模块的类似类型合并为一个单一的类型。这允许每个模块的类似类型被代表。目前有六个这样的数据类型。

    Call 调用--公布了可以用一组参数调用的函数
    Error 错误--用于告知用户为什么函数调用()失败Call
    Event 事件 - 托盘可以发出事件,使用户了解一些状态变化
    Log 日志--一个可扩展的头部部件
    Metadata 元数据--允许检查上述内容的信息
    Origin - 指定一个函数调用(Call)的来源

    Approval Voting

    .........

    个人总结

    这一段是如何入门,整个框架有哪些东西,根据个人情况选择入门的方式。整个系统架构,如何安装开发编译环境。

    特别说了在windows下如何开发。

    其实这块个人觉得如果单纯的编译可以使用docker进行编译。

    运行也可以使用docker进行运行。

    相关文章

      网友评论

          本文标题:2021-10-28 起步 Substrate

          本文链接:https://www.haomeiwen.com/subject/jzuvaltx.html