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
396
397
398
399
400
401
use crate::{
    commons::{
        models::{
            approval::{ApprovalEntity, ApprovalResponse, ApprovalState},
            event::Metadata,
            state::{generate_subject_id, Subject},
        },
        self_signature_manager::{SelfSignatureInterface, SelfSignatureManager},
        settings::VotationType,
    },
    database::DB,
    governance::{error::RequestError, GovernanceInterface},
    identifier::{Derivable, DigestIdentifier, KeyIdentifier},
    request::EventRequest,
    signature::Signed,
    ApprovalRequest, DatabaseCollection, Notification,
};

use super::error::{ApprovalErrorResponse, ApprovalManagerError};

pub trait NotifierInterface {
    fn request_reached(&self, id: &str, subject_id: &str, sn: u64);
    fn request_obsolete(&self, id: String, subject_id: String, sn: u64);
}

pub struct RequestNotifier {
    sender: tokio::sync::mpsc::Sender<Notification>,
}

impl RequestNotifier {
    pub fn new(sender: tokio::sync::mpsc::Sender<Notification>) -> Self {
        Self { sender }
    }
}

impl NotifierInterface for RequestNotifier {
    fn request_reached(&self, id: &str, subject_id: &str, sn: u64) {
        let _ = self.sender.send(Notification::ApprovalReceived {
            id: id.to_owned(),
            subject_id: subject_id.to_owned(),
            sn,
        });
    }

    fn request_obsolete(&self, id: String, subject_id: String, sn: u64) {
        let _ = self
            .sender
            .send(Notification::ObsoletedApproval { id, subject_id, sn });
    }
}

pub struct InnerApprovalManager<G: GovernanceInterface, N: NotifierInterface, C: DatabaseCollection>
{
    governance: G,
    database: DB<C>,
    notifier: N,
    signature_manager: SelfSignatureManager,
    // Cola de 1 elemento por sujeto
    // subject_been_approved: HashMap<DigestIdentifier, DigestIdentifier>, // SubjectID -> ReqId
    pass_votation: VotationType,
}

