Skip to content

Commit e3fe85c

Browse files
authored
[Sui 18/x] - governance action tests (#949)
* on-chain tests for governance actions * edit name * edit * remove governance data source test that is not working * edit comment
1 parent eddb789 commit e3fe85c

File tree

6 files changed

+169
-4
lines changed

6 files changed

+169
-4
lines changed

target_chains/sui/contracts/sources/data_source.move

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module pyth::data_source {
1313
friend pyth::set_governance_data_source;
1414
#[test_only]
1515
friend pyth::pyth_tests;
16+
#[test_only]
17+
friend pyth::set_data_sources_tests;
1618

1719
const KEY: vector<u8> = b"data_sources";
1820
const E_DATA_SOURCE_REGISTRY_ALREADY_EXISTS: u64 = 0;

target_chains/sui/contracts/sources/governance/governance_action.move

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module pyth::governance_action {
2-
//use pyth::error;
32

43
const CONTRACT_UPGRADE: u8 = 0;
54
const SET_GOVERNANCE_DATA_SOURCE: u8 = 1;

target_chains/sui/contracts/sources/governance/set_data_sources.move

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,59 @@ module pyth::set_data_sources {
7272
}
7373
}
7474
}
75+
76+
#[test_only]
77+
module pyth::set_data_sources_tests {
78+
use sui::test_scenario::{Self};
79+
use sui::coin::Self;
80+
81+
use wormhole::governance_message::verify_vaa;
82+
use wormhole::external_address::{Self};
83+
use wormhole::bytes32::{Self};
84+
85+
use pyth::pyth_tests::{Self, setup_test, take_wormhole_and_pyth_states};
86+
use pyth::set_data_sources::{Self};
87+
use pyth::state::Self;
88+
use pyth::data_source::Self;
89+
90+
const SET_DATA_SOURCES_VAA: vector<u8> = x"01000000000100ac52663a7e50ab23db4f00f0607d930ffd438c5a214b3013418b57117590f76c32d2f790ec62be5f6e69d96273b1a567b8a698a8f5069c1ccd27a6874af2adc00100bc614e00000000000163278d271099bfd491951b3e648f08b1c71631e4a53674ad43e8f9f98068c38500000000000000010100000000000000000000000000000000000000000000000000000000000000010200155054474d01020015030001f346195ac02f37d60d4db8ffa6ef74cb1be3550047543a4a9ee9acf4d78697b0001aa27839d641b07743c0cb5f68c51f8cd31d2c0762bec00dc6fcd25433ef1ab5b6001ae101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71";
91+
// VAA Info:
92+
// module name: 0x1
93+
// action: 2
94+
// chain: 21
95+
// data sources (chain, addr) pairs: [(1, 0xf346195ac02f37d60d4db8ffa6ef74cb1be3550047543a4a9ee9acf4d78697b0), (26, 0xa27839d641b07743c0cb5f68c51f8cd31d2c0762bec00dc6fcd25433ef1ab5b6), (26, 0xe101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71)]
96+
97+
const DEPLOYER: address = @0x1234;
98+
const DEFAULT_BASE_UPDATE_FEE: u64 = 0;
99+
const DEFAULT_COIN_TO_MINT: u64 = 0;
100+
101+
#[test]
102+
fun set_data_sources(){
103+
let (scenario, test_coins, clock) = setup_test(500, 1, x"63278d271099bfd491951b3e648f08b1c71631e4a53674ad43e8f9f98068c385", pyth_tests::data_sources_for_test_vaa(), vector[x"13947bd48b18e53fdaeee77f3473391ac727c638"], DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
104+
test_scenario::next_tx(&mut scenario, DEPLOYER);
105+
let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
106+
107+
let ticket = set_data_sources::authorize_governance(&pyth_state, false);
108+
109+
let verified_vaa = wormhole::vaa::parse_and_verify(&mut worm_state, SET_DATA_SOURCES_VAA, &clock);
110+
111+
let receipt = verify_vaa(&worm_state, verified_vaa, ticket);
112+
113+
test_scenario::next_tx(&mut scenario, DEPLOYER);
114+
115+
pyth::governance::execute_governance_instruction(&mut pyth_state, receipt);
116+
117+
test_scenario::next_tx(&mut scenario, DEPLOYER);
118+
119+
// assert data sources are set correctly
120+
121+
assert!(state::is_valid_data_source(&pyth_state, data_source::new(1, external_address::new(bytes32::from_bytes(x"f346195ac02f37d60d4db8ffa6ef74cb1be3550047543a4a9ee9acf4d78697b0")))), 0);
122+
assert!(state::is_valid_data_source(&pyth_state, data_source::new(26, external_address::new(bytes32::from_bytes(x"a27839d641b07743c0cb5f68c51f8cd31d2c0762bec00dc6fcd25433ef1ab5b6")))), 0);
123+
assert!(state::is_valid_data_source(&pyth_state, data_source::new(26, external_address::new(bytes32::from_bytes(x"e101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71")))), 0);
124+
125+
// clean up
126+
coin::burn_for_testing(test_coins);
127+
pyth_tests::cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
128+
test_scenario::end(scenario);
129+
}
130+
}

target_chains/sui/contracts/sources/governance/set_stale_price_threshold.move

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,54 @@ module pyth::set_stale_price_threshold {
5050
}
5151
}
5252
}
53+
54+
#[test_only]
55+
module pyth::set_stale_price_threshold_test {
56+
use sui::test_scenario::{Self};
57+
use sui::coin::Self;
58+
59+
use wormhole::governance_message::verify_vaa;
60+
61+
use pyth::pyth_tests::{Self, setup_test, take_wormhole_and_pyth_states};
62+
use pyth::set_stale_price_threshold::{Self};
63+
use pyth::state::Self;
64+
65+
const SET_STALE_PRICE_THRESHOLD_VAA: vector<u8> = x"01000000000100196a91724d472b6c160c44ddcc9f9cef531aa95442739300023048bd066b77ca1a02bbfd9ff1799f3d63a4dd10c5348ab3b231e3bb66232e0cb4c07daa3647090100bc614e00000000000163278d271099bfd491951b3e648f08b1c71631e4a53674ad43e8f9f98068c38500000000000000010100000000000000000000000000000000000000000000000000000000000000010400155054474d0104001500000000000aee23";
66+
// VAA Info:
67+
// module name: 0x1
68+
// action: 4
69+
// chain: 21
70+
// stale price threshold: 716323
71+
72+
const DEPLOYER: address = @0x1234;
73+
const DEFAULT_BASE_UPDATE_FEE: u64 = 0;
74+
const DEFAULT_COIN_TO_MINT: u64 = 0;
75+
76+
#[test]
77+
fun set_stale_price_threshold(){
78+
79+
let (scenario, test_coins, clock) = setup_test(500, 1, x"63278d271099bfd491951b3e648f08b1c71631e4a53674ad43e8f9f98068c385", pyth_tests::data_sources_for_test_vaa(), vector[x"13947bd48b18e53fdaeee77f3473391ac727c638"], DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
80+
test_scenario::next_tx(&mut scenario, DEPLOYER);
81+
let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
82+
83+
let ticket = set_stale_price_threshold::authorize_governance(&pyth_state, false);
84+
85+
let verified_vaa = wormhole::vaa::parse_and_verify(&mut worm_state, SET_STALE_PRICE_THRESHOLD_VAA, &clock);
86+
87+
let receipt = verify_vaa(&worm_state, verified_vaa, ticket);
88+
89+
test_scenario::next_tx(&mut scenario, DEPLOYER);
90+
91+
pyth::governance::execute_governance_instruction(&mut pyth_state, receipt);
92+
93+
test_scenario::next_tx(&mut scenario, DEPLOYER);
94+
95+
// assert stale price threshold is set correctly
96+
assert!(state::get_stale_price_threshold_secs(&pyth_state)==716323, 0);
97+
98+
// clean up
99+
coin::burn_for_testing(test_coins);
100+
pyth_tests::cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
101+
test_scenario::end(scenario);
102+
}
103+
}

target_chains/sui/contracts/sources/governance/set_update_fee.move

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,54 @@ module pyth::set_update_fee {
6565
mantissa * math::pow(10, exponent)
6666
}
6767
}
68+
69+
#[test_only]
70+
module pyth::set_update_fee_tests {
71+
use sui::test_scenario::{Self};
72+
use sui::coin::Self;
73+
74+
use wormhole::governance_message::verify_vaa;
75+
76+
use pyth::pyth_tests::{Self, setup_test, take_wormhole_and_pyth_states};
77+
use pyth::set_update_fee::{Self};
78+
use pyth::state::Self;
79+
80+
const SET_FEE_VAA: vector<u8> = x"01000000000100e773bfd4a262ecd012333a953aadd243b8c116cc059b970ecb91216675eff89a39438570efb6eedcea15dad71d6ad0a18a7d01617e3cf61d53339df705a36df00100bc614e00000000000163278d271099bfd491951b3e648f08b1c71631e4a53674ad43e8f9f98068c38500000000000000010100000000000000000000000000000000000000000000000000000000000000010300155054474d0103001500000000000000030000000000000002";
81+
// VAA Info:
82+
// module name: 0x1
83+
// action: 3
84+
// chain: 21
85+
// new fee: 3, new exponent: 2
86+
87+
const DEPLOYER: address = @0x1234;
88+
const DEFAULT_BASE_UPDATE_FEE: u64 = 0;
89+
const DEFAULT_COIN_TO_MINT: u64 = 0;
90+
91+
#[test]
92+
fun set_update_fee(){
93+
94+
let (scenario, test_coins, clock) = setup_test(500, 1, x"63278d271099bfd491951b3e648f08b1c71631e4a53674ad43e8f9f98068c385", pyth_tests::data_sources_for_test_vaa(), vector[x"13947bd48b18e53fdaeee77f3473391ac727c638"], DEFAULT_BASE_UPDATE_FEE, DEFAULT_COIN_TO_MINT);
95+
test_scenario::next_tx(&mut scenario, DEPLOYER);
96+
let (pyth_state, worm_state) = take_wormhole_and_pyth_states(&scenario);
97+
98+
let ticket = set_update_fee::authorize_governance(&pyth_state, false);
99+
100+
let verified_vaa = wormhole::vaa::parse_and_verify(&mut worm_state, SET_FEE_VAA, &clock);
101+
102+
let receipt = verify_vaa(&worm_state, verified_vaa, ticket);
103+
104+
test_scenario::next_tx(&mut scenario, DEPLOYER);
105+
106+
pyth::governance::execute_governance_instruction(&mut pyth_state, receipt);
107+
108+
test_scenario::next_tx(&mut scenario, DEPLOYER);
109+
110+
// assert fee is set correctly
111+
assert!(state::get_base_update_fee(&pyth_state)==300, 0);
112+
113+
// clean up
114+
coin::burn_for_testing(test_coins);
115+
pyth_tests::cleanup_worm_state_pyth_state_and_clock(worm_state, pyth_state, clock);
116+
test_scenario::end(scenario);
117+
}
118+
}

