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
// This file is part of Gear.
//
// Copyright (C) 2021-2024 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 gear_common::gas_provider::Imbalance;

impl ExtManager {
    /// Spends given amount of gas from given `MessageId` in `GasTree`.
    ///
    /// Represents logic of burning gas by transferring gas from
    /// current `GasTree` owner to actual block producer.
    pub(crate) fn spend_gas(&mut self, id: MessageId, amount: u64) {
        if amount.is_zero() {
            return;
        }

        self.gas_tree.spend(id, amount).unwrap_or_else(|e| {
            let err_msg = format!(
                "spend_gas: failed spending gas. Message id - {id}, amount - {amount}. Got error - {e:?}"
            );

            unreachable!("{err_msg}");
        });

        let (external, multiplier, _) = self.gas_tree.get_origin_node(id).unwrap_or_else(|e| {
            let err_msg = format!(
                "spend_gas: failed getting origin node for the current one. Message id - {id}, Got error - {e:?}"
            );
            unreachable!("{err_msg}");
        });

        self.bank.spend_gas(external.cast(), amount, multiplier)
    }

    pub(crate) fn spend_burned(&mut self, id: MessageId, amount: u64) {
        self.gas_burned
            .entry(id)
            .and_modify(|v| *v = v.saturating_sub(Gas(amount)))
            .or_insert(Gas(amount));
        self.spend_gas(id, amount);
    }

    pub(crate) fn cost_by_storage_type(storage_type: StorageType) -> u64 {
        // Cost per block based on the storage used for holding
        let schedule = Schedule::default();
        let RentWeights {
            waitlist,
            dispatch_stash,
            reservation,
            mailbox,
            ..
        } = schedule.rent_weights;
        match storage_type {
            StorageType::Code => todo!("#646"),
            StorageType::Waitlist => waitlist.ref_time,
            StorageType::Mailbox => mailbox.ref_time,
            StorageType::DispatchStash => dispatch_stash.ref_time,
            StorageType::Program => todo!("#646"),
            StorageType::Reservation => reservation.ref_time,
        }
    }

    pub(crate) fn consume_and_retrieve(&mut self, id: impl Origin) {
        let id_origin = id.into_origin();
        let outcome = self.gas_tree.consume(id_origin).unwrap_or_else(|e| {
            let err_msg = format!(
                "consume_and_retrieve: failed consuming the rest of gas. Got error - {e:?}"
            );

            unreachable!("{err_msg}")
        });

        if let Some((imbalance, multiplier, external)) = outcome {
            let gas_left = imbalance.peek();
            log::debug!(
                "Consumed message {id_origin}. Unreserving {gas_left} (gas) from {external:?}",
            );

            if !gas_left.is_zero() {
                self.bank
                    .withdraw_gas(external.cast(), gas_left, multiplier);
            }
        }
    }

    pub(crate) fn charge_for_hold(
        &mut self,
        id: impl Origin,
        hold_interval: Interval<BlockNumber>,
        storage_type: StorageType,
    ) {
        let id: MessageId = id.cast();
        let current = self.block_height();

        // Deadline of the task.
        let deadline = hold_interval.finish.saturating_add(RESERVE_FOR);

        // The block number, which was the last paid for hold.
        //
        // Outdated tasks can end up being store for free - this case has to be
        // controlled by a correct selection of the `ReserveFor` constant.
        let paid_until = current.min(deadline);

        // holding duration
        let duration: u64 = paid_until.saturating_sub(hold_interval.start).into();

        // Cost per block based on the storage used for holding
        let cost = Self::cost_by_storage_type(storage_type);

        let amount = storage_type.try_into().map_or_else(
            |_| duration.saturating_mul(cost),
            |lock_id| {
                let prepaid = self.gas_tree.unlock_all(id, lock_id).unwrap_or_else(|e| {
                    let err_msg = format!(
                        "charge_for_hold: failed unlocking locked gas.
                        Got error - {e:?}"
                    );

                    unreachable!("{err_msg}");
                });

                prepaid.min(duration.saturating_mul(cost))
            },
        );

        if !amount.is_zero() {
            self.spend_gas(id, amount);
        }
    }
}