okx

如何在 Substrate 上构建您的第一个区块链?

时间:2023-12-30|浏览:287

众所周知,区块链是一种去中心化的账本技术,它在一系列区块中记录信息或允许网络参与者之间进行交易。

它还由分散的节点组成一个网络。

但首次构建新的区块链技术可能非常具有挑战性,并且存在严重的限制,例如开发区块链代码固有的可扩展性。

这就是为什么构建您的第一个区块链必须在底层上,因为它提供了灵活性以及开放和可扩展的开发环境,使您能够设计和构建完全定制的区块链节点,以满足您的应用程序或业务模型的需求。

如果您想了解更多关于区块链以及区块是如何形成的信息,那么您需要阅读这篇关于如何将区块添加到区块链的文章

在 Substrate 上构建区块链可以通过编译节点模板并启动本地基于 Substrate 的区块链来完成。

安装前端模板提交与本地区块链节点通信。

然后使用前端模板提交交易并查看结果。

Substrate 节点模板提供了一个工作开发环境,以便您可以立即开始在 Substrate 上进行构建

底层区块链非常重要,因为与构建在以太坊等通用智能合约区块链之上相比,它提供了更大的自由度、灵活性和优化能力。

这种基于底层的区块链作为 Polkadot 和任何其他平台的集成而存在。

在本文中,我们将更多地讨论如何从头开始在底层上构建区块链。

了解基材

该底层可以被描述为一个区块链框架,具体来说是一个用于构建定制区块链的开源框架。

这些区块链可以完全自主运行,这意味着它们不依赖于任何外部技术来运行。

用于构建在底层上的区块链框架是用于使用区块链服务的工具和库的集合,这些服务和区块链服务在区块链上存储和操作,并使用其货币来处理交易。

该区块链框架由点对点网络、账户管理、交易区块以及用户与区块链的交互组成。

而这一切都在基质中起作用。

The industry behind the substrate that was co-founded by Ethereum (the co-founder of Ethereum, Gavin Wood) is named PARITY, which also built the Polkadot network. The Polkadot is a decentralized, protocol-based blockchain platform that enables secure cross-blockchain communication, meaning Polkadot can be used as a bridge between blockchains which takes care of the communication layer between chains and makes it possible to interact between different blockchains such as Bitcoin and Ethereum.

This represents remarkable progress towards making the vision of web3 which is also a decentralized, blockchain-based version of the internet and making it a reality. Substrate-based blockchain exists as an integration to the Polkadot network so that when you create a blockchain with the substrate, you can be hooked into Polkadot. Among the several blockchain platforms, Substrate stands exceptional as a powerful framework that equips developers to build custom blockchains and decentralized applications (dApps) effortlessly.

Key features of Substrate

There are key features of substrate that make it stand out as a powerful framework equipment. They are as follows:

Modularity

It allows developers to pick and make a choice from a set of pre-built modules named pallets that can be customized and assembled to create a unique blockchain.

Flexibility

It is suitable for a wide range of applications since Substrate supports both agreement and non-agreement blockchains. However, its real power lies in the flexibility it offers. With Substrate, developers can blend and adjust these fundamental characteristics based on their specific needs and goals.

Interoperability

Substrate is designed to work together with other blockchains that enable cross-chain communication and data sharing. Furthermore, Substrate can be connected to other blockchains through bridges of the Polkadot network which will enhance its interoperability.

Security

Substrate is built with security in mind contributing a strong foundation for building robust and reliable blockchain applications.

“I’ve been playing around with Substrate over the past couple of weeks. Substrate is a Rust-based blockchain-building framework that is powering Polkadot and its associated blockchains. I’ve been a blockchain skeptic but there are a few changes in the ecosystem that is changing my opinion- By Vagmi Mudumbai”

Components Of The Substrate App

Building blockchain technology on the substrate has its components and they are:

Runtime

The runtime is the core logic of your blockchain application. It defines the business logic of your blockchain, the rules and state transformation. It is written in Rust and sums up the functionality of your blockchain making it the most critical component.

Pallets

Pallets are modular components that expand the functionality of the runtime. Substrate comes with a collection of pallets, for example, the balances, staking, and governance pallets. Developers can create custom pallets that will add specific features to their blockchain.

Consensus Mechanism

The substrate supports numerous consensus mechanisms like Proof of Stake (PoS) and Proof of Authority (PoA). With this, you can then choose the consensus mechanism that best suits the requirements of your project.

