fn main(){ let previous_hash = "b9b9ee9ffc95fa4956b63b6043a99d0a8f04e0e52e687fc1958d3c6dff885f01"; let mut num = rand::thread_rng().gen_range(0..1000000); let mut hash_num = format!("{}{}", previous_hash, num.to_string()); let mut header = Sha256::digest(hash_num); let mut target: String = (&hex::encode(header)[..4]).to_string(); let mut cnt = 1; println!("count: {} {:x}", cnt, header); while target != "0000" { println!("count: {} {:x}", cnt, header); num = rand::thread_rng().gen_range(0..1000000); hash_num = format!("{}{}", previous_hash, num.to_string()); header = Sha256::digest(hash_num); target = (&hex::encode(header)[..4]).to_string(); cnt += 1; } println!("count: {} {:x}", cnt, header); let t = vec!["sender".to_string(), "receiver".to_string(), "amount".to_string()]; let utc_datetime: DateTime<Utc> = Utc::now(); let b = Block{time:utc_datetime.to_string(), transactions: t, hash: hex::encode(header).to_string(), nonce:num.to_string()}; println!("{:?}", b); }
$ cargo run
…
Block { time: “2025-01-04 00:22:15.386703002 UTC”, transactions: [“sender”, “receiver”, “amount”], hash: “0000cef5d82d7d11f5e90aab7439a128e5877ee6e6b8da31301a6359d74b5503”, nonce: “494683” }
なるほど、こういう仕組みなのか!
うん、悪くないかも
これで、トランザクションの箇所を、Postされてきたデータの構造体に置き換えると…
#[derive(Debug, Serialize, Clone, Deserialize)] struct Block { time: String, transactions: Vec<SignedTransaction>, hash: String, nonce: String, }
$ cargo run
…
Block { time: “2025-01-04 00:31:53.015599503 UTC”, transactions: [SignedTransaction { time: “2024-12-25 22:53:36.824066840 UTC”, sender: “5bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991123”, receiver: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124”, amount: 10, signature: “8000E340A55A517D0F27F3A63FBE39ED576BA491DFAC89B44654AB147EC66B206B054BAAF53E318EB2721DC892B4736630F400547989AE9F7C069034ECB4DF98” }, SignedTransaction { time: “2024-12-25 22:53:36.824066840 UTC”, sender: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991123”, receiver: “4bac6cb0f4ad6397752c3d73b88c5c86e3d88ac695118494a1732e2abd16c76acad3d6586c37c8db7e69c2f812f99275198936957d72c38d71981991124”, amount: 10, signature: “8000E340A55A517D0F27F3A63FBE39ED576BA491DFAC89B44654AB147EC66B206B054BAAF53E318EB2721DC892B4736630F400547989AE9F7C069034ECB4DF98” }], hash: “0000f5b116872033b276499b3e039b03fc4753aaa4e400f2cce856c358d2d51f”, nonce: “4961” }
blockを作成しました。
SignedTransactionがvectorとしてblockの中に入ります。
おおおおおおおおおおおおおお
基本的な形はできたやん
次の課題として、
– blockをjsonデータとして保存する
– genesis blockと生成したblockを繋げていく処理を検討する