forked from shawntabrizi/rust-and-polkadot-workshop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
139 lines (124 loc) · 4.44 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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: 20 }),
},
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);
}