serialize\deserialize all primitives and store it to db

This commit is contained in:
DieMyst 2021-01-21 20:26:17 +03:00
parent afab376ccc
commit 467391a8f1
9 changed files with 181 additions and 11 deletions

1
Cargo.lock generated
View File

@ -404,6 +404,7 @@ dependencies = [
"rand 0.7.3", "rand 0.7.3",
"serde", "serde",
"serde_json", "serde_json",
"serde_with",
"signature", "signature",
] ]

13
bin/Cargo.lock generated
View File

@ -66,6 +66,16 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "bincode"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f30d3a39baa26f9651f17b375061f3233dde33424a8b72b0dbe93a68a0bc896d"
dependencies = [
"byteorder",
"serde",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@ -429,6 +439,7 @@ dependencies = [
"rand 0.7.3", "rand 0.7.3",
"serde", "serde",
"serde_json", "serde_json",
"serde_with",
"signature", "signature",
] ]
@ -1493,7 +1504,9 @@ name = "trust-graph-wasm"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bincode",
"boolinator", "boolinator",
"bs58 0.3.1",
"fce-sqlite-connector", "fce-sqlite-connector",
"fluence", "fluence",
"fluence-identity", "fluence-identity",

View File

@ -21,3 +21,5 @@ once_cell = "1.4.1"
parking_lot = "0.11.1" parking_lot = "0.11.1"
fce-sqlite-connector = "0.1.3" fce-sqlite-connector = "0.1.3"
serde_json = "1.0" serde_json = "1.0"
bs58 = "0.3.1"
bincode = "1.3.1"

15
bin/Config.toml Normal file
View File

@ -0,0 +1,15 @@
modules_dir = "artifacts/"
[[module]]
name = "sqlite3"
mem_pages_count = 100
logger_enabled = false
[[module]]
name = "trust-graph"
mem_pages_count = 1
logger_enabled = true
[module.wasi]
preopened_files = ["/tmp"]
mapped_dirs = { "tmp" = "/tmp" }

View File

