From 86df3ef9970d6eafe0032ca71cae6a58b66e7dd9 Mon Sep 17 00:00:00 2001 From: Tom Pointon Date: Wed, 12 Oct 2022 14:47:26 +0100 Subject: [PATCH] Make imports used for tests test only (#342) --- .../sources/batch_price_attestation.move | 10 +- aptos/contracts/sources/deserialize.move | 5 +- .../sources/governance/governance.move | 51 ++++---- aptos/contracts/sources/pyth.move | 121 ++++++++++-------- aptos/contracts/sources/state.move | 3 + 5 files changed, 113 insertions(+), 77 deletions(-) diff --git a/aptos/contracts/sources/batch_price_attestation.move b/aptos/contracts/sources/batch_price_attestation.move index 25100bef..abd26787 100644 --- a/aptos/contracts/sources/batch_price_attestation.move +++ b/aptos/contracts/sources/batch_price_attestation.move @@ -1,17 +1,21 @@ module pyth::batch_price_attestation { use pyth::price_feed::{Self}; - use pyth::price; use pyth::error; - use pyth::i64; use pyth::price_info::{Self, PriceInfo}; use pyth::price_identifier::{Self}; use pyth::price_status; use pyth::deserialize::{Self}; - use aptos_framework::account; use aptos_framework::timestamp; use wormhole::cursor::{Self, Cursor}; use std::vector::{Self}; + #[test_only] + use pyth::price; + #[test_only] + use pyth::i64; + #[test_only] + use aptos_framework::account; + const MAGIC: u64 = 0x50325748; // "P2WH" (Pyth2Wormhole) raw ASCII bytes struct BatchPriceAttestation { diff --git a/aptos/contracts/sources/deserialize.move b/aptos/contracts/sources/deserialize.move index c0a2f62e..0ea94c35 100644 --- a/aptos/contracts/sources/deserialize.move +++ b/aptos/contracts/sources/deserialize.move @@ -2,9 +2,12 @@ module pyth::deserialize { use wormhole::deserialize; use wormhole::u16; use wormhole::u32; - use wormhole::cursor::{Self, Cursor}; + use wormhole::cursor::{Cursor}; use pyth::i64::{Self, I64}; + #[test_only] + use wormhole::cursor::{Self}; + public fun deserialize_vector(cur: &mut Cursor, n: u64): vector { deserialize::deserialize_vector(cur, n) } diff --git a/aptos/contracts/sources/governance/governance.move b/aptos/contracts/sources/governance/governance.move index 46a97300..bdbc1b70 100644 --- a/aptos/contracts/sources/governance/governance.move +++ b/aptos/contracts/sources/governance/governance.move @@ -1,21 +1,16 @@ module pyth::governance { use wormhole::vaa::{Self, VAA}; - use pyth::data_source::{Self, DataSource}; + use pyth::data_source::{Self}; use wormhole::u16; use pyth::governance_instruction; - use pyth::pyth; use pyth::governance_action; use pyth::contract_upgrade; - use pyth::contract_upgrade_hash; use pyth::set_governance_data_source; use pyth::set_data_sources; use pyth::set_stale_price_threshold; use pyth::error; use pyth::set_update_fee; use pyth::state; - use wormhole::external_address; - use std::account; - use std::vector; public entry fun execute_governance_instruction(vaa_bytes: vector) { let parsed_vaa = parse_and_verify_governance_vaa(vaa_bytes); @@ -59,6 +54,18 @@ module pyth::governance { parsed_vaa } +} + +#[test_only] +module pyth::governance_test { + use pyth::data_source::{Self, DataSource}; + use pyth::pyth; + use pyth::governance; + use pyth::contract_upgrade_hash; + use pyth::state; + use wormhole::external_address; + use std::account; + use std::vector; #[test_only] fun setup_test( @@ -82,7 +89,7 @@ module pyth::governance { fun test_execute_governance_instruction_invalid_vaa() { setup_test(50, 24, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100); let vaa_bytes = x"6c436741b108"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); } #[test] @@ -94,7 +101,7 @@ module pyth::governance { // - Emitter chain ID of 20 // - Emitter address of 0xed67fcc21620d1bf9f69db61ea65ea36ae2df4f86c8e1b9503f0da287c24ed41 let vaa_bytes = x"0100000000010066359039306c20c8e6d0047ca82aef1b3d1059a3196ab9b21ee9eb8d8438c4e06c3f181d86687cf52f8c4a167ce8af6a5dbadad22253a4016dc28a25f181a37301527e4f9b000000010014ed67fcc21620d1bf9f69db61ea65ea36ae2df4f86c8e1b9503f0da287c24ed410000000000000000005054474eb01087a85361f738f19454e66664d3c9"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); } #[test] @@ -108,7 +115,7 @@ module pyth::governance { // - Emitter address 0xf06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf // - Sequence number 0 let vaa_bytes = x"010000000001004d7facf7151ada96a35a3f099843c5f13bd0e0a6cbf50722d4e456d370bbce8641ecc16450979d4c403888f9f08d5975503d810732dc95575880d2a4c64d40aa01527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000000005054474eb01087a85361f738f19454e66664d3c9"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); } #[test] @@ -122,7 +129,7 @@ module pyth::governance { // - Sequence number 1 // - A payload with the value x"5054474eb01087a85361f738f19454e66664d3c9", so the magic number will be 5054474e let vaa_bytes = x"01000000000100583334c65aff30780bf7f2ac783398a2a985e3e4873264e46c3cddfdfb2eaa484365e9f4a3ecc14d059ac1cf0a7b6a58075749ad17a3bfd4153d8f45b9084a3500527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000001005054474eb01087a85361f738f19454e66664d3c9"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); } #[test] @@ -137,7 +144,7 @@ module pyth::governance { // - A payload representing a governance instruction with: // - Module number 2 let vaa_bytes = x"010000000001001d9fd73b3fb0fc522eae5eb5bd40ddf68941894495d7cec8c8efdbf462e48715171b5c6d4bbca0c1e3843b3c28d0ca6f3f76874624b5595a3a2cbfdb3907b62501527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000001005054474d0202001003001793a28e2e5b4cb88f69e96fb29a8287a88b23f0e99f5502f81744e904da8e3b4d000c9a4066ce1fa26da1c102a3e268abd3ca58e3b3c25f250e6ad9a3525066fbf8b00012f7778ca023d5cbe37449bab2faa2a133fe02b056c2c25605950320df08750f35"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); } #[test] @@ -153,7 +160,7 @@ module pyth::governance { // - Module number 1 // - Target chain 17 != wormhole test chain ID 22 let vaa_bytes = x"010000000001001ed81e10f8e52e6a7daeca12bf0859c14e8dabed737eaed9a1f8227190a9d11c48d58856713243c5d7de08ed49de4aa1efe7c5e6020c11056802e2d702aa4b2e00527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000001005054474d0102001103001793a28e2e5b4cb88f69e96fb29a8287a88b23f0e99f5502f81744e904da8e3b4d000c9a4066ce1fa26da1c102a3e268abd3ca58e3b3c25f250e6ad9a3525066fbf8b00012f7778ca023d5cbe37449bab2faa2a133fe02b056c2c25605950320df08750f35"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); } #[test] @@ -170,7 +177,7 @@ module pyth::governance { // - Target chain 22 // - Action 19 (invalid) let vaa_bytes = x"0100000000010049fdadd56a51e8bd30637dbf9fc79a154a80c96479ce223061ec1f5094f2908715d6c691e5f06068873daa79c87fc25deb62555db7c520468d05aa2437fda97201527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000001005054474d0113001603001793a28e2e5b4cb88f69e96fb29a8287a88b23f0e99f5502f81744e904da8e3b4d000c9a4066ce1fa26da1c102a3e268abd3ca58e3b3c25f250e6ad9a3525066fbf8b00012f7778ca023d5cbe37449bab2faa2a133fe02b056c2c25605950320df08750f35"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); } #[test] @@ -189,7 +196,7 @@ module pyth::governance { // } let vaa_bytes = x"010000000001002242229aec7d320a437cb241672dacfbc34c9155c02f60cd806bbfcd69bb7ba667fc069e372ae0443a7f3e08eaad61930b00784faeb2b72ecf5d1b0f0fa486a101527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000005005054474d01000016a381a47fd0e97f34c71ef491c82208f58cd0080e784c697e65966d2a25d20d56"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); assert!(state::get_last_executed_governance_sequence() == 5, 1); assert!(state::get_contract_upgrade_authorized_hash() == @@ -213,7 +220,7 @@ module pyth::governance { // } let vaa_bytes = x"01000000000100303c10020c537205ed0322b7ec9d9b296f4e3e12e39ebde985ed4ef4c8f5565256cfc6f90800c4683dba62b577cc994e2ca9135d32b955040b94718cdcb5527600527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000005005054474d01000000a381a47fd0e97f34c71ef491c82208f58cd0080e784c697e65966d2a25d20d56"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); assert!(state::get_last_executed_governance_sequence() == 5, 1); assert!(state::get_contract_upgrade_authorized_hash() == @@ -245,7 +252,7 @@ module pyth::governance { // } let vaa_bytes = x"01000000000100e8ce9e581b64ab7fbe168a0d9f86d1d2220e57947fb0c75174849838104d5fdf39ceb52ca44706bbe2817e6d33dd84ff92dc13ffe024578722178602ffd1775b01527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf000000000000001b005054474d010100160009625bae57728a368652a0ab0a89808de5fffa61d3312f1a27c3e200e99b1f3058000000000000000a"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); // Check that the governance data source and sequence number has been updated correctly assert!(state::get_last_executed_governance_sequence() == 10, 1); @@ -266,7 +273,7 @@ module pyth::governance { // threshold: 900 // } let second_vaa_bytes = x"010000000001008df31b9853fe9f49b1949b66e10795595c37dfc5dede5ea15c1d136cc104843e2048488dfffc3d791ac1c11c71cdb7b73f250b00eb6977cd80e943542142c3a500527e4f9b000000010009625bae57728a368652a0ab0a89808de5fffa61d3312f1a27c3e200e99b1f3058000000000000000f005054474d010400160000000000000384"; - execute_governance_instruction(second_vaa_bytes); + governance::execute_governance_instruction(second_vaa_bytes); assert!(state::get_last_executed_governance_sequence() == 15, 1); assert!(state::get_stale_price_threshold_secs() == 900, 1); @@ -298,7 +305,7 @@ module pyth::governance { // } let vaa_bytes = x"01000000000100e8ce9e581b64ab7fbe168a0d9f86d1d2220e57947fb0c75174849838104d5fdf39ceb52ca44706bbe2817e6d33dd84ff92dc13ffe024578722178602ffd1775b01527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf000000000000001b005054474d010100160009625bae57728a368652a0ab0a89808de5fffa61d3312f1a27c3e200e99b1f3058000000000000000a"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); // Check that the governance data source and sequence number has been updated correctly assert!(state::get_last_executed_governance_sequence() == 10, 1); @@ -313,7 +320,7 @@ module pyth::governance { // - Emitter address 0xf06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf // - Sequence number 30 let second_vaa_bytes = x"010000000001000e2670b14d716673d44f3766684a42a55c49feaf9a38acffb6971ec66fee2a211e7260413ccf4e3de608111dc0b92a131e8c9b8f5e83e6c36d5fc2228e46eb2d01527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf000000000000001e005054474d010400160000000000000384"; - execute_governance_instruction(second_vaa_bytes); + governance::execute_governance_instruction(second_vaa_bytes); } #[test] @@ -335,7 +342,7 @@ module pyth::governance { // } let vaa_bytes = x"010000000001009f843a3359e75940cad00eaec50a1ac075aca3248634576437cfd53d95c2e29859a3a1902a3ef3e0529b434cf63ce96b21e4e6c05204ba62a446371aa132174000527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000001005054474d0103001600000000000000110000000000000003"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); assert!(state::get_last_executed_governance_sequence() == 1, 1); let expected = 17000; @@ -360,7 +367,7 @@ module pyth::governance { // } let vaa_bytes = x"01000000000100e863ad8824f2c2a1695c6b028fa36c5f654b5f3e8d33712032aa3a2197329f3e2c59fc86cc026e6c68608d9e13982f2a22098bbc877ae2b106f6659ea320850a00527e4f9b000000010032f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf0000000000000001005054474d0104001600000000000002f4"; - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); assert!(state::get_last_executed_governance_sequence() == 1, 1); assert!(state::get_stale_price_threshold_secs() == 756, 1); @@ -391,7 +398,7 @@ module pyth::governance { state::set_data_sources(vector[initial_data_source]); // Execute the VAA - execute_governance_instruction(vaa_bytes); + governance::execute_governance_instruction(vaa_bytes); assert!(state::get_last_executed_governance_sequence() == 1, 1); // Check that the data sources have been set correctly diff --git a/aptos/contracts/sources/pyth.move b/aptos/contracts/sources/pyth.move index ec45efa8..a8b11a0f 100644 --- a/aptos/contracts/sources/pyth.move +++ b/aptos/contracts/sources/pyth.move @@ -3,9 +3,8 @@ module pyth::pyth { use pyth::price_identifier::{Self, PriceIdentifier}; use pyth::price_info::{Self, PriceInfo}; use pyth::price_feed::{Self}; - use aptos_framework::coin::{Self, Coin, BurnCapability, MintCapability}; - use aptos_framework::aptos_coin::{Self, AptosCoin}; - use pyth::i64; + use aptos_framework::coin::{Self, Coin}; + use aptos_framework::aptos_coin::{AptosCoin}; use pyth::price::Price; use pyth::price; use pyth::data_source::{Self, DataSource}; @@ -21,6 +20,9 @@ module pyth::pyth { use pyth::error; use pyth::event; + #[test_only] + friend pyth::pyth_test; + // ----------------------------------------------------------------------------- // Initialisation functions @@ -171,7 +173,7 @@ module pyth::pyth { } /// Update the cache with given price updates, if they are newer than the ones currently cached. - fun update_cache(updates: vector) { + public(friend) fun update_cache(updates: vector) { while (!vector::is_empty(&updates)) { let update = vector::pop_back(&mut updates); if (is_fresh_update(&update)) { @@ -365,9 +367,26 @@ module pyth::pyth { public fun get_update_fee(update_data: &vector>): u64 { state::get_base_update_fee() * vector::length(update_data) } +} // ----------------------------------------------------------------------------- // Tests +#[test_only] +module pyth::pyth_test { + use pyth::pyth; + use pyth::price_identifier::{Self}; + use pyth::price_info::{Self, PriceInfo}; + use pyth::price_feed::{Self}; + use aptos_framework::coin::{Self, Coin, BurnCapability, MintCapability}; + use aptos_framework::aptos_coin::{Self, AptosCoin}; + use pyth::i64; + use pyth::price; + use pyth::data_source::{Self, DataSource}; + use aptos_framework::timestamp; + use std::vector; + use wormhole::external_address; + use std::account; + use std::signer; #[test_only] fun setup_test( @@ -388,7 +407,7 @@ module pyth::pyth { let deployer = account::create_signer_with_capability(& account::create_test_signer_cap(@0x277fa055b6a73c42c0662d5236c65c864ccbf2d4abd21f174a30c8b786eab84b)); let (_pyth, signer_capability) = account::create_resource_account(&deployer, b"pyth"); - init_test(signer_capability, stale_price_threshold, governance_emitter_chain_id, governance_emitter_address, data_sources, update_fee); + pyth::init_test(signer_capability, stale_price_threshold, governance_emitter_chain_id, governance_emitter_address, data_sources, update_fee); let (burn_capability, mint_capability) = aptos_coin::initialize_for_test(aptos_framework); let coins = coin::mint(to_mint, &mint_capability); @@ -456,12 +475,12 @@ module pyth::pyth { let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], 50, 0); // Pass in a single VAA - assert!(get_update_fee(&vector[ + assert!(pyth::get_update_fee(&vector[ x"fb1543888001083cf2e6ef3afdcf827e89b11efd87c563638df6e1995ada9f93", ]) == single_update_fee, 1); // Pass in multiple VAAs - assert!(get_update_fee(&vector[ + assert!(pyth::get_update_fee(&vector[ x"4ee17a1a4524118de513fddcf82b77454e51be5d6fc9e29fc72dd6c204c0e4fa", x"c72fdf81cfc939d4286c93fbaaae2eec7bae28a5926fa68646b43a279846ccc1", x"d9a8123a793529c31200339820a3210059ecace6c044f81ecad62936e47ca049", @@ -480,7 +499,7 @@ module pyth::pyth { // Pass in a corrupt VAA, which should fail deseriaizing let corrupt_vaa = x"90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; - update_price_feeds(vector[corrupt_vaa], coins); + pyth::update_price_feeds(vector[corrupt_vaa], coins); cleanup_test(burn_capability, mint_capability); } @@ -497,7 +516,7 @@ module pyth::pyth { ]; let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, 50, 100); - update_price_feeds(TEST_VAAS, coins); + pyth::update_price_feeds(TEST_VAAS, coins); cleanup_test(burn_capability, mint_capability); } @@ -526,7 +545,7 @@ module pyth::pyth { // Coins provided to update < update fee 20); - update_price_feeds(TEST_VAAS, coins); + pyth::update_price_feeds(TEST_VAAS, coins); cleanup_test(burn_capability, mint_capability); } @@ -536,7 +555,7 @@ module pyth::pyth { let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), 50, 100); // Update the price feeds from the VAA - update_price_feeds(TEST_VAAS, coins); + pyth::update_price_feeds(TEST_VAAS, coins); // Check that the cache has been updated let expected = get_mock_price_infos(); @@ -557,21 +576,21 @@ module pyth::pyth { coin::register(&funder); coin::deposit(funder_addr, coins); - assert!(get_update_fee(&TEST_VAAS) == update_fee, 1); + assert!(pyth::get_update_fee(&TEST_VAAS) == update_fee, 1); assert!(coin::balance(signer::address_of(&funder)) == initial_balance, 1); assert!(coin::balance(@pyth) == 0, 1); // Update the price feeds using the funder - update_price_feeds_with_funder(&funder, TEST_VAAS); + pyth::update_price_feeds_with_funder(&funder, TEST_VAAS); // Check that the price feeds are now cached check_price_feeds_cached(&get_mock_price_infos()); // Check that the funder's balance has decreased by the update_fee amount - assert!(coin::balance(signer::address_of(&funder)) == initial_balance - get_update_fee(&TEST_VAAS), 1); + assert!(coin::balance(signer::address_of(&funder)) == initial_balance - pyth::get_update_fee(&TEST_VAAS), 1); // Check that the amount has been transferred to the Pyth contract - assert!(coin::balance(@pyth) == get_update_fee(&TEST_VAAS), 1); + assert!(coin::balance(@pyth) == pyth::get_update_fee(&TEST_VAAS), 1); cleanup_test(burn_capability, mint_capability); } @@ -589,12 +608,12 @@ module pyth::pyth { coin::register(&funder); coin::deposit(funder_addr, coins); - assert!(get_update_fee(&TEST_VAAS) == update_fee, 1); + assert!(pyth::get_update_fee(&TEST_VAAS) == update_fee, 1); assert!(coin::balance(signer::address_of(&funder)) == initial_balance, 1); assert!(coin::balance(@pyth) == 0, 1); // Update the price feeds using the funder - update_price_feeds_with_funder(&funder, TEST_VAAS); + pyth::update_price_feeds_with_funder(&funder, TEST_VAAS); cleanup_test(burn_capability, mint_capability); } @@ -609,13 +628,13 @@ module pyth::pyth { let price = price_feed::get_price(price_feed); let price_identifier = *price_feed::get_price_identifier(price_feed); - assert!(price_feed_exists(price_identifier), 1); - let cached_price = get_price(price_identifier); + assert!(pyth::price_feed_exists(price_identifier), 1); + let cached_price = pyth::get_price(price_identifier); assert!(cached_price == price, 1); let ema_price = price_feed::get_ema_price(price_feed); - let cached_ema_price = get_ema_price(price_identifier); + let cached_ema_price = pyth::get_ema_price(price_identifier); assert!(cached_ema_price == ema_price, 1); @@ -634,12 +653,12 @@ module pyth::pyth { let i = 0; while (i < vector::length(&updates)) { let price_feed = price_info::get_price_feed(vector::borrow(&updates, i)); - assert!(!price_feed_exists(*price_feed::get_price_identifier(price_feed)), 1); + assert!(!pyth::price_feed_exists(*price_feed::get_price_identifier(price_feed)), 1); i = i + 1; }; // Submit the updates - update_cache(updates); + pyth::update_cache(updates); // Check that the price feeds are now cached check_price_feeds_cached(&updates); @@ -666,10 +685,10 @@ module pyth::pyth { ema_price, ) ); - update_cache(vector[update]); + pyth::update_cache(vector[update]); // Check that we can retrieve the current price - assert!(get_price(price_identifier) == price, 1); + assert!(pyth::get_price(price_identifier) == price, 1); // Attempt to update the price with an update older than the current cached one let old_price = price::new(i64::new(1243, true), 9802, i64::new(6, false), timestamp - 200); @@ -683,11 +702,11 @@ module pyth::pyth { old_ema_price, ) ); - update_cache(vector[old_update]); + pyth::update_cache(vector[old_update]); // Confirm that the current price and ema price didn't change - assert!(get_price(price_identifier) == price, 1); - assert!(get_ema_price(price_identifier) == ema_price, 1); + assert!(pyth::get_price(price_identifier) == price, 1); + assert!(pyth::get_ema_price(price_identifier) == ema_price, 1); // Update the cache with a fresh update let fresh_price = price::new(i64::new(4857, true), 9979, i64::new(243, false), timestamp + 200); @@ -701,11 +720,11 @@ module pyth::pyth { fresh_ema_price, ) ); - update_cache(vector[fresh_update]); + pyth::update_cache(vector[fresh_update]); // Confirm that the current price was updated - assert!(get_price(price_identifier) == fresh_price, 1); - assert!(get_ema_price(price_identifier) == fresh_ema_price, 1); + assert!(pyth::get_price(price_identifier) == fresh_price, 1); + assert!(pyth::get_ema_price(price_identifier) == fresh_ema_price, 1); cleanup_test(burn_capability, mint_capability); coin::destroy_zero(coins); @@ -730,19 +749,19 @@ module pyth::pyth { price::new(i64::new(1536, true), 869, i64::new(100, false), 1257212500), ) ); - update_cache(vector[update]); - assert!(get_price(price_identifier) == price, 1); + pyth::update_cache(vector[update]); + assert!(pyth::get_price(price_identifier) == price, 1); // Now advance the clock on the target chain, until the age of the cached update exceeds the // stale_price_threshold. timestamp::update_global_time_for_test_secs(current_timestamp + stale_price_threshold); // Check that we can access the price if we increase the threshold by 1 - assert!(get_price_no_older_than( - price_identifier, get_stale_price_threshold_secs() + 1) == price, 1); + assert!(pyth::get_price_no_older_than( + price_identifier, pyth::get_stale_price_threshold_secs() + 1) == price, 1); // However, retrieving the latest price fails - assert!(get_price(price_identifier) == price, 1); + assert!(pyth::get_price(price_identifier) == price, 1); cleanup_test(burn_capability, mint_capability); coin::destroy_zero(coins); @@ -767,21 +786,21 @@ module pyth::pyth { ema_price, ) ); - update_cache(vector[update]); + pyth::update_cache(vector[update]); // Check that the EMA price has been updated - assert!(get_ema_price(price_identifier) == ema_price, 1); + assert!(pyth::get_ema_price(price_identifier) == ema_price, 1); // Now advance the clock on the target chain, until the age of the cached update exceeds the // stale_price_threshold. timestamp::update_global_time_for_test_secs(current_timestamp + stale_price_threshold); // Check that we can access the EMA price if we increase the threshold by 1 - assert!(get_ema_price_no_older_than( - price_identifier, get_stale_price_threshold_secs() + 1) == ema_price, 1); + assert!(pyth::get_ema_price_no_older_than( + price_identifier, pyth::get_stale_price_threshold_secs() + 1) == ema_price, 1); // However, retrieving the latest EMA price fails - assert!(get_ema_price(price_identifier) == ema_price, 1); + assert!(pyth::get_ema_price(price_identifier) == ema_price, 1); cleanup_test(burn_capability, mint_capability); coin::destroy_zero(coins); @@ -802,7 +821,7 @@ module pyth::pyth { let publish_times = vector[ 734639463 ]; - update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins); + pyth::update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins); cleanup_test(burn_capability, mint_capability); } @@ -822,7 +841,7 @@ module pyth::pyth { let publish_times = vector[ 1663680745, 1663680730, 1663680760, 1663680720 ]; - update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins); + pyth::update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins); // Check that the cache has been updated let expected = get_mock_price_infos(); @@ -843,7 +862,7 @@ module pyth::pyth { coin::register(&funder); coin::deposit(funder_addr, coins); - assert!(get_update_fee(&TEST_VAAS) == update_fee, 1); + assert!(pyth::get_update_fee(&TEST_VAAS) == update_fee, 1); assert!(coin::balance(signer::address_of(&funder)) == initial_balance, 1); assert!(coin::balance(@pyth) == 0, 1); @@ -858,16 +877,16 @@ module pyth::pyth { let publish_times = vector[ 1663680790, 1663680730, 1663680760, 1663680720 ]; - update_price_feeds_if_fresh_with_funder(&funder, bytes, price_identifiers, publish_times); + pyth::update_price_feeds_if_fresh_with_funder(&funder, bytes, price_identifiers, publish_times); // Check that the price feeds are now cached check_price_feeds_cached(&get_mock_price_infos()); // Check that the funder's balance has decreased by the update_fee amount - assert!(coin::balance(signer::address_of(&funder)) == initial_balance - get_update_fee(&TEST_VAAS), 1); + assert!(coin::balance(signer::address_of(&funder)) == initial_balance - pyth::get_update_fee(&TEST_VAAS), 1); // Check that the amount has been transferred to the Pyth contract - assert!(coin::balance(@pyth) == get_update_fee(&TEST_VAAS), 1); + assert!(coin::balance(@pyth) == pyth::get_update_fee(&TEST_VAAS), 1); cleanup_test(burn_capability, mint_capability); } @@ -878,7 +897,7 @@ module pyth::pyth { let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), 50, 50); // First populate the cache - update_cache(get_mock_price_infos()); + pyth::update_cache(get_mock_price_infos()); // Now attempt to update the price feeds with publish_times that are older than those we have cached // This should abort with error::no_fresh_data() @@ -892,7 +911,7 @@ module pyth::pyth { let publish_times = vector[ 67, 35, 26, 64 ]; - update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins); + pyth::update_price_feeds_if_fresh(bytes, price_identifiers, publish_times, coins); cleanup_test(burn_capability, mint_capability); } @@ -910,12 +929,12 @@ module pyth::pyth { coin::register(&funder); coin::deposit(funder_addr, coins); - assert!(get_update_fee(&TEST_VAAS) == update_fee, 1); + assert!(pyth::get_update_fee(&TEST_VAAS) == update_fee, 1); assert!(coin::balance(signer::address_of(&funder)) == initial_balance, 1); assert!(coin::balance(@pyth) == 0, 1); // First populate the cache - update_cache(get_mock_price_infos()); + pyth::update_cache(get_mock_price_infos()); // Now attempt to update the price feeds with publish_times that are older than those we have cached // This should abort with error::no_fresh_data() @@ -929,7 +948,7 @@ module pyth::pyth { let publish_times = vector[ 100, 76, 29, 64 ]; - update_price_feeds_if_fresh_with_funder(&funder, bytes, price_identifiers, publish_times); + pyth::update_price_feeds_if_fresh_with_funder(&funder, bytes, price_identifiers, publish_times); cleanup_test(burn_capability, mint_capability); } diff --git a/aptos/contracts/sources/state.move b/aptos/contracts/sources/state.move index 82e446e5..7bfcb12f 100644 --- a/aptos/contracts/sources/state.move +++ b/aptos/contracts/sources/state.move @@ -17,6 +17,9 @@ module pyth::state { friend pyth::set_stale_price_threshold; friend pyth::set_data_sources; + #[test_only] + friend pyth::governance_test; + /// The valid data sources an attestation VAA can be emitted from struct DataSources has key { sources: Set,