hypersonic 0.12.0

Formally-verifiable distributed software
Documentation
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// SONIC: Standard library for formally-verifiable distributed contracts
//
// SPDX-License-Identifier: Apache-2.0
//
// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
//
// Copyright (C) 2019-2024 LNP/BP Standards Association, Switzerland.
// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
//                         Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
// Copyright (C) 2019-2025 Dr Maxim Orlovsky.
// All rights under the above copyrights are reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

extern crate alloc;

#[macro_use]
extern crate amplify;
#[macro_use]
extern crate strict_types;

use std::convert::Infallible;
use std::fs;
use std::path::Path;

use aluvm::{CoreConfig, LibSite};
use amplify::num::u256;
use commit_verify::{Digest, Sha256, StrictHash};
use hypersonic::{Api, GlobalApi, OwnedApi};
use sonic_persist_fs::LedgerDir;
use sonicapi::{
    Aggregator, Issuer, RawBuilder, RawConvertor, Semantics, SigBlob, StateArithm, StateBuilder, StateConvertor,
    SubAggregator,
};
use strict_types::{SemId, StrictVal};
use ultrasonic::aluvm::FIELD_ORDER_SECP;
use ultrasonic::{AuthToken, CellAddr, Codex, Consensus, Identity};

fn codex() -> Codex {
    let lib = libs::success();
    let lib_id = lib.lib_id();
    Codex {
        name: tiny_s!("SimpleDAO"),
        developer: Identity::default(),
        version: default!(),
        timestamp: 1732529307,
        features: none!(),
        field_order: FIELD_ORDER_SECP,
        input_config: CoreConfig::default(),
        verification_config: CoreConfig::default(),
        verifiers: tiny_bmap! {
            0 => LibSite::new(lib_id, 0),
            1 => LibSite::new(lib_id, 0),
            2 => LibSite::new(lib_id, 0),
        },
    }
}

fn api() -> Api {
    let types = stl::DaoTypes::new();

    let codex = codex();

    Api {
        codex_id: codex.codex_id(),
        conforms: none!(),
        default_call: None,
        global: tiny_bmap! {
            vname!("_parties") => GlobalApi {
                published: true,
                sem_id: types.get("DAO.PartyId"),
                convertor: StateConvertor::TypedEncoder(u256::ZERO),
                builder: StateBuilder::TypedEncoder(u256::ZERO),
                raw_convertor: RawConvertor::StrictDecode(types.get("DAO.Party")),
                raw_builder: RawBuilder::StrictEncode(types.get("DAO.Party")),
            },
            vname!("_votings") => GlobalApi {
                published: true,
                sem_id: types.get("DAO.VoteId"),
                convertor: StateConvertor::TypedEncoder(u256::ONE),
                builder: StateBuilder::TypedEncoder(u256::ONE),
                raw_convertor: RawConvertor::StrictDecode(types.get("DAO.Voting")),
                raw_builder: RawBuilder::StrictEncode(types.get("DAO.Voting")),
            },
            vname!("_votes") => GlobalApi {
                published: true,
                sem_id: types.get("DAO.CastVote"),
                convertor: StateConvertor::TypedEncoder(u256::from(2u8)),
                builder: StateBuilder::TypedEncoder(u256::from(2u8)),
                raw_convertor: RawConvertor::StrictDecode(SemId::unit()),
                raw_builder: RawBuilder::StrictEncode(SemId::unit()),
            },
        },
        owned: tiny_bmap! {
            vname!("signers") => OwnedApi {
                sem_id: types.get("DAO.PartyId"),
                arithmetics: StateArithm::NonFungible,
                convertor: StateConvertor::TypedEncoder(u256::ZERO),
                builder: StateBuilder::TypedEncoder(u256::ZERO),
                witness_sem_id: SemId::unit(),
                witness_builder: StateBuilder::TypedEncoder(u256::ZERO),
            }
        },
        aggregators: tiny_bmap! {
            vname!("parties") => Aggregator::Take(SubAggregator::MapV2U(vname!("_parties"))),
            vname!("votings") => Aggregator::Take(SubAggregator::MapV2U(vname!("_votings"))),
            vname!("votes") => Aggregator::Take(SubAggregator::SetV(vname!("_votes"))),
            vname!("votingCount") => Aggregator::Take(SubAggregator::Count(vname!("_votings"))),
        },
        verifiers: tiny_bmap! {
            vname!("setup") => 0,
            vname!("proposal") => 1,
            vname!("castVote") => 2,
        },
        errors: Default::default(),
    }
}

