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
use super::*;
use crate::deposit_indexed_event;
impl<T: Config> Pallet<T> {
pub(super) fn add_params_(
AddAccumulatorParams { params, .. }: AddAccumulatorParams<T>,
StoredAccumulatorOwnerCounters { params_counter, .. }: &mut StoredAccumulatorOwnerCounters,
owner: AccumulatorOwner,
) -> DispatchResult {
AccumulatorParams::<T>::insert(owner, params_counter.inc(), params);
Self::deposit_event(Event::ParamsAdded(owner, *params_counter));
Ok(())
}
pub(super) fn add_public_key_(
AddAccumulatorPublicKey { public_key, .. }: AddAccumulatorPublicKey<T>,
StoredAccumulatorOwnerCounters { key_counter, .. }: &mut StoredAccumulatorOwnerCounters,
owner: AccumulatorOwner,
) -> DispatchResult {
if let Some(AccumParametersStorageKey(acc_owner, params_id)) = public_key.params_ref {
ensure!(
AccumulatorParams::<T>::contains_key(acc_owner, params_id),
Error::<T>::ParamsDontExist
);
}
AccumulatorKeys::insert(owner, key_counter.inc(), public_key);
Self::deposit_event(Event::KeyAdded(owner, *key_counter));
Ok(())
}
pub(super) fn remove_params_(
RemoveAccumulatorParams {
params_ref: AccumParametersStorageKey(did, counter),
..
}: RemoveAccumulatorParams<T>,
accumulator_params: &mut Option<AccumulatorParameters<T>>,
_: AccumulatorOwner,
) -> DispatchResult {
accumulator_params
.take()
.ok_or(Error::<T>::ParamsDontExist)?;
Self::deposit_event(Event::ParamsRemoved(did, counter));
Ok(())
}
pub(super) fn remove_public_key_(
RemoveAccumulatorPublicKey {
key_ref: AccumPublicKeyStorageKey(did, counter),
..
}: RemoveAccumulatorPublicKey<T>,
accumulator_pk: &mut Option<AccumulatorPublicKey<T>>,
_: AccumulatorOwner,
) -> DispatchResult {
accumulator_pk
.take()
.ok_or(Error::<T>::PublicKeyDoesntExist)?;
Self::deposit_event(Event::KeyRemoved(did, counter));
Ok(())
}
pub(super) fn add_accumulator_(
AddAccumulator {
id, accumulator, ..
}: AddAccumulator<T>,
acc_opt: &mut Option<AccumulatorWithUpdateInfo<T>>,
owner: AccumulatorOwner,
) -> DispatchResult {
let AccumPublicKeyStorageKey(acc_owner, key_id) = accumulator.key_ref();
if !key_id.is_zero() {
ensure!(
AccumulatorKeys::<T>::contains_key(acc_owner, key_id),
Error::<T>::PublicKeyDoesntExist
);
}
ensure!(acc_owner == owner, Error::<T>::NotPublicKeyOwner);
let accumulated = accumulator.accumulated().to_vec().into();
let current_block = <frame_system::Pallet<T>>::block_number();
let acc = AccumulatorWithUpdateInfo::new(accumulator, current_block);
ensure!(
acc_opt.replace(acc).is_none(),
Error::<T>::AccumulatorAlreadyExists
);
deposit_indexed_event!(AccumulatorAdded(id, accumulated) over id);
Ok(())
}
pub(super) fn update_accumulator_(
UpdateAccumulator {
id,
new_accumulated,
..
}: UpdateAccumulator<T>,
accumulator: &mut AccumulatorWithUpdateInfo<T>,
owner: AccumulatorOwner,
) -> DispatchResult {
ensure!(
*accumulator.accumulator.owner_did() == owner,
Error::<T>::NotAccumulatorOwner
);
accumulator
.accumulator
.set_new_accumulated(new_accumulated.clone().0)
.map_err(|_| Error::<T>::AccumulatedTooBig)?;
accumulator.last_updated_at = <frame_system::Pallet<T>>::block_number();
deposit_indexed_event!(AccumulatorUpdated(id, new_accumulated) over id);
Ok(())
}
pub(super) fn remove_accumulator_(
RemoveAccumulator { id, .. }: RemoveAccumulator<T>,
accumulator: &mut Option<AccumulatorWithUpdateInfo<T>>,
signer: AccumulatorOwner,
) -> DispatchResult {
let accumulator = accumulator
.take()
.ok_or(Error::<T>::AccumulatorDoesntExist)?;
ensure!(
*accumulator.accumulator.owner_did() == signer,
Error::<T>::NotAccumulatorOwner
);
deposit_indexed_event!(AccumulatorRemoved(id));
Ok(())
}
pub fn public_key_with_params(
AccumPublicKeyStorageKey(key_did, key_id): &AccumPublicKeyStorageKey,
) -> Option<AccumPublicKeyWithParams<T>> {
let pk = AccumulatorKeys::get(key_did, key_id)?;
let params = match &pk.params_ref {
Some(AccumParametersStorageKey(params_did, params_id)) => {
AccumulatorParams::<T>::get(params_did, params_id)
}
_ => None,
};
Some((pk, params))
}
pub fn get_accumulator_with_public_key_and_params(
id: &AccumulatorId,
) -> Option<(Vec<u8>, Option<AccumPublicKeyWithParams<T>>)> {
let stored_acc = Accumulators::<T>::get(id)?;
let pk_p = Self::public_key_with_params(&stored_acc.accumulator.key_ref());
Some((stored_acc.accumulator.accumulated().to_vec(), pk_p))
}
}