From a7383a3648046682e467eb29dddaafa5057fa6c5 Mon Sep 17 00:00:00 2001 From: optke3 <108488464+optke3@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:09:50 -0500 Subject: [PATCH] [sui 11/x] - pyth merkle accumulator (#910) * merkle tree impl * - take leftmost 20 bytes in hash - don't assign output of cursor::take_rest to _, instead just drop it * push PREFIXes (MERKLE_LEAF_PREFIX, MERKLE_NODE_PREFIX) to front instead of back * delete testXOR * test construct merkle tree depth exceeded error * invalid merkle proof test cases * comments * rename failure tests * simplification for initializing a vector * fix leafHash bug, add tests for hashLeaf and hashNode * pyth accumulator start, extract_price_info_from_merkle_proof, parse_price_feed_message * parse_price_feed_message, parse_and_verify_accumulator_updates * implementation + debugging for merkle pyth accumulator * edit merkle tree * testNodehash * test hash * delete prints * test case for parse and verify TEST_ACCUMULATOR_3_MSGS * hot potato vector -> authenticated price infos * refactor - move tests from pyth_accumulator to pyth to avoid dependency cycle * remove _ from deserializing unused vaa * add sui-contract.yml for github actions * AuthenticatedPriceInfos -> AuthenticatedVector * charge base update fee per call to update_single_price_feed * add back multiple tests, including test_create_and_update_price_feeds_insufficient_fee, update cache, update cache old update * test multiple price feed creation and update accumulator * authenticated_price_infos.move -> authenticated_vector.move * 5 * single_update_fee * delete some comments, add accumulator test info * don't make TEST_VAAS test_only in pyth.move * remove #[test_only]s * assert price info object contains correct price feed info * factor out some constants from accumulator test cases to reduce duplicate code * add sui-contract.yml file for github actions CI * more refactor and clean-up * assert price_info_object_1 is correct in test_create_and_update_price_feeds_with_batch_attestation_success * removed the parse_and_verify_accumulator_message_with_worm_state entirely, and instead added the helper parse_vaa_bytes_from_accumulator_message * edit comment * update comment * edit sui github ci * fix for sui-contract.yml * MINIMUM_SUPPORTED_MINOR_VERSION and MAJOR_VERSION * remove test_get_price_feed_updates_from_accumulator and parse_vaa_bytes_from_accumulator_message from pyth_accumulator.move * test_parse_and_verify_accumulator_updates_with_extra_bytes_at_end_of_message * sui contract yml update * use rev to cargo install sui in github actions ci * cargo install --locked for github CI --- .github/workflows/sui-contract.yml | 30 + .../sources/authenticated_vector.move | 65 + .../contracts/sources/hot_potato_vector.move | 65 - .../sui/contracts/sources/merkle_tree.move | 10 +- .../sui/contracts/sources/price_status.move | 2 +- target_chains/sui/contracts/sources/pyth.move | 1317 +++++++++++------ .../contracts/sources/pyth_accumulator.move | 116 ++ .../sui/contracts/sources/state.move | 5 + 8 files changed, 1118 insertions(+), 492 deletions(-) create mode 100644 .github/workflows/sui-contract.yml create mode 100644 target_chains/sui/contracts/sources/authenticated_vector.move delete mode 100644 target_chains/sui/contracts/sources/hot_potato_vector.move create mode 100644 target_chains/sui/contracts/sources/pyth_accumulator.move diff --git a/.github/workflows/sui-contract.yml b/.github/workflows/sui-contract.yml new file mode 100644 index 00000000..ec6741af --- /dev/null +++ b/.github/workflows/sui-contract.yml @@ -0,0 +1,30 @@ +on: + pull_request: + paths: + - target_chains/sui/contracts/** + push: + branches: + - main + paths: + - target_chains/sui/contracts/** + +name: Sui Contracts + +jobs: + sui-tests: + name: Sui tests + runs-on: ubuntu-latest + defaults: + run: + working-directory: target_chains/sui/contracts/ + steps: + - uses: actions/checkout@v3 + + - name: Update rust + run: rustup update stable + + - name: Install Sui CLI + run: cargo install --locked --git https://github.com/MystenLabs/sui.git --rev 09b2081498366df936abae26eea4b2d5cafb2788 sui + + - name: Run tests + run: sui move test diff --git a/target_chains/sui/contracts/sources/authenticated_vector.move b/target_chains/sui/contracts/sources/authenticated_vector.move new file mode 100644 index 00000000..3fecd1e3 --- /dev/null +++ b/target_chains/sui/contracts/sources/authenticated_vector.move @@ -0,0 +1,65 @@ +/// This class represents a collection of objects wrapped inside of a struct +/// called AuthenticatedVector. Its constructor is non-public and can only be called +/// by friend modules, making the creation of new AuthenticatedVector protected. +module pyth::authenticated_vector { + use std::vector; + + friend pyth::pyth; + + // A vector of elements + struct AuthenticatedVector has drop { + contents: vector + } + + // A public destroy function. + public fun destroy(vec: AuthenticatedVector){ + let AuthenticatedVector {contents: _} = vec; + } + + // Only certain on-chain functions are allowed to create a new hot potato vector. + public(friend) fun new(vec: vector): AuthenticatedVector{ + AuthenticatedVector { + contents: vec + } + } + + public fun length(vec: &AuthenticatedVector): u64 { + vector::length(&vec.contents) + } + + public fun is_empty(vec: &AuthenticatedVector): bool { + vector::is_empty(&vec.contents) + } + + public fun borrow(vec: &AuthenticatedVector, i: u64): &T { + vector::borrow(&vec.contents, i) + } + + public(friend) fun pop_back(vec: AuthenticatedVector): (T, AuthenticatedVector){ + let elem = vector::pop_back(&mut vec.contents); + return (elem, vec) + } + + #[test_only] + struct A has copy, drop { + a : u64 + } + + #[test] + fun test_authenticated_vector(){ + let vec_of_a = vector::empty(); + vector::push_back(&mut vec_of_a, A{a:5}); + vector::push_back(&mut vec_of_a, A{a:11}); + vector::push_back(&mut vec_of_a, A{a:23}); + + let vec = new(vec_of_a); + let (b, vec) = pop_back(vec); + assert!(b.a==23, 0); + (b, vec) = pop_back(vec); + assert!(b.a==11, 0); + let (b, vec) = pop_back(vec); + assert!(b.a==5, 0); + + destroy(vec); + } +} diff --git a/target_chains/sui/contracts/sources/hot_potato_vector.move b/target_chains/sui/contracts/sources/hot_potato_vector.move deleted file mode 100644 index 5bcb39bd..00000000 --- a/target_chains/sui/contracts/sources/hot_potato_vector.move +++ /dev/null @@ -1,65 +0,0 @@ -/// This class represents a vector of objects wrapped -/// inside of a hot potato struct. -module pyth::hot_potato_vector { - use std::vector; - const E_EMPTY_HOT_POTATO: u64 = 0; - - friend pyth::pyth; - - // A hot potato containing a vector of elements - struct HotPotatoVector { - contents: vector - } - - // A public destroy function. - public fun destroy(hot_potato_vector: HotPotatoVector){ - let HotPotatoVector {contents: _} = hot_potato_vector; - } - - // Only certain on-chain functions are allowed to create a new hot potato vector. - public(friend) fun new(vec: vector): HotPotatoVector{ - HotPotatoVector { - contents: vec - } - } - - public fun length(potato: &HotPotatoVector): u64 { - vector::length(&potato.contents) - } - - public fun is_empty(potato: &HotPotatoVector): bool { - vector::is_empty(&potato.contents) - } - - public fun borrow(potato: &HotPotatoVector, i: u64): &T { - vector::borrow(&potato.contents, i) - } - - public(friend) fun pop_back(hot_potato_vector: HotPotatoVector): (T, HotPotatoVector){ - let elem = vector::pop_back(&mut hot_potato_vector.contents); - return (elem, hot_potato_vector) - } - - #[test_only] - struct A has copy, drop { - a : u64 - } - - #[test] - fun test_hot_potato_vector(){ - let vec_of_a = vector::empty(); - vector::push_back(&mut vec_of_a, A{a:5}); - vector::push_back(&mut vec_of_a, A{a:11}); - vector::push_back(&mut vec_of_a, A{a:23}); - - let hot_potato = new(vec_of_a); - let (b, hot_potato) = pop_back(hot_potato); - assert!(b.a==23, 0); - (b, hot_potato) = pop_back(hot_potato); - assert!(b.a==11, 0); - let (b, hot_potato) = pop_back(hot_potato); - assert!(b.a==5, 0); - - destroy(hot_potato); - } -} diff --git a/target_chains/sui/contracts/sources/merkle_tree.move b/target_chains/sui/contracts/sources/merkle_tree.move index 53529af0..742b2037 100644 --- a/target_chains/sui/contracts/sources/merkle_tree.move +++ b/target_chains/sui/contracts/sources/merkle_tree.move @@ -80,16 +80,14 @@ module pyth::merkle_tree { } // isProofValid returns whether a merkle proof is valid - fun isProofValid( + public fun isProofValid( encodedProof: &mut Cursor, root: Bytes20, leafData: vector, ): bool { - let currentDigest: Bytes20 = leafHash(&leafData); let proofSize: u8 = deserialize::deserialize_u8(encodedProof); - let i: u8 = 0; - while (i < proofSize){ + while (proofSize > 0){ let siblingDigest: Bytes20 = bytes20::new( deserialize::deserialize_vector(encodedProof, 20) ); @@ -98,14 +96,14 @@ module pyth::merkle_tree { currentDigest, siblingDigest ); - i = i + 1; + proofSize = proofSize - 1; }; bytes20::data(¤tDigest) == bytes20::data(&root) } // constructProofs constructs a merkle tree and returns the root of the tree as // a Bytes20 as well as the vector of encoded proofs - fun constructProofs( + public fun constructProofs( messages: &vector>, depth: u8 ) : (Bytes20, vector) { diff --git a/target_chains/sui/contracts/sources/price_status.move b/target_chains/sui/contracts/sources/price_status.move index 4d976c97..01a1bd0e 100644 --- a/target_chains/sui/contracts/sources/price_status.move +++ b/target_chains/sui/contracts/sources/price_status.move @@ -13,7 +13,7 @@ module pyth::price_status { } public fun from_u64(status: u64): PriceStatus { - assert!(status <= TRADING, 0); // error::invalid_price_status() + assert!(status <= TRADING, 0); PriceStatus { status: status } diff --git a/target_chains/sui/contracts/sources/pyth.move b/target_chains/sui/contracts/sources/pyth.move index 64d10600..98d618eb 100644 --- a/target_chains/sui/contracts/sources/pyth.move +++ b/target_chains/sui/contracts/sources/pyth.move @@ -16,11 +16,14 @@ module pyth::pyth { use pyth::price::{Self, Price}; use pyth::price_identifier::{PriceIdentifier}; use pyth::setup::{Self, DeployerCap}; - use pyth::hot_potato_vector::{Self, HotPotatoVector}; + use pyth::authenticated_vector::{Self, AuthenticatedVector}; + use pyth::accumulator::{Self}; + use pyth::deserialize::{Self}; use wormhole::external_address::{Self}; use wormhole::vaa::{Self, VAA}; use wormhole::bytes32::{Self}; + use wormhole::cursor::{Self}; const E_DATA_SOURCE_EMITTER_ADDRESS_AND_CHAIN_IDS_DIFFERENT_LENGTHS: u64 = 0; const E_INVALID_DATA_SOURCE: u64 = 1; @@ -28,6 +31,10 @@ module pyth::pyth { const E_STALE_PRICE_UPDATE: u64 = 3; const E_UPDATE_AND_PRICE_INFO_OBJECT_MISMATCH: u64 = 4; const E_PRICE_UPDATE_NOT_FOUND_FOR_PRICE_INFO_OBJECT: u64 = 5; + const E_INVALID_ACCUMULATOR_HEADER: u64 = 6; + const E_INVALID_ACCUMULATOR_MAGIC: u64 = 7; + + const PYTHNET_ACCUMULATOR_UPDATE_MAGIC: u64 = 1347305813; #[test_only] friend pyth::pyth_tests; @@ -85,8 +92,47 @@ module pyth::pyth { sources } - /// Create and share new price feed objects if they don't already exist. - public fun create_price_feeds( + /// Create and share new price feed objects if they don't already exist using accumulator message. + public fun create_price_feeds_using_accumulator( + pyth_state: &mut PythState, + accumulator_message: vector, + vaa: VAA, // the verified version of the vaa bytes encoded within the accumulator_message + clock: &Clock, + ctx: &mut TxContext + ){ + // This capability ensures that the current build version is used. + let latest_only = state::assert_latest_only(pyth_state); + + // Check that the VAA is from a valid data source (emitter) + assert!( + state::is_valid_data_source( + pyth_state, + data_source::new( + (vaa::emitter_chain(&vaa) as u64), + vaa::emitter_address(&vaa)) + ), + E_INVALID_DATA_SOURCE + ); + + // decode the price info updates from the VAA payload (first check if it is an accumulator or batch price update) + let accumulator_message_cursor = cursor::new(accumulator_message); + let header = deserialize::deserialize_u32(&mut accumulator_message_cursor); + + if ((header as u64) != PYTHNET_ACCUMULATOR_UPDATE_MAGIC) { + abort E_INVALID_ACCUMULATOR_HEADER + }; + let price_infos = accumulator::parse_and_verify_accumulator_message(&mut accumulator_message_cursor, vaa::take_payload(vaa), clock); + + // Create and share new price info objects, if not already exists. + create_and_share_price_feeds_using_verified_price_infos(&latest_only, pyth_state, price_infos, ctx); + + // destroy rest of cursor + cursor::take_rest(accumulator_message_cursor); + } + + + /// Create and share new price feed objects if they don't already exist using batch price attestation. + public fun create_price_feeds_using_batch_attestation( pyth_state: &mut PythState, // These vaas have been verified and consumed, so we don't have to worry about // doing replay protection for them. @@ -113,38 +159,85 @@ module pyth::pyth { // Deserialize the batch price attestation let price_infos = batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::take_payload(vaa), clock)); - while (!vector::is_empty(&price_infos)){ - let cur_price_info = vector::pop_back(&mut price_infos); - // Only create new Sui PriceInfoObject if not already - // registered with the Pyth State object. - if (!state::price_feed_object_exists( - pyth_state, - price_feed::get_price_identifier( - price_info::get_price_feed(&cur_price_info) - ) - ) - ){ - // Create and share newly created Sui PriceInfoObject containing a price feed, - // and then register a copy of its ID with State. - let new_price_info_object = price_info::new_price_info_object(cur_price_info, ctx); - let price_identifier = price_info::get_price_identifier(&cur_price_info); - let id = price_info::uid_to_inner(&new_price_info_object); - - state::register_price_info_object(&latest_only, pyth_state, price_identifier, id); - - transfer::public_share_object(new_price_info_object); - } - } + // Create and share new price info objects, if not already exists. + create_and_share_price_feeds_using_verified_price_infos(&latest_only, pyth_state, price_infos, ctx); }; vector::destroy_empty(verified_vaas); } - public fun create_price_infos_hot_potato( + // create_and_share_price_feeds_using_verified_price_infos is a private function used by + // 1) create_price_feeds_using_batch_attestation + // 2) create_price_feeds_using_accumulator + // to create new price feeds for symbols. + fun create_and_share_price_feeds_using_verified_price_infos(latest_only: &LatestOnly, pyth_state: &mut PythState, price_infos: vector, ctx: &mut TxContext){ + while (!vector::is_empty(&price_infos)){ + let cur_price_info = vector::pop_back(&mut price_infos); + + // Only create new Sui PriceInfoObject if not already + // registered with the Pyth State object. + if (!state::price_feed_object_exists( + pyth_state, + price_feed::get_price_identifier( + price_info::get_price_feed(&cur_price_info) + ) + ) + ){ + // Create and share newly created Sui PriceInfoObject containing a price feed, + // and then register a copy of its ID with State. + let new_price_info_object = price_info::new_price_info_object(cur_price_info, ctx); + let price_identifier = price_info::get_price_identifier(&cur_price_info); + let id = price_info::uid_to_inner(&new_price_info_object); + + state::register_price_info_object(latest_only, pyth_state, price_identifier, id); + + transfer::public_share_object(new_price_info_object); + } + } + } + + + // verified_vaa is the verified version of the VAA encoded within the accumulator_message + public fun create_authenticated_price_infos_using_accumulator( + pyth_state: &PythState, + accumulator_message: vector, + verified_vaa: VAA, + clock: &Clock, + ): AuthenticatedVector { + let _ = state::assert_latest_only(pyth_state); + + // verify that the VAA originates from a valid data source + assert!( + state::is_valid_data_source( + pyth_state, + data_source::new( + (vaa::emitter_chain(&verified_vaa) as u64), + vaa::emitter_address(&verified_vaa)) + ), + E_INVALID_DATA_SOURCE + ); + + // decode the price info updates from the VAA payload (first check if it is an accumulator or batch price update) + let accumulator_message_cursor = cursor::new(accumulator_message); + let header = deserialize::deserialize_u32(&mut accumulator_message_cursor); + + let _price_infos = vector::empty(); + if ((header as u64) == PYTHNET_ACCUMULATOR_UPDATE_MAGIC) { + _price_infos = accumulator::parse_and_verify_accumulator_message(&mut accumulator_message_cursor, vaa::take_payload(verified_vaa), clock); + } + else { + abort E_INVALID_ACCUMULATOR_HEADER + }; + // check that accumulator message has been fully consumed + cursor::destroy_empty(accumulator_message_cursor); + authenticated_vector::new(_price_infos) + } + + public fun create_authenticated_price_infos_using_batch_price_attestation( pyth_state: &PythState, verified_vaas: vector, clock: &Clock - ): HotPotatoVector { + ): AuthenticatedVector { let _ = state::assert_latest_only(pyth_state); let price_updates = vector::empty(); @@ -167,15 +260,13 @@ module pyth::pyth { } }; vector::destroy_empty(verified_vaas); - return hot_potato_vector::new(price_updates) + return authenticated_vector::new(price_updates) } /// Update a singular Pyth PriceInfoObject (containing a price feed) with the - /// price data in the given hot potato vector (a vector of PriceInfo objects). + /// price data in the authenticated price infos vector (a vector of PriceInfo objects). /// - /// The javascript https://github.com/pyth-network/pyth-js/tree/main/pyth-sui-js package - /// should be used to fetch these VAAs from the Price Service. More information about this - /// process can be found at https://docs.pyth.network/consume-data. + /// For more information on the end-to-end process for updating a price feed, please see the README. /// /// The given fee must contain a sufficient number of coins to pay the update fee for the given vaas. /// The update fee amount can be queried by calling get_update_fee(&vaas). @@ -183,16 +274,17 @@ module pyth::pyth { /// Please read more information about the update fee here: https://docs.pyth.network/consume-data/on-demand#fees public fun update_single_price_feed( pyth_state: &PythState, - price_updates: HotPotatoVector, + price_updates: AuthenticatedVector, price_info_object: &mut PriceInfoObject, fee: Coin, clock: &Clock - ): HotPotatoVector { + ): AuthenticatedVector { let latest_only = state::assert_latest_only(pyth_state); - // Since we charge the base update fee per 5 price updates, here we check that - // Coin Value >= update_fee / 5 (we only update a single price feed in this function). - assert!(state::get_base_update_fee(pyth_state) <= 5 * coin::value(&fee), E_INSUFFICIENT_FEE); + // On Sui, users get to choose which price feeds to update. They specify a single price feed to + // update at a time. We therefore charge the base fee for each such individual update. + // This is a departure from Eth, where users don't get to necessarily choose. + assert!(state::get_base_update_fee(pyth_state) <= coin::value(&fee), E_INSUFFICIENT_FEE); transfer::public_transfer(fee, state::get_fee_recipient(pyth_state)); @@ -200,8 +292,8 @@ module pyth::pyth { // and use it to update PriceInfoObject. let i = 0; let found = false; - while (i < hot_potato_vector::length(&price_updates)){ - let cur_price_info = hot_potato_vector::borrow(&price_updates, i); + while (i < authenticated_vector::length(&price_updates)){ + let cur_price_info = authenticated_vector::borrow(&price_updates, i); if (has_same_price_identifier(cur_price_info, price_info_object)){ found = true; update_cache(latest_only, cur_price_info, price_info_object, clock); @@ -348,24 +440,35 @@ module pyth::pyth_tests{ use pyth::state::{State as PythState}; use pyth::setup::{Self}; - //use pyth::price_identifier::{Self}; - use pyth::price_info::{PriceInfo, PriceInfoObject};//, PriceInfo, PriceInfoObject}; - //use pyth::price_feed::{Self}; + use pyth::price_info::{Self, PriceInfo, PriceInfoObject};//, PriceInfo, PriceInfoObject}; use pyth::data_source::{Self, DataSource}; //use pyth::i64::{Self}; //use pyth::price::{Self}; - use pyth::pyth::{Self, create_price_infos_hot_potato, update_single_price_feed}; - use pyth::hot_potato_vector::{Self}; + use pyth::pyth::{Self, create_authenticated_price_infos_using_batch_price_attestation, update_single_price_feed}; + use pyth::authenticated_vector::{Self}; + use pyth::price_identifier::{Self}; + use pyth::price_feed::{Self}; + use pyth::accumulator::{Self}; + use pyth::deserialize::{Self}; use wormhole::setup::{Self as wormhole_setup, DeployerCap}; use wormhole::external_address::{Self}; use wormhole::bytes32::{Self}; use wormhole::state::{State as WormState}; use wormhole::vaa::{Self, VAA}; + use wormhole::cursor::{Self}; const DEPLOYER: address = @0x1234; + const ACCUMULATOR_TESTS_EMITTER_ADDRESS: vector = x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b"; + const ACCUMULATOR_TESTS_INITIAL_GUARDIANS: vector> = vector[x"7E5F4552091A69125d5DfCb7b8C2659029395Bdf"]; + const DEFAULT_BASE_UPDATE_FEE: u64 = 50; + const DEFAULT_COIN_TO_MINT: u64 = 5000; + const BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS: vector> = vector[x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"]; + + fun ACCUMULATOR_TESTS_DATA_SOURCE(): vector { + vector[data_source::new(1, external_address::new(bytes32::from_bytes(ACCUMULATOR_TESTS_EMITTER_ADDRESS)))] + } - #[test_only] // /// A vector containing a single VAA with: // /// - emitter chain ID 17 // /// - emitter address 0x71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b @@ -392,16 +495,41 @@ module pyth::pyth_tests{ verified_vaas } + // get_verified_vaa_from_accumulator_message parses the accumulator message up until the vaa, then + // parses the vaa, yielding a verified wormhole::vaa::VAA object + fun get_verified_vaa_from_accumulator_message(worm_state: &WormState, accumulator_message: vector, clock: &Clock): VAA { + let _PYTHNET_ACCUMULATOR_UPDATE_MAGIC: u64 = 1347305813; + + let cursor = cursor::new(accumulator_message); + let header: u32 = deserialize::deserialize_u32(&mut cursor); + assert!((header as u64) == _PYTHNET_ACCUMULATOR_UPDATE_MAGIC, 0); + let major = deserialize::deserialize_u8(&mut cursor); + assert!(major == 1, 0); + let _minor = deserialize::deserialize_u8(&mut cursor); + + let trailing_size = deserialize::deserialize_u8(&mut cursor); + deserialize::deserialize_vector(&mut cursor, (trailing_size as u64)); + + let proof_type = deserialize::deserialize_u8(&mut cursor); + assert!(proof_type == 0, 0); + + let vaa_size = deserialize::deserialize_u16(&mut cursor); + let vaa = deserialize::deserialize_vector(&mut cursor, (vaa_size as u64)); + cursor::take_rest(cursor); + vaa::parse_and_verify(worm_state, vaa, clock) + } + #[test_only] /// Init Wormhole core bridge state. /// Init Pyth state. /// Set initial Sui clock time. /// Mint some SUI fee coins. - fun setup_test( + public fun setup_test( stale_price_threshold: u64, governance_emitter_chain_id: u64, governance_emitter_address: vector, data_sources: vector, + initial_guardians: vector>, base_update_fee: u64, to_mint: u64 ): (Scenario, Coin, Clock) { @@ -430,10 +558,6 @@ module pyth::pyth_tests{ let governance_chain = 1234; let governance_contract = x"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; - let initial_guardians = - vector[ - x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe" - ]; let guardian_set_seconds_to_live = 5678; let message_fee = 350; let guardian_set_index = 0; @@ -478,108 +602,108 @@ module pyth::pyth_tests{ (scenario, coins, clock) } - // #[test_only] - // fun get_mock_price_infos(): vector { - // vector[ - // price_info::new_price_info( - // 1663680747, - // 1663074349, - // price_feed::new( - // price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"), - // price::new(i64::new(1557, false), 7, i64::new(5, true), 1663680740), - // price::new(i64::new(1500, false), 3, i64::new(5, true), 1663680740), - // ), - // ), - // price_info::new_price_info( - // 1663680747, - // 1663074349, - // price_feed::new( - // price_identifier::from_byte_vec(x"3b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe"), - // price::new(i64::new(1050, false), 3, i64::new(5, true), 1663680745), - // price::new(i64::new(1483, false), 3, i64::new(5, true), 1663680745), - // ), - // ), - // price_info::new_price_info( - // 1663680747, - // 1663074349, - // price_feed::new( - // price_identifier::from_byte_vec(x"33832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d"), - // price::new(i64::new(1010, false), 2, i64::new(5, true), 1663680745), - // price::new(i64::new(1511, false), 3, i64::new(5, true), 1663680745), - // ), - // ), - // price_info::new_price_info( - // 1663680747, - // 1663074349, - // price_feed::new( - // price_identifier::from_byte_vec(x"21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db8"), - // price::new(i64::new(1739, false), 1, i64::new(5, true), 1663680745), - // price::new(i64::new(1508, false), 3, i64::new(5, true), 1663680745), - // ), - // ), - // ] - // } + fun get_mock_price_infos(): vector { + use pyth::i64::Self; + use pyth::price::{Self}; + vector[ + price_info::new_price_info( + 1663680747, + 1663074349, + price_feed::new( + price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"), + price::new(i64::new(1557, false), 7, i64::new(5, true), 1663680740), + price::new(i64::new(1500, false), 3, i64::new(5, true), 1663680740), + ), + ), + price_info::new_price_info( + 1663680747, + 1663074349, + price_feed::new( + price_identifier::from_byte_vec(x"3b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe"), + price::new(i64::new(1050, false), 3, i64::new(5, true), 1663680745), + price::new(i64::new(1483, false), 3, i64::new(5, true), 1663680745), + ), + ), + price_info::new_price_info( + 1663680747, + 1663074349, + price_feed::new( + price_identifier::from_byte_vec(x"33832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d"), + price::new(i64::new(1010, false), 2, i64::new(5, true), 1663680745), + price::new(i64::new(1511, false), 3, i64::new(5, true), 1663680745), + ), + ), + price_info::new_price_info( + 1663680747, + 1663074349, + price_feed::new( + price_identifier::from_byte_vec(x"21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db8"), + price::new(i64::new(1739, false), 1, i64::new(5, true), 1663680745), + price::new(i64::new(1508, false), 3, i64::new(5, true), 1663680745), + ), + ), + ] + } - // #[test_only] - // /// Compare the expected price feed with the actual Pyth price feeds. - // fun check_price_feeds_cached(expected: &vector, actual: &vector) { - // // Check that we can retrieve the correct current price and ema price for each price feed - // let i = 0; - // while (i < vector::length(expected)) { - // let price_feed = price_info::get_price_feed(vector::borrow(expected, i)); - // let price = price_feed::get_price(price_feed); - // let ema_price = price_feed::get_ema_price(price_feed); - // let price_identifier = price_info::get_price_identifier(vector::borrow(expected, i)); + /// Compare the expected price feed with the actual Pyth price feeds. + fun check_price_feeds_cached(expected: &vector, actual: &vector) { + // Check that we can retrieve the correct current price and ema price for each price feed + let i = 0; + while (i < vector::length(expected)) { + let price_feed = price_info::get_price_feed(vector::borrow(expected, i)); + let price = price_feed::get_price(price_feed); + let ema_price = price_feed::get_ema_price(price_feed); + let price_identifier = price_info::get_price_identifier(vector::borrow(expected, i)); - // let actual_price_info = price_info::get_price_info_from_price_info_object(vector::borrow(actual, i)); - // let actual_price_feed = price_info::get_price_feed(&actual_price_info); - // let actual_price = price_feed::get_price(actual_price_feed); - // let actual_ema_price = price_feed::get_ema_price(actual_price_feed); - // let actual_price_identifier = price_info::get_price_identifier(&actual_price_info); + let actual_price_info = price_info::get_price_info_from_price_info_object(vector::borrow(actual, i)); + let actual_price_feed = price_info::get_price_feed(&actual_price_info); + let actual_price = price_feed::get_price(actual_price_feed); + let actual_ema_price = price_feed::get_ema_price(actual_price_feed); + let actual_price_identifier = price_info::get_price_identifier(&actual_price_info); - // assert!(price == actual_price, 0); - // assert!(ema_price == actual_ema_price, 0); - // assert!(price_identifier::get_bytes(&price_identifier) == price_identifier::get_bytes(&actual_price_identifier), 0); + assert!(price == actual_price, 0); + assert!(ema_price == actual_ema_price, 0); + assert!(price_identifier::get_bytes(&price_identifier) == price_identifier::get_bytes(&actual_price_identifier), 0); - // i = i + 1; - // }; - // } + i = i + 1; + }; + } - // #[test] - // fun test_get_update_fee() { - // let single_update_fee = 50; - // let (scenario, test_coins, _clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], single_update_fee, 0); - // test_scenario::next_tx(&mut scenario, DEPLOYER, ); - // let pyth_state = take_shared(&scenario); - // // Pass in a single VAA + #[test] + fun test_get_update_fee() { + let (scenario, test_coins, _clock) = setup_test(500 /* stale_price_threshold */, 23 /* governance emitter chain */, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, 0); + test_scenario::next_tx(&mut scenario, DEPLOYER, ); + let pyth_state = take_shared(&scenario); + // Pass in a single VAA - // let single_vaa = vector[ - // x"fb1543888001083cf2e6ef3afdcf827e89b11efd87c563638df6e1995ada9f93", - // ]; + let single_vaa = vector[ + x"fb1543888001083cf2e6ef3afdcf827e89b11efd87c563638df6e1995ada9f93", + ]; - // assert!(pyth::get_total_update_fee(&pyth_state, vector::length>(&single_vaa)) == single_update_fee, 1); + assert!(pyth::get_total_update_fee(&pyth_state, vector::length>(&single_vaa)) == DEFAULT_BASE_UPDATE_FEE, 1); - // let multiple_vaas = vector[ - // x"4ee17a1a4524118de513fddcf82b77454e51be5d6fc9e29fc72dd6c204c0e4fa", - // x"c72fdf81cfc939d4286c93fbaaae2eec7bae28a5926fa68646b43a279846ccc1", - // x"d9a8123a793529c31200339820a3210059ecace6c044f81ecad62936e47ca049", - // x"84e4f21b3e65cef47fda25d15b4eddda1edf720a1d062ccbf441d6396465fbe6", - // x"9e73f9041476a93701a0b9c7501422cc2aa55d16100bec628cf53e0281b6f72f" - // ]; + let multiple_vaas = vector[ + x"4ee17a1a4524118de513fddcf82b77454e51be5d6fc9e29fc72dd6c204c0e4fa", + x"c72fdf81cfc939d4286c93fbaaae2eec7bae28a5926fa68646b43a279846ccc1", + x"d9a8123a793529c31200339820a3210059ecace6c044f81ecad62936e47ca049", + x"84e4f21b3e65cef47fda25d15b4eddda1edf720a1d062ccbf441d6396465fbe6", + x"9e73f9041476a93701a0b9c7501422cc2aa55d16100bec628cf53e0281b6f72f" + ]; - // // Pass in multiple VAAs - // assert!(pyth::get_total_update_fee(&pyth_state, vector::length>(&multiple_vaas)) == 250, 1); + // Pass in multiple VAAs + assert!(pyth::get_total_update_fee(&pyth_state, vector::length>(&multiple_vaas)) == 5*DEFAULT_BASE_UPDATE_FEE, 1); - // return_shared(pyth_state); - // coin::burn_for_testing(test_coins); - // clock::destroy_for_testing(_clock); - // test_scenario::end(scenario); - // } + return_shared(pyth_state); + coin::burn_for_testing(test_coins); + clock::destroy_for_testing(_clock); + test_scenario::end(scenario); + } #[test] #[expected_failure(abort_code = wormhole::vaa::E_WRONG_VERSION)] fun test_create_price_feeds_corrupt_vaa() { - let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], 50, 0); + let (scenario, test_coins, clock) = setup_test(500 /* stale_price_threshold */, 23 /* governance emitter chain */, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], vector[x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"], 50, 0); + test_scenario::next_tx(&mut scenario, DEPLOYER); let pyth_state = take_shared(&scenario); let worm_state = take_shared(&scenario); @@ -588,7 +712,7 @@ module pyth::pyth_tests{ let corrupt_vaa = x"90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"; let verified_vaas = vector[vaa::parse_and_verify(&worm_state, corrupt_vaa, &clock)]; // Create Pyth price feed - pyth::create_price_feeds( + pyth::create_price_feeds_using_batch_attestation( &mut pyth_state, verified_vaas, &clock, @@ -602,42 +726,40 @@ module pyth::pyth_tests{ test_scenario::end(scenario); } - // #[test] - // #[expected_failure(abort_code = pyth::pyth::E_INVALID_DATA_SOURCE)] - // fun test_create_price_feeds_invalid_data_source() { - // // Initialize the contract with some valid data sources, excluding our test VAA's source - // let data_sources = vector[ - // data_source::new( - // 4, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007742")) - // ), - // data_source::new( - // 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637")) - // ) - // ]; + #[test] + #[expected_failure(abort_code = pyth::pyth::E_INVALID_DATA_SOURCE)] + fun test_create_price_feeds_invalid_data_source() { + // Initialize the contract with some valid data sources, excluding our test VAA's source + let data_sources = vector[ + data_source::new( + 4, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007742")) + ), + data_source::new( + 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637")) + ) + ]; + let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS, 50, 0); + test_scenario::next_tx(&mut scenario, DEPLOYER); - // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, 50, 0); - // test_scenario::next_tx(&mut scenario, DEPLOYER); + let pyth_state = take_shared(&scenario); + let worm_state = take_shared(&scenario); - // let pyth_state = take_shared(&scenario); - // let worm_state = take_shared(&scenario); + let verified_vaas = get_verified_test_vaas(&worm_state, &clock); - // let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + pyth::create_price_feeds_using_batch_attestation( + &mut pyth_state, + verified_vaas, + &clock, + ctx(&mut scenario) + ); - // pyth::create_price_feeds( - // &mut pyth_state, - // verified_vaas, - // &clock, - // ctx(&mut scenario) - // ); + return_shared(pyth_state); + return_shared(worm_state); + clock::destroy_for_testing(clock); + coin::burn_for_testing(test_coins); + test_scenario::end(scenario); + } - // return_shared(pyth_state); - // return_shared(worm_state); - // clock::destroy_for_testing(clock); - // coin::burn_for_testing(test_coins); - // test_scenario::end(scenario); - // } - - #[test_only] fun data_sources_for_test_vaa(): vector { // Set some valid data sources, including our test VAA's source vector[ @@ -646,17 +768,13 @@ module pyth::pyth_tests{ data_source::new( 5, external_address::new(bytes32::new(x"0000000000000000000000000000000000000000000000000000000000007637"))), data_source::new( - 17, external_address::new(bytes32::new(x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b"))) + 17, external_address::new(bytes32::new(ACCUMULATOR_TESTS_EMITTER_ADDRESS))) ] } #[test] - fun test_create_and_update_price_feeds_success() { - let data_sources = data_sources_for_test_vaa(); - let base_update_fee = 50; - let coins_to_mint = 5000; - - let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint); + fun test_create_and_update_price_feeds_with_batch_attestation_success() { + let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), vector[x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"], DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT); test_scenario::next_tx(&mut scenario, DEPLOYER); let pyth_state = take_shared(&scenario); @@ -666,7 +784,7 @@ module pyth::pyth_tests{ test_scenario::next_tx(&mut scenario, DEPLOYER); - pyth::create_price_feeds( + pyth::create_price_feeds_using_batch_attestation( &mut pyth_state, verified_vaas, &clock, @@ -697,24 +815,29 @@ module pyth::pyth_tests{ test_scenario::next_tx(&mut scenario, DEPLOYER); - // Create hot potato. - let potato = create_price_infos_hot_potato( + // Create authenticated price infos + let vec = create_authenticated_price_infos_using_batch_price_attestation( &pyth_state, vector[vaa_1], &clock ); test_scenario::next_tx(&mut scenario, DEPLOYER); - potato = update_single_price_feed( + + vec = update_single_price_feed( &mut pyth_state, - potato, + vec, &mut price_info_object_1, test_coins, &clock ); test_scenario::next_tx(&mut scenario, DEPLOYER); - hot_potato_vector::destroy(potato); + + assert!(price_feeds_equal(authenticated_vector::borrow(&vec, 3), &price_info::get_price_info_from_price_info_object(&price_info_object_1)), 0); + + test_scenario::next_tx(&mut scenario, DEPLOYER); + authenticated_vector::destroy(vec); vector::destroy_empty(verified_vaas); return_shared(pyth_state); @@ -728,296 +851,650 @@ module pyth::pyth_tests{ test_scenario::end(scenario); } - // #[test] - // #[expected_failure(abort_code = pyth::pyth::E_PRICE_INFO_OBJECT_NOT_FOUND)] - // fun test_create_and_update_price_feeds_price_info_object_not_found_failure() { - // let data_sources = data_sources_for_test_vaa(); - // let base_update_fee = 50; - // let coins_to_mint = 5000; - // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint); - // test_scenario::next_tx(&mut scenario, DEPLOYER); + const TEST_ACCUMULATOR_SINGLE_FEED: vector = x"504e41550100000000a0010000000001005d461ac1dfffa8451edda17e4b28a46c8ae912422b2dc0cb7732828c497778ea27147fb95b4d250651931845e7f3e22c46326716bcf82be2874a9c9ab94b6e42000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000000004155575600000000000000000000000000da936d73429246d131873a0bab90ad7b416510be01005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf65f958f4883f9d2a8b5b1008d1fa01db95cf4a8c7000000006491cc757be59f3f377c0d3f423a695e81ad1eb504f8554c3620c3fd02f2ee15ea639b73fa3db9b34a245bdfa015c260c5a8a1180177cf30b2c0bebbb1adfe8f7985d051d2"; + // Info about the accumulator message: + // Price Identifier: 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6 + // Price: 6887568746747646632 + // Conf: 13092246197863718329 + // Exponent: 1559537863 + // EMA Price: 4772242609775910581 + // EMA Conf: 358129956189946877 + // EMA Expo: 1559537863 + // Published Time: 1687276661 + #[test] + fun test_create_and_update_single_price_feed_with_accumulator_success() { + let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT); - // let pyth_state = take_shared(&scenario); - // let worm_state = take_shared(&scenario); - // let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + test_scenario::next_tx(&mut scenario, DEPLOYER); - // pyth::create_price_feeds( - // &mut pyth_state, - // verified_vaas, - // &clock, - // ctx(&mut scenario) - // ); + let pyth_state = take_shared(&scenario); + let worm_state = take_shared(&scenario); - // // Affirm that 4 objects, which correspond to the 4 new price info objects - // // containing the price feeds were created and shared. - // let effects = test_scenario::next_tx(&mut scenario, DEPLOYER); - // let shared_ids = test_scenario::shared(&effects); - // let created_ids = test_scenario::created(&effects); - // assert!(vector::length(&shared_ids)==4, 0); - // assert!(vector::length(&created_ids)==4, 0); + let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_SINGLE_FEED, &clock); - // let price_info_object_1 = take_shared(&scenario); - // let price_info_object_2 = take_shared(&scenario); - // let price_info_object_3 = take_shared(&scenario); - // let price_info_object_4 = take_shared(&scenario); + test_scenario::next_tx(&mut scenario, DEPLOYER); - // // Note that here we only pass in 3 price info objects corresponding to 3 out - // // of the 4 price feeds. - // let price_info_object_vec = vector[price_info_object_1, price_info_object_2, price_info_object_3]; - // let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + pyth::create_price_feeds_using_accumulator( + &mut pyth_state, + TEST_ACCUMULATOR_SINGLE_FEED, + verified_vaa, + &clock, + ctx(&mut scenario) + ); - // pyth::update_price_feeds( - // &mut pyth_state, - // verified_vaas, - // &mut price_info_object_vec, - // test_coins, - // &clock - // ); + // Affirm that 1 object, which correspond to the 1 new price info object + // containing the price feeds were created and shared. + let effects = test_scenario::next_tx(&mut scenario, DEPLOYER); + let shared_ids = test_scenario::shared(&effects); + let created_ids = test_scenario::created(&effects); + assert!(vector::length(&shared_ids)==1, 0); + assert!(vector::length(&created_ids)==1, 0); - // price_info_object_3 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_2 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_1 = vector::pop_back(&mut price_info_object_vec); + let price_info_object_1 = take_shared(&scenario); - // vector::destroy_empty(price_info_object_vec); - // return_shared(pyth_state); - // return_shared(worm_state); - // return_shared(price_info_object_1); - // return_shared(price_info_object_2); - // return_shared(price_info_object_3); - // return_shared(price_info_object_4); + // Create authenticated price infos + verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_SINGLE_FEED, &clock); + let auth_price_infos = pyth::create_authenticated_price_infos_using_accumulator( + &pyth_state, + TEST_ACCUMULATOR_SINGLE_FEED, + verified_vaa, + &clock + ); - // return_shared(clock); - // test_scenario::end(scenario); - // } + test_scenario::next_tx(&mut scenario, DEPLOYER); + auth_price_infos = update_single_price_feed( + &mut pyth_state, + auth_price_infos, + &mut price_info_object_1, + coins, + &clock + ); - // #[test] - // #[expected_failure(abort_code = pyth::pyth::E_INSUFFICIENT_FEE)] - // fun test_create_and_update_price_feeds_insufficient_fee() { - // let data_sources = data_sources_for_test_vaa(); - // let base_update_fee = 50; - // let coins_to_mint = 5; + // assert that price info obejct is as expected + let expected = accumulator_test_1_to_price_info(); + assert!(price_feeds_equal(&expected, &price_info::get_price_info_from_price_info_object(&price_info_object_1)), 0); - // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint); - // test_scenario::next_tx(&mut scenario, DEPLOYER); + // clean up test scenario - // let pyth_state = take_shared(&scenario); - // let worm_state = take_shared(&scenario); + test_scenario::next_tx(&mut scenario, DEPLOYER); + authenticated_vector::destroy(auth_price_infos); - // let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + return_shared(pyth_state); + return_shared(worm_state); + return_shared(price_info_object_1); - // pyth::create_price_feeds( - // &mut pyth_state, - // verified_vaas, - // &clock, - // ctx(&mut scenario) - // ); + clock::destroy_for_testing(clock); + test_scenario::end(scenario); + } - // test_scenario::next_tx(&mut scenario, DEPLOYER); + #[test] + #[expected_failure(abort_code = pyth::accumulator::E_INVALID_PROOF)] + fun test_create_and_update_single_price_feed_with_accumulator_failure() { + use pyth::price_info::Self; - // let price_info_object = take_shared(&scenario); - // let price_info_object_vec = vector[price_info_object]; + let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT); - // verified_vaas = get_verified_test_vaas(&worm_state, &clock); + test_scenario::next_tx(&mut scenario, DEPLOYER); - // pyth::update_price_feeds( - // &mut pyth_state, - // verified_vaas, - // &mut price_info_object_vec, - // test_coins, - // &clock - // ); + let pyth_state = take_shared(&scenario); + let worm_state = take_shared(&scenario); - // price_info_object = vector::pop_back(&mut price_info_object_vec); - // vector::destroy_empty(price_info_object_vec); - // return_shared(pyth_state); - // return_shared(worm_state); - // return_shared(price_info_object); - // return_shared(clock); - // test_scenario::end(scenario); - // } + // the verified vaa here contains the wrong merkle root + let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock); - // #[test] - // fun test_update_cache(){ - // let data_sources = data_sources_for_test_vaa(); - // let base_update_fee = 50; - // let coins_to_mint = 5000; + test_scenario::next_tx(&mut scenario, DEPLOYER); - // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint); - // test_scenario::next_tx(&mut scenario, DEPLOYER); + pyth::create_price_feeds_using_accumulator( + &mut pyth_state, + TEST_ACCUMULATOR_SINGLE_FEED, + verified_vaa, + &clock, + ctx(&mut scenario) + ); - // let pyth_state = take_shared(&scenario); - // let worm_state = take_shared(&scenario); + // Affirm that 1 object, which correspond to the 1 new price info object + // containing the price feeds were created and shared. + let effects = test_scenario::next_tx(&mut scenario, DEPLOYER); + let shared_ids = test_scenario::shared(&effects); + let created_ids = test_scenario::created(&effects); + assert!(vector::length(&shared_ids)==1, 0); + assert!(vector::length(&created_ids)==1, 0); - // let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + let price_info_object_1 = take_shared(&scenario); - // // Update cache is called by create_price_feeds. - // pyth::create_price_feeds( - // &mut pyth_state, - // verified_vaas, - // &clock, - // ctx(&mut scenario) - // ); + // Create authenticated price infos + verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_SINGLE_FEED, &clock); + let auth_price_infos = pyth::create_authenticated_price_infos_using_accumulator( + &pyth_state, + TEST_ACCUMULATOR_SINGLE_FEED, + verified_vaa, + &clock + ); - // test_scenario::next_tx(&mut scenario, DEPLOYER); + test_scenario::next_tx(&mut scenario, DEPLOYER); + auth_price_infos = update_single_price_feed( + &mut pyth_state, + auth_price_infos, + &mut price_info_object_1, + coins, + &clock + ); - // let price_info_object_1 = take_shared(&scenario); - // let price_info_object_2 = take_shared(&scenario); - // let price_info_object_3 = take_shared(&scenario); - // let price_info_object_4 = take_shared(&scenario); + // assert that price info obejct is as expected + let expected = accumulator_test_1_to_price_info(); + assert!(price_feeds_equal(&expected, &price_info::get_price_info_from_price_info_object(&price_info_object_1)), 0); - // // These updates are price infos that correspond to the ones in TEST_VAAS. - // let updates = get_mock_price_infos(); - // let price_info_object_vec = vector[ - // price_info_object_1, - // price_info_object_2, - // price_info_object_3, - // price_info_object_4 - // ]; + // clean up test scenario + test_scenario::next_tx(&mut scenario, DEPLOYER); + authenticated_vector::destroy(auth_price_infos); - // // Check that TEST_VAAS was indeed used to instantiate the price feeds correctly, - // // by confirming that the info in updates is contained in price_info_object_vec. - // check_price_feeds_cached(&updates, &price_info_object_vec); + return_shared(pyth_state); + return_shared(worm_state); + return_shared(price_info_object_1); - // price_info_object_4 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_3 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_2 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_1 = vector::pop_back(&mut price_info_object_vec); - // vector::destroy_empty(price_info_object_vec); + clock::destroy_for_testing(clock); + test_scenario::end(scenario); + } - // return_shared(pyth_state); - // return_shared(worm_state); - // return_shared(price_info_object_1); - // return_shared(price_info_object_2); - // return_shared(price_info_object_3); - // return_shared(price_info_object_4); - // coin::burn_for_testing(test_coins); + const TEST_ACCUMULATOR_3_MSGS: vector = x"504e41550100000000a001000000000100d39b55fa311213959f91866d52624f3a9c07350d8956f6d42cfbb037883f31575c494a2f09fea84e4884dc9c244123fd124bc7825cd64d7c11e33ba5cfbdea7e010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000029da4c066b6e03b16a71e77811570dd9e19f258103005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60000000000000064000000000000003200000009000000006491cc747be59f3f377c0d3f000000000000006300000000000000340436992facb15658a7e9f08c4df4848ca80750f61fadcd96993de66b1fe7aef94e29e3bbef8b12db2305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d950055006e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af000000000000006500000000000000330000000a000000006491cc7504f8554c3620c3fd0000000000000064000000000000003504171ed10ac4f1eacf3a4951e1da6b119f07c45da5adcd96993de66b1fe7aef94e29e3bbef8b12db2305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d9500550031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68000000000000006600000000000000340000000b000000006491cc76e87d69c7b51242890000000000000065000000000000003604f2ee15ea639b73fa3db9b34a245bdfa015c260c5fe83e4772e0e346613de00e5348158a01bcb27b305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95"; + // Info about the price infos encoded in the accumulator message: + // Price Identifier: 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6 + // Price: 100 + // Conf: 50 + // Exponent: 9 + // EMA Price: 99 + // EMA Conf: 52 + // EMA Expo: 9 + // Published Time: 1687276660 - // clock::destroy_for_testing(clock); - // test_scenario::end(scenario); - // } + // Price Identifier: 0x6e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af + // Price: 101 + // Conf: 51 + // Exponent: 10 + // EMA Price: 100 + // EMA Conf: 53 + // EMA Expo: 10 + // Published Time: 1687276661 - // #[test] - // fun test_update_cache_old_update() { - // let data_sources = data_sources_for_test_vaa(); - // let base_update_fee = 50; - // let coins_to_mint = 5000; + // Price Identifier: 0x31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68 + // Price: 102 + // Conf: 52 + // Exponent: 11 + // EMA Price: 101 + // EMA Conf: 54 + // EMA Expo: 11 + // Published Time: 1687276662 - // let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources, base_update_fee, coins_to_mint); - // test_scenario::next_tx(&mut scenario, DEPLOYER); + #[test] + fun test_create_and_update_multiple_price_feeds_with_accumulator_success() { + use sui::coin::Self; - // let pyth_state = take_shared(&scenario); - // let worm_state = take_shared(&scenario); - // let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, ACCUMULATOR_TESTS_DATA_SOURCE(), ACCUMULATOR_TESTS_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT); - // pyth::create_price_feeds( - // &mut pyth_state, - // verified_vaas, - // &clock, - // ctx(&mut scenario) - // ); + test_scenario::next_tx(&mut scenario, DEPLOYER); - // test_scenario::next_tx(&mut scenario, DEPLOYER); + let pyth_state = take_shared(&scenario); + let worm_state = take_shared(&scenario); - // let price_info_object_1 = take_shared(&scenario); - // let price_info_object_2 = take_shared(&scenario); - // let price_info_object_3 = take_shared(&scenario); - // let price_info_object_4 = take_shared(&scenario); + let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock); - // let price_info_object_vec = vector[ - // price_info_object_1, - // price_info_object_2, - // price_info_object_3, - // price_info_object_4 - // ]; + test_scenario::next_tx(&mut scenario, DEPLOYER); - // // Hardcode the price identifier, price, and ema_price for price_info_object_1, because - // // it's easier than unwrapping price_info_object_1 and getting the quantities via getters. - // let timestamp = 1663680740; - // let price_identifier = price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"); - // let price = price::new(i64::new(1557, false), 7, i64::new(5, true), timestamp); - // let ema_price = price::new(i64::new(1500, false), 3, i64::new(5, true), timestamp); + pyth::create_price_feeds_using_accumulator( + &mut pyth_state, + TEST_ACCUMULATOR_3_MSGS, + verified_vaa, + &clock, + ctx(&mut scenario) + ); - // // 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); - // let old_ema_price = price::new(i64::new(8976, true), 234, i64::new(897, false), timestamp - 200); - // let old_update = price_info::new_price_info( - // 1257278600, - // 1690226180, - // price_feed::new( - // price_identifier, - // old_price, - // old_ema_price, - // ) - // ); - // pyth::update_cache(vector[old_update], &mut price_info_object_vec, &clock); + // Affirm that 3 objects, which correspond to the 3 new price info objects + // containing the price feeds were created and shared. + let effects = test_scenario::next_tx(&mut scenario, DEPLOYER); + let shared_ids = test_scenario::shared(&effects); + let created_ids = test_scenario::created(&effects); + assert!(vector::length(&shared_ids)==3, 0); + assert!(vector::length(&created_ids)==3, 0); - // price_info_object_4 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_3 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_2 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_1 = vector::pop_back(&mut price_info_object_vec); + let price_info_object_1 = take_shared(&scenario); + let price_info_object_2 = take_shared(&scenario); + let price_info_object_3 = take_shared(&scenario); - // vector::destroy_empty(price_info_object_vec); + // Create authenticated price infos + verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock); + let auth_price_infos = pyth::create_authenticated_price_infos_using_accumulator( + &pyth_state, + TEST_ACCUMULATOR_3_MSGS, + verified_vaa, + &clock + ); - // let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1); - // let current_price_feed = price_info::get_price_feed(¤t_price_info); - // let current_price = price_feed::get_price(current_price_feed); - // let current_ema_price = price_feed::get_ema_price(current_price_feed); + let coins2 = coin::split(&mut coins, 1000, ctx(&mut scenario)); + let coins3 = coin::split(&mut coins, 1000, ctx(&mut scenario)); - // // Confirm that no price update occurred when we tried to update cache with an - // // outdated update: old_update. - // assert!(current_price == price, 1); - // assert!(current_ema_price == ema_price, 1); + test_scenario::next_tx(&mut scenario, DEPLOYER); - // test_scenario::next_tx(&mut scenario, DEPLOYER); + // Update price feeds + auth_price_infos = update_single_price_feed( + &mut pyth_state, + auth_price_infos, + &mut price_info_object_1, + coins, + &clock + ); - // // Update the cache with a fresh update. - // let fresh_price = price::new(i64::new(5243, true), 2, i64::new(3, false), timestamp + 200); - // let fresh_ema_price = price::new(i64::new(8976, true), 21, i64::new(32, false), timestamp + 200); - // let fresh_update = price_info::new_price_info( - // 1257278600, - // 1690226180, - // price_feed::new( - // price_identifier, - // fresh_price, - // fresh_ema_price, - // ) - // ); + auth_price_infos = update_single_price_feed( + &mut pyth_state, + auth_price_infos, + &mut price_info_object_2, + coins2, + &clock + ); - // let price_info_object_vec = vector[ - // price_info_object_1, - // price_info_object_2, - // price_info_object_3, - // price_info_object_4 - // ]; + auth_price_infos = update_single_price_feed( + &mut pyth_state, + auth_price_infos, + &mut price_info_object_3, + coins3, + &clock + ); - // pyth::update_cache(vector[fresh_update], &mut price_info_object_vec, &clock); + // assert price feeds are as expected + let expected_price_infos = accumulator_test_3_to_price_info(0 /*offset argument*/); - // price_info_object_4 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_3 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_2 = vector::pop_back(&mut price_info_object_vec); - // price_info_object_1 = vector::pop_back(&mut price_info_object_vec); + let price_info_1 = price_info::get_price_info_from_price_info_object(&price_info_object_1); + assert!(price_feeds_equal(&price_info_1, vector::borrow(&expected_price_infos, 0)), 0); - // vector::destroy_empty(price_info_object_vec); + let price_info_2 = price_info::get_price_info_from_price_info_object(&price_info_object_2); + assert!(price_feeds_equal(&price_info_2, vector::borrow(&expected_price_infos, 1)), 0); - // // Confirm that the Pyth cached price got updated to fresh_price. - // let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1); - // let current_price_feed = price_info::get_price_feed(¤t_price_info); - // let current_price = price_feed::get_price(current_price_feed); - // let current_ema_price = price_feed::get_ema_price(current_price_feed); + let price_info_3 = price_info::get_price_info_from_price_info_object(&price_info_object_3); + assert!(price_feeds_equal(&price_info_3, vector::borrow(&expected_price_infos, 2)), 0); - // assert!(current_price==fresh_price, 0); - // assert!(current_ema_price==fresh_ema_price, 0); - // return_shared(pyth_state); - // return_shared(worm_state); - // return_shared(price_info_object_1); - // return_shared(price_info_object_2); - // return_shared(price_info_object_3); - // return_shared(price_info_object_4); - // coin::burn_for_testing(test_coins); + // clean up test scenario + test_scenario::next_tx(&mut scenario, DEPLOYER); + authenticated_vector::destroy(auth_price_infos); - // clock::destroy_for_testing(clock); - // test_scenario::end(scenario); - // } + return_shared(pyth_state); + return_shared(worm_state); + return_shared(price_info_object_1); + return_shared(price_info_object_2); + return_shared(price_info_object_3); + + clock::destroy_for_testing(clock); + test_scenario::end(scenario); + } + + #[test] + #[expected_failure(abort_code = pyth::pyth::E_INSUFFICIENT_FEE)] + fun test_create_and_update_price_feeds_insufficient_fee() { + + // this is not enough fee + let coins_to_mint = 1; + + let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), vector[x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe"], DEFAULT_BASE_UPDATE_FEE, coins_to_mint); + test_scenario::next_tx(&mut scenario, DEPLOYER); + + let pyth_state = take_shared(&scenario); + let worm_state = take_shared(&scenario); + + let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + + test_scenario::next_tx(&mut scenario, DEPLOYER); + + pyth::create_price_feeds_using_batch_attestation( + &mut pyth_state, + verified_vaas, + &clock, + ctx(&mut scenario) + ); + + // Affirm that 4 objects, which correspond to the 4 new price info objects + // containing the price feeds were created and shared. + let effects = test_scenario::next_tx(&mut scenario, DEPLOYER); + let shared_ids = test_scenario::shared(&effects); + let created_ids = test_scenario::created(&effects); + assert!(vector::length(&shared_ids)==4, 0); + assert!(vector::length(&created_ids)==4, 0); + + let price_info_object_1 = take_shared(&scenario); + let price_info_object_2 = take_shared(&scenario); + let price_info_object_3 = take_shared(&scenario); + let price_info_object_4 = take_shared(&scenario); + + // Create vector of price info objects (Sui objects with key ability and living in global store), + // which contain the price feeds we want to update. Note that these can be passed into + // update_price_feeds in any order! + //let price_info_object_vec = vector[price_info_object_1, price_info_object_2, price_info_object_3, price_info_object_4]; + verified_vaas = get_verified_test_vaas(&worm_state, &clock); + test_scenario::next_tx(&mut scenario, DEPLOYER); + + let vaa_1 = vector::pop_back(&mut verified_vaas); + + test_scenario::next_tx(&mut scenario, DEPLOYER); + + // Create authenticated price infos + let vec = create_authenticated_price_infos_using_batch_price_attestation( + &pyth_state, + vector[vaa_1], + &clock + ); + + test_scenario::next_tx(&mut scenario, DEPLOYER); + vec = update_single_price_feed( + &mut pyth_state, + vec, + &mut price_info_object_1, + test_coins, + &clock + ); + + test_scenario::next_tx(&mut scenario, DEPLOYER); + authenticated_vector::destroy(vec); + + vector::destroy_empty(verified_vaas); + return_shared(pyth_state); + return_shared(worm_state); + return_shared(price_info_object_1); + return_shared(price_info_object_2); + return_shared(price_info_object_3); + return_shared(price_info_object_4); + + clock::destroy_for_testing(clock); + test_scenario::end(scenario); + } + + + #[test] + fun test_update_cache(){ + let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT); + test_scenario::next_tx(&mut scenario, DEPLOYER); + + let pyth_state = take_shared(&scenario); + let worm_state = take_shared(&scenario); + + let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + + // Update cache is called by create_price_feeds. + pyth::create_price_feeds_using_batch_attestation( + &mut pyth_state, + verified_vaas, + &clock, + ctx(&mut scenario) + ); + + test_scenario::next_tx(&mut scenario, DEPLOYER); + + let price_info_object_1 = take_shared(&scenario); + let price_info_object_2 = take_shared(&scenario); + let price_info_object_3 = take_shared(&scenario); + let price_info_object_4 = take_shared(&scenario); + + // These updates are price infos that correspond to the ones in TEST_VAAS. + let updates = get_mock_price_infos(); + let price_info_object_vec = vector[ + price_info_object_1, + price_info_object_2, + price_info_object_3, + price_info_object_4 + ]; + + // Check that TEST_VAAS was indeed used to instantiate the price feeds correctly, + // by confirming that the info in updates is contained in price_info_object_vec. + check_price_feeds_cached(&updates, &price_info_object_vec); + + price_info_object_4 = vector::pop_back(&mut price_info_object_vec); + price_info_object_3 = vector::pop_back(&mut price_info_object_vec); + price_info_object_2 = vector::pop_back(&mut price_info_object_vec); + price_info_object_1 = vector::pop_back(&mut price_info_object_vec); + vector::destroy_empty(price_info_object_vec); + + return_shared(pyth_state); + return_shared(worm_state); + return_shared(price_info_object_1); + return_shared(price_info_object_2); + return_shared(price_info_object_3); + return_shared(price_info_object_4); + coin::burn_for_testing(test_coins); + + clock::destroy_for_testing(clock); + test_scenario::end(scenario); + } + + #[test] + fun test_update_cache_old_update() { + use pyth::i64::Self; + use pyth::price::Self; + + let (scenario, test_coins, clock) = setup_test(500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), BATCH_ATTESTATION_TEST_INITIAL_GUARDIANS, DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT); + test_scenario::next_tx(&mut scenario, DEPLOYER); + + let pyth_state = take_shared(&scenario); + let worm_state = take_shared(&scenario); + let verified_vaas = get_verified_test_vaas(&worm_state, &clock); + + pyth::create_price_feeds_using_batch_attestation( + &mut pyth_state, + verified_vaas, + &clock, + ctx(&mut scenario) + ); + + test_scenario::next_tx(&mut scenario, DEPLOYER); + + let price_info_object_1 = take_shared(&scenario); + let price_info_object_2 = take_shared(&scenario); + let price_info_object_3 = take_shared(&scenario); + let price_info_object_4 = take_shared(&scenario); + + // Hardcode the price identifier, price, and ema_price for price_info_object_1, because + // it's easier than unwrapping price_info_object_1 and getting the quantities via getters. + let timestamp = 1663680740; + let price_identifier = price_identifier::from_byte_vec(x"c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1"); + let price = price::new(i64::new(1557, false), 7, i64::new(5, true), timestamp); + let ema_price = price::new(i64::new(1500, false), 3, i64::new(5, true), timestamp); + + // 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); + let old_ema_price = price::new(i64::new(8976, true), 234, i64::new(897, false), timestamp - 200); + let old_update = price_info::new_price_info( + 1257278600, + 1690226180, + price_feed::new( + price_identifier, + old_price, + old_ema_price, + ) + ); + let latest_only = pyth::state::create_latest_only_for_test(); + pyth::update_cache(latest_only, &old_update, &mut price_info_object_1, &clock); + + let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1); + let current_price_feed = price_info::get_price_feed(¤t_price_info); + let current_price = price_feed::get_price(current_price_feed); + let current_ema_price = price_feed::get_ema_price(current_price_feed); + + // Confirm that no price update occurred when we tried to update cache with an + // outdated update: old_update. + assert!(current_price == price, 1); + assert!(current_ema_price == ema_price, 1); + + test_scenario::next_tx(&mut scenario, DEPLOYER); + + // Update the cache with a fresh update. + let fresh_price = price::new(i64::new(5243, true), 2, i64::new(3, false), timestamp + 200); + let fresh_ema_price = price::new(i64::new(8976, true), 21, i64::new(32, false), timestamp + 200); + let fresh_update = price_info::new_price_info( + 1257278600, + 1690226180, + price_feed::new( + price_identifier, + fresh_price, + fresh_ema_price, + ) + ); + + let latest_only = pyth::state::create_latest_only_for_test(); + pyth::update_cache(latest_only, &fresh_update, &mut price_info_object_1, &clock); + + // Confirm that the Pyth cached price got updated to fresh_price. + let current_price_info = price_info::get_price_info_from_price_info_object(&price_info_object_1); + let current_price_feed = price_info::get_price_feed(¤t_price_info); + let current_price = price_feed::get_price(current_price_feed); + let current_ema_price = price_feed::get_ema_price(current_price_feed); + + assert!(current_price==fresh_price, 0); + assert!(current_ema_price==fresh_ema_price, 0); + + return_shared(pyth_state); + return_shared(worm_state); + return_shared(price_info_object_1); + return_shared(price_info_object_2); + return_shared(price_info_object_3); + return_shared(price_info_object_4); + coin::burn_for_testing(test_coins); + + clock::destroy_for_testing(clock); + test_scenario::end(scenario); + } + + // pyth accumulator tests (included in this file instead of pyth_accumulator.move to avoid dependency cycle - as we need pyth_tests::setup_test) + #[test] + fun test_parse_and_verify_accumulator_updates(){ + use sui::test_scenario::{Self, take_shared, return_shared}; + use sui::transfer::{Self}; + + let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, vector[], ACCUMULATOR_TESTS_INITIAL_GUARDIANS, 50, 0); + let worm_state = take_shared(&scenario); + test_scenario::next_tx(&mut scenario, @0x123); + + let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock); + + let cur = cursor::new(TEST_ACCUMULATOR_3_MSGS); + // pop header from cursor before passing to parse_and_verify_accumulator_message + let _header: u32 = deserialize::deserialize_u32(&mut cur); + + let price_info_updates = accumulator::parse_and_verify_accumulator_message(&mut cur, vaa::take_payload(verified_vaa), &clock); + + let expected_price_infos = accumulator_test_3_to_price_info(0); + let num_updates = vector::length(&price_info_updates); + let i = 0; + while (i < num_updates){ + assert!(price_feeds_equal(vector::borrow(&price_info_updates, i), vector::borrow(&expected_price_infos, i)), 0); + i = i + 1; + }; + + // clean-up + cursor::take_rest(cur); + transfer::public_transfer(coins, @0x1234); + clock::destroy_for_testing(clock); + return_shared(worm_state); + test_scenario::end(scenario); + } + + #[test] + fun test_parse_and_verify_accumulator_updates_with_extra_bytes_at_end_of_message(){ + use sui::test_scenario::{Self, take_shared, return_shared}; + use sui::transfer::{Self}; + + let (scenario, coins, clock) = setup_test(500, 23, ACCUMULATOR_TESTS_EMITTER_ADDRESS, vector[], ACCUMULATOR_TESTS_INITIAL_GUARDIANS, 50, 0); + let worm_state = take_shared(&scenario); + test_scenario::next_tx(&mut scenario, @0x123); + + let verified_vaa = get_verified_vaa_from_accumulator_message(&worm_state, TEST_ACCUMULATOR_3_MSGS, &clock); + + // append some extra garbage bytes at the end of the accumulator message, and make sure + // that test does not error out + vector::append(&mut TEST_ACCUMULATOR_3_MSGS, x"1234123412341234"); + + let cur = cursor::new(TEST_ACCUMULATOR_3_MSGS); + // pop header from cursor before passing to parse_and_verify_accumulator_message + let _header: u32 = deserialize::deserialize_u32(&mut cur); + + let price_info_updates = accumulator::parse_and_verify_accumulator_message(&mut cur, vaa::take_payload(verified_vaa), &clock); + + let expected_price_infos = accumulator_test_3_to_price_info(0); + let num_updates = vector::length(&price_info_updates); + let i = 0; + while (i < num_updates){ + assert!(price_feeds_equal(vector::borrow(&price_info_updates, i), vector::borrow(&expected_price_infos, i)), 0); + i = i + 1; + }; + + // clean-up + cursor::take_rest(cur); + transfer::public_transfer(coins, @0x1234); + clock::destroy_for_testing(clock); + return_shared(worm_state); + test_scenario::end(scenario); + } + + fun price_feeds_equal(p1: &PriceInfo, p2: &PriceInfo): bool{ + price_info::get_price_feed(p1)== price_info::get_price_feed(p2) + } + + // accumulator_test_3_to_price_info gets the data encoded within TEST_ACCUMULATOR_3_MSGS + fun accumulator_test_3_to_price_info(offset: u64): vector { + use pyth::i64::{Self}; + use pyth::price::{Self}; + let i = 0; + let feed_ids = vector[x"b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6", + x"6e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af", + x"31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68"]; + let expected: vector = vector[]; + while (i < 3) { + vector::push_back(&mut expected, price_info::new_price_info( + 1663680747, + 1663074349, + price_feed::new( + price_identifier::from_byte_vec( + *vector::borrow(&feed_ids, i) + ), + price::new( + i64::new(100 + i + offset, false), + 50 + i + offset, + i64::new(9 + i + offset, false), + 1687276660 + i + offset + ), + price::new( + i64::new(99 + i + offset, false), + 52 + i + offset, + i64::new(9 + i + offset, false), + 1687276660 + i + offset + ), + ), + )); + i = i + 1; + }; + return expected + } + + // accumulator_test_1_to_price_info gets the data encoded within TEST_ACCUMULATOR_SINGLE_FEED + fun accumulator_test_1_to_price_info(): PriceInfo { + use pyth::i64::{Self}; + use pyth::price::{Self}; + price_info::new_price_info( + 1663680747, + 1663074349, + price_feed::new( + price_identifier::from_byte_vec( + x"b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6" + ), + price::new( + i64::new(6887568746747646632, false), + 13092246197863718329, + i64::new(1559537863, false), + 1687276661 + ), + price::new( + i64::new(4772242609775910581, false), + 358129956189946877, + i64::new(1559537863, false), + 1687276661 + ), + ), + ) + } } diff --git a/target_chains/sui/contracts/sources/pyth_accumulator.move b/target_chains/sui/contracts/sources/pyth_accumulator.move new file mode 100644 index 00000000..0af313af --- /dev/null +++ b/target_chains/sui/contracts/sources/pyth_accumulator.move @@ -0,0 +1,116 @@ +module pyth::accumulator { + use std::vector::{Self}; + use sui::clock::{Clock, Self}; + use wormhole::bytes20::{Self, Bytes20}; + use wormhole::cursor::{Self, Cursor}; + use pyth::deserialize::{Self}; + use pyth::price_identifier::{Self}; + use pyth::price_info::{Self, PriceInfo}; + use pyth::price_feed::{Self}; + use pyth::merkle_tree::{Self}; + + const PRICE_FEED_MESSAGE_TYPE: u64 = 0; + const E_INVALID_UPDATE_DATA: u64 = 245; + const E_INVALID_PROOF: u64 = 345; + const E_INVALID_WORMHOLE_MESSAGE: u64 = 454; + const E_INVALID_ACCUMULATOR_PAYLOAD: u64 = 554; + + const ACCUMULATOR_UPDATE_WORMHOLE_VERIFICATION_MAGIC: u32 = 1096111958; + const PYTHNET_ACCUMULATOR_UPDATE_MAGIC: u64 = 1347305813; + + const MINIMUM_SUPPORTED_MINOR_VERSION: u8 = 0; + const MAJOR_VERSION: u8 = 1; + + friend pyth::pyth; + #[test_only] + friend pyth::pyth_tests; + + // parse_and_verify_accumulator_message verifies that the price updates encoded in the + // accumulator message (accessed via cursor) belong to the merkle tree defined by the merkle root encoded in + // vaa_payload. + public(friend) fun parse_and_verify_accumulator_message(cursor: &mut Cursor, vaa_payload: vector, clock: &Clock): vector { + let major = deserialize::deserialize_u8(cursor); + assert!(major == MAJOR_VERSION, E_INVALID_ACCUMULATOR_PAYLOAD); + + let minor = deserialize::deserialize_u8(cursor); + assert!(minor >= MINIMUM_SUPPORTED_MINOR_VERSION, E_INVALID_ACCUMULATOR_PAYLOAD); + + let trailing_size = deserialize::deserialize_u8(cursor); + deserialize::deserialize_vector(cursor, (trailing_size as u64)); + + let proof_type = deserialize::deserialize_u8(cursor); + assert!(proof_type == 0, E_INVALID_ACCUMULATOR_PAYLOAD); + + // Ignore the vaa in the accumulator message because presumably it has already been verified + // and passed to this function as input. + let vaa_size = deserialize::deserialize_u16(cursor); + deserialize::deserialize_vector(cursor, (vaa_size as u64)); + + let merkle_root_hash = parse_accumulator_merkle_root_from_vaa_payload(vaa_payload); + parse_and_verify_accumulator_updates(cursor, merkle_root_hash, clock) + } + + // parse_accumulator_merkle_root_from_vaa_payload takes in some VAA bytes, verifies that the vaa + // corresponds to a merkle update, and finally returns the keccak hash of the merkle root. + // Note: this function is adapted from the Aptos Pyth accumulator + fun parse_accumulator_merkle_root_from_vaa_payload(message: vector): Bytes20 { + let msg_payload_cursor = cursor::new(message); + let payload_type = deserialize::deserialize_u32(&mut msg_payload_cursor); + assert!(payload_type == ACCUMULATOR_UPDATE_WORMHOLE_VERIFICATION_MAGIC, E_INVALID_WORMHOLE_MESSAGE); + let wh_message_payload_type = deserialize::deserialize_u8(&mut msg_payload_cursor); + assert!(wh_message_payload_type == 0, E_INVALID_WORMHOLE_MESSAGE); // Merkle variant + let _merkle_root_slot = deserialize::deserialize_u64(&mut msg_payload_cursor); + let _merkle_root_ring_size = deserialize::deserialize_u32(&mut msg_payload_cursor); + let merkle_root_hash = deserialize::deserialize_vector(&mut msg_payload_cursor, 20); + cursor::take_rest(msg_payload_cursor); + bytes20::new(merkle_root_hash) + } + + // Note: this parsing function is adapted from the Aptos Pyth parse_price_feed_message function + fun parse_price_feed_message(message_cur: &mut Cursor, clock: &Clock): PriceInfo { + let message_type = deserialize::deserialize_u8(message_cur); + + assert!(message_type == 0, 0); // PriceFeedMessage variant + let price_identifier = price_identifier::from_byte_vec(deserialize::deserialize_vector(message_cur, 32)); + let price = deserialize::deserialize_i64(message_cur); + let conf = deserialize::deserialize_u64(message_cur); + let expo = deserialize::deserialize_i32(message_cur); + let publish_time = deserialize::deserialize_u64(message_cur); + let _prev_publish_time = deserialize::deserialize_i64(message_cur); + let ema_price = deserialize::deserialize_i64(message_cur); + let ema_conf = deserialize::deserialize_u64(message_cur); + let price_info = price_info::new_price_info( + clock::timestamp_ms(clock) / 1000, // not used anywhere kept for backward compatibility + clock::timestamp_ms(clock) / 1000, + price_feed::new( + price_identifier, + pyth::price::new(price, conf, expo, publish_time), + pyth::price::new(ema_price, ema_conf, expo, publish_time), + ) + ); + price_info + } + + // parse_and_verify_accumulator_updates takes as input a merkle root and cursor over the encoded update message (containing encoded + // leafs and merkle proofs), iterates over each leaf/proof pair and verifies it is part of the tree, and finally outputs the set of + // decoded and authenticated PriceInfos. + fun parse_and_verify_accumulator_updates(cursor: &mut Cursor, merkle_root: Bytes20, clock: &Clock): vector { + let update_size = deserialize::deserialize_u8(cursor); + let price_info_updates: vector = vector[]; + while (update_size > 0) { + let message_size = deserialize::deserialize_u16(cursor); + let message = deserialize::deserialize_vector(cursor, (message_size as u64)); //should be safe to go from u16 to u16 + let message_cur = cursor::new(message); + let price_info = parse_price_feed_message(&mut message_cur, clock); + cursor::take_rest(message_cur); + + vector::push_back(&mut price_info_updates, price_info); + + // isProofValid pops the next merkle proof from the front of cursor and checks if it proves that message is part of the + // merkle tree defined by merkle_root + assert!(merkle_tree::isProofValid(cursor, merkle_root, message), E_INVALID_PROOF); + update_size = update_size - 1; + }; + price_info_updates + } +} diff --git a/target_chains/sui/contracts/sources/state.move b/target_chains/sui/contracts/sources/state.move index fb78edac..6a357155 100644 --- a/target_chains/sui/contracts/sources/state.move +++ b/target_chains/sui/contracts/sources/state.move @@ -40,6 +40,11 @@ module pyth::state { /// state methods. struct LatestOnly has drop {} + #[test_only] + public fun create_latest_only_for_test():LatestOnly { + LatestOnly{} + } + struct State has key, store { id: UID, governance_data_source: DataSource,