impl<G: GovernanceInterface, N: NotifierInterface, C: DatabaseCollection>
    InnerApprovalManager<G, N, C>
{
    pub fn new(
        governance: G,
        database: DB<C>,
        notifier: N,
        signature_manager: SelfSignatureManager,
        pass_votation: VotationType,
    ) -> Self {
        Self {
            governance,
            database,
            notifier,
            signature_manager,
            // subject_been_approved: HashMap::new(),
            pass_votation,
        }
    }

    pub fn get_single_request(
        &self,
        request_id: &DigestIdentifier,
    ) -> Result<ApprovalEntity, ApprovalErrorResponse> {
        let request = self
            .database
            .get_approval(request_id)
            .map_err(|_| ApprovalErrorResponse::ApprovalRequestNotFound)?;
        Ok(request)
    }

    pub fn get_all_request(&self) -> Vec<ApprovalEntity> {
        self.database
            .get_approvals(Some(ApprovalState::Pending), None, isize::MAX)
            .unwrap()
    }

    #[allow(dead_code)]
    pub fn change_pass_votation(&mut self, pass_votation: VotationType) {
        self.pass_votation = pass_votation;
    }

    pub async fn get_governance_version(
        &self,
        governance_id: &DigestIdentifier,
        subject_id: &DigestIdentifier,
    ) -> Result<Result<u64, ApprovalErrorResponse>, ApprovalManagerError> {
        match self
            .governance
            .get_governance_version(governance_id.to_owned(), subject_id.clone())
            .await
        {
            Ok(data) => Ok(Ok(data)),
            Err(RequestError::GovernanceNotFound(_str)) => {
                Ok(Err(ApprovalErrorResponse::GovernanceNotFound))
            }
            Err(RequestError::InvalidGovernanceID) => {
                Ok(Err(ApprovalErrorResponse::InvalidGovernanceID))
            }
            Err(RequestError::ChannelClosed) => Err(ApprovalManagerError::GovernanceChannelFailed),
            Err(_error) => Err(ApprovalManagerError::UnexpectedError),
        }
    }

    pub fn new_governance_version(
        &mut self,
        governance_id: &DigestIdentifier,
    ) -> Result<(), ApprovalManagerError> {
        // Comprobamos todas las peticiones guardadas y borramos las afectadas
        let affected_requests = self
            .database
            .get_approvals_by_governance(governance_id)
            .map_err(|_| ApprovalManagerError::DatabaseError)?;
        for request in affected_requests {
            // Borrarlas de la colección principal y del índice
            let approval_entity = self
                .database
                .get_approval(&request)
                .map_err(|_| ApprovalManagerError::DatabaseError)?;
            let subject_id = {
                match approval_entity.request.content.event_request.content {
                    EventRequest::Fact(ref fact_request) => fact_request.subject_id.clone(),
                    EventRequest::Create(ref create_request) => generate_subject_id(
                        &create_request.namespace,
                        &create_request.schema_id,
                        create_request.public_key.to_str(),
                        create_request.governance_id.to_str(),
                        approval_entity.request.content.gov_version,
                    )
                    .map_err(|_| ApprovalManagerError::UnexpectedError)?,
                    _ => return Err(ApprovalManagerError::UnexpectedRequestType),
                }
            };
            self.notifier.request_obsolete(
                approval_entity.id.to_str(),
                subject_id.to_str(),
                approval_entity.request.content.sn,
            );
            self.database
                .del_approval(&request)
                .map_err(|_| ApprovalManagerError::DatabaseError)?;
            self.database
                .del_governance_approval_index(&governance_id, &request)
                .map_err(|_| ApprovalManagerError::DatabaseError)?;
            self.database
                .del_subject_approval_index(&subject_id, &request)
                .map_err(|_| ApprovalManagerError::DatabaseError)?;
        }
        Ok(())
    }

    pub async fn process_approval_request(
        &mut self,
        approval_request: Signed<ApprovalRequest>,
        sender: KeyIdentifier,
    ) -> Result<
        Result<Option<(Signed<ApprovalResponse>, KeyIdentifier)>, ApprovalErrorResponse>,
        ApprovalManagerError,
    > {
        /*
            THE APPROVER IS NOW ALSO A WITNESS
            - Check if the subject is possessed
            - Check if we are synchronized
            We check the governance version
                - We reject requests that have a governance version different from ours
            We check the cryptographic validity of the information given to us.
                - Check the invocation signature.
                - Check the validity of the invoker.
                - Check the evaluation signatures.
                - Check the validity of the evaluators.
            The requests will not be saved in the DB, but in memory.
            Only one request per subject will be saved. There is the problem that an event has been approved without our intervention.
            intervention. In this case it is necessary to delete the request and update to the new one.
            We must always check if we already have the request sent to us.
        */
        let id: DigestIdentifier =
            match DigestIdentifier::from_serializable_borsh(&approval_request.content)
                .map_err(|_| ApprovalErrorResponse::ErrorHashing)
            {
                Ok(id) => id,
                Err(error) => return Ok(Err(error)),
            };

        if let Ok(data) = self.get_single_request(&id) {
            match data.state {
                ApprovalState::Pending | ApprovalState::Obsolete => {
                    return Ok(Err(ApprovalErrorResponse::RequestAlreadyKnown))
                }
                ApprovalState::RespondedAccepted | ApprovalState::RespondedRejected => {
                    let response = data
                        .response
                        .clone()
                        .expect("Has to have a response because it is Reponded already");
                    let sender = data.sender.clone();
                    return Ok(Ok(Some((response, sender))));
                }
            }
        };

        // We check if we are already approving the subject for an equal or greater event.
        // If there is no previous request, we continue.
        let subject_id = subject_id_by_request(
            &approval_request.content.event_request.content,
            approval_request.content.gov_version,
        )?;
        let request_queue = self
            .database
            .get_approvals_by_subject(&subject_id)
            .map_err(|_| ApprovalManagerError::DatabaseError)?;
        if request_queue.len() == 1 {
            let data = self.get_single_request(&request_queue[0]).unwrap();
            if approval_request.content.sn <= data.request.content.sn {
                return Ok(Err(ApprovalErrorResponse::PreviousEventDetected));
            }
        } else if request_queue.len() != 0 {
            return Err(ApprovalManagerError::MoreRequestThanMaxAllowed);
        }

        // We check if the governance version is correct
        let version = match self
            .get_governance_version(&approval_request.content.gov_id, &subject_id)
            .await?
        {
            Ok(version) => version,
            Err(error) => return Ok(Err(error)),
        };

        let request_gov_version = approval_request.content.gov_version;

        if version > request_gov_version {
            // Nuestra gov es mayor: mandamos mensaje para que actualice el emisor
            return Ok(Err(ApprovalErrorResponse::OurGovIsHigher {
                our_id: self.signature_manager.get_own_identifier(),
                sender,
                gov_id: approval_request.content.gov_id.clone(),
            }));
        } else if version < request_gov_version {
            // Nuestra gov es menor: no podemos hacer nada. Pedimos LCE al que nos lo envió
            return Ok(Err(ApprovalErrorResponse::OurGovIsLower {
                our_id: self.signature_manager.get_own_identifier(),
                sender,
                gov_id: approval_request.content.gov_id.clone(),
            }));
        }

        // The EventRequest is correct. We can move on to save it in the system if applicable.
        // This will depend on the Flag PassVotation
        // - VotationType::Normal => It is saved in the system waiting for the user.
        // - VotarionType::AlwaysAccept => Yes vote is cast
        // - VotarionType::AlwaysReject => Negative vote cast
        let gov_id = approval_request.content.gov_id.clone();
        let sn = approval_request.content.sn;
        let approval_entity = ApprovalEntity {
            id: id.clone(),
            request: approval_request,
            response: None,
            state: ApprovalState::Pending,
            sender,
        };
        self.database
            .set_subject_approval_index(&subject_id, &id)
            .map_err(|_| ApprovalManagerError::DatabaseError)?;
        if !gov_id.digest.is_empty() {
            self.database
                .set_governance_approval_index(&gov_id, &id)
                .map_err(|_| ApprovalManagerError::DatabaseError)?;
        }
        let Ok(_result) = self.database.set_approval(&id, approval_entity) else {
            return Err(ApprovalManagerError::DatabaseError);
        };
        self.notifier
            .request_reached(&id.to_str(), &subject_id.to_str(), sn);

        match self.pass_votation {
            VotationType::Normal => return Ok(Ok(None)),
            VotationType::AlwaysAccept => {
                let (vote, sender) = self
                    .generate_vote(&id, true)
                    .await?
                    .expect("Request should be in data structure");
                return Ok(Ok(Some((vote.response.unwrap(), sender))));
            }
            VotationType::AlwaysReject => {
                let (vote, sender) = self
                    .generate_vote(&id, false)
                    .await?
                    .expect("Request should be in data structure");
                return Ok(Ok(Some((vote.response.unwrap(), sender))));
            }
        }
    }

    pub async fn generate_vote(
        &mut self,
        request_id: &DigestIdentifier,
        acceptance: bool,
    ) -> Result<Result<(ApprovalEntity, KeyIdentifier), ApprovalErrorResponse>, ApprovalManagerError>
    {
        // Obtenemos la petición
        let Ok(mut data) = self.get_single_request(&request_id) else {
            return Ok(Err(ApprovalErrorResponse::RequestNotFound));
        };
        if let ApprovalState::RespondedAccepted = data.state {
            return Ok(Err(ApprovalErrorResponse::RequestAlreadyResponded));
        } else if ApprovalState::RespondedRejected == data.state {
            return Ok(Err(ApprovalErrorResponse::RequestAlreadyResponded));
        }
        let response = ApprovalResponse {
            appr_req_hash: request_id.clone(),
            approved: acceptance,
        };
        let subject_id = subject_id_by_request(
            &data.request.content.event_request.content,
            data.request.content.gov_version,
        )?;
        let signature = self
            .signature_manager
            .sign(&response)
            .map_err(|_| ApprovalManagerError::SignProcessFailed)?;
        data.state = if acceptance {
            ApprovalState::RespondedAccepted
        } else {
            ApprovalState::RespondedRejected
        };
        data.response = Some(Signed::<ApprovalResponse> {
            content: response,
            signature,
        });
        let Ok(_result) = self.database.set_approval(&request_id, data.clone()) else {
            return Err(ApprovalManagerError::DatabaseError)
        };
        self.database
            .del_subject_approval_index(&subject_id, request_id)
            .map_err(|_| ApprovalManagerError::DatabaseError)?;
        self.database
            .del_governance_approval_index(&data.request.content.gov_id, request_id)
            .map_err(|_| ApprovalManagerError::DatabaseError)?;
        let sender = data.sender.clone();
        Ok(Ok((data, sender)))
    }
}

