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
use super::GlobalStreamGens;
use super::RestrictedStreamGens;
use crate::cid_store::CidStore;
use crate::CanonCidAggregate;
use crate::ExecutionTrace;
use crate::JValue;
use crate::ServiceResultAggregate;
use air_utils::measure;
use polyplets::SecurityTetraplet;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterpreterData {
#[serde(flatten)]
pub versions: Versions,
pub trace: ExecutionTrace,
#[serde(rename = "streams")] pub global_streams: GlobalStreamGens,
#[serde(default)]
#[serde(rename = "r_streams")]
pub restricted_streams: RestrictedStreamGens,
#[serde(default)]
#[serde(rename = "lcid")]
pub last_call_request_id: u32,
pub cid_info: CidInfo,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Versions {
#[serde(rename = "version")] pub data_version: semver::Version,
pub interpreter_version: semver::Version,
}
impl InterpreterData {
pub fn new(interpreter_version: semver::Version) -> Self {
let versions = Versions::new(interpreter_version);
Self {
versions,
trace: ExecutionTrace::default(),
global_streams: GlobalStreamGens::new(),
last_call_request_id: 0,
restricted_streams: RestrictedStreamGens::new(),
cid_info: <_>::default(),
}
}
#[allow(clippy::too_many_arguments)]
pub fn from_execution_result(
trace: ExecutionTrace,
streams: GlobalStreamGens,
restricted_streams: RestrictedStreamGens,
cid_info: CidInfo,
last_call_request_id: u32,
interpreter_version: semver::Version,
) -> Self {
let versions = Versions::new(interpreter_version);
Self {
versions,
trace,
global_streams: streams,
last_call_request_id,
restricted_streams,
cid_info,
}
}
pub fn try_from_slice(slice: &[u8]) -> Result<Self, serde_json::Error> {
measure!(
serde_json::from_slice(slice),
tracing::Level::INFO,
"serde_json::from_slice"
)
}
pub fn try_get_versions(slice: &[u8]) -> Result<Versions, serde_json::Error> {
serde_json::from_slice(slice)
}
}
impl Versions {
pub fn new(interpreter_version: semver::Version) -> Self {
Self {
data_version: crate::data_version().clone(),
interpreter_version,
}
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CidInfo {
pub value_store: CidStore<JValue>,
pub tetraplet_store: CidStore<SecurityTetraplet>,
pub canon_store: CidStore<CanonCidAggregate>,
pub service_result_store: CidStore<ServiceResultAggregate>,
}
#[cfg(test)]
mod tests {
use super::*;
use serde::Deserialize;
use serde::Serialize;
#[test]
fn compatible_with_0_6_0_version() {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterpreterData0_6_0 {
pub trace: ExecutionTrace,
#[serde(rename = "streams")] pub global_streams: GlobalStreamGens,
pub version: semver::Version,
#[serde(default)]
#[serde(rename = "lcid")]
pub last_call_request_id: u32,
#[serde(default)]
#[serde(rename = "r_streams")]
pub restricted_streams: RestrictedStreamGens,
pub interpreter_version: semver::Version,
pub cid_info: CidInfo,
}
let data_0_6_0 = InterpreterData0_6_0 {
trace: ExecutionTrace::default(),
global_streams: GlobalStreamGens::default(),
version: semver::Version::new(0, 2, 0),
last_call_request_id: 0,
restricted_streams: RestrictedStreamGens::default(),
interpreter_version: semver::Version::new(0, 1, 1),
cid_info: CidInfo::default(),
};
let data_0_6_0_se = serde_json::to_vec(&data_0_6_0).unwrap();
let data_0_6_1 = serde_json::from_slice::<InterpreterData>(&data_0_6_0_se);
assert!(data_0_6_1.is_ok());
let data_0_6_1 = InterpreterData::new(semver::Version::new(1, 1, 1));
let data_0_6_1_se = serde_json::to_vec(&data_0_6_1).unwrap();
let data_0_6_0 = serde_json::from_slice::<InterpreterData0_6_0>(&data_0_6_1_se);
assert!(data_0_6_0.is_ok());
}
}