From 20dad7d4290f2c98583168cd4b9afcdec4802944 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Wed, 7 Apr 2021 04:33:45 +0300 Subject: Implement Block GET/PUT with new schema - `Arc`+`Mutex` is replaced by `parking_lot::RwLock,` decoupled Read+Write and ability to upgrade read locks into write locks if needed - Schema has changed, `Db` is now a struct that implements `new()` to return a new instance of itself, pros/cons listed in code but tl;dr blockchain and pending transactions are separate now - `custom_filters` now supports extracting Block json and Transaction json in separate functions too - /block GET and PUT implemented, `Blocks` currently have one check (transactions appear in pending transaction) - debug is working after something, dunno how I fixed it --- src/schema.rs | 65 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 20 deletions(-) (limited to 'src/schema.rs') diff --git a/src/schema.rs b/src/schema.rs index ea36a70..57210a3 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,31 +1,46 @@ -// Common types used across API - -use chrono::{NaiveDate, NaiveDateTime}; +use chrono::NaiveDateTime; +use parking_lot::RwLock; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use std::sync::Arc; -use tokio::sync::Mutex; // use crate::validators; -pub fn ledger() -> Db { - // TODO: there was something simpler in one of the other tutorials? <07-04-21, yigit> // - - Arc::new(Mutex::new(vec![ - Transaction { - source: String::from("Myself"), - target: String::from("Nobody"), - amount: 4, - timestamp: NaiveDate::from_ymd(2021, 4, 7).and_hms(00, 17, 00), - }, - ])) +// In memory data structure + +// Two approaches here +// 1. Db is a type +// pub type Db = Arc>>; +// Ledger is a struct, we wrap the ledger with arc + mutex in ledger() +// to access transactions we need to unwrap blocks as well, vice versa +// +// 2. Db is a struct, attributes are wrapped +// we can offload ::new() to it's member method +// blocks and transactions are accessible separately, which is the biggest pro + +/// Creates a new database +pub fn create_database() -> Db { + Db::new() } +#[derive(Debug, Clone)] +pub struct Db { + // heh. also https://doc.rust-lang.org/std/collections/struct.LinkedList.html says Vec is generally faster + pub blockchain: Arc>>, + // every proposer can have _one_ pending transaction, a way to enforce this, String is proposer identifier + pub pending_transactions: Arc>>, +} -// For presentation purposes keep mocked data in in-memory structure -// In real life scenario connection with regular database would be established - -pub type Db = Arc>>; +impl Db { + fn new() -> Self { + Db { + blockchain: Arc::new(RwLock::new(Vec::new())), + pending_transactions: Arc::new(RwLock::new(HashMap::new())), + } + } +} +/// A transaction between `source` and `target` that moves `amount` #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct Transaction { pub source: String, @@ -34,14 +49,22 @@ pub struct Transaction { pub timestamp: NaiveDateTime, } +/// A block that was proposed with `transaction_list` and `nonce` that made `hash` valid #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct Block { - pub transaction_list: Vec, // [Transaction; N] + pub transaction_list: Vec, // hashes of the transactions (or just "source" for now) pub nonce: i32, pub timestamp: NaiveDateTime, pub hash: String, // future proof'd baby } +// pub struct Ledger { +// // heh. also https://doc.rust-lang.org/std/collections/struct.LinkedList.html says Vec is generally faster +// blockchain: Vec, +// // every proposer can have _one_ pending transaction, a way to enforce this, String is proposer identifier +// pending_transactions: HashMap, +// } + // #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] // #[serde(rename_all = "camelCase")] // pub struct Game { @@ -99,6 +122,8 @@ pub struct Block { // )) // } +// TODO: these tests are amazing, we should write some when schema is decided upon <07-04-21, yigit> // + // #[cfg(test)] // mod tests { // use super::*; -- cgit v1.2.3-70-g09d2