-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from keep-starknet-strange/mk/state-draft
Cleaner ChainState
- Loading branch information
Showing
7 changed files
with
149 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
scarb 2.6.3 | ||
scarb 2.7.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,3 @@ version = "0.1.0" | |
edition = "2023_11" | ||
|
||
[dependencies] | ||
starknet = "2.6.3" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
pub mod engine; | ||
|
||
mod state; | ||
mod validation; | ||
mod main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,3 @@ | ||
use raito::engine::BlockHeaderEngineTrait; | ||
use raito::engine::BlockHeaderEngineImpl; | ||
use raito::engine::BlockHeaderValidationContextTrait; | ||
use raito::engine::BlockHeaderValidationContextImpl; | ||
use raito::engine::BlockHeaderTrait; | ||
use raito::engine::BlockHeaderImpl; | ||
use raito::engine::ChainStateTrait; | ||
use raito::engine::ChainStateImpl; | ||
use raito::engine::ChainState; | ||
|
||
fn main() { | ||
println!("Running Raito Bitcoin ZK client"); | ||
let block_header = BlockHeaderImpl::new(0, "", "", 0, 0, 0); | ||
|
||
// The Times 03/Jan/2009 Chancellor on brink of second bailout for banks | ||
let GENESIS = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"; | ||
|
||
let chain_state = ChainState { | ||
block_height: 0, | ||
total_work: 0, | ||
best_block_hash: GENESIS, | ||
current_target: 0, | ||
epoch_start_time: 0, | ||
prev_timestamps: ArrayTrait::new() | ||
}; | ||
|
||
let mut context = BlockHeaderValidationContextImpl::new(block_header, "", 0, chain_state, 0); | ||
let mut engine = BlockHeaderEngineImpl::new(context); | ||
|
||
let res = engine.validate_and_apply_block_header(); | ||
if res.is_ok() { | ||
println!("Execution successful"); | ||
} else { | ||
println!("Execution failed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/// Represents the state of the blockchain. | ||
#[derive(Drop, Copy)] | ||
pub struct ChainState { | ||
/// Height of the current block. | ||
pub block_height: u32, // not u256? | ||
/// Total work done. | ||
pub total_work: u256, | ||
/// Best block. | ||
pub best_block_hash: u256, | ||
/// Current block. | ||
pub current_target: u32, | ||
/// Start of the current epoch. | ||
pub epoch_start_time: u32, | ||
/// Previous timestamps. | ||
pub prev_timestamps: Span<u32>, | ||
// TODO: utreexo_roots? | ||
} | ||
|
||
/// Represents a block in the blockchain. | ||
/// | ||
#[derive(Drop, Copy)] | ||
pub struct Block { | ||
/// block header | ||
pub header: Header, | ||
// TODO: how to handle coinbase transactions? | ||
|
||
/// Transactions | ||
pub txs: Span<Transaction>, | ||
} | ||
|
||
/// Block header | ||
/// https://developer.bitcoin.org/reference/block_chain.html#block-headers | ||
#[derive(Drop, Copy)] | ||
struct Header { | ||
/// The version of the block. | ||
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. | ||
pub bits: u32, | ||
/// The nonce used in mining the block. | ||
pub nonce: u32, | ||
} | ||
|
||
/// Transaction | ||
/// https://developer.bitcoin.org/reference/transactions.html#raw-transaction-format | ||
#[derive(Drop, Copy)] | ||
pub struct Transaction { | ||
/// The version of the transaction. | ||
pub version: i32, | ||
/// The inputs of the transaction. | ||
pub inputs: Span<TxIn>, | ||
/// The outputs of the transaction. | ||
pub outputs: Span<TxOut>, | ||
/// The lock time of the transaction. | ||
pub lock_time: u32, | ||
} | ||
|
||
/// Output of a transaction. | ||
/// https://developer.bitcoin.org/reference/transactions.html#txout-a-transaction-output | ||
#[derive(Drop, Copy)] | ||
pub struct TxOut { | ||
/// The value of the output. | ||
value: i64, | ||
/// The public key script of the output. | ||
pk_script: @ByteArray, | ||
} | ||
|
||
/// Input of a transaction. | ||
/// https://developer.bitcoin.org/reference/transactions.html#txin-a-transaction-input-non-coinbase | ||
#[derive(Drop, Copy)] | ||
pub struct TxIn { | ||
/// The transaction ID of the input. | ||
txid: u256, | ||
/// The index of the input. | ||
index: u32, | ||
/// The script of the input. | ||
script: @ByteArray, | ||
/// The sequence of the input. | ||
sequence: u32, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use super::state::{Block, ChainState}; | ||
|
||
#[generate_trait] | ||
impl BlockValidatorImpl of BlockValidator { | ||
fn validate_and_apply(self: ChainState, block: Block) -> Result<ChainState, ByteArray> { | ||
validate_prev_block_hash(@self, @block)?; | ||
validate_proof_of_work(@self, @block)?; | ||
validate_target(@self, @block)?; | ||
validate_timestamp(@self, @block)?; | ||
|
||
// validate_merkle_root | ||
// validate_and_apply_transactions | ||
|
||
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); | ||
|
||
Result::Ok( | ||
ChainState { total_work, current_target, epoch_start_time, prev_timestamps, ..self, } | ||
) | ||
} | ||
} | ||
|
||
fn validate_prev_block_hash(self: @ChainState, block: @Block) -> Result<(), ByteArray> { | ||
if self.best_block_hash == block.header.prev_block_hash { | ||
Result::Ok(()) | ||
} else { | ||
Result::Err("Invalid `prev_block_hash`. This block does not extend the current chain.") | ||
} | ||
} | ||
|
||
fn validate_proof_of_work(self: @ChainState, block: @Block) -> Result<(), ByteArray> { | ||
// TODO: implement | ||
Result::Ok(()) | ||
} | ||
|
||
fn validate_target(self: @ChainState, block: @Block) -> Result<(), ByteArray> { | ||
// TODO: implement | ||
Result::Ok(()) | ||
} | ||
|
||
fn validate_timestamp(self: @ChainState, block: @Block) -> Result<(), ByteArray> { | ||
// TODO: implement | ||
Result::Ok(()) | ||
} | ||
|
||
fn next_prev_timestamps(self: @ChainState, block: @Block) -> Span<u32> { | ||
// TODO: implement | ||
*self.prev_timestamps | ||
} | ||
|
||
fn compute_total_work(self: @ChainState, block: @Block) -> u256 { | ||
// TODO: implement | ||
*self.total_work | ||
} | ||
|
||
fn adjust_difficulty(self: @ChainState, block: @Block) -> (u32, u32) { | ||
// TODO: implement | ||
(*self.current_target, *self.epoch_start_time) | ||
} |