sonicapi/adaptors/
alu.rs

1// SONIC: Standard library 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
24#![allow(unused_variables)]
25// TODO: Implement AluVM-based APIs.
26
27use std::cmp::Ordering;
28
29use aluvm::LibSite;
30use amplify::confinement::ConfinedBlob;
31use strict_types::{SemId, StrictDumb, StrictVal, TypeSystem};
32use ultrasonic::{StateData, StateValue};
33
34use crate::api::TOTAL_BYTES;
35use crate::{
36    ApiVm, StateAdaptor, StateArithm, StateAtom, StateCalc, StateCalcError, StateName, StateReader, VmType,
37    LIB_NAME_SONIC,
38};
39
40impl ApiVm for aluvm::Vm {
41    type Arithm = AluVMArithm;
42    type Reader = AluReader;
43    type Adaptor = AluAdaptor;
44
45    fn vm_type(&self) -> VmType { VmType::AluVM }
46}
47
48#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
49#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
50#[strict_type(lib = LIB_NAME_SONIC)]
51#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
52pub struct AluReader(LibSite);
53
54impl StateReader for AluReader {
55    fn read<'s, I: IntoIterator<Item = &'s StateAtom>>(&self, state: impl Fn(&StateName) -> I) -> StrictVal { todo!() }
56}
57
58#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
59#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
60#[strict_type(lib = LIB_NAME_SONIC)]
61#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
62pub struct AluAdaptor {
63    pub converter: LibSite,
64    pub builder: LibSite,
65}
66
67impl StateAdaptor for AluAdaptor {
68    fn convert_immutable(
69        &self,
70        sem_id: SemId,
71        raw_sem_id: SemId,
72        data: &StateData,
73        sys: &TypeSystem,
74    ) -> Option<StateAtom> {
75        todo!()
76    }
77
78    fn convert_destructible(&self, sem_id: SemId, value: StateValue, sys: &TypeSystem) -> Option<StrictVal> { todo!() }
79
80    fn build_immutable(&self, value: ConfinedBlob<0, TOTAL_BYTES>) -> StateValue { todo!() }
81
82    fn build_destructible(&self, value: ConfinedBlob<0, TOTAL_BYTES>) -> StateValue { todo!() }
83}
84
85#[derive(Clone, Debug)]
86#[derive(StrictType, StrictEncode, StrictDecode)]
87#[strict_type(lib = LIB_NAME_SONIC)]
88#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
89pub struct AluVMArithm {
90    #[strict_type(skip)]
91    #[cfg_attr(feature = "serde", serde(skip))]
92    pub vm: Option<aluvm::Vm>,
93    pub accumulate: LibSite,
94    pub lessen: LibSite,
95    pub diff: LibSite,
96}
97
98impl StrictDumb for AluVMArithm {
99    fn strict_dumb() -> Self {
100        Self {
101            vm: None,
102            accumulate: LibSite::strict_dumb(),
103            lessen: LibSite::strict_dumb(),
104            diff: LibSite::strict_dumb(),
105        }
106    }
107}
108
109impl StateArithm for AluVMArithm {
110    fn calculator(&self) -> Box<dyn StateCalc> { todo!() }
111}
112
113impl StateCalc for () {
114    fn compare(&self, a: &StrictVal, b: &StrictVal) -> Option<Ordering> { todo!() }
115
116    fn accumulate(&mut self, state: &StrictVal) -> Result<(), StateCalcError> { Err(StateCalcError::UncountableState) }
117
118    fn lessen(&mut self, state: &StrictVal) -> Result<(), StateCalcError> { Err(StateCalcError::UncountableState) }
119
120    fn diff(&self) -> Result<Vec<StrictVal>, StateCalcError> { Err(StateCalcError::UncountableState) }
121
122    fn is_satisfied(&self, state: &StrictVal) -> bool { todo!() }
123}