gtest 1.9.2

Testing utils for developing Gear programs.
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// This file is part of Gear.
//
// Copyright (C) 2021-2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use super::*;
use crate::state::{
    blocks,
    programs::{GTestProgram, MockWasmProgram, PLACEHOLDER_MESSAGE_ID},
};
use core_processor::{ContextCharged, ForProgram, ProcessExecutionContext};
use gear_core::{
    code::{InstrumentedCodeAndMetadata, MAX_WASM_PAGES_AMOUNT},
    message::{DispatchKind, ReplyPacket, StoredDispatch},
    program::ProgramState,
    str::LimitedStr,
};

impl ExtManager {
    pub(crate) fn validate_and_route_dispatch(&mut self, dispatch: Dispatch) -> MessageId {
        self.validate_dispatch(&dispatch);
        let gas_limit = dispatch
            .gas_limit()
            .unwrap_or_else(|| unreachable!("message from program API always has gas"));
        self.gas_tree
            .create(
                dispatch.source(),
                dispatch.id(),
                gas_limit,
                dispatch.is_reply(),
            )
            .unwrap_or_else(|e| unreachable!("GasTree corrupted! {:?}", e));
        self.route_dispatch(dispatch)
    }

    fn validate_dispatch(&mut self, dispatch: &Dispatch) {
        let source = dispatch.source();
        let destination = dispatch.destination();

        if ProgramsStorageManager::is_program(source) {
            usage_panic!(
                "Sending messages allowed only from users id. Please, provide user id as source."
            );
        }

        assert!(
            self.no_code_program.is_empty(),
            "internal error: no code programs set is not empty"
        );

        // User must exist
        if !Accounts::exists(source) {
            usage_panic!("User's {source} balance is zero; mint value to it first.");
        }

        if !ProgramsStorageManager::is_active_program(destination) {
            usage_panic!("User message can't be sent to non active program");
        }

        let is_init_msg = dispatch.kind().is_init();
        // We charge ED only for init messages
        let maybe_ed = if is_init_msg { EXISTENTIAL_DEPOSIT } else { 0 };
        let balance = Accounts::balance(source);

        let gas_limit = dispatch
            .gas_limit()
            .unwrap_or_else(|| unreachable!("message from program API always has gas"));

        if gas_limit > MAX_USER_GAS_LIMIT {
            usage_panic!(
                "User message gas limit ({gas_limit}) is greater than \
                maximum allowed ({MAX_USER_GAS_LIMIT})."
            );
        };

        let gas_value = GAS_MULTIPLIER.gas_to_value(gas_limit);

        // Check sender has enough balance to cover dispatch costs
        if balance < { dispatch.value() + gas_value + maybe_ed } {
            usage_panic!(
                "Insufficient balance: user ({}) tries to send \
                ({}) value, ({}) gas and ED ({}), while his balance ({:?}). \
                Please, mint more balance to the user.",
                source,
                dispatch.value(),
                gas_value,
                maybe_ed,
                balance,
            );
        }

        // Charge for program ED upon creation
        if is_init_msg {
            Accounts::transfer(source, destination, EXISTENTIAL_DEPOSIT, false);
        }

        if dispatch.is_error_reply() {
            panic!("Internal error: users are not allowed to send error replies");
        }

        // It's necessary to deposit value so the source would have enough
        // balance locked (in gear-bank) for future value processing.
        if dispatch.value() != 0 {
            self.bank.deposit_value(source, dispatch.value(), false);
        }

        // Deposit gas
        self.bank.deposit_gas(source, gas_limit, false);
    }

    pub(crate) fn route_dispatch(&mut self, dispatch: Dispatch) -> MessageId {
        let stored_dispatch = dispatch.into_stored();
        if ProgramsStorageManager::is_user(stored_dispatch.destination()) {
            panic!("Program API only sends message to programs.")
        }

        let message_id = stored_dispatch.id();
        self.dispatches.push_back(stored_dispatch);

        message_id
    }

    pub(crate) fn run_new_block(&mut self, allowance: Gas) -> BlockRunResult {
        self.gas_allowance = allowance;
        self.blocks_manager.next_block();
        let new_block_bn = self.block_height();

        log::debug!("⚙️  Initialization of block #{new_block_bn}");

        self.process_tasks(new_block_bn);
        let total_processed = self.process_messages();

        log::debug!("⚙️  Finalization of block #{new_block_bn}");

        // Clean up no code programs for the next block
        self.no_code_program.clear();

        BlockRunResult {
            block_info: self.blocks_manager.get(),
            gas_allowance_spent: GAS_ALLOWANCE - self.gas_allowance,
            succeed: mem::take(&mut self.succeed),
            failed: mem::take(&mut self.failed),
            not_executed: mem::take(&mut self.not_executed),
            total_processed,
            log: mem::take(&mut self.log)
                .into_iter()
                .map(CoreLog::from)
                .collect(),
            gas_burned: mem::take(&mut self.gas_burned),
        }
    }

