Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft of coinbase transaction validation #38

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ 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:
Expand Down
3 changes: 1 addition & 2 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod utils;
mod utils;
pub mod validation;

mod state;
mod main;
2 changes: 0 additions & 2 deletions src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
73 changes: 58 additions & 15 deletions src/validation.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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;

Expand All @@ -12,19 +11,47 @@ impl BlockValidatorImpl of BlockValidator {
validate_target(@self, @block)?;
validate_timestamp(@self, @block)?;

validate_merkle_root(@self, @block)?;
// validate_and_apply_transactions
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 TransactionValidatorImpl of TransactionValidator {
fn txid(self: @Transaction) -> u256 {
// TODO: implement
0
}
fn fee(self: @Transaction) -> u256 {
// TODO: implement
0
}
}

fn block_hash(block: @Block, merkle_root: u256) -> Result<u256, ByteArray> {
// TODO: implement
Result::Ok(0)
}

fn validate_prev_block_hash(self: @ChainState, block: @Block) -> Result<(), ByteArray> {
if self.best_block_hash == block.header.prev_block_hash {
Result::Ok(())
Expand Down Expand Up @@ -124,6 +151,28 @@ pub fn target_to_bits(target: u256) -> Result<u32, felt252> {
Result::Ok(result)
}

fn 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>) -> u256 {
// TODO: implement
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};
Expand All @@ -142,9 +191,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(),
};

Expand Down Expand Up @@ -174,9 +221,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(),
};

Expand All @@ -198,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(),
};

Expand Down
3 changes: 0 additions & 3 deletions tests/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use core::result::ResultTrait;
use core::option::OptionTrait;
use core::traits::Into;
use raito::validation::target_to_bits;

#[test]
Expand Down