-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathlib.rs
More file actions
43 lines (37 loc) · 1.83 KB
/
lib.rs
File metadata and controls
43 lines (37 loc) · 1.83 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
use serializable_account_meta::SimulationReturnData;
use solana_program::{program::set_return_data, program_error::ProgramError};
/// Single source of truth for all Hyperlane SVM program versions.
/// Compiled into each program's binary — atomic on upgrade, no migration step.
pub const PACKAGE_VERSION: &str = "1.0.0";
/// Trait for programs that expose their version.
/// Programs implement with empty impl block to get the default.
pub trait PackageVersioned {
fn package_version() -> &'static str {
PACKAGE_VERSION
}
}
/// 8-byte discriminator for the `GetProgramVersion` instruction.
/// First 8 bytes of `sha256(b"hyperlane:get-program-version")`.
/// This is independent of any program's instruction enum, allowing
/// universal version queries across all Hyperlane SVM programs.
pub const GET_PROGRAM_VERSION_DISCRIMINATOR: [u8; 8] = [150, 230, 176, 162, 236, 96, 183, 171];
/// Attempts to decode a `GetProgramVersion` instruction from raw instruction data.
/// Returns true if the data matches the discriminator, false otherwise.
pub fn is_get_program_version(instruction_data: &[u8]) -> bool {
instruction_data.len() == GET_PROGRAM_VERSION_DISCRIMINATOR.len()
&& instruction_data == GET_PROGRAM_VERSION_DISCRIMINATOR
}
/// Builds the instruction data for a `GetProgramVersion` call.
pub fn get_program_version_instruction_data() -> Vec<u8> {
GET_PROGRAM_VERSION_DISCRIMINATOR.to_vec()
}
/// Shared handler for the `GetProgramVersion` instruction.
/// Writes the version string as return data wrapped in SimulationReturnData.
pub fn process_get_program_version<T: PackageVersioned>() -> Result<(), ProgramError> {
let version = T::package_version();
set_return_data(
&borsh::to_vec(&SimulationReturnData::new(version.to_string()))
.map_err(|_| ProgramError::BorshIoError)?,
);
Ok(())
}