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

use std::{
    collections::HashMap,
    error::Error,
    fmt::{Debug, Display, Formatter},
};

use move_binary_format::errors::{Location, PartialVMError, PartialVMResult, VMError, VMResult};
use move_core_types::{
    account_address::AccountAddress,
    effects::{ChangeSet, Event},
    gas_schedule::GasAlgebra,
    identifier::Identifier,
    language_storage::{ModuleId, StructTag, TypeTag},
    resolver::MoveResolver,
    vm_status::StatusCode,
};
use move_vm_runtime::{
    move_vm::MoveVM,
    native_extensions::NativeContextExtensions,
    native_functions::NativeFunction,
    session::{SerializedReturnValues, Session},
};
use move_vm_types::{
    gas_schedule::GasStatus,
    values::{Reference, Value},
};

use crate::{actor_metadata, actor_metadata::ActorMetadata, natives, natives::AsyncExtension};

/// Represents an instance of an async VM.
pub struct AsyncVM {
    move_vm: MoveVM,
    actor_metadata: HashMap<ModuleId, ActorMetadata>,
    message_table: HashMap<u64, (ModuleId, Identifier)>,
}

impl AsyncVM {
    /// Creates a new VM, registering the given natives and actors.
    pub fn new<I, A>(async_lib_addr: AccountAddress, natives: I, actors: A) -> VMResult<Self>
    where
        I: IntoIterator<Item = (AccountAddress, Identifier, Identifier, NativeFunction)>,
        A: IntoIterator<Item = ActorMetadata>,
    {
        let actor_metadata: HashMap<ModuleId, ActorMetadata> = actors
            .into_iter()
            .map(|a| (a.module_id.clone(), a))
            .collect();
        let message_table: HashMap<u64, (ModuleId, Identifier)> = actor_metadata
            .values()
            .flat_map(|a| {
                a.messages.iter().map(move |m| {
                    (
                        actor_metadata::message_hash(&a.module_id, m.as_ident_str()),
                        (a.module_id.clone(), m.clone()),
                    )
                })
            })
            .collect();
        Ok(AsyncVM {
            move_vm: MoveVM::new(
                natives
                    .into_iter()
                    .chain(natives::actor_natives(async_lib_addr).into_iter()),
            )?,
            actor_metadata,
            message_table,
        })
    }

    /// Creates a new session.
    pub fn new_session<'r, 'l, S: MoveResolver>(
        &'l self,
        for_actor: AccountAddress,
        virtual_time: u128,
        move_resolver: &'r mut S,
    ) -> AsyncSession<'r, 'l, S> {
        self.new_session_with_extensions(
            for_actor,
            virtual_time,
            move_resolver,
            NativeContextExtensions::default(),
        )
    }

    /// Creates a new session.
    pub fn new_session_with_extensions<'r, 'l, S: MoveResolver>(
        &'l self,
        for_actor: AccountAddress,
        virtual_time: u128,
        move_resolver: &'r mut S,
        ext: NativeContextExtensions<'r>,
    ) -> AsyncSession<'r, 'l, S> {
        let extensions = make_extensions(ext, for_actor, virtual_time, true);
        AsyncSession {
            vm: self,
            vm_session: self
                .move_vm
                .new_session_with_extensions(move_resolver, extensions),
        }
    }

    /// Get the underlying Move VM.
    pub fn get_move_vm(&mut self) -> &mut MoveVM {
        &mut self.move_vm
    }

    /// Resolve the message hash into module and handler function.
    pub fn resolve_message_hash(&self, message_hash: u64) -> Option<&(ModuleId, Identifier)> {
        self.message_table.get(&message_hash)
    }

    /// Get the actor metadata.
    pub fn actor_metadata(&self, module_id: &ModuleId) -> Option<&ActorMetadata> {
        self.actor_metadata.get(module_id)
    }

    /// Get all know actors.
    pub fn actors(&self) -> Vec<ModuleId> {
        self.actor_metadata.keys().cloned().collect()
    }
}

/// Represents an Async Move execution session.
pub struct AsyncSession<'r, 'l, S> {
    vm: &'l AsyncVM,
    vm_session: Session<'r, 'l, S>,
}

/// Represents a message being sent, consisting of target address, message hash, and arguments.
pub type Message = (AccountAddress, u64, Vec<Vec<u8>>);