#[allow(dead_code)]
fn event_proposal_hash_gen(
    approval_request: &Signed<ApprovalRequest>,
) -> Result<DigestIdentifier, ApprovalManagerError> {
    Ok(DigestIdentifier::from_serializable_borsh(approval_request)
        .map_err(|_| ApprovalManagerError::HashGenerationFailed)?)
}

#[allow(dead_code)]
fn create_metadata(subject_data: &Subject, governance_version: u64) -> Metadata {
    Metadata {
        namespace: subject_data.namespace.clone(),
        subject_id: subject_data.subject_id.clone(),
        governance_id: subject_data.governance_id.clone(),
        governance_version,
        schema_id: subject_data.schema_id.clone(),
    }
}

fn subject_id_by_request(
    request: &EventRequest,
    gov_version: u64,
) -> Result<DigestIdentifier, ApprovalManagerError> {
    let subject_id = match request {
        EventRequest::Fact(ref fact_request) => fact_request.subject_id.clone(),
        EventRequest::Create(ref create_request) => generate_subject_id(
            &create_request.namespace,
            &create_request.schema_id,
            create_request.public_key.to_str(),
            create_request.governance_id.to_str(),
            gov_version,
        )
        .map_err(|_| ApprovalManagerError::UnexpectedError)?,
        _ => return Err(ApprovalManagerError::UnexpectedRequestType),
    };
    Ok(subject_id)
}