-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathtest_aggregation_zero_conf.rs
More file actions
206 lines (186 loc) · 7.33 KB
/
test_aggregation_zero_conf.rs
File metadata and controls
206 lines (186 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use {
crate::{
accounts::{
PermissionAccount,
PriceAccount,
PriceAccountFlags,
PythAccount,
},
c_oracle_header::{
PC_STATUS_TRADING,
PC_STATUS_UNKNOWN,
PC_VERSION,
},
deserialize::{
load_checked,
load_mut,
},
instruction::{
AddPublisherArgs,
OracleCommand,
UpdPriceArgs,
},
processor::{
process_instruction,
ALLOW_ZERO_CI,
FORBID_ZERO_CI,
},
tests::test_utils::{
update_clock_slot,
AccountSetup,
},
},
bytemuck::bytes_of,
solana_program::pubkey::Pubkey,
std::mem::size_of,
};
struct Accounts {
program_id: Pubkey,
publisher_account: AccountSetup,
funding_account: AccountSetup,
price_account: AccountSetup,
permissions_account: AccountSetup,
clock_account: AccountSetup,
}
impl Accounts {
fn new() -> Self {
let program_id = Pubkey::new_unique();
let publisher_account = AccountSetup::new_funding();
let clock_account = AccountSetup::new_clock();
let mut funding_account = AccountSetup::new_funding();
let mut permissions_account = AccountSetup::new_permission(&program_id);
let mut price_account = AccountSetup::new::<PriceAccount>(&program_id);
PriceAccount::initialize(&price_account.as_account_info(), PC_VERSION).unwrap();
{
let permissions_account_info = permissions_account.as_account_info();
let mut permissions_account_data =
PermissionAccount::initialize(&permissions_account_info, PC_VERSION).unwrap();
permissions_account_data.master_authority = *funding_account.as_account_info().key;
permissions_account_data.data_curation_authority =
*funding_account.as_account_info().key;
permissions_account_data.security_authority = *funding_account.as_account_info().key;
}
Self {
program_id,
publisher_account,
funding_account,
price_account,
permissions_account,
clock_account,
}
}
}
fn add_publisher(accounts: &mut Accounts, publisher: Option<Pubkey>) {
let args = AddPublisherArgs {
header: OracleCommand::AddPublisher.into(),
publisher: publisher.unwrap_or(*accounts.publisher_account.as_account_info().key),
};
assert!(process_instruction(
&accounts.program_id,
&[
accounts.funding_account.as_account_info(),
accounts.price_account.as_account_info(),
accounts.permissions_account.as_account_info(),
],
bytes_of::<AddPublisherArgs>(&args)
)
.is_ok());
}
fn update_price(accounts: &mut Accounts, price: i64, conf: u64, slot: u64) {
let instruction_data = &mut [0u8; size_of::<UpdPriceArgs>()];
let mut cmd = load_mut::<UpdPriceArgs>(instruction_data).unwrap();
cmd.header = OracleCommand::UpdPrice.into();
cmd.status = PC_STATUS_TRADING;
cmd.price = price;
cmd.confidence = conf;
cmd.publishing_slot = slot;
cmd.unused_ = 0;
let mut clock = accounts.clock_account.as_account_info();
clock.is_signer = false;
clock.is_writable = false;
process_instruction(
&accounts.program_id,
&[
accounts.publisher_account.as_account_info(),
accounts.price_account.as_account_info(),
clock,
],
instruction_data,
)
.unwrap();
}
#[test]
fn test_aggregate_v2_toggle() {
let accounts = &mut Accounts::new();
// Add an initial Publisher to test with.
add_publisher(accounts, None);
// Update the price, no aggregation will happen on the first slot.
{
update_clock_slot(&mut accounts.clock_account.as_account_info(), 1);
update_price(accounts, 42, 2, 1);
let info = accounts.price_account.as_account_info();
let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap();
assert_eq!(price_data.last_slot_, 0);
assert!(!price_data.flags.contains(PriceAccountFlags::ACCUMULATOR_V2));
}
// Update again, component is now TRADING so aggregation should trigger.
{
update_clock_slot(&mut accounts.clock_account.as_account_info(), 2);
update_price(accounts, 43, 3, 2);
let info = accounts.price_account.as_account_info();
let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap();
assert_eq!(price_data.agg_.status_, PC_STATUS_TRADING);
assert_eq!(price_data.last_slot_, 2);
assert_eq!(price_data.agg_.price_, 42);
assert_eq!(price_data.agg_.conf_, 2);
}
// Update again, but with confidence set to 0, it should not aggregate *in the next slot*.
{
update_clock_slot(&mut accounts.clock_account.as_account_info(), 3);
update_price(accounts, 44, 0, 3);
let info = accounts.price_account.as_account_info();
let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap();
assert_eq!(price_data.agg_.status_, PC_STATUS_TRADING);
assert_eq!(price_data.last_slot_, 3);
assert_eq!(price_data.agg_.price_, 43);
assert_eq!(price_data.agg_.conf_, 3);
}
// Update again, to trigger aggregation. We should see status go to unknown
{
update_clock_slot(&mut accounts.clock_account.as_account_info(), 4);
update_price(accounts, 45, 0, 4);
let info = accounts.price_account.as_account_info();
let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap();
println!("Price Data: {:?}", price_data.agg_);
assert_eq!(price_data.agg_.status_, PC_STATUS_UNKNOWN);
assert_eq!(price_data.last_slot_, 3);
}
// Enable allow zero confidence bit
add_publisher(accounts, Some(ALLOW_ZERO_CI.into()));
// Update again, with allow zero confidence bit set, aggregation should support
// zero confidence values. Note that we don't need to do this twice, because the
// price with ci zero was already stored in the previous slot.
{
update_clock_slot(&mut accounts.clock_account.as_account_info(), 5);
update_price(accounts, 46, 0, 5);
let info = accounts.price_account.as_account_info();
let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap();
assert!(price_data.flags.contains(PriceAccountFlags::ALLOW_ZERO_CI));
assert_eq!(price_data.agg_.status_, PC_STATUS_TRADING);
assert_eq!(price_data.last_slot_, 5);
assert_eq!(price_data.agg_.price_, 45);
assert_eq!(price_data.agg_.conf_, 0);
}
// Disable allow zero confidence bit
add_publisher(accounts, Some(FORBID_ZERO_CI.into()));
// Update again, with forbid zero confidence bit set, aggregation should have status
// of unknown
{
update_clock_slot(&mut accounts.clock_account.as_account_info(), 6);
update_price(accounts, 47, 0, 6);
let info = accounts.price_account.as_account_info();
let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap();
assert!(!price_data.flags.contains(PriceAccountFlags::ALLOW_ZERO_CI));
assert_eq!(price_data.agg_.status_, PC_STATUS_UNKNOWN);
}
}