/// A structure to represent success for the execution of an async session operation.
#[derive(Debug, Clone)]
pub struct AsyncSuccess {
    pub change_set: ChangeSet,
    pub events: Vec<Event>,
    pub messages: Vec<Message>,
    pub gas_used: u64,
}

/// A structure to represent failure for the execution of an async session operation.
#[derive(Debug, Clone)]
pub struct AsyncError {
    pub error: VMError,
    pub gas_used: u64,
}

/// Result type for operations of an AsyncSession.
pub type AsyncResult = Result<AsyncSuccess, AsyncError>;

impl<'r, 'l, S: MoveResolver> AsyncSession<'r, 'l, S> {
    /// Get the underlying Move VM session.
    pub fn get_move_session(&mut self) -> &mut Session<'r, 'l, S> {
        &mut self.vm_session
    }

    /// Creates a new actor, identified by the module_id, at the given account address.
    /// This calls the initializer function of the actor, and returns on success
    /// a changeset which needs to be committed to persist the new actors state.
    pub fn new_actor(
        mut self,
        module_id: &ModuleId,
        actor_addr: AccountAddress,
        gas_status: &mut GasStatus,
    ) -> AsyncResult {
        let actor = self
            .vm
            .actor_metadata
            .get(module_id)
            .ok_or_else(|| async_extension_error(format!("actor `{}` unknown", module_id)))?;
        let state_type_tag = TypeTag::Struct(actor.state_tag.clone());
        let state_type = self
            .vm_session
            .load_type(&state_type_tag)
            .map_err(vm_error_to_async)?;

        // Check whether the actor state already exists.
        let state = self
            .vm_session
            .get_data_store()
            .load_resource(actor_addr, &state_type)
            .map_err(partial_vm_error_to_async)?;
        if state.exists().map_err(partial_vm_error_to_async)? {
            return Err(async_extension_error(format!(
                "actor `{}` already exists at `{}`",
                module_id.short_str_lossless(),
                actor_addr.short_str_lossless()
            )));
        }

        // Execute the initializer.
        let gas_before = gas_status.remaining_gas().get();
        let result = self
            .vm_session
            .execute_function_bypass_visibility(
                &actor.module_id,
                &actor.initializer,
                vec![],
                Vec::<Vec<u8>>::new(),
                gas_status,
            )
            .and_then(|ret| Ok((ret, self.vm_session.finish_with_extensions()?)));
        let gas_used = gas_status.remaining_gas().get() - gas_before;

        // Process the result, moving the return value of the initializer function into the
        // changeset.
        match result {
            Ok((
                SerializedReturnValues {
                    mutable_reference_outputs: _,
                    mut return_values,
                },
                (mut change_set, events, mut native_extensions),
            )) => {
                if return_values.len() != 1 {
                    Err(async_extension_error(format!(
                        "inconsistent initializer `{}`",
                        actor.initializer
                    )))
                } else {
                    publish_actor_state(
                        &mut change_set,
                        actor_addr,
                        actor.state_tag.clone(),
                        return_values.remove(0).0,
                    )
                    .map_err(partial_vm_error_to_async)?;
                    let async_ext = native_extensions.remove::<AsyncExtension>();
                    Ok(AsyncSuccess {
                        change_set,
                        events,
                        messages: async_ext.sent,
                        gas_used,
                    })
                }
            }
            Err(error) => Err(AsyncError { error, gas_used }),
        }
    }

