SHA256
Обновить shine-users и синхронизацию пользователей
This commit is contained in:
@@ -25,7 +25,8 @@ entrypoint!(process_instruction);
|
||||
|
||||
const MAGIC: &[u8; 5] = b"SHiNE";
|
||||
const FORMAT_MAJOR: u8 = 1;
|
||||
const FORMAT_MINOR: u8 = 2;
|
||||
const FORMAT_MINOR: u8 = 3;
|
||||
const PREVIOUS_FORMAT_MINOR: u8 = 2;
|
||||
const LEGACY_FORMAT_MINOR: u8 = 0;
|
||||
const MAX_AUTO_REALLOC_INCREASE: usize = 10_000;
|
||||
const ZERO_HASH: [u8; 32] = [0; 32];
|
||||
@@ -36,7 +37,6 @@ const BLOCK_TYPE_SERVER_PROFILE: u8 = 30;
|
||||
const BLOCK_TYPE_ACCESS_SERVERS: u8 = 40;
|
||||
const BLOCK_VERSION_0: u8 = 0;
|
||||
|
||||
const AUTH_MODE_BLOCKCHAIN: u8 = 0;
|
||||
const AUTH_MODE_ROOT: u8 = 1;
|
||||
const DAY_MS: u64 = 86_400_000;
|
||||
const FORK_COOLDOWN_MS: u64 = 3 * DAY_MS;
|
||||
@@ -202,7 +202,6 @@ pub struct CreateUserPdaArgs {
|
||||
pub login: String,
|
||||
pub root_key: Pubkey,
|
||||
pub created_at_ms: u64,
|
||||
pub additional_limit: u64,
|
||||
pub client_key: Pubkey,
|
||||
pub blockchain_key: Pubkey,
|
||||
pub server_addresses: Vec<ServerAddressRecordV12>,
|
||||
@@ -216,7 +215,6 @@ pub struct UpdateUserPdaArgs {
|
||||
pub login: String,
|
||||
pub root_key: Pubkey,
|
||||
pub updated_at_ms: u64,
|
||||
pub additional_limit: u64,
|
||||
pub client_key: Pubkey,
|
||||
pub auth_mode: u8,
|
||||
pub new_blockchain_key: Option<Pubkey>,
|
||||
@@ -232,17 +230,13 @@ pub struct CloseLegacyUserPdaArgs {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UpdateUsersEconomyConfigArgs {
|
||||
pub registration_fee_lamports: u64,
|
||||
pub lamports_per_limit_step: u64,
|
||||
pub start_bonus_limit: u64,
|
||||
pub update_fee_lamports: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UsersEconomyConfigState {
|
||||
pub version: u8,
|
||||
pub registration_fee_lamports: u64,
|
||||
pub lamports_per_limit_step: u64,
|
||||
pub start_bonus_limit: u64,
|
||||
pub update_fee_lamports: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
@@ -327,9 +321,7 @@ fn process_instruction<'a>(program_id: &Pubkey, accounts: &'a [AccountInfo<'a>],
|
||||
}
|
||||
IX_UPDATE_USERS_ECONOMY_CONFIG => {
|
||||
let args = UpdateUsersEconomyConfigArgs {
|
||||
registration_fee_lamports: r.read_u64()?,
|
||||
lamports_per_limit_step: r.read_u64()?,
|
||||
start_bonus_limit: r.read_u64()?,
|
||||
update_fee_lamports: r.read_u64()?,
|
||||
};
|
||||
r.finish()?;
|
||||
process_update_users_economy_config(program_id, accounts, args)
|
||||
@@ -431,7 +423,6 @@ fn parse_create_args(r: &mut Reader<'_>) -> Result<CreateUserPdaArgs, ProgramErr
|
||||
login: r.read_string_u8()?,
|
||||
root_key: r.read_pubkey()?,
|
||||
created_at_ms: r.read_u64()?,
|
||||
additional_limit: r.read_u64()?,
|
||||
client_key: r.read_pubkey()?,
|
||||
blockchain_key: r.read_pubkey()?,
|
||||
server_addresses: parse_server_addresses(r)?,
|
||||
@@ -449,7 +440,6 @@ fn parse_update_args(r: &mut Reader<'_>) -> Result<UpdateUserPdaArgs, ProgramErr
|
||||
let login = r.read_string_u8()?;
|
||||
let root_key = r.read_pubkey()?;
|
||||
let updated_at_ms = r.read_u64()?;
|
||||
let additional_limit = r.read_u64()?;
|
||||
let client_key = r.read_pubkey()?;
|
||||
let auth_mode = r.read_u8()?;
|
||||
let new_blockchain_key = match r.read_u8()? {
|
||||
@@ -464,7 +454,6 @@ fn parse_update_args(r: &mut Reader<'_>) -> Result<UpdateUserPdaArgs, ProgramErr
|
||||
login,
|
||||
root_key,
|
||||
updated_at_ms,
|
||||
additional_limit,
|
||||
client_key,
|
||||
auth_mode,
|
||||
new_blockchain_key,
|
||||
@@ -496,10 +485,8 @@ fn process_init_users_economy_config(program_id: &Pubkey, accounts: &[AccountInf
|
||||
require!(users_economy_config_pda.data_is_empty(), ShineUsersError::SystemAlreadyInitialized);
|
||||
|
||||
let state = UsersEconomyConfigState {
|
||||
version: 1,
|
||||
registration_fee_lamports: settings::START_REGISTRATION_FEE_LAMPORTS,
|
||||
lamports_per_limit_step: settings::START_LAMPORTS_PER_LIMIT_STEP,
|
||||
start_bonus_limit: settings::START_BONUS_LIMIT,
|
||||
version: 2,
|
||||
update_fee_lamports: settings::UPDATE_FEE_LAMPORTS,
|
||||
};
|
||||
let data = serialize_users_economy_config(&state);
|
||||
create_pda_account(
|
||||
@@ -526,12 +513,10 @@ fn process_update_users_economy_config(program_id: &Pubkey, accounts: &[AccountI
|
||||
let (expected_pda, _) = find_users_economy_config_pda(program_id);
|
||||
require_keys_eq!(expected_pda, *users_economy_config_pda.key, ShineUsersError::InvalidPdaAddress);
|
||||
require!(users_economy_config_pda.owner == program_id, ShineUsersError::InvalidPdaAddress);
|
||||
require!(args.lamports_per_limit_step > 0, ShineUsersError::InvalidRecordData);
|
||||
|
||||
let mut state = read_users_economy_config(users_economy_config_pda)?;
|
||||
state.registration_fee_lamports = args.registration_fee_lamports;
|
||||
state.lamports_per_limit_step = args.lamports_per_limit_step;
|
||||
state.start_bonus_limit = args.start_bonus_limit;
|
||||
state.version = 2;
|
||||
state.update_fee_lamports = args.update_fee_lamports;
|
||||
write_pda_exact(users_economy_config_pda, &serialize_users_economy_config(&state))?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -639,10 +624,9 @@ fn process_create_user_pda<'a>(program_id: &Pubkey, accounts: &'a [AccountInfo<'
|
||||
validate_access_servers(&args.access_servers)?;
|
||||
for server in &args.access_servers { validate_full_login(server)?; }
|
||||
validate_inflow_vault(inflow_vault)?;
|
||||
require!(args.additional_limit % settings::LIMIT_STEP == 0, ShineUsersError::InvalidLimitIncrement);
|
||||
validate_users_economy_config_pda(program_id, users_economy_config_pda)?;
|
||||
require!(!users_economy_config_pda.is_writable, ShineUsersError::InvalidAccountData);
|
||||
let economy = read_users_economy_config(users_economy_config_pda)?;
|
||||
read_users_economy_config(users_economy_config_pda)?;
|
||||
let (expected_pda, bump) = find_user_pda(program_id, &args.login);
|
||||
require_keys_eq!(expected_pda, *user_pda.key, ShineUsersError::InvalidPdaAddress);
|
||||
require!(user_pda.owner == &system_program::id(), ShineUsersError::UserAlreadyExists);
|
||||
@@ -653,13 +637,11 @@ fn process_create_user_pda<'a>(program_id: &Pubkey, accounts: &'a [AccountInfo<'
|
||||
let signature = args.registrar_signature.as_ref().ok_or(ShineUsersError::CertificateRequired)?;
|
||||
let registrar = namespace.registrar_pubkey.ok_or(ShineUsersError::InvalidNamespace)?;
|
||||
let message = certificate_inner_message(&args.login, &args.root_key, &args.blockchain_key, &args.client_key)?;
|
||||
verify_ed25519_signature_instruction(instructions_sysvar, -3, ®istrar, signature, &message)?;
|
||||
verify_ed25519_signature_instruction(instructions_sysvar, -2, ®istrar, signature, &message)?;
|
||||
},
|
||||
_ => return Err(ShineUsersError::InvalidNamespace.into()),
|
||||
}
|
||||
|
||||
let start_balance_u64 = economy.start_bonus_limit.checked_add(args.additional_limit).ok_or(ProgramError::from(ShineUsersError::MathOverflow))?;
|
||||
let start_balance = u32::try_from(start_balance_u64).map_err(|_| ProgramError::from(ShineUsersError::PaidLimitTooLarge))?;
|
||||
let now_ms = current_time_ms()?;
|
||||
validate_client_time_ms(args.created_at_ms, now_ms)?;
|
||||
let fork_created_at_ms = args.created_at_ms;
|
||||
@@ -675,7 +657,7 @@ fn process_create_user_pda<'a>(program_id: &Pubkey, accounts: &'a [AccountInfo<'
|
||||
forks: vec![ForkRecordV12 {
|
||||
blockchain_key: args.blockchain_key,
|
||||
created_at_ms: fork_created_at_ms,
|
||||
paid_limit_bytes: start_balance,
|
||||
paid_limit_bytes: 0,
|
||||
}],
|
||||
server_addresses: args.server_addresses,
|
||||
access_servers: args.access_servers,
|
||||
@@ -683,11 +665,11 @@ fn process_create_user_pda<'a>(program_id: &Pubkey, accounts: &'a [AccountInfo<'
|
||||
};
|
||||
|
||||
let unsigned = serialize_unsigned_record_v12(&record)?;
|
||||
let unsigned_hash = hashv(&[&unsigned]);
|
||||
// На create root доказывает владение recovery-ключом, а сама новая запись
|
||||
// подписывается активным blockchain-key (новым authority состояния).
|
||||
verify_ed25519_instruction_pubkey_message(instructions_sysvar, -2, &record.root_key, unsigned_hash.as_ref())?;
|
||||
record.signature = verify_record_signature_hash_at(instructions_sysvar, -1, &record.active_fork()?.blockchain_key, &args.signature, unsigned_hash.as_ref())?;
|
||||
let unsigned_hash = hashv(&[&signed_bytes_without_limits(&unsigned)?]);
|
||||
// PDA 1.3: only root signs the on-chain identity record. The blockchain
|
||||
// key signs content blocks but cannot authorize changes to this PDA.
|
||||
// For free namespaces registrar certificate is the previous ed25519 ix (-2).
|
||||
record.signature = verify_record_signature_hash_at(instructions_sysvar, -1, &record.root_key, &args.signature, unsigned_hash.as_ref())?;
|
||||
|
||||
let serialized = serialize_record_with_stats(&record, SupportStats::default())?;
|
||||
let serialized = pad_to_fixed_size(serialized, settings::USER_PDA_SPACE)?;
|
||||
@@ -702,9 +684,7 @@ fn process_create_user_pda<'a>(program_id: &Pubkey, accounts: &'a [AccountInfo<'
|
||||
let namespace_remainder = namespace.registration_fee
|
||||
.checked_sub(rent_paid_by_signer)
|
||||
.ok_or(ProgramError::from(ShineUsersError::RegistrationFeeBelowRent))?;
|
||||
let vault_fee = namespace_remainder
|
||||
.checked_add(limit_fee_lamports(args.additional_limit, economy.lamports_per_limit_step)?)
|
||||
.ok_or(ProgramError::from(ShineUsersError::MathOverflow))?;
|
||||
let vault_fee = namespace_remainder;
|
||||
create_pda_account(
|
||||
signer,
|
||||
user_pda,
|
||||
@@ -738,7 +718,6 @@ fn process_update_user_pda(program_id: &Pubkey, accounts: &[AccountInfo], args:
|
||||
validate_server_addresses(&args.server_addresses)?;
|
||||
validate_access_servers(&args.access_servers)?;
|
||||
validate_inflow_vault(inflow_vault)?;
|
||||
require!(args.additional_limit % settings::LIMIT_STEP == 0, ShineUsersError::InvalidLimitIncrement);
|
||||
validate_users_economy_config_pda(program_id, users_economy_config_pda)?;
|
||||
let economy = read_users_economy_config(users_economy_config_pda)?;
|
||||
|
||||
@@ -751,33 +730,25 @@ fn process_update_user_pda(program_id: &Pubkey, accounts: &[AccountInfo], args:
|
||||
require!(user_pda.owner == program_id, ShineUsersError::InvalidPdaAddress);
|
||||
|
||||
let raw = read_pda_all(user_pda)?;
|
||||
require!(record_format_minor(&raw)? == FORMAT_MINOR, ShineUsersError::InvalidRecordFormat);
|
||||
require!(matches!(record_format_minor(&raw)?, FORMAT_MINOR | PREVIOUS_FORMAT_MINOR), ShineUsersError::InvalidRecordFormat);
|
||||
let old_record = deserialize_record_v12_from_pda(&raw)?;
|
||||
require!(old_record.login == args.login, ShineUsersError::ImmutableFieldChanged);
|
||||
require!(args.updated_at_ms >= old_record.updated_at_ms, ShineUsersError::InvalidRecordData);
|
||||
let now_ms = current_time_ms()?;
|
||||
validate_client_time_ms(args.updated_at_ms, now_ms)?;
|
||||
require!(args.auth_mode == AUTH_MODE_BLOCKCHAIN || args.auth_mode == AUTH_MODE_ROOT, ShineUsersError::InvalidInstruction);
|
||||
require!(args.auth_mode == AUTH_MODE_ROOT, ShineUsersError::InvalidInstruction);
|
||||
|
||||
let root_changed = args.root_key != old_record.root_key;
|
||||
let client_changed = args.client_key != old_record.client_key;
|
||||
let full_key_rotation = root_changed && client_changed && args.new_blockchain_key.is_some();
|
||||
|
||||
// root/client меняются только вместе с созданием нового blockchain fork.
|
||||
// Обычные update и обычный fork всегда авторизуются текущим blockchain key.
|
||||
// Rotating root/client remains atomic with appending a fresh blockchain key.
|
||||
if root_changed || client_changed {
|
||||
require!(full_key_rotation, ShineUsersError::AmbiguousAuthorityRotation);
|
||||
require!(args.auth_mode == AUTH_MODE_ROOT, ShineUsersError::RootChangeRequiresRoot);
|
||||
} else {
|
||||
require!(args.auth_mode == AUTH_MODE_BLOCKCHAIN, ShineUsersError::RootModeRequiresFullRotation);
|
||||
}
|
||||
|
||||
let old_active_key = old_record.active_fork()?.blockchain_key;
|
||||
let old_authority = if full_key_rotation { old_record.root_key } else { old_active_key };
|
||||
let old_authority = old_record.root_key;
|
||||
let mut forks = old_record.forks.clone();
|
||||
|
||||
let additional_limit_u32 = u32::try_from(args.additional_limit).map_err(|_| ProgramError::from(ShineUsersError::PaidLimitTooLarge))?;
|
||||
let mut appended_key = None;
|
||||
if let Some(new_key) = args.new_blockchain_key {
|
||||
for fork in &forks {
|
||||
require!(fork.blockchain_key != new_key, ShineUsersError::DuplicateBlockchainKey);
|
||||
@@ -785,17 +756,20 @@ fn process_update_user_pda(program_id: &Pubkey, accounts: &[AccountInfo], args:
|
||||
if !full_key_rotation {
|
||||
enforce_fork_cooldown(old_record.active_fork()?.created_at_ms, args.updated_at_ms)?;
|
||||
}
|
||||
|
||||
let current_limit = old_record.active_fork()?.paid_limit_bytes;
|
||||
let new_limit = current_limit.checked_add(additional_limit_u32).ok_or(ProgramError::from(ShineUsersError::PaidLimitTooLarge))?;
|
||||
forks.push(ForkRecordV12 { blockchain_key: new_key, created_at_ms: args.updated_at_ms, paid_limit_bytes: new_limit });
|
||||
appended_key = Some(new_key);
|
||||
} else if additional_limit_u32 > 0 {
|
||||
let active = forks.last_mut().ok_or(ProgramError::from(ShineUsersError::InvalidRecordData))?;
|
||||
active.paid_limit_bytes = active.paid_limit_bytes.checked_add(additional_limit_u32).ok_or(ProgramError::from(ShineUsersError::PaidLimitTooLarge))?;
|
||||
// Each new fork starts with its own unlimited quota (0).
|
||||
// Historical forks retain their existing system-owned limits.
|
||||
forks.push(ForkRecordV12 { blockchain_key: new_key, created_at_ms: args.updated_at_ms, paid_limit_bytes: 0 });
|
||||
}
|
||||
|
||||
let prev_hash = hash_unsigned_record_v12(&old_record)?;
|
||||
let old_record_len = u16::from_le_bytes([raw[7], raw[8]]) as usize;
|
||||
let old_unsigned = &raw[..old_record_len - 64];
|
||||
// Existing v1.2 links were hashed including limits. The first migration
|
||||
// link commits to the exact old bytes; v1.3 links omit quota bytes.
|
||||
let prev_hash = if raw[6] == PREVIOUS_FORMAT_MINOR {
|
||||
hash(old_unsigned).to_bytes()
|
||||
} else {
|
||||
hash(&signed_bytes_without_limits(old_unsigned)?).to_bytes()
|
||||
};
|
||||
let mut new_record = UserRecordV12 {
|
||||
created_at_ms: old_record.created_at_ms,
|
||||
updated_at_ms: args.updated_at_ms,
|
||||
@@ -810,27 +784,21 @@ fn process_update_user_pda(program_id: &Pubkey, accounts: &[AccountInfo], args:
|
||||
signature: [0; 64],
|
||||
};
|
||||
|
||||
let record_signer = if let Some(new_key) = appended_key {
|
||||
new_key
|
||||
} else {
|
||||
new_record.active_fork()?.blockchain_key
|
||||
};
|
||||
|
||||
let unsigned = serialize_unsigned_record_v12(&new_record)?;
|
||||
let unsigned_hash = hashv(&[&unsigned]);
|
||||
// Первая подпись разрешает переход старым authority. Вторая подпись хранится
|
||||
// в PDA и принадлежит authority уже нового состояния.
|
||||
verify_ed25519_instruction_pubkey_message(instructions_sysvar, -2, &old_authority, unsigned_hash.as_ref())?;
|
||||
new_record.signature = verify_record_signature_hash_at(instructions_sysvar, -1, &record_signer, &args.signature, unsigned_hash.as_ref())?;
|
||||
let unsigned_hash = hashv(&[&signed_bytes_without_limits(&unsigned)?]);
|
||||
// Normal update: one root signature. Root rotation: both old and new root
|
||||
// authorize the same canonical hash (old at -2, new at -1).
|
||||
if root_changed {
|
||||
verify_ed25519_instruction_pubkey_message(instructions_sysvar, -2, &old_authority, unsigned_hash.as_ref())?;
|
||||
}
|
||||
new_record.signature = verify_record_signature_hash_at(instructions_sysvar, -1, &new_record.root_key, &args.signature, unsigned_hash.as_ref())?;
|
||||
|
||||
let stats = read_support_stats(&raw)?;
|
||||
let serialized = serialize_record_with_stats(&new_record, stats)?;
|
||||
ensure_pda_size_and_rent(user_pda, signer, system_program_ai, serialized.len())?;
|
||||
write_pda_exact(user_pda, &serialized)?;
|
||||
|
||||
let topup_fee = limit_fee_lamports(args.additional_limit, economy.lamports_per_limit_step)?;
|
||||
let total_fee = topup_fee.checked_add(settings::UPDATE_FEE_LAMPORTS).ok_or(ProgramError::from(ShineUsersError::MathOverflow))?;
|
||||
transfer_lamports(signer, inflow_vault, system_program_ai, total_fee)?;
|
||||
transfer_lamports(signer, inflow_vault, system_program_ai, economy.update_fee_lamports)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -912,7 +880,7 @@ fn process_support_user(program_id: &Pubkey, accounts: &[AccountInfo], args: Sup
|
||||
require!(sender_pda.owner == program_id && recipient_pda.owner == program_id, ShineUsersError::InvalidAccountOwner);
|
||||
let sender_raw = read_pda_all(sender_pda)?;
|
||||
let recipient_raw = read_pda_all(recipient_pda)?;
|
||||
require!(record_format_minor(&sender_raw)? == FORMAT_MINOR && record_format_minor(&recipient_raw)? == FORMAT_MINOR, ShineUsersError::InvalidRecordFormat);
|
||||
require!(matches!(record_format_minor(&sender_raw)?, FORMAT_MINOR | PREVIOUS_FORMAT_MINOR) && matches!(record_format_minor(&recipient_raw)?, FORMAT_MINOR | PREVIOUS_FORMAT_MINOR), ShineUsersError::InvalidRecordFormat);
|
||||
let sender_record = deserialize_record_v12_from_pda(&sender_raw)?;
|
||||
let recipient_record = deserialize_record_v12_from_pda(&recipient_raw)?;
|
||||
require!(sender_record.login == args.sender_login && recipient_record.login == args.recipient_login, ShineUsersError::InvalidRecordData);
|
||||
@@ -929,8 +897,8 @@ fn process_support_user(program_id: &Pubkey, accounts: &[AccountInfo], args: Sup
|
||||
let mut recipient_stats = read_support_stats(&recipient_raw)?;
|
||||
sender_stats.support_sent = sender_stats.support_sent.checked_add(args.amount_star_cents).ok_or(ProgramError::from(ShineUsersError::MathOverflow))?;
|
||||
recipient_stats.support_received = recipient_stats.support_received.checked_add(author_star_cents).ok_or(ProgramError::from(ShineUsersError::MathOverflow))?;
|
||||
let sender_data = serialize_record_with_stats(&sender_record, sender_stats)?;
|
||||
let recipient_data = serialize_record_with_stats(&recipient_record, recipient_stats)?;
|
||||
let sender_data = append_stats_to_existing_signed_record(&sender_raw, sender_stats)?;
|
||||
let recipient_data = append_stats_to_existing_signed_record(&recipient_raw, recipient_stats)?;
|
||||
ensure_pda_size_and_rent(sender_pda, sender, system_program_ai, sender_data.len())?;
|
||||
ensure_pda_size_and_rent(recipient_pda, sender, system_program_ai, recipient_data.len())?;
|
||||
transfer_lamports(sender, recipient, system_program_ai, recipient_amount)?;
|
||||
@@ -964,13 +932,13 @@ fn process_support_dao(program_id: &Pubkey, accounts: &[AccountInfo], args: Supp
|
||||
require_keys_eq!(find_user_pda(program_id, &args.sender_login).0, *sender_pda.key, ShineUsersError::InvalidPdaAddress);
|
||||
require!(sender_pda.owner == program_id, ShineUsersError::InvalidAccountOwner);
|
||||
let raw = read_pda_all(sender_pda)?;
|
||||
require!(record_format_minor(&raw)? == FORMAT_MINOR, ShineUsersError::InvalidRecordFormat);
|
||||
require!(matches!(record_format_minor(&raw)?, FORMAT_MINOR | PREVIOUS_FORMAT_MINOR), ShineUsersError::InvalidRecordFormat);
|
||||
let record = deserialize_record_v12_from_pda(&raw)?;
|
||||
require!(record.login == args.sender_login, ShineUsersError::InvalidRecordData);
|
||||
require_keys_eq!(record.active_fork()?.blockchain_key, *sender.key, ShineUsersError::InvalidSigner);
|
||||
let mut stats = read_support_stats(&raw)?;
|
||||
stats.dao_direct = stats.dao_direct.checked_add(args.amount_star_cents).ok_or(ProgramError::from(ShineUsersError::MathOverflow))?;
|
||||
let data = serialize_record_with_stats(&record, stats)?;
|
||||
let data = append_stats_to_existing_signed_record(&raw, stats)?;
|
||||
ensure_pda_size_and_rent(sender_pda, sender, system_program_ai, data.len())?;
|
||||
transfer_lamports(sender, inflow_vault, system_program_ai, args.amount_lamports)?;
|
||||
write_pda_exact(sender_pda, &data)?;
|
||||
@@ -1009,11 +977,9 @@ fn process_close_legacy_user_pda(program_id: &Pubkey, accounts: &[AccountInfo],
|
||||
}
|
||||
|
||||
fn serialize_users_economy_config(state: &UsersEconomyConfigState) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(1 + 8 + 8 + 8);
|
||||
let mut out = Vec::with_capacity(1 + 8);
|
||||
out.push(state.version);
|
||||
out.extend_from_slice(&state.registration_fee_lamports.to_le_bytes());
|
||||
out.extend_from_slice(&state.lamports_per_limit_step.to_le_bytes());
|
||||
out.extend_from_slice(&state.start_bonus_limit.to_le_bytes());
|
||||
out.extend_from_slice(&state.update_fee_lamports.to_le_bytes());
|
||||
out
|
||||
}
|
||||
|
||||
@@ -1027,13 +993,16 @@ fn validate_users_economy_config_pda(program_id: &Pubkey, pda: &AccountInfo) ->
|
||||
fn read_users_economy_config(pda: &AccountInfo) -> Result<UsersEconomyConfigState, ProgramError> {
|
||||
let raw = read_pda_all(pda)?;
|
||||
require!(!raw.is_empty(), ShineUsersError::EmptyPdaData);
|
||||
require!(raw.len() >= 25 && raw[0] == 1, ShineUsersError::InvalidAccountData);
|
||||
Ok(UsersEconomyConfigState {
|
||||
version: raw[0],
|
||||
registration_fee_lamports: u64::from_le_bytes(raw[1..9].try_into().unwrap()),
|
||||
lamports_per_limit_step: u64::from_le_bytes(raw[9..17].try_into().unwrap()),
|
||||
start_bonus_limit: u64::from_le_bytes(raw[17..25].try_into().unwrap()),
|
||||
})
|
||||
// Existing v1 config remains readable until an authority-signed update to v2.
|
||||
// Its update fee was previously hardcoded; do not reinterpret v1 bytes as fee.
|
||||
match raw[0] {
|
||||
1 if raw.len() >= 25 => Ok(UsersEconomyConfigState { version: 1, update_fee_lamports: settings::UPDATE_FEE_LAMPORTS }),
|
||||
2 if raw.len() >= 9 => Ok(UsersEconomyConfigState {
|
||||
version: 2,
|
||||
update_fee_lamports: u64::from_le_bytes(raw[1..9].try_into().unwrap()),
|
||||
}),
|
||||
_ => Err(ShineUsersError::InvalidAccountData.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn record_format_minor(raw: &[u8]) -> Result<u8, ProgramError> {
|
||||
@@ -1046,7 +1015,7 @@ fn record_format_minor(raw: &[u8]) -> Result<u8, ProgramError> {
|
||||
fn deserialize_record_v12_from_pda(raw: &[u8]) -> Result<UserRecordV12, ProgramError> {
|
||||
require!(raw.len() >= 9, ShineUsersError::InvalidRecordData);
|
||||
require!(sol_memcmp(&raw[0..5], MAGIC, 5) == 0, ShineUsersError::InvalidRecordMagic);
|
||||
require!(raw[5] == FORMAT_MAJOR && raw[6] == FORMAT_MINOR, ShineUsersError::InvalidRecordFormat);
|
||||
require!(raw[5] == FORMAT_MAJOR && matches!(raw[6], FORMAT_MINOR | PREVIOUS_FORMAT_MINOR), ShineUsersError::InvalidRecordFormat);
|
||||
let record_len = u16::from_le_bytes([raw[7], raw[8]]) as usize;
|
||||
require!(record_len >= 9 + 64, ShineUsersError::InvalidRecordLength);
|
||||
require!(record_len <= raw.len(), ShineUsersError::InvalidRecordLength);
|
||||
@@ -1226,6 +1195,60 @@ fn serialize_unsigned_record_v12(record: &UserRecordV12) -> Result<Vec<u8>, Prog
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Canonical signing bytes of a physical PDA 1.3 unsigned record:
|
||||
/// all original headers, lengths and blocks, EXCEPT four limit bytes per fork.
|
||||
/// Works over raw bytes to preserve unknown future variable blocks verbatim.
|
||||
fn signed_bytes_without_limits(unsigned: &[u8]) -> Result<Vec<u8>, ProgramError> {
|
||||
require!(unsigned.len() >= 9 + 8 + 8 + 4 + 32 + 1 + 1, ShineUsersError::InvalidRecordLength);
|
||||
require!(&unsigned[..5] == MAGIC && unsigned[5] == FORMAT_MAJOR && unsigned[6] == FORMAT_MINOR, ShineUsersError::InvalidRecordFormat);
|
||||
let expected = u16::from_le_bytes([unsigned[7], unsigned[8]]) as usize;
|
||||
require!(expected == unsigned.len() + 64, ShineUsersError::InvalidRecordLength);
|
||||
let mut c = 9 + 8 + 8 + 4 + 32;
|
||||
let login_len = *unsigned.get(c).ok_or(ShineUsersError::InvalidRecordLength)? as usize;
|
||||
c = c.checked_add(1 + login_len).ok_or(ShineUsersError::MathOverflow)?;
|
||||
let blocks = *unsigned.get(c).ok_or(ShineUsersError::InvalidRecordLength)? as usize;
|
||||
c += 1;
|
||||
let mut out = Vec::with_capacity(unsigned.len());
|
||||
out.extend_from_slice(&unsigned[..c]);
|
||||
let mut found_registry = false;
|
||||
for _ in 0..blocks {
|
||||
let start = c;
|
||||
let ty = *unsigned.get(c).ok_or(ShineUsersError::InvalidRecordLength)?;
|
||||
let ver = *unsigned.get(c + 1).ok_or(ShineUsersError::InvalidRecordLength)?;
|
||||
c += 2;
|
||||
if ty == BLOCK_TYPE_ROOT_KEY || ty == BLOCK_TYPE_CLIENT_KEY {
|
||||
c = c.checked_add(32).ok_or(ShineUsersError::MathOverflow)?;
|
||||
require!(c <= unsigned.len(), ShineUsersError::InvalidRecordLength);
|
||||
out.extend_from_slice(&unsigned[start..c]);
|
||||
continue;
|
||||
}
|
||||
let len_slice = unsigned.get(c..c + 2).ok_or(ShineUsersError::InvalidRecordLength)?;
|
||||
let len = u16::from_le_bytes([len_slice[0], len_slice[1]]) as usize;
|
||||
c += 2;
|
||||
let end = c.checked_add(len).ok_or(ShineUsersError::MathOverflow)?;
|
||||
require!(end <= unsigned.len(), ShineUsersError::InvalidRecordLength);
|
||||
if ty == BLOCK_TYPE_BLOCKCHAIN_REGISTRY && ver == BLOCK_VERSION_0 {
|
||||
require!(!found_registry, ShineUsersError::InvalidRecordData);
|
||||
found_registry = true;
|
||||
let count_slice = unsigned.get(c..c + 2).ok_or(ShineUsersError::InvalidRecordLength)?;
|
||||
let count = u16::from_le_bytes([count_slice[0], count_slice[1]]) as usize;
|
||||
require!(count > 0 && len == 2 + count * 44, ShineUsersError::InvalidRecordLength);
|
||||
out.extend_from_slice(&unsigned[start..c + 2]);
|
||||
c += 2;
|
||||
for _ in 0..count {
|
||||
out.extend_from_slice(&unsigned[c..c + 40]);
|
||||
c += 44;
|
||||
}
|
||||
require!(c == end, ShineUsersError::InvalidRecordLength);
|
||||
} else {
|
||||
out.extend_from_slice(&unsigned[start..end]);
|
||||
}
|
||||
c = end;
|
||||
}
|
||||
require!(found_registry && c == unsigned.len(), ShineUsersError::InvalidRecordLength);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn serialize_full_record_v12(record: &UserRecordV12) -> Result<Vec<u8>, ProgramError> {
|
||||
let mut out = serialize_unsigned_record_v12(record)?;
|
||||
out.extend_from_slice(&record.signature);
|
||||
@@ -1259,11 +1282,18 @@ fn serialize_record_with_stats(record: &UserRecordV12, stats: SupportStats) -> R
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn hash_unsigned_record_v12(record: &UserRecordV12) -> Result<[u8; 32], ProgramError> {
|
||||
let unsigned = serialize_unsigned_record_v12(record)?;
|
||||
let digest = hashv(&[&unsigned]);
|
||||
let mut out = [0u8; 32];
|
||||
out.copy_from_slice(digest.as_ref());
|
||||
/// Update only the program-owned Stars stats tail; never alter the signed
|
||||
/// PDA record (particularly v1.2 signatures during transition to 1.3).
|
||||
fn append_stats_to_existing_signed_record(raw: &[u8], stats: SupportStats) -> Result<Vec<u8>, ProgramError> {
|
||||
require!(raw.len() >= 9, ShineUsersError::InvalidRecordLength);
|
||||
let signed_len = u16::from_le_bytes([raw[7], raw[8]]) as usize;
|
||||
require!(signed_len >= 9 + 64 && signed_len <= raw.len(), ShineUsersError::InvalidRecordLength);
|
||||
let mut out = Vec::with_capacity(signed_len + SUPPORT_STATS_LEN);
|
||||
out.extend_from_slice(&raw[..signed_len]);
|
||||
out.push(SUPPORT_STATS_VERSION);
|
||||
out.extend_from_slice(&stats.support_sent.to_le_bytes());
|
||||
out.extend_from_slice(&stats.support_received.to_le_bytes());
|
||||
out.extend_from_slice(&stats.dao_direct.to_le_bytes());
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
@@ -1549,7 +1579,6 @@ pub fn find_namespace_pda(program_id: &Pubkey, name: &str) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[&seed], program_id)
|
||||
}
|
||||
fn find_users_economy_config_pda(program_id: &Pubkey) -> (Pubkey, u8) { Pubkey::find_program_address(&[settings::USERS_ECONOMY_CONFIG_SEED], program_id) }
|
||||
fn limit_fee_lamports(limit_delta: u64, lamports_per_limit_step: u64) -> Result<u64, ProgramError> { (limit_delta / settings::LIMIT_STEP).checked_mul(lamports_per_limit_step).ok_or(ProgramError::from(ShineUsersError::MathOverflow)) }
|
||||
fn pad_to_fixed_size(mut bytes: Vec<u8>, target_size: usize) -> Result<Vec<u8>, ProgramError> { require!(bytes.len() <= target_size, ShineUsersError::RecordTooLarge); bytes.resize(target_size, 0); Ok(bytes) }
|
||||
|
||||
fn read_pda_all(pda: &AccountInfo) -> Result<Vec<u8>, ProgramError> { Ok(pda.try_borrow_data().map_err(|_| ProgramError::from(ShineUsersError::InvalidAccountData))?.to_vec()) }
|
||||
|
||||
Reference in New Issue
Block a user