Remote Procedure Call (RPC) API

The remote procedure call API allows external applications and services to communicate with your blockchain and it will expose the various processes for querying and sending transactions to the blockchain.

User Interfaces

Building user-friendly interfaces like web or mobile applications which is a vital part of developing a blockchain on Substrate ( building substrate app). Developers will use libraries like Polkadot.js (javascript code) or create their custom UIs to interact with the blockchain.

Benefits Of Using Substrate To Develop Blockchain

I am going to list out at least 3 benefits of using a substrate to build blockchain and here they are:

Faster Development With Reduced Time and Cost

You can develop the blockchain node easily within the space of 60 minutes or less. What is more, is that Substrate as a blockchain framework does all the hard jobs for you. You do not have to worry about the core components such as data layer, networking, transaction queue and consensus. From the start, you can make use of the default ones and start playing with your new blockchain. However, as you are getting yourself involved, you can still exchange components but do not worry about implementing them but make use of a pallet that will suit your needs.

More Flexibility Than Other Blockchain Networks

When building Substrate, it gives you the liberty to use custom blocks as you wish. It is not necessary to develop smart contracts but it is your choice if you want it. Instead of building smart contracts, you can create functions that ensure the safety of your network. However, it is still your full responsibility to deliver proper functions.

Faster development Using Substrate As A Developer

Substrate is written in Rust and that alone is great news for many developers. Rust is a bit similar to JavaScript especially how you write the code but its logic might be compared to C++. Even if you only know JavaScript (a website code), you will easily move it to Rust. It is worth mentioning that Rust might be similar to C++ but only in a good way. Rust is quick as it does not have any runtime.

Understading Bittensors Programmable Blockchain Incentivized Rewards using $TAO:We are set to revolutionize the way we think about collaborative computation and machine learning. With Bittensor's ingenious approach, built on the Substrate framework, we're not just incentivizing… pic.twitter.com/JYp2CTrelm

— Andy ττ (@bittingthembits) December 14, 2023

Getting Started With Substrate

Now that you know what the substrate is all about, we will be dealing with how to start building blockchain on substrate or simply say, building on substrate apps. Since we are dealing with a peer-to-peer decentralized network system, the primary unit we are talking about is the node where the blockchain runs.

This node runs inside a user providing all the basic components of the system needed to function (as discussed in Section 2 above). Now here are the step-by-step instructions to set up the development for building the substrate apps.

Setting Up Your Development Environment

This is done by compiling a substrate node template that will provide you with a working development environment for you to start building on the substrate right away. To compile the substrate node template, you either create your first substrate chain from the substrate applications or install substrate dependencies where you check both the backend and front-end templates to familiarize yourself with the app. You can also install Rust

The Runtime Of The Substrate node

You can choose to add a Contracts Pallet on your own or you can decide to build the node. cargo is a package manager for Rust, which you should already have on your computer after completing the “Create your First Substrate Chain tutorial”.

cargo build –release

You can continue by purging the chain and run it in development mode with the following commands below:

./target/release/node-template purge-chain --dev./target/release/node-template --dev

To ensure that your chain is up and running, you can use the Polkadot JS Apps. Running the app you will need a Chromium-based browser (not all browsers allow you to connect to the local node). You can expand the list of available chains by clicking on the Substrate logo in the upper left corner. Expand the development list and choose the Local Node ( as illustrated below)

From the developer page, check that you have the contracts and see if the contracts pallet was added successfully.

Get The Code Of ERC20 Token Smart Contract

You can get this code from the repository of the tutorial from GitHub by accessing it from the NestJS Application, you can also check it below to get the code

The code in substrate dir is cloned from here below

https://github.com/substrate-developer-hub/substrate-node-template

The code in smart-contract dir is based on the tutorial, check it here below

https://substrate.dev/substrate-contracts-workshop/#/

Build The Contract

Immediately you have the code of ERC20, then you have to build and deploy it to the Substrate chain. If you are done with the tutorial from the previous step above, then you must be probably already familiar with the process. But if you haven’t and still wish to build the contract by yourself, you will need to install the ink command line utility. Or you can also decide to skip it and use the contract wasm binary and metadata files uploaded to this post’s repository and go straight to deploying the contract to the chain.

Install the ink! command line utility. Here it is below:

cargo install cargo-contract –vers 0.6.2 –force

Build the contract with the following command, which will create a .wasm file.

cargo +nightly contract build