@ -10,6 +10,7 @@ fn test() -> String {
let mut tg = get_data().lock(); let mut tg = get_data().lock();
let root_kp = KeyPair::generate(); let root_kp = KeyPair::generate();
let root_kp2 = KeyPair::generate();
let second_kp = KeyPair::generate(); let second_kp = KeyPair::generate();
let expires_at = Duration::new(15, 15); let expires_at = Duration::new(15, 15);
@ -17,6 +18,7 @@ fn test() -> String {
let mut cert = Certificate::issue_root(&root_kp, second_kp.public_key(), expires_at, issued_at); let mut cert = Certificate::issue_root(&root_kp, second_kp.public_key(), expires_at, issued_at);
tg.add_root_weight(root_kp.public().into(), 0); tg.add_root_weight(root_kp.public().into(), 0);
tg.add_root_weight(root_kp2.public().into(), 1);
tg.add(cert, Duration::new(10, 10)); tg.add(cert, Duration::new(10, 10));
let a = tg.get(second_kp.public_key()); let a = tg.get(second_kp.public_key());

View File

@ -8,6 +8,7 @@ use fce_sqlite_connector::{Connection, State};
use fluence_identity::public_key::PublicKey; use fluence_identity::public_key::PublicKey;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use parking_lot::Mutex; use parking_lot::Mutex;
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};
@ -18,9 +19,13 @@ pub fn get_data() -> &'static Mutex<TrustGraph> {
let db_path = "/tmp/users.sqlite"; let db_path = "/tmp/users.sqlite";
let connection = fce_sqlite_connector::open(db_path).unwrap(); let connection = fce_sqlite_connector::open(db_path).unwrap();
let init_sql = "CREATE TABLE IF NOT EXISTS trustnodes(\ let init_sql = "CREATE TABLE IF NOT EXISTS trustnodes(
public_key TEXT PRIMARY KEY,\ public_key TEXT PRIMARY KEY,
trustnode TEXT NOT NULL,\ trustnode BLOB NOT NULL
);
CREATE TABLE IF NOT EXISTS roots(
public_key TEXT,
weight INTEGER
);"; );";
connection.execute(init_sql).expect("cannot connect to db"); connection.execute(init_sql).expect("cannot connect to db");
@ -49,11 +54,15 @@ impl Storage for SqliteStorage {
match cursor.next().unwrap() { match cursor.next().unwrap() {
Some(r) => { Some(r) => {
let tn_str = r[0] let tn_bin = r[0]
.as_string() .as_binary()
.expect("unexpected: 'trustnode' in a table should be as string"); .expect("unexpected: 'trustnode' in a table should be as string");
let trust_node: TrustNode = serde_json::from_str(tn_str)
let trust_node: TrustNode = bincode::deserialize(tn_bin)
.expect("unexpected: 'trustnode' should be as correct json"); .expect("unexpected: 'trustnode' should be as correct json");
log::info!("trustnode: {:?}", trust_node);
Some(trust_node) Some(trust_node)
} }
@ -68,11 +77,10 @@ impl Storage for SqliteStorage {
.unwrap() .unwrap()
.cursor(); .cursor();
let tn_str = serde_json::to_string(&node).unwrap(); let tn_vec = bincode::serialize(&node).unwrap();
cursor.bind(&[Value::String(format!("{}", pk))]).unwrap();
cursor cursor
.bind(&[Value::String(format!("{}", tn_str))]) .bind(&[Value::String(format!("{}", pk)), Value::Binary(tn_vec)])
.unwrap(); .unwrap();
cursor.next().unwrap(); cursor.next().unwrap();
@ -82,10 +90,42 @@ impl Storage for SqliteStorage {
None None
} }
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {} fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {
log::info!("add root: {} weight: {}", pk, weight);
let mut cursor = self
.connection
.prepare("INSERT INTO roots VALUES (?, ?)")
.unwrap()
.cursor();
cursor
.bind(&[
Value::String(format!("{}", pk)),
Value::Integer(i64::from(weight)),
])
.unwrap();
cursor.next().unwrap();
}
fn root_keys(&self) -> Vec<PublicKeyHashable> { fn root_keys(&self) -> Vec<PublicKeyHashable> {
vec![] let mut cursor = self
.connection
.prepare("SELECT public_key,weight FROM roots")
.unwrap()
.cursor();
let mut roots = vec![];
while let Some(row) = cursor.next().unwrap() {
log::info!("row: {:?}", row);
let pk: PublicKeyHashable =
PublicKeyHashable::from_str(row[0].as_string().unwrap()).unwrap();
roots.push(pk)
}
roots
} }
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String> { fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String> {
@ -99,5 +139,16 @@ impl Storage for SqliteStorage {
issued_for: &PublicKey, issued_for: &PublicKey,
cur_time: Duration, cur_time: Duration,
) { ) {
match self.get(&pk) {
Some(mut trust_node) => {
trust_node.update_auth(auth);
self.insert(pk.clone(), trust_node)
}
None => {
let mut trust_node = TrustNode::new(issued_for.clone(), cur_time);
trust_node.update_auth(auth);
self.insert(pk.clone(), trust_node);
}
}
} }
} }

70
identity/Cargo.lock generated
View File

@ -162,6 +162,41 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "darling"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]] [[package]]
name = "data-encoding" name = "data-encoding"
version = "2.3.1" version = "2.3.1"
@ -307,6 +342,7 @@ dependencies = [
"rand 0.7.3", "rand 0.7.3",
"serde", "serde",
"serde_json", "serde_json",
"serde_with",
"signature", "signature",
] ]
@ -514,6 +550,12 @@ dependencies = [
"hmac", "hmac",
] ]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.2.0" version = "0.2.0"
@ -1083,6 +1125,28 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_with"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15f6201e064705553ece353a736a64be975680bd244908cf63e8fa71e478a51a"
dependencies = [
"serde",
"serde_with_macros",
]
[[package]]
name = "serde_with_macros"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1197ff7de45494f290c1e3e1a6f80e108974681984c87a3e480991ef3d0f1950"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.8.2" version = "0.8.2"
@ -1138,6 +1202,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "1.0.0" version = "1.0.0"

View File

@ -15,3 +15,4 @@ ed25519-dalek = { version = "1.0.1", features = ["serde"] }
rand = "0.7.0" rand = "0.7.0"
signature = "1.3.0" signature = "1.3.0"
ed25519 = "1.0.3" ed25519 = "1.0.3"
serde_with = "1.6.0"

View File

@ -19,6 +19,7 @@ use fluence_identity::public_key::PublicKey;
use core::fmt; use core::fmt;
use ref_cast::RefCast; use ref_cast::RefCast;
use serde::ser::Serializer; use serde::ser::Serializer;
use std::str::FromStr;
use std::{ use std::{
fmt::{Display, Formatter}, fmt::{Display, Formatter},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
@ -80,6 +81,20 @@ impl Display for PublicKeyHashable {
} }
} }
impl FromStr for PublicKeyHashable {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = bs58::decode(s)
.into_vec()
.map_err(|err| format!("Invalid string '{}': {}", s, err))?;
let pk = PublicKey::from_bytes(&bytes)
.map_err(|err| format!("Invalid bytes {:?}: {}", bytes, err))?;
Ok(PublicKeyHashable::from(pk))
}
}
impl serde::Serialize for PublicKeyHashable { impl serde::Serialize for PublicKeyHashable {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where