Skip to content

Commit

Permalink
feat: Implement Utxo set flow separated from Utreexo accumulator upda…
Browse files Browse the repository at this point in the history
…tes (#212)
  • Loading branch information
Groxan authored Sep 24, 2024
1 parent 851e347 commit 079e69e
Show file tree
Hide file tree
Showing 35 changed files with 603 additions and 199 deletions.
19 changes: 11 additions & 8 deletions packages/client/src/main.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use consensus::types::block::Block;
use consensus::types::state::State;
use consensus::types::chain_state::BlockValidator;
use consensus::types::utxo_set::UtxoSet;
use consensus::types::utreexo::UtreexoStateTrait;
use consensus::types::utxo_set::{UtxoSet, UtxoSetTrait};

/// Raito program arguments.
#[derive(Serde)]
Expand All @@ -21,20 +22,22 @@ fn main(mut arguments: Span<felt252>) -> State {
let Args { mut state, blocks, } = Serde::deserialize(ref arguments)
.expect('Failed to deserialize');

let mut utxo_set = UtxoSet {
utreexo_state: state.utreexo_state,
leaves_to_add: Default::default(),
cache: Default::default(),
};
let mut utxo_set: UtxoSet = Default::default();

// Validate and apply block, accumulating UTXO updates in utxo_set
for block in blocks {
state
.chain_state = state
.chain_state
.validate_and_apply(block, ref utxo_set)
.expect('Validation failed');
.expect('Block validation failed');
};

state.utreexo_state = utxo_set.utreexo_state;
// Validate and apply UTXO updates
state.utreexo_state.validate_and_apply(ref utxo_set).expect('Utreexo validation failed');

// Ensure all UTXOs have been processed
utxo_set.finalize().expect('UtxoSet finalization failed');

state
}
40 changes: 33 additions & 7 deletions packages/client/src/test.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use consensus::types::block::Block;
use consensus::types::chain_state::{ChainState, BlockValidatorImpl};
use consensus::types::state::{State};
use consensus::types::utxo_set::UtxoSet;
use consensus::types::utxo_set::{UtxoSet, UtxoSetTrait};
use core::testing::get_available_gas;

/// Integration testing program arguments.
Expand All @@ -27,11 +27,7 @@ fn test(mut arguments: Span<felt252>) {
// Temporary solution while script doesn't handle utreexo.
// Allows to test one isolated block, or a batch of blocks starting from genesis.
let mut state: State = State { chain_state: chain_state, utreexo_state: Default::default(), };
let mut utxo_set: UtxoSet = UtxoSet {
utreexo_state: state.utreexo_state,
leaves_to_add: Default::default(),
cache: Default::default()
};
let mut utxo_set: UtxoSet = Default::default();

let mut gas_before = get_available_gas();

Expand All @@ -56,11 +52,41 @@ fn test(mut arguments: Span<felt252>) {

if state.chain_state != expected_chain_state {
println!(
"FAIL: block={} error='expected state {:?}, actual {:?}'",
"FAIL: block={} error='expected chain state {:?}, actual {:?}'",
state.chain_state.block_height,
expected_chain_state,
state.chain_state
);
panic!();
}

// TODO: provide the expected utreexo state via args and compare it with the actual one
//
// gas_before = get_available_gas();
//
// match state.utreexo_state.validate_and_apply(ref utxo_set) {
// Result::Ok(()) => {
// let gas_after = get_available_gas();
// println!("OK: gas_spent={}", gas_before - gas_after);
// },
// Result::Err(err) => {
// let gas_after = get_available_gas();
// println!("FAIL: gas_spent={} error='{:?}'", gas_before - gas_after, err);
// panic!();
// }
// }
//
// if state.utreexo_state != expected_utreexo_state {
// println!(
// "FAIL: error='expected utreexo state {:?}, actual {:?}'",
// expected_utreexo_state,
// state.utreexo_state
// );
// panic!();
// }

if let Result::Err(err) = utxo_set.finalize() {
println!("FAIL: error='{}'", err);
panic!();
}
}
3 changes: 3 additions & 0 deletions packages/client/tests/data/full_169.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"blocks": [
{
"header": {
"hash": "00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee",
"version": 1,
"time": 1231731025,
"bits": 486604799,
Expand All @@ -45,6 +46,7 @@
"pk_script": "0x",
"cached": false
},
"block_hash": "0000000000000000000000000000000000000000000000000000000000000000",
"block_height": 0,
"block_time": 0,
"is_coinbase": false
Expand Down Expand Up @@ -78,6 +80,7 @@
"pk_script": "0x410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac",
"cached": false
},
"block_hash": "000000008d9dc510f23c2657fc4f67bea30078cc05a90eb89e84cc475c080805",
"block_height": 9,
"block_time": 1231473279,
"is_coinbase": true
Expand Down
Loading

0 comments on commit 079e69e

Please sign in to comment.