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
// Copyright (c) The Diem Core Contributors
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::async_vm::Message;
use better_any::{Tid, TidAble};
use move_binary_format::errors::PartialVMResult;
use move_core_types::{account_address::AccountAddress, identifier::Identifier};
use move_vm_runtime::{
    native_functions,
    native_functions::{NativeContext, NativeFunction},
};
use move_vm_types::{
    loaded_data::runtime_types::Type,
    natives::function::NativeResult,
    pop_arg,
    values::{Value, Vector},
};
use smallvec::smallvec;
use std::{collections::VecDeque, sync::Arc};

/// Environment extension for the Move VM which we pass down to native functions,
/// to implement message sending and retrieval of actor address.
#[derive(Tid)]
pub struct AsyncExtension {
    pub current_actor: AccountAddress,
    pub sent: Vec<Message>,
    pub virtual_time: u128,
    pub in_initializer: bool,
}

#[derive(Clone, Debug)]
pub struct GasParameters {
    pub self_: SelfGasParameters,
    pub send: SendGasParameters,
    pub virtual_time: VirtualTimeGasParameters,
}

impl GasParameters {
    pub fn zeros() -> Self {
        Self {
            self_: SelfGasParameters { base_cost: 0 },
            send: SendGasParameters {
                base_cost: 0,
                unit_cost: 0,
            },
            virtual_time: VirtualTimeGasParameters { base_cost: 0 },
        }
    }
}

pub fn actor_natives(
    async_addr: AccountAddress,
    gas_params: GasParameters,
) -> Vec<(AccountAddress, Identifier, Identifier, NativeFunction)> {
    let natives = [
        ("Actor", "self", make_native_self(gas_params.self_)),
        (
            "Actor",
            "virtual_time",
            make_native_virtual_time(gas_params.virtual_time),
        ),
        (
            "Runtime",
            "send__0",
            make_native_send(gas_params.send.clone()),
        ),
        (
            "Runtime",
            "send__1",
            make_native_send(gas_params.send.clone()),
        ),
        (
            "Runtime",
            "send__2",
            make_native_send(gas_params.send.clone()),
        ),
        (
            "Runtime",
            "send__3",
            make_native_send(gas_params.send.clone()),
        ),
        (
            "Runtime",
            "send__4",
            make_native_send(gas_params.send.clone()),
        ),
        (
            "Runtime",
            "send__5",
            make_native_send(gas_params.send.clone()),
        ),
        (
            "Runtime",
            "send__6",
            make_native_send(gas_params.send.clone()),
        ),
        (
            "Runtime",
            "send__7",
            make_native_send(gas_params.send.clone()),
        ),
        ("Runtime", "send__8", make_native_send(gas_params.send)),
    ];
    native_functions::make_table_from_iter(async_addr, natives)
}

#[derive(Clone, Debug)]
pub struct SelfGasParameters {
    base_cost: u64,
}

fn native_self(
    gas_params: &SelfGasParameters,
    context: &mut NativeContext,
    mut _ty_args: Vec<Type>,
    mut _args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    let ext = context.extensions().get::<AsyncExtension>();
    Ok(NativeResult::ok(
        gas_params.base_cost,
        smallvec![Value::address(ext.current_actor)],
    ))
}

fn make_native_self(gas_params: SelfGasParameters) -> NativeFunction {
    Arc::new(move |context, ty_args, args| native_self(&gas_params, context, ty_args, args))
}

#[derive(Clone, Debug)]
pub struct SendGasParameters {
    base_cost: u64,
    unit_cost: u64,
}

fn native_send(
    gas_params: &SendGasParameters,
    context: &mut NativeContext,
    mut _ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    let ext = context.extensions_mut().get_mut::<AsyncExtension>();
    let mut bcs_args = vec![];
    while args.len() > 2 {
        bcs_args.push(pop_arg!(args, Vector).to_vec_u8()?);
    }
    bcs_args.reverse();
    let message_hash = pop_arg!(args, u64);
    let target = pop_arg!(args, AccountAddress);
    ext.sent.push((target, message_hash, bcs_args));

    let cost = gas_params.base_cost + gas_params.unit_cost * args.len() as u64;

    Ok(NativeResult::ok(cost, smallvec![]))
}

fn make_native_send(gas_params: SendGasParameters) -> NativeFunction {
    Arc::new(move |context, ty_args, args| native_send(&gas_params, context, ty_args, args))
}

#[derive(Clone, Debug)]
pub struct VirtualTimeGasParameters {
    base_cost: u64,
}

fn native_virtual_time(
    gas_params: &VirtualTimeGasParameters,
    context: &mut NativeContext,
    mut _ty_args: Vec<Type>,
    mut _args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    let ext = context.extensions().get::<AsyncExtension>();
    Ok(NativeResult::ok(
        gas_params.base_cost,
        smallvec![Value::u128(ext.virtual_time)],
    ))
}

fn make_native_virtual_time(gas_params: VirtualTimeGasParameters) -> NativeFunction {
    Arc::new(move |context, ty_args, args| native_virtual_time(&gas_params, context, ty_args, args))
}