Skip to content

Commit 5ee1f20

Browse files
authored
[pythnet] Update serialization for MerkelPath/MerkleAccumulator (#830)
* feat: update serialization for MerkelPath/MerkleAccumulator * Fix incorrect variable name.
1 parent e802593 commit 5ee1f20

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

.pre-commit-config.yaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,26 @@ repos:
7171
language: "rust"
7272
entry: cargo +nightly fmt --manifest-path ./pythnet/message_buffer/Cargo.toml --all -- --config-path rustfmt.toml
7373
pass_filenames: false
74-
files: message_buffer
74+
files: pythnet/message_buffer
7575
- id: cargo-clippy-message-buffer
7676
name: Cargo clippy for message buffer contract
7777
language: "rust"
7878
entry: cargo +nightly clippy --manifest-path ./pythnet/message_buffer/Cargo.toml --tests --fix --allow-dirty --allow-staged --features test-bpf -- -D warnings
7979
pass_filenames: false
80-
files: message_buffer
80+
files: pythnet/message_buffer
81+
# Hooks for pythnet_sdk
82+
- id: cargo-fmt-pythnet-sdk
83+
name: Cargo format for pythnet SDK
84+
language: "rust"
85+
entry: cargo +nightly fmt --manifest-path ./pythnet/pythnet_sdk/Cargo.toml --all -- --config-path rustfmt.toml
86+
pass_filenames: false
87+
files: pythnet/pythnet_sdk
88+
- id: cargo-clippy-pythnet-sdk
89+
name: Cargo clippy for pythnet SDK
90+
language: "rust"
91+
entry: cargo +nightly clippy --manifest-path ./pythnet/pythnet_sdk/Cargo.toml --tests --fix --allow-dirty --allow-staged -- -D warnings
92+
pass_filenames: false
93+
files: pythnet/pythnet_sdk
8194
# Hooks for solana receiver contract
8295
- id: cargo-fmt-solana-receiver
8396
name: Cargo format for solana target chain contract

pythnet/pythnet_sdk/examples/generate_pyth_data.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use {
88
solana_client::rpc_client::RpcClient,
99
solana_pyth::PYTH_PID,
1010
solana_sdk::pubkey::Pubkey,
11-
std::str::FromStr,
12-
std::io::Write,
11+
std::{
12+
io::Write,
13+
str::FromStr,
14+
},
1315
};
1416

1517
fn main() {

pythnet/pythnet_sdk/src/accumulators/merkle.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ impl<H: Hasher> MerklePath<H> {
5858
pub fn new(path: Vec<H::Hash>) -> Self {
5959
Self(path)
6060
}
61+
62+
pub fn serialize(&self) -> Option<Vec<u8>> {
63+
let mut serialized = vec![];
64+
let proof_size: u8 = self.0.len().try_into().ok()?;
65+
serialized.extend_from_slice(&proof_size.to_be_bytes());
66+
67+
for node in self.0.iter() {
68+
serialized.extend_from_slice(node.as_ref());
69+
}
70+
71+
Some(serialized)
72+
}
6173
}
6274

6375
/// A MerkleAccumulator maintains a Merkle Tree.
@@ -86,11 +98,12 @@ pub struct MerkleAccumulator<H: Hasher = Keccak256> {
8698
// TODO: This code does not belong to MerkleAccumulator, we should be using the wire data types in
8799
// calling code to wrap this value.
88100
impl<'a, H: Hasher + 'a> MerkleAccumulator<H> {
89-
pub fn serialize(&self, storage: u32) -> Vec<u8> {
101+
pub fn serialize(&self, slot: u64, ring_size: u32) -> Vec<u8> {
90102
let mut serialized = vec![];
91103
serialized.extend_from_slice(0x41555756u32.to_be_bytes().as_ref());
92104
serialized.extend_from_slice(0u8.to_be_bytes().as_ref());
93-
serialized.extend_from_slice(storage.to_be_bytes().as_ref());
105+
serialized.extend_from_slice(slot.to_be_bytes().as_ref());
106+
serialized.extend_from_slice(ring_size.to_be_bytes().as_ref());
94107
serialized.extend_from_slice(self.root.as_ref());
95108
serialized
96109
}
@@ -146,7 +159,7 @@ impl<H: Hasher> MerkleAccumulator<H> {
146159
// Filling the leaf hashes
147160
for i in 0..(1 << depth) {
148161
if i < items.len() {
149-
tree[(1 << depth) + i] = hash_leaf::<H>(items[i].as_ref());
162+
tree[(1 << depth) + i] = hash_leaf::<H>(items[i]);
150163
} else {
151164
tree[(1 << depth) + i] = hash_null::<H>();
152165
}
@@ -171,7 +184,7 @@ impl<H: Hasher> MerkleAccumulator<H> {
171184
fn find_path(&self, mut index: usize) -> MerklePath<H> {
172185
let mut path = Vec::new();
173186
while index > 1 {
174-
path.push(self.nodes[index ^ 1].clone());
187+
path.push(self.nodes[index ^ 1]);
175188
index /= 2;
176189
}
177190
MerklePath::new(path)
@@ -369,7 +382,7 @@ mod test {
369382
// Accumulate into a 2 level tree.
370383
let accumulator = MerkleAccumulator::<Keccak256>::from_set(set.into_iter()).unwrap();
371384
let proof = accumulator.prove(&item_a).unwrap();
372-
assert!(accumulator.check(proof.clone(), &item_a));
385+
assert!(accumulator.check(proof, &item_a));
373386

374387
// We now have a 2 level tree with 4 nodes:
375388
//
@@ -399,10 +412,10 @@ mod test {
399412
let faulty_accumulator = MerkleAccumulator::<Keccak256> {
400413
root: accumulator.root,
401414
nodes: vec![
402-
accumulator.nodes[0].clone(),
403-
accumulator.nodes[1].clone(), // Root Stays the Same
404-
accumulator.nodes[2].clone(), // Left node hash becomes a leaf.
405-
accumulator.nodes[3].clone(), // Right node hash becomes a leaf.
415+
accumulator.nodes[0],
416+
accumulator.nodes[1], // Root Stays the Same
417+
accumulator.nodes[2], // Left node hash becomes a leaf.
418+
accumulator.nodes[3], // Right node hash becomes a leaf.
406419
],
407420
};
408421

@@ -415,13 +428,13 @@ mod test {
415428
.concat();
416429

417430
// Confirm our combined hash existed as a node pair in the original tree.
418-
assert_eq!(hash_leaf::<Keccak256>(&fake_leaf_A), accumulator.nodes[2]);
431+
assert_eq!(hash_leaf::<Keccak256>(fake_leaf_A), accumulator.nodes[2]);
419432

420433
// Now we can try and prove leaf membership in the faulty accumulator. NOTE: this should
421434
// fail but to confirm that the test is actually correct you can remove the PREFIXES from
422435
// the hash functions and this test will erroneously pass.
423-
let proof = faulty_accumulator.prove(&fake_leaf_A).unwrap();
424-
assert!(faulty_accumulator.check(proof, &fake_leaf_A));
436+
let proof = faulty_accumulator.prove(fake_leaf_A).unwrap();
437+
assert!(faulty_accumulator.check(proof, fake_leaf_A));
425438
}
426439

427440
proptest! {

0 commit comments

Comments
 (0)