SHA256
Добавить безопасную очистку user PDA перед запуском 1.0
This commit is contained in:
@@ -51,6 +51,9 @@ const IX_CREATE_NAMESPACE: u8 = 7;
|
||||
const IX_UPDATE_NAMESPACE_PRICE: u8 = 8;
|
||||
const IX_SUPPORT_USER: u8 = 9;
|
||||
const IX_SUPPORT_DAO: u8 = 10;
|
||||
/// Одноразовая административная инструкция промежуточного релиза reset.
|
||||
/// В финальную публичную 1.0 она не войдёт.
|
||||
const IX_RESET_USER_PDA: u8 = 11;
|
||||
const SUPPORT_STATS_VERSION: u8 = 2; // ONLY Stars cents, old SOL stats rejected
|
||||
const SUPPORT_STATS_LEN: usize = 25;
|
||||
const SUPPORT_TARGET_USER: u8 = 0;
|
||||
@@ -228,6 +231,13 @@ pub struct CloseLegacyUserPdaArgs {
|
||||
pub login: String,
|
||||
}
|
||||
|
||||
/// Логин нужен не для поиска по данным аккаунта, а чтобы программа сама вывела
|
||||
/// допустимый PDA. Это исключает закрытие произвольного PDA программы.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ResetUserPdaArgs {
|
||||
pub login: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UpdateUsersEconomyConfigArgs {
|
||||
pub update_fee_lamports: u64,
|
||||
@@ -343,6 +353,7 @@ fn process_instruction<'a>(program_id: &Pubkey, accounts: &'a [AccountInfo<'a>],
|
||||
process_update_namespace_price(program_id, accounts, &namespace_name, currency_code, registration_fee)
|
||||
},
|
||||
IX_CLOSE_LEGACY_USER_PDA => dispatch_close_legacy_user_pda(program_id, accounts, r),
|
||||
IX_RESET_USER_PDA => dispatch_reset_user_pda(program_id, accounts, r),
|
||||
_ => Err(ProgramError::from(ShineUsersError::InvalidInstruction)),
|
||||
}
|
||||
}
|
||||
@@ -469,6 +480,12 @@ fn dispatch_close_legacy_user_pda(program_id: &Pubkey, accounts: &[AccountInfo],
|
||||
process_close_legacy_user_pda(program_id, accounts, args)
|
||||
}
|
||||
|
||||
fn dispatch_reset_user_pda(program_id: &Pubkey, accounts: &[AccountInfo], mut r: Reader<'_>) -> ProgramResult {
|
||||
let args = ResetUserPdaArgs { login: r.read_string_u8()? };
|
||||
r.finish()?;
|
||||
process_reset_user_pda(program_id, accounts, args)
|
||||
}
|
||||
|
||||
fn process_init_users_economy_config(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
|
||||
let mut it = accounts.iter();
|
||||
let signer = next_account_info(&mut it)?;
|
||||
@@ -976,6 +993,60 @@ fn process_close_legacy_user_pda(program_id: &Pubkey, accounts: &[AccountInfo],
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Служебная инструкция только для планового запуска публичного формата 1.0.
|
||||
///
|
||||
/// Accounts:
|
||||
/// 0. `[signer, writable]` текущая upgrade authority; ей возвращается рента;
|
||||
/// 1. `[writable]` пользовательский PDA;
|
||||
/// 2. `[]` executable Program аккаунт `shine_users`;
|
||||
/// 3. `[]` связанный ProgramData аккаунт Upgradeable Loader.
|
||||
///
|
||||
/// Эта инструкция не умеет трогать namespace, economy config или иной PDA:
|
||||
/// адрес обязательно выводится из переданного login по одному из двух
|
||||
/// исторических user-seed форматов, а содержимое обязано быть user-записью SHiNE.
|
||||
fn process_reset_user_pda(program_id: &Pubkey, accounts: &[AccountInfo], args: ResetUserPdaArgs) -> ProgramResult {
|
||||
let mut it = accounts.iter();
|
||||
let authority = next_account_info(&mut it)?;
|
||||
let user_pda = next_account_info(&mut it)?;
|
||||
let program_ai = next_account_info(&mut it)?;
|
||||
let programdata_ai = next_account_info(&mut it)?;
|
||||
require!(it.next().is_none(), ShineUsersError::InvalidInstruction);
|
||||
require!(authority.is_writable && user_pda.is_writable, ShineUsersError::InvalidAccountData);
|
||||
validate_upgrade_authority(program_id, authority, program_ai, programdata_ai)?;
|
||||
|
||||
let expected_pda = if args.login.contains('@') {
|
||||
validate_full_login(&args.login)?;
|
||||
find_user_pda(program_id, &args.login).0
|
||||
} else {
|
||||
validate_legacy_login(&args.login)?;
|
||||
find_legacy_user_pda(program_id, &args.login.to_ascii_lowercase()).0
|
||||
};
|
||||
require_keys_eq!(expected_pda, *user_pda.key, ShineUsersError::InvalidPdaAddress);
|
||||
require!(user_pda.owner == program_id, ShineUsersError::InvalidAccountOwner);
|
||||
// Проверяем заголовок, но намеренно принимаем любой исторический minor:
|
||||
// это переходная утилита для удаления всех форматов перед чистым запуском.
|
||||
record_format_minor(&read_pda_all(user_pda)?)?;
|
||||
|
||||
let lamports = user_pda.lamports();
|
||||
{
|
||||
let mut authority_lamports = authority.try_borrow_mut_lamports()
|
||||
.map_err(|_| ProgramError::from(ShineUsersError::InvalidAccountData))?;
|
||||
**authority_lamports = (**authority_lamports)
|
||||
.checked_add(lamports)
|
||||
.ok_or(ProgramError::from(ShineUsersError::MathOverflow))?;
|
||||
}
|
||||
{
|
||||
let mut pda_lamports = user_pda.try_borrow_mut_lamports()
|
||||
.map_err(|_| ProgramError::from(ShineUsersError::InvalidAccountData))?;
|
||||
**pda_lamports = 0;
|
||||
}
|
||||
// realloc сам по себе оставит владельцем программу. Возвращаем System
|
||||
// Program, чтобы финальная 1.0 могла создать новую запись по тому же PDA.
|
||||
user_pda.realloc(0, false)?;
|
||||
user_pda.assign(&system_program::id());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_users_economy_config(state: &UsersEconomyConfigState) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(1 + 8);
|
||||
out.push(state.version);
|
||||
|
||||
Reference in New Issue
Block a user