linera_base/
vm.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The virtual machines being supported.
5
6use std::str::FromStr;
7
8use async_graphql::scalar;
9use derive_more::Display;
10use linera_witty::{WitLoad, WitStore, WitType};
11use serde::{Deserialize, Serialize};
12use thiserror::Error;
13
14#[derive(
15    Clone,
16    Copy,
17    Default,
18    Display,
19    Hash,
20    PartialEq,
21    Eq,
22    PartialOrd,
23    Ord,
24    Serialize,
25    Deserialize,
26    WitType,
27    WitStore,
28    WitLoad,
29    Debug,
30)]
31#[cfg_attr(with_testing, derive(test_strategy::Arbitrary))]
32/// The virtual machine runtime
33pub enum VmRuntime {
34    /// The Wasm virtual machine
35    #[default]
36    Wasm,
37    /// The Evm virtual machine
38    Evm,
39}
40
41impl FromStr for VmRuntime {
42    type Err = InvalidVmRuntime;
43
44    fn from_str(string: &str) -> Result<Self, Self::Err> {
45        match string {
46            "wasm" => Ok(VmRuntime::Wasm),
47            "evm" => Ok(VmRuntime::Evm),
48            unknown => Err(InvalidVmRuntime(unknown.to_owned())),
49        }
50    }
51}
52
53scalar!(VmRuntime);
54
55/// Error caused by invalid VM runtimes
56#[derive(Clone, Debug, Error)]
57#[error("{0:?} is not a valid virtual machine runtime")]
58pub struct InvalidVmRuntime(String);
59
60/// The possible types of queries for an EVM contract
61#[derive(Clone, Debug, Deserialize, Serialize)]
62pub enum EvmQuery {
63    /// A read-only query.
64    Query(Vec<u8>),
65    /// A request to schedule an operation that can mutate the application state.
66    Mutation(Vec<u8>),
67}