Introduce the System Pallet
We have basically completed the creation of a basic Balances Pallet. This is the pallet that most users will interact with.
However, your blockchain usually needs to keep track of many other pieces of data to function properly.
For this, we will create a new pallet called the System Pallet.
What is the System Pallet?
The System Pallet is a "meta"-pallet which stores all the metadata needed for your blockchain to function. For example, the current blocknumber or the nonce of users on your blockchain.
This pallet does not need to expose any functions to end users, but can still play an important role in our overall state transition function.
We will see the importance of the System Pallet evolve as you walk through the steps of building it.
Create the System Pallet
- Create a new file
src/system.rs
in your project. - Copy the starting template provided, then complete the steps outlined by the template code.
- Import the
system
module into yourmain.rs
file.
You will notice that the instructions here are quite brief. You have already done all of these steps before, so you should already be familiar with everything you need to complete this step.
Confirm everything is compiling. You should expect some "never used/constructed" warnings. That is okay.
mod balances; /* TODO: Import your new `system` module. */ fn main() { println!("Hello, world!"); }
#![allow(unused)] fn main() { /* TODO: You might need to update your imports. */ /// This is the System Pallet. /// It handles low level state needed for your blockchain. pub struct Pallet { /// The current block number. /* TODO: Create a field `block_number` that stores a `u32`. */ /// A map from an account to their nonce. /* TODO: Create a field `nonce` that is a `BTreeMap` from `String` to `u32`. */ } impl Pallet { /// Create a new instance of the System Pallet. pub fn new() -> Self { /* TODO: Return a new instance of the `Pallet` struct. */ } } }
mod balances; mod system; fn main() { println!("Hello, world!"); }
#![allow(unused)] fn main() { use std::collections::BTreeMap; /// This is the System Pallet. /// It handles low level state needed for your blockchain. pub struct Pallet { /// The current block number. block_number: u32, /// A map from an account to their nonce. nonce: BTreeMap<String, u32>, } impl Pallet { /// Create a new instance of the System Pallet. pub fn new() -> Self { Self { block_number: 0, nonce: BTreeMap::new() } } } }
diff --git a/src/main.rs b/src/main.rs
index ea8024e5..d2fffb8b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,5 @@
mod balances;
+/* TODO: Import your new `system` module. */
fn main() {
println!("Hello, world!");
diff --git a/src/system.rs b/src/system.rs
new file mode 100644
index 00000000..b4e6ef4e
--- /dev/null
+++ b/src/system.rs
@@ -0,0 +1,17 @@
+/* TODO: You might need to update your imports. */
+
+/// This is the System Pallet.
+/// It handles low level state needed for your blockchain.
+pub struct Pallet {
+ /// The current block number.
+ /* TODO: Create a field `block_number` that stores a `u32`. */
+ /// A map from an account to their nonce.
+ /* TODO: Create a field `nonce` that is a `BTreeMap` from `String` to `u32`. */
+}
+
+impl Pallet {
+ /// Create a new instance of the System Pallet.
+ pub fn new() -> Self {
+ /* TODO: Return a new instance of the `Pallet` struct. */
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index d2fffb8b..815fd741 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,5 @@
mod balances;
-/* TODO: Import your new `system` module. */
+mod system;
fn main() {
println!("Hello, world!");
diff --git a/src/system.rs b/src/system.rs
index b4e6ef4e..99d537c8 100644
--- a/src/system.rs
+++ b/src/system.rs
@@ -1,17 +1,17 @@
-/* TODO: You might need to update your imports. */
+use std::collections::BTreeMap;
/// This is the System Pallet.
/// It handles low level state needed for your blockchain.
pub struct Pallet {
/// The current block number.
- /* TODO: Create a field `block_number` that stores a `u32`. */
+ block_number: u32,
/// A map from an account to their nonce.
- /* TODO: Create a field `nonce` that is a `BTreeMap` from `String` to `u32`. */
+ nonce: BTreeMap<String, u32>,
}
impl Pallet {
/// Create a new instance of the System Pallet.
pub fn new() -> Self {
- /* TODO: Return a new instance of the `Pallet` struct. */
+ Self { block_number: 0, nonce: BTreeMap::new() }
}
}