-
Notifications
You must be signed in to change notification settings - Fork 7
geolocation: add AddParentDevice and RemoveParentDevice instructions #3151
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
Open
nikw9944
wants to merge
2
commits into
main
Choose a base branch
from
bdz/doublezero-2952-part-3-parent-devices
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
86 changes: 86 additions & 0 deletions
86
smartcontract/programs/doublezero-geolocation/src/processors/geo_probe/add_parent_device.rs
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,86 @@ | ||
| use crate::{ | ||
| error::GeolocationError, processors::check_foundation_allowlist, serializer::try_acc_write, | ||
| state::geo_probe::{GeoProbe, MAX_PARENT_DEVICES}, | ||
| }; | ||
| use solana_program::{ | ||
| account_info::{next_account_info, AccountInfo}, | ||
| entrypoint::ProgramResult, | ||
| msg, | ||
| program_error::ProgramError, | ||
| pubkey::Pubkey, | ||
| }; | ||
|
|
||
| pub fn process_add_parent_device(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult { | ||
| let accounts_iter = &mut accounts.iter(); | ||
|
|
||
| let probe_account = next_account_info(accounts_iter)?; | ||
| let device_account = next_account_info(accounts_iter)?; | ||
| let program_config_account = next_account_info(accounts_iter)?; | ||
| let serviceability_globalstate_account = next_account_info(accounts_iter)?; | ||
| let payer_account = next_account_info(accounts_iter)?; | ||
| let _system_program = next_account_info(accounts_iter)?; | ||
|
|
||
| if !payer_account.is_signer { | ||
| msg!("Payer must be a signer"); | ||
| return Err(ProgramError::MissingRequiredSignature); | ||
| } | ||
|
|
||
| check_foundation_allowlist( | ||
| program_config_account, | ||
| serviceability_globalstate_account, | ||
| payer_account, | ||
| program_id, | ||
| )?; | ||
|
|
||
| if probe_account.owner != program_id { | ||
| msg!("Invalid GeoProbe Account Owner"); | ||
| return Err(ProgramError::IllegalOwner); | ||
| } | ||
| if !probe_account.is_writable { | ||
| msg!("GeoProbe account must be writable"); | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
|
|
||
| // Validate device_account belongs to the Serviceability program | ||
| let serviceability_program_id = crate::serviceability_program_id(); | ||
| if *device_account.owner != serviceability_program_id { | ||
| msg!( | ||
| "Device account owner {} does not match serviceability program {}", | ||
| device_account.owner, | ||
| serviceability_program_id | ||
| ); | ||
| return Err(GeolocationError::InvalidServiceabilityProgramId.into()); | ||
| } | ||
|
|
||
| // Verify it's a valid, activated Device | ||
| let device = doublezero_serviceability::state::device::Device::try_from(device_account)?; | ||
| if device.status != doublezero_serviceability::state::device::DeviceStatus::Activated { | ||
| msg!( | ||
| "Device {} is not activated (status: {:?})", | ||
| device_account.key, | ||
| device.status | ||
| ); | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
|
|
||
| let mut probe = GeoProbe::try_from(probe_account)?; | ||
|
|
||
| if probe.parent_devices.contains(device_account.key) { | ||
| msg!("Device {} is already a parent device", device_account.key); | ||
| return Err(GeolocationError::ParentDeviceAlreadyExists.into()); | ||
| } | ||
|
|
||
| if probe.parent_devices.len() >= MAX_PARENT_DEVICES { | ||
| msg!( | ||
| "Cannot add parent device: already at maximum of {}", | ||
| MAX_PARENT_DEVICES | ||
| ); | ||
| return Err(GeolocationError::MaxParentDevicesReached.into()); | ||
| } | ||
|
|
||
| probe.parent_devices.push(*device_account.key); | ||
|
|
||
| try_acc_write(&probe, probe_account, payer_account, accounts)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
2 changes: 2 additions & 0 deletions
2
smartcontract/programs/doublezero-geolocation/src/processors/geo_probe/mod.rs
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,3 +1,5 @@ | ||
| pub mod add_parent_device; | ||
| pub mod create; | ||
| pub mod delete; | ||
| pub mod remove_parent_device; | ||
| pub mod update; |
65 changes: 65 additions & 0 deletions
65
...contract/programs/doublezero-geolocation/src/processors/geo_probe/remove_parent_device.rs
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,65 @@ | ||
| use crate::{ | ||
| error::GeolocationError, processors::check_foundation_allowlist, serializer::try_acc_write, | ||
| state::geo_probe::GeoProbe, | ||
| }; | ||
| use borsh::{BorshDeserialize, BorshSerialize}; | ||
| use solana_program::{ | ||
| account_info::{next_account_info, AccountInfo}, | ||
| entrypoint::ProgramResult, | ||
| msg, | ||
| program_error::ProgramError, | ||
| pubkey::Pubkey, | ||
| }; | ||
|
|
||
| #[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Clone)] | ||
| pub struct RemoveParentDeviceArgs { | ||
| pub device_pk: Pubkey, | ||
| } | ||
|
|
||
| pub fn process_remove_parent_device( | ||
| program_id: &Pubkey, | ||
| accounts: &[AccountInfo], | ||
| args: &RemoveParentDeviceArgs, | ||
| ) -> ProgramResult { | ||
| let accounts_iter = &mut accounts.iter(); | ||
|
|
||
| let probe_account = next_account_info(accounts_iter)?; | ||
| let program_config_account = next_account_info(accounts_iter)?; | ||
| let serviceability_globalstate_account = next_account_info(accounts_iter)?; | ||
| let payer_account = next_account_info(accounts_iter)?; | ||
| let _system_program = next_account_info(accounts_iter)?; | ||
|
|
||
| if !payer_account.is_signer { | ||
| msg!("Payer must be a signer"); | ||
| return Err(ProgramError::MissingRequiredSignature); | ||
| } | ||
|
|
||
| check_foundation_allowlist( | ||
| program_config_account, | ||
| serviceability_globalstate_account, | ||
| payer_account, | ||
| program_id, | ||
| )?; | ||
|
|
||
| if probe_account.owner != program_id { | ||
| msg!("Invalid GeoProbe Account Owner"); | ||
| return Err(ProgramError::IllegalOwner); | ||
| } | ||
| if !probe_account.is_writable { | ||
| msg!("GeoProbe account must be writable"); | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
|
|
||
| let mut probe = GeoProbe::try_from(probe_account)?; | ||
|
|
||
| if !probe.parent_devices.contains(&args.device_pk) { | ||
| msg!("Device {} is not a parent device", args.device_pk); | ||
| return Err(GeolocationError::ParentDeviceNotFound.into()); | ||
| } | ||
|
|
||
| probe.parent_devices.retain(|&pk| pk != args.device_pk); | ||
|
|
||
| try_acc_write(&probe, probe_account, payer_account, accounts)?; | ||
|
|
||
| Ok(()) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.