    pub(crate) fn process_tasks(&mut self, current_bn: u32) {
        let db_weights = DbWeights::default();

        let (first_incomplete_block, were_empty) = self
            .first_incomplete_tasks_block
            .take()
            .map(|block| {
                self.gas_allowance = self.gas_allowance.saturating_sub(db_weights.write.ref_time);
                (block, false)
            })
            .unwrap_or_else(|| {
                self.gas_allowance = self.gas_allowance.saturating_sub(db_weights.read.ref_time);
                (current_bn, true)
            });

        // When we had to stop processing due to insufficient gas allowance.
        let mut stopped_at = None;

        let missing_blocks = first_incomplete_block..=current_bn;
        for bn in missing_blocks {
            if self.gas_allowance <= db_weights.write.ref_time.saturating_mul(2) {
                stopped_at = Some(bn);
                log::debug!(
                    "Stopped processing tasks at: {stopped_at:?} due to insufficient allowance"
                );
                break;
            }

            let mut last_task = None;
            for task in self.task_pool.drain_prefix_keys(bn) {
                // decreasing allowance due to DB deletion
                self.on_task_pool_change();

                let max_task_gas = task::get_maximum_task_gas(&task);
                log::debug!(
                    "⚙️  Processing task {task:?} at the block {bn}, max gas = {max_task_gas}"
                );

                if self.gas_allowance.saturating_sub(max_task_gas) <= db_weights.write.ref_time {
                    // Since the task is not processed write DB cost should be refunded.
                    // In the same time gas allowance should be charged for read DB cost.
                    self.gas_allowance = self
                        .gas_allowance
                        .saturating_add(db_weights.write.ref_time)
                        .saturating_sub(db_weights.read.ref_time);

                    last_task = Some(task);

                    log::debug!("Not enough gas to process task at {bn:?}");

                    break;
                }

                let task_gas = task.process_with(self);

                self.gas_allowance = self.gas_allowance.saturating_sub(task_gas);

                if self.gas_allowance <= db_weights.write.ref_time + db_weights.read.ref_time {
                    stopped_at = Some(bn);
                    log::debug!("Stopping processing tasks at (read next): {stopped_at:?}");
                    break;
                }
            }

            if let Some(task) = last_task {
                stopped_at = Some(bn);

                self.gas_allowance = self.gas_allowance.saturating_add(db_weights.write.ref_time);

                self.task_pool.add(bn, task).unwrap_or_else(|e| {
                    let err_msg = format!(
                        "process_tasks: failed adding not processed last task to task pool. \
                        Bn - {bn:?}, task - {task:?}. Got error - {e:?}"
                    );

                    unreachable!("{err_msg}");
                });
                self.on_task_pool_change();
            }

            if stopped_at.is_some() {
                break;
            }
        }

        if let Some(stopped_at) = stopped_at {
            if were_empty {
                // Charging for inserting into storage of the first block of incomplete tasks,
                // if we were reading it only (they were empty).
                self.gas_allowance = self.gas_allowance.saturating_sub(db_weights.write.ref_time);
            }

            self.first_incomplete_tasks_block = Some(stopped_at);
        }
    }

    fn process_messages(&mut self) -> u32 {
        self.messages_processing_enabled = true;

        let block_config = self.block_config();

        log::debug!(
            "⚙️  Message queue processing at the block {}",
            self.block_height()
        );
        let mut total_processed = 0;
        while self.messages_processing_enabled {
            let dispatch = match self.dispatches.pop_front() {
                Some(dispatch) => dispatch,
                None => break,
            };

            let journal = self.process_dispatch(&block_config, dispatch);
            core_processor::handle_journal(journal, self);

            total_processed += 1;
        }

        total_processed
    }

