Integrate PoE Into Your Runtime
The Proof of Existence pallet is done, but we still need to integrate it into your Runtime.
Let's take a look at that process.
Integration Steps
-
The first place to start is adding the
proof_of_existence
field to yourstruct Runtime
. -
Next you need to update your
fn new()
to also initializeproof_of_existence
. -
After, create a new concrete
type Content
which is a&'static str
. As mentioned, normally this would be a hash, but for simplicity we are once again a simple static string.If you want to use a hash now or in the future, it would be as simple as updating this one line to change all the types in your Runtime and Pallet. That is the kind of flexibility we have been working toward!
-
Then, implement
proof_of_existence::Config
forRuntime
, using yourtypes::Content
. -
At this point, things should already compile successfully, so use this as a checkpoint.
-
Introduce a new variant
ProofOfExistence
for theRuntimeCall
. -
Finally, update your
fn dispatch
logic to handle re-dispatchingProofOfExistence
calls to theproof_of_existence::Pallet
.
Hopefully from this process, you can see how all of the abstractions we have introduced has made integrating new Pallets into your runtime quite easy.
We will make this process even easier in the near future using macros!
By the end of this step, everything should compile without warnings.
mod balances; mod proof_of_existence; mod support; mod system; use crate::support::Dispatch; // These are the concrete types we will use in our simple state machine. // Modules are configured for these types directly, and they satisfy all of our // trait requirements. mod types { pub type AccountId = String; pub type Balance = u128; pub type BlockNumber = u32; pub type Nonce = u32; pub type Extrinsic = crate::support::Extrinsic<AccountId, crate::RuntimeCall>; pub type Header = crate::support::Header<BlockNumber>; pub type Block = crate::support::Block<Header, Extrinsic>; /* TODO: Add the concrete `Content` type for your runtime. */ } // These are all the calls which are exposed to the world. // Note that it is just an accumulation of the calls exposed by each module. pub enum RuntimeCall { Balances(balances::Call<Runtime>), /* TODO: Add a `ProofOfExistence` variant to access `proof_of_existence::Call`. */ } // This is our main Runtime. // It accumulates all of the different pallets we want to use. #[derive(Debug)] pub struct Runtime { system: system::Pallet<Self>, balances: balances::Pallet<Self>, /* TODO: Add `proof_of_existence` field to your `Runtime`. */ } impl system::Config for Runtime { type AccountId = types::AccountId; type BlockNumber = types::BlockNumber; type Nonce = types::Nonce; } impl balances::Config for Runtime { type Balance = types::Balance; } /* TODO: Implement proof_of_existence::Config` for `Runtime`. */ impl Runtime { // Create a new instance of the main Runtime, by creating a new instance of each pallet. fn new() -> Self { Self { system: system::Pallet::new(), balances: balances::Pallet::new(), /* TODO: Initialize the `proof_of_existence` pallet. */ } } // Execute a block of extrinsics. Increments the block number. fn execute_block(&mut self, block: types::Block) -> support::DispatchResult { self.system.inc_block_number(); if block.header.block_number != self.system.block_number() { return Err("block number does not match what is expected"); } // An extrinsic error is not enough to trigger the block to be invalid. We capture the // result, and emit an error message if one is emitted. for (i, support::Extrinsic { caller, call }) in block.extrinsics.into_iter().enumerate() { self.system.inc_nonce(&caller); let _res = self.dispatch(caller, call).map_err(|e| { eprintln!( "Extrinsic Error\n\tBlock Number: {}\n\tExtrinsic Number: {}\n\tError: {}", block.header.block_number, i, e ) }); } Ok(()) } } impl crate::support::Dispatch for Runtime { type Caller = <Runtime as system::Config>::AccountId; type Call = RuntimeCall; // Dispatch a call on behalf of a caller. Increments the caller's nonce. // // Dispatch allows us to identify which underlying module call we want to execute. // Note that we extract the `caller` from the extrinsic, and use that information // to determine who we are executing the call on behalf of. fn dispatch( &mut self, caller: Self::Caller, runtime_call: Self::Call, ) -> support::DispatchResult { // This match statement will allow us to correctly route `RuntimeCall`s // to the appropriate pallet level function. match runtime_call { RuntimeCall::Balances(call) => { self.balances.dispatch(caller, call)?; }, /* TODO: Dispatch `calls` to the `ProofOfExistence` pallet. */ } Ok(()) } } fn main() { // Create a new instance of the Runtime. // It will instantiate with it all the modules it uses. let mut runtime = Runtime::new(); let alice = "alice".to_string(); let bob = "bob".to_string(); let charlie = "charlie".to_string(); // Initialize the system with some initial balance. runtime.balances.set_balance(&alice, 100); // Here are the extrinsics in our block. // You can add or remove these based on the modules and calls you have set up. let block_1 = types::Block { header: support::Header { block_number: 1 }, extrinsics: vec![ support::Extrinsic { caller: alice.clone(), call: RuntimeCall::Balances(balances::Call::Transfer { to: bob, amount: 30 }), }, support::Extrinsic { caller: alice, call: RuntimeCall::Balances(balances::Call::Transfer { to: charlie, amount: 20 }), }, ], }; // Execute the extrinsics which make up our block. // If there are any errors, our system panics, since we should not execute invalid blocks. runtime.execute_block(block_1).expect("invalid block"); // Simply print the debug format of our runtime state. println!("{:#?}", runtime); }
mod balances; mod proof_of_existence; mod support; mod system; use crate::support::Dispatch; // These are the concrete types we will use in our simple state machine. // Modules are configured for these types directly, and they satisfy all of our // trait requirements. mod types { pub type AccountId = String; pub type Balance = u128; pub type BlockNumber = u32; pub type Nonce = u32; pub type Extrinsic = crate::support::Extrinsic<AccountId, crate::RuntimeCall>; pub type Header = crate::support::Header<BlockNumber>; pub type Block = crate::support::Block<Header, Extrinsic>; pub type Content = &'static str; } // These are all the calls which are exposed to the world. // Note that it is just an accumulation of the calls exposed by each module. pub enum RuntimeCall { Balances(balances::Call<Runtime>), ProofOfExistence(proof_of_existence::Call<Runtime>), } // This is our main Runtime. // It accumulates all of the different pallets we want to use. #[derive(Debug)] pub struct Runtime { system: system::Pallet<Self>, balances: balances::Pallet<Self>, proof_of_existence: proof_of_existence::Pallet<Self>, } impl system::Config for Runtime { type AccountId = types::AccountId; type BlockNumber = types::BlockNumber; type Nonce = types::Nonce; } impl balances::Config for Runtime { type Balance = types::Balance; } impl proof_of_existence::Config for Runtime { type Content = types::Content; } impl Runtime { // Create a new instance of the main Runtime, by creating a new instance of each pallet. fn new() -> Self { Self { system: system::Pallet::new(), balances: balances::Pallet::new(), proof_of_existence: proof_of_existence::Pallet::new(), } } // Execute a block of extrinsics. Increments the block number. fn execute_block(&mut self, block: types::Block) -> support::DispatchResult { self.system.inc_block_number(); if block.header.block_number != self.system.block_number() { return Err("block number does not match what is expected"); } // An extrinsic error is not enough to trigger the block to be invalid. We capture the // result, and emit an error message if one is emitted. for (i, support::Extrinsic { caller, call }) in block.extrinsics.into_iter().enumerate() { self.system.inc_nonce(&caller); let _res = self.dispatch(caller, call).map_err(|e| { eprintln!( "Extrinsic Error\n\tBlock Number: {}\n\tExtrinsic Number: {}\n\tError: {}", block.header.block_number, i, e ) }); } Ok(()) } } impl crate::support::Dispatch for Runtime { type Caller = <Runtime as system::Config>::AccountId; type Call = RuntimeCall; // Dispatch a call on behalf of a caller. Increments the caller's nonce. // // Dispatch allows us to identify which underlying module call we want to execute. // Note that we extract the `caller` from the extrinsic, and use that information // to determine who we are executing the call on behalf of. fn dispatch( &mut self, caller: Self::Caller, runtime_call: Self::Call, ) -> support::DispatchResult { // This match statement will allow us to correctly route `RuntimeCall`s // to the appropriate pallet level function. match runtime_call { RuntimeCall::Balances(call) => { self.balances.dispatch(caller, call)?; }, RuntimeCall::ProofOfExistence(call) => { self.proof_of_existence.dispatch(caller, call)?; }, } Ok(()) } } fn main() { // Create a new instance of the Runtime. // It will instantiate with it all the modules it uses. let mut runtime = Runtime::new(); let alice = "alice".to_string(); let bob = "bob".to_string(); let charlie = "charlie".to_string(); // Initialize the system with some initial balance. runtime.balances.set_balance(&alice, 100); // Here are the extrinsics in our block. // You can add or remove these based on the modules and calls you have set up. let block_1 = types::Block { header: support::Header { block_number: 1 }, extrinsics: vec![ support::Extrinsic { caller: alice.clone(), call: RuntimeCall::Balances(balances::Call::Transfer { to: bob, amount: 30 }), }, support::Extrinsic { caller: alice, call: RuntimeCall::Balances(balances::Call::Transfer { to: charlie, amount: 20 }), }, ], }; // Execute the extrinsics which make up our block. // If there are any errors, our system panics, since we should not execute invalid blocks. runtime.execute_block(block_1).expect("invalid block"); // Simply print the debug format of our runtime state. println!("{:#?}", runtime); }
diff --git a/src/main.rs b/src/main.rs
index 6dd23260..a86c9ee4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,12 +16,14 @@ mod types {
pub type Extrinsic = crate::support::Extrinsic<AccountId, crate::RuntimeCall>;
pub type Header = crate::support::Header<BlockNumber>;
pub type Block = crate::support::Block<Header, Extrinsic>;
+ /* TODO: Add the concrete `Content` type for your runtime. */
}
// These are all the calls which are exposed to the world.
// Note that it is just an accumulation of the calls exposed by each module.
pub enum RuntimeCall {
Balances(balances::Call<Runtime>),
+ /* TODO: Add a `ProofOfExistence` variant to access `proof_of_existence::Call`. */
}
// This is our main Runtime.
@@ -30,6 +32,7 @@ pub enum RuntimeCall {
pub struct Runtime {
system: system::Pallet<Self>,
balances: balances::Pallet<Self>,
+ /* TODO: Add `proof_of_existence` field to your `Runtime`. */
}
impl system::Config for Runtime {
@@ -42,10 +45,16 @@ impl balances::Config for Runtime {
type Balance = types::Balance;
}
+/* TODO: Implement proof_of_existence::Config` for `Runtime`. */
+
impl Runtime {
// Create a new instance of the main Runtime, by creating a new instance of each pallet.
fn new() -> Self {
- Self { system: system::Pallet::new(), balances: balances::Pallet::new() }
+ Self {
+ system: system::Pallet::new(),
+ balances: balances::Pallet::new(),
+ /* TODO: Initialize the `proof_of_existence` pallet. */
+ }
}
// Execute a block of extrinsics. Increments the block number.
@@ -88,6 +97,7 @@ impl crate::support::Dispatch for Runtime {
RuntimeCall::Balances(call) => {
self.balances.dispatch(caller, call)?;
},
+ /* TODO: Dispatch `calls` to the `ProofOfExistence` pallet. */
}
Ok(())
}
diff --git a/src/main.rs b/src/main.rs
index a86c9ee4..868309a7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,14 +16,14 @@ mod types {
pub type Extrinsic = crate::support::Extrinsic<AccountId, crate::RuntimeCall>;
pub type Header = crate::support::Header<BlockNumber>;
pub type Block = crate::support::Block<Header, Extrinsic>;
- /* TODO: Add the concrete `Content` type for your runtime. */
+ pub type Content = &'static str;
}
// These are all the calls which are exposed to the world.
// Note that it is just an accumulation of the calls exposed by each module.
pub enum RuntimeCall {
Balances(balances::Call<Runtime>),
- /* TODO: Add a `ProofOfExistence` variant to access `proof_of_existence::Call`. */
+ ProofOfExistence(proof_of_existence::Call<Runtime>),
}
// This is our main Runtime.
@@ -32,7 +32,7 @@ pub enum RuntimeCall {
pub struct Runtime {
system: system::Pallet<Self>,
balances: balances::Pallet<Self>,
- /* TODO: Add `proof_of_existence` field to your `Runtime`. */
+ proof_of_existence: proof_of_existence::Pallet<Self>,
}
impl system::Config for Runtime {
@@ -45,7 +45,9 @@ impl balances::Config for Runtime {
type Balance = types::Balance;
}
-/* TODO: Implement proof_of_existence::Config` for `Runtime`. */
+impl proof_of_existence::Config for Runtime {
+ type Content = types::Content;
+}
impl Runtime {
// Create a new instance of the main Runtime, by creating a new instance of each pallet.
@@ -53,7 +55,7 @@ impl Runtime {
Self {
system: system::Pallet::new(),
balances: balances::Pallet::new(),
- /* TODO: Initialize the `proof_of_existence` pallet. */
+ proof_of_existence: proof_of_existence::Pallet::new(),
}
}
@@ -97,7 +99,9 @@ impl crate::support::Dispatch for Runtime {
RuntimeCall::Balances(call) => {
self.balances.dispatch(caller, call)?;
},
- /* TODO: Dispatch `calls` to the `ProofOfExistence` pallet. */
+ RuntimeCall::ProofOfExistence(call) => {
+ self.proof_of_existence.dispatch(caller, call)?;
+ },
}
Ok(())
}