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] Solana tests 2 #1255

Merged
merged 16 commits into from
Jan 30, 2024
33 changes: 33 additions & 0 deletions .github/workflows/ci-solana-contract.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Test Solana Contract

on:
pull_request:
paths:
- target_chains/solana/**
- pythnet/pythnet_sdk/**
push:
branches:
- main
paths:
- target_chains/solana/**
- pythnet/pythnet_sdk/**
guibescos marked this conversation as resolved.
Show resolved Hide resolved

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: target_chains/solana
steps:
- uses: actions/checkout@v2
- name: Install Solana
run: |
sh -c "$(curl -sSfL https://release.solana.com/v1.16.20/install)"
echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
- name: Build
run: cargo-build-sbf
- name: Run tests
run: cargo-test-sbf
4 changes: 3 additions & 1 deletion pythnet/pythnet_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ crate-type = ["lib"]
name = "pythnet_sdk"

[features]
test-utils = ["dep:wormhole-sdk", "dep:serde_wormhole"]
test-utils = ["dep:wormhole-sdk", "dep:serde_wormhole", "dep:libsecp256k1", "dep:rand"]

[dependencies]
bincode = "1.3.1"
Expand All @@ -28,6 +28,8 @@ slow_primes = "0.1.14"
thiserror = "1.0.40"
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", optional = true, tag="rust-sdk-2024-01-25"}
wormhole-sdk = { git = "https://github.com/wormhole-foundation/wormhole", optional = true, tag="rust-sdk-2024-01-25"}
libsecp256k1 = {version ="0.7.1", optional = true}
rand = {version = "0.8.5", optional = true}

[dev-dependencies]
base64 = "0.21.0"
Expand Down
82 changes: 78 additions & 4 deletions pythnet/pythnet_sdk/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@ use {
},
},
byteorder::BigEndian,
libsecp256k1::{
Message as libsecp256k1Message,
RecoveryId,
SecretKey,
Signature,
},
rand::{
seq::SliceRandom,
thread_rng,
},
serde_wormhole::RawMessage,
wormhole_sdk::{
vaa::{
Body,
Header,
},
Address,
Chain,
Vaa,
Expand Down Expand Up @@ -67,6 +81,18 @@ pub const DEFAULT_VALID_TIME_PERIOD: u64 = 180;

const DEFAULT_SEQUENCE: u64 = 2;

const NUM_GUARDIANS: u8 = 19; // Matches wormhole mainnet
const DEFAULT_NUM_SIGNATURES: usize = 13; // Matches wormhole mainnet

pub fn dummy_guardians() -> Vec<SecretKey> {
let mut result: Vec<SecretKey> = vec![];
for i in 0..NUM_GUARDIANS {
let mut secret_key_bytes = [0u8; 32];
secret_key_bytes[0] = i + 1;
result.push(SecretKey::parse(&secret_key_bytes).unwrap());
}
result
}
guibescos marked this conversation as resolved.
Show resolved Hide resolved

pub fn create_dummy_price_feed_message(value: i64) -> Message {
let mut dummy_id = [0; 32];
Expand Down Expand Up @@ -155,12 +181,60 @@ pub fn create_vaa_from_payload(
emitter_chain: Chain,
sequence: u64,
) -> Vaa<Box<RawMessage>> {
let vaa: Vaa<Box<RawMessage>> = Vaa {
emitter_chain: emitter_chain,
emitter_address: emitter_address,
let guardians = dummy_guardians();

let body: Body<Box<RawMessage>> = Body {
emitter_chain,
emitter_address,
sequence,
payload: <Box<RawMessage>>::from(payload.to_vec()),
..Default::default()
};
vaa

let digest = libsecp256k1Message::parse_slice(&body.digest().unwrap().secp256k_hash).unwrap();

let signatures: Vec<(Signature, RecoveryId)> = guardians
.iter()
.map(|x| libsecp256k1::sign(&digest, &x))
.collect();
let wormhole_signatures: Vec<wormhole_sdk::vaa::Signature> = signatures
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding actual signatures to the common sdk

.iter()
.enumerate()
.map(|(i, (x, y))| {
let mut signature = [0u8; 65];
signature[..64].copy_from_slice(&x.serialize());
signature[64] = y.serialize();
wormhole_sdk::vaa::Signature {
index: i as u8,
signature,
}
})
.collect();

let mut wormhole_signatures_subset: Vec<wormhole_sdk::vaa::Signature> = wormhole_signatures
.choose_multiple(&mut thread_rng(), DEFAULT_NUM_SIGNATURES)
.cloned()
.collect();

wormhole_signatures_subset.sort_by(|a, b| a.index.cmp(&b.index));

let header = Header {
version: 1,
signatures: wormhole_signatures_subset,
..Default::default()
};


(header, body).into()
}

pub fn trim_vaa_signatures(vaa: Vaa<&RawMessage>, n: u8) -> Vaa<&RawMessage> {
let mut vaa_copy = vaa.clone();
vaa_copy.signatures = vaa
.signatures
.choose_multiple(&mut thread_rng(), n.into())
.cloned()
.collect();
vaa_copy.signatures.sort_by(|a, b| a.index.cmp(&b.index));
vaa_copy
}
116 changes: 115 additions & 1 deletion target_chains/cosmwasm/Cargo.lock

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

Loading
Loading