From 38ffcd1122e6fe223c9d70bccfd2c851586d5c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 7 Aug 2024 16:23:27 +0200 Subject: [PATCH 1/4] add tg group --- README.md | 4 ++++ src/lib.cairo | 5 ++--- src/validation.cairo | 53 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index acbb1900..372b9f8f 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,10 @@ Raito is a reference to Light Yagami (夜神月, Yagami Raito) from the manga/an ![Raito and Raito](./docs/img/memes/raito_shinigami_fusion.jpg) +# Contact +* [Raito Telegram](https://t.me/RaitoStarknet) +* [Raito OnlyDust](https://app.onlydust.com/p/raito---bitcoin-zk-client) + ## Usage This will compile all the components: diff --git a/src/lib.cairo b/src/lib.cairo index ce41db81..49ebbf17 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,5 +1,4 @@ -pub mod utils; -pub mod validation; - +mod utils; +mod validation; mod state; mod main; diff --git a/src/validation.cairo b/src/validation.cairo index d88aa3ea..e49306c5 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -1,19 +1,20 @@ -use super::state::{Block, ChainState, UtreexoState}; -use raito::utils::shl; -use raito::utils::shr; +use super::utils::{shl, shr}; +use super::state::{Block, ChainState, Transaction, UtreexoState}; const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000; + #[generate_trait] impl BlockValidatorImpl of BlockValidator { fn validate_and_apply(self: ChainState, block: Block) -> Result { + + validate_block_hash(@self, @block)?; validate_prev_block_hash(@self, @block)?; validate_proof_of_work(@0_u256, @block)?; validate_target(@self, @block)?; validate_timestamp(@self, @block)?; - validate_merkle_root(@self, @block)?; - // validate_and_apply_transactions + let (total_fee, merkle_root) = get_fee_and_merkle_root(@self, @block)?; let prev_timestamps = next_prev_timestamps(@self, @block); let total_work = compute_total_work(@self, @block); @@ -25,6 +26,23 @@ impl BlockValidatorImpl of BlockValidator { } } +#[generate_trait] +impl TransactionImplIml of TransactionImpl { + fn txid(self: @Transaction) -> u256 { + // TODO: implement + 0 + } + fn fee(self: @Transaction) -> u256 { + // TODO: implement + 0 + } +} + +fn validate_block_hash(self: @ChainState, block: @Block) -> Result<(), ByteArray> { + // TODO: implement + Result::Ok(()) +} + fn validate_prev_block_hash(self: @ChainState, block: @Block) -> Result<(), ByteArray> { if self.best_block_hash == block.header.prev_block_hash { Result::Ok(()) @@ -124,6 +142,31 @@ pub fn target_to_bits(target: u256) -> Result { Result::Ok(result) } +fn get_fee_and_merkle_root(self: @ChainState, block: @Block) -> Result<(u256, u256), ByteArray> { + + let mut txids = ArrayTrait::new(); + let mut total_fee = 0; + + for tx in *block.txs { + txids.append(tx.txid()); + total_fee += tx.fee(); + }; + + Result::Ok((total_fee, merkle_root(txids))) +} + +fn merkle_root(txids: Array) -> u256 { + // TODO: implement + 0 +} + +fn validate_transaction(tx: @Transaction) -> Result { + // TODO: implement + Result::Ok(0) +} + + + #[cfg(test)] mod tests { use super::{validate_target, validate_timestamp, validate_proof_of_work}; From 4d39b81f3f3020def404cbcc4bfe8b842fa2bd07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 7 Aug 2024 21:46:46 +0200 Subject: [PATCH 2/4] validate_coinbase draft --- src/state.cairo | 2 -- src/validation.cairo | 42 +++++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/state.cairo b/src/state.cairo index 2f5e3653..6fcaf700 100644 --- a/src/state.cairo +++ b/src/state.cairo @@ -75,8 +75,6 @@ pub struct Header { pub version: u32, /// The hash of the previous block in the blockchain. pub prev_block_hash: u256, - /// The Merkle root hash of the transactions in the block. - pub merkle_root_hash: u256, /// The timestamp of the block. pub time: u32, /// The difficulty target for mining the block. diff --git a/src/validation.cairo b/src/validation.cairo index e49306c5..262b9917 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -3,31 +3,40 @@ use super::state::{Block, ChainState, Transaction, UtreexoState}; const MAX_TARGET: u256 = 0x00000000FFFF0000000000000000000000000000000000000000000000000000; - #[generate_trait] impl BlockValidatorImpl of BlockValidator { fn validate_and_apply(self: ChainState, block: Block) -> Result { - - validate_block_hash(@self, @block)?; validate_prev_block_hash(@self, @block)?; validate_proof_of_work(@0_u256, @block)?; validate_target(@self, @block)?; validate_timestamp(@self, @block)?; - let (total_fee, merkle_root) = get_fee_and_merkle_root(@self, @block)?; + let (total_fees, merkle_root) = fee_and_merkle_root(@self, @block)?; + + validate_coinbase(@block, total_fees)?; + let best_block_hash = block_hash(@block, merkle_root)?; let prev_timestamps = next_prev_timestamps(@self, @block); let total_work = compute_total_work(@self, @block); let (current_target, epoch_start_time) = adjust_difficulty(@self, @block); + let block_height = self.block_height + 1; Result::Ok( - ChainState { total_work, current_target, epoch_start_time, prev_timestamps, ..self, } + ChainState { + block_height, + total_work, + best_block_hash, + current_target, + epoch_start_time, + prev_timestamps, + ..self, + } ) } } #[generate_trait] -impl TransactionImplIml of TransactionImpl { +impl TransactionValidatorImpl of TransactionValidator { fn txid(self: @Transaction) -> u256 { // TODO: implement 0 @@ -38,9 +47,9 @@ impl TransactionImplIml of TransactionImpl { } } -fn validate_block_hash(self: @ChainState, block: @Block) -> Result<(), ByteArray> { +fn block_hash(block: @Block, merkle_root: u256) -> Result { // TODO: implement - Result::Ok(()) + Result::Ok(0) } fn validate_prev_block_hash(self: @ChainState, block: @Block) -> Result<(), ByteArray> { @@ -144,6 +153,7 @@ pub fn target_to_bits(target: u256) -> Result { fn get_fee_and_merkle_root(self: @ChainState, block: @Block) -> Result<(u256, u256), ByteArray> { +fn fee_and_merkle_root(self: @ChainState, block: @Block) -> Result<(u256, u256), ByteArray> { let mut txids = ArrayTrait::new(); let mut total_fee = 0; @@ -160,13 +170,11 @@ fn merkle_root(txids: Array) -> u256 { 0 } -fn validate_transaction(tx: @Transaction) -> Result { - // TODO: implement - Result::Ok(0) +fn validate_coinbase(block: @Block, total_fees: u256) -> Result<(), ByteArray> { + //TODO implement + Result::Ok(()) } - - #[cfg(test)] mod tests { use super::{validate_target, validate_timestamp, validate_proof_of_work}; @@ -185,9 +193,7 @@ mod tests { utreexo_state: UtreexoState { roots: array![].span() }, }; let mut block = Block { - header: Header { - version: 1, prev_block_hash: 1, merkle_root_hash: 1, time: 1, bits: 1, nonce: 1, - }, + header: Header { version: 1, prev_block_hash: 1, time: 1, bits: 1, nonce: 1, }, txs: ArrayTrait::new().span(), }; @@ -217,9 +223,7 @@ mod tests { utreexo_state: UtreexoState { roots: array![].span() }, }; let mut block = Block { - header: Header { - version: 1, prev_block_hash: 1, merkle_root_hash: 1, time: 12, bits: 1, nonce: 1, - }, + header: Header { version: 1, prev_block_hash: 1, time: 12, bits: 1, nonce: 1, }, txs: ArrayTrait::new().span(), }; From 0cdc8b3672a6bf93a441190571d905e5a420234e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 7 Aug 2024 21:58:51 +0200 Subject: [PATCH 3/4] fixes --- src/lib.cairo | 2 +- src/validation.cairo | 6 +----- tests/tests.cairo | 3 --- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/lib.cairo b/src/lib.cairo index 49ebbf17..7778f44f 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,4 +1,4 @@ mod utils; -mod validation; +pub mod validation; mod state; mod main; diff --git a/src/validation.cairo b/src/validation.cairo index 262b9917..ce17c749 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -151,8 +151,6 @@ pub fn target_to_bits(target: u256) -> Result { Result::Ok(result) } -fn get_fee_and_merkle_root(self: @ChainState, block: @Block) -> Result<(u256, u256), ByteArray> { - fn fee_and_merkle_root(self: @ChainState, block: @Block) -> Result<(u256, u256), ByteArray> { let mut txids = ArrayTrait::new(); let mut total_fee = 0; @@ -245,9 +243,7 @@ mod tests { #[test] fn test_validate_proof_of_work() { let mut block = Block { - header: Header { - version: 1, prev_block_hash: 1, merkle_root_hash: 1, time: 12, bits: 1, nonce: 1, - }, + header: Header { version: 1, prev_block_hash: 1, time: 12, bits: 1, nonce: 1, }, txs: ArrayTrait::new().span(), }; diff --git a/tests/tests.cairo b/tests/tests.cairo index 76c15f51..0646d0a5 100644 --- a/tests/tests.cairo +++ b/tests/tests.cairo @@ -1,6 +1,3 @@ -use core::result::ResultTrait; -use core::option::OptionTrait; -use core::traits::Into; use raito::validation::target_to_bits; #[test] From e60c611d372f2ffaa5b9073a497345e0d08b3542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 7 Aug 2024 22:04:18 +0200 Subject: [PATCH 4/4] fix md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 372b9f8f..ed92fc57 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ Raito is a reference to Light Yagami (夜神月, Yagami Raito) from the manga/an ![Raito and Raito](./docs/img/memes/raito_shinigami_fusion.jpg) # Contact + * [Raito Telegram](https://t.me/RaitoStarknet) * [Raito OnlyDust](https://app.onlydust.com/p/raito---bitcoin-zk-client)