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
// 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::{
    gas_schedule::NativeCostIndex,
    loaded_data::runtime_types::Type,
    natives::function::{native_gas, NativeResult},
    pop_arg,
    values::{Value, Vector},
};
use smallvec::smallvec;
use std::collections::VecDeque;

// TODO: make cost tables extensible; right now we forward to one of the predefined cost indices
// as an approximation.
const SELF_COST_INDEX: NativeCostIndex = NativeCostIndex::LENGTH;
const SEND_COST_INDEX: NativeCostIndex = NativeCostIndex::EMIT_EVENT;
const EPOCH_TIME_INDEX: NativeCostIndex = NativeCostIndex::LENGTH;

/// 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,
}

pub fn actor_natives(
    async_addr: AccountAddress,
) -> Vec<(AccountAddress, Identifier, Identifier, NativeFunction)> {
    const NATIVES: &[(&str, &str, NativeFunction)] = &[
        ("Actor", "self", native_self),
        ("Actor", "virtual_time", native_virtual_time),
        ("Runtime", "send__0", native_send),
        ("Runtime", "send__1", native_send),
        ("Runtime", "send__2", native_send),
        ("Runtime", "send__3", native_send),
        ("Runtime", "send__4", native_send),
        ("Runtime", "send__5", native_send),
        ("Runtime", "send__6", native_send),
        ("Runtime", "send__7", native_send),
        ("Runtime", "send__8", native_send),
    ];
    native_functions::make_table(async_addr, NATIVES)
}

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

fn native_send(
    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 = native_gas(context.cost_table(), SEND_COST_INDEX, args.len());
    Ok(NativeResult::ok(cost, smallvec![]))
}

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