Skip to main content

midnight_onchain_runtime/
transcript.rs

1// This file is part of midnight-ledger.
2// Copyright (C) 2025 Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14use crate::context::Effects;
15use crate::ops::Op;
16use crate::result_mode::ResultModeVerify;
17use base_crypto::cost_model::RunningCost;
18use derive_where::derive_where;
19use serialize::tag_enforcement_test;
20use serialize::{Deserializable, Serializable, Tagged};
21use storage::Storable;
22use storage::arena::{ArenaKey, Sp};
23use storage::db::DB;
24use storage::storable::Loader;
25// #[cfg(feature = "proptest")] TODO WG
26// use proptest_derive::Arbitrary;
27use serde::{Deserialize, Serialize};
28
29#[derive(Storable, Serialize, Deserialize, Serializable, Clone, PartialEq, Eq, Debug)]
30#[storable(base)]
31#[tag = "contract-transcript-version"]
32pub struct TranscriptVersion {
33    pub major: u8,
34    pub minor: u8,
35}
36tag_enforcement_test!(TranscriptVersion);
37
38// #[cfg_attr(feature = "proptest", derive(Arbitrary))] TODO WG
39#[derive(Storable, Serialize, Deserialize)]
40#[serde(bound(serialize = "", deserialize = ""))]
41#[derive_where(Clone, PartialEq, Eq, Debug)]
42#[storable(db = D)]
43#[tag = "contract-transcript[v4]"]
44pub struct Transcript<D: DB> {
45    pub gas: RunningCost,
46    pub effects: Effects<D>,
47    pub program: storage::storage::Array<Op<ResultModeVerify, D>, D>,
48    pub version: Option<Sp<TranscriptVersion, D>>,
49}
50tag_enforcement_test!(Transcript<storage::db::InMemoryDB>);
51
52// TODO WG
53// #[cfg(feature = "proptest")]
54// impl<D: DB> rand::distributions::Distribution<Transcript<D>> for rand::distributions::Standard {
55//     fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Transcript<D> {
56//         Transcript {
57//             gas: rng.gen(),
58//             effects: rng.gen(),
59//             program: storage::storage::Vec::from_std_vec(vec![]),
60//             version: rng.gen(),
61//         }
62//     }
63// }
64
65impl<D: DB> Transcript<D> {
66    pub const VERSION: TranscriptVersion = TranscriptVersion { major: 2, minor: 3 };
67}