    pub(crate) fn process_dispatch(
        &mut self,
        block_config: &BlockConfig,
        dispatch: StoredDispatch,
    ) -> Vec<JournalNote> {
        let destination_id = dispatch.destination();
        let dispatch_id = dispatch.id();
        let dispatch_kind = dispatch.kind();

        let gas_limit = self
            .gas_tree
            .get_limit(dispatch_id)
            .unwrap_or_else(|e| unreachable!("GasTree corrupted! {:?}", e));

        log::debug!(
            "Processing message ({:?}): {:?} to {:?} / gas_limit: {}, gas_allowance: {}",
            dispatch_kind,
            dispatch_id,
            destination_id,
            gas_limit,
            self.gas_allowance,
        );

        let context = ContextCharged::new(
            destination_id,
            dispatch.into_incoming(gas_limit),
            self.gas_allowance,
        );

        let context = match context.charge_for_program(block_config) {
            Ok(context) => context,
            Err(journal) => {
                return journal;
            }
        };

        ProgramsStorageManager::modify_program(destination_id, |program| {
            let Some(program) = program else {
                log::debug!(
                    "Message {dispatch_id} is sent to program {destination_id} which does not exist"
                );
                return core_processor::process_code_not_exists(context);
            };

            match program.as_primary_program() {
                Program::Active(active_program) => {
                    // Check for invalid init message to already initialized program
                    if active_program.state == ProgramState::Initialized && dispatch_kind.is_init()
                    {
                        unreachable!(
                            "Got init message for already initialized program. \
                            Current init message id: {dispatch_id:?}, already initialized program id: {destination_id:?}"
                        );
                    }

                    // Handle uninitialized program states
                    if let ProgramState::Uninitialized { message_id } = active_program.state {
                        // Check for incomplete user programs (placeholder message ID)
                        if message_id == PLACEHOLDER_MESSAGE_ID {
                            unreachable!(
                                "Got message sent to incomplete user program. First send manually via `Program` API \
                                message to {destination_id} program, so it's completely created and possibly initialized."
                            );
                        }

                        // If the destination program is uninitialized, then we allow
                        // to process message, if it's a reply (async init case) or init message.
                        // Otherwise, we return error reply.
                        if message_id != dispatch_id && !dispatch_kind.is_reply() {
                            if dispatch_kind.is_init() {
                                // This should never happen as the protocol doesn't allow second
                                // init messages
                                unreachable!(
                                    "Got init message which is not the first init message to the program. \
                                    Current init message id: {dispatch_id:?}, original init message id: {message_id:?}, \
                                    program: {destination_id:?}"
                                );
                            }

                            return core_processor::process_uninitialized(context);
                        }
                    }
                }
                Program::Terminated(_) => {
                    log::debug!(
                        "Message {dispatch_id} is sent to program {destination_id} which is failed to initialize"
                    );
                    return core_processor::process_failed_init(context);
                }
                Program::Exited(inheritor) => {
                    log::debug!("Message {dispatch_id} is sent to exited program {destination_id}");
                    return core_processor::process_program_exited(context, *inheritor);
                }
            };

            // Dispatch to mock or regular program based on the type
            match program {
                GTestProgram::Default(Program::Active(active_program)) => self.process_program(
                    block_config,
                    context,
                    active_program,
                    dispatch_kind,
                    destination_id,
                ),
                GTestProgram::Mock(mock_program) => {
                    self.process_mock_program(context, mock_program)
                }
                _ => {
                    unreachable!("Program {destination_id:?} is in unexpected state - {program:?}");
                }
            }
        })
    }

    fn process_program(
        &mut self,
        block_config: &BlockConfig,
        context: ContextCharged<ForProgram>,
        active_program: &mut ActiveProgram<BlockNumber>,
        dispatch_kind: DispatchKind,
        destination_id: ActorId,
    ) -> Vec<JournalNote> {
        let context = match context.charge_for_code_metadata(block_config) {
            Ok(context) => context,
            Err(journal) => {
                return journal;
            }
        };

        let code_id = active_program.code_id;
        let code_metadata = self.code_metadata(code_id).cloned().unwrap_or_else(|| {
            unreachable!(
                "Code metadata for program {destination_id:?} with code id {} not found",
                active_program.code_id
            )
        });

        // TODO: This is an early check, it should be moved after re-instrumentation, in
        // case re-instrumentation will change/add exports
        if !code_metadata.exports().contains(&dispatch_kind) {
            let (destination_id, dispatch, gas_counter, _) = context.into_parts();

            let notes = core_processor::process_success(
                SuccessfulDispatchResultKind::Success,
                DispatchResult::success(&dispatch, destination_id, gas_counter.to_amount()),
                dispatch,
            );

            return notes;
        }

        // No re-instrumentation is needed for test environment,
        // as `gtest` test runtime doesn't provide an opportunity
        // to change the Schedule with weights data.
        let instrumented_code_len = code_metadata.instrumented_code_len().unwrap_or_else(|| {
            let err_msg = format!(
                "Code metadata for the existing program does not contain \
                instrumented code length. Program id -'{destination_id:?}', Code id - '{code_id:?}'."
            );

            log::error!("{err_msg}");
            unreachable!("{err_msg}");
        });

        // Adjust gas counters for fetching instrumented binary code.
        let context =
            match context.charge_for_instrumented_code(block_config, instrumented_code_len) {
                Ok(context) => context,
                Err(journal) => return journal,
            };

        let instrumented_code = self.instrumented_code(code_id).cloned().unwrap_or_else(|| {
            let err_msg = format!(
                "Failed to get instrumented code for the existing program. \
                    Program id -'{destination_id:?}', Code id - '{code_id:?}'."
            );

            log::error!("{err_msg}");
            unreachable!("{err_msg}");
        });

        let context = match context
            .charge_for_allocations(block_config, active_program.allocations_tree_len)
        {
            Ok(context) => context,
            Err(journal) => {
                return journal;
            }
        };

        let allocations = if active_program.allocations_tree_len != 0 {
            ProgramsStorageManager::allocations(destination_id).unwrap_or_else(||
                unreachable!(
                    "`allocations_tree_len` {} is not zero, so program {destination_id:?} must have allocations",
                    active_program.allocations_tree_len,
                )
            )
        } else {
            Default::default()
        };

        let actor_data = ExecutableActorData {
            allocations,
            memory_infix: active_program.memory_infix,
            gas_reservation_map: active_program.gas_reservation_map.clone(),
        };

        let context = match context.charge_for_module_instantiation(
            block_config,
            actor_data,
            instrumented_code.instantiated_section_sizes(),
            &code_metadata,
        ) {
            Ok(context) => context,
            Err(journal) => {
                return journal;
            }
        };

        let balance = Accounts::reducible_balance(destination_id);

        core_processor::process::<Ext<LazyPagesNative>>(
            block_config,
            ProcessExecutionContext::new(
                context,
                InstrumentedCodeAndMetadata {
                    instrumented_code,
                    metadata: code_metadata,
                },
                balance,
            ),
            (
                blocks::current_epoch_random(),
                block_config.block_info.height,
            ),
        )
        .unwrap_or_else(|e| unreachable!("core-processor logic violated: {}", e))
    }

