Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[solana/cosmwasm/near] Rust tests refactor #1238

Merged
merged 41 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e6954be
Try
guibescos Jan 23, 2024
530d752
Near
guibescos Jan 23, 2024
23d7a99
revert
guibescos Jan 23, 2024
d36b990
Try to add CI
guibescos Jan 23, 2024
9f166eb
Cleanup
guibescos Jan 23, 2024
cce1258
Cleanup
guibescos Jan 23, 2024
7511724
Fmt
guibescos Jan 23, 2024
f122c42
cleanup
guibescos Jan 23, 2024
e3e7b2e
Fix CI
guibescos Jan 23, 2024
9f248c5
Cleanup
guibescos Jan 23, 2024
74fcdce
Cleanup
guibescos Jan 23, 2024
b37532b
Cleanup rust toolchains
guibescos Jan 23, 2024
00d1378
Pin to tag
guibescos Jan 24, 2024
f1345f6
Rename to test-utils
guibescos Jan 24, 2024
117fbd0
Add deps
guibescos Jan 24, 2024
995d2fe
Checkpoint
guibescos Jan 24, 2024
6623c6d
Checkpoint
guibescos Jan 24, 2024
13ba4d9
Checkpoint
guibescos Jan 24, 2024
11fa9fd
Fix wormhole stub
guibescos Jan 24, 2024
f591ec5
Checkpoint
guibescos Jan 24, 2024
4e38c4e
Start debugging
guibescos Jan 24, 2024
559cec4
Green
guibescos Jan 24, 2024
ed2a6f1
More green
guibescos Jan 24, 2024
7bbea16
Cleanup
guibescos Jan 24, 2024
77b63db
More cleanup
guibescos Jan 24, 2024
a3a65cc
Cleanup
guibescos Jan 24, 2024
611eb5e
Merge branch 'main' into rust-tests-refactor
guibescos Jan 25, 2024
9cf784a
Factor create_dummy_price_feed_message
guibescos Jan 25, 2024
8fa2607
Format
guibescos Jan 25, 2024
9265b8b
Cleanup imports
guibescos Jan 25, 2024
927c393
Cleanup
guibescos Jan 25, 2024
ba2325d
Near cleanup:
guibescos Jan 25, 2024
bf00645
Format
guibescos Jan 25, 2024
9ad96b6
Refactor constants
guibescos Jan 25, 2024
d8a8f75
cleanup
guibescos Jan 25, 2024
7b649e9
More refactor
guibescos Jan 25, 2024
bd5d947
Cleanup more magic numbers
guibescos Jan 25, 2024
a2dabb9
Remove magic numbers
guibescos Jan 25, 2024
40d7b6d
Cleanup
guibescos Jan 25, 2024
a5c137e
Format
guibescos Jan 25, 2024
0064135
Remove magic numnber
guibescos Jan 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/ci-near-contract.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test Near Contract

on:
pull_request:
paths:
- target_chains/near/**
- wormhole_attester/sdk/rust/**
push:
branches:
- main
paths:
- target_chains/near/**
- wormhole_attester/sdk/rust/**

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: target_chains/near/receiver
steps:
- uses: actions/checkout@v2
- name: Install nextest
run: curl -LsSf https://get.nexte.st/latest/linux | tar zxf - -C ${CARGO_HOME:-~/.cargo}/bin
- name: Test
run: ./workspace-test.sh
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
133 changes: 133 additions & 0 deletions pythnet/pythnet_sdk/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
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,
std::io::{
Cursor,
Write,
},
wormhole_sdk::{
Address,
Chain,
Vaa,
},
};

pub const EMITTER_CHAIN: u16 = 0;
pub const DEFAULT_SEQUENCE: u64 = 2;

pub fn default_emitter_addr() -> [u8; 32] {
[1u8; 32]
}

pub fn default_governance_addr() -> [u8; 32] {
[2u8; 32]
}

pub fn default_new_governance_addr() -> [u8; 32] {
[3u8; 32]
}

pub fn default_wrong_emitter_addr() -> [u8; 32] {
[4u8; 32]
}

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,
)
}

pub 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_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: [u8; 32],
emitter_chain: u16,
sequence: u64,
) -> Vaa<Box<RawMessage>> {
let vaa: Vaa<Box<RawMessage>> = Vaa {
emitter_chain: Chain::from(emitter_chain),
emitter_address: 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
Loading