Skip to content

Commit

Permalink
Utreexo integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus committed Oct 18, 2024
1 parent c3c1581 commit 0beba77
Show file tree
Hide file tree
Showing 21 changed files with 785 additions and 3 deletions.
Empty file.
1 change: 1 addition & 0 deletions packages/utreexo/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ cairo_test.workspace = true
[scripts]
# TODO: cairo lint
lint = "scarb fmt"
test = "scarb build && ../../scripts/data/integration_tests.sh"
1 change: 1 addition & 0 deletions packages/utreexo/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod stump {
pub mod proof;
pub mod accumulator;
}
pub mod test;

use core::poseidon::PoseidonTrait;
use core::hash::{HashStateTrait, HashStateExTrait};
Expand Down
2 changes: 1 addition & 1 deletion packages/utreexo/src/stump/proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use utils::{numeric::u64_next_power_of_two, sort::bubble_sort};

/// Utreexo inclusion proof for multiple outputs.
/// Compatible with https://github.com/utreexo/utreexo
#[derive(Drop, Copy)]
#[derive(Drop, Copy, Serde, Debug)]
pub struct UtreexoBatchProof {
/// List of sibling nodes required to calculate the roots.
pub proof: Span<felt252>,
Expand Down
42 changes: 42 additions & 0 deletions packages/utreexo/src/test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::stump::state::UtreexoStumpState;
use crate::stump::proof::UtreexoBatchProof;
use crate::stump::accumulator::StumpUtreexoAccumulator;
use core::testing::get_available_gas;

#[derive(Drop, Serde, Debug)]
struct Args {
state: UtreexoStumpState,
proof: UtreexoBatchProof,
leaves_to_del: Array<felt252>,
leaves_to_add: Array<felt252>,
expected_state: UtreexoStumpState,
}

fn main(mut arguments: Span<felt252>, _flags: felt252) {
let mut gas_before = get_available_gas();

let Args { mut state, proof, leaves_to_del, leaves_to_add: _, expected_state } = Serde::deserialize(
ref arguments
)
.expect('Failed to deserialize');

match state.verify_and_delete(@proof, leaves_to_del.span()) {
Result::Ok(new_state) => { state = new_state; },
Result::Err(err) => {
println!("FAIL: gas_spent={} error='{:?}'", gas_before - get_available_gas(), err);
panic!();
}
}

if state != expected_state {
println!(
"FAIL: gas_spent={} error='expected state {:?}, actual {:?}'",
gas_before - get_available_gas(),
expected_state,
state
);
panic!();
}

println!("OK: gas_spent={}", gas_before - get_available_gas());
}
37 changes: 37 additions & 0 deletions packages/utreexo/tests/data/deletion_test_case_0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [
0,
6,
359114454570462701179676018441683730149326686283278794303413350979946254235,
3613143053999770272842665473502706307602279676970981102529837756866990003067
],
"targets": [
1,
7
]
},
"leaves_to_del": [
1,
7
],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 2623024148755398637531760140996385406771006182960749518965731515106835184482
}
],
"leaves": 8
}
}
39 changes: 39 additions & 0 deletions packages/utreexo/tests/data/deletion_test_case_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [
0,
4,
6,
359114454570462701179676018441683730149326686283278794303413350979946254235
],
"targets": [
1,
5,
7
]
},
"leaves_to_del": [
1,
5,
7
],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 551674533840830050473419886120072409925785030121488629468288542069393223983
}
],
"leaves": 8
}
}
53 changes: 53 additions & 0 deletions packages/utreexo/tests/data/deletion_test_case_10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
},
{
"variant_id": 0,
"value": 2224391216833402212724735773212940252356536072547264452695587483243907176367
}
],
"leaves": 12
},
"proof": {
"nodes": [
1,
9,
359114454570462701179676018441683730149326686283278794303413350979946254235,
3567352371131958462633700980001500490451506860561008566474965365048073632618
],
"targets": [
0,
4,
5,
6,
7,
8
]
},
"leaves_to_del": [
0,
4,
5,
6,
7,
8
],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 841749818242255598355457760484755763160419106386224879307430326240151869062
},
{
"variant_id": 0,
"value": 1551534320701356863254738291358041183709275983112386770096580481322892724373
}
],
"leaves": 12
}
}
43 changes: 43 additions & 0 deletions packages/utreexo/tests/data/deletion_test_case_11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [],
"targets": [
0,
1,
2,
3,
4,
5,
6,
7
]
},
"leaves_to_del": [
0,
1,
2,
3,
4,
5,
6,
7
],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 1
}
],
"leaves": 8
}
}
38 changes: 38 additions & 0 deletions packages/utreexo/tests/data/deletion_test_case_12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [
2920447154653459698578961030005574439730780339384884329678783637696763668074
],
"targets": [
0,
1,
2,
3
]
},
"leaves_to_del": [
0,
1,
2,
3
],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 2920447154653459698578961030005574439730780339384884329678783637696763668074
}
],
"leaves": 8
}
}
41 changes: 41 additions & 0 deletions packages/utreexo/tests/data/deletion_test_case_13.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 1010147253610699894986241683624609428739638206544914327400350896031110287324
}
],
"leaves": 8
},
"proof": {
"nodes": [
1,
3,
5,
7
],
"targets": [
0,
2,
4,
6
]
},
"leaves_to_del": [
0,
2,
4,
6
],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 2007301334687130043979282869173510263602829546765086564193349908634632922671
}
],
"leaves": 8
}
}
47 changes: 47 additions & 0 deletions packages/utreexo/tests/data/deletion_test_case_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"state": {
"roots": [
{
"variant_id": 0,
"value": 2778277074578782368986165095004756321440748237082580104984033528445453379385
},
{
"variant_id": 0,
"value": 1187458855538029538020919690451164587984966864934526448127883456389212101807
}
],
"leaves": 20
},
"proof": {
"nodes": [
0,
11,
359114454570462701179676018441683730149326686283278794303413350979946254235,
3115762988631556491925147498418117978906005591291390166955707455104569660364,
2920447154653459698578961030005574439730780339384884329678783637696763668074,
1813826121834271504980511192245059557593697196840757755209576674667633130596
],
"targets": [
1,
10
]
},
"leaves_to_del": [
1,
10
],
"leaves_to_add": [],
"expected_state": {
"roots": [
{
"variant_id": 0,
"value": 2830407139356916834023424457422660277807343551646444886940602849914950685391
},
{
"variant_id": 0,
"value": 1187458855538029538020919690451164587984966864934526448127883456389212101807
}
],
"leaves": 20
}
}
Loading

0 comments on commit 0beba77

Please sign in to comment.