-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathtransaction_z1.rs
More file actions
221 lines (188 loc) · 5.98 KB
/
transaction_z1.rs
File metadata and controls
221 lines (188 loc) · 5.98 KB
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
use std::{borrow::Cow, sync::Arc};
use feldera_storage::{FileCommitter, StoragePath};
use size_of::SizeOf;
use crate::{
circuit::{
checkpointer::Checkpoint,
operator_traits::{Operator, UnaryOperator},
GlobalNodeId, OwnershipPreference,
},
operator::require_persistent_id,
storage::file::to_bytes,
Circuit, Error, NumEntries, Runtime, Scope, Stream,
};
impl<C, D> Stream<C, D>
where
C: Circuit,
{
/// Applies [`TransactionZ1`] operator to `self`.
#[track_caller]
pub fn transaction_delay_with_initial_value(&self, initial: D) -> Stream<C, D>
where
D: Checkpoint + SizeOf + NumEntries + Clone + 'static,
{
let delay_pid = self
.get_persistent_id()
.map(|pid| format!("{pid}.transaction_delay"));
self.circuit()
.add_unary_operator(TransactionZ1::new(initial.clone()), self)
.set_persistent_id(delay_pid.as_deref())
}
}
pub struct TransactionZ1<T> {
zero: T,
// For error reporting,
global_id: GlobalNodeId,
empty_output: bool,
flush: bool,
old_value: T,
new_value: T,
}
#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
pub struct CommittedTransactionZ1 {
old_value: Vec<u8>,
new_value: Vec<u8>,
}
impl<T> TryFrom<&TransactionZ1<T>> for CommittedTransactionZ1
where
T: Checkpoint,
{
type Error = Error;
fn try_from(z1: &TransactionZ1<T>) -> Result<CommittedTransactionZ1, Error> {
Ok(CommittedTransactionZ1 {
old_value: z1.old_value.checkpoint()?,
new_value: z1.new_value.checkpoint()?,
})
}
}
impl<T> TransactionZ1<T>
where
T: Checkpoint + Clone,
{
pub fn new(zero: T) -> Self {
Self {
empty_output: false,
flush: false,
global_id: GlobalNodeId::root(),
zero: zero.clone(),
old_value: zero.clone(),
new_value: zero.clone(),
}
}
/// Return the absolute path of the file for this operator.
///
/// # Arguments
/// - `cid`: The checkpoint id.
/// - `persistent_id`: The persistent id that identifies the spine within
/// the circuit for a given checkpoint.
fn checkpoint_file<P: AsRef<str>>(base: &StoragePath, persistent_id: P) -> StoragePath {
base.child(format!("transaction-z1-{}.dat", persistent_id.as_ref()))
}
}
impl<T> Operator for TransactionZ1<T>
where
T: Checkpoint + SizeOf + NumEntries + Clone + 'static,
{
fn name(&self) -> Cow<'static, str> {
Cow::from("Transaction Z^-1")
}
fn clock_start(&mut self, _scope: Scope) {}
fn clock_end(&mut self, _scope: Scope) {
self.empty_output = false;
self.new_value = self.zero.clone();
self.old_value = self.zero.clone();
}
fn init(&mut self, global_id: &GlobalNodeId) {
self.global_id = global_id.clone();
}
fn fixedpoint(&self, scope: Scope) -> bool {
if scope == 0 {
self.new_value.num_entries_shallow() == 0
&& self.old_value.num_entries_shallow() == 0
&& self.empty_output
} else {
true
}
}
fn checkpoint(
&mut self,
base: &StoragePath,
persistent_id: Option<&str>,
files: &mut Vec<Arc<dyn FileCommitter>>,
) -> Result<(), Error> {
let persistent_id = require_persistent_id(persistent_id, &self.global_id)?;
let committed: CommittedTransactionZ1 = (self as &Self).try_into()?;
let as_bytes =
to_bytes(&committed).expect("Serializing CommittedTransactionZ1 should work.");
files.push(
Runtime::storage_backend()
.unwrap()
.write(&Self::checkpoint_file(base, persistent_id), as_bytes)?,
);
Ok(())
}
fn restore(&mut self, base: &StoragePath, persistent_id: Option<&str>) -> Result<(), Error> {
let persistent_id = require_persistent_id(persistent_id, &self.global_id)?;
let z1_path = Self::checkpoint_file(base, persistent_id);
let content = Runtime::storage_backend().unwrap().read(&z1_path)?;
let committed = unsafe { rkyv::archived_root::<CommittedTransactionZ1>(&content) };
let mut old_value = self.zero.clone();
let mut new_value = self.zero.clone();
old_value.restore(committed.old_value.as_slice())?;
new_value.restore(committed.new_value.as_slice())?;
self.empty_output = false;
self.old_value = old_value;
self.new_value = new_value;
Ok(())
}
fn clear_state(&mut self) -> Result<(), Error> {
self.empty_output = false;
self.old_value = self.zero.clone();
self.new_value = self.zero.clone();
Ok(())
}
fn flush(&mut self) {
self.flush = true;
}
}
impl<T> UnaryOperator<T, T> for TransactionZ1<T>
where
T: Checkpoint + SizeOf + NumEntries + Clone + 'static,
{
async fn eval(&mut self, i: &T) -> T {
if self.flush {
self.flush = false;
self.old_value = self.new_value.clone();
self.new_value = i.clone();
}
self.old_value.clone()
}
fn input_preference(&self) -> OwnershipPreference {
OwnershipPreference::PREFER_OWNED
}
}
#[cfg(test)]
mod test {
use crate::{
circuit::operator_traits::{Operator, UnaryOperator},
operator::TransactionZ1,
};
#[tokio::test]
async fn transaction_z1_test() {
let mut z1 = TransactionZ1::new(0);
z1.clock_start(0);
assert_eq!(z1.eval(&1).await, 0);
assert_eq!(z1.eval(&2).await, 0);
assert_eq!(z1.eval(&3).await, 0);
z1.flush();
assert_eq!(z1.eval(&4).await, 0);
assert_eq!(z1.eval(&5).await, 0);
assert_eq!(z1.eval(&6).await, 0);
z1.flush();
assert_eq!(z1.eval(&7).await, 4);
assert_eq!(z1.eval(&8).await, 4);
assert_eq!(z1.eval(&9).await, 4);
z1.clock_end(0);
assert_eq!(z1.eval(&10).await, 0);
}
}