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