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
//! Subcall dispatch.
use std::cell::RefCell;

use crate::{
    context::Context,
    dispatcher,
    module::CallResult,
    modules::core::{Error, API as _},
    runtime::Runtime,
    state::{CurrentState, Options, TransactionResult, TransactionWithMeta},
    types::{token, transaction, transaction::CallerAddress},
};

thread_local! {
    /// The subcall stack for tracking depth and other metadata.
    static SUBCALL_STACK: RefCell<SubcallStack> = RefCell::new(SubcallStack::new());
}

/// Subcall validator.
pub trait Validator {
    /// Validate a subcall before it is performed.
    fn validate(&self, info: &SubcallInfo) -> Result<(), Error>;
}

/// A validator which allows everything.
pub struct AllowAllValidator;

impl Validator for AllowAllValidator {
    fn validate(&self, _info: &SubcallInfo) -> Result<(), Error> {
        Ok(())
    }
}

/// Information about a subcall to be dispatched.
#[derive(Clone, Debug)]
pub struct SubcallInfo {
    /// Address of the caller.
    pub caller: CallerAddress,
    /// Method to call.
    pub method: String,
    /// Subcall body.
    pub body: cbor::Value,
    /// Maximum subcall depth.
    pub max_depth: u16,
    /// Maximum gas amount that can be consumed.
    pub max_gas: u64,
}

/// Result of dispatching a subcall.
#[derive(Debug)]
pub struct SubcallResult {
    /// Result of the subcall.
    pub call_result: CallResult,
    /// Gas used by the subcall.
    pub gas_used: u64,
}

struct SubcallStackEntry {
    validator: Box<dyn Validator>,
}

struct SubcallStack {
    stack: Vec<SubcallStackEntry>,
}

impl SubcallStack {
    fn new() -> Self {
        Self { stack: Vec::new() }
    }

    fn depth(&self) -> u16 {
        self.stack.len() as u16
    }

    fn push(&mut self, entry: SubcallStackEntry) {
        self.stack.push(entry);
    }

    fn pop(&mut self) {
        self.stack.pop();
    }

    fn run_validators(&self, info: &SubcallInfo) -> Result<(), Error> {
        for entry in &self.stack {
            entry.validator.validate(info)?;
        }
        Ok(())
    }
}

struct SubcallStackGuard;

impl Drop for SubcallStackGuard {
    fn drop(&mut self) {
        SUBCALL_STACK.with(|ss| {
            ss.borrow_mut().pop();
        });
    }
}

/// The current subcall depth.
pub fn get_current_subcall_depth<C: Context>(_ctx: &C) -> u16 {
    SUBCALL_STACK.with(|ss| ss.borrow().depth())
}

/// Perform a subcall.
pub fn call<C: Context, V: Validator + 'static>(
    ctx: &C,
    info: SubcallInfo,
    validator: V,
) -> Result<SubcallResult, Error> {
    // Run validator first.
    validator.validate(&info)?;

    // Update the subcall stack after doing validation.
    SUBCALL_STACK.with(|ss| {
        let mut stack = ss.borrow_mut();

        // Ensure the call depth is not too large.
        if stack.depth() >= info.max_depth {
            return Err(Error::CallDepthExceeded(stack.depth() + 1, info.max_depth));
        }

        // Run existing validators.
        stack.run_validators(&info)?;

        // Push subcall to stack.
        stack.push(SubcallStackEntry {
            validator: Box::new(validator) as Box<dyn Validator>,
        });

        Ok(())
    })?;
    let _guard = SubcallStackGuard; // Ensure subcall is popped from stack.

    // Generate an internal transaction.
    let tx = transaction::Transaction {
        version: transaction::LATEST_TRANSACTION_VERSION,
        call: transaction::Call {
            format: transaction::CallFormat::Plain,
            method: info.method,
            body: info.body,
            ..Default::default()
        },
        auth_info: transaction::AuthInfo {
            signer_info: vec![transaction::SignerInfo {
                // The call is being performed on the caller's behalf.
                address_spec: transaction::AddressSpec::Internal(info.caller),
                nonce: 0,
            }],
            fee: transaction::Fee {
                amount: token::BaseUnits::new(0, token::Denomination::NATIVE),
                // Limit gas usage inside the child context to the allocated maximum.
                gas: info.max_gas,
                // Propagate consensus message limit.
                consensus_messages: CurrentState::with(|state| state.emitted_messages_max(ctx)),
            },
            ..Default::default()
        },
    };
    let call = tx.call.clone(); // TODO: Avoid clone.

    // Execute a transaction in a child context.
    let (call_result, gas) = CurrentState::with_transaction_opts(
        Options::new()
            .with_internal(true)
            .with_tx(TransactionWithMeta::internal(tx)),
        || {
            // Dispatch the call.
            let (result, _) = dispatcher::Dispatcher::<C::Runtime>::dispatch_tx_call(
                &ctx.clone(), // Must clone to avoid infinite type.
                call,
                &Default::default(),
            );
            // Retrieve remaining gas.
            let gas = <C::Runtime as Runtime>::Core::remaining_tx_gas();

            // Commit store and return emitted tags and messages on successful dispatch,
            // otherwise revert state and ignore any emitted events/messages.
            if result.is_success() {
                TransactionResult::Commit((result, gas))
            } else {
                // Ignore tags/messages on failure.
                TransactionResult::Rollback((result, gas))
            }
        },
    );

    // Compute the amount of gas used.
    let gas_used = info.max_gas.saturating_sub(gas);

    Ok(SubcallResult {
        call_result,
        gas_used,
    })
}