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
use std::fmt;
use std::io::{Read, Write};
use attestation::Attestation;
use error::Error;
use hex::Hexed;
use op::Op;
use ser;
const RECURSION_LIMIT: usize = 256;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum StepData {
Fork,
Op(Op),
Attestation(Attestation)
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Step {
pub data: StepData,
pub output: Vec<u8>,
pub next: Vec<Step>
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Timestamp {
pub start_digest: Vec<u8>,
pub first_step: Step
}
impl Timestamp {
fn deserialize_step_recurse<R: Read>(deser: &mut ser::Deserializer<R>, input_digest: Vec<u8>, tag: Option<u8>, recursion_limit: usize) -> Result<Step, Error> {
if recursion_limit == 0 {
return Err(Error::StackOverflow);
}
let tag = match tag {
Some(tag) => tag,
None => deser.read_byte()?
};
match tag {
0x00 => {
let attest = Attestation::deserialize(deser)?;
trace!("[{:3}] Attestation: {}", recursion_limit, attest);
Ok(Step {
data: StepData::Attestation(attest),
output: input_digest,
next: vec![]
})
}
0xff => {
let mut forks = vec![];
let mut next_tag = 0xff;
while next_tag == 0xff {
trace!("[{:3}] Forking..", recursion_limit);
forks.push(Timestamp::deserialize_step_recurse(deser, input_digest.clone(), None, recursion_limit - 1)?);
next_tag = deser.read_byte()?;
}
forks.push(Timestamp::deserialize_step_recurse(deser, input_digest.clone(), Some(next_tag), recursion_limit - 1)?);
Ok(Step {
data: StepData::Fork,
output: input_digest,
next: forks
})
}
tag => {
let op = Op::deserialize_with_tag(deser, tag)?;
let output_digest = op.execute(&input_digest);
trace!("[{:3}] Tag {} maps {} to {}.", recursion_limit, op, Hexed(&input_digest), Hexed(&output_digest));
let next = vec![Timestamp::deserialize_step_recurse(deser, output_digest.clone(), None, recursion_limit - 1)?];
Ok(Step {
data: StepData::Op(op),
output: output_digest,
next: next
})
}
}
}
pub fn deserialize<R: Read>(deser: &mut ser::Deserializer<R>, digest: Vec<u8>) -> Result<Timestamp, Error> {
let first_step = Timestamp::deserialize_step_recurse(deser, digest.clone(), None, RECURSION_LIMIT)?;
Ok(Timestamp {
start_digest: digest,
first_step: first_step
})
}
fn serialize_step_recurse<W: Write>(ser: &mut ser::Serializer<W>, step: &Step) -> Result<(), Error> {
match step.data {
StepData::Fork => {
for i in 0..step.next.len() - 1 {
ser.write_byte(0xff)?;
Timestamp::serialize_step_recurse(ser, &step.next[i])?;
}
Timestamp::serialize_step_recurse(ser, &step.next[step.next.len() - 1])
}
StepData::Op(ref op) => {
op.serialize(ser)?;
Timestamp::serialize_step_recurse(ser, &step.next[0])
}
StepData::Attestation(ref attest) => {
ser.write_byte(0x00)?;
attest.serialize(ser)
}
}
}
pub fn serialize<W: Write>(&self, ser: &mut ser::Serializer<W>) -> Result<(), Error> {
Timestamp::serialize_step_recurse(ser, &self.first_step)
}
}
fn fmt_recurse(step: &Step, f: &mut fmt::Formatter, depth: usize, first_line: bool) -> fmt::Result {
fn indent(f: &mut fmt::Formatter, depth: usize, first_line: bool) -> fmt::Result {
if depth == 0 {
return Ok(());
}
for _ in 0..depth-1 {
f.write_str(" ")?;
}
if first_line {
f.write_str("--->")?;
} else {
f.write_str(" ")?;
}
Ok(())
}
match step.data {
StepData::Fork => {
indent(f, depth, first_line)?;
writeln!(f, "(fork {} ways)", step.next.len())?;
for fork in &step.next {
fmt_recurse(fork, f, depth + 1, true)?;
}
Ok(())
}
StepData::Op(ref op) => {
indent(f, depth, first_line)?;
writeln!(f, "execute {}", op)?;
indent(f, depth, false)?;
writeln!(f, " result {}", Hexed(&step.output))?;
fmt_recurse(&step.next[0], f, depth, false)
}
StepData::Attestation(ref attest) => {
indent(f, depth, first_line)?;
writeln!(f, "result attested by {}", attest)
}
}
}
impl fmt::Display for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Starting digest: {}", Hexed(&self.start_digest))?;
fmt_recurse(&self.first_step, f, 0, false)
}
}