-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(oracle): WIP add scores account #407
Open
keyvankhademi
wants to merge
39
commits into
main
Choose a base branch
from
scores
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 27 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
b343951
feat(oracle): WIP add scores account
keyvankhademi 1561e80
feat: use bitset to reduce scores account size
keyvankhademi b58625c
fix: pre-commit
keyvankhademi 18c6787
fix: add price test
keyvankhademi 32c577e
fix: rename score to cap
keyvankhademi 2ec0004
feat: remove publisher sorting features
keyvankhademi a02a10b
feat: improve caps account calculations
keyvankhademi 0c98527
feat: rename score to publisher_caps
keyvankhademi 87f61ec
fix: bypass false positive clippy
keyvankhademi 2f6f359
feat: optimize oracle command enum
keyvankhademi 2697cb2
feat: test new way of calculating score
keyvankhademi d07a256
fix: correct rust doc format
keyvankhademi 98a6a52
fix: clippy
keyvankhademi 4260e2e
feat: add vscode settings
keyvankhademi 6c9d3e6
fix: clippy warning
keyvankhademi 8e00103
feat: change OracleCommand repr from i32 to u32
keyvankhademi aa1e0a7
feat: add initial tests for publisher caps
keyvankhademi 2372be6
feat: add migration step
keyvankhademi 63352ec
test: try a different implementation for program size
keyvankhademi aaa12bf
test: try manual swap to decrease program size
keyvankhademi e2aa645
fix: add price when initializing the caps account
keyvankhademi 2169e8d
fix: rename scores account to cap account
keyvankhademi de0f7c3
refactor: rename calculate_scores to calculate_caps
keyvankhademi 4396054
refactor: rename symbols to prices in PublisherCapsAccount
keyvankhademi 0563db6
refactor: update publisher permission index to use price instead of s…
keyvankhademi 6c321bf
refactor: update publisher permission index to use price instead of s…
keyvankhademi 9e90897
chore: Update num-traits dependency to version 0.2
keyvankhademi 5c1f750
feat: add initial quickcheck for caps account
keyvankhademi 02f6257
refactor: update upd_price.rs to use send_message_to_message_buffer f…
keyvankhademi 7800412
feat: send caps to message buffer for add publisher
keyvankhademi 9b52a40
fix: comment migration code to test program size
keyvankhademi a18ec0c
feat: remove init mapping to reduce program size
keyvankhademi 5932402
feat: keep init mapping for tests
keyvankhademi 21c38c8
fix: self ref error
keyvankhademi 413abc3
fix: revert mapping account changes
keyvankhademi 23a1da8
feat: add cfg test flag for init mapping
keyvankhademi 1a11ca4
temporary remove pipeline test
keyvankhademi 1fa4c6a
remove upd_permissions to check size
keyvankhademi dd453fb
add feature test to be able to compile oracle for test
keyvankhademi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"rust-analyzer.check.command": "clippy", | ||
"editor.defaultFormatter": "rust-lang.rust-analyzer", | ||
"rust-analyzer.rustfmt.overrideCommand": [ | ||
"rustfmt", | ||
"+nightly" | ||
], | ||
"editor.formatOnSave": true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,307 @@ | ||
use { | ||
super::{ | ||
AccountHeader, | ||
PythAccount, | ||
}, | ||
crate::c_oracle_header::{ | ||
PC_ACCTYPE_SCORE, | ||
PC_MAX_PUBLISHERS, | ||
PC_MAX_SYMBOLS, | ||
PC_MAX_SYMBOLS_64, | ||
}, | ||
bytemuck::{ | ||
Pod, | ||
Zeroable, | ||
}, | ||
solana_program::{ | ||
program_error::ProgramError, | ||
pubkey::Pubkey, | ||
}, | ||
std::cmp::max, | ||
}; | ||
|
||
#[repr(C)] | ||
#[derive(Copy, Clone, Pod, Zeroable)] | ||
|
||
/// This account is part of Community Integrity Pool (CIP) project. | ||
/// It is used to store the caps of the publishers which will be sent | ||
/// to the `integrity_pool` program on mainnet to calculate the rewards. | ||
pub struct PublisherCapsAccount { | ||
pub header: AccountHeader, | ||
pub num_publishers: usize, | ||
pub num_symbols: usize, | ||
// Z is a constant used to normalize the caps | ||
pub z: u64, | ||
// M is a constant showing the target stake per symbol | ||
pub m: u64, | ||
|
||
// array[x][y] is a u64 whose bits represent if publisher x publishes symbols 64*y to 64*(y+1) - 1 | ||
pub publisher_permissions: [[u64; PC_MAX_SYMBOLS_64 as usize]; PC_MAX_PUBLISHERS as usize], | ||
pub caps: [u64; PC_MAX_PUBLISHERS as usize], | ||
pub publishers: [Pubkey; PC_MAX_PUBLISHERS as usize], | ||
pub prices: [Pubkey; PC_MAX_SYMBOLS as usize], | ||
} | ||
|
||
impl PublisherCapsAccount { | ||
fn get_publisher_permission(&self, x: usize, y: usize) -> bool { | ||
(self.publisher_permissions[x][y / 64] >> (y % 64)) & 1 == 1 | ||
} | ||
|
||
fn set_publisher_permission(&mut self, x: usize, y: usize, value: bool) { | ||
if value { | ||
self.publisher_permissions[x][y / 64] |= 1 << (y % 64); | ||
} else { | ||
self.publisher_permissions[x][y / 64] &= !(1 << (y % 64)); | ||
} | ||
} | ||
|
||
pub fn add_publisher( | ||
&mut self, | ||
publisher: Pubkey, | ||
price_account: Pubkey, | ||
) -> Result<(), ProgramError> { | ||
let publisher_index = self | ||
.publishers | ||
.iter() | ||
.position(|&x| x == publisher) | ||
.unwrap_or(self.num_publishers); | ||
|
||
if publisher_index == self.num_publishers { | ||
if self.num_publishers == PC_MAX_PUBLISHERS as usize { | ||
return Err(ProgramError::AccountDataTooSmall); | ||
} | ||
|
||
self.publishers[self.num_publishers] = publisher; | ||
self.num_publishers += 1; | ||
} | ||
|
||
let symbol_index = self | ||
.prices | ||
.iter() | ||
.position(|&x| x == price_account) | ||
.ok_or(ProgramError::InvalidArgument)?; | ||
|
||
self.set_publisher_permission(publisher_index, symbol_index, true); | ||
|
||
self.calculate_caps() | ||
} | ||
|
||
pub fn del_publisher( | ||
&mut self, | ||
publisher: Pubkey, | ||
price_account: Pubkey, | ||
) -> Result<(), ProgramError> { | ||
let publisher_index = self | ||
.publishers | ||
.iter() | ||
.position(|&x| x == publisher) | ||
.ok_or(ProgramError::InvalidArgument)?; | ||
|
||
let price_index = self | ||
.prices | ||
.iter() | ||
.position(|&x| x == price_account) | ||
.ok_or(ProgramError::InvalidArgument)?; | ||
|
||
self.set_publisher_permission(publisher_index, price_index, false); | ||
self.calculate_caps() | ||
} | ||
|
||
pub fn add_price(&mut self, symbol: Pubkey) -> Result<(), ProgramError> { | ||
let price_index = self | ||
.prices | ||
.iter() | ||
.position(|&x| x == symbol) | ||
.unwrap_or(self.num_symbols); | ||
|
||
if price_index == self.num_symbols { | ||
if self.num_symbols == PC_MAX_SYMBOLS as usize { | ||
return Err(ProgramError::AccountDataTooSmall); | ||
} | ||
|
||
self.prices[self.num_symbols] = symbol; | ||
self.num_symbols += 1; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn del_price(&mut self, symbol: Pubkey) -> Result<(), ProgramError> { | ||
let price_index = self | ||
.prices | ||
.iter() | ||
.position(|&x| x == symbol) | ||
.ok_or(ProgramError::InvalidArgument)?; | ||
|
||
// update symbol list | ||
self.prices[price_index] = self.prices[self.num_symbols - 1]; | ||
self.prices[self.num_symbols - 1] = Pubkey::default(); | ||
|
||
// update publisher permissions | ||
for i in 0..self.num_publishers { | ||
let value = self.get_publisher_permission(i, self.num_symbols - 1); | ||
self.set_publisher_permission(i, price_index, value) | ||
} | ||
|
||
self.num_symbols -= 1; | ||
self.calculate_caps() | ||
} | ||
|
||
|
||
pub fn calculate_caps(&mut self) -> Result<(), ProgramError> { | ||
let price_weights: Vec<u64> = self | ||
.prices | ||
.iter() | ||
.enumerate() | ||
.map(|(j, _)| { | ||
let weight = self | ||
.publisher_permissions | ||
.iter() | ||
.enumerate() | ||
.filter(|(i, _)| self.get_publisher_permission(*i, j)) | ||
.count() as u64; | ||
max(weight, self.z) | ||
}) | ||
.collect(); | ||
|
||
for i in 0..self.num_publishers { | ||
self.caps[i] = self | ||
.prices | ||
.iter() | ||
.enumerate() | ||
.filter(|(j, _)| self.get_publisher_permission(i, *j)) | ||
.map(|(j, _)| self.m * 1_000_000_000_u64 / price_weights[j]) | ||
.sum(); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl PythAccount for PublisherCapsAccount { | ||
const ACCOUNT_TYPE: u32 = PC_ACCTYPE_SCORE; | ||
// Calculate the initial size of the account | ||
const INITIAL_SIZE: u32 = 75824; | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use { | ||
super::*, | ||
crate::c_oracle_header::{ | ||
PC_ACCTYPE_SCORE, | ||
PC_VERSION, | ||
}, | ||
solana_program::pubkey::Pubkey, | ||
std::mem::size_of, | ||
}; | ||
|
||
fn get_empty_account() -> PublisherCapsAccount { | ||
PublisherCapsAccount { | ||
header: AccountHeader { | ||
magic_number: PC_ACCTYPE_SCORE, | ||
account_type: PC_ACCTYPE_SCORE, | ||
version: PC_VERSION, | ||
size: size_of::<PublisherCapsAccount>() as u32, | ||
}, | ||
num_publishers: 0, | ||
num_symbols: 0, | ||
z: 2, | ||
m: 1000, | ||
publisher_permissions: [[0; PC_MAX_SYMBOLS_64 as usize]; PC_MAX_PUBLISHERS as usize], | ||
caps: [0; PC_MAX_PUBLISHERS as usize], | ||
publishers: [Pubkey::default(); PC_MAX_PUBLISHERS as usize], | ||
prices: [Pubkey::default(); PC_MAX_SYMBOLS as usize], | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_size_of_publisher_caps_account() { | ||
let account = get_empty_account(); | ||
assert_eq!( | ||
PublisherCapsAccount::INITIAL_SIZE as usize, | ||
size_of::<PublisherCapsAccount>() | ||
); | ||
assert_eq!( | ||
account.header.size as usize, | ||
size_of::<PublisherCapsAccount>() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_publisher_caps_account() { | ||
let mut account = get_empty_account(); | ||
|
||
let publisher1 = Pubkey::new_unique(); | ||
let publisher2 = Pubkey::new_unique(); | ||
let publisher3 = Pubkey::new_unique(); | ||
let symbol1 = Pubkey::new_unique(); | ||
let symbol2 = Pubkey::new_unique(); | ||
|
||
account.add_price(symbol1).unwrap(); | ||
account.add_price(symbol2).unwrap(); | ||
|
||
account.add_publisher(publisher1, symbol1).unwrap(); | ||
assert_eq!(account.caps[..2], [account.m * 500_000_000, 0]); | ||
|
||
account.add_publisher(publisher1, symbol2).unwrap(); | ||
assert!(account.get_publisher_permission(0, 0)); | ||
assert!(account.get_publisher_permission(0, 1)); | ||
assert_eq!(account.caps[..2], [account.m * 1_000_000_000, 0]); | ||
|
||
|
||
account.add_publisher(publisher2, symbol1).unwrap(); | ||
assert_eq!( | ||
account.caps[..2], | ||
[account.m * 1_000_000_000, account.m * 500_000_000] | ||
); | ||
|
||
account.add_publisher(publisher2, symbol2).unwrap(); | ||
assert!(account.get_publisher_permission(1, 0)); | ||
assert!(account.get_publisher_permission(1, 1)); | ||
assert_eq!( | ||
account.caps[..2], | ||
[account.m * 1_000_000_000, account.m * 1_000_000_000] | ||
); | ||
|
||
account.add_publisher(publisher3, symbol1).unwrap(); | ||
assert_eq!( | ||
account.caps[..3], | ||
[833_333_333_333, 833_333_333_333, 333333333333] | ||
); | ||
|
||
account.del_publisher(publisher1, symbol1).unwrap(); | ||
assert_eq!(account.num_publishers, 3); | ||
assert_eq!(account.num_symbols, 2); | ||
assert!(!account.get_publisher_permission(0, 0)); | ||
assert!(account.get_publisher_permission(0, 1)); | ||
assert!(account.get_publisher_permission(1, 0)); | ||
assert!(account.get_publisher_permission(1, 1)); | ||
|
||
account.del_publisher(publisher1, symbol2).unwrap(); | ||
assert_eq!(account.num_publishers, 3); | ||
assert_eq!(account.num_symbols, 2); | ||
assert!(!account.get_publisher_permission(0, 0)); | ||
assert!(!account.get_publisher_permission(0, 1)); | ||
assert!(account.get_publisher_permission(1, 0)); | ||
assert!(account.get_publisher_permission(1, 1)); | ||
|
||
account.del_publisher(publisher2, symbol1).unwrap(); | ||
assert_eq!(account.num_publishers, 3); | ||
assert_eq!(account.num_symbols, 2); | ||
assert!(!account.get_publisher_permission(0, 0)); | ||
assert!(!account.get_publisher_permission(0, 1)); | ||
assert!(!account.get_publisher_permission(1, 0)); | ||
assert!(account.get_publisher_permission(1, 1)); | ||
|
||
account.del_publisher(publisher2, symbol2).unwrap(); | ||
assert_eq!(account.num_publishers, 3); | ||
assert_eq!(account.num_symbols, 2); | ||
assert!(!account.get_publisher_permission(0, 0)); | ||
assert!(!account.get_publisher_permission(0, 1)); | ||
assert!(!account.get_publisher_permission(1, 0)); | ||
assert!(!account.get_publisher_permission(1, 1)); | ||
|
||
assert_eq!(account.caps[..3], [0, 0, 500000000000]); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what we do if we exceed this numbers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a Pythnet v2 problem anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The program doesn't allow exceeding these numbers, we will probably face a lot of issues if we decide to increase these numbers. We are hoping to go to pythnet V2 before we need to increase these numbers. From the trasactions limit that we currently have, i don't think this will an issue.
But if you think there is a chance that we exceed these number before pythnet v2, we better address it now.