add_root function

This commit is contained in:
DieMyst 2021-02-12 14:54:08 +03:00
parent a9d1e72653
commit 3bcc928a72
3 changed files with 47 additions and 4 deletions

View File

@ -70,3 +70,24 @@ impl From<Result<Vec<Certificate>, ServiceError>> for AllCertsResult {
}
}
}
#[fce]
pub struct AddRootResult {
pub ret_code: u32,
pub error: String,
}
impl From<Result<(), ServiceError>> for AddRootResult {
fn from(result: Result<(), ServiceError>) -> Self {
match result {
Ok(()) => AddRootResult {
ret_code: 0,
error: "".to_string(),
},
Err(e) => AddRootResult {
ret_code: 1,
error: format!("{}", e),
},
}
}
}

View File

@ -1,9 +1,9 @@
use crate::dto::Certificate;
use crate::results::{AllCertsResult, InsertResult, WeightResult};
use crate::results::{AddRootResult, AllCertsResult, InsertResult, WeightResult};
use crate::service_impl::{
get_all_certs_impl, get_weight_impl, insert_cert_impl, insert_cert_impl_raw,
add_root_impl, get_all_certs_impl, get_weight_impl, insert_cert_impl, insert_cert_impl_raw,
};
use fluence::fce;
use fluence::{fce, CallParameters};
#[fce]
/// add a certificate in string representation to trust graph if it is valid
@ -30,6 +30,21 @@ fn get_all_certs(issued_for: String) -> AllCertsResult {
get_all_certs_impl(issued_for).into()
}
#[fce]
/// could add only a host of a trust graph service
fn add_root(pk: String, weight: u32) -> AddRootResult {
let call_parameters: CallParameters = fluence::get_call_parameters();
let init_peer_id = call_parameters.init_peer_id.clone();
if call_parameters.host_id == init_peer_id {
add_root_impl(pk, weight).into()
} else {
return AddRootResult {
ret_code: 1,
error: "Root could add only a host of trust graph service",
};
}
}
// TODO rewrite test after #[fce_test] will be implemented
// #[fce]
// fn test() -> String {

View File

@ -6,7 +6,7 @@ use std::convert::{Into, TryInto};
use std::str::FromStr;
use std::time::Duration;
use thiserror::Error as ThisError;
use trust_graph::{CertificateError, TrustGraphError};
use trust_graph::{CertificateError, PublicKeyHashable, TrustGraphError};
#[derive(ThisError, Debug)]
pub enum ServiceError {
@ -61,3 +61,10 @@ pub fn insert_cert_impl(certificate: Certificate, duration: u64) -> Result<(), S
add_cert(certificate, duration)?;
Ok(())
}
pub fn add_root_impl(pk: String, weight: u32) -> Result<(), ServiceError> {
let mut tg = get_data().lock();
let pk = PublicKey::from_base58(&pk)?.into();
tg.add_root_weight(pk, weight)?;
Ok(())
}