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
use crate::{
    core::{
        common::crypto::{hash::Hash, signature::PublicKey as CorePublicKey},
        consensus::beacon::EpochTime,
    },
    crypto::signature::PublicKey,
    state::CurrentState,
    storage::{self, Store},
};

use super::{app_id::AppId, types, Error, MODULE_NAME};

/// Map of application identifiers to their configs.
const APPS: &[u8] = &[0x01];
/// Map of (application identifier, H(RAK)) tuples to their registrations.
const REGISTRATIONS: &[u8] = &[0x02];
/// Map of H(pk)s to KeyEndorsementInfos. This is used when just the public key is needed to avoid
/// fetching entire registrations from storage.
const ENDORSERS: &[u8] = &[0x03];
/// A queue of registration expirations.
const EXPIRATION_QUEUE: &[u8] = &[0x04];

/// Information about an endorsed key.
#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
#[cbor(as_array)]
pub struct KeyEndorsementInfo {
    /// Identifier of node that endorsed the enclave.
    pub node_id: CorePublicKey,
    /// RAK of the enclave that endorsed the key. This is only set for endorsements of extra keys.
    pub rak: Option<CorePublicKey>,
}

impl KeyEndorsementInfo {
    /// Create a new key endorsement information for RAK endorsed by given node directly.
    pub fn for_rak(node_id: CorePublicKey) -> Self {
        Self {
            node_id,
            ..Default::default()
        }
    }

    /// Create a new key endorsement information for extra key endorsed by RAK.
    pub fn for_extra_key(node_id: CorePublicKey, rak: CorePublicKey) -> Self {
        Self {
            node_id,
            rak: Some(rak),
        }
    }
}

/// Retrieves an application configuration.
pub fn get_app(app_id: AppId) -> Option<types::AppConfig> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let apps = storage::TypedStore::new(storage::PrefixStore::new(store, &APPS));
        apps.get(app_id)
    })
}

/// Updates an application configuration.
pub fn set_app(cfg: types::AppConfig) {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let mut apps = storage::TypedStore::new(storage::PrefixStore::new(store, &APPS));
        apps.insert(cfg.id, cfg);
    })
}

/// Removes an application configuration.
pub fn remove_app(app_id: AppId) {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let mut apps = storage::TypedStore::new(storage::PrefixStore::new(store, &APPS));
        apps.remove(app_id);
    })
}

/// Updates registration of the given ROFL enclave.
pub fn update_registration(registration: types::Registration) -> Result<(), Error> {
    let hrak = hash_rak(&registration.rak);

    // Update expiration queue.
    if let Some(existing) = get_registration_hrak(registration.app, hrak) {
        // Disallow modification of extra keys.
        if existing.extra_keys != registration.extra_keys {
            return Err(Error::ExtraKeyUpdateNotAllowed);
        }

        remove_expiration_queue(existing.expiration, registration.app, hrak);
    }
    insert_expiration_queue(registration.expiration, registration.app, hrak);

    // Update registration.
    CurrentState::with_store(|mut root_store| {
        let store = storage::PrefixStore::new(&mut root_store, &MODULE_NAME);
        let mut endorsers = storage::TypedStore::new(storage::PrefixStore::new(store, &ENDORSERS));
        endorsers.insert(hrak, KeyEndorsementInfo::for_rak(registration.node_id));

        for pk in &registration.extra_keys {
            endorsers.insert(
                hash_pk(pk),
                KeyEndorsementInfo::for_extra_key(registration.node_id, registration.rak),
            );
        }

        let app_id = registration.app;
        let store = storage::PrefixStore::new(&mut root_store, &MODULE_NAME);
        let registrations = storage::PrefixStore::new(store, &REGISTRATIONS);
        let mut app = storage::TypedStore::new(storage::PrefixStore::new(registrations, app_id));
        app.insert(hrak, registration);
    });

    Ok(())
}

