I'm storing a vector of u16 user IDs. I decided not to store public keys in vector, which I can use as ids or get rid of ids completely instead, however public keys are 32 bytes and will have limited space being stored as vector attribute in an account.
I'm creating a lottery program that stores all user IDs who enter, and then assigns some random winner IDs in a separate instruction. This is the Lottery account:
#[account]
pub struct Lottery {
pub authority: Pubkey,
pub all_user_ids: Option<Vec<u16>>,
pub winner_ids: Option<Vec<u16>>,
}
For my user account, I'm doing creating it this:
#[derive(Accounts)]
pub struct CreateUser<'info> {
#[account(
init,
payer = authority,
space = 8 + std::mem::size_of::<UserAccount>(),
seeds = ["user".as_bytes(), authority.key().as_ref()],
bump,
)]
pub user_account: Account<'info, UserAccount>,
#[account(mut,)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
pub fn handler(ctx: Context<CreateUser>, user_id: u16) -> Result<()> {
let user_account = &mut ctx.accounts.user_account;
user_account.id = user_id;
msg!("Created User: {}", user_account.id);
Ok(())
}
The issue is, I'm dependent on client to pass a user ID here, and there is no way to ensure that the user Id is unique. I want to keep it as small as u16. My own client can use different methods to ensure it so that it only sends incremented and unique Ids when creating new accounts for users, but other malicious attacker may call the program instruction from their separate client and add duplicate ids. What can I do here?
I don't want other methods like using zero copy to increase account sound to directly store public keys or anything else. Since I want to keep things simple here, and storing user Ids as u16 vector is easiest method here, just need one solution to a problem.