#[test]
fn main() {
    let types = stl::DaoTypes::new();
    let codex = codex();
    let api = api();

    // Creating DAO with three participants
    let semantics = Semantics {
        version: 0,
        default: api,
        custom: none!(),
        codex_libs: small_bset![libs::success()],
        api_libs: none!(),
        types: types.type_system(),
    };

    let sig = SigBlob::from_slice_checked(*b"me");
    fn sig_validator(_: StrictHash, _: &Identity, s: &SigBlob) -> Result<(), ()> {
        if s.as_slice() == b"me" {
            Ok(())
        } else {
            Err(())
        }
    }
    let issuer = Issuer::with(codex, semantics, sig, sig_validator).unwrap();
    let filename = "examples/dao/data/SimpleDAO.issuer";
    fs::remove_file(filename).ok();
    issuer
        .save(filename)
        .expect("unable to save an issuer to a file");

    let issuer2 = Issuer::load(filename, sig_validator).unwrap();
    assert_eq!(issuer.issuer_id(), issuer2.issuer_id());
    assert_eq!(issuer.codex_id(), issuer2.codex_id());
    assert_eq!(issuer.codex_name(), issuer2.codex_name());
    assert_eq!(issuer.codex(), issuer2.codex());
    assert_eq!(issuer.types(), issuer2.types());
    assert_eq!(issuer.semantics(), issuer2.semantics());
    assert_eq!(issuer.default_api(), issuer2.default_api());
    assert_eq!(issuer.custom_apis().collect::<Vec<_>>(), issuer2.custom_apis().collect::<Vec<_>>());
    assert_eq!(issuer.codex_libs().collect::<Vec<_>>(), issuer2.codex_libs().collect::<Vec<_>>());

    let seed = &[0xCA; 30][..];
    let mut auth = Sha256::digest(seed);
    let mut next_auth = || -> AuthToken {
        auth = Sha256::digest(&*auth);
        let mut buf = [0u8; 30];
        buf.copy_from_slice(&auth[..30]);
        AuthToken::from(buf)
    };

    let alice_auth = next_auth();
    let bob_auth = next_auth();
    let carol_auth = next_auth();

    let articles = issuer
        .start_issue_testnet("setup", Consensus::None)
        // Alice
        .append("_parties", svnum!(0u64), Some(ston!(name "alice", identity "Alice Wonderland")))
        .assign("signers", alice_auth, svnum!(0u64), None)
        // Bob
        .append("_parties", svnum!(1u64), Some(ston!(name "bob", identity "Bob Capricorn")))
        .assign("signers", bob_auth, svnum!(1u64), None)
        // Carol
        .append("_parties", svnum!(2u64), Some(ston!(name "carol", identity "Carol Caterpillar")))
        .assign("signers", carol_auth, svnum!(2u64), None)

        .finish("WonderlandDAO", 1732529307);
    let opid = articles.genesis_opid();

    let contract_path = Path::new("tests/data/WonderlandDAO.contract");
    if contract_path.exists() {
        fs::remove_dir_all(contract_path).expect("Unable to remove a contract file");
    }
    fs::create_dir_all(contract_path).expect("Unable to create a contract folder");
    let mut ledger = LedgerDir::new(articles.clone(), contract_path.to_path_buf()).expect("Can't issue contract");

    // Proposing vote
    let votings = ledger
        .start_deed("proposal")
        .append(
            "_votings",
            svnum!(100u64),
            Some(ston!(title "Is Alice on duty today?", text "Vote 'pro' if Alice should be on duty today")),
        )
        .commit()
        .unwrap();

    let alice_auth2 = next_auth();
    let bob_auth2 = next_auth();
    let carol_auth2 = next_auth();

    // Alice vote against her being on duty today
    ledger
        .start_deed("castVote")
        .using(CellAddr::new(opid, 0))
        .reading(CellAddr::new(votings, 0))
        .append("_votes", ston!(voteId 100u64, vote svenum!(0u8), partyId 0u64), None)
        .assign("signers", alice_auth2, svnum!(0u64), None)
        .commit()
        .unwrap();

    // Bob and Carol vote for Alice being on duty today
    ledger
        .start_deed("castVote")
        .using(CellAddr::new(opid, 1))
        .reading(CellAddr::new(votings, 0))
        .append("_votes", ston!(voteId 100u64, vote svenum!(1u8), partyId 1u64), None)
        .assign("signers", bob_auth2, svnum!(1u64), None)
        .commit()
        .unwrap();
    ledger
        .start_deed("castVote")
        .using(CellAddr::new(opid, 2))
        .reading(CellAddr::new(votings, 0))
        .append("_votes", ston!(voteId 100u64, vote svenum!(1u8), partyId 2u64), None)
        .assign("signers", carol_auth2, svnum!(2u64), None)
        .commit()
        .unwrap();

    let StrictVal::Map(votings) = ledger.state().read("votings") else {
        panic!("invalid data")
    };
    let (_, first_voting) = votings.first().unwrap();
    println!("voting: {first_voting}");
    println!("Votes:");
    let StrictVal::Set(votes) = ledger.state().read("votes") else {
        panic!("invalid data")
    };
    for vote in votes {
        println!("- {vote}");
    }

    // Now anybody accessing this file can figure out who is on duty today, by the decision of DAO.
    let deeds_path = Path::new("tests/data/voting.deeds");
    if deeds_path.exists() {
        fs::remove_file(deeds_path).expect("unable to remove contract file");
    }

    ledger
        .export_to_file([alice_auth2, bob_auth2, carol_auth2], deeds_path)
        .expect("unable to save deeds to a file");

    let contract_path = Path::new("tests/data/WonderlandDAO-2.contract");
    if contract_path.exists() {
        fs::remove_dir_all(contract_path).expect("Unable to remove a contract file");
    }
    fs::create_dir_all(contract_path).expect("Unable to create a contract folder");
    let mut ledger2 = LedgerDir::new(articles, contract_path.to_path_buf()).expect("Can't issue contract");
    ledger2
        .accept_from_file(deeds_path, |_, _, _| Result::<_, Infallible>::Ok(()))
        .unwrap();

    let deeds_path = "tests/data/votings-all.deeds";
    fs::remove_file(deeds_path).ok();
    ledger2.export_all_to_file(deeds_path).unwrap();
}

