-
Notifications
You must be signed in to change notification settings - Fork 119
Rust instructions #264
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
Merged
Merged
Rust instructions #264
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8e36ac0
OracleCommand struct
guibescos 355bca0
Cleanup entrypoint:
guibescos e9ff9eb
Format
guibescos be189b4
Cleanup
guibescos d9abc74
Additional comment
guibescos 89e5940
Merge branch 'main' into rust-instructions
guibescos 2681acf
Delete accidental files
guibescos dc4eec5
Explicit instruction order
guibescos 1b5f746
Merge branch 'main' into rust-instructions
guibescos 5d613b1
Fix rust oracle
guibescos 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,107 @@ | ||
| use crate::c_oracle_header::PC_VERSION; | ||
| use crate::deserialize::load; | ||
| use crate::error::OracleError; | ||
| use bytemuck::{ | ||
| Pod, | ||
| Zeroable, | ||
| }; | ||
| use num_derive::{ | ||
| FromPrimitive, | ||
| ToPrimitive, | ||
| }; | ||
| use num_traits::FromPrimitive; | ||
|
|
||
| /// WARNING : NEW COMMANDS SHOULD BE ADDED AT THE END OF THE LIST | ||
| #[repr(i32)] | ||
| #[derive(FromPrimitive, ToPrimitive)] | ||
| pub enum OracleCommand { | ||
| // initialize first mapping list account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] mapping account [signer writable] | ||
| InitMapping = 0, | ||
| // initialize and add new mapping account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] tail mapping account [signer writable] | ||
| // account[2] new mapping account [signer writable] | ||
| AddMapping = 1, | ||
| // initialize and add new product reference data account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] mapping account [signer writable] | ||
| // account[2] new product account [signer writable] | ||
| AddProduct = 2, | ||
| // update product account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] product account [signer writable] | ||
| UpdProduct = 3, | ||
| // add new price account to a product account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] product account [signer writable] | ||
| // account[2] new price account [signer writable] | ||
| AddPrice = 4, | ||
| // add publisher to symbol account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] price account [signer writable] | ||
| AddPublisher = 5, | ||
| // delete publisher from symbol account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] price account [signer writable] | ||
| DelPublisher = 6, | ||
| // publish component price | ||
| // account[0] funding account [signer writable] | ||
| // account[1] price account [writable] | ||
| // account[2] sysvar_clock account [] | ||
| UpdPrice = 7, | ||
| // compute aggregate price | ||
| // account[0] funding account [signer writable] | ||
| // account[1] price account [writable] | ||
| // account[2] sysvar_clock account [] | ||
| AggPrice = 8, | ||
| // (re)initialize price account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] new price account [signer writable] | ||
| InitPrice = 9, | ||
| // deprecated | ||
| InitTest = 10, | ||
| // deprecated | ||
| UpdTest = 11, | ||
| // set min publishers | ||
| // account[0] funding account [signer writable] | ||
| // account[1] price account [signer writable] | ||
| SetMinPub = 12, | ||
| // publish component price, never returning an error even if the update failed | ||
| // account[0] funding account [signer writable] | ||
| // account[1] price account [writable] | ||
| // account[2] sysvar_clock account [] | ||
| UpdPriceNoFailOnError = 13, | ||
| // resizes a price account so that it fits the Time Machine | ||
| // account[0] funding account [signer writable] | ||
| // account[1] price account [signer writable] | ||
| // account[2] system program [] | ||
| ResizePriceAccount = 14, | ||
| // deletes a price account | ||
| // account[0] funding account [signer writable] | ||
| // account[1] product account [signer writable] | ||
| // account[2] price account [signer writable] | ||
| DelPrice = 15, | ||
| // deletes a product account | ||
| // key[0] funding account [signer writable] | ||
| // key[1] mapping account [signer writable] | ||
| // key[2] product account [signer writable] | ||
| DelProduct = 16, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Zeroable, Pod, Copy, Clone)] | ||
| pub struct CommandHeader { | ||
| pub version: u32, | ||
| pub command: i32, | ||
| } | ||
|
|
||
| pub fn load_command_header_checked(data: &[u8]) -> Result<OracleCommand, OracleError> { | ||
| let command_header = load::<CommandHeader>(data)?; | ||
|
|
||
| if command_header.version != PC_VERSION { | ||
| return Err(OracleError::InvalidInstructionVersion); | ||
| } | ||
| OracleCommand::from_i32(command_header.command).ok_or(OracleError::UnrecognizedInstruction) | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,26 +1,9 @@ | ||
| use crate::c_oracle_header::{ | ||
| cmd_hdr, | ||
| command_t_e_cmd_add_mapping, | ||
| command_t_e_cmd_add_price, | ||
| command_t_e_cmd_add_product, | ||
| command_t_e_cmd_add_publisher, | ||
| command_t_e_cmd_agg_price, | ||
| command_t_e_cmd_del_price, | ||
| command_t_e_cmd_del_product, | ||
| command_t_e_cmd_del_publisher, | ||
| command_t_e_cmd_init_mapping, | ||
| command_t_e_cmd_init_price, | ||
| command_t_e_cmd_resize_price_account, | ||
| command_t_e_cmd_set_min_pub, | ||
| command_t_e_cmd_upd_price, | ||
| command_t_e_cmd_upd_price_no_fail_on_error, | ||
| command_t_e_cmd_upd_product, | ||
| PC_VERSION, | ||
| }; | ||
| use crate::deserialize::load; | ||
| use crate::error::OracleError; | ||
| use crate::instruction::{ | ||
| load_command_header_checked, | ||
| OracleCommand, | ||
| }; | ||
| use solana_program::entrypoint::ProgramResult; | ||
| use solana_program::program_error::ProgramError; | ||
| use solana_program::pubkey::Pubkey; | ||
| use solana_program::sysvar::slot_history::AccountInfo; | ||
|
|
||
|
|
@@ -47,37 +30,27 @@ pub fn process_instruction( | |
| accounts: &[AccountInfo], | ||
| instruction_data: &[u8], | ||
| ) -> ProgramResult { | ||
| let cmd_data = load::<cmd_hdr>(instruction_data)?; | ||
|
|
||
| if cmd_data.ver_ != PC_VERSION { | ||
| return Err(ProgramError::InvalidArgument); | ||
| } | ||
|
|
||
| match cmd_data | ||
| .cmd_ | ||
| .try_into() | ||
| .map_err(|_| OracleError::IntegerCastingError)? | ||
| { | ||
| command_t_e_cmd_upd_price | command_t_e_cmd_agg_price => { | ||
| upd_price(program_id, accounts, instruction_data) | ||
| } | ||
| command_t_e_cmd_upd_price_no_fail_on_error => { | ||
| match load_command_header_checked(instruction_data)? { | ||
| OracleCommand::InitMapping => init_mapping(program_id, accounts, instruction_data), | ||
| OracleCommand::AddMapping => add_mapping(program_id, accounts, instruction_data), | ||
| OracleCommand::AddProduct => add_product(program_id, accounts, instruction_data), | ||
| OracleCommand::UpdProduct => upd_product(program_id, accounts, instruction_data), | ||
| OracleCommand::AddPrice => add_price(program_id, accounts, instruction_data), | ||
| OracleCommand::AddPublisher => add_publisher(program_id, accounts, instruction_data), | ||
| OracleCommand::DelPublisher => del_publisher(program_id, accounts, instruction_data), | ||
| OracleCommand::UpdPrice => upd_price(program_id, accounts, instruction_data), | ||
| OracleCommand::AggPrice => upd_price(program_id, accounts, instruction_data), | ||
| OracleCommand::InitPrice => init_price(program_id, accounts, instruction_data), | ||
| OracleCommand::InitTest => Err(OracleError::UnrecognizedInstruction.into()), | ||
| OracleCommand::UpdTest => Err(OracleError::UnrecognizedInstruction.into()), | ||
| OracleCommand::SetMinPub => set_min_pub(program_id, accounts, instruction_data), | ||
| OracleCommand::UpdPriceNoFailOnError => { | ||
| upd_price_no_fail_on_error(program_id, accounts, instruction_data) | ||
| } | ||
| command_t_e_cmd_resize_price_account => { | ||
| OracleCommand::ResizePriceAccount => { | ||
| resize_price_account(program_id, accounts, instruction_data) | ||
| } | ||
| command_t_e_cmd_add_price => add_price(program_id, accounts, instruction_data), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you delete the enum in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some things still depend, just wanted to stage it incrementally |
||
| command_t_e_cmd_init_mapping => init_mapping(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_init_price => init_price(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_add_mapping => add_mapping(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_add_publisher => add_publisher(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_del_publisher => del_publisher(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_add_product => add_product(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_upd_product => upd_product(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_set_min_pub => set_min_pub(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_del_price => del_price(program_id, accounts, instruction_data), | ||
| command_t_e_cmd_del_product => del_product(program_id, accounts, instruction_data), | ||
| _ => Err(OracleError::UnrecognizedInstruction.into()), | ||
| OracleCommand::DelPrice => del_price(program_id, accounts, instruction_data), | ||
| OracleCommand::DelProduct => del_product(program_id, accounts, instruction_data), | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.