    fn process_mock_program(
        &mut self,
        context: ContextCharged<ForProgram>,
        mock_program: &mut MockWasmProgram,
    ) -> Vec<JournalNote> {
        let (destination_id, dispatch, gas_counter, _) = context.into_parts();
        let payload = dispatch.payload().to_vec();
        let dispatch_kind = dispatch.kind();
        let handlers = mock_program.handlers_mut();

        let outcome = match dispatch_kind {
            DispatchKind::Init => {
                log::debug!("Calling mock program init for {destination_id:?}");
                handlers.init(payload)
            }
            DispatchKind::Handle => {
                log::debug!("Calling mock program handle for {destination_id:?}");
                handlers.handle(payload)
            }
            _ => unreachable!("Unsupported dispatch kind for mock program"),
        };

        match outcome {
            Ok(maybe_reply) => {
                let (generated_dispatches, reply_sent) = maybe_reply
                    .map(|payload| {
                        let reply_message = ReplyMessage::from_packet(
                            MessageId::generate_reply(dispatch.id()),
                            ReplyPacket::new(payload.try_into().expect("too big payload"), 0),
                        );
                        let dispatch = reply_message.into_dispatch(
                            destination_id,
                            dispatch.source(),
                            dispatch.id(),
                        );

                        (vec![(dispatch, 0, None)], true)
                    })
                    .unwrap_or_default();

                let dispatch_result = {
                    let mut dp =
                        DispatchResult::success(&dispatch, destination_id, gas_counter.to_amount());
                    dp.generated_dispatches = generated_dispatches;
                    dp.reply_sent = reply_sent;

                    dp
                };

                core_processor::process_success(
                    SuccessfulDispatchResultKind::Success,
                    dispatch_result,
                    dispatch,
                )
            }
            Err(error_msg) => {
                handlers.debug(error_msg);

                let err = ActorExecutionErrorReplyReason::Trap(TrapExplanation::Panic(
                    LimitedStr::from_small_str(error_msg).into(),
                ));

                core_processor::process_execution_error(
                    dispatch,
                    destination_id,
                    gas_counter.burned(),
                    Default::default(),
                    err,
                )
            }
        }
    }

    pub(crate) fn block_config(&self) -> BlockConfig {
        let schedule = Schedule::default();
        BlockConfig {
            block_info: self.blocks_manager.get(),
            performance_multiplier: gsys::Percent::new(100),
            forbidden_funcs: Default::default(),
            reserve_for: RESERVE_FOR,
            gas_multiplier: gsys::GasMultiplier::from_value_per_gas(VALUE_PER_GAS),
            costs: schedule.process_costs(),
            existential_deposit: EXISTENTIAL_DEPOSIT,
            mailbox_threshold: schedule.rent_weights.mailbox_threshold.ref_time,
            max_reservations: MAX_RESERVATIONS,
            max_pages: MAX_WASM_PAGES_AMOUNT.into(),
            outgoing_limit: OUTGOING_LIMIT,
            outgoing_bytes_limit: OUTGOING_BYTES_LIMIT,
        }
    }
}