From d855383dbad51322670891779d41b6a453631178 Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Sun, 15 Dec 2024 12:42:21 +0900 Subject: [PATCH 1/9] =?UTF-8?q?Block,=20BlockHeader=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- jaeseung-blockchain/Cargo.lock | 321 +++++++++++++++++++++++++ jaeseung-blockchain/Cargo.toml | 8 + jaeseung-blockchain/src/block/block.rs | 77 ++++++ jaeseung-blockchain/src/block/mod.rs | 2 + jaeseung-blockchain/src/main.rs | 10 + jaeseung/src/dependency.rs | 2 +- jaeseung/src/domain/login.rs | 8 +- jaeseung/src/domain/mod.rs | 2 +- jaeseung/src/domain/sign.rs | 9 +- jaeseung/src/main.rs | 10 +- jaeseung/src/user/mod.rs | 2 +- jaeseung/src/user/repo/db.rs | 31 ++- jaeseung/src/user/repo/file.rs | 25 +- jaeseung/src/user/repo/interface.rs | 2 +- jaeseung/src/user/user.rs | 4 +- jaeseung/src/utils/mod.rs | 2 +- jaeseung/src/utils/print_util.rs | 6 +- 18 files changed, 485 insertions(+), 39 deletions(-) create mode 100644 jaeseung-blockchain/Cargo.lock create mode 100644 jaeseung-blockchain/Cargo.toml create mode 100644 jaeseung-blockchain/src/block/block.rs create mode 100644 jaeseung-blockchain/src/block/mod.rs create mode 100644 jaeseung-blockchain/src/main.rs diff --git a/.gitignore b/.gitignore index 6fe8ad4..5f73c98 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/jaeseung/target/ \ No newline at end of file +/jaeseung/target/ +/jaeseung-blockchain/target/ \ No newline at end of file diff --git a/jaeseung-blockchain/Cargo.lock b/jaeseung-blockchain/Cargo.lock new file mode 100644 index 0000000..06b9650 --- /dev/null +++ b/jaeseung-blockchain/Cargo.lock @@ -0,0 +1,321 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "async-trait" +version = "0.1.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bytes" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "jaeseung-blockchain" +version = "0.1.0" +dependencies = [ + "rs_merkle", + "sha256", +] + +[[package]] +name = "libc" +version = "0.2.168" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rs_merkle" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b241d2e59b74ef9e98d94c78c47623d04c8392abaf82014dfd372a16041128f" +dependencies = [ + "sha2", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha256" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18278f6a914fa3070aa316493f7d2ddfb9ac86ebc06fa3b83bffda487e9065b0" +dependencies = [ + "async-trait", + "bytes", + "hex", + "sha2", + "tokio", +] + +[[package]] +name = "syn" +version = "2.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tokio" +version = "1.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" +dependencies = [ + "backtrace", + "bytes", + "pin-project-lite", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/jaeseung-blockchain/Cargo.toml b/jaeseung-blockchain/Cargo.toml new file mode 100644 index 0000000..aae1b38 --- /dev/null +++ b/jaeseung-blockchain/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "jaeseung-blockchain" +version = "0.1.0" +edition = "2021" + +[dependencies] +rs_merkle = "1.4" +sha256 = "1.5.0" \ No newline at end of file diff --git a/jaeseung-blockchain/src/block/block.rs b/jaeseung-blockchain/src/block/block.rs new file mode 100644 index 0000000..0b31801 --- /dev/null +++ b/jaeseung-blockchain/src/block/block.rs @@ -0,0 +1,77 @@ +use std::{str::from_utf8, time::{SystemTime, UNIX_EPOCH}}; + +use sha256::digest; + +use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree}; + +#[derive(Debug)] +pub struct Block { + header: BlockHeader, + data: Vec +} + +impl Block { + + pub fn new_genesis(height: i32, nonce: i32, data: &Vec) -> Result { + Ok(Block { + header: BlockHeader::make(height, &["0"; 64].join(""), nonce, &data)?, + data: data.clone() + }) + } + + pub fn new(height: i32, previous_hash: &String, nonce: i32, data: &Vec) -> Result { + Ok(Block { + header: BlockHeader::make(height, previous_hash, nonce, data)?, + data: data.clone() + }) + } +} + +#[derive(Debug)] +struct BlockHeader { + version: String, + height: i32, // 블록의 높이. 블록 체인에 연결된 블록의 수 + timestamp: u64, + hash: String, // 생성일시, 버전, bits, 머클루트, previousHash, nonce 의 조합을 해싱한 값 + previous_hash: Option, // 이전 블록의 해싱 값 + merkle_root: String, // https://brunch.co.kr/@gapcha/263 + nonce: i32, + difficulty: i32, +} + +impl BlockHeader { + const VERSION: &str = "1.0.0"; + const DIFFICULTY: i32 = 0; + + fn make(height: i32, previous_hash: &String, nonce: i32, data: &Vec) -> Result { + let version = Self::VERSION.to_string(); + let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); + let difficulty = Self::DIFFICULTY; + let merkle_root = Self::make_merkle_root(data).ok_or("Merkle Tree Parsing Failed")?; + + Ok(BlockHeader { + version, + height, + timestamp, + hash: Self::make_hash(timestamp, &merkle_root, previous_hash, nonce), + previous_hash: Some(previous_hash.clone()), + merkle_root, + nonce, + difficulty + }) + } + + // TODO : data 가 빈 배열일 때 오류남. + fn make_merkle_root(data: &Vec) -> Option { + let mut merkle_tree: MerkleTree = MerkleTree::new(); + let mut leaves: Vec<[u8; 32]> = data.iter().map(|x| Sha256::hash(x.as_bytes())).collect(); + merkle_tree.append(&mut leaves).commit(); + + merkle_tree.root_hex() + } + + fn make_hash(timestamp: u64, merkle_root: &String, previous_hash: &String, nonce: i32) -> String { + let target = format!("{}{}{}{}", timestamp.to_string(), merkle_root, previous_hash, nonce.to_string()); + digest(target) + } +} \ No newline at end of file diff --git a/jaeseung-blockchain/src/block/mod.rs b/jaeseung-blockchain/src/block/mod.rs new file mode 100644 index 0000000..215fd92 --- /dev/null +++ b/jaeseung-blockchain/src/block/mod.rs @@ -0,0 +1,2 @@ +pub mod block; +pub use block::*; diff --git a/jaeseung-blockchain/src/main.rs b/jaeseung-blockchain/src/main.rs new file mode 100644 index 0000000..a535ba9 --- /dev/null +++ b/jaeseung-blockchain/src/main.rs @@ -0,0 +1,10 @@ +mod block; + +use block::Block; +use rs_merkle::{algorithms::Sha256, MerkleTree}; + +fn main() { + let block = Block::new_genesis(0, 0, &vec![String::from("asdasd")]); + + print!("{:?}", block); +} diff --git a/jaeseung/src/dependency.rs b/jaeseung/src/dependency.rs index 5242e58..16b79eb 100644 --- a/jaeseung/src/dependency.rs +++ b/jaeseung/src/dependency.rs @@ -1,3 +1,3 @@ use crate::user::{repo, UserDbRepository}; -pub static USER_REPO: repo::UserDbRepository = UserDbRepository {}; \ No newline at end of file +pub static USER_REPO: repo::UserDbRepository = UserDbRepository {}; diff --git a/jaeseung/src/domain/login.rs b/jaeseung/src/domain/login.rs index 03f5b10..c6f2825 100644 --- a/jaeseung/src/domain/login.rs +++ b/jaeseung/src/domain/login.rs @@ -1,7 +1,7 @@ -use std::ops::Deref; +use crate::dependency::USER_REPO; use crate::user::UserRepository; use crate::utils::get_input; -use crate::dependency::USER_REPO; +use std::ops::Deref; pub fn login() -> Result<(), String> { let id = get_input("ID 를 입력해 주세요"); @@ -11,6 +11,6 @@ pub fn login() -> Result<(), String> { match user_opt { Some(user) if user.password.eq(password.deref()) => Ok(()), - _ => Err("id, password 를 다시 확인해 주세요.".to_string()) + _ => Err("id, password 를 다시 확인해 주세요.".to_string()), } -} \ No newline at end of file +} diff --git a/jaeseung/src/domain/mod.rs b/jaeseung/src/domain/mod.rs index 3673407..92ae353 100644 --- a/jaeseung/src/domain/mod.rs +++ b/jaeseung/src/domain/mod.rs @@ -1,4 +1,4 @@ pub mod login; pub use login::*; pub mod sign; -pub use sign::*; \ No newline at end of file +pub use sign::*; diff --git a/jaeseung/src/domain/sign.rs b/jaeseung/src/domain/sign.rs index 1e7cab1..5268979 100644 --- a/jaeseung/src/domain/sign.rs +++ b/jaeseung/src/domain/sign.rs @@ -1,5 +1,5 @@ -use crate::get_input; use crate::dependency::USER_REPO; +use crate::get_input; use crate::user::{User, UserRepository}; pub fn sign_in() -> Result<(), String> { @@ -14,10 +14,13 @@ pub fn sign_in() -> Result<(), String> { let password_check = get_input("비밀번호를 다시 입력해 주세요"); if password == password_check { - USER_REPO.save_user(User { id: id.to_string(), password: password.to_string() }) + USER_REPO.save_user(User { + id: id.to_string(), + password: password.to_string(), + }) } else { Err("비밀번호가 다릅니다. 처음부터 다시 시도해 주세요.".to_string()) } } } -} \ No newline at end of file +} diff --git a/jaeseung/src/main.rs b/jaeseung/src/main.rs index 2655180..ecaf654 100644 --- a/jaeseung/src/main.rs +++ b/jaeseung/src/main.rs @@ -1,11 +1,11 @@ -mod domain; -mod utils; mod dependency; +mod domain; mod user; +mod utils; -use std::ops::Deref; -use utils::*; use domain::*; +use std::{collections::HashMap, ops::Deref}; +use utils::*; fn main() { loop { @@ -29,4 +29,4 @@ fn main() { } } } -} \ No newline at end of file +} diff --git a/jaeseung/src/user/mod.rs b/jaeseung/src/user/mod.rs index 29fe760..6824944 100644 --- a/jaeseung/src/user/mod.rs +++ b/jaeseung/src/user/mod.rs @@ -1,4 +1,4 @@ pub mod user; pub use user::*; pub mod repo; -pub use repo::*; \ No newline at end of file +pub use repo::*; diff --git a/jaeseung/src/user/repo/db.rs b/jaeseung/src/user/repo/db.rs index 32c195d..37a8311 100644 --- a/jaeseung/src/user/repo/db.rs +++ b/jaeseung/src/user/repo/db.rs @@ -8,40 +8,51 @@ const DB_USER_NAME: &'static str = "root"; const DB_PASSWORD: &'static str = "1234"; const DB_IP: &'static str = "localhost"; const DB_PORT: u16 = 3306_u16; -const DB_NAME:&'static str = "my_schema"; +const DB_NAME: &'static str = "my_schema"; pub struct UserDbRepository; impl UserRepository for UserDbRepository { - fn find_user_by_id(&self, id: &Box) -> Result, String> { // TODO : 커넥션 static 하게 - let db_url = format!("mysql://{}:{}@{}:{}/{}", DB_USER_NAME, DB_PASSWORD, DB_IP, DB_PORT, DB_NAME); + let db_url = format!( + "mysql://{}:{}@{}:{}/{}", + DB_USER_NAME, DB_PASSWORD, DB_IP, DB_PORT, DB_NAME + ); let pool = mysql::Pool::new(db_url).expect("연결 실패!"); let query = "select * from user where id=:id"; - let results = pool.prep_exec(query, mysql::params! { "id" => id.as_str() }).expect("쿼리 실패"); + let results = pool + .prep_exec(query, mysql::params! { "id" => id.as_str() }) + .expect("쿼리 실패"); let row = results.last().map(|row| row.unwrap()); match row { Some(row) => { let (id, password): (String, String) = mysql::from_row(row); - Ok(Some(User {id, password})) - }, - None => Ok(None) + Ok(Some(User { id, password })) + } + None => Ok(None), } } fn save_user(&self, user: User) -> Result<(), String> { - let db_url = format!("mysql://{}:{}@{}:{}/{}", DB_USER_NAME, DB_PASSWORD, DB_IP, DB_PORT, DB_NAME); + let db_url = format!( + "mysql://{}:{}@{}:{}/{}", + DB_USER_NAME, DB_PASSWORD, DB_IP, DB_PORT, DB_NAME + ); let pool = mysql::Pool::new(db_url).expect("연결 실패!"); let query = "insert into user (id, password) values (:id, :password)"; - pool.prep_exec(query, mysql::params! { "id" => user.id.as_str(), "password" => user.password.as_str() }).expect("쿼리 실패"); + pool.prep_exec( + query, + mysql::params! { "id" => user.id.as_str(), "password" => user.password.as_str() }, + ) + .expect("쿼리 실패"); Ok(()) } -} \ No newline at end of file +} diff --git a/jaeseung/src/user/repo/file.rs b/jaeseung/src/user/repo/file.rs index 5327e02..f073eb7 100644 --- a/jaeseung/src/user/repo/file.rs +++ b/jaeseung/src/user/repo/file.rs @@ -13,18 +13,24 @@ impl UserRepository for UserFileRepository { let mut file = OpenOptions::new().read(true).open("src/users.csv").unwrap(); let mut contents = String::new(); - file.read_to_string(&mut contents).expect("read file Error 발생!"); + file.read_to_string(&mut contents) + .expect("read file Error 발생!"); // TODO : 함수형으로 작성 for content in contents.split("\n") { let row = content.split(",").collect::>(); - if row.len() < 2 { break; } + if row.len() < 2 { + break; + } let exist_id = row.get(0).unwrap(); let password = row.get(1).unwrap(); if id.deref() == exist_id { - return Ok(Some(User { id: exist_id.to_string(), password: password.to_string()})); + return Ok(Some(User { + id: exist_id.to_string(), + password: password.to_string(), + })); } } @@ -32,10 +38,15 @@ impl UserRepository for UserFileRepository { } fn save_user(&self, user: User) -> Result<(), String> { - let mut file = OpenOptions::new().write(true).append(true).open("src/users.csv").unwrap(); + let mut file = OpenOptions::new() + .write(true) + .append(true) + .open("src/users.csv") + .unwrap(); let content = format!("{},{}\n", user.id, user.password); - - file.write(content.as_bytes()).expect("write file Error 발생!"); + + file.write(content.as_bytes()) + .expect("write file Error 발생!"); Ok(()) } -} \ No newline at end of file +} diff --git a/jaeseung/src/user/repo/interface.rs b/jaeseung/src/user/repo/interface.rs index 1f7b533..255f18d 100644 --- a/jaeseung/src/user/repo/interface.rs +++ b/jaeseung/src/user/repo/interface.rs @@ -3,4 +3,4 @@ use crate::user::User; pub trait UserRepository { fn find_user_by_id(&self, id: &Box) -> Result, String>; fn save_user(&self, user: User) -> Result<(), String>; -} \ No newline at end of file +} diff --git a/jaeseung/src/user/user.rs b/jaeseung/src/user/user.rs index 6aa1f58..73dd7b5 100644 --- a/jaeseung/src/user/user.rs +++ b/jaeseung/src/user/user.rs @@ -1,4 +1,4 @@ pub struct User { pub id: String, - pub password: String -} \ No newline at end of file + pub password: String, +} diff --git a/jaeseung/src/utils/mod.rs b/jaeseung/src/utils/mod.rs index 426222f..eadd0c8 100644 --- a/jaeseung/src/utils/mod.rs +++ b/jaeseung/src/utils/mod.rs @@ -1,2 +1,2 @@ pub mod print_util; -pub use print_util::*; \ No newline at end of file +pub use print_util::*; diff --git a/jaeseung/src/utils/print_util.rs b/jaeseung/src/utils/print_util.rs index 62ff04c..403d76e 100644 --- a/jaeseung/src/utils/print_util.rs +++ b/jaeseung/src/utils/print_util.rs @@ -14,6 +14,8 @@ pub fn get_input(order: &str) -> Box { io::stdout().flush().expect("Flush 실패!"); let mut input = String::new(); - io::stdin().read_line(&mut input).expect("read_line Error 발생!"); + io::stdin() + .read_line(&mut input) + .expect("read_line Error 발생!"); Box::from(input.trim().to_string()) -} \ No newline at end of file +} From 2ab739c4be5a88b64f307f2e54a583f753999393 Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Sun, 5 Jan 2025 09:42:31 +0900 Subject: [PATCH 2/9] =?UTF-8?q?=EB=B8=94=EB=A1=9D=EC=B2=B4=EC=9D=B8=201?= =?UTF-8?q?=EC=A3=BC=EC=B0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeseung-blockchain/src/block/block.rs | 75 ++++++++++++++++++++------ jaeseung-blockchain/src/main.rs | 3 +- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/jaeseung-blockchain/src/block/block.rs b/jaeseung-blockchain/src/block/block.rs index 0b31801..6e16db0 100644 --- a/jaeseung-blockchain/src/block/block.rs +++ b/jaeseung-blockchain/src/block/block.rs @@ -1,33 +1,38 @@ -use std::{str::from_utf8, time::{SystemTime, UNIX_EPOCH}}; +use std::time::{SystemTime, UNIX_EPOCH}; use sha256::digest; use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree}; -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct Block { header: BlockHeader, - data: Vec + data: Vec, } impl Block { - - pub fn new_genesis(height: i32, nonce: i32, data: &Vec) -> Result { + const GENESIS_DATA: &str = "This is GENESIS block"; + + pub fn new_genesis() -> Result { + let data = vec![Self::GENESIS_DATA.to_string()]; + Ok(Block { - header: BlockHeader::make(height, &["0"; 64].join(""), nonce, &data)?, - data: data.clone() + header: BlockHeader::make(0, &["0"; 64].join(""), 0, &data)?, + data: data, }) } - pub fn new(height: i32, previous_hash: &String, nonce: i32, data: &Vec) -> Result { + pub fn new(previous_block: &Block, nonce: i32, data: &Vec) -> Result { + let p_header = &previous_block.header; + Ok(Block { - header: BlockHeader::make(height, previous_hash, nonce, data)?, - data: data.clone() + header: BlockHeader::make(p_header.height + 1, &p_header.hash, nonce, data)?, + data: data.clone(), }) } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] struct BlockHeader { version: String, height: i32, // 블록의 높이. 블록 체인에 연결된 블록의 수 @@ -43,9 +48,17 @@ impl BlockHeader { const VERSION: &str = "1.0.0"; const DIFFICULTY: i32 = 0; - fn make(height: i32, previous_hash: &String, nonce: i32, data: &Vec) -> Result { + fn make( + height: i32, + previous_hash: &String, + nonce: i32, + data: &Vec, + ) -> Result { let version = Self::VERSION.to_string(); - let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); + let timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs(); let difficulty = Self::DIFFICULTY; let merkle_root = Self::make_merkle_root(data).ok_or("Merkle Tree Parsing Failed")?; @@ -57,7 +70,7 @@ impl BlockHeader { previous_hash: Some(previous_hash.clone()), merkle_root, nonce, - difficulty + difficulty, }) } @@ -70,8 +83,36 @@ impl BlockHeader { merkle_tree.root_hex() } - fn make_hash(timestamp: u64, merkle_root: &String, previous_hash: &String, nonce: i32) -> String { - let target = format!("{}{}{}{}", timestamp.to_string(), merkle_root, previous_hash, nonce.to_string()); + fn make_hash( + timestamp: u64, + merkle_root: &String, + previous_hash: &String, + nonce: i32, + ) -> String { + let target = format!( + "{}{}{}{}", + timestamp.to_string(), + merkle_root, + previous_hash, + nonce.to_string() + ); digest(target) } -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_new_genesis() { + let actual = Block::new_genesis().unwrap(); + + let data = vec![Block::GENESIS_DATA.to_string()]; + let expected = Block { + header: BlockHeader::make(0, &["0"; 64].join(""), 0, &data).unwrap(), + data, + }; + assert_eq!(actual, expected); + } +} diff --git a/jaeseung-blockchain/src/main.rs b/jaeseung-blockchain/src/main.rs index a535ba9..199a2d8 100644 --- a/jaeseung-blockchain/src/main.rs +++ b/jaeseung-blockchain/src/main.rs @@ -1,10 +1,9 @@ mod block; use block::Block; -use rs_merkle::{algorithms::Sha256, MerkleTree}; fn main() { - let block = Block::new_genesis(0, 0, &vec![String::from("asdasd")]); + let block = Block::new_genesis(); print!("{:?}", block); } From 8c3e989caa8abafca028b50990654352355737ce Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Sun, 26 Jan 2025 11:19:24 +0900 Subject: [PATCH 3/9] =?UTF-8?q?Chain=20=EA=B5=AC=EC=A1=B0=EC=B2=B4=20?= =?UTF-8?q?=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeseung-blockchain/src/block/block.rs | 4 +- jaeseung-blockchain/src/chain/chain.rs | 83 ++++++++++++++++++++++++++ jaeseung-blockchain/src/chain/mod.rs | 2 + jaeseung-blockchain/src/main.rs | 1 + 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 jaeseung-blockchain/src/chain/chain.rs create mode 100644 jaeseung-blockchain/src/chain/mod.rs diff --git a/jaeseung-blockchain/src/block/block.rs b/jaeseung-blockchain/src/block/block.rs index 6e16db0..27f5869 100644 --- a/jaeseung-blockchain/src/block/block.rs +++ b/jaeseung-blockchain/src/block/block.rs @@ -4,7 +4,7 @@ use sha256::digest; use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree}; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] // TODO : Copy 와 Clone 의 차이점? pub struct Block { header: BlockHeader, data: Vec, @@ -32,7 +32,7 @@ impl Block { } } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] struct BlockHeader { version: String, height: i32, // 블록의 높이. 블록 체인에 연결된 블록의 수 diff --git a/jaeseung-blockchain/src/chain/chain.rs b/jaeseung-blockchain/src/chain/chain.rs new file mode 100644 index 0000000..fcb27f2 --- /dev/null +++ b/jaeseung-blockchain/src/chain/chain.rs @@ -0,0 +1,83 @@ +use crate::block::Block; + +#[derive(Debug)] +pub struct Chain { + block_chain: Vec, +} + +impl Chain { + pub fn new() -> Result { + Block::new_genesis().map(|b| Chain { + block_chain: vec![b], + }) + } + + pub fn get_length(&self) -> usize { + self.block_chain.len() + } + + pub fn get_lastest_block(&self) -> Option<&Block> { + self.block_chain.last() + } + + pub fn add_block(&mut self, data: &Vec) -> Result<&Chain, String> { + let previous_block = self + .get_lastest_block() + .ok_or("previous_block not exist.")?; + let new_block = Block::new(previous_block, 0, data)?; + + self.block_chain.push(new_block); // TODO : 안전할까? + Ok(self) + } +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn create_chain() { + let chain = Chain::new().unwrap(); + + assert_eq!(chain.block_chain.len(), 1); + } + + #[test] + fn get_length() { + let chain = Chain::new().unwrap(); + + assert_eq!(chain.get_length(), 1); + } + + #[test] + fn get_lastest_block() { + let first_block = Block::new_genesis().unwrap(); + let second_block = Block::new(&first_block, 0, &vec!["data".to_string()]).unwrap(); + + let chain = Chain { + block_chain: vec![first_block, second_block.clone()], + }; + + assert_eq!(*chain.get_lastest_block().unwrap(), second_block); + } + + #[test] + fn add_block_to_empty_chain() { + let mut empty_chain = Chain { + block_chain: Vec::new(), + }; + let result = empty_chain.add_block(&vec!["data".to_string()]); + + assert_eq!(result.is_err(), true); + assert_eq!(empty_chain.block_chain.len(), 0); + } + + #[test] + fn add_block() { + let mut chain = Chain::new().unwrap(); + let added_chain = chain.add_block(&vec!["data".to_string()]).unwrap(); + + assert_eq!(added_chain.block_chain.len(), 2); + } +} diff --git a/jaeseung-blockchain/src/chain/mod.rs b/jaeseung-blockchain/src/chain/mod.rs new file mode 100644 index 0000000..0b86d70 --- /dev/null +++ b/jaeseung-blockchain/src/chain/mod.rs @@ -0,0 +1,2 @@ +pub mod chain; +pub use chain::*; diff --git a/jaeseung-blockchain/src/main.rs b/jaeseung-blockchain/src/main.rs index 199a2d8..f943de5 100644 --- a/jaeseung-blockchain/src/main.rs +++ b/jaeseung-blockchain/src/main.rs @@ -1,4 +1,5 @@ mod block; +mod chain; use block::Block; From 7d86f9d0499c655c840d5f4ebcddb6974a79b09b Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Sun, 26 Jan 2025 17:26:53 +0900 Subject: [PATCH 4/9] =?UTF-8?q?difficulty=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeseung-blockchain/src/block/block.rs | 107 +++++++++++++++++++++---- jaeseung-blockchain/src/constants.rs | 8 ++ jaeseung-blockchain/src/main.rs | 1 + 3 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 jaeseung-blockchain/src/constants.rs diff --git a/jaeseung-blockchain/src/block/block.rs b/jaeseung-blockchain/src/block/block.rs index 27f5869..6159730 100644 --- a/jaeseung-blockchain/src/block/block.rs +++ b/jaeseung-blockchain/src/block/block.rs @@ -4,6 +4,10 @@ use sha256::digest; use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree}; +use crate::constants::{ + BLOCK_GENERATION_INTERVAL_MINUTE, BLOCK_GENERATION_MILLIS, DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT, +}; + #[derive(Debug, PartialEq, Clone)] // TODO : Copy 와 Clone 의 차이점? pub struct Block { header: BlockHeader, @@ -17,19 +21,82 @@ impl Block { let data = vec![Self::GENESIS_DATA.to_string()]; Ok(Block { - header: BlockHeader::make(0, &["0"; 64].join(""), 0, &data)?, + header: BlockHeader::make( + 0, + Self::unsafe_get_timestamp(), + &["0"; 64].join(""), + 0, + 0, + &data, + )?, data: data, }) } - pub fn new(previous_block: &Block, nonce: i32, data: &Vec) -> Result { + pub fn new( + previous_block: &Block, + nonce: i32, + data: &Vec, + adjustment_block: &Block, + ) -> Result { let p_header = &previous_block.header; + let new_height = p_header.height + 1; + let new_timestamp = Self::unsafe_get_timestamp(); + + let difficulty = + Self::get_difficulty(new_height, new_timestamp, previous_block, adjustment_block); + Ok(Block { - header: BlockHeader::make(p_header.height + 1, &p_header.hash, nonce, data)?, - data: data.clone(), + header: BlockHeader::make( + p_header.height + 1, + new_timestamp, + &p_header.hash, + nonce, + difficulty, + data, + )?, + data: data.clone(), // TODO : clone 이 맞을까? }) } + + fn get_difficulty( + new_height: i32, + new_timestamp: u64, + previous_block: &Block, + adjustment_block: &Block, + ) -> i32 { + match new_height { + 0..=9 => 0, + 10..=19 => 1, + h if h % 10 != i32::from(DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT) => { + previous_block.header.difficulty + } + _ => { + let time_taken = new_timestamp - adjustment_block.header.timestamp; + let time_expected = u64::from( + DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT + * BLOCK_GENERATION_INTERVAL_MINUTE + * BLOCK_GENERATION_MILLIS, + ); + + if time_taken < time_expected / 2 { + adjustment_block.header.difficulty + 1 + } else if time_taken > time_expected * 2 { + adjustment_block.header.difficulty - 1 + } else { + adjustment_block.header.difficulty + } + } + } + } + + fn unsafe_get_timestamp() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() + } } #[derive(Debug, PartialEq, Clone)] @@ -46,27 +113,30 @@ struct BlockHeader { impl BlockHeader { const VERSION: &str = "1.0.0"; - const DIFFICULTY: i32 = 0; fn make( height: i32, + timestamp: u64, previous_hash: &String, nonce: i32, + difficulty: i32, data: &Vec, ) -> Result { - let version = Self::VERSION.to_string(); - let timestamp = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs(); - let difficulty = Self::DIFFICULTY; let merkle_root = Self::make_merkle_root(data).ok_or("Merkle Tree Parsing Failed")?; Ok(BlockHeader { - version, + version: Self::VERSION.to_string(), height, timestamp, - hash: Self::make_hash(timestamp, &merkle_root, previous_hash, nonce), + hash: Self::make_block_hash( + // TODO : 여기서 바로 마이닝을 하자. + height, + timestamp, + &merkle_root, + previous_hash, + nonce, + difficulty, + ), previous_hash: Some(previous_hash.clone()), merkle_root, nonce, @@ -83,18 +153,23 @@ impl BlockHeader { merkle_tree.root_hex() } - fn make_hash( + fn make_block_hash( + height: i32, timestamp: u64, merkle_root: &String, previous_hash: &String, nonce: i32, + difficulty: i32, ) -> String { let target = format!( - "{}{}{}{}", + "{}{}{}{}{}{}{}", + Self::VERSION.to_string(), + height.to_string(), timestamp.to_string(), merkle_root, previous_hash, - nonce.to_string() + nonce.to_string(), + difficulty.to_string() ); digest(target) } diff --git a/jaeseung-blockchain/src/constants.rs b/jaeseung-blockchain/src/constants.rs new file mode 100644 index 0000000..bdf03db --- /dev/null +++ b/jaeseung-blockchain/src/constants.rs @@ -0,0 +1,8 @@ +// 블록 1 묶음의 블록 개수 +pub const DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT: u16 = 10; + +// 블록 1 묶음이 생성 되기까지의 예상 시간(분) +pub const BLOCK_GENERATION_INTERVAL_MINUTE: u16 = 10; + +// 블록 1 개가 생성 되기까지의 예상 시간(밀리초) +pub const BLOCK_GENERATION_MILLIS: u16 = 60000; diff --git a/jaeseung-blockchain/src/main.rs b/jaeseung-blockchain/src/main.rs index f943de5..fe78f4e 100644 --- a/jaeseung-blockchain/src/main.rs +++ b/jaeseung-blockchain/src/main.rs @@ -1,5 +1,6 @@ mod block; mod chain; +mod constants; use block::Block; From b40a95134fdd79eb075bdbb15e7fb4ef5ecba9b7 Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Mon, 27 Jan 2025 14:43:11 +0900 Subject: [PATCH 5/9] =?UTF-8?q?Chain=20=ED=95=A8=EC=88=98=20=EC=99=84?= =?UTF-8?q?=EC=84=B1.=20difficulty=20=EC=A6=9D=EA=B0=80=ED=95=98=EC=A7=80?= =?UTF-8?q?=20=EC=95=8A=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=EC=9E=88=EC=9D=8C?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeseung-blockchain/Cargo.lock | 10 +++++ jaeseung-blockchain/Cargo.toml | 3 +- jaeseung-blockchain/src/block/block.rs | 57 +++++++++++++------------- jaeseung-blockchain/src/chain/chain.rs | 21 ++++++++-- jaeseung-blockchain/src/constants.rs | 6 +-- jaeseung-blockchain/src/main.rs | 9 +++- 6 files changed, 67 insertions(+), 39 deletions(-) diff --git a/jaeseung-blockchain/Cargo.lock b/jaeseung-blockchain/Cargo.lock index 06b9650..4fb6a79 100644 --- a/jaeseung-blockchain/Cargo.lock +++ b/jaeseung-blockchain/Cargo.lock @@ -121,6 +121,7 @@ version = "0.1.0" dependencies = [ "rs_merkle", "sha256", + "to-binary", ] [[package]] @@ -227,6 +228,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "to-binary" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4424552bc848fd1afbcd81f0e8a54b7401b90fd81bb418655ad6dc6d0823bbe3" +dependencies = [ + "hex", +] + [[package]] name = "tokio" version = "1.42.0" diff --git a/jaeseung-blockchain/Cargo.toml b/jaeseung-blockchain/Cargo.toml index aae1b38..32cc45e 100644 --- a/jaeseung-blockchain/Cargo.toml +++ b/jaeseung-blockchain/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" [dependencies] rs_merkle = "1.4" -sha256 = "1.5.0" \ No newline at end of file +sha256 = "1.5.0" +to-binary = "0.4.0" \ No newline at end of file diff --git a/jaeseung-blockchain/src/block/block.rs b/jaeseung-blockchain/src/block/block.rs index 6159730..7c41aed 100644 --- a/jaeseung-blockchain/src/block/block.rs +++ b/jaeseung-blockchain/src/block/block.rs @@ -4,6 +4,8 @@ use sha256::digest; use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree}; +use to_binary::BinaryString; + use crate::constants::{ BLOCK_GENERATION_INTERVAL_MINUTE, BLOCK_GENERATION_MILLIS, DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT, }; @@ -26,7 +28,6 @@ impl Block { Self::unsafe_get_timestamp(), &["0"; 64].join(""), 0, - 0, &data, )?, data: data, @@ -35,7 +36,6 @@ impl Block { pub fn new( previous_block: &Block, - nonce: i32, data: &Vec, adjustment_block: &Block, ) -> Result { @@ -52,7 +52,6 @@ impl Block { p_header.height + 1, new_timestamp, &p_header.hash, - nonce, difficulty, data, )?, @@ -69,7 +68,7 @@ impl Block { match new_height { 0..=9 => 0, 10..=19 => 1, - h if h % 10 != i32::from(DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT) => { + h if h % 10 != DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT as i32 => { previous_block.header.difficulty } _ => { @@ -118,25 +117,42 @@ impl BlockHeader { height: i32, timestamp: u64, previous_hash: &String, - nonce: i32, difficulty: i32, data: &Vec, ) -> Result { + let prefix_zero = "0".repeat(difficulty as usize); + let merkle_root = Self::make_merkle_root(data).ok_or("Merkle Tree Parsing Failed")?; - Ok(BlockHeader { - version: Self::VERSION.to_string(), - height, - timestamp, - hash: Self::make_block_hash( - // TODO : 여기서 바로 마이닝을 하자. + let mut nonce = 0; + + // TODO : closure 와 꼬리 재귀를 써서 할 순 없을까? + let (nonce, hash) = loop { + nonce += 1; + + let hash = Self::make_block_hash( height, timestamp, &merkle_root, previous_hash, nonce, difficulty, - ), + ); + + let binary = BinaryString::from_hex(&hash) + .map_err(|_| "hex to binary failed".to_string())? + .to_string(); + + if binary.as_str().starts_with(prefix_zero.as_str()) { + break (nonce, hash); + } + }; + + Ok(BlockHeader { + version: Self::VERSION.to_string(), + height, + timestamp, + hash, previous_hash: Some(previous_hash.clone()), merkle_root, nonce, @@ -174,20 +190,3 @@ impl BlockHeader { digest(target) } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_new_genesis() { - let actual = Block::new_genesis().unwrap(); - - let data = vec![Block::GENESIS_DATA.to_string()]; - let expected = Block { - header: BlockHeader::make(0, &["0"; 64].join(""), 0, &data).unwrap(), - data, - }; - assert_eq!(actual, expected); - } -} diff --git a/jaeseung-blockchain/src/chain/chain.rs b/jaeseung-blockchain/src/chain/chain.rs index fcb27f2..aed187c 100644 --- a/jaeseung-blockchain/src/chain/chain.rs +++ b/jaeseung-blockchain/src/chain/chain.rs @@ -1,4 +1,4 @@ -use crate::block::Block; +use crate::{block::Block, constants::DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT}; #[derive(Debug)] pub struct Chain { @@ -12,8 +12,8 @@ impl Chain { }) } - pub fn get_length(&self) -> usize { - self.block_chain.len() + pub fn get_length(&self) -> u32 { + self.block_chain.len() as u32 } pub fn get_lastest_block(&self) -> Option<&Block> { @@ -24,11 +24,24 @@ impl Chain { let previous_block = self .get_lastest_block() .ok_or("previous_block not exist.")?; - let new_block = Block::new(previous_block, 0, data)?; + let adjustment_block = self.get_adjustment_block()?; + + let new_block = Block::new(previous_block, data, &adjustment_block)?; self.block_chain.push(new_block); // TODO : 안전할까? Ok(self) } + + fn get_adjustment_block(&self) -> Result { + let current_length = self.get_length(); + + match current_length { + ..DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT => Block::new_genesis(), + _ => Ok(self.block_chain + [(current_length - DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT) as usize] + .clone()), + } + } } #[cfg(test)] diff --git a/jaeseung-blockchain/src/constants.rs b/jaeseung-blockchain/src/constants.rs index bdf03db..44fc3bf 100644 --- a/jaeseung-blockchain/src/constants.rs +++ b/jaeseung-blockchain/src/constants.rs @@ -1,8 +1,8 @@ // 블록 1 묶음의 블록 개수 -pub const DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT: u16 = 10; +pub const DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT: u32 = 10; // 블록 1 묶음이 생성 되기까지의 예상 시간(분) -pub const BLOCK_GENERATION_INTERVAL_MINUTE: u16 = 10; +pub const BLOCK_GENERATION_INTERVAL_MINUTE: u32 = 10; // 블록 1 개가 생성 되기까지의 예상 시간(밀리초) -pub const BLOCK_GENERATION_MILLIS: u16 = 60000; +pub const BLOCK_GENERATION_MILLIS: u32 = 60000; diff --git a/jaeseung-blockchain/src/main.rs b/jaeseung-blockchain/src/main.rs index fe78f4e..8687258 100644 --- a/jaeseung-blockchain/src/main.rs +++ b/jaeseung-blockchain/src/main.rs @@ -3,9 +3,14 @@ mod chain; mod constants; use block::Block; +use chain::Chain; fn main() { - let block = Block::new_genesis(); + let mut chain = Chain::new().unwrap(); - print!("{:?}", block); + for x in 1..30000 { + chain.add_block(&vec![format!("data-{x}").to_string()]); + } + + print!("{:#?}", chain); } From f6ff419ab80ce81490b9b7bc368790096475763b Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Wed, 29 Jan 2025 12:46:54 +0900 Subject: [PATCH 6/9] =?UTF-8?q?difficulty=20=EC=95=88=20=EC=98=AC=EB=9D=BC?= =?UTF-8?q?=EA=B0=80=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeseung-blockchain/src/block/block.rs | 2 +- jaeseung-blockchain/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jaeseung-blockchain/src/block/block.rs b/jaeseung-blockchain/src/block/block.rs index 7c41aed..b703cba 100644 --- a/jaeseung-blockchain/src/block/block.rs +++ b/jaeseung-blockchain/src/block/block.rs @@ -68,7 +68,7 @@ impl Block { match new_height { 0..=9 => 0, 10..=19 => 1, - h if h % 10 != DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT as i32 => { + h if h % DIFFICULTY_ADJUSTMENT_INTERVAL_COUNT as i32 != 0 => { previous_block.header.difficulty } _ => { diff --git a/jaeseung-blockchain/src/main.rs b/jaeseung-blockchain/src/main.rs index 8687258..43e9ad1 100644 --- a/jaeseung-blockchain/src/main.rs +++ b/jaeseung-blockchain/src/main.rs @@ -8,7 +8,7 @@ use chain::Chain; fn main() { let mut chain = Chain::new().unwrap(); - for x in 1..30000 { + for x in 1..10 { chain.add_block(&vec![format!("data-{x}").to_string()]); } From 587238a0fcc1b7505d94a0efb6cb837d63e35ff2 Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Sun, 16 Feb 2025 20:02:31 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=EC=86=8C=EC=BC=93,=20http=20=EC=84=9C?= =?UTF-8?q?=EB=B2=84=20=EC=84=B8=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeseung-blockchain/Cargo.lock | 1126 ++++++++++++++++- jaeseung-blockchain/Cargo.toml | 7 +- jaeseung-blockchain/src/constants.rs | 3 + jaeseung-blockchain/src/main.rs | 45 +- jaeseung-blockchain/src/server/http_server.rs | 33 + jaeseung-blockchain/src/server/mod.rs | 4 + jaeseung-blockchain/src/server/p2p_server.rs | 57 + 7 files changed, 1210 insertions(+), 65 deletions(-) create mode 100644 jaeseung-blockchain/src/server/http_server.rs create mode 100644 jaeseung-blockchain/src/server/mod.rs create mode 100644 jaeseung-blockchain/src/server/p2p_server.rs diff --git a/jaeseung-blockchain/Cargo.lock b/jaeseung-blockchain/Cargo.lock index 4fb6a79..42914a0 100644 --- a/jaeseung-blockchain/Cargo.lock +++ b/jaeseung-blockchain/Cargo.lock @@ -17,6 +17,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "async-trait" version = "0.1.83" @@ -28,6 +37,12 @@ dependencies = [ "syn", ] +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "backtrace" version = "0.3.74" @@ -43,6 +58,33 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -52,6 +94,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.9.0" @@ -64,6 +112,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "cpufeatures" version = "0.2.16" @@ -77,121 +131,763 @@ dependencies = [ name = "crypto-common" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "derive_more" +version = "0.99.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3da29a38df43d6f156149c9b43ded5e018ddff2a855cf2cfd62e8cd7d079c69f" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "crypto-common", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "headers" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06683b93020a07e3dbcf5f8c0f6d40080d725bea7936fc01ad345c01b97dc270" +dependencies = [ + "base64 0.21.7", + "bytes", + "headers-core", + "http", + "httpdate", + "mime", + "sha1", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2d708df4e7140240a16cd6ab0ab65c972d7433ab77819ea693fde9c43811e2a" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "0.14.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "icu_collections" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locid" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locid_transform" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_locid_transform_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locid_transform_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" + +[[package]] +name = "icu_normalizer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "utf16_iter", + "utf8_iter", + "write16", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" + +[[package]] +name = "icu_properties" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93d6020766cfc6302c15dbbc9c8778c37e62c14427cb7f6e601d849e092aeef5" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locid_transform", + "icu_properties_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" + +[[package]] +name = "icu_provider" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_provider_macros", + "stable_deref_trait", + "tinystr", + "writeable", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_provider_macros" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "idna" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "686f825264d630750a544639377bae737628043f20d38bbc029e8f29ea968a7e" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daca1df1c957320b2cf139ac61e7bd64fed304c5040df000a745aa1de3b4ef71" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + +[[package]] +name = "jaeseung-blockchain" +version = "0.1.0" +dependencies = [ + "futures", + "hyper", + "routerify", + "routerify-websocket", + "rs_merkle", + "sha256", + "to-binary", + "tokio", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.168" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" + +[[package]] +name = "litemap" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" + +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project-lite" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" dependencies = [ - "generic-array", - "typenum", + "bitflags", ] [[package]] -name = "digest" -version = "0.10.7" +name = "regex" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ - "block-buffer", - "crypto-common", + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", ] [[package]] -name = "generic-array" -version = "0.14.7" +name = "regex-automata" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" dependencies = [ - "typenum", - "version_check", + "aho-corasick", + "memchr", + "regex-syntax", ] [[package]] -name = "gimli" -version = "0.31.1" +name = "regex-syntax" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] -name = "hex" -version = "0.4.3" +name = "routerify" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +checksum = "496c1d3718081c45ba9c31fbfc07417900aa96f4070ff90dc29961836b7a9945" +dependencies = [ + "http", + "hyper", + "lazy_static", + "percent-encoding", + "regex", +] [[package]] -name = "jaeseung-blockchain" -version = "0.1.0" +name = "routerify-websocket" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec67fdcefd167c87b384c0c1b08aaf3483793d205805ebc9a966cc79d3ac3b93" dependencies = [ - "rs_merkle", - "sha256", - "to-binary", + "derive_more", + "futures", + "headers", + "hyper", + "log", + "routerify", + "tokio", + "tokio-tungstenite", ] [[package]] -name = "libc" -version = "0.2.168" +name = "rs_merkle" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" +checksum = "3b241d2e59b74ef9e98d94c78c47623d04c8392abaf82014dfd372a16041128f" +dependencies = [ + "sha2", +] [[package]] -name = "memchr" -version = "2.7.4" +name = "rustc-demangle" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] -name = "miniz_oxide" -version = "0.8.0" +name = "rustc_version" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" dependencies = [ - "adler2", + "semver", ] [[package]] -name = "object" -version = "0.36.5" +name = "scopeguard" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" -dependencies = [ - "memchr", -] +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] -name = "pin-project-lite" -version = "0.2.15" +name = "semver" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" +checksum = "f79dfe2d285b0488816f30e700a7438c5a73d816b5b7d3ac72fbc48b0d185e03" [[package]] -name = "proc-macro2" -version = "1.0.92" +name = "serde" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" dependencies = [ - "unicode-ident", + "serde_derive", ] [[package]] -name = "quote" -version = "1.0.37" +name = "serde_derive" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" dependencies = [ "proc-macro2", + "quote", + "syn", ] [[package]] -name = "rs_merkle" -version = "1.4.2" +name = "sha-1" +version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b241d2e59b74ef9e98d94c78c47623d04c8392abaf82014dfd372a16041128f" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ - "sha2", + "block-buffer 0.9.0", + "cfg-if", + "cpufeatures", + "digest 0.9.0", + "opaque-debug", ] [[package]] -name = "rustc-demangle" -version = "0.1.24" +name = "sha1" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest 0.10.7", +] [[package]] name = "sha2" @@ -201,7 +897,7 @@ checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ "cfg-if", "cpufeatures", - "digest", + "digest 0.10.7", ] [[package]] @@ -217,6 +913,46 @@ dependencies = [ "tokio", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" + +[[package]] +name = "socket2" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "syn" version = "2.0.90" @@ -228,6 +964,47 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinystr" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "to-binary" version = "0.4.0" @@ -245,7 +1022,87 @@ checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" dependencies = [ "backtrace", "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e80b39df6afcc12cdf752398ade96a6b9e99c903dfdc36e53ad10b9c366bca72" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite", +] + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "tungstenite" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ad3713a14ae247f22a728a0456a545df14acf3867f905adff84be99e23b3ad1" +dependencies = [ + "base64 0.13.1", + "byteorder", + "bytes", + "http", + "httparse", + "log", + "rand", + "sha-1", + "thiserror", + "url", + "utf-8", ] [[package]] @@ -260,12 +1117,65 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +[[package]] +name = "url" +version = "2.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "utf16_iter" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -329,3 +1239,103 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "write16" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" + +[[package]] +name = "writeable" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" + +[[package]] +name = "yoke" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerofrom" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerovec" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa2b893d79df23bfb12d5461018d408ea19dfafe76c2c7ef6d4eba614f8ff079" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/jaeseung-blockchain/Cargo.toml b/jaeseung-blockchain/Cargo.toml index 32cc45e..ae1d5ba 100644 --- a/jaeseung-blockchain/Cargo.toml +++ b/jaeseung-blockchain/Cargo.toml @@ -6,4 +6,9 @@ edition = "2021" [dependencies] rs_merkle = "1.4" sha256 = "1.5.0" -to-binary = "0.4.0" \ No newline at end of file +to-binary = "0.4.0" +routerify-websocket = "3.0.0" +routerify = "3" +hyper = "0.14" +tokio = { version = "1", features = ["full"] } +futures = "0.3.31" \ No newline at end of file diff --git a/jaeseung-blockchain/src/constants.rs b/jaeseung-blockchain/src/constants.rs index 44fc3bf..fa07b18 100644 --- a/jaeseung-blockchain/src/constants.rs +++ b/jaeseung-blockchain/src/constants.rs @@ -6,3 +6,6 @@ pub const BLOCK_GENERATION_INTERVAL_MINUTE: u32 = 10; // 블록 1 개가 생성 되기까지의 예상 시간(밀리초) pub const BLOCK_GENERATION_MILLIS: u32 = 60000; + +pub const HTTP_PORT: u16 = 80; +pub const SOCKET_PORT: u16 = 3000; diff --git a/jaeseung-blockchain/src/main.rs b/jaeseung-blockchain/src/main.rs index 43e9ad1..58ccf16 100644 --- a/jaeseung-blockchain/src/main.rs +++ b/jaeseung-blockchain/src/main.rs @@ -1,16 +1,49 @@ mod block; mod chain; mod constants; +mod server; + +use std::{ + convert::Infallible, + net::{SocketAddr, TcpListener, TcpStream}, + result, +}; use block::Block; use chain::Chain; +use constants::{HTTP_PORT, SOCKET_PORT}; +use futures::{SinkExt, StreamExt}; +use hyper::{Body, Response, Server}; +use routerify::{Router, RouterService}; +use routerify_websocket::{upgrade_ws, Message, WebSocket}; +use server::{HttpServer, P2PServer}; + +// fn main() -> std::io::Result<()> { +// let mut chain = Chain::new().unwrap(); + +// let listener = TcpListener::bind("127.0.0.1:80")?; + +// for stream in listener.incoming() { +// handle_client(stream?); +// } + +// Ok(()) + +// // for x in 1..10 { +// // chain.add_block(&vec![format!("data-{x}").to_string()]); +// // } -fn main() { - let mut chain = Chain::new().unwrap(); +// // print!("{:#?}", chain); +// } - for x in 1..10 { - chain.add_block(&vec![format!("data-{x}").to_string()]); - } +// tokio : async 런타임 라이브러리 +#[tokio::main] +async fn main() { + let http_server = HttpServer::new(); + let p2p_server = P2PServer::new(); - print!("{:#?}", chain); + let ((), ()) = futures::join!( + http_server.listen(HTTP_PORT), + p2p_server.listen(SOCKET_PORT) + ); } diff --git a/jaeseung-blockchain/src/server/http_server.rs b/jaeseung-blockchain/src/server/http_server.rs new file mode 100644 index 0000000..ad9c921 --- /dev/null +++ b/jaeseung-blockchain/src/server/http_server.rs @@ -0,0 +1,33 @@ +use std::{convert::Infallible, net::SocketAddr}; + +use futures::{SinkExt, StreamExt}; +use hyper::{Body, Response, Server}; +use routerify::{Router, RouterService}; +use routerify_websocket::{upgrade_ws, Message, WebSocket}; + +pub struct HttpServer {} + +impl HttpServer { + pub fn new() -> HttpServer { + HttpServer {} + } + + pub async fn listen(&self, port: u16) { + let router: Router = Router::builder() + .get("/", |_req| async move { + Ok(Response::new("I also serve http requests".into())) + }) + .build() + .unwrap(); + + let service = RouterService::new(router).unwrap(); + + let addr = SocketAddr::from(([127, 0, 0, 1], port)); + + let server = Server::bind(&addr).serve(service); + + if let Err(err) = server.await { + eprintln!("Http Server error: {}", err); + } + } +} diff --git a/jaeseung-blockchain/src/server/mod.rs b/jaeseung-blockchain/src/server/mod.rs new file mode 100644 index 0000000..3b41e78 --- /dev/null +++ b/jaeseung-blockchain/src/server/mod.rs @@ -0,0 +1,4 @@ +pub mod http_server; +pub mod p2p_server; +pub use http_server::*; +pub use p2p_server::*; diff --git a/jaeseung-blockchain/src/server/p2p_server.rs b/jaeseung-blockchain/src/server/p2p_server.rs new file mode 100644 index 0000000..6cb31ae --- /dev/null +++ b/jaeseung-blockchain/src/server/p2p_server.rs @@ -0,0 +1,57 @@ +use std::{convert::Infallible, net::SocketAddr}; + +use futures::{SinkExt, StreamExt}; +use hyper::{Body, Server}; +use routerify::{Router, RouterService}; +use routerify_websocket::{upgrade_ws, Message, WebSocket}; + +pub struct P2PServer { + sockets: Vec, +} + +impl P2PServer { + pub fn new() -> P2PServer { + P2PServer { sockets: vec![] } + } + + pub async fn listen(&self, port: u16) { + let router: Router = Router::builder() + .any_method("/", upgrade_ws(Self::ws_handler)) + .build() + .unwrap(); + + let service = RouterService::new(router).unwrap(); + + let addr = SocketAddr::from(([127, 0, 0, 1], port)); + + let server = Server::bind(&addr).serve(service); + + if let Err(err) = server.await { + eprintln!("Socket Server error: {}", err); + } + } + + async fn ws_handler(ws: WebSocket) { + println!("New websocket connection: {}", ws.remote_addr()); + + // The `WebSocket` implements the `Sink` and `Stream` traits + // to read and write messages. + let (mut tx, mut rx) = ws.split(); + + // Read messages. + while let Some(msg) = rx.next().await { + let msg = msg.unwrap(); + + // Check message type and take appropriate actions. + if msg.is_text() { + println!("{}", msg.into_text().unwrap()); + } else if msg.is_binary() { + println!("{:?}", msg.into_bytes()); + } + + // Send a text message. + let send_msg = Message::text("Hello world"); + tx.send(send_msg).await.unwrap(); + } + } +} From 13a04300261b0c21c27d1e0631cb178c67af56bf Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Sun, 23 Feb 2025 16:15:36 +0900 Subject: [PATCH 8/9] =?UTF-8?q?connect=5Fto=5Fpeer=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeseung-blockchain/src/main.rs | 40 ++++++++++++++++++++ jaeseung-blockchain/src/server/p2p_server.rs | 38 +++++++++++++------ 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/jaeseung-blockchain/src/main.rs b/jaeseung-blockchain/src/main.rs index 58ccf16..eed13dc 100644 --- a/jaeseung-blockchain/src/main.rs +++ b/jaeseung-blockchain/src/main.rs @@ -36,9 +36,47 @@ use server::{HttpServer, P2PServer}; // // print!("{:#?}", chain); // } +async fn ws_handler(ws: WebSocket) { + println!("New websocket connection: {}", ws.remote_addr()); + connect_socket(ws); +} + +async fn connect_socket(ws: WebSocket) { + // 참조를 sockets 에 저 + + // The `WebSocket` implements the `Sink` and `Stream` traits + // to read and write messages. + let (mut tx, mut rx) = ws.split(); + + // Read messages. + while let Some(msg) = rx.next().await { + let msg = msg.unwrap(); + + // Check message type and take appropriate actions. + if msg.is_text() { + println!("{}", msg.into_text().unwrap()); + } else if msg.is_binary() { + println!("{:?}", msg.into_bytes()); + } + + // Send a text message. + let send_msg = Message::text("msg from server"); + tx.send(send_msg).await.unwrap(); + } +} + // tokio : async 런타임 라이브러리 #[tokio::main] async fn main() { + tokio::spawn(async move { + match hyper::upgrade::on(req).await { + Ok(upgraded) => { + ws_handler(WebSocket::from_raw_socket(upgraded, remote_addr, config).await).await; + } + Err(err) => log::error!("{}", crate::WebsocketError::Upgrade(err.into())), + } + }); + let http_server = HttpServer::new(); let p2p_server = P2PServer::new(); @@ -46,4 +84,6 @@ async fn main() { http_server.listen(HTTP_PORT), p2p_server.listen(SOCKET_PORT) ); + + println!("{:#?}", P2PServer::sockets); } diff --git a/jaeseung-blockchain/src/server/p2p_server.rs b/jaeseung-blockchain/src/server/p2p_server.rs index 6cb31ae..b5a2158 100644 --- a/jaeseung-blockchain/src/server/p2p_server.rs +++ b/jaeseung-blockchain/src/server/p2p_server.rs @@ -1,22 +1,29 @@ -use std::{convert::Infallible, net::SocketAddr}; +use std::{convert::Infallible, net::SocketAddr, sync::OnceLock}; use futures::{SinkExt, StreamExt}; -use hyper::{Body, Server}; +use hyper::{upgrade, Body, Request, Response, Server}; use routerify::{Router, RouterService}; -use routerify_websocket::{upgrade_ws, Message, WebSocket}; +use routerify_websocket::{upgrade_ws, upgrade_ws_with_config, Message, WebSocket}; -pub struct P2PServer { - sockets: Vec, -} +pub struct P2PServer {} impl P2PServer { - pub fn new() -> P2PServer { - P2PServer { sockets: vec![] } + pub const sockets: Vec<&WebSocket> = vec![]; + + // P2PServer 는 하나의 인스턴스만 존재한다. + pub fn new() -> &'static P2PServer { + static INSTANCE: OnceLock = OnceLock::new(); + INSTANCE.get_or_init(|| P2PServer {}) } - pub async fn listen(&self, port: u16) { + pub async fn listen(&'static self, port: u16) { let router: Router = Router::builder() - .any_method("/", upgrade_ws(Self::ws_handler)) + .any_method("/", upgrade_ws(|ws| self.connect_socket(ws))) + // upgrade_ws 의 handler 는 static 한 무언가를 받는 것 같다. + .post("/addToPeer", move |req| async move { + let response = self.connect_to_peer(req); + Ok(response) + }) .build() .unwrap(); @@ -31,9 +38,16 @@ impl P2PServer { } } - async fn ws_handler(ws: WebSocket) { + fn connect_to_peer(&'static self, req: Request) { + return Response::new("I also serve http requests".into()); + } + + async fn connect_socket(&'static self, ws: WebSocket) { println!("New websocket connection: {}", ws.remote_addr()); + // 참조를 sockets 에 저장. + Self::sockets.push(&ws); + // The `WebSocket` implements the `Sink` and `Stream` traits // to read and write messages. let (mut tx, mut rx) = ws.split(); @@ -50,7 +64,7 @@ impl P2PServer { } // Send a text message. - let send_msg = Message::text("Hello world"); + let send_msg = Message::text("msg from server"); tx.send(send_msg).await.unwrap(); } } From 9e8d34f4265bd0eb4cd1d8874f905cf51c1991c3 Mon Sep 17 00:00:00 2001 From: leejaeseung Date: Sat, 12 Apr 2025 09:31:59 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=EC=BB=B4=ED=8C=8C=EC=9D=BC=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeseung-blockchain/src/server/p2p_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jaeseung-blockchain/src/server/p2p_server.rs b/jaeseung-blockchain/src/server/p2p_server.rs index b5a2158..145f507 100644 --- a/jaeseung-blockchain/src/server/p2p_server.rs +++ b/jaeseung-blockchain/src/server/p2p_server.rs @@ -38,7 +38,7 @@ impl P2PServer { } } - fn connect_to_peer(&'static self, req: Request) { + fn connect_to_peer(&'static self, req: Request) -> Response { return Response::new("I also serve http requests".into()); }