fn remove_registration_hrak(app_id: AppId, hrak: Hash) {
    let registration = match get_registration_hrak(app_id, hrak) {
        Some(registration) => registration,
        None => return,
    };

    // Remove from expiration queue if present.
    remove_expiration_queue(registration.expiration, registration.app, hrak);

    // Remove registration.
    CurrentState::with_store(|mut root_store| {
        let store = storage::PrefixStore::new(&mut root_store, &MODULE_NAME);
        let mut endorsers = storage::TypedStore::new(storage::PrefixStore::new(store, &ENDORSERS));
        endorsers.remove(hrak);

        for pk in &registration.extra_keys {
            endorsers.remove(hash_pk(pk));
        }

        let store = storage::PrefixStore::new(&mut root_store, &MODULE_NAME);
        let registrations = storage::PrefixStore::new(store, &REGISTRATIONS);
        let mut app = storage::TypedStore::new(storage::PrefixStore::new(registrations, app_id));
        app.remove(hrak);
    });
}

/// Removes an existing registration of the given ROFL enclave.
pub fn remove_registration(app_id: AppId, rak: &CorePublicKey) {
    remove_registration_hrak(app_id, hash_rak(rak))
}

fn get_registration_hrak(app_id: AppId, hrak: Hash) -> Option<types::Registration> {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let registrations = storage::PrefixStore::new(store, &REGISTRATIONS);
        let app = storage::TypedStore::new(storage::PrefixStore::new(registrations, app_id));
        app.get(hrak)
    })
}

/// Retrieves registration of the given ROFL enclave. In case enclave is not registered, returns
/// `None`.
pub fn get_registration(app_id: AppId, rak: &CorePublicKey) -> Option<types::Registration> {
    get_registration_hrak(app_id, hash_rak(rak))
}

/// Retrieves all registrations for the given ROFL application.
pub fn get_registrations_for_app(app_id: AppId) -> Vec<types::Registration> {
    CurrentState::with_store(|mut root_store| {
        let store = storage::PrefixStore::new(&mut root_store, &MODULE_NAME);
        let registrations = storage::PrefixStore::new(store, &REGISTRATIONS);
        let app = storage::TypedStore::new(storage::PrefixStore::new(registrations, app_id));

        app.iter()
            .map(|(_, registration): (Hash, types::Registration)| registration)
            .collect()
    })
}

/// Retrieves endorser of the given ROFL enclave. In case enclave is not registered, returns `None`.
pub fn get_endorser(pk: &PublicKey) -> Option<KeyEndorsementInfo> {
    let hpk = hash_pk(pk);

    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let endorsers = storage::TypedStore::new(storage::PrefixStore::new(store, &ENDORSERS));
        endorsers.get(hpk)
    })
}

fn hash_rak(rak: &CorePublicKey) -> Hash {
    hash_pk(&PublicKey::Ed25519(rak.into()))
}

fn hash_pk(pk: &PublicKey) -> Hash {
    Hash::digest_bytes_list(&[pk.key_type().as_bytes(), pk.as_ref()])
}

fn queue_entry_key(epoch: EpochTime, app_id: AppId, hrak: Hash) -> Vec<u8> {
    [&epoch.to_be_bytes(), app_id.as_ref(), hrak.as_ref()].concat()
}

fn insert_expiration_queue(epoch: EpochTime, app_id: AppId, hrak: Hash) {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let mut queue = storage::PrefixStore::new(store, &EXPIRATION_QUEUE);
        queue.insert(&queue_entry_key(epoch, app_id, hrak), &[]);
    })
}

fn remove_expiration_queue(epoch: EpochTime, app_id: AppId, hrak: Hash) {
    CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let mut queue = storage::PrefixStore::new(store, &EXPIRATION_QUEUE);
        queue.remove(&queue_entry_key(epoch, app_id, hrak));
    })
}

struct ExpirationQueueEntry {
    epoch: EpochTime,
    app_id: AppId,
    hrak: Hash,
}

impl<'a> TryFrom<&'a [u8]> for ExpirationQueueEntry {
    type Error = anyhow::Error;

    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
        // Decode a storage key of the format (epoch, hrak).
        if value.len() != 8 + AppId::SIZE + Hash::len() {
            anyhow::bail!("incorrect expiration queue key size");
        }

