Skip to content

Commit

Permalink
Fix outpoint serialization issues
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus committed Oct 28, 2024
1 parent 08f5be7 commit ac6d98c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 43 deletions.
32 changes: 16 additions & 16 deletions packages/client/tests/data/utreexo_169.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,37 +128,37 @@
"roots": [
{
"variant_id": 0,
"value": 2243729579132454921815355740493841076889575709816526684999662912394274975584
"value": 3014509601099450824957764636677076983423737078119271708422294651717848944146
},
null,
null,
{
"variant_id": 0,
"value": 2261147245204776368334862529741319780868768593345793617211117537122266238577
"value": 533679620917332202524742698205102224878035031483832105912988809157979116435
},
null,
{
"variant_id": 0,
"value": 1478944086323777945936459676941601959769102822055825304629072181687547875531
"value": 1790999406438209766835706829961712782712666539252070841105759178141628778929
},
null,
{
"variant_id": 0,
"value": 619721724166206394862378867028287088626965757449429663146230416403718619093
"value": 1466128916896969404680984091783771661029213620940363609875929577329647604442
},
null
]
},
"proofs": [
{
"proof": [
274817076080541397502696906861536371115695596063526833550058573072108749115,
1259176147345751145191111662219840526356501226618672639406065326898135902264,
1848169735107995159136156318494866770346290350559572574934907975254450481704,
2706681913355105973250866982294192611208835262570818334870601840808848384459,
2214630919606246947408368640763197246751208900324012684012261556337212626757,
1101985322418033002091130704802558308187225122297992534396475623983004812420,
2729277592763167786852222424289031376235136104897643593004420518718352940231
1341831566043387106325844845909158799402866001662974927108069625603583971480,
368972441593618994557730456479873890910548867425570803234565201052562959106,
1419122656056762194903314678870864746341472136326851634183301537260163253426,
1129334889173766898759974826926150500378581466631368492477274753513812385698,
627157693386820306589919304217888126632511920825623115787661367209974488232,
1541076055468219256958548579763022121260181656240960330928352480415156088584,
3587610550992307773987219514490394196594915164063058596789500050188311666288
],
"leaf_index": 8
}
Expand All @@ -167,26 +167,26 @@
"roots": [
{
"variant_id": 0,
"value": 2843980787531725946792369479988202437986876451050335501296002525043057800822
"value": 1179811170457606057563835418653424295842192081881134790344715340360752949038
},
{
"variant_id": 0,
"value": 3259667504329439104864108759538360061214302036282299607930636468007761714215
"value": 1244943440808937605897621623373234998580616809226931298373102272903357779750
},
null,
{
"variant_id": 0,
"value": 2261147245204776368334862529741319780868768593345793617211117537122266238577
"value": 533679620917332202524742698205102224878035031483832105912988809157979116435
},
null,
{
"variant_id": 0,
"value": 1478944086323777945936459676941601959769102822055825304629072181687547875531
"value": 1790999406438209766835706829961712782712666539252070841105759178141628778929
},
null,
{
"variant_id": 0,
"value": 1597678775304253472610727607736636075835427473969519327752899884098801800240
"value": 2809989705256359302685824335644053071077397689442031541648352068549693441145
},
null
]
Expand Down
64 changes: 52 additions & 12 deletions packages/consensus/src/types/transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ impl TxOutDisplay of Display<TxOut> {

#[cfg(test)]
mod tests {
use super::{OutPoint, OutPointTrait, TxOut, HashStateTrait, HashStateExTrait};
use utils::hash::{DigestTrait};
use super::{OutPoint, TxOut, HashStateTrait, HashStateExTrait, OutPointTrait};
use utils::hex::{hex_to_hash_rev, from_hex};
use core::poseidon::PoseidonTrait;

fn hash(tx: @TxOut) -> felt252 {
Expand Down Expand Up @@ -256,24 +256,64 @@ mod tests {
assert_ne!(hash(@tx1), hash(@tx_with_value_changed));
}

#[derive(Debug, Drop, Default)]
pub struct HashState {
pub value: Array<felt252>
}

impl HashStateImpl of HashStateTrait<HashState> {
fn update(self: HashState, value: felt252) -> HashState {
let mut new_value = self.value;
new_value.append(value);
HashState { value: new_value }
}

fn finalize(self: HashState) -> felt252 {
0
}
}

#[test]
pub fn test_outpoint_poseidon_hash() {
let mut test_outpoint = OutPoint {
txid: DigestTrait::new([1, 2, 3, 4, 5, 6, 7, 8]),
vout: 2,
// https://learnmeabitcoin.com/explorer/tx/0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9#output-0
let mut coinbase_9_utxo = OutPoint {
txid: hex_to_hash_rev(
"0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9"
),
vout: 0,
data: TxOut {
value: 50_u64,
pk_script: @"410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac",
value: 5000000000,
pk_script: @from_hex(
"410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac"
),
cached: false,
},
block_height: 9,
median_time_past: 1650000000,
is_coinbase: false,
median_time_past: 1231471428,
is_coinbase: true,
};

let mut state: HashState = Default::default();
state = state.update_with(coinbase_9_utxo);

let expected: Array<felt252> = array![
5606656307511680658662848977137541728,
9103019671783490751638296454939121609,
0,
5000000000,
2,
114873147639302600539941532864842037771792291166958548649371950632810924198,
255491345418700057264349667014908841246825595399329019948869966327385048054,
372388307884,
5,
9,
1231471428,
1
];
assert_eq!(expected, state.value);

let hash = coinbase_9_utxo.hash();
assert_eq!(
test_outpoint.hash(),
3426256427357770988835517595549266311441229240020614569376880225204214993534
3389520813959039216633078222391869163691026953965968461933084500972022086342, hash
);
}
}
8 changes: 1 addition & 7 deletions packages/consensus/src/types/utxo_set.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,7 @@ pub impl UtxoSetImpl of UtxoSetTrait {
fn finalize(ref self: UtxoSet) -> Result<(), ByteArray> {
if self.num_cached != 0 {
Result::Err("There are unprocessed cached outputs")
} // TODO: uncomment when utreexo is enabled
// else if self.leaves_to_add.len() != 0 {
// Result::Err("There are unprocessed leaves to add")
// } else if self.leaves_to_delete.len() != 0 {
// Result::Err("There are unprocessed leaves to delete")
// }
else {
} else {
Result::Ok(())
}
}
Expand Down
13 changes: 7 additions & 6 deletions scripts/data/generate_utreexo_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ def _calculate_sub_data(self) -> t.List[int]:
pending_word = 0
pending_word_len = 0

# Check if there's still a word pending
if pending_word_len > 0:
sub_data.append(pending_word)

sub_data.append(pending_word)
sub_data.append(pending_word_len)

return sub_data
Expand All @@ -61,7 +58,8 @@ def serialize(self) -> OrderedDict:
sub_data = self._calculate_sub_data()

res["value"] = self.value
res["sub_data_len"] = len(sub_data)
# length of the array containing full words
res["sub_data_len"] = len(sub_data) - 2

for idx, word in enumerate(sub_data):
res["sub_data_{}".format(idx)] = word
Expand Down Expand Up @@ -98,15 +96,18 @@ def hash(self):
tab.append(self.vout)

# prev output
for e in self.data.serialize():
for _, e in self.data.serialize().items():
tab.append(e)

tab.append(self.block_height)
tab.append(self.median_time_past)

tab.append(int(self.is_coinbase))

print(f"Outpoint {tab}")

hash = poseidon_hash_many(tab)
print(f"Hash {hash}")
return hash

def __repr__(self):
Expand Down
3 changes: 1 addition & 2 deletions scripts/data/integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ ignored_files=(
"tests/data/full_478557.json", # Run panicked with [108217864776563 ('blocks'), ].
"tests/data/full_57042.json", # Run panicked with [108217864776563 ('blocks'), ].
"tests/data/full_72575.json", # Run panicked with [108217864776563 ('blocks'), ].
"tests/data/full_757752.json", # Run panicked with [108217864776563 ('blocks'), ].
"tests/data/utreexo_169.json", # Unexpected root (TODO: create issue)
"tests/data/full_757752.json", # Run panicked with [108217864776563 ('blocks'), ].
# "tests/data/full_478557.json", #runs on server
)

Expand Down

0 comments on commit ac6d98c

Please sign in to comment.