Skip to content

Commit

Permalink
Try
Browse files Browse the repository at this point in the history
  • Loading branch information
guibescos committed Jan 23, 2024
1 parent 4abbbd1 commit dbb0cfd
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 29 deletions.
9 changes: 9 additions & 0 deletions pythnet/pythnet_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
114 changes: 114 additions & 0 deletions pythnet/pythnet_sdk/src/gen/mod.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
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::<Keccak160>::new(all_feeds_bytes_refs.as_slice()).unwrap();
let mut price_updates: Vec<MerklePriceUpdate> = 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<MerklePriceUpdate>,
tree: MerkleTree<Keccak160>,
corrupt_wormhole_message: bool,
emitter_address: [u8; 32],
emitter_chain: u16,
) -> Vec<u8> {
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 = <Box<RawMessage>>::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<Box<RawMessage>> {
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: <Box<RawMessage>>::from(vec![]),
}
}
3 changes: 3 additions & 0 deletions pythnet/pythnet_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 45 additions & 5 deletions target_chains/cosmwasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion target_chains/cosmwasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
1 change: 1 addition & 0 deletions target_chains/cosmwasm/contracts/pyth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
25 changes: 2 additions & 23 deletions target_chains/cosmwasm/contracts/pyth/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ mod test {
merkle::MerkleTree,
Accumulator,
},
gen::create_accumulator_message as create_accumulator_message_v1,
messages::{
PriceFeedMessage,
TwapMessage,
Expand Down Expand Up @@ -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::<Keccak160>::new(all_feeds_bytes_refs.as_slice()).unwrap();
let mut price_updates: Vec<MerklePriceUpdate> = 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()
}


Expand Down

0 comments on commit dbb0cfd

Please sign in to comment.