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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use crate::*;
#[derive(Clone, Debug)]
pub struct ObjectSigns {
desc_signs: Option<Vec<Signature>>,
body_signs: Option<Vec<Signature>>,
}
pub struct ObjectSignsHelper;
impl ObjectSignsHelper {
pub fn set_sign(list: &mut Option<Vec<Signature>>, sign: Signature) {
if list.is_none() {
*list = Some(vec![]);
}
let list = list.as_mut().unwrap();
list.clear();
list.push(sign);
}
pub fn push_sign(list: &mut Option<Vec<Signature>>, sign: Signature) {
if list.is_none() {
*list = Some(vec![]);
}
let list = list.as_mut().unwrap();
if let Some(cur) = list.iter_mut().find(|v| v.compare_source(&sign)) {
if sign.sign_time() > cur.sign_time() {
info!("desc sign with same source will update! new={:?}, cur={:?}", sign, cur);
*cur = sign;
} else{
warn!("desc sign with same source already exists! sign={:?}", sign);
}
} else {
list.push(sign);
}
}
pub fn latest_sign_time(list: &Option<Vec<Signature>>) -> u64 {
let mut ret = 0;
if let Some(signs) = list.as_ref() {
for sign in signs {
if ret < sign.sign_time() {
ret = sign.sign_time();
}
}
}
ret
}
pub fn merge(dest: &mut Option<Vec<Signature>>, src: &Option<Vec<Signature>>) -> usize {
match src {
Some(src) => {
match dest {
Some(dest) => {
let mut ret = 0;
for src_item in src {
match dest.iter_mut().find(|s| s.compare_source(src_item)) {
Some(dest_item) => {
if src_item.sign_time() > dest_item.sign_time() {
*dest_item = src_item.clone();
}
}
None => {
dest.push(src_item.clone());
ret += 1;
}
}
}
ret
}
None => {
let mut ret = 0;
let mut dest_list: Vec<Signature> = Vec::new();
for src_item in src {
match dest_list.iter_mut().find(|s| s.compare_source(src_item)) {
Some(dest_item) => {
if src_item.sign_time() > dest_item.sign_time() {
*dest_item = src_item.clone();
}
}
None => {
dest_list.push(src_item.clone());
ret += 1;
}
}
}
if !dest_list.is_empty() {
*dest = Some(dest_list);
}
ret
}
}
}
None => 0,
}
}
}
#[derive(Clone)]
pub struct ObjectSignsBuilder {
desc_signs: Option<Vec<Signature>>,
body_signs: Option<Vec<Signature>>,
}
impl ObjectSignsBuilder {
pub fn new() -> Self {
Self {
desc_signs: None,
body_signs: None,
}
}
pub fn set_desc_sign(mut self, sign: Signature) -> Self {
ObjectSignsHelper::set_sign(&mut self.desc_signs, sign);
self
}
pub fn set_body_sign(mut self, sign: Signature) -> Self {
ObjectSignsHelper::set_sign(&mut self.body_signs, sign);
self
}
pub fn push_desc_sign(mut self, sign: Signature) -> Self {
ObjectSignsHelper::push_sign(&mut self.desc_signs, sign);
self
}
pub fn push_body_sign(mut self, sign: Signature) -> Self {
ObjectSignsHelper::push_sign(&mut self.body_signs, sign);
self
}
pub fn build(self) -> ObjectSigns {
ObjectSigns {
desc_signs: self.desc_signs,
body_signs: self.body_signs,
}
}
}
impl ObjectSigns {
pub fn new() -> ObjectSignsBuilder {
ObjectSignsBuilder::new()
}
pub fn is_desc_signs_empty(&self) -> bool {
if let Some(signs) = &self.desc_signs {
signs.len() == 0
} else {
true
}
}
pub fn is_body_signs_empty(&self) -> bool {
if let Some(signs) = &self.body_signs {
signs.len() == 0
} else {
true
}
}
pub fn is_empty(&self) -> bool {
self.is_desc_signs_empty() && self.is_body_signs_empty()
}
pub fn desc_signs(&self) -> Option<&Vec<Signature>> {
self.desc_signs.as_ref()
}
pub fn body_signs(&self) -> Option<&Vec<Signature>> {
self.body_signs.as_ref()
}
pub fn set_desc_sign(&mut self, sign: Signature) {
ObjectSignsHelper::set_sign(&mut self.desc_signs, sign)
}
pub fn set_body_sign(&mut self, sign: Signature) {
ObjectSignsHelper::set_sign(&mut self.body_signs, sign)
}
pub fn push_desc_sign(&mut self, sign: Signature) {
ObjectSignsHelper::push_sign(&mut self.desc_signs, sign)
}
pub fn push_body_sign(&mut self, sign: Signature) {
ObjectSignsHelper::push_sign(&mut self.body_signs, sign)
}
pub fn clear_desc_signs(&mut self) {
self.desc_signs.take();
}
pub fn clear_body_signs(&mut self) {
self.body_signs.take();
}
pub fn latest_body_sign_time(&self) -> u64 {
ObjectSignsHelper::latest_sign_time(&self.body_signs)
}
pub fn latest_desc_sign_time(&self) -> u64 {
ObjectSignsHelper::latest_sign_time(&self.desc_signs)
}
pub fn latest_sign_time(&self) -> u64 {
std::cmp::max(self.latest_desc_sign_time(), self.latest_body_sign_time())
}
pub fn merge(&mut self, other: &ObjectSigns) -> usize {
ObjectSignsHelper::merge(&mut self.desc_signs, &other.desc_signs)
+ ObjectSignsHelper::merge(&mut self.body_signs, &other.body_signs)
}
pub fn merge_ex(&mut self, other: &ObjectSigns, desc: bool, body: bool) -> usize {
let mut ret = 0;
if desc {
ret += ObjectSignsHelper::merge(&mut self.desc_signs, &other.desc_signs);
}
if body {
ret += ObjectSignsHelper::merge(&mut self.body_signs, &other.body_signs);
}
ret
}
}
impl Default for ObjectSigns {
fn default() -> Self {
Self {
desc_signs: None,
body_signs: None,
}
}
}
impl RawEncodeWithContext<NamedObjectContext> for ObjectSigns {
fn raw_measure_with_context(
&self,
ctx: &mut NamedObjectContext,
purpose: &Option<RawEncodePurpose>,
) -> BuckyResult<usize> {
let mut size = 0;
if self.desc_signs.is_some() {
ctx.with_desc_signs();
assert!(ctx.has_desc_signs());
size = size
+ self
.desc_signs
.as_ref()
.unwrap()
.raw_measure(purpose)
.map_err(|e| {
log::error!(
"ObjectSigns::raw_measure_with_context/desc_signs error:{}",
e
);
e
})?;
}
if self.body_signs.is_some() {
ctx.with_body_signs();
assert!(ctx.has_body_signs());
size = size
+ self
.body_signs
.as_ref()
.unwrap()
.raw_measure(purpose)
.map_err(|e| {
log::error!(
"ObjectSigns::raw_measure_with_context/body_signs error:{}",
e
);
e
})?;
}
Ok(size)
}
fn raw_encode_with_context<'a>(
&self,
buf: &'a mut [u8],
ctx: &mut NamedObjectContext,
purpose: &Option<RawEncodePurpose>,
) -> BuckyResult<&'a mut [u8]> {
let buf = if self.desc_signs.is_some() {
assert!(ctx.has_desc_signs());
self.desc_signs
.as_ref()
.unwrap()
.raw_encode(buf, purpose)
.map_err(|e| {
log::error!(
"ObjectSigns::raw_encode_with_context/desc_signs error:{}",
e
);
e
})?
} else {
buf
};
let buf = if self.body_signs.is_some() {
assert!(ctx.has_body_signs());
self.body_signs
.as_ref()
.unwrap()
.raw_encode(buf, purpose)
.map_err(|e| {
log::error!(
"ObjectSigns::raw_encode_with_context/body_signs error:{}",
e
);
e
})?
} else {
buf
};
Ok(buf)
}
}
impl<'de> RawDecodeWithContext<'de, &NamedObjectContext> for ObjectSigns {
fn raw_decode_with_context(
buf: &'de [u8],
ctx: &NamedObjectContext,
) -> BuckyResult<(Self, &'de [u8])> {
let (desc_signs, buf) = if ctx.has_desc_signs() {
let (desc_signs, buf) = Vec::<Signature>::raw_decode(buf).map_err(|e| {
log::error!(
"ObjectSigns::raw_decode_with_context/desc_signs error:{}",
e
);
e
})?;
(Some(desc_signs), buf)
} else {
(None, buf)
};
let (body_signs, buf) = if ctx.has_body_signs() {
let (body_signs, buf) = Vec::<Signature>::raw_decode(buf).map_err(|e| {
log::error!(
"ObjectSigns::raw_decode_with_context/body_signs error:{}",
e
);
e
})?;
(Some(body_signs), buf)
} else {
(None, buf)
};
Ok((
Self {
desc_signs,
body_signs,
},
buf,
))
}
}