    /// Handles a message at `actor` with the given `message_hash`. This will call the
    /// according function as determined by the AsyncResolver, passing a reference to
    /// the actors state.
    pub fn handle_message(
        mut self,
        actor_addr: AccountAddress,
        message_hash: u64,
        mut args: Vec<Vec<u8>>,
        gas_status: &mut GasStatus,
    ) -> AsyncResult {
        // Resolve actor and function which handles the message.
        let (module_id, handler_id) =
            self.vm.message_table.get(&message_hash).ok_or_else(|| {
                async_extension_error(format!("unknown message hash `{}`", message_hash))
            })?;
        let actor = self.vm.actor_metadata.get(module_id).ok_or_else(|| {
            async_extension_error(format!(
                "actor `{}` unknown",
                module_id.short_str_lossless()
            ))
        })?;

        // Load the resource representing the actor state and add to arguments.
        let state_type_tag = TypeTag::Struct(actor.state_tag.clone());
        let state_type = self
            .vm_session
            .load_type(&state_type_tag)
            .map_err(vm_error_to_async)?;

        let actor_state_global = self
            .vm_session
            .get_data_store()
            .load_resource(actor_addr, &state_type)
            .map_err(partial_vm_error_to_async)?;
        let actor_state = actor_state_global
            .borrow_global()
            .and_then(|v| v.value_as::<Reference>())
            .and_then(|r| r.read_ref())
            .map_err(partial_vm_error_to_async)?;
        args.insert(
            0,
            self.to_bcs(actor_state, &state_type_tag)
                .map_err(partial_vm_error_to_async)?,
        );

        // Execute the handler.
        let gas_before = gas_status.remaining_gas().get();
        let result = self
            .vm_session
            .execute_function_bypass_visibility(module_id, handler_id, vec![], args, gas_status)
            .and_then(|ret| Ok((ret, self.vm_session.finish_with_extensions()?)));

        let gas_used = gas_status.remaining_gas().get() - gas_before;

        // Process the result, moving the mutated value of the handlers first parameter
        // into the changeset.
        match result {
            Ok((
                SerializedReturnValues {
                    mut mutable_reference_outputs,
                    return_values: _,
                },
                (mut change_set, events, mut native_extensions),
            )) => {
                if mutable_reference_outputs.len() > 1 {
                    Err(async_extension_error(format!(
                        "inconsistent handler `{}`",
                        handler_id
                    )))
                } else {
                    if !mutable_reference_outputs.is_empty() {
                        publish_actor_state(
                            &mut change_set,
                            actor_addr,
                            actor.state_tag.clone(),
                            mutable_reference_outputs.remove(0).1,
                        )
                        .map_err(partial_vm_error_to_async)?;
                    }
                    let async_ext = native_extensions.remove::<AsyncExtension>();
                    Ok(AsyncSuccess {
                        change_set,
                        events,
                        messages: async_ext.sent,
                        gas_used,
                    })
                }
            }
            Err(error) => Err(AsyncError { error, gas_used }),
        }
    }

    fn to_bcs(&self, value: Value, tag: &TypeTag) -> PartialVMResult<Vec<u8>> {
        let type_layout = self
            .vm_session
            .get_type_layout(tag)
            .map_err(|e| e.to_partial())?;
        value
            .simple_serialize(&type_layout)
            .ok_or_else(|| partial_extension_error("serialization failed"))
    }
}

fn make_extensions(
    mut exts: NativeContextExtensions,
    actor_addr: AccountAddress,
    virtual_time: u128,
    in_initializer: bool,
) -> NativeContextExtensions {
    exts.add(AsyncExtension {
        current_actor: actor_addr,
        sent: vec![],
        in_initializer,
        virtual_time,
    });
    exts
}

fn publish_actor_state(
    change_set: &mut ChangeSet,
    actor_addr: AccountAddress,
    state_tag: StructTag,
    state: Vec<u8>,
) -> PartialVMResult<()> {
    change_set
        .publish_resource(actor_addr, state_tag, state)
        .map_err(|err| partial_extension_error(format!("cannot publish actor state: {}", err)))
}

pub(crate) fn partial_extension_error(msg: impl ToString) -> PartialVMError {
    PartialVMError::new(StatusCode::VM_EXTENSION_ERROR).with_message(msg.to_string())
}

pub(crate) fn extension_error(msg: impl ToString) -> VMError {
    partial_extension_error(msg).finish(Location::Undefined)
}

fn async_extension_error(msg: impl ToString) -> AsyncError {
    AsyncError {
        error: extension_error(msg),
        gas_used: 0,
    }
}

fn vm_error_to_async(error: VMError) -> AsyncError {
    AsyncError { error, gas_used: 0 }
}

fn partial_vm_error_to_async(error: PartialVMError) -> AsyncError {
    vm_error_to_async(error.finish(Location::Undefined))
}

// ------------------------------------------------------------------------------------------
// Displaying

impl Display for AsyncError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.error)
    }
}

impl Error for AsyncError {}

impl Display for AsyncSuccess {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let AsyncSuccess {
            change_set,
            events,
            messages,
            gas_used,
        } = self;
        write!(f, "change_set: {:?}", change_set)?;
        write!(f, ", events: {:?}", events)?;
        write!(f, ", messages: {:?}", messages)?;
        write!(f, ", gas: {}", gas_used)
    }
}