diff --git a/packages/client/tests/data/ignore b/packages/client/tests/data/ignore new file mode 100644 index 00000000..e69de29b diff --git a/packages/utreexo/Scarb.toml b/packages/utreexo/Scarb.toml index e823b1f8..31b1bf00 100644 --- a/packages/utreexo/Scarb.toml +++ b/packages/utreexo/Scarb.toml @@ -12,3 +12,4 @@ cairo_test.workspace = true [scripts] # TODO: cairo lint lint = "scarb fmt" +test = "scarb build && ../../scripts/data/integration_tests.sh" \ No newline at end of file diff --git a/packages/utreexo/src/lib.cairo b/packages/utreexo/src/lib.cairo index dd1760e4..1b1b80ca 100644 --- a/packages/utreexo/src/lib.cairo +++ b/packages/utreexo/src/lib.cairo @@ -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}; diff --git a/packages/utreexo/src/stump/proof.cairo b/packages/utreexo/src/stump/proof.cairo index 3e46b4ac..832ceecc 100644 --- a/packages/utreexo/src/stump/proof.cairo +++ b/packages/utreexo/src/stump/proof.cairo @@ -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, diff --git a/packages/utreexo/src/test.cairo b/packages/utreexo/src/test.cairo new file mode 100644 index 00000000..0222b5bb --- /dev/null +++ b/packages/utreexo/src/test.cairo @@ -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, + leaves_to_add: Array, + expected_state: UtreexoStumpState, +} + +fn main(mut arguments: Span, _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()); +} diff --git a/packages/utreexo/tests/data/deletion_test_case_0.json b/packages/utreexo/tests/data/deletion_test_case_0.json new file mode 100644 index 00000000..9868fdb5 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_0.json @@ -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 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_1.json b/packages/utreexo/tests/data/deletion_test_case_1.json new file mode 100644 index 00000000..4412a11c --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_1.json @@ -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 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_10.json b/packages/utreexo/tests/data/deletion_test_case_10.json new file mode 100644 index 00000000..a1330e5d --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_10.json @@ -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 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_11.json b/packages/utreexo/tests/data/deletion_test_case_11.json new file mode 100644 index 00000000..7ff8eaf8 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_11.json @@ -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 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_12.json b/packages/utreexo/tests/data/deletion_test_case_12.json new file mode 100644 index 00000000..921c8eb6 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_12.json @@ -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 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_13.json b/packages/utreexo/tests/data/deletion_test_case_13.json new file mode 100644 index 00000000..938ecba7 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_13.json @@ -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 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_2.json b/packages/utreexo/tests/data/deletion_test_case_2.json new file mode 100644 index 00000000..607319ae --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_2.json @@ -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 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_3.json b/packages/utreexo/tests/data/deletion_test_case_3.json new file mode 100644 index 00000000..30003925 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_3.json @@ -0,0 +1,51 @@ +{ + "state": { + "roots": [ + { + "variant_id": 0, + "value": 2778277074578782368986165095004756321440748237082580104984033528445453379385 + }, + { + "variant_id": 0, + "value": 1187458855538029538020919690451164587984966864934526448127883456389212101807 + } + ], + "leaves": 20 + }, + "proof": { + "nodes": [ + 0, + 11, + 17, + 359114454570462701179676018441683730149326686283278794303413350979946254235, + 3115762988631556491925147498418117978906005591291390166955707455104569660364, + 3325741655535215356957865124770093176746675852005881929681191209366655117343, + 2920447154653459698578961030005574439730780339384884329678783637696763668074, + 1813826121834271504980511192245059557593697196840757755209576674667633130596 + ], + "targets": [ + 1, + 10, + 16 + ] + }, + "leaves_to_del": [ + 1, + 10, + 16 + ], + "leaves_to_add": [], + "expected_state": { + "roots": [ + { + "variant_id": 0, + "value": 2830407139356916834023424457422660277807343551646444886940602849914950685391 + }, + { + "variant_id": 0, + "value": 3518456284848065536877573702921456496163509108617676224404664807737058463259 + } + ], + "leaves": 20 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_4.json b/packages/utreexo/tests/data/deletion_test_case_4.json new file mode 100644 index 00000000..6d01e617 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_4.json @@ -0,0 +1,65 @@ +{ + "state": { + "roots": [ + { + "variant_id": 0, + "value": 1010147253610699894986241683624609428739638206544914327400350896031110287324 + }, + { + "variant_id": 0, + "value": 2224391216833402212724735773212940252356536072547264452695587483243907176367 + }, + { + "variant_id": 0, + "value": 3009277733733429566191908933097273596911357184294896938062356457901603258437 + }, + { + "variant_id": 0, + "value": 14 + } + ], + "leaves": 15 + }, + "proof": { + "nodes": [ + 0, + 4, + 11, + 359114454570462701179676018441683730149326686283278794303413350979946254235, + 2476911194812244264213538976037850550079366744233323933541290896048104351430, + 3115762988631556491925147498418117978906005591291390166955707455104569660364 + ], + "targets": [ + 1, + 10, + 5 + ] + }, + "leaves_to_del": [ + 1, + 10, + 5 + ], + "leaves_to_add": [], + "expected_state": { + "roots": [ + { + "variant_id": 0, + "value": 2774269110009363850047571152130147152783590739705553396730017152246271218557 + }, + { + "variant_id": 0, + "value": 2434209091672931462468799562562031916768655108202953779055187960434828922032 + }, + { + "variant_id": 0, + "value": 3009277733733429566191908933097273596911357184294896938062356457901603258437 + }, + { + "variant_id": 0, + "value": 14 + } + ], + "leaves": 15 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_5.json b/packages/utreexo/tests/data/deletion_test_case_5.json new file mode 100644 index 00000000..a96f7620 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_5.json @@ -0,0 +1,66 @@ +{ + "state": { + "roots": [ + { + "variant_id": 0, + "value": 1010147253610699894986241683624609428739638206544914327400350896031110287324 + }, + { + "variant_id": 0, + "value": 2224391216833402212724735773212940252356536072547264452695587483243907176367 + }, + { + "variant_id": 0, + "value": 3009277733733429566191908933097273596911357184294896938062356457901603258437 + }, + { + "variant_id": 0, + "value": 14 + } + ], + "leaves": 15 + }, + "proof": { + "nodes": [ + 4, + 11, + 359114454570462701179676018441683730149326686283278794303413350979946254235, + 2476911194812244264213538976037850550079366744233323933541290896048104351430, + 3115762988631556491925147498418117978906005591291390166955707455104569660364 + ], + "targets": [ + 0, + 1, + 10, + 5 + ] + }, + "leaves_to_del": [ + 0, + 1, + 10, + 5 + ], + "leaves_to_add": [], + "expected_state": { + "roots": [ + { + "variant_id": 0, + "value": 497732802815518640976532643405733396450470267615991171885882933583320966954 + }, + { + "variant_id": 0, + "value": 2434209091672931462468799562562031916768655108202953779055187960434828922032 + }, + { + "variant_id": 0, + "value": 3009277733733429566191908933097273596911357184294896938062356457901603258437 + }, + { + "variant_id": 0, + "value": 14 + } + ], + "leaves": 15 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_6.json b/packages/utreexo/tests/data/deletion_test_case_6.json new file mode 100644 index 00000000..47409461 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_6.json @@ -0,0 +1,63 @@ +{ + "state": { + "roots": [ + { + "variant_id": 0, + "value": 1010147253610699894986241683624609428739638206544914327400350896031110287324 + }, + { + "variant_id": 0, + "value": 2224391216833402212724735773212940252356536072547264452695587483243907176367 + }, + { + "variant_id": 0, + "value": 3009277733733429566191908933097273596911357184294896938062356457901603258437 + }, + { + "variant_id": 0, + "value": 14 + } + ], + "leaves": 15 + }, + "proof": { + "nodes": [ + 4, + 359114454570462701179676018441683730149326686283278794303413350979946254235, + 2476911194812244264213538976037850550079366744233323933541290896048104351430 + ], + "targets": [ + 0, + 1, + 5, + 14 + ] + }, + "leaves_to_del": [ + 0, + 1, + 5, + 14 + ], + "leaves_to_add": [], + "expected_state": { + "roots": [ + { + "variant_id": 0, + "value": 497732802815518640976532643405733396450470267615991171885882933583320966954 + }, + { + "variant_id": 0, + "value": 2224391216833402212724735773212940252356536072547264452695587483243907176367 + }, + { + "variant_id": 0, + "value": 3009277733733429566191908933097273596911357184294896938062356457901603258437 + }, + { + "variant_id": 1 + } + ], + "leaves": 15 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_7.json b/packages/utreexo/tests/data/deletion_test_case_7.json new file mode 100644 index 00000000..235f2d88 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_7.json @@ -0,0 +1,65 @@ +{ + "state": { + "roots": [ + { + "variant_id": 0, + "value": 1010147253610699894986241683624609428739638206544914327400350896031110287324 + }, + { + "variant_id": 0, + "value": 2224391216833402212724735773212940252356536072547264452695587483243907176367 + }, + { + "variant_id": 0, + "value": 3009277733733429566191908933097273596911357184294896938062356457901603258437 + }, + { + "variant_id": 0, + "value": 14 + } + ], + "leaves": 15 + }, + "proof": { + "nodes": [ + 0, + 2, + 4, + 7 + ], + "targets": [ + 1, + 6, + 5, + 3 + ] + }, + "leaves_to_del": [ + 1, + 6, + 5, + 3 + ], + "leaves_to_add": [], + "expected_state": { + "roots": [ + { + "variant_id": 0, + "value": 2542834485472201375063952267657234229360599181580459538243740237309237489997 + }, + { + "variant_id": 0, + "value": 2224391216833402212724735773212940252356536072547264452695587483243907176367 + }, + { + "variant_id": 0, + "value": 3009277733733429566191908933097273596911357184294896938062356457901603258437 + }, + { + "variant_id": 0, + "value": 14 + } + ], + "leaves": 15 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_8.json b/packages/utreexo/tests/data/deletion_test_case_8.json new file mode 100644 index 00000000..d2952f7e --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_8.json @@ -0,0 +1,64 @@ +{ + "state": { + "roots": [ + { + "variant_id": 0, + "value": 2165915913108448461952915035199992115188274826980627306957797425653511112322 + }, + { + "variant_id": 0, + "value": 2233258972414885866189363096992191546710327778084747172639566606002708566944 + }, + { + "variant_id": 0, + "value": 642347556491670165283398532129323576556514980317062989585571746245257915465 + } + ], + "leaves": 100 + }, + "proof": { + "nodes": [ + 3, + 8, + 2476911194812244264213538976037850550079366744233323933541290896048104351430, + 3567352371131958462633700980001500490451506860561008566474965365048073632618, + 1813826121834271504980511192245059557593697196840757755209576674667633130596, + 1858980809370786577311980994727901038167608739072537272840848798584819431473, + 1024769225221052025281254286802090898314321261159479176848884875817503580840 + ], + "targets": [ + 0, + 1, + 2, + 4, + 5, + 9 + ] + }, + "leaves_to_del": [ + 0, + 1, + 2, + 4, + 5, + 9 + ], + "leaves_to_add": [], + "expected_state": { + "roots": [ + { + "variant_id": 0, + "value": 394259646554252767984184254989108416967864354151675101674138588881099113659 + }, + { + "variant_id": 0, + "value": 2233258972414885866189363096992191546710327778084747172639566606002708566944 + }, + { + "variant_id": 0, + "value": 642347556491670165283398532129323576556514980317062989585571746245257915465 + } + ], + "leaves": 100 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/deletion_test_case_9.json b/packages/utreexo/tests/data/deletion_test_case_9.json new file mode 100644 index 00000000..2afe53a1 --- /dev/null +++ b/packages/utreexo/tests/data/deletion_test_case_9.json @@ -0,0 +1,53 @@ +{ + "state": { + "roots": [ + { + "variant_id": 0, + "value": 2165915913108448461952915035199992115188274826980627306957797425653511112322 + }, + { + "variant_id": 0, + "value": 2233258972414885866189363096992191546710327778084747172639566606002708566944 + }, + { + "variant_id": 0, + "value": 642347556491670165283398532129323576556514980317062989585571746245257915465 + } + ], + "leaves": 100 + }, + "proof": { + "nodes": [ + 39, + 3445535368271015044293121856705513217511168631048114526337134830187316161922, + 1591239225204504744081254413392096259071062676306271598794215164553405364886, + 1547130669050410027801432723566156116714889887376207199769468845503460861186, + 2258044221655052220972134952667238440136729258761489832316394515872580012696, + 1164355837085349317552079963264680967657912756202432097157524592786311377117 + ], + "targets": [ + 38 + ] + }, + "leaves_to_del": [ + 38 + ], + "leaves_to_add": [], + "expected_state": { + "roots": [ + { + "variant_id": 0, + "value": 929158566745605449453211918707771431050728700323714963056620405087827552396 + }, + { + "variant_id": 0, + "value": 2233258972414885866189363096992191546710327778084747172639566606002708566944 + }, + { + "variant_id": 0, + "value": 642347556491670165283398532129323576556514980317062989585571746245257915465 + } + ], + "leaves": 100 + } +} \ No newline at end of file diff --git a/packages/utreexo/tests/data/ignore b/packages/utreexo/tests/data/ignore new file mode 100644 index 00000000..f0909d68 --- /dev/null +++ b/packages/utreexo/tests/data/ignore @@ -0,0 +1,5 @@ +deletion_test_case_3.json +deletion_test_case_4.json +deletion_test_case_5.json +deletion_test_case_6.json +deletion_test_case_10.json diff --git a/scripts/data/integration_tests.sh b/scripts/data/integration_tests.sh index cf32e288..edc0c2ec 100755 --- a/scripts/data/integration_tests.sh +++ b/scripts/data/integration_tests.sh @@ -51,13 +51,21 @@ ignored_files=( "tests/data/utreexo_169.json", # Unexpected root (TODO: create issue) # "tests/data/full_478557.json", #runs on server ) + +ignore_file="tests/data/ignore" +if [[ -f "$ignore_file" ]]; then + while IFS= read -r line; do + ignored_files+="tests/data/$line" + done < "$ignore_file" +fi + ignored="${ignored_files[@]}" # If no test files are explicitly specified, default to tests/data/* if [[ $fullonly -eq 1 && ${#test_files[@]} -eq 0 ]]; then - test_files=("tests/data"/full*) + test_files=("tests/data"/full*.json) elif [[ ${#test_files[@]} -eq 0 ]]; then - test_files=("tests/data"/*) + test_files=("tests/data"/*.json) fi if [[ $execute_scripts -eq 1 ]]; then