Skip to content
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

Fix ups for hotspot info #379

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helium-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async-trait = "0"
anchor-client = {version = "0.30.0", features = ["async"] }
anchor-spl = { version = "0.30.0", features = ["mint", "token"] }
url = {version = "2", features = ["serde"]}
h3o = {version = "0", features = ["std", "serde"]}
h3o = {version = "0", features = ["serde"]}
helium-crypto = {workspace = true}
itertools = "0.10.5"
jsonrpc_client = {version = "0.7", features = ["reqwest"]}
Expand Down
58 changes: 45 additions & 13 deletions helium-lib/src/hotspot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,27 @@ pub mod info {
.into_iter()
.map(HotspotInfoUpdate::from_ui_instruction)
.filter(|result| matches!(result, Ok(Some(_v))))
.collect::<Vec<StdResult<Option<HotspotInfoUpdate>, _>>>()
.collect::<Vec<StdResult<Option<(Pubkey, HotspotInfoUpdate)>, _>>>()
.into_iter()
.collect::<StdResult<Vec<Option<HotspotInfoUpdate>>, _>>()?
.collect::<StdResult<Vec<Option<(Pubkey, HotspotInfoUpdate)>>, _>>()?
.first()
.cloned()
.flatten()
.map(|update| Self {
.map(|(info_key, update)| Self {
timestamp,
signature: signature.clone(),
info_key,
update,
});
Ok(update)
}
}

impl HotspotInfoUpdate {
fn from_ui_instruction(ixn: UiInstruction) -> StdResult<Option<Self>, DecodeError> {
fn from_ui_instruction(
ixn: UiInstruction,
) -> StdResult<Option<(Pubkey, Self)>, DecodeError> {
use solana_transaction_status::UiPartiallyDecodedInstruction;
let UiInstruction::Parsed(UiParsedInstruction::PartiallyDecoded(decoded)) = ixn else {
return Err(DecodeError::other("not a decoded instruction"));
};
Expand All @@ -266,21 +270,47 @@ pub mod info {
discriminator.copy_from_slice(&decoded_data[..8]);
let args = &decoded_data[8..];

fn get_info_key(
decoded: &UiPartiallyDecodedInstruction,
index: usize,
) -> StdResult<Pubkey, DecodeError> {
let account_str = decoded.accounts.get(index).ok_or_else(|| {
DecodeError::other("missing info key in instruction accounts")
})?;
let account = Pubkey::from_str(account_str).map_err(DecodeError::from)?;
Ok(account)
}

match discriminator {
UpdateMobileInfoV0::DISCRIMINATOR => {
UpdateMobileInfoArgsV0::try_from_slice(args).map(Into::into)
let info_key = get_info_key(&decoded, 2)?;
UpdateMobileInfoArgsV0::try_from_slice(args)
.map(Into::into)
.map(|v| (info_key, v))
}
OnboardMobileHotspotV0::DISCRIMINATOR => {
OnboardMobileHotspotArgsV0::try_from_slice(args).map(Into::into)
let info_key = get_info_key(&decoded, 3)?;
OnboardMobileHotspotArgsV0::try_from_slice(args)
.map(Into::into)
.map(|v| (info_key, v))
}
OnboardIotHotspotV0::DISCRIMINATOR => {
OnboardIotHotspotArgsV0::try_from_slice(args).map(Into::into)
let info_key = get_info_key(&decoded, 3)?;
OnboardIotHotspotArgsV0::try_from_slice(args)
.map(Into::into)
.map(|v| (info_key, v))
}
UpdateIotInfoV0::DISCRIMINATOR => {
UpdateIotInfoArgsV0::try_from_slice(args).map(Into::into)
let info_key = get_info_key(&decoded, 2)?;
UpdateIotInfoArgsV0::try_from_slice(args)
.map(Into::into)
.map(|v| (info_key, v))
}
OnboardDataOnlyIotHotspotV0::DISCRIMINATOR => {
OnboardDataOnlyIotHotspotArgsV0::try_from_slice(args).map(Into::into)
let info_key = get_info_key(&decoded, 2)?;
OnboardDataOnlyIotHotspotArgsV0::try_from_slice(args)
.map(Into::into)
.map(|v| (info_key, v))
}
_ => return Ok(None),
}
Expand Down Expand Up @@ -759,8 +789,8 @@ impl Hotspot {

#[derive(Serialize, Debug, Clone, Copy)]
pub struct HotspotGeo {
lat: f64,
lng: f64,
pub lat: f64,
pub lng: f64,
}

impl From<h3o::CellIndex> for HotspotGeo {
Expand All @@ -776,8 +806,8 @@ impl From<h3o::CellIndex> for HotspotGeo {
#[derive(Serialize, Debug, Clone, Copy)]
pub struct HotspotLocation {
#[serde(with = "serde_cell_index")]
location: h3o::CellIndex,
geo: HotspotGeo,
pub location: h3o::CellIndex,
pub geo: HotspotGeo,
}

impl From<h3o::CellIndex> for HotspotLocation {
Expand Down Expand Up @@ -873,6 +903,8 @@ pub enum HotspotInfo {
pub struct CommittedHotspotInfoUpdate {
pub timestamp: chrono::DateTime<Utc>,
pub signature: String,
#[serde(with = "serde_pubkey")]
pub info_key: Pubkey,
pub update: HotspotInfoUpdate,
}

Expand Down