zephyr_vm/
vm_context.rs

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
//! Defines the context for the host environment.
//! VM context is used when dealing with reading and
//! writing the guest memory.

use anyhow::Result;
use std::rc::Weak;

use crate::{
    db::{database::ZephyrDatabase, ledger::LedgerStateRead},
    error::HostError,
    vm::Vm,
    ZephyrMock, ZephyrStandard,
};

/// VM Context.
/// The object is currently simply a wrapper for an
/// optional reference to the Virtual Machine.
#[derive(Clone)]
pub struct VmContext<DB: ZephyrDatabase, L: LedgerStateRead> {
    /// Optional Zephyr Virtual Machine.
    pub vm: Option<Weak<Vm<DB, L>>>,
}

impl<DB: ZephyrDatabase + ZephyrStandard, L: LedgerStateRead + ZephyrStandard> ZephyrStandard
    for VmContext<DB, L>
{
    fn zephyr_standard() -> Result<Self>
    where
        Self: Sized,
    {
        Ok(Self { vm: None })
    }
}

impl<DB: ZephyrDatabase + ZephyrMock, L: LedgerStateRead + ZephyrMock> ZephyrMock
    for VmContext<DB, L>
{
    fn mocked() -> Result<Self>
    where
        Self: Sized,
    {
        Ok(Self { vm: None })
    }
}

impl<DB: ZephyrDatabase, L: LedgerStateRead> VmContext<DB, L> {
    /// Writes the provided VM as the context's Virtual Machine.
    /// Errors when a VM is already present in the context.
    pub fn load_vm(&mut self, vm: Weak<Vm<DB, L>>) -> Result<()> {
        if self.vm.is_some() {
            return Err(HostError::ContextAlreadyExists.into());
        }

        self.vm = Some(vm);

        Ok(())
    }
}