展示如何构建一个 Substrate 区块链的 Runtime Library
- make sure node and npm is installed
- Rust and other dependencies for substrate
1 |
curl https://getsubstrate.io -sSf | bash |
进入到上面的项目中。
1 |
./substrate-node-new substrate-node-template <author-name> |
This will create a folder called substrate-node-template and substrate-ui with the corresponding repositories cloned in them.
Launch a Blockchain
If you have set up everything correctly, you can now start a substrate dev chain! In substrate-node-template run:
1 |
./target/release/substrate-node-template --dev |
if you run into any errors starting or running your node, you may need to purge the chain files on your computer. You can do this by running:
1 |
// 优先使用这个 |
If everything is working it should start producing blocks!
To interact with the blockchain, you need to start the Substrate UI. Navigate to the substrate-ui folder and run:
1 |
npm run dev |
Finally, if open your browser to http://localhost:8000, you should be able to interact with your new chain!
Add Alice to your network
Alice is a hard-coded account in the substrate system, which is pre-funded to make your life easier. Alice may already be added to your network if you used the latest version of the substrate-node-template.
1 |
subkey restore Alice |
Then in the Substrate UI, you can go into the Wallet section and add Alice using her seed and name.
If all is working correctly, you can now go into the Send Funds section and send funds from Alice to Default. You will see that Alice has a bunch of units pre-funded in her account, so send some and wait for the green checkmark and an updated balance for Default to show that the transfer has been successfully recorded on the blockchain.
Create a new runtime module
Now it’s time to create our own runtime.
Open up the substrate-node-template folder and create a new file:
1 |
./runtime/src/demo.rs |
This is where our new runtime module will live. Inline comments will hopefully give you insight to what the code is doing.
First, we will need to import a few libraries at the top of our file:
1 |
// Encoding library |
All modules need to expose a configuration trait. In this case, our trait inherits from the Balances module’s trait since we will be using features and functions made available to us there.
1 |
pub trait Trait: balances::Trait {} |
近期评论