From dbb0cfd455e472a7a42a4320d54a6ca8e891e5e0 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Tue, 23 Jan 2024 19:36:25 +0000 Subject: [PATCH] Try --- pythnet/pythnet_sdk/Cargo.toml | 9 ++ pythnet/pythnet_sdk/src/gen/mod.rs | 114 ++++++++++++++++++ pythnet/pythnet_sdk/src/lib.rs | 3 + target_chains/cosmwasm/Cargo.lock | 50 +++++++- target_chains/cosmwasm/Cargo.toml | 2 +- .../cosmwasm/contracts/pyth/Cargo.toml | 1 + .../cosmwasm/contracts/pyth/src/contract.rs | 25 +--- 7 files changed, 175 insertions(+), 29 deletions(-) create mode 100644 pythnet/pythnet_sdk/src/gen/mod.rs diff --git a/pythnet/pythnet_sdk/Cargo.toml b/pythnet/pythnet_sdk/Cargo.toml index cd47cc933..179e27cc4 100644 --- a/pythnet/pythnet_sdk/Cargo.toml +++ b/pythnet/pythnet_sdk/Cargo.toml @@ -10,6 +10,9 @@ edition = "2021" crate-type = ["lib"] name = "pythnet_sdk" +[features] +gen = ["dep:wormhole-sdk", "dep:serde_wormhole"] + [dependencies] bincode = "1.3.1" borsh = "0.10.3" @@ -23,6 +26,12 @@ quickcheck = { version = "1", optional = true} sha3 = "0.10.4" slow_primes = "0.1.14" thiserror = "1.0.40" +serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", optional = true } +wormhole-sdk = { git = "https://github.com/wormhole-foundation/wormhole", optional = true } + +[patch.crates-io] +serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole"} + [dev-dependencies] base64 = "0.21.0" diff --git a/pythnet/pythnet_sdk/src/gen/mod.rs b/pythnet/pythnet_sdk/src/gen/mod.rs new file mode 100644 index 000000000..f3fc0d3b4 --- /dev/null +++ b/pythnet/pythnet_sdk/src/gen/mod.rs @@ -0,0 +1,114 @@ +use { + crate::{ + accumulators::{ + merkle::MerkleTree, + Accumulator, + }, + hashers::keccak256_160::Keccak160, + messages::Message, + wire::{ + to_vec, + v1::{ + AccumulatorUpdateData, + MerklePriceUpdate, + Proof, + WormholeMerkleRoot, + WormholeMessage, + WormholePayload, + }, + PrefixedVec, + }, + }, + byteorder::BigEndian, + serde_wormhole::RawMessage, + wormhole_sdk::{ + Address, + Chain, + Vaa, + }, +}; + +fn default_emitter_addr() -> [u8; 32] { + [0u8; 32] +} +const EMITTER_CHAIN: u16 = 3; + +pub fn create_accumulator_message( + all_feeds: &[Message], + updates: &[Message], + corrupt_wormhole_message: bool, +) -> Vec { + let all_feeds_bytes: Vec<_> = all_feeds + .iter() + .map(|f| to_vec::<_, BigEndian>(f).unwrap()) + .collect(); + let all_feeds_bytes_refs: Vec<_> = all_feeds_bytes.iter().map(|f| f.as_ref()).collect(); + let tree = MerkleTree::::new(all_feeds_bytes_refs.as_slice()).unwrap(); + let mut price_updates: Vec = vec![]; + for update in updates { + let proof = tree + .prove(&to_vec::<_, BigEndian>(update).unwrap()) + .unwrap(); + price_updates.push(MerklePriceUpdate { + message: PrefixedVec::from(to_vec::<_, BigEndian>(update).unwrap()), + proof, + }); + } + create_accumulator_message_from_updates( + price_updates, + tree, + corrupt_wormhole_message, + default_emitter_addr(), + EMITTER_CHAIN, + ) +} + +fn create_accumulator_message_from_updates( + price_updates: Vec, + tree: MerkleTree, + corrupt_wormhole_message: bool, + emitter_address: [u8; 32], + emitter_chain: u16, +) -> Vec { + let mut root_hash = [0u8; 20]; + root_hash.copy_from_slice(&to_vec::<_, BigEndian>(&tree.root).unwrap()[..20]); + let wormhole_message = WormholeMessage::new(WormholePayload::Merkle(WormholeMerkleRoot { + slot: 0, + ring_size: 0, + root: root_hash, + })); + + let mut vaa = create_zero_vaa(); + vaa.emitter_address = Address(emitter_address); + vaa.emitter_chain = Chain::from(emitter_chain); + let mut payload = to_vec::<_, BigEndian>(&wormhole_message).unwrap(); + + if corrupt_wormhole_message { + payload[0] = 0; + } + vaa.payload = >::from(payload); + + + let vaa_binary = serde_wormhole::to_vec(&vaa).unwrap(); + let accumulator_update_data = AccumulatorUpdateData::new(Proof::WormholeMerkle { + vaa: PrefixedVec::from(vaa_binary.to_vec()), + updates: price_updates, + }); + + to_vec::<_, BigEndian>(&accumulator_update_data).unwrap() +} + +fn create_zero_vaa() -> Vaa> { + Vaa { + version: 1, + guardian_set_index: 0, + signatures: vec![], + timestamp: 0, + nonce: 0, + emitter_chain: Chain::Any, + emitter_address: Address([0u8; 32]), + sequence: 0, + consistency_level: 0, + payload: >::from(vec![]), + } +} diff --git a/pythnet/pythnet_sdk/src/lib.rs b/pythnet/pythnet_sdk/src/lib.rs index c30a316c7..eeaec70e6 100644 --- a/pythnet/pythnet_sdk/src/lib.rs +++ b/pythnet/pythnet_sdk/src/lib.rs @@ -5,6 +5,9 @@ pub mod messages; pub mod wire; pub mod wormhole; +#[cfg(feature = "gen")] +pub mod gen; + pub(crate) type Pubkey = [u8; 32]; /// Official Message Buffer Program Id diff --git a/target_chains/cosmwasm/Cargo.lock b/target_chains/cosmwasm/Cargo.lock index 975f23674..b64f13132 100644 --- a/target_chains/cosmwasm/Cargo.lock +++ b/target_chains/cosmwasm/Cargo.lock @@ -213,6 +213,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "bstr" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6798148dccfbff0fae41c7574d2fa8f1ef3492fba0face179de5d8d447d67b05" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "bumpalo" version = "3.12.0" @@ -1545,9 +1556,11 @@ dependencies = [ "hex", "rustc_version", "serde", + "serde_wormhole", "sha3 0.10.8", "slow_primes", "thiserror", + "wormhole-sdk", ] [[package]] @@ -1616,6 +1629,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "regex-automata" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9" + [[package]] name = "region" version = "3.0.0" @@ -1856,6 +1875,18 @@ dependencies = [ "syn 2.0.15", ] +[[package]] +name = "serde_wormhole" +version = "0.1.0" +source = "git+https://github.com/wormhole-foundation/wormhole#ded305ffab3ac6b55fe85cb0431626a176baf8bb" +dependencies = [ + "base64", + "itoa", + "serde", + "serde_bytes", + "thiserror", +] + [[package]] name = "sha2" version = "0.9.9" @@ -2644,13 +2675,22 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +[[package]] +name = "wormhole-sdk" +version = "0.1.0" +source = "git+https://github.com/wormhole-foundation/wormhole#ded305ffab3ac6b55fe85cb0431626a176baf8bb" +dependencies = [ + "anyhow", + "bstr", + "schemars", + "serde", + "serde_wormhole", + "sha3 0.10.8", + "thiserror", +] + [[package]] name = "zeroize" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" - -[[patch.unused]] -name = "cw20-wrapped-2" -version = "0.1.0" -source = "git+https://github.com/wormhole-foundation/wormhole?tag=v2.14.8#7e982cb03264cf1cccfbb5d947c00d6ad3e2f8f1" diff --git a/target_chains/cosmwasm/Cargo.toml b/target_chains/cosmwasm/Cargo.toml index c2b1345f5..f22a764e5 100644 --- a/target_chains/cosmwasm/Cargo.toml +++ b/target_chains/cosmwasm/Cargo.toml @@ -14,4 +14,4 @@ incremental = false overflow-checks = true [patch.crates-io] -cw20-wrapped-2 = { git = "https://github.com/wormhole-foundation/wormhole", tag = "v2.14.8"} +serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole" } diff --git a/target_chains/cosmwasm/contracts/pyth/Cargo.toml b/target_chains/cosmwasm/contracts/pyth/Cargo.toml index 7d716d5e4..103693fd7 100644 --- a/target_chains/cosmwasm/contracts/pyth/Cargo.toml +++ b/target_chains/cosmwasm/contracts/pyth/Cargo.toml @@ -44,3 +44,4 @@ pythnet-sdk = { path = "../../../../pythnet/pythnet_sdk" } [dev-dependencies] cosmwasm-vm = { version = "1.0.0", default-features = false } serde_json = "1.0" +pythnet-sdk = { path = "../../../../pythnet/pythnet_sdk", features = ["gen"] } diff --git a/target_chains/cosmwasm/contracts/pyth/src/contract.rs b/target_chains/cosmwasm/contracts/pyth/src/contract.rs index 218ac4b47..b21e1e9a9 100644 --- a/target_chains/cosmwasm/contracts/pyth/src/contract.rs +++ b/target_chains/cosmwasm/contracts/pyth/src/contract.rs @@ -831,6 +831,7 @@ mod test { merkle::MerkleTree, Accumulator, }, + gen::create_accumulator_message as create_accumulator_message_v1, messages::{ PriceFeedMessage, TwapMessage, @@ -1235,29 +1236,7 @@ mod test { updates: &[Message], corrupt_wormhole_message: bool, ) -> Binary { - let all_feeds_bytes: Vec<_> = all_feeds - .iter() - .map(|f| to_vec::<_, BigEndian>(f).unwrap()) - .collect(); - let all_feeds_bytes_refs: Vec<_> = all_feeds_bytes.iter().map(|f| f.as_ref()).collect(); - let tree = MerkleTree::::new(all_feeds_bytes_refs.as_slice()).unwrap(); - let mut price_updates: Vec = vec![]; - for update in updates { - let proof = tree - .prove(&to_vec::<_, BigEndian>(update).unwrap()) - .unwrap(); - price_updates.push(MerklePriceUpdate { - message: PrefixedVec::from(to_vec::<_, BigEndian>(update).unwrap()), - proof, - }); - } - create_accumulator_message_from_updates( - price_updates, - tree, - corrupt_wormhole_message, - default_emitter_addr(), - EMITTER_CHAIN, - ) + create_accumulator_message_v1(all_feeds, updates, corrupt_wormhole_message).into() }