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
use crate::execution_step::RcSecurityTetraplet;
use crate::execution_step::ValueAggregate;
use crate::JValue;
use crate::UncatchableError;
use air_interpreter_cid::CidCalculationError;
use air_interpreter_cid::CID;
use air_interpreter_data::CanonCidAggregate;
use air_interpreter_data::CidInfo;
use air_interpreter_data::CidTracker;
use air_interpreter_data::ServiceResultAggregate;
use air_interpreter_data::TracePos;
use polyplets::SecurityTetraplet;
use std::rc::Rc;
#[derive(Debug, Default, Clone)]
pub struct ExecutionCidState {
pub value_tracker: CidTracker<JValue>,
pub tetraplet_tracker: CidTracker<SecurityTetraplet>,
pub canon_tracker: CidTracker<CanonCidAggregate>,
pub service_result_agg_tracker: CidTracker<ServiceResultAggregate>,
}
impl ExecutionCidState {
pub fn new() -> Self {
Self::default()
}
pub fn insert_value(
&mut self,
value: Rc<JValue>,
tetraplet: RcSecurityTetraplet,
argument_hash: Rc<str>,
) -> Result<Rc<CID<ServiceResultAggregate>>, CidCalculationError> {
let value_cid = self.value_tracker.record_value(value)?;
let tetraplet_cid = self.tetraplet_tracker.record_value(tetraplet)?;
let service_result_agg = ServiceResultAggregate {
value_cid,
argument_hash,
tetraplet_cid,
};
self.service_result_agg_tracker.record_value(service_result_agg)
}
pub(crate) fn from_cid_info(prev_cid_info: CidInfo, current_cid_info: CidInfo) -> Self {
let value_tracker = CidTracker::from_cid_stores(prev_cid_info.value_store, current_cid_info.value_store);
let tetraplet_tracker =
CidTracker::from_cid_stores(prev_cid_info.tetraplet_store, current_cid_info.tetraplet_store);
let canon_tracker = CidTracker::from_cid_stores(prev_cid_info.canon_store, current_cid_info.canon_store);
let service_result_agg_tracker = CidTracker::from_cid_stores(
prev_cid_info.service_result_store,
current_cid_info.service_result_store,
);
Self {
value_tracker,
tetraplet_tracker,
canon_tracker,
service_result_agg_tracker,
}
}
pub(crate) fn get_value_by_cid(&self, cid: &CID<JValue>) -> Result<Rc<JValue>, UncatchableError> {
self.value_tracker
.get(cid)
.ok_or_else(|| UncatchableError::ValueForCidNotFound("value", cid.clone().into()))
}
pub(crate) fn get_tetraplet_by_cid(
&self,
cid: &CID<SecurityTetraplet>,
) -> Result<RcSecurityTetraplet, UncatchableError> {
self.tetraplet_tracker
.get(cid)
.ok_or_else(|| UncatchableError::ValueForCidNotFound("tetraplet", cid.clone().into()))
}
pub(crate) fn get_canon_value_by_cid(
&self,
cid: &CID<CanonCidAggregate>,
) -> Result<ValueAggregate, UncatchableError> {
let canon_aggregate = self
.canon_tracker
.get(cid)
.ok_or_else(|| UncatchableError::ValueForCidNotFound("canon aggregate", cid.clone().into()))?;
let result = self.get_value_by_cid(&canon_aggregate.value)?;
let tetraplet = self.get_tetraplet_by_cid(&canon_aggregate.tetraplet)?;
let fake_trace_pos = TracePos::default();
Ok(ValueAggregate {
result,
tetraplet,
trace_pos: fake_trace_pos,
})
}
pub(crate) fn get_service_result_agg_by_cid(
&self,
cid: &CID<ServiceResultAggregate>,
) -> Result<Rc<ServiceResultAggregate>, UncatchableError> {
self.service_result_agg_tracker
.get(cid)
.ok_or_else(|| UncatchableError::ValueForCidNotFound("service result aggregate", cid.clone().into()))
}
pub(crate) fn resolve_service_value(
&self,
service_result_agg_cid: &CID<ServiceResultAggregate>,
) -> Result<Rc<JValue>, UncatchableError> {
let service_result_aggregate = self.get_service_result_agg_by_cid(service_result_agg_cid)?;
self.get_value_by_cid(&service_result_aggregate.value_cid)
}
}
impl From<ExecutionCidState> for CidInfo {
fn from(value: ExecutionCidState) -> Self {
Self {
value_store: value.value_tracker.into(),
tetraplet_store: value.tetraplet_tracker.into(),
canon_store: value.canon_tracker.into(),
service_result_store: value.service_result_agg_tracker.into(),
}
}
}