Skip to content

Commit

Permalink
[solana/cosmwasm/near] Rust tests refactor (#1238)
Browse files Browse the repository at this point in the history
* Try

* Near

* revert

* Try to add CI

* Cleanup

* Cleanup

* Fmt

* cleanup

* Fix CI

* Cleanup

* Cleanup

* Cleanup rust toolchains

* Pin to tag

* Rename to test-utils

* Add deps

* Checkpoint

* Checkpoint

* Checkpoint

* Fix wormhole stub

* Checkpoint

* Start debugging

* Green

* More green

* Cleanup

* More cleanup

* Cleanup

* Factor create_dummy_price_feed_message

* Format

* Cleanup imports

* Cleanup

* Near cleanup:

* Format

* Refactor constants

* cleanup

* More refactor

* Cleanup more magic numbers

* Remove magic numbers

* Cleanup

* Format

* Remove magic numnber
  • Loading branch information
guibescos authored Jan 25, 2024
1 parent 82cd11c commit 9697fad
Show file tree
Hide file tree
Showing 10 changed files with 732 additions and 818 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]
test-utils = ["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, tag="v2.23.37"}
wormhole-sdk = { git = "https://github.com/wormhole-foundation/wormhole", optional = true, tag="v2.23.37"}

[patch.crates-io]
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag="v2.23.37"}


[dev-dependencies]
base64 = "0.21.0"
Expand Down
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 = "test-utils")]
pub mod test_utils;

pub(crate) type Pubkey = [u8; 32];

/// Official Message Buffer Program Id
Expand Down
166 changes: 166 additions & 0 deletions pythnet/pythnet_sdk/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
use {
crate::{
accumulators::{
merkle::MerkleTree,
Accumulator,
},
hashers::keccak256_160::Keccak160,
messages::{
Message,
PriceFeedMessage,
},
wire::{
to_vec,
v1::{
AccumulatorUpdateData,
MerklePriceUpdate,
Proof,
WormholeMerkleRoot,
WormholeMessage,
WormholePayload,
},
PrefixedVec,
},
},
byteorder::BigEndian,
serde_wormhole::RawMessage,
wormhole_sdk::{
Address,
Chain,
Vaa,
},
};

pub struct DataSource {
pub address: Address,
pub chain: Chain,
}

pub const DEFAULT_DATA_SOURCE: DataSource = DataSource {
address: Address([1u8; 32]),
chain: Chain::Solana,
};

pub const DEFAULT_GOVERNANCE_SOURCE: DataSource = DataSource {
address: Address([2u8; 32]),
chain: Chain::Ethereum,
};

pub const WRONG_SOURCE: DataSource = DataSource {
address: Address([3u8; 32]),
chain: Chain::Bsc,
};

pub const SECONDARY_DATA_SOURCE: DataSource = DataSource {
address: Address([4u8; 32]),
chain: Chain::Polygon,
};

pub const SECONDARY_GOVERNANCE_SOURCE: DataSource = DataSource {
address: Address([5u8; 32]),
chain: Chain::Avalanche,
};

pub const DEFAULT_CHAIN_ID: Chain = Chain::Oasis;
pub const WRONG_CHAIN_ID: Chain = Chain::Algorand;
pub const DEFAULT_VALID_TIME_PERIOD: u64 = 180;

const DEFAULT_SEQUENCE: u64 = 2;


pub fn create_dummy_price_feed_message(value: i64) -> Message {
let mut dummy_id = [0; 32];
dummy_id[0] = value as u8;
let msg = PriceFeedMessage {
feed_id: dummy_id,
price: value,
conf: value as u64,
exponent: value as i32,
publish_time: value,
prev_publish_time: value,
ema_price: value,
ema_conf: value as u64,
};
Message::PriceFeedMessage(msg)
}

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_DATA_SOURCE.address,
DEFAULT_DATA_SOURCE.chain,
)
}

pub fn create_accumulator_message_from_updates(
price_updates: Vec<MerklePriceUpdate>,
tree: MerkleTree<Keccak160>,
corrupt_wormhole_message: bool,
emitter_address: Address,
emitter_chain: Chain,
) -> 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_payload = to_vec::<_, BigEndian>(&wormhole_message).unwrap();
if corrupt_wormhole_message {
vaa_payload[0] = 0;
}

let vaa = create_vaa_from_payload(
&vaa_payload,
emitter_address,
emitter_chain,
DEFAULT_SEQUENCE,
);

let accumulator_update_data = AccumulatorUpdateData::new(Proof::WormholeMerkle {
vaa: PrefixedVec::from(serde_wormhole::to_vec(&vaa).unwrap()),
updates: price_updates,
});

to_vec::<_, BigEndian>(&accumulator_update_data).unwrap()
}

pub fn create_vaa_from_payload(
payload: &[u8],
emitter_address: Address,
emitter_chain: Chain,
sequence: u64,
) -> Vaa<Box<RawMessage>> {
let vaa: Vaa<Box<RawMessage>> = Vaa {
emitter_chain: emitter_chain,
emitter_address: emitter_address,
sequence,
payload: <Box<RawMessage>>::from(payload.to_vec()),
..Default::default()
};
vaa
}
47 changes: 47 additions & 0 deletions target_chains/cosmwasm/Cargo.lock

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

3 changes: 3 additions & 0 deletions target_chains/cosmwasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true

[patch.crates-io]
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag="v2.23.37"}
3 changes: 3 additions & 0 deletions target_chains/cosmwasm/contracts/pyth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ wormhole-cosmwasm = {git = "https://github.com/wormhole-foundation/wormhole", t
[dev-dependencies]
cosmwasm-vm = { version = "1.0.0", default-features = false }
serde_json = "1.0"
pythnet-sdk = { path = "../../../../pythnet/pythnet_sdk", features = ["test-utils"] }
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag="v2.23.37"}
wormhole-sdk = { git = "https://github.com/wormhole-foundation/wormhole", tag="v2.23.37"}
Loading

0 comments on commit 9697fad

Please sign in to comment.