target_chains/sui/contracts/sources/pyth.move

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ module pyth::pyth {
3636

3737
#[test_only]
3838
friend pyth::pyth_tests;
39+
#[test_only]
40+
friend pyth::set_data_sources_tests;
41+
#[test_only]
42+
friend pyth::set_stale_price_threshold_test;
43+
#[test_only]
44+
friend pyth::set_update_fee_tests;
3945

4046
/// Init state and emit event corresponding to Pyth initialization.
4147
public entry fun init_pyth(
@@ -742,7 +748,7 @@ module pyth::pyth_tests{
742748
test_scenario::end(scenario);
743749
}
744750

745-
fun data_sources_for_test_vaa(): vector<DataSource> {
751+
public fun data_sources_for_test_vaa(): vector<DataSource> {
746752
// Set some valid data sources, including our test VAA's source
747753
vector<DataSource>[
748754
data_source::new(
@@ -1544,13 +1550,13 @@ module pyth::pyth_tests{
15441550
)
15451551
}
15461552

1547-
fun cleanup_worm_state_pyth_state_and_clock(worm_state: WormState, pyth_state: PythState, clock: Clock){
1553+
public fun cleanup_worm_state_pyth_state_and_clock(worm_state: WormState, pyth_state: PythState, clock: Clock){
15481554
return_shared(worm_state);
15491555
return_shared(pyth_state);
15501556
clock::destroy_for_testing(clock);
15511557
}
15521558

1553-
fun take_wormhole_and_pyth_states(scenario: &Scenario): (PythState, WormState){
1559+
public fun take_wormhole_and_pyth_states(scenario: &Scenario): (PythState, WormState){
15541560
(take_shared<PythState>(scenario), take_shared<WormState>(scenario))
15551561
}
15561562
}

0 commit comments

Comments
 (0)