sonicapi/adaptors/
alu.rs

1// SONIC: Toolchain for formally-verifiable distributed contracts
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
6// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7//
8// Copyright (C) 2019-2024 LNP/BP Standards Association, Switzerland.
9// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
10//                         Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
11// Copyright (C) 2019-2025 Dr Maxim Orlovsky.
12// All rights under the above copyrights are reserved.
13//
14// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
15// in compliance with the License. You may obtain a copy of the License at
16//
17//        http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software distributed under the License
20// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
21// or implied. See the License for the specific language governing permissions and limitations under
22// the License.
23
24use std::cmp::Ordering;
25
26use aluvm::LibSite;
27use amplify::confinement::ConfinedBlob;
28use strict_types::{SemId, StrictDumb, StrictVal, TypeSystem};
29use ultrasonic::{StateData, StateValue};
30
31use crate::api::TOTAL_BYTES;
32use crate::{
33    ApiVm, StateAdaptor, StateArithm, StateAtom, StateCalc, StateCalcError, StateName, StateReader, VmType,
34    LIB_NAME_SONIC,
35};
36
37impl ApiVm for aluvm::Vm {
38    type Arithm = AluVMArithm;
39    type Reader = AluReader;
40    type Adaptor = AluAdaptor;
41
42    fn vm_type(&self) -> VmType { VmType::AluVM }
43}
44
45#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
46#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
47#[strict_type(lib = LIB_NAME_SONIC)]
48#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
49pub struct AluReader(LibSite);
50
51impl StateReader for AluReader {
52    fn read<'s, I: IntoIterator<Item = &'s StateAtom>>(&self, state: impl Fn(&StateName) -> I) -> StrictVal { todo!() }
53}
54
55#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
56#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
57#[strict_type(lib = LIB_NAME_SONIC)]
58#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
59pub struct AluAdaptor {
60    pub converter: LibSite,
61    pub builder: LibSite,
62}
63
64impl StateAdaptor for AluAdaptor {
65    fn convert_immutable(
66        &self,
67        sem_id: SemId,
68        raw_sem_id: SemId,
69        data: &StateData,
70        sys: &TypeSystem,
71    ) -> Option<StateAtom> {
72        todo!()
73    }
74
75    fn convert_destructible(&self, sem_id: SemId, value: StateValue, sys: &TypeSystem) -> Option<StrictVal> { todo!() }
76
77    fn build_immutable(&self, value: ConfinedBlob<0, TOTAL_BYTES>) -> StateValue { todo!() }
78
79    fn build_destructible(&self, value: ConfinedBlob<0, TOTAL_BYTES>) -> StateValue { todo!() }
80}
81
82#[derive(Clone, Debug)]
83#[derive(StrictType, StrictEncode, StrictDecode)]
84#[strict_type(lib = LIB_NAME_SONIC)]
85#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
86pub struct AluVMArithm {
87    #[strict_type(skip)]
88    #[cfg_attr(feature = "serde", serde(skip))]
89    pub vm: Option<aluvm::Vm>,
90    pub accumulate: LibSite,
91    pub lessen: LibSite,
92    pub diff: LibSite,
93}
94
95impl StrictDumb for AluVMArithm {
96    fn strict_dumb() -> Self {
97        Self {
98            vm: None,
99            accumulate: LibSite::strict_dumb(),
100            lessen: LibSite::strict_dumb(),
101            diff: LibSite::strict_dumb(),
102        }
103    }
104}
105
106impl StateArithm for AluVMArithm {
107    fn calculator(&self) -> Box<dyn StateCalc> { todo!() }
108}
109
110impl StateCalc for () {
111    fn compare(&self, a: &StrictVal, b: &StrictVal) -> Option<Ordering> { todo!() }
112
113    fn accumulate(&mut self, state: &StrictVal) -> Result<(), StateCalcError> { Err(StateCalcError::UncountableState) }
114
115    fn lessen(&mut self, state: &StrictVal) -> Result<(), StateCalcError> { Err(StateCalcError::UncountableState) }
116
117    fn diff(&self) -> Result<Vec<StrictVal>, StateCalcError> { Err(StateCalcError::UncountableState) }
118
119    fn is_satisfied(&self, state: &StrictVal) -> bool { todo!() }
120}