        Ok(Self {
            epoch: EpochTime::from_be_bytes(value[..8].try_into()?),
            app_id: value[8..8 + AppId::SIZE].try_into()?,
            hrak: value[8 + AppId::SIZE..].into(),
        })
    }
}

/// Removes all expired registrations, e.g. those that expire in epochs earlier than or equal to the
/// passed epoch.
pub fn expire_registrations(epoch: EpochTime, limit: usize) {
    let expired: Vec<_> = CurrentState::with_store(|store| {
        let store = storage::PrefixStore::new(store, &MODULE_NAME);
        let queue = storage::TypedStore::new(storage::PrefixStore::new(store, &EXPIRATION_QUEUE));

        queue
            .iter()
            .take_while(|(e, _): &(ExpirationQueueEntry, CorePublicKey)| e.epoch <= epoch)
            .map(|(e, _)| (e.app_id, e.hrak))
            .take(limit)
            .collect()
    });

    for (app_id, hrak) in expired {
        remove_registration_hrak(app_id, hrak);
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::testing::{keys, mock};

    #[test]
    fn test_app_cfg() {
        let _mock = mock::Mock::default();

        let app_id = AppId::from_creator_round_index(keys::alice::address(), 0, 0);
        let app = get_app(app_id);
        assert!(app.is_none());

        let cfg = types::AppConfig {
            id: app_id,
            policy: Default::default(),
            admin: Some(keys::alice::address()),
            stake: Default::default(),
        };
        set_app(cfg.clone());
        let app = get_app(app_id).expect("application config should be created");
        assert_eq!(app, cfg);

        let cfg = types::AppConfig { admin: None, ..cfg };
        set_app(cfg.clone());
        let app = get_app(app_id).expect("application config should be updated");
        assert_eq!(app, cfg);

        remove_app(app_id);
        let app = get_app(app_id);
        assert!(app.is_none(), "application should have been removed");
    }

    #[test]
    fn test_registration() {
        let _mock = mock::Mock::default();
        let app_id = Default::default();
        let rak = keys::alice::pk().try_into().unwrap(); // Fake RAK.
        let rak_pk = keys::alice::pk();

        let registration = get_registration(app_id, &rak);
        assert!(registration.is_none());
        let endorser = get_endorser(&rak_pk);
        assert!(endorser.is_none());
        let endorser = get_endorser(&keys::dave::pk());
        assert!(endorser.is_none());

        let new_registration = types::Registration {
            app: app_id,
            rak,
            expiration: 42,
            extra_keys: vec![
                keys::dave::pk(), // Add dave as an extra endorsed key.
            ],
            ..Default::default()
        };
        update_registration(new_registration.clone()).expect("registration update should work");

        // Ensure extra endorsed keys cannot be updated later.
        let bad_registration = types::Registration {
            app: app_id,
            extra_keys: vec![],
            ..new_registration.clone()
        };
        update_registration(bad_registration.clone())
            .expect_err("extra endorsed key update should not be allowed");

        let registration = get_registration(app_id, &rak).expect("registration should be present");
        assert_eq!(registration, new_registration);
        let endorser = get_endorser(&rak_pk).expect("endorser should be present");
        assert_eq!(endorser.node_id, new_registration.node_id);
        assert!(endorser.rak.is_none());
        let endorser = get_endorser(&keys::dave::pk()).expect("extra keys should be endorsed");
        assert_eq!(endorser.node_id, new_registration.node_id);
        assert_eq!(endorser.rak, Some(rak));
        let registrations = get_registrations_for_app(new_registration.app);
        assert_eq!(registrations.len(), 1);

        expire_registrations(42, 128);

        let registration = get_registration(app_id, &rak);
        assert!(registration.is_none());
        let endorser = get_endorser(&rak_pk);
        assert!(endorser.is_none());
        let endorser = get_endorser(&keys::dave::pk());
        assert!(endorser.is_none());
        let registrations = get_registrations_for_app(new_registration.app);
        assert_eq!(registrations.len(), 0);
    }
}