Aptos accumulators (#904)

* Pin dependency versions

* Implement accumulator support on aptos

* Fix formatting on error.move in aptos

* Add CI workflow for aptos

* Upgrade to aptos cli 1.0.4
This commit is contained in:
Mohammad Amin Khashkhashi Moghaddam 2023-06-23 11:27:25 +01:00 committed by GitHub
parent 919f71e68f
commit fb6672fbfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 741 additions and 62 deletions

30
.github/workflows/aptos-contract.yml vendored Normal file
View File

@ -0,0 +1,30 @@
on:
pull_request:
paths:
- target_chains/aptos/contracts/**
push:
branches:
- main
paths:
- target_chains/aptos/contracts/**
name: Aptos Contract
jobs:
aptos-tests:
name: Aptos tests
runs-on: ubuntu-latest
defaults:
run:
working-directory: target_chains/aptos/contracts/
steps:
- uses: actions/checkout@v3
- name: Download CLI
run: wget https://github.com/aptos-labs/aptos-core/releases/download/aptos-cli-v1.0.4/aptos-cli-1.0.4-Ubuntu-22.04-x86_64.zip
- name: Unzip CLI
run: unzip aptos-cli-1.0.4-Ubuntu-22.04-x86_64.zip
- name: Run tests
run: ./aptos move test

View File

@ -4,11 +4,10 @@ version = "0.0.1"
upgrade_policy = "compatible"
[dependencies]
# TODO: pin versions before mainnet release
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework/", rev = "main" }
MoveStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/move-stdlib/", rev = "main" }
AptosStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-stdlib/", rev = "main" }
AptosToken = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-token/", rev = "main" }
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework/", rev = "2c74a456298fcd520241a562119b6fe30abdaae2" }
MoveStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/move-stdlib/", rev = "2c74a456298fcd520241a562119b6fe30abdaae2" }
AptosStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-stdlib/", rev = "2c74a456298fcd520241a562119b6fe30abdaae2" }
AptosToken = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-token/", rev = "2c74a456298fcd520241a562119b6fe30abdaae2" }
Wormhole = { git = "https://github.com/wormhole-foundation/wormhole.git", subdir = "aptos/wormhole", rev = "aptos/integration" }
Deployer = { git = "https://github.com/wormhole-foundation/wormhole.git", subdir = "aptos/deployer", rev = "aptos/integration" }

View File

@ -159,7 +159,7 @@ module pyth::batch_price_attestation {
}
#[test]
#[expected_failure(abort_code = 65560)]
#[expected_failure(abort_code = 65560, location = pyth::batch_price_attestation)]
fun test_deserialize_batch_price_attestation_invalid_magic() {
// A batch price attestation with a magic number of 0x50325749
let bytes = x"5032574900030000000102000400951436e0be37536be96f0896366089506a59763d036728332d3e3038047851aea7c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1000000000000049a0000000000000008fffffffb00000000000005dc0000000000000003000000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000006150000000000000007215258d81468614f6b7e194c5d145609394f67b041e93e6695dcc616faadd0603b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe000000000000041a0000000000000003fffffffb00000000000005cb0000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e4000000000000048600000000000000078ac9cf3ab299af710d735163726fdae0db8465280502eb9f801f74b3c1bd190333832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d00000000000003f20000000000000002fffffffb00000000000005e70000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e40000000000000685000000000000000861db714e9ff987b6fedf00d01f9fea6db7c30632d6fc83b7bc9459d7192bc44a21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db800000000000006cb0000000000000001fffffffb00000000000005e40000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000007970000000000000001";

View File

@ -79,27 +79,47 @@ module pyth::error {
error::invalid_state(19)
}
public fun invalid_governance_magic_value(): u64 {
public fun invalid_governance_magic_value(): u64 {
error::invalid_argument(20)
}
}
public fun magnitude_too_large(): u64 {
public fun magnitude_too_large(): u64 {
error::invalid_argument(21)
}
}
public fun governance_contract_upgrade_chain_id_zero(): u64 {
public fun governance_contract_upgrade_chain_id_zero(): u64 {
error::invalid_argument(22)
}
}
public fun invalid_price_status(): u64 {
public fun invalid_price_status(): u64 {
error::invalid_argument(23)
}
}
public fun invalid_attestation_magic_value(): u64 {
public fun invalid_attestation_magic_value(): u64 {
error::invalid_argument(24)
}
}
public fun data_source_emitter_address_and_chain_ids_different_lengths(): u64 {
public fun data_source_emitter_address_and_chain_ids_different_lengths(): u64 {
error::invalid_argument(25)
}
}
public fun invalid_accumulator_payload(): u64 {
error::invalid_argument(26)
}
public fun invalid_accumulator_message(): u64 {
error::invalid_argument(27)
}
public fun invalid_wormhole_message(): u64 {
error::invalid_argument(28)
}
public fun invalid_proof(): u64 {
error::invalid_argument(29)
}
public fun invalid_keccak160_length(): u64 {
error::invalid_argument(30)
}
}

View File

@ -85,7 +85,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 6)]
#[expected_failure(abort_code = 6, location = wormhole::vaa)]
fun test_execute_governance_instruction_invalid_vaa() {
setup_test(50, 24, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100);
let vaa_bytes = x"6c436741b108";
@ -93,7 +93,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 65550)]
#[expected_failure(abort_code = 65550, location = pyth::governance)]
fun test_execute_governance_instruction_invalid_data_source() {
setup_test(100, 50, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100);
@ -105,7 +105,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 65551)]
#[expected_failure(abort_code = 65551, location = pyth::governance)]
fun test_execute_governance_instruction_invalid_sequence_number_0() {
setup_test(100, 50, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100);
assert!(state::get_last_executed_governance_sequence() == 0, 1);
@ -119,7 +119,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 65556)]
#[expected_failure(abort_code = 65556, location = pyth::governance_instruction)]
fun test_execute_governance_instruction_invalid_instruction_magic() {
setup_test(100, 50, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100);
@ -133,7 +133,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 65548)]
#[expected_failure(abort_code = 65548, location = pyth::governance_instruction)]
fun test_execute_governance_instruction_invalid_module() {
setup_test(100, 50, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100);
@ -148,7 +148,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 65549)]
#[expected_failure(abort_code = 65549, location = pyth::governance_instruction)]
fun test_execute_governance_instruction_invalid_target_chain() {
setup_test(100, 50, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100);
@ -164,7 +164,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 65552)]
#[expected_failure(abort_code = 65552, location = pyth::governance_action)]
fun test_execute_governance_instruction_invalid_action() {
setup_test(100, 50, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100);
@ -204,7 +204,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 65558)]
#[expected_failure(abort_code = 65558, location = pyth::governance)]
fun test_execute_governance_instruction_upgrade_contract_chain_id_zero() {
setup_test(100, 50, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", 100);
@ -280,7 +280,7 @@ module pyth::governance_test {
}
#[test]
#[expected_failure(abort_code = 65550)]
#[expected_failure(abort_code = 65550, location = pyth::governance)]
fun test_execute_governance_instruction_set_governance_data_source_old_source_invalid() {
let initial_governance_emitter_chain_id = 50;
let initial_governance_emitter_address = x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf";

View File

@ -64,21 +64,21 @@ module pyth::governance_instruction {
}
#[test]
#[expected_failure(abort_code = 65556)]
#[expected_failure(abort_code = 65556, location = pyth::governance_instruction)]
fun test_from_byte_vec_invalid_magic() {
let bytes = x"5054474eb01087a85361f738f19454e66664d3c9";
destroy(from_byte_vec(bytes));
}
#[test]
#[expected_failure(abort_code = 65548)]
#[expected_failure(abort_code = 65548, location = pyth::governance_instruction)]
fun test_from_byte_vec_invalid_module() {
let bytes = x"5054474db00187a85361f738f19454e66664d3c9";
destroy(from_byte_vec(bytes));
}
#[test]
#[expected_failure(abort_code = 65548)]
#[expected_failure(abort_code = 65548, location = pyth::governance_instruction)]
fun test_from_byte_vec_invalid_target_chain_id() {
let bytes = x"5054474db00187a85361f738f19454e66664d3c9";
destroy(from_byte_vec(bytes));

View File

@ -76,7 +76,7 @@ module pyth::i64 {
}
#[test]
#[expected_failure(abort_code = 65557)]
#[expected_failure(abort_code = 65557, location = pyth::i64)]
fun test_magnitude_too_large_positive() {
new(0x8000000000000000, false);
}
@ -88,7 +88,7 @@ module pyth::i64 {
}
#[test]
#[expected_failure(abort_code = 65557)]
#[expected_failure(abort_code = 65557, location = pyth::i64)]
fun test_magnitude_too_large_negative() {
new(0x8000000000000001, true);
}
@ -115,7 +115,7 @@ module pyth::i64 {
}
#[test]
#[expected_failure(abort_code = 196609)]
#[expected_failure(abort_code = 196609, location = pyth::i64)]
fun test_get_magnitude_if_positive_negative() {
assert!(get_magnitude_if_positive(&new(7686, true)) == 7686, 1);
}
@ -126,7 +126,7 @@ module pyth::i64 {
}
#[test]
#[expected_failure(abort_code = 196627)]
#[expected_failure(abort_code = 196627, location = pyth::i64)]
fun test_get_magnitude_if_negative_positive() {
assert!(get_magnitude_if_negative(&new(7686, false)) == 7686, 1);
}

View File

@ -0,0 +1,83 @@
module pyth::keccak160 {
use std::vector;
use pyth::error;
use std::aptos_hash;
struct Hash has drop {
data: vector<u8>,
}
const HASH_LENGTH: u64 = 20;
public fun get_data(hash: &Hash): vector<u8> {
hash.data
}
public fun get_hash_length(): u64 {
HASH_LENGTH
}
public fun new(data: vector<u8>): Hash {
assert!(vector::length(&data) == HASH_LENGTH, error::invalid_keccak160_length());
Hash { data }
}
public fun from_data(data: vector<u8>): Hash {
let hash = aptos_hash::keccak256(data);
while (vector::length(&hash) > HASH_LENGTH) {
vector::pop_back(&mut hash);
};
new(hash)
}
public fun is_smaller(lhs: &Hash, rhs: &Hash): bool {
let i = 0;
while (i < vector::length(&get_data(lhs))) {
let lhs_val: u8 = *vector::borrow(&get_data(lhs), i);
let rhs_val: u8 = *vector::borrow(&get_data(rhs), i);
if (lhs_val != rhs_val) {
return lhs_val < rhs_val
};
i = i + 1;
};
false
}
#[test]
fun test_from_data() {
let hash = from_data(vector[0]);
let expected = new(x"bc36789e7a1e281436464229828f817d6612f7b4");
assert!(&hash == &expected, 1);
}
#[test]
fun test_is_smaller() {
let h1 = new(x"0000000000000000010000000000000000000000");
let h2 = new(x"0000000000000000000300000000000000000000");
let h3 = new(x"0000000000000000000200000000000000000000");
assert!(is_smaller(&h3, &h2), 1);
assert!(!is_smaller(&h2, &h3), 1);
assert!(is_smaller(&h2, &h1), 1);
assert!(!is_smaller(&h1, &h2), 1);
assert!(is_smaller(&h3, &h1), 1);
assert!(!is_smaller(&h1, &h3), 1);
assert!(!is_smaller(&h1, &h1), 1);
assert!(!is_smaller(&h2, &h2), 1);
assert!(!is_smaller(&h3, &h3), 1);
}
#[test]
fun test_new_success() {
new(x"05c51b04b820c0f704e3fdd2e4fc1e70aff26dff");
}
#[test]
#[expected_failure(abort_code = 65566, location = pyth::keccak160)]
fun test_new_wrong_size() {
new(vector[1, 2, 3]);
}
}

View File

@ -0,0 +1,100 @@
module pyth::merkle {
use std::vector;
use pyth::keccak160;
use pyth::keccak160::Hash;
const LEAF_PREFIX: u8 = 0;
const NODE_PREFIX: u8 = 1;
fun hash_leaf(leaf: vector<u8>): Hash {
let leaf_data: vector<u8> = vector[LEAF_PREFIX];
vector::append(&mut leaf_data, leaf);
keccak160::from_data(leaf_data)
}
fun hash_node(node1: &Hash, node2: &Hash): Hash {
let node_data: vector<u8> = vector[NODE_PREFIX];
if (keccak160::is_smaller(node2, node1)) {
vector::append(&mut node_data, keccak160::get_data(node2));
vector::append(&mut node_data, keccak160::get_data(node1));
}
else {
vector::append(&mut node_data, keccak160::get_data(node1));
vector::append(&mut node_data, keccak160::get_data(node2));
};
keccak160::from_data(node_data)
}
public fun check(path: &vector<Hash>, root: &Hash, leaf: vector<u8>): bool {
let i = 0;
let current = hash_leaf(leaf);
while (i < vector::length(path)) {
current = hash_node(&current, vector::borrow(path, i));
i = i + 1;
};
&current == root
}
#[test]
fun test_hash_leaf() {
let data: vector<u8> = x"00640000000000000000000000000000000000000000000000000000000000000000000000000000640000000000000064000000640000000000000064000000000000006400000000000000640000000000000064";
let hash = hash_leaf(data);
let expected = keccak160::new(x"afc6a8ac466430f35895055f8a4c951785dad5ce");
assert!(&hash == &expected, 1);
}
#[test]
fun test_hash_node() {
let h1 = keccak160::new(x"05c51b04b820c0f704e3fdd2e4fc1e70aff26dff");
let h2 = keccak160::new(x"1e108841c8d21c7a5c4860c8c3499c918ea9e0ac");
let hash = hash_node(&h1, &h2);
let expected = keccak160::new(x"2d0e4fde68184c7ce8af426a0865bd41ef84dfa4");
assert!(&hash == &expected, 1);
}
#[test_only]
fun setup_tree(): (Hash, Hash, Hash, Hash, Hash, Hash, Hash) {
// h1 h2 h3 h4
// \ / \ /
// h5 h6
// \ /
// h7
let h1 = hash_leaf(x"adad11");
let h2 = hash_leaf(x"adad12");
let h3 = hash_leaf(x"adad13");
let h4 = hash_leaf(x"adad14");
let h5 = hash_node(&h1, &h2);
let h6 = hash_node(&h3, &h4);
let h7 = hash_node(&h5, &h6);
(h1, h2, h3, h4, h5, h6, h7)
}
#[test]
fun test_check_valid_proofs() {
let (h1, h2, h3, h4, h5, h6, h7) = setup_tree();
assert!(check(&vector[h2, h6], &h7, x"adad11"), 1);
assert!(check(&vector[h3, h5], &h7, x"adad14"), 1);
assert!(!check(&vector[h1, h4], &h7, x"adad14"), 1);
}
#[test]
fun test_check_valid_proofs_subtree() {
let (h1, h2, h3, h4, h5, h6, _) = setup_tree();
assert!(check(&vector[h1], &h5, x"adad12"), 1);
assert!(check(&vector[h2], &h5, x"adad11"), 1);
assert!(check(&vector[h3], &h6, x"adad14"), 1);
assert!(check(&vector[h4], &h6, x"adad13"), 1);
}
#[test]
fun test_check_invalid_proofs() {
let (h1, h2, h3, h4, h5, h6, h7) = setup_tree();
assert!(!check(&vector[h2, h6], &h7, x"dead"), 1);
assert!(!check(&vector[h3, h5], &h7, x"dead"), 1);
assert!(!check(&vector[h1, h4], &h7, x"dead"), 1);
}
}

View File

@ -46,7 +46,7 @@ module pyth::price_status {
}
#[test]
#[expected_failure(abort_code = 65559)]
#[expected_failure(abort_code = 65559, location = pyth::price_status)]
fun test_invalid_price_status() {
from_u64(3);
}

View File

@ -9,6 +9,8 @@ module pyth::pyth {
use pyth::price;
use pyth::data_source::{Self, DataSource};
use aptos_framework::timestamp;
use pyth::deserialize::{Self};
use wormhole::cursor::{Self, Cursor};
use std::vector;
use pyth::state;
use wormhole::vaa;
@ -19,12 +21,18 @@ module pyth::pyth {
use deployer::deployer;
use pyth::error;
use pyth::event;
use pyth::merkle;
use pyth::keccak160;
#[test_only]
friend pyth::pyth_test;
// -----------------------------------------------------------------------------
// Initialisation functions
const PYTHNET_ACCUMULATOR_UPDATE_MAGIC: u64 = 1347305813;
const ACCUMULATOR_UPDATE_WORMHOLE_VERIFICATION_MAGIC: u64 = 1096111958;
// -----------------------------------------------------------------------------
// Initialisation functions
public entry fun init(
deployer: &signer,
@ -163,20 +171,124 @@ module pyth::pyth {
};
}
fun update_price_feed_from_single_vaa(vaa: vector<u8>) {
// Deserialize the VAA
let vaa = vaa::parse_and_verify(vaa);
// Check that the VAA is from a valid data source (emitter)
fun verify_data_source(vaa: &vaa::VAA) {
assert!(
state::is_valid_data_source(
data_source::new(
u16::to_u64(vaa::get_emitter_chain(&vaa)),
vaa::get_emitter_address(&vaa))),
u16::to_u64(vaa::get_emitter_chain(vaa)),
vaa::get_emitter_address(vaa))),
error::invalid_data_source());
}
// Deserialize the batch price attestation
update_cache(batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::destroy(vaa))));
/// Given the payload of a VAA related to accumulator messages, asserts the verification variant is merkle and
/// extracts the merkle root digest
fun parse_accumulator_merkle_root_from_vaa_payload(message: vector<u8>): keccak160::Hash {
let msg_payload_cursor = cursor::init(message);
let payload_type = deserialize::deserialize_u32(&mut msg_payload_cursor);
assert!(payload_type == ACCUMULATOR_UPDATE_WORMHOLE_VERIFICATION_MAGIC, error::invalid_wormhole_message());
let wh_message_payload_type = deserialize::deserialize_u8(&mut msg_payload_cursor);
assert!(wh_message_payload_type == 0, error::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::rest(msg_payload_cursor);
keccak160::new(merkle_root_hash)
}
/// Given a single accumulator price update message, asserts that it is a PriceFeedMessage,
/// parses the info and returns a PriceInfo representing the encoded information
fun parse_accumulator_update_message(message: vector<u8>): PriceInfo {
let message_cur = cursor::init(message);
let message_type = deserialize::deserialize_u8(&mut message_cur);
assert!(message_type == 0, error::invalid_accumulator_message()); // PriceFeedMessage variant
let price_identifier = price_identifier::from_byte_vec(deserialize::deserialize_vector(&mut message_cur, 32));
let price = deserialize::deserialize_i64(&mut message_cur);
let conf = deserialize::deserialize_u64(&mut message_cur);
let expo = deserialize::deserialize_i32(&mut message_cur);
let publish_time = deserialize::deserialize_u64(&mut message_cur);
let _prev_publish_time = deserialize::deserialize_i64(&mut message_cur);
let ema_price = deserialize::deserialize_i64(&mut message_cur);
let ema_conf = deserialize::deserialize_u64(&mut message_cur);
let price_info = price_info::new(
timestamp::now_seconds(), // not used anywhere kept for backward compatibility
timestamp::now_seconds(),
price_feed::new(
price_identifier,
pyth::price::new(price, conf, expo, publish_time),
pyth::price::new(ema_price, ema_conf, expo, publish_time),
)
);
cursor::rest(message_cur);
price_info
}
/// Given a cursor at the beginning of accumulator price updates array data and a merkle_root hash,
/// parses the price updates and proofs, verifies the proofs against the merkle_root and
/// returns an array of PriceInfo representing the updates
fun parse_and_verify_accumulator_updates(
cursor: &mut Cursor<u8>,
merkle_root: &keccak160::Hash
): vector<PriceInfo> {
let update_size = deserialize::deserialize_u8(cursor);
let updates: vector<PriceInfo> = vector[];
while (update_size > 0) {
let message_size = deserialize::deserialize_u16(cursor);
let message = deserialize::deserialize_vector(cursor, message_size);
let update = parse_accumulator_update_message(message);
vector::push_back(&mut updates, update);
let path_size = deserialize::deserialize_u8(cursor);
let merkle_path: vector<keccak160::Hash> = vector[];
while (path_size > 0) {
let hash = deserialize::deserialize_vector(cursor, keccak160::get_hash_length());
vector::push_back(&mut merkle_path, keccak160::new(hash));
path_size = path_size - 1;
};
assert!(merkle::check(&merkle_path, merkle_root, message), error::invalid_proof());
update_size = update_size - 1;
};
updates
}
/// Given a cursor at the beginning of an accumulator message, verifies the validity of the message and the
/// embedded VAA, parses and verifies the price updates and returns an array of PriceInfo representing the updates
fun parse_and_verify_accumulator_message(cursor: &mut Cursor<u8>): vector<PriceInfo> {
let major = deserialize::deserialize_u8(cursor);
assert!(major == 1, error::invalid_accumulator_payload());
let _minor = deserialize::deserialize_u8(cursor);
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, error::invalid_accumulator_payload());
let vaa_size = deserialize::deserialize_u16(cursor);
let vaa = deserialize::deserialize_vector(cursor, vaa_size);
let msg_vaa = vaa::parse_and_verify(vaa);
verify_data_source(&msg_vaa);
let merkle_root_hash = parse_accumulator_merkle_root_from_vaa_payload(vaa::get_payload(&msg_vaa));
vaa::destroy(msg_vaa);
parse_and_verify_accumulator_updates(cursor, &merkle_root_hash)
}
fun update_price_feed_from_single_vaa(vaa: vector<u8>) {
let cur = cursor::init(vaa);
let header: u64 = deserialize::deserialize_u32(&mut cur);
let updates = if (header == PYTHNET_ACCUMULATOR_UPDATE_MAGIC) {
parse_and_verify_accumulator_message(&mut cur)
}
else {
// Deserialize the VAA
let vaa = vaa::parse_and_verify(vaa);
// Check that the VAA is from a valid data source (emitter)
verify_data_source(&vaa);
// Deserialize the batch price attestation
batch_price_attestation::destroy(batch_price_attestation::deserialize(vaa::destroy(vaa)))
};
update_cache(updates);
cursor::rest(cur);
}
/// Update the cache with given price updates, if they are newer than the ones currently cached.
@ -220,7 +332,7 @@ module pyth::pyth {
///
/// For a given price update i in the batch, that price is considered fresh if the current cached
/// price for price_identifiers[i] is older than publish_times[i].
public entry fun update_price_feeds_if_fresh(
public fun update_price_feeds_if_fresh(
vaas: vector<vector<u8>>,
price_identifiers: vector<vector<u8>>,
publish_times: vector<u64>,
@ -376,7 +488,23 @@ module pyth::pyth {
///
/// Please read more information about the update fee here: https://docs.pyth.network/consume-data/on-demand#fees
public fun get_update_fee(update_data: &vector<vector<u8>>): u64 {
state::get_base_update_fee() * vector::length(update_data)
let i = 0;
let total_updates = 0;
while (i < vector::length(update_data)) {
let cur = cursor::init(*vector::borrow(update_data, i));
let header: u64 = deserialize::deserialize_u32(&mut cur);
if (header == PYTHNET_ACCUMULATOR_UPDATE_MAGIC) {
//TODO: this may be expensive and can be optimized by not verifying the messages
let updates = parse_and_verify_accumulator_message(&mut cur);
total_updates = total_updates + vector::length(&updates);
}
else {
total_updates = total_updates + 1;
};
cursor::rest(cur);
i = i + 1;
};
state::get_base_update_fee() * total_updates
}
}
@ -398,6 +526,7 @@ module pyth::pyth_test {
use wormhole::external_address;
use std::account;
use std::signer;
use wormhole::wormhole;
#[test_only]
fun setup_test(
@ -470,7 +599,7 @@ module pyth::pyth_test {
price::new(i64::new(1508, false), 3, i64::new(5, true), 1663680745),
),
),
]
]
}
#[test_only]
@ -480,6 +609,26 @@ module pyth::pyth_test {
/// - payload corresponding to the batch price attestation of the prices returned by get_mock_price_infos()
const TEST_VAAS: vector<vector<u8>> = vector[x"0100000000010036eb563b80a24f4253bee6150eb8924e4bdf6e4fa1dfc759a6664d2e865b4b134651a7b021b7f1ce3bd078070b688b6f2e37ce2de0d9b48e6a78684561e49d5201527e4f9b00000001001171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000001005032574800030000000102000400951436e0be37536be96f0896366089506a59763d036728332d3e3038047851aea7c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1000000000000049a0000000000000008fffffffb00000000000005dc0000000000000003000000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000006150000000000000007215258d81468614f6b7e194c5d145609394f67b041e93e6695dcc616faadd0603b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe000000000000041a0000000000000003fffffffb00000000000005cb0000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e4000000000000048600000000000000078ac9cf3ab299af710d735163726fdae0db8465280502eb9f801f74b3c1bd190333832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d00000000000003f20000000000000002fffffffb00000000000005e70000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e40000000000000685000000000000000861db714e9ff987b6fedf00d01f9fea6db7c30632d6fc83b7bc9459d7192bc44a21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db800000000000006cb0000000000000001fffffffb00000000000005e40000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000007970000000000000001"];
#[test_only]
const TEST_ACCUMULATOR: vector<u8> = x"504e41550100000000a0010000000001005d461ac1dfffa8451edda17e4b28a46c8ae912422b2dc0cb7732828c497778ea27147fb95b4d250651931845e7f3e22c46326716bcf82be2874a9c9ab94b6e42000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000000004155575600000000000000000000000000da936d73429246d131873a0bab90ad7b416510be01005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf65f958f4883f9d2a8b5b1008d1fa01db95cf4a8c7000000006491cc757be59f3f377c0d3f423a695e81ad1eb504f8554c3620c3fd02f2ee15ea639b73fa3db9b34a245bdfa015c260c5a8a1180177cf30b2c0bebbb1adfe8f7985d051d2";
#[test_only]
const TEST_ACCUMULATOR_3_MSGS: vector<u8> = x"504e41550100000000a001000000000100d39b55fa311213959f91866d52624f3a9c07350d8956f6d42cfbb037883f31575c494a2f09fea84e4884dc9c244123fd124bc7825cd64d7c11e33ba5cfbdea7e010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000029da4c066b6e03b16a71e77811570dd9e19f258103005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60000000000000064000000000000003200000009000000006491cc747be59f3f377c0d3f000000000000006300000000000000340436992facb15658a7e9f08c4df4848ca80750f61fadcd96993de66b1fe7aef94e29e3bbef8b12db2305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d950055006e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af000000000000006500000000000000330000000a000000006491cc7504f8554c3620c3fd0000000000000064000000000000003504171ed10ac4f1eacf3a4951e1da6b119f07c45da5adcd96993de66b1fe7aef94e29e3bbef8b12db2305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d9500550031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68000000000000006600000000000000340000000b000000006491cc76e87d69c7b51242890000000000000065000000000000003604f2ee15ea639b73fa3db9b34a245bdfa015c260c5fe83e4772e0e346613de00e5348158a01bcb27b305a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
#[test_only]
const TEST_ACCUMULATOR_3_MSGS_LATER: vector<u8> = x"504e41550100000000a00100000000010056c1b66d4c93315ace5b0d8f5cee8ff068226e8623d7774389083a0ddf9952124cdc90c36f064c4e2b613a0dfe440c48892cf430b232abdc4993ce32463e4a1a000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000000004155575600000000000000000000000000cff05501cdaa105762dfedd238329fa3a96706dd03005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6000000000000006e000000000000003c00000013000000006491cc7e7be59f3f377c0d3f000000000000006d000000000000003e0445fb6c6bcdc996d401388588879afee37bc9c721394c4cc20e3b10b0c72d37ab53b0d56880d2d37905a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d950055006e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af000000000000006f000000000000003d00000014000000006491cc7f04f8554c3620c3fd000000000000006e000000000000003f04f41f65577b25a78041be51a163e2e6e991c4c92b394c4cc20e3b10b0c72d37ab53b0d56880d2d37905a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d9500550031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c680000000000000070000000000000003e00000015000000006491cc80e87d69c7b5124289000000000000006f000000000000004004f2ee15ea639b73fa3db9b34a245bdfa015c260c512ff930a26f8bce92f17545a6d60502c7767e71e05a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
#[test_only]
const TEST_ACCUMULATOR_INVALID_PROOF_1: vector<u8> = x"504e41550100000000a001000000000100110db9cd8325ccfab0dae92eeb9ea70a1faba5c5e96dc21ff46a8ddc560afc9a60df096b8ff21172804692bbdc958153e838437d8b474cbf45f0dc2a8acae831000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000000004155575600000000000000000000000000a8bea2b5f12f3177ff9b3929d77c3476ab2d32c602005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6fa75cd3aa3bb5ace5e2516446f71f85be36bd19bb0703f3154bb3db07be59f3f377c0d3f44661d9a8736c68884c8169e8b636ee3043202397384073120dce9e5d0efe24b44b4a0d62da8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d950055006e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af5f958f4883f9d2a8b5b1008d1fa01db95cf4a8c7423a695e81ad1eb504f8554c3620c3fd40b40f7d581ac802e2de5cb82a9ae672043202397384073120dce9e5d0efe24b44b4a0d62da8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
#[test_only]
const TEST_ACCUMULATOR_INVALID_ACC_MSG: vector<u8> = x"504e41550100000000a0010000000001005d461ac1dfffa8451edda17e4b28a46c8ae912422b2dc0cb7732828c497778ea27147fb95b4d250651931845e7f3e22c46326716bcf82be2874a9c9ab94b6e42000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000000004155575600000000000000000000000000da936d73429246d131873a0bab90ad7b416510be01005540b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf65f958f4883f9d2a8b5b1008d1fa01db95cf4a8c7000000006491cc757be59f3f377c0d3f423a695e81ad1eb504f8554c3620c3fd02f2ee15ea639b73fa3db9b34a245bdfa015c260c5a8a1180177cf30b2c0bebbb1adfe8f7985d051d2";
#[test_only]
const TEST_ACCUMULATOR_INVALID_WH_MSG: vector<u8> = x"504e41550100000000a001000000000100e87f98238c5357730936cfdfde3a37249e5219409a4f41b301924b8eb10815a43ea2f96e4fe1bc8cd398250f39448d3b8ca57c96f9cf7a2be292517280683caa010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b00000000000000000041555755000000000000000000000000000fb6f9f2b3b6cc1c9ef6708985fef226d92a3c0801005500b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6fa75cd3aa3bb5ace5e2516446f71f85be36bd19b000000006491cc747be59f3f377c0d3f44661d9a8736c68884c8169e8b636ee301f2ee15ea639b73fa3db9b34a245bdfa015c260c5";
#[test_only]
const TEST_ACCUMULATOR_INVALID_MAJOR_VERSION: vector<u8> = x"504e41553c00000000a001000000000100496b7fbd18dca2f0e690712fd8ca522ff79ca7d9d6d22e9f5d753fba4bd16fff440a811bad710071c79859290bcb1700de49dd8400db90b048437b521200123e010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000005f5db4488a7cae9f9a6c1938340c0fbf4beb9090200550031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6879bc5a3617ec3444d93c06501cf6a0909c38d4ec81d96026b71ec475e87d69c7b5124289adbf24212bed8c15db354391d2378d2e0454d2655c6c34e7e50580fd8c94511322968bbc6da8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95005500944998273e477b495144fb8794c914197f3ccb46be2900f4698fd0ef743c9695a573a6ff665ff63edb5f9a85ad579dc14500a2112c09680fc146134f9a539ca82cb6e3501c801278fd08d80732a24118292866bb049e6e88181a1e1e8b6d3c6bbb95135a73041f3b56a8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
#[test_only]
const TEST_ACCUMULATOR_INCREASED_MINOR_VERSION: vector<u8> = x"504e4155010a000000a001000000000100496b7fbd18dca2f0e690712fd8ca522ff79ca7d9d6d22e9f5d753fba4bd16fff440a811bad710071c79859290bcb1700de49dd8400db90b048437b521200123e010000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000005f5db4488a7cae9f9a6c1938340c0fbf4beb9090200550031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6879bc5a3617ec3444d93c06501cf6a0909c38d4ec81d96026b71ec475e87d69c7b5124289adbf24212bed8c15db354391d2378d2e0454d2655c6c34e7e50580fd8c94511322968bbc6da8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95005500944998273e477b495144fb8794c914197f3ccb46be2900f4698fd0ef743c9695a573a6ff665ff63edb5f9a85ad579dc14500a2112c09680fc146134f9a539ca82cb6e3501c801278fd08d80732a24118292866bb049e6e88181a1e1e8b6d3c6bbb95135a73041f3b56a8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
#[test_only]
const TEST_ACCUMULATOR_EXTRA_PAYLOAD: vector<u8> = x"504e41550100000000a001000000000100b2d11f181d81b4ff10beca30091754b464dc48bc1f7432d114f64a7a8f660e7964f2a0c6121bae6c1977514d46ee7a29d9395b20a45f2086071715c1dc19ab74000000000000000000000171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b000000000000000000415557560000000000000000000000000013f83cfdf63a5a1b3189182fa0a52e6de53ba7d002005d0031ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6879bc5a3617ec3444d93c06501cf6a0909c38d4ec81d96026b71ec475e87d69c7b5124289adbf24212bed8c15db354391d2378d2e000000000000000004a576f4a87f443f7d961a682f508c4f7b06ee1595a8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95005d00944998273e477b495144fb8794c914197f3ccb46be2900f4698fd0ef743c9695a573a6ff665ff63edb5f9a85ad579dc14500a2112c09680fc146134f9a539ca82cb6e3501c801278fd08d80732a24118292866bb0000000000000000045be67ba87a8dfbea404827ccbf07790299b6c023a8a1180177cf30b2c0bebbb1adfe8f7985d051d205a01e2504d9f0c06e7e7cb0cf24116098ca202ac5f6ade2e8f5a12ec006b16d46be1f0228b94d95";
#[test_only]
/// Allow anyone to update the cache with given updates. For testing purpose only.
public fun update_cache_for_test(updates: vector<PriceInfo>) {
@ -510,7 +659,7 @@ module pyth::pyth_test {
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 6)]
#[expected_failure(abort_code = 6, location = wormhole::vaa)]
fun test_update_price_feeds_corrupt_vaa(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], 50, 100);
@ -522,7 +671,7 @@ module pyth::pyth_test {
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65539)]
#[expected_failure(abort_code = 65539, location = pyth::pyth)]
fun test_update_price_feeds_invalid_data_source(aptos_framework: &signer) {
// Initialize the contract with some valid data sources, excluding our test VAA's source
let data_sources = vector<DataSource>[
@ -544,15 +693,17 @@ module pyth::pyth_test {
vector<DataSource>[
data_source::new(
1, external_address::from_bytes(x"0000000000000000000000000000000000000000000000000000000000000004")),
data_source::new(
data_source::new(
5, external_address::from_bytes(x"0000000000000000000000000000000000000000000000000000000000007637")),
data_source::new(
17, external_address::from_bytes(x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b"))
data_source::new(
17, external_address::from_bytes(x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b")),
data_source::new(
1, external_address::from_bytes(x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b")),
]
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65542)]
#[expected_failure(abort_code = 65542, location = pyth::pyth)]
fun test_update_price_feeds_insufficient_fee(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 1,
x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92",
@ -581,11 +732,307 @@ module pyth::pyth_test {
cleanup_test(burn_capability, mint_capability);
}
#[test_only]
fun setup_accumulator_test(
aptos_framework: &signer,
data_sources: vector<DataSource>,
to_mint: u64
): (BurnCapability<AptosCoin>, MintCapability<AptosCoin>, Coin<AptosCoin>) {
let aptos_framework_account = std::account::create_account_for_test(@aptos_framework);
std::timestamp::set_time_has_started_for_testing(&aptos_framework_account);
wormhole::init_test(
22,
1,
x"0000000000000000000000000000000000000000000000000000000000000004",
x"7E5F4552091A69125d5DfCb7b8C2659029395Bdf",
100000
);
// Set the current time
timestamp::update_global_time_for_test_secs(1687276659);
// Deploy and initialize a test instance of the Pyth contract
let deployer = account::create_signer_with_capability(
&account::create_test_signer_cap(@0x277fa055b6a73c42c0662d5236c65c864ccbf2d4abd21f174a30c8b786eab84b)
);
let (_pyth, signer_capability) = account::create_resource_account(&deployer, b"pyth");
pyth::init_test(signer_capability,
500,
1,
x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92",
data_sources,
50);
let (burn_capability, mint_capability) = aptos_coin::initialize_for_test(aptos_framework);
let coins = coin::mint(to_mint, &mint_capability);
(burn_capability, mint_capability, coins)
}
#[test(aptos_framework = @aptos_framework)]
fun test_accumulator_update_price(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
50
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR], coins);
let expected = vector<PriceInfo>[
price_info::new(
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
),
),
)];
check_price_feeds_cached(&expected);
cleanup_test(burn_capability, mint_capability);
}
#[test_only]
fun check_accumulator_test_price_feeds(offset: u64) {
let i = 0;
let feed_ids = vector[x"b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6",
x"6e1540171b6c0c960b71a7020d9f60077f6af931a8bbf590da0223dacf75c7af",
x"31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68"];
let expected: vector<PriceInfo> = vector[];
while (i < 3) {
vector::push_back(&mut expected, price_info::new(
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;
};
check_price_feeds_cached(&expected);
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65542, location = pyth::pyth)]
fun test_accumulator_update_price_feeds_insufficient_fee(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
149
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_3_MSGS], coins);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
fun test_accumulator_update_price_multi_feed(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
150
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_3_MSGS], coins);
check_accumulator_test_price_feeds(0);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
fun test_accumulator_update_price_out_of_order(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
300
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_3_MSGS_LATER, TEST_ACCUMULATOR_3_MSGS], coins);
check_accumulator_test_price_feeds(10);
// we pass the old message again in a separate call to make sure it will not overwrite the recent values in neither case
let coins_second_call = coin::mint(150, &mint_capability);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_3_MSGS], coins_second_call);
check_accumulator_test_price_feeds(10);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
fun test_accumulator_update_price_multi_msg(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
150
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_3_MSGS], coins);
check_accumulator_test_price_feeds(0);
let coins_second_call = coin::mint(150, &mint_capability);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_3_MSGS_LATER], coins_second_call);
check_accumulator_test_price_feeds(10);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65562, location = pyth::pyth)]
fun test_accumulator_invalid_payload(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
100
);
pyth::update_price_feeds(vector[x"504e415500000000"], coins);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65563, location = pyth::pyth)]
fun test_accumulator_invalid_accumulator_message(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
100
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_INVALID_ACC_MSG], coins);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65564, location = pyth::pyth)]
fun test_accumulator_invalid_wormhole_message(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
100
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_INVALID_WH_MSG], coins);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65562, location = pyth::pyth)]
fun test_accumulator_invalid_major_version(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
100
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_INVALID_MAJOR_VERSION], coins);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
fun test_accumulator_forward_compatibility(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
100
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_INCREASED_MINOR_VERSION], coins);
let coins_second_call = coin::mint(100, &mint_capability);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_EXTRA_PAYLOAD], coins_second_call);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65539, location = pyth::pyth)]
fun test_accumulator_invalid_data_source(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(aptos_framework, vector[data_source::new(
2, // correct emitter chain is 1
external_address::from_bytes(x"71f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b")
)], 100);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR], coins);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
fun test_accumulator_update_fee(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
0
);
let single_update_fee = 50;
assert!(pyth::get_update_fee(&vector[
TEST_ACCUMULATOR,
]) == single_update_fee, 1);
assert!(pyth::get_update_fee(&vector[
TEST_ACCUMULATOR,
TEST_ACCUMULATOR,
]) == single_update_fee * 2, 1);
assert!(pyth::get_update_fee(&vector[
TEST_ACCUMULATOR,
TEST_ACCUMULATOR_3_MSGS,
]) == single_update_fee * 4, 1);
assert!(pyth::get_update_fee(&vector[
TEST_ACCUMULATOR,
TEST_ACCUMULATOR_3_MSGS,
x"deaddeaddead", // random non-accumulator data
]) == single_update_fee * 5, 1);
coin::destroy_zero(coins);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65565, location = pyth::pyth)]
fun test_accumulator_invalid_proof(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_accumulator_test(
aptos_framework,
data_sources_for_test_vaa(),
100
);
pyth::update_price_feeds(vector[TEST_ACCUMULATOR_INVALID_PROOF_1], coins);
cleanup_test(burn_capability, mint_capability);
}
#[test(aptos_framework = @aptos_framework)]
fun test_update_price_feeds_with_funder(aptos_framework: &signer) {
let update_fee = 50;
let initial_balance = 75;
let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), update_fee, initial_balance);
let (burn_capability, mint_capability, coins) = setup_test(
aptos_framework,
500,
23,
x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92",
data_sources_for_test_vaa(),
update_fee,
initial_balance
);
// Create a test funder account and register it to store funds
let funder_addr = @0xbfbffd8e2af9a3e3ce20d2d2b22bd640;
@ -613,7 +1060,7 @@ module pyth::pyth_test {
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65542)]
#[expected_failure(abort_code = 65542, location = aptos_framework::coin)]
fun test_update_price_feeds_with_funder_insufficient_balance(aptos_framework: &signer) {
let update_fee = 50;
let initial_balance = 25;
@ -748,7 +1195,7 @@ module pyth::pyth_test {
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 524292)]
#[expected_failure(abort_code = 524292, location = pyth::pyth)]
fun test_stale_price_threshold_exceeded(aptos_framework: &signer) {
let stale_price_threshold = 500;
let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, stale_price_threshold, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), 50, 0);
@ -785,7 +1232,7 @@ module pyth::pyth_test {
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 524292)]
#[expected_failure(abort_code = 524292, location = pyth::pyth)]
fun test_stale_price_threshold_exceeded_ema(aptos_framework: &signer) {
let stale_price_threshold = 500;
let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, stale_price_threshold, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), 50, 0);
@ -824,7 +1271,7 @@ module pyth::pyth_test {
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 65541)]
#[expected_failure(abort_code = 65541, location = pyth::pyth)]
fun test_update_price_feeds_if_fresh_invalid_length(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), 50, 0);
@ -909,7 +1356,7 @@ module pyth::pyth_test {
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 524295)]
#[expected_failure(abort_code = 524295, location = pyth::pyth)]
fun test_update_price_feeds_if_fresh_stale_data(aptos_framework: &signer) {
let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 1, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", data_sources_for_test_vaa(), 50, 50);
@ -934,7 +1381,7 @@ module pyth::pyth_test {
}
#[test(aptos_framework = @aptos_framework)]
#[expected_failure(abort_code = 524295)]
#[expected_failure(abort_code = 524295, location = pyth::pyth)]
fun test_update_price_feeds_if_fresh_with_funder_stale_data(aptos_framework: &signer) {
let update_fee = 50;
let initial_balance = 75;