From 426e83ec6fba028692bed334803ae9d3a645cb18 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Mon, 11 Apr 2022 19:01:42 +0300 Subject: [WIP] Spring cleaning --- src/block.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/block.rs (limited to 'src/block.rs') diff --git a/src/block.rs b/src/block.rs new file mode 100644 index 0000000..e707779 --- /dev/null +++ b/src/block.rs @@ -0,0 +1,92 @@ +//! # Data Representations +//! +//! We need persistence for [`Block`]s and [`User`]s, not so much for [`Transaction`]s +//! +//! There are around 30 students, a full fledged database would be an overkill (for next year?) +//! +//! Pending transactions are held in memory, these are cleared with every new block +//! Only the last block is held in memory, every block is written to a file +//! Users are held in memory and they're also backed up to text files +use chrono::{NaiveDate, NaiveDateTime}; +use serde::{Deserialize, Serialize}; +use std::{string::String, vec::Vec}; + +pub type Fingerprint = String; +pub type Id = String; + +/// A block that was proposed with `transaction_list` and `nonce` +/// that made `hash` valid, 6 zeroes at the left hand side of the hash (24 bytes) +/// +/// We are mining using blake2s algorithm, which produces 256 bit hashes. +/// Hash/second is roughly 20x10^3. +/// +/// might be valuable to normalize the +/// serialize/deserialize conventions as these will be hashed +/// +#[derive(Serialize, Deserialize, Debug, PartialEq)] +pub struct Block { + #[serde(skip_serializing_if = "Vec::is_empty")] + pub transaction_list: Vec, + pub nonce: u32, + pub timestamp: NaiveDateTime, + pub hash: String, +} + +impl Default for Block { + fn default() -> Self { + Block { + transaction_list: vec!["gradecoin_bank".to_owned()], + nonce: 0, + timestamp: NaiveDate::from_ymd(2022, 4, 11).and_hms(20, 45, 00), + hash: String::from("not_actually_mined"), + } + } +} + +/// For prototyping and letting serde handle everything json +#[derive(Serialize, Deserialize, Debug, PartialEq)] +pub struct NakedBlock { + #[serde(skip_serializing_if = "Vec::is_empty", default)] + pub transaction_list: Vec, + pub nonce: u32, + pub timestamp: NaiveDateTime, +} + +/// A transaction between `source` and `target` that moves `amount` +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +pub struct Transaction { + pub source: Fingerprint, + pub target: Fingerprint, + pub amount: u16, + pub timestamp: NaiveDateTime, +} + +/// A JWT Payload/Claims representation +/// +/// +/// +/// - `tha`: Transaction Hash, String (custom field) +/// - `iat`: Issued At, Unix Time, epoch +/// - `exp`: Expiration Time, epoch +#[derive(Debug, Serialize, Deserialize, PartialEq)] +pub struct Claims { + pub tha: String, + pub iat: usize, + pub exp: usize, +} + +/// The plaintext of the initial user authentication request +#[derive(Serialize, Deserialize, Debug, PartialEq)] +pub struct AuthRequest { + pub student_id: String, + pub passwd: String, + pub public_key: String, +} + +/// Ciphertext of the initial authentication request, or what we will receive +#[derive(Serialize, Deserialize, Debug)] +pub struct InitialAuthRequest { + pub c: String, + pub iv: String, + pub key: String, +} -- cgit v1.2.3-70-g09d2