mirror of
https://github.com/fluencelabs/trust-graph-test
synced 2025-04-25 13:02:20 +00:00
wasm with error handling compiles
This commit is contained in:
parent
e2183b4180
commit
7ae745c3c3
1
bin/Cargo.lock
generated
1
bin/Cargo.lock
generated
@ -1594,6 +1594,7 @@ dependencies = [
|
|||||||
"serde_bencode",
|
"serde_bencode",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sqlite",
|
"sqlite",
|
||||||
|
"thiserror",
|
||||||
"trust-graph",
|
"trust-graph",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -26,3 +26,4 @@ rmp-serde = "0.15.0"
|
|||||||
bincode = "1.3.1"
|
bincode = "1.3.1"
|
||||||
serde_bencode = "^0.2.3"
|
serde_bencode = "^0.2.3"
|
||||||
sqlite = "0.25.3"
|
sqlite = "0.25.3"
|
||||||
|
thiserror = "1.0.23"
|
||||||
|
@ -25,19 +25,6 @@ fn insert_cert(certificate: String, duration: u64) -> InsertResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[fce]
|
|
||||||
fn looper() {
|
|
||||||
|
|
||||||
let second = std::time::Duration::from_millis(1000);
|
|
||||||
|
|
||||||
let mut a = 0;
|
|
||||||
while true {
|
|
||||||
std::thread::sleep(second);
|
|
||||||
a = a + 1;
|
|
||||||
log::info!("{}", a)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fce]
|
#[fce]
|
||||||
fn test() -> String {
|
fn test() -> String {
|
||||||
let mut tg = get_data().lock();
|
let mut tg = get_data().lock();
|
||||||
|
@ -9,14 +9,19 @@ use parking_lot::Mutex;
|
|||||||
use fce_sqlite_connector;
|
use fce_sqlite_connector;
|
||||||
use fce_sqlite_connector::Connection;
|
use fce_sqlite_connector::Connection;
|
||||||
use fce_sqlite_connector::Value;
|
use fce_sqlite_connector::Value;
|
||||||
|
use fce_sqlite_connector::Error as InternalSqliteError;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use trust_graph::{Auth, PublicKeyHashable, Revoke, Storage, TrustGraph, TrustNode, Weight};
|
use trust_graph::{Auth, PublicKeyHashable, Revoke, Storage, TrustGraph, TrustNode, Weight, StorageError};
|
||||||
use std::ops::Deref;
|
use thiserror::Error as ThisError;
|
||||||
|
use crate::storage_impl::SqliteStorageError::{SqliteError, EncodeError, ConvertionError, DecodeError, Unexpected, RevokeError};
|
||||||
|
use rmp_serde::encode::Error as RmpEncodeError;
|
||||||
|
use rmp_serde::decode::Error as RmpDecodeError;
|
||||||
|
use std::convert::From;
|
||||||
|
|
||||||
static INSTANCE: OnceCell<Mutex<TrustGraph>> = OnceCell::new();
|
static INSTANCE: OnceCell<Mutex<TrustGraph<SqliteStorage>>> = OnceCell::new();
|
||||||
|
|
||||||
pub fn get_data() -> &'static Mutex<TrustGraph> {
|
pub fn get_data() -> &'static Mutex<TrustGraph<SqliteStorage>> {
|
||||||
INSTANCE.get_or_init(|| {
|
INSTANCE.get_or_init(|| {
|
||||||
let db_path = "/tmp/users123123.sqlite";
|
let db_path = "/tmp/users123123.sqlite";
|
||||||
let connection = fce_sqlite_connector::open(db_path).unwrap();
|
let connection = fce_sqlite_connector::open(db_path).unwrap();
|
||||||
@ -46,127 +51,163 @@ impl SqliteStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(ThisError, Debug)]
|
||||||
|
pub enum SqliteStorageError {
|
||||||
|
#[error("Unexpected: {0}")]
|
||||||
|
Unexpected(String),
|
||||||
|
#[error("{0}")]
|
||||||
|
SqliteError(InternalSqliteError),
|
||||||
|
#[error("{0}")]
|
||||||
|
EncodeError(String),
|
||||||
|
#[error("{0}")]
|
||||||
|
DecodeError(String),
|
||||||
|
#[error("There is no entry for {0}")]
|
||||||
|
ConvertionError(String),
|
||||||
|
#[error("Cannot revoke a trust: {0}")]
|
||||||
|
RevokeError(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<InternalSqliteError> for SqliteStorageError {
|
||||||
|
fn from(err: InternalSqliteError) -> Self {
|
||||||
|
SqliteError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RmpEncodeError> for SqliteStorageError {
|
||||||
|
fn from(err: RmpEncodeError) -> Self {
|
||||||
|
EncodeError(format!("{}", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RmpDecodeError> for SqliteStorageError {
|
||||||
|
fn from(err: RmpDecodeError) -> Self {
|
||||||
|
DecodeError(format!("{}", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SqliteStorageError> for String {
|
||||||
|
fn from(err: SqliteStorageError) -> Self {
|
||||||
|
err.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StorageError for SqliteStorageError {}
|
||||||
|
|
||||||
impl Storage for SqliteStorage {
|
impl Storage for SqliteStorage {
|
||||||
fn get(&self, pk: &PublicKeyHashable) -> Option<TrustNode> {
|
|
||||||
|
type Error = SqliteStorageError;
|
||||||
|
|
||||||
|
fn get(&self, pk: &PublicKeyHashable) -> Result<Option<TrustNode>, Self::Error> {
|
||||||
let mut cursor = self
|
let mut cursor = self
|
||||||
.connection
|
.connection
|
||||||
.prepare("SELECT trustnode FROM trustnodes WHERE public_key = ?")
|
.prepare("SELECT trustnode FROM trustnodes WHERE public_key = ?")?
|
||||||
.expect("unexpected: 'get' request should be correct")
|
|
||||||
.cursor();
|
.cursor();
|
||||||
|
|
||||||
cursor
|
cursor
|
||||||
.bind(&[Value::String(format!("{}", pk))])
|
.bind(&[Value::String(format!("{}", pk))])?;
|
||||||
.expect("unexpected: 'public_key' field should be string");
|
|
||||||
|
|
||||||
match cursor.next().unwrap() {
|
match cursor.next().unwrap() {
|
||||||
Some(r) => {
|
Some(r) => {
|
||||||
log::info!("row: {:?}", r);
|
log::info!("row: {:?}", r);
|
||||||
let tn_bin: &[u8] = r[0]
|
let tn_bin: &[u8] = r[0]
|
||||||
.as_binary()
|
.as_binary().ok_or(ConvertionError("cannot get trustnode as binary".to_string()))?;
|
||||||
.expect("unexpected: 'trustnode' in a table should be as binary");
|
|
||||||
|
|
||||||
log::info!("binary: {:?}", tn_bin);
|
log::info!("binary: {:?}", tn_bin);
|
||||||
|
|
||||||
let trust_node: TrustNode = rmp_serde::from_read_ref(tn_bin)
|
let trust_node: TrustNode = rmp_serde::from_read_ref(tn_bin)?;
|
||||||
// let trust_node: TrustNode = bincode::deserialize(tn_bin)
|
|
||||||
// let trust_node: TrustNode = serde_bencode::de::from_bytes(tn_bin)
|
|
||||||
.expect("unexpected: 'trustnode' should be as correct binary");
|
|
||||||
|
|
||||||
log::info!("trustnode: {:?}", trust_node);
|
log::info!("trustnode: {:?}", trust_node);
|
||||||
|
|
||||||
Some(trust_node)
|
Ok(Some(trust_node))
|
||||||
}
|
}
|
||||||
|
|
||||||
None => None,
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) {
|
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) -> Result<(), Self::Error> {
|
||||||
let mut cursor = self
|
let mut cursor = self
|
||||||
.connection
|
.connection
|
||||||
.prepare("INSERT OR REPLACE INTO trustnodes VALUES (?, ?)")
|
.prepare("INSERT OR REPLACE INTO trustnodes VALUES (?, ?)")?
|
||||||
.unwrap()
|
|
||||||
.cursor();
|
.cursor();
|
||||||
|
|
||||||
let tn_vec = rmp_serde::to_vec(&node).unwrap();
|
let tn_vec = rmp_serde::to_vec(&node)?;
|
||||||
// let tn_vec = bincode::serialize(&node).unwrap();
|
|
||||||
let tn_vec = serde_bencode::to_bytes(&node).unwrap();
|
|
||||||
|
|
||||||
log::info!("insert: {:?}", tn_vec);
|
log::info!("insert: {:?}", tn_vec);
|
||||||
|
|
||||||
cursor
|
cursor
|
||||||
.bind(&[Value::String(format!("{}", pk)), Value::Binary(tn_vec)])
|
.bind(&[Value::String(format!("{}", pk)), Value::Binary(tn_vec)])?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
cursor.next().unwrap();
|
cursor.next()?;
|
||||||
|
Ok({})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<Weight> {
|
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Result<Option<Weight>, Self::Error> {
|
||||||
let mut cursor = self
|
let mut cursor = self
|
||||||
.connection
|
.connection
|
||||||
.prepare("SELECT public_key,weight FROM roots WHERE public_key = ?")
|
.prepare("SELECT public_key,weight FROM roots WHERE public_key = ?")?
|
||||||
.unwrap()
|
|
||||||
.cursor();
|
.cursor();
|
||||||
|
|
||||||
cursor.bind(&[Value::String(format!("{}", pk))]).unwrap();
|
cursor.bind(&[Value::String(format!("{}", pk))])?;
|
||||||
|
|
||||||
if let Some(row) = cursor.next().unwrap() {
|
if let Some(row) = cursor.next()? {
|
||||||
log::info!("row: {:?}", row);
|
log::info!("row: {:?}", row);
|
||||||
|
|
||||||
let w = u32::try_from(row[1].as_integer().unwrap()).unwrap();
|
let w = u32::try_from(row[1].as_integer().ok_or(ConvertionError("cannot get weight as integer".to_string()))?)
|
||||||
|
.map_err(|e| Unexpected(format!("Unexpected. Cannot convert weight to u32: {}", e)))?;
|
||||||
|
|
||||||
Some(w)
|
Ok(Some(w))
|
||||||
} else {
|
} else {
|
||||||
None
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {
|
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) -> Result<(), Self::Error> {
|
||||||
log::info!("add root: {} weight: {}", pk, weight);
|
log::info!("add root: {} weight: {}", pk, weight);
|
||||||
let mut cursor = self
|
let mut cursor = self
|
||||||
.connection
|
.connection
|
||||||
.prepare("INSERT OR REPLACE INTO roots VALUES (?, ?)")
|
.prepare("INSERT OR REPLACE INTO roots VALUES (?, ?)")?
|
||||||
.unwrap()
|
|
||||||
.cursor();
|
.cursor();
|
||||||
|
|
||||||
cursor
|
cursor
|
||||||
.bind(&[
|
.bind(&[
|
||||||
Value::String(format!("{}", pk)),
|
Value::String(format!("{}", pk)),
|
||||||
Value::Integer(i64::from(weight)),
|
Value::Integer(i64::from(weight)),
|
||||||
])
|
])?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
cursor.next().unwrap();
|
cursor.next()?;
|
||||||
|
Ok({})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn root_keys(&self) -> Vec<PublicKeyHashable> {
|
fn root_keys(&self) -> Result<Vec<PublicKeyHashable>, Self::Error> {
|
||||||
let mut cursor = self
|
let mut cursor = self
|
||||||
.connection
|
.connection
|
||||||
.prepare("SELECT public_key,weight FROM roots")
|
.prepare("SELECT public_key,weight FROM roots")?
|
||||||
.unwrap()
|
|
||||||
.cursor();
|
.cursor();
|
||||||
|
|
||||||
let mut roots = vec![];
|
let mut roots = vec![];
|
||||||
|
|
||||||
while let Some(row) = cursor.next().unwrap() {
|
while let Some(row) = cursor.next()? {
|
||||||
log::info!("row: {:?}", row);
|
log::info!("row: {:?}", row);
|
||||||
|
let pk = row[0].as_string()
|
||||||
|
.ok_or(ConvertionError("cannot get public key as binary".to_string()))?;
|
||||||
let pk: PublicKeyHashable =
|
let pk: PublicKeyHashable =
|
||||||
PublicKeyHashable::from_str(row[0].as_string().unwrap()).unwrap();
|
PublicKeyHashable::from_str(pk).map_err(|e| DecodeError(e.to_string()))?;
|
||||||
|
|
||||||
roots.push(pk)
|
roots.push(pk)
|
||||||
}
|
}
|
||||||
|
|
||||||
roots
|
Ok(roots)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String> {
|
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), Self::Error> {
|
||||||
match self.get(&pk) {
|
match self.get(&pk)? {
|
||||||
Some(mut trust_node) => {
|
Some(mut trust_node) => {
|
||||||
trust_node.update_revoke(revoke);
|
trust_node.update_revoke(revoke);
|
||||||
self.insert(pk.clone(), trust_node);
|
self.insert(pk.clone(), trust_node)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
None => Err("There is no trust with such PublicKey".to_string()),
|
None => Err(RevokeError("There is no trust with such PublicKey".to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,8 +217,8 @@ impl Storage for SqliteStorage {
|
|||||||
auth: Auth,
|
auth: Auth,
|
||||||
issued_for: &PublicKey,
|
issued_for: &PublicKey,
|
||||||
cur_time: Duration,
|
cur_time: Duration,
|
||||||
) {
|
) -> Result<(), Self::Error> {
|
||||||
match self.get(&pk) {
|
match self.get(&pk)? {
|
||||||
Some(mut trust_node) => {
|
Some(mut trust_node) => {
|
||||||
trust_node.update_auth(auth);
|
trust_node.update_auth(auth);
|
||||||
self.insert(pk.clone(), trust_node)
|
self.insert(pk.clone(), trust_node)
|
||||||
@ -185,7 +226,7 @@ impl Storage for SqliteStorage {
|
|||||||
None => {
|
None => {
|
||||||
let mut trust_node = TrustNode::new(issued_for.clone(), cur_time);
|
let mut trust_node = TrustNode::new(issued_for.clone(), cur_time);
|
||||||
trust_node.update_auth(auth);
|
trust_node.update_auth(auth);
|
||||||
self.insert(pk.clone(), trust_node);
|
self.insert(pk.clone(), trust_node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ pub struct Certificate {
|
|||||||
pub enum CerificateError {
|
pub enum CerificateError {
|
||||||
#[error("Error while decoding a certificate: {0}")]
|
#[error("Error while decoding a certificate: {0}")]
|
||||||
DecodeError(String),
|
DecodeError(String),
|
||||||
#[error("Certificate is expired. Issued at {issued_at:?} and expired at {expires_at:?}")]
|
#[error("Certificate is expired. Issued at {issued_at} and expired at {expires_at}")]
|
||||||
ExpirationError {
|
ExpirationError {
|
||||||
expires_at: String,
|
expires_at: String,
|
||||||
issued_at: String,
|
issued_at: String,
|
||||||
|
@ -42,5 +42,5 @@ pub use crate::public_key_hashable::PublicKeyHashable;
|
|||||||
pub use crate::revoke::Revoke;
|
pub use crate::revoke::Revoke;
|
||||||
pub use crate::trust::Trust;
|
pub use crate::trust::Trust;
|
||||||
pub use crate::trust_graph::{TrustGraph, Weight};
|
pub use crate::trust_graph::{TrustGraph, Weight};
|
||||||
pub use crate::trust_graph_storage::Storage;
|
pub use crate::trust_graph_storage::{Storage, StorageError};
|
||||||
pub use crate::trust_node::{Auth, TrustNode};
|
pub use crate::trust_node::{Auth, TrustNode};
|
||||||
|
@ -119,7 +119,7 @@ impl Storage for InMemoryStorage {
|
|||||||
auth: Auth,
|
auth: Auth,
|
||||||
issued_for: &PublicKey,
|
issued_for: &PublicKey,
|
||||||
cur_time: Duration,
|
cur_time: Duration,
|
||||||
) -> Result<(), InMemoryStorageError> {
|
) -> Result<(), Self::Error> {
|
||||||
match self.nodes.get_mut(&pk) {
|
match self.nodes.get_mut(&pk) {
|
||||||
Some(trust_node) => {
|
Some(trust_node) => {
|
||||||
trust_node.update_auth(auth);
|
trust_node.update_auth(auth);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user