mod libs {
    use aluvm::{aluasm, Lib};

    pub fn success() -> Lib {
        let code = aluasm! {
            stop;
        };
        Lib::assemble(&code).unwrap()
    }

    #[allow(dead_code)]
    pub fn cast_vote() -> Lib {
        // 1. Verify that there is just one referenced global state for the party and one for the voting
        // 2. Verify that the referenced global state has a valid voteId matching the one provided operation
        // 3. Verify that the referenced global state has a valid partyId matching the one provided
        //    operation
        // 4. Verify there is just one input
        // 5. Verify that the provided witness argument is a preimage of the input
        todo!()
    }
}

mod stl {
    use amplify::confinement::{SmallString, TinyString};
    use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};
    use strict_types::stl::std_stl;
    use strict_types::{LibBuilder, SemId, SymbolicSys, SystemBuilder, TypeLib, TypeSystem};

    use super::*;

    pub const LIB_NAME_DAO: &str = "DAO";

    #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
    #[display(inner)]
    #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
    #[strict_type(lib = LIB_NAME_DAO)]
    pub struct PartyId(u64);

    #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
    #[display(r#"{name} "{identity}""#)]
    #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
    #[strict_type(lib = LIB_NAME_DAO)]
    pub struct Party {
        pub name: TinyString,
        pub identity: TinyString,
    }

    #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
    #[display(inner)]
    #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
    #[strict_type(lib = LIB_NAME_DAO)]
    pub struct VoteId(u64);

    #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
    #[display(lowercase)]
    #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
    #[strict_type(lib = LIB_NAME_DAO, tags = repr, try_from_u8, into_u8)]
    #[repr(u8)]
    pub enum Vote {
        #[strict_type(dumb)]
        Contra = 0,
        Pro = 1,
    }

    #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
    #[display("Title: {title}\n\n{text}")]
    #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
    #[strict_type(lib = LIB_NAME_DAO)]
    pub struct Voting {
        pub title: TinyString,
        pub text: SmallString,
    }

    #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From, Display)]
    #[display("Participant #{party_id} voted {vote} in voting #{vote_id}")]
    #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
    #[strict_type(lib = LIB_NAME_DAO)]
    pub struct CastVote {
        pub vote_id: VoteId,
        pub vote: Vote,
        pub party_id: PartyId,
    }

    #[derive(Debug)]
    pub struct DaoTypes(SymbolicSys);

    impl Default for DaoTypes {
        fn default() -> Self { DaoTypes::new() }
    }

    pub fn stl() -> TypeLib {
        LibBuilder::with(libname!(LIB_NAME_DAO), [std_stl().to_dependency_types()])
            .transpile::<Party>()
            .transpile::<Voting>()
            .transpile::<CastVote>()
            .compile()
            .expect("invalid DAO type library")
    }

    impl DaoTypes {
        pub fn new() -> Self {
            Self(
                SystemBuilder::new()
                    .import(std_stl())
                    .unwrap()
                    .import(stl())
                    .unwrap()
                    .finalize()
                    .unwrap(),
            )
        }

        pub fn type_system(&self) -> TypeSystem {
            let types = stl().types;
            let types = types.iter().map(|(tn, ty)| ty.sem_id_named(tn));
            self.0.as_types().extract(types).unwrap()
        }

        pub fn get(&self, name: &'static str) -> SemId {
            *self
                .0
                .resolve(name)
                .unwrap_or_else(|| panic!("type '{name}' is absent in standard RGBContract type library"))
        }
    }
}