時間:2023-12-30|瀏覽:310
眾所周知,區(qū)塊鏈是一種去中心化的賬本技術,它在一系列區(qū)塊中記錄信息或允許網(wǎng)絡參與者之間進行交易。 它還由分散的節(jié)點組成一個網(wǎng)絡。 但首次構建新的區(qū)塊鏈技術可能非常具有挑戰(zhàn)性,并且存在嚴重的限制,例如開發(fā)區(qū)塊鏈代碼固有的可擴展性。
這就是為什么構建您的第一個區(qū)塊鏈必須在底層上,因為它提供了靈活性以及開放和可擴展的開發(fā)環(huán)境,使您能夠設計和構建完全定制的區(qū)塊鏈節(jié)點,以滿足您的應用程序或業(yè)務模型的需求。
如果您想了解更多關于區(qū)塊鏈以及區(qū)塊是如何形成的信息,那么您需要閱讀這篇關于如何將區(qū)塊添加到區(qū)塊鏈的文章
在 Substrate 上構建區(qū)塊鏈可以通過編譯節(jié)點模板并啟動本地基于 Substrate 的區(qū)塊鏈來完成。 安裝前端模板提交與本地區(qū)塊鏈節(jié)點通信。 然后使用前端模板提交交易并查看結果。 Substrate 節(jié)點模板提供了一個工作開發(fā)環(huán)境,以便您可以立即開始在 Substrate 上進行構建
底層區(qū)塊鏈非常重要,因為與構建在以太坊等通用智能合約區(qū)塊鏈之上相比,它提供了更大的自由度、靈活性和優(yōu)化能力。 這種基于底層的區(qū)塊鏈作為 Polkadot 和任何其他平臺的集成而存在。 在本文中,我們將更多地討論如何從頭開始在底層上構建區(qū)塊鏈。
了解基材
該底層可以被描述為一個區(qū)塊鏈框架,具體來說是一個用于構建定制區(qū)塊鏈的開源框架。 這些區(qū)塊鏈可以完全自主運行,這意味著它們不依賴于任何外部技術來運行。
用于構建在底層上的區(qū)塊鏈框架是用于使用區(qū)塊鏈服務的工具和庫的集合,這些服務和區(qū)塊鏈服務在區(qū)塊鏈上存儲和操作,并使用其貨幣來處理交易。 該區(qū)塊鏈框架由點對點網(wǎng)絡、賬戶管理、交易區(qū)塊以及用戶與區(qū)塊鏈的交互組成。 而這一切都在基質中起作用。
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
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.
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.
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.
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.
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
這個托盤定義了一個貨幣特征,它有一個 抽象函數(shù)接口 ,稍后將實現(xiàn)實際的傳輸邏輯。
Rust pub TraitCurrencyAccountId { // -- snip -- fn Transfer( source: &AccountId, dest: &AccountId, value: Self::Balance, // 現(xiàn)在不用擔心最后一個參數(shù) Existence_requirement: ExistenceRequirement, ) - 調度結果; }
創(chuàng)世配置
它描述了 Genesis 配置的主要元素。
在文本編輯器中打開 node/src/chain_spec.rs 文件后,您可以找到
GenesisConfig
并設置某些存儲項的初始值。
例如,使用這段代碼
節(jié)點/src/chain_spec.rs
下面的文件,
Rust - GenesisConfig { GenesisConfig { system: SystemConfig { // 將 Wasm 運行時添加到存儲中。 代碼: wasm_binary.to_vec(), }, template_module: TemplateModuleConfig { 一些: 221u32, }, transaction_ payment: Default::default(), } }
結論
文本提供了如何在 Substrate 上構建區(qū)塊鏈的指南,強調理解底層技術并遵循分步過程的重要性。 但是,如果您想開始在 Substrate 上構建第一個區(qū)塊鏈的旅程,請訪問 Substrate 應用程序以獲取教程,該教程將逐步向您展示如何輕松且不浪費時間地在 Substrate 上構建區(qū)塊鏈。