|
1 | 1 | // Copyright (c) 2021 MASSA LABS <[email protected]>
|
2 | 2 |
|
3 |
| -use crate::{ |
4 |
| - start_consensus_controller, |
5 |
| - tests::{ |
6 |
| - mock_execution_controller::MockExecutionController, |
7 |
| - mock_pool_controller::{MockPoolController, PoolCommandSink}, |
8 |
| - mock_protocol_controller::MockProtocolController, |
9 |
| - tools::{ |
10 |
| - self, create_block_with_operations, create_transaction, generate_ledger_file, |
11 |
| - propagate_block, |
12 |
| - }, |
13 |
| - }, |
| 3 | +use crate::tests::tools::{ |
| 4 | + self, create_block_with_operations, create_transaction, generate_ledger_file, propagate_block, |
14 | 5 | };
|
15 | 6 | use massa_models::{address::AddressHashSet, ledger::LedgerData};
|
16 | 7 | use massa_models::{Address, Amount, Slot};
|
@@ -79,110 +70,171 @@ async fn test_operations_check() {
|
79 | 70 | cfg.disable_block_creation = true;
|
80 | 71 | cfg.genesis_timestamp = cfg.genesis_timestamp.saturating_sub(10000.into());
|
81 | 72 |
|
82 |
| - // mock protocol & pool |
83 |
| - let (mut protocol_controller, protocol_command_sender, protocol_event_receiver) = |
84 |
| - MockProtocolController::new(); |
85 |
| - let (pool_controller, pool_command_sender) = MockPoolController::new(); |
86 |
| - let pool_sink = PoolCommandSink::new(pool_controller).await; |
87 |
| - let (mut _execution_controller, execution_command_sender, execution_event_receiver) = |
88 |
| - MockExecutionController::new(); |
89 |
| - |
90 |
| - // launch consensus controller |
91 |
| - let (consensus_command_sender, consensus_event_receiver, consensus_manager) = |
92 |
| - start_consensus_controller( |
93 |
| - cfg.clone(), |
94 |
| - execution_command_sender, |
95 |
| - execution_event_receiver, |
96 |
| - protocol_command_sender.clone(), |
97 |
| - protocol_event_receiver, |
98 |
| - pool_command_sender, |
99 |
| - None, |
100 |
| - None, |
101 |
| - 0, |
102 |
| - ) |
103 |
| - .await |
104 |
| - .expect("could not start consensus controller"); |
105 |
| - |
106 |
| - let genesis_ids = consensus_command_sender |
107 |
| - .get_block_graph_status(None, None) |
108 |
| - .await |
109 |
| - .expect("could not get block graph status") |
110 |
| - .genesis_blocks; |
111 |
| - |
112 |
| - // Valid block A sending 5 from addr1 to addr2 + reward 1 to addr1 |
113 |
| - let operation_1 = create_transaction(private_key_1, public_key_1, address_2, 5, 5, 1); |
114 |
| - let (id_a, block_a, _) = create_block_with_operations( |
115 |
| - &cfg, |
116 |
| - Slot::new(1, 0), |
117 |
| - &genesis_ids, |
118 |
| - private_key_1, |
119 |
| - vec![operation_1.clone()], |
120 |
| - ); |
121 |
| - propagate_block(&mut protocol_controller, block_a, true, 150).await; |
122 |
| - |
123 |
| - // assert address 1 has 1 coin at blocks (A, genesis_ids[1]) (see #269) |
124 |
| - let mut set = AddressHashSet::default(); |
125 |
| - set.insert(address_1); |
126 |
| - let res = consensus_command_sender |
127 |
| - .get_addresses_info(set) |
128 |
| - .await |
129 |
| - .unwrap() |
130 |
| - .get(&address_1) |
131 |
| - .unwrap() |
132 |
| - .ledger_info |
133 |
| - .candidate_ledger_info; |
134 |
| - assert_eq!(res.balance, Amount::from_str("1").unwrap()); |
135 |
| - |
136 |
| - // receive block b with invalid operation (not enough coins) |
137 |
| - let operation_2 = create_transaction(private_key_2, public_key_2, address_1, 10, 8, 1); |
138 |
| - let (_, block_2b, _) = create_block_with_operations( |
139 |
| - &cfg, |
140 |
| - Slot::new(1, 1), |
141 |
| - &vec![id_a, genesis_ids[1]], |
142 |
| - private_key_1, |
143 |
| - vec![operation_2], |
144 |
| - ); |
145 |
| - propagate_block(&mut protocol_controller, block_2b, false, 1000).await; |
146 |
| - |
147 |
| - // receive empty block b |
148 |
| - let (id_b, block_b, _) = create_block_with_operations( |
149 |
| - &cfg, |
150 |
| - Slot::new(1, 1), |
151 |
| - &vec![id_a, genesis_ids[1]], |
152 |
| - private_key_1, |
153 |
| - vec![], |
154 |
| - ); |
155 |
| - propagate_block(&mut protocol_controller, block_b, true, 150).await; |
156 |
| - |
157 |
| - // assert address 2 has 5 coins at block B |
158 |
| - let mut set = AddressHashSet::default(); |
159 |
| - set.insert(address_2); |
160 |
| - let res = consensus_command_sender |
161 |
| - .get_addresses_info(set) |
162 |
| - .await |
163 |
| - .unwrap() |
164 |
| - .get(&address_2) |
165 |
| - .unwrap() |
166 |
| - .ledger_info |
167 |
| - .candidate_ledger_info; |
168 |
| - assert_eq!(res.balance, Amount::from_str("5").unwrap()); |
169 |
| - |
170 |
| - // receive block with reused operation |
171 |
| - let (_, block_1c, _) = create_block_with_operations( |
172 |
| - &cfg, |
173 |
| - Slot::new(1, 0), |
174 |
| - &vec![id_a, id_b], |
175 |
| - private_key_1, |
176 |
| - vec![operation_1.clone()], |
| 73 | + tools::consensus_without_pool_test( |
| 74 | + cfg.clone(), |
| 75 | + async move |mut protocol_controller, consensus_command_sender, consensus_event_receiver| { |
| 76 | + let genesis_ids = consensus_command_sender |
| 77 | + .get_block_graph_status(None, None) |
| 78 | + .await |
| 79 | + .expect("could not get block graph status") |
| 80 | + .genesis_blocks; |
| 81 | + |
| 82 | + // Valid block A sending 5 from addr1 to addr2 + reward 1 to addr1 |
| 83 | + let operation_1 = create_transaction(private_key_1, public_key_1, address_2, 5, 5, 1); |
| 84 | + let (id_a, block_a, _) = create_block_with_operations( |
| 85 | + &cfg, |
| 86 | + Slot::new(1, 0), |
| 87 | + &genesis_ids, |
| 88 | + private_key_1, |
| 89 | + vec![operation_1.clone()], |
| 90 | + ); |
| 91 | + propagate_block(&mut protocol_controller, block_a, true, 150).await; |
| 92 | + |
| 93 | + // assert address 1 has 1 coin at blocks (A, genesis_ids[1]) (see #269) |
| 94 | + let mut set = AddressHashSet::default(); |
| 95 | + set.insert(address_1); |
| 96 | + let res = consensus_command_sender |
| 97 | + .get_addresses_info(set) |
| 98 | + .await |
| 99 | + .unwrap() |
| 100 | + .get(&address_1) |
| 101 | + .unwrap() |
| 102 | + .ledger_info |
| 103 | + .candidate_ledger_info; |
| 104 | + assert_eq!(res.balance, Amount::from_str("1").unwrap()); |
| 105 | + |
| 106 | + // receive block b with invalid operation (not enough coins) |
| 107 | + let operation_2 = create_transaction(private_key_2, public_key_2, address_1, 10, 8, 1); |
| 108 | + let (_, block_2b, _) = create_block_with_operations( |
| 109 | + &cfg, |
| 110 | + Slot::new(1, 1), |
| 111 | + &vec![id_a, genesis_ids[1]], |
| 112 | + private_key_1, |
| 113 | + vec![operation_2], |
| 114 | + ); |
| 115 | + propagate_block(&mut protocol_controller, block_2b, false, 1000).await; |
| 116 | + |
| 117 | + // receive empty block b |
| 118 | + let (id_b, block_b, _) = create_block_with_operations( |
| 119 | + &cfg, |
| 120 | + Slot::new(1, 1), |
| 121 | + &vec![id_a, genesis_ids[1]], |
| 122 | + private_key_1, |
| 123 | + vec![], |
| 124 | + ); |
| 125 | + propagate_block(&mut protocol_controller, block_b, true, 150).await; |
| 126 | + |
| 127 | + // assert address 2 has 5 coins at block B |
| 128 | + let mut set = AddressHashSet::default(); |
| 129 | + set.insert(address_2); |
| 130 | + let res = consensus_command_sender |
| 131 | + .get_addresses_info(set) |
| 132 | + .await |
| 133 | + .unwrap() |
| 134 | + .get(&address_2) |
| 135 | + .unwrap() |
| 136 | + .ledger_info |
| 137 | + .candidate_ledger_info; |
| 138 | + assert_eq!(res.balance, Amount::from_str("5").unwrap()); |
| 139 | + |
| 140 | + // receive block with reused operation |
| 141 | + let (_, block_1c, _) = create_block_with_operations( |
| 142 | + &cfg, |
| 143 | + Slot::new(1, 0), |
| 144 | + &vec![id_a, id_b], |
| 145 | + private_key_1, |
| 146 | + vec![operation_1.clone()], |
| 147 | + ); |
| 148 | + propagate_block(&mut protocol_controller, block_1c, false, 1000).await; |
| 149 | + |
| 150 | + ( |
| 151 | + protocol_controller, |
| 152 | + consensus_command_sender, |
| 153 | + consensus_event_receiver, |
| 154 | + ) |
| 155 | + }, |
| 156 | + ) |
| 157 | + .await; |
| 158 | +} |
| 159 | + |
| 160 | +#[tokio::test] |
| 161 | +#[serial] |
| 162 | +async fn test_execution_check() { |
| 163 | + let thread_count = 2; |
| 164 | + |
| 165 | + let private_key_1 = generate_random_private_key(); |
| 166 | + let public_key_1 = derive_public_key(&private_key_1); |
| 167 | + let address_1 = Address::from_public_key(&public_key_1); |
| 168 | + |
| 169 | + let mut ledger = HashMap::new(); |
| 170 | + ledger.insert(address_1, LedgerData::new(Amount::from_str("5").unwrap())); |
| 171 | + |
| 172 | + let ledger_file = generate_ledger_file(&ledger); |
| 173 | + let staking_keys: Vec<PrivateKey> = vec![private_key_1]; |
| 174 | + let staking_file = tools::generate_staking_keys_file(&staking_keys); |
| 175 | + let roll_counts_file = tools::generate_default_roll_counts_file(staking_keys.clone()); |
| 176 | + let mut cfg = tools::default_consensus_config( |
| 177 | + ledger_file.path(), |
| 178 | + roll_counts_file.path(), |
| 179 | + staking_file.path(), |
177 | 180 | );
|
178 |
| - propagate_block(&mut protocol_controller, block_1c, false, 1000).await; |
179 |
| - |
180 |
| - // stop controller while ignoring all commands |
181 |
| - let stop_fut = consensus_manager.stop(consensus_event_receiver); |
182 |
| - tokio::pin!(stop_fut); |
183 |
| - protocol_controller |
184 |
| - .ignore_commands_while(stop_fut) |
185 |
| - .await |
186 |
| - .unwrap(); |
187 |
| - pool_sink.stop().await; |
| 181 | + cfg.t0 = 1000.into(); |
| 182 | + cfg.future_block_processing_max_periods = 50; |
| 183 | + cfg.max_future_processing_blocks = 10; |
| 184 | + cfg.block_reward = Amount::from_str("1").unwrap(); |
| 185 | + cfg.thread_count = thread_count; |
| 186 | + cfg.operation_validity_periods = 10; |
| 187 | + cfg.disable_block_creation = true; |
| 188 | + cfg.genesis_timestamp = cfg.genesis_timestamp.saturating_sub(10000.into()); |
| 189 | + |
| 190 | + tools::consensus_without_pool_test( |
| 191 | + cfg.clone(), |
| 192 | + async move |mut protocol_controller, consensus_command_sender, consensus_event_receiver| { |
| 193 | + let genesis_ids = consensus_command_sender |
| 194 | + .get_block_graph_status(None, None) |
| 195 | + .await |
| 196 | + .expect("could not get block graph status") |
| 197 | + .genesis_blocks; |
| 198 | + |
| 199 | + // Valid block A executing some bytecode and spending 2 coins. |
| 200 | + let operation_1 = tools::create_executesc( |
| 201 | + private_key_1, |
| 202 | + public_key_1, |
| 203 | + 5, |
| 204 | + 5, |
| 205 | + Default::default(), |
| 206 | + 1, |
| 207 | + 2, |
| 208 | + 1, |
| 209 | + ); |
| 210 | + let (_id_a, block_a, _) = create_block_with_operations( |
| 211 | + &cfg, |
| 212 | + Slot::new(1, 0), |
| 213 | + &genesis_ids, |
| 214 | + private_key_1, |
| 215 | + vec![operation_1.clone()], |
| 216 | + ); |
| 217 | + propagate_block(&mut protocol_controller, block_a, true, 150).await; |
| 218 | + |
| 219 | + // assert the `coins` argument as been deducted from the balance of address 1. |
| 220 | + let mut set = AddressHashSet::default(); |
| 221 | + set.insert(address_1); |
| 222 | + let res = consensus_command_sender |
| 223 | + .get_addresses_info(set) |
| 224 | + .await |
| 225 | + .unwrap() |
| 226 | + .get(&address_1) |
| 227 | + .unwrap() |
| 228 | + .ledger_info |
| 229 | + .candidate_ledger_info; |
| 230 | + assert_eq!(res.balance, Amount::from_str("3").unwrap()); |
| 231 | + |
| 232 | + ( |
| 233 | + protocol_controller, |
| 234 | + consensus_command_sender, |
| 235 | + consensus_event_receiver, |
| 236 | + ) |
| 237 | + }, |
| 238 | + ) |
| 239 | + .await; |
188 | 240 | }
|
0 commit comments