Generate metadata json file, which describes the smart contract.

cargo +nightly contract generate-metadata

Once you are done installing the ink or following the previous step, you deploy the contract by checking your substrate contract chapter from the tutorial. When creating an instance of the contract, we will need to pass the initial supply value of at least 1,000,000.

You can also build your blockchain using cargo build command. Then you test your blockchain locally or on a testnet to ensure that it functions correctly. And verify if the blockchain node is running and verified. The terminal would display similarly below:

2022-08-16 13:43:58 Substrate Node 2022-08-16 13:43:58 version 4.0.0-dev-de262935ede 2022-08-16 13:43:58 by Substrate DevHub https://github.com/substrate-developer-hub, 2017-2022 2022-08-16 13:43:58 Chain specification: Development 2022-08-16 13:43:58 Node name: limping-oatmeal-7460 2022-08-16 13:43:58 Role: AUTHORITY 2022-08-16 13:43:58 Database: RocksDb at /var/folders/2_/g86ns85j5l7fdnl621ptzn500000gn/T/substrate95LPvM/chains/dev/db/full 2022-08-16 13:43:58 Native runtime: node-template-100 (node-template-1.tx1.au1) 2022-08-16 13:43:58 Initializing Genesis block/state (state: 0xf6f5…423f, header-hash: 0xc665…cf6a) 2022-08-16 13:43:58 Loading GRANDPA authority set from genesis on what appears to be first startup. 2022-08-16 13:43:59 Using default protocol ID "sup" because none is configured in the chain specs 2022-08-16 13:43:59 Local node identity is: 12D3KooWCu9uPCYZVsayaCKLdZLF8CmqiHkX2wHsAwSYVc2CxmiE ... ... ... ... 2022-08-16 13:54:26 Idle (0 peers), best: #3 (0xcdac…26e5), finalized #1 (0x107c…9bae), ⬇ 0 ⬆ 0

Create A User Interface

You can create a user-friendly frontend for your Substrate app that will allow users to communicate with the blockchain and connect the front end to the blockchain using the Remote Procedure Call (RPC) API.

Deploying Your Blockchain

You can then deploy your blockchain on a testnet for final testing and once you are satisfied with the results, deploy your blockchain on the mainnet.

Upgrading And Maintaining

You must keep on maintaining and upgrading your Substrate app to embrace new features, bug fixes and security improvements.

The Installation Process Of Substrate

There are different tools used to build blockchain on substrate or blockchain-base substrate and they are Parity, Rust, and Git or Github. You can use any of these frameworks or tools mentioned to start building your substrate application. Now the installation process of the substrate and its dependencies are as follows

Install Rust Toolchain

Rust is a modern programming language that provides a rich feature set for building complex systems. For you to be familiar with it, learn it by visiting the Rust site. The core tools in the Rust toolchain are the rustc compiler, the cargo build-up and the package manager with the rustup toolchain manager.

To Set Up The Development Environment

Since you are going to write the code on Rust, ensure that your development environment has the appropriate extensions and plug-ins for working with Rust. As you start to develop with the Substrate node template, you’ll see that it includes a core set of features and tools specifically designed for runtime development. Right now, let’s begin with developing a Substrate-based blockchain by using Polkadot-JS API.

Install The API

Once you installed the API via

yarn add @polkadot/api

It will be added and ready for use and the above code will always install the latest steady release which will allow you to connect and test the networks with the local nodes that will be tracking the versioned releases for Polkadot and Substrate.

Publishing Betas

For the higher risk of either polkadot or substrate we can publish a beta version once anything is merged into the API master branch. It contains all the latest features and the version is inside the polkadot-js projects. By so doing, you can install via the @beta tag, i.e.

yarn add @polkadot/api@beta

Other Dependencies

This is not necessary once you have @polkadot/api above but with other dependencies like @polkadot/types are installed automatically. When using yarn the dependencies will be installed and available for use and you will never run into issues with conflicting versions. Other dependencies are installing @cargo.

For you to install cargo dependencies, you must clone the substrate node template and navigate the repository.

Build The Command

Use the following command to build the node without launching it

cargo build –release

Run The Command

By removing any existing developer chain state if available:

./target/release/node-template purge-chain –dev

And start a new development chain with:

./target/release/node-template –dev

The detailed logs will show by running the node with the following environment variables set below:

RUST_LOG=debug RUST_BACKTRACE=1 cargo run — –dev.

Once you run it, then verify the node if it’s running.

My new blog post about "How To #Build Your Own #Blockchain using Parity Substrate" just got published on @hackernoon. It covers the process for getting started with the #Substrate framework. Building fully customizable #blockchains has never been easier.https://t.co/Ft5SJaa2NB

— Gautam Dhameja (@gautamdhameja) April 10, 2019

Building Your First Blockchain On Substrate

If you are a newbie developer or you have experience in using blockchain framework, then Substrate offers tutorials and will take you by hand step by step to build it and the Substrate Playground is for those who would love to experiment even with little guidance.

Substrate tutorials are capable even for absolute beginners with no crypto and website knowledge, covering all steps without going much into the coding details. Apart from these tutorials, Substrate will provide several how-to guides on specific topics and also grant you access to many open-source projects that have been built using the Substrate app.

Steps-BY-Steps Creating Your First Blockchain On Substrate

  1. You can start the process by building a node in a development environment and then simulate the two (2) node network to confirm if all the interactions between the 2 nodes are taking place as required.

  2. Once you are done with step 1, the developers will then proceed to start with validators ( small network) by upgrading the run-time code for a node before the larger scale can be expanded.

  3. Using the substrate playground, the developers will decide to skip the initial substrate node setup and directly transition to a cloud-box sandbox environment where they will select either a node template or a front-end template.

  4. Using the template, the developers will then work with a “blockchain framework’’ and build upon the essential capabilities using P2P networking, consensus mechanism, account transition and so on.

  5. This front-end template contains all the components necessary to interact with the runtime of node template while acting as a front-end to substrate node template

The Examples And Code Snippets To Illustrate The Process Of Building And Configure The Basic Blockchain On Substrate

The Runtime Storage

This offers a closer look at storage structure and how to navigate to data stored in the runtime. You can declare your runtime storage with #[pallet::storage]. There are different examples but we will use a single storage value to illustrate the below

Rust #[pallet::storage] type SomePrivateValueT = StorageValue _, u32, ValueQuery ; #[pallet::storage] #[pallet::getter(fn some_primitive_value)] pub(super) type SomePrimitiveValueT = StorageValue_, u32, ValueQuery; #[pallet::storage] pub(super) type SomeComplexValueT: Config = StorageValue_, T::AccountId, ValueQuery;

Pallet Coupling

这个托盘定义了一个货币特征,它有一个

抽象函数接口

,稍后将实现实际的传输逻辑。

Rust pub TraitCurrencyAccountId { // -- snip -- fn Transfer( source: &AccountId, dest: &AccountId, value: Self::Balance, // 现在不用担心最后一个参数 Existence_requirement: ExistenceRequirement, ) - 调度结果;

}

创世配置

它描述了 Genesis 配置的主要元素。

在文本编辑器中打开

node/src/chain_spec.rs

文件后,您可以找到

GenesisConfig

并设置某些存储项的初始值。

例如,使用这段代码

节点/src/chain_spec.rs

下面的文件,

Rust - GenesisConfig { GenesisConfig { system: SystemConfig { // 将 Wasm 运行时添加到存储中。

代码: wasm_binary.to_vec(), }, template_module: TemplateModuleConfig { 一些: 221u32, }, transaction_ payment: Default::default(), } }

结论

文本提供了如何在 Substrate 上构建区块链的指南,强调理解底层技术并遵循分步过程的重要性。

但是,如果您想开始在 Substrate 上构建第一个区块链的旅程,请访问 Substrate 应用程序以获取教程,该教程将逐步向您展示如何轻松且不浪费时间地在 Substrate 上构建区块链。

热点:区块链 如何在

欧易

欧易(OKX)

用戶喜愛的交易所

币安

币安(Binance)

已有账号登陆后会弹出下载

« 上一条| 下一条 »
区块链交流群
数藏交流群

合作伙伴

非小号交易所排名-专业的交易行情资讯门户网站,提供区块链比特币行情查询、比特币价格、比特币钱包、比特币智能合约、比特币量化交易策略分析,狗狗币以太坊以太币玩客币雷达币波场环保币柚子币莱特币瑞波币公信宝等虚拟加密电子数字货币价格查询汇率换算,币看比特儿火币网币安网欧易虎符抹茶XMEX合约交易所APP,比特币挖矿金色财经巴比特范非小号资讯平台。
非小号行情 yonghaoka.cn 飞鸟用好卡 ©2020-2024版权所有 桂ICP备18005582号-1