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
//! The logic for applying the updates contained in a party.
use crate::account;
use crate::party::{self, SetOrKeep};
use crate::primitives::*;
use crate::sequence_event;

/// Compute the new value, given the original value and a [`SetOrKeep`] update.
/// If the update is `Keep`, return the original value.
/// If the value is `Set(new_value)`, return `new_value`.
pub fn set_or_keep<T>(original: T, update: SetOrKeep<T>) -> T {
    match update {
        SetOrKeep::Keep => original,
        SetOrKeep::Set(new_value) => new_value,
    }
}

/// Compute the update for the app state, by applying [`set_or_keep`] to each pair of values from
/// the original and the update:
/// ```rust
/// [
///     set_or_keep(original[0], update[0]),
///     set_or_keep(original[1], update[1]),
///     set_or_keep(original[2], update[2]),
///     set_or_keep(original[3], update[3]),
///     set_or_keep(original[4], update[4]),
///     set_or_keep(original[5], update[5]),
///     set_or_keep(original[6], update[6]),
///     set_or_keep(original[7], update[7]),
/// ]
/// ```
pub fn compute_app_state_update(original: [Fp; 8], update: [SetOrKeep<Fp>; 8]) -> [Fp; 8] {
    [
        set_or_keep(original[0], update[0]),
        set_or_keep(original[1], update[1]),
        set_or_keep(original[2], update[2]),
        set_or_keep(original[3], update[3]),
        set_or_keep(original[4], update[4]),
        set_or_keep(original[5], update[5]),
        set_or_keep(original[6], update[6]),
        set_or_keep(original[7], update[7]),
    ]
}

/// Update the `app_state` field of [`account::SnappAccount`] with the new value from the
/// [`party::PartyUpdate`].
///
/// Using [`compute_app_state_update`]:
/// ```rust
/// snapp_account.app_state = compute_app_state_update(snapp_account.app_state, update.app_state)
/// ```
pub fn apply_snapp_app_state_update<'a>(
    mut snapp_account: account::SnappAccount<'a>,
    update: &party::PartyUpdate,
) -> account::SnappAccount<'a> {
    snapp_account.app_state = compute_app_state_update(snapp_account.app_state, update.app_state);
    snapp_account
}

/// Returns true if any of the updates are [`SetOrKeep::Set`], false otherwise.
pub fn any_are_set<'a>(app_state_updates: &[SetOrKeep<Fp>; 8]) -> bool {
    for app_state_update in app_state_updates {
        match app_state_update {
            SetOrKeep::Set(_) => {
                return true;
            }
            SetOrKeep::Keep => (),
        }
    }
    return false;
}

/// Returns true if all of the updates are [`SetOrKeep::Set`], false otherwise.
pub fn all_are_set<'a>(app_state_updates: &[SetOrKeep<Fp>; 8]) -> bool {
    for app_state_update in app_state_updates {
        match app_state_update {
            SetOrKeep::Keep => {
                return false;
            }
            SetOrKeep::Set(_) => (),
        }
    }
    return true;
}

/// Update the `proved_state` field of [`account::SnappAccount`] with the new computed value.
///
/// * If the authorization is [`Signature`](`party::PartyAuthorization::Signature`) or
/// [`NoneGiven`](`party::PartyAuthorization::NoneGiven`), and if `any_are_set(update.app_state)`
///   is true, then set `snapp_account.proved_state` to `false`.
/// * If the authorization is [`Proof`](`party::PartyAuthorization::Proof`) and
///   `all_are_set(update.app_state)` is true, then set `snapp_account.proved_state` to `true`.
/// * Otherwise, leave `proved_state` unchanged.
pub fn apply_snapp_proved_state_update<'a>(
    mut snapp_account: account::SnappAccount<'a>,
    update: &party::PartyUpdate,
    authorization: &party::PartyAuthorization,
) -> account::SnappAccount<'a> {
    match authorization {
        party::PartyAuthorization::Proof(_) => {
            if all_are_set(&update.app_state) {
                snapp_account.proved_state = true
            }
        }
        party::PartyAuthorization::Signature(_) | party::PartyAuthorization::NoneGiven => {
            if any_are_set(&update.app_state) {
                snapp_account.proved_state = false
            }
        }
    }
    snapp_account
}

/// Update the `delegate` field of [`account::Account`] with the new value from the
/// [`party::PartyUpdate`].
///
/// Using [`set_or_keep`]:
/// ```rust
/// account.delegate = set_or_keep(account.delegate, update.delegate)
/// ```
pub fn apply_delegate_update<'a>(
    mut account: account::Account<'a>,
    update: &party::PartyUpdate,
) -> account::Account<'a> {
    account.delegate = set_or_keep(account.delegate, update.delegate);
    account
}

/// Update the `verification_key` field of [`account::SnappAccount`] with the new value from the
/// [`party::PartyUpdate`].
///
/// Using [`set_or_keep`]:
/// ```rust
/// snapp_account.verification_key =
///     set_or_keep(snapp_account.verification_key, update.verification_key)
/// ```
pub fn apply_verification_key_update<'a>(
    mut snapp_account: account::SnappAccount<'a>,
    update: &party::PartyUpdate,
) -> account::SnappAccount<'a> {
    snapp_account.verification_key = set_or_keep(
        snapp_account.verification_key.clone(),
        update.verification_key.clone(),
    );
    snapp_account
}

/// Update the `permissions` field of [`account::Account`] with the new value from the
/// [`party::PartyUpdate`].
///
/// Using [`set_or_keep`]:
/// ```rust
/// account.permissions = set_or_keep(account.permissions, update.permissions)
/// ```
pub fn apply_permissions_update<'a>(
    mut account: account::Account<'a>,
    update: &party::PartyUpdate,
) -> account::Account<'a> {
    account.permissions = set_or_keep(account.permissions, update.permissions);
    account
}

/// Convert a [`SetOrKeep<account::TimingInfo>`] in an [`account::AccountTiming::Timed`].
/// If the value is `Keep`, return `Keep`.
/// Otherwise, if the value is `Set(timing_info)`, return
/// `Set(account::AccountTiming::Timed(timing_info)`.
pub fn wrap_set_or_keep_timing_info(
    timing_info: SetOrKeep<account::TimingInfo>,
) -> SetOrKeep<account::AccountTiming> {
    match timing_info {
        SetOrKeep::Keep => SetOrKeep::Keep,
        SetOrKeep::Set(timing_info) => SetOrKeep::Set(account::AccountTiming::Timed(timing_info)),
    }
}

/// Update the `timing` field of [`account::Account`] with the new value from the
/// [`party::PartyUpdate`].
///
/// Using [`set_or_keep`] and [`wrap_set_or_keep_timing_info`]:
/// ```rust
/// account.timing = set_or_keep(account.timing, wrap_set_or_keep_timing_info(update.timing))
/// ```
pub fn apply_timing_update<'a>(
    mut account: account::Account<'a>,
    update: &party::PartyUpdate,
) -> account::Account<'a> {
    account.timing = set_or_keep(account.timing, wrap_set_or_keep_timing_info(update.timing));
    account
}

/// Update the `balance` field of [`account::Account`] by adding the `balance_change` from
/// [`party::PartyUpdate`] using [`party::wrapping_add_signed`].
/// ```rust
/// account.balance = party::wrapping_add_signed(account.balance, balance_change);
/// ```
pub fn apply_balance_change<'a>(
    mut account: account::Account<'a>,
    balance_change: party::SignedAmount,
) -> account::Account<'a> {
    account.balance = party::wrapping_add_signed(account.balance, balance_change);
    account
}

/// Update the `voting_for` field of [`account::Account`] with the new value from the
/// [`party::PartyUpdate`].
///
/// Using [`set_or_keep`]:
/// ```rust
/// account.voting_for = set_or_keep(account.voting_for, update.voting_for)
/// ```
pub fn apply_voting_for_update<'a>(
    mut account: account::Account<'a>,
    update: &party::PartyUpdate,
) -> account::Account<'a> {
    account.voting_for = set_or_keep(account.voting_for, update.voting_for);
    account
}

/// Update the `sequence_event` field of [`account::SnappAccount`] with the new sequence events at
/// the given global slot (since genesis).
///
/// This calls [`sequence_event::SequenceState::push_sequence_events`]:
/// ```rust
/// snapp.sequence_state = sequence_event::SequenceState::push_sequence_events(
///     snapp.sequence_state,
///     sequence_events,
///     global_slot,
/// )
/// ```
pub fn apply_sequence_event_update<'a>(
    mut snapp: account::SnappAccount<'a>,
    sequence_events: &sequence_event::SequenceEvents,
    global_slot: u32,
) -> account::SnappAccount<'a> {
    snapp.sequence_state = sequence_event::SequenceState::push_sequence_events(
        snapp.sequence_state,
        sequence_events,
        global_slot,
    );
    snapp
}

/// Update the `uri` field of [`account::SnappAccount`] with the new value from the
/// [`party::PartyUpdate`].
///
/// Using [`set_or_keep`]:
/// ```rust
/// snapp_account.uri = set_or_keep(snapp_account.uri, update.uri)
/// ```
pub fn apply_uri_update<'a>(
    mut snapp_account: account::SnappAccount<'a>,
    update: &party::PartyUpdate<'a>,
) -> account::SnappAccount<'a> {
    snapp_account.uri = set_or_keep(snapp_account.uri.clone(), update.snapp_uri);
    snapp_account
}

/// Update the `token_symbol` field of [`account::SnappAccount`] with the new value from the
/// [`party::PartyUpdate`].
///
/// Using [`set_or_keep`]:
/// ```rust
/// snapp_account.token_symbol = set_or_keep(snapp_account.token_symbol, update.token_symbol)
/// ```
pub fn apply_token_symbol_update<'a>(
    mut snapp_account: account::SnappAccount<'a>,
    update: &party::PartyUpdate<'a>,
) -> account::SnappAccount<'a> {
    snapp_account.token_symbol =
        set_or_keep(snapp_account.token_symbol.clone(), update.token_symbol);
    snapp_account
}

/// Update the [`account::SnappAccount`] with a [`party::PartyUpdate`] and a
/// [`sequence_event::SequenceEvents`].
///
/// This applies, in order:
/// * [`apply_snapp_app_state_update`]
/// * [`apply_snapp_proved_state_update`]
/// * [`apply_verification_key_update`]
/// * [`apply_sequence_event_update`]
/// * [`apply_uri_update`]
/// * [`apply_token_symbol_update`]
pub fn apply_snapp_update<'a>(
    mut snapp: account::SnappAccount<'a>,
    update: &party::PartyUpdate<'a>,
    authorization: &party::PartyAuthorization,
    sequence_events: &sequence_event::SequenceEvents,
    global_slot: u32,
) -> account::SnappAccount<'a> {
    snapp = apply_snapp_app_state_update(snapp, update);
    snapp = apply_snapp_proved_state_update(snapp, update, authorization);
    snapp = apply_verification_key_update(snapp, update);
    snapp = apply_sequence_event_update(snapp, sequence_events, global_slot);
    snapp = apply_uri_update(snapp, update);
    snapp
}

/// Update the `snapp` field of [`account::Account`] with a [`party::PartyUpdate`] and a
/// [`sequence_event::SequenceEvents`].
///
/// In order, this must
/// * get the `snapp` field of the account, or [`account::SnappAccount::empty`] if it is `None`
/// * apply [`apply_snapp_update`] to this value
/// * set the `snapp` field of the account to the result, or to `None` if
///   [`account::SnappAccount::is_empty`] is true.
pub fn apply_snapp_account_update<'a>(
    mut account: account::Account<'a>,
    update: &party::PartyUpdate<'a>,
    authorization: &party::PartyAuthorization,
    sequence_events: &sequence_event::SequenceEvents,
    global_slot: u32,
) -> account::Account<'a> {
    account.snapp = {
        let snapp = match account.snapp {
            Some(snapp) => snapp,
            None => account::SnappAccount::empty(),
        };
        let snapp = apply_snapp_update(snapp, update, authorization, sequence_events, global_slot);
        if account::SnappAccount::is_empty(&snapp) {
            None
        } else {
            Some(snapp)
        }
    };
    account
}

/// Increment the `nonce` field of [`account::Account`] if the `increment_nonce` argument is true;
/// otherwise, leave the account unchanged.
pub fn apply_nonce_update<'a>(
    mut account: account::Account<'a>,
    increment_nonce: bool,
) -> account::Account<'a> {
    if increment_nonce {
        account.nonce = account.nonce + 1
    }
    account
}

/// Update the [`account::Account`] with a [`party::PartyUpdate`] and a
/// [`sequence_event::SequenceEvents`].
///
/// This applies, in order:
/// * [`apply_timing_update`]
/// * [`apply_balance_change`]
/// * [`apply_snapp_account_update`]
/// * [`apply_delegate_update`]
/// * [`apply_nonce_update`]
/// * [`apply_voting_for_update`]
/// * [`apply_permissions_update`]
pub fn apply_update<'a>(
    mut account: account::Account<'a>,
    update: &party::PartyUpdate<'a>,
    authorization: &party::PartyAuthorization,
    sequence_events: &sequence_event::SequenceEvents,
    balance_change: party::SignedAmount,
    global_slot: u32,
    increment_nonce: bool,
) -> account::Account<'a> {
    account = apply_timing_update(account, update);
    account = apply_balance_change(account, balance_change);
    // TODO: Check timing, but not here..
    account =
        apply_snapp_account_update(account, update, authorization, sequence_events, global_slot);
    account = apply_delegate_update(account, update);
    account = apply_nonce_update(account, increment_nonce);
    account = apply_voting_for_update(account, update);
    // Finally, update permissions
    account = apply_permissions_update(account, update);
    account
}