29198
Hope you like it! We will share some here Blockchain security related content Hi, this is ChainSecLabs! ![]() storage cost When allocating storage space, the payer must pay a certain amount of SOL for each byte allocated. Solana calls this “rent.” The name comes from the way Solana initially charged an annual fee based on the number of bytes of storage occupied by an account. If you only pay half a year's rent, the account will be cleared after six months ; If you pay two years' rent in advance, the account will be considered "rent exempt" and you will not need to pay rent in the future. Now, all accounts must meet rent-free standards when created. Although rent is calculated on a "per byte" basis, it is not free even if the account does not store data. ; Solana still needs to index them and store the associated metadata. The following is the code for calculating fees under the Anchor framework: use anchor_lang::prelude::*; use anchor_lang::solana_program::rent as rent_module; declare_id!("BfMny1VwizQh89rZtikEVSXbNCVYRmi6ah8kzvze5j1S"); #[program]pub mod rent { use super::*; pub fn initialize(ctx: Context<Initialize>) -> Result<()> { let cost_of_empty_acc = rent_module::ACCOUNT_STORAGE_OVERHEAD as f64 * rent_module::DEFAULT_LAMPORTS_PER_BYTE_YEAR as f64 * rent_module::DEFAULT_EXEMPTION_THRESHOLD; msg!("cost to create an empty account: {}", cost_of_empty_acc); 32 as f64 * rent_module::DEFAULT_LAMPORTS_PER_BYTE_YEAR as f64 * rent_module::DEFAULT_EXEMPTION_THRESHOLD; msg!("cost to create a 32 byte account: {}", cost_for_32_bytes); } } #[derive(Accounts)]pub struct Initialize {} ![]() Storage size When initializing an account, only accounts no larger than 10240 bytes can be initialized. If you need to increase the account size, you can use the realloc macro. Each realloc can only add up to 10240 bytes. The maximum account size in Solana is 10MB. For examples, see the increase_account_size function and the IncreaseAccountSize context structure, which increase the account size by 1000 bytes. use anchor_lang::prelude::*; use std::mem::size_of; declare_id!("GLKUcCtHx6nkuDLTz5TNFrR4tt4wDNuk24Aid2GrDLC6"); #[program]pub mod basic_storage { use super::*; pub fn initialize(ctx: Context<Initialize>) -> Result<()> { Ok(()) } pub fn increase_account_size(ctx: Context<IncreaseAccountSize>) -> Result<()> { Ok(()) } } #[derive(Accounts)]pub struct IncreaseAccountSize<'info> { #[account(mut, realloc = size_of::<MyStorage>() + 8 + 1000, realloc::payer = signer, realloc::zero = false, seeds = [], bump)] pub my_storage: Account<'info, MyStorage>, #[account(mut)]pub signer: Signer<'info>, pub system_program: Program<'info, System>, } #[derive(Accounts)]pub struct Initialize<'info> { #[account(init, payer = signer, space=size_of::<MyStorage>() + 8, seeds = [], bump)] pub my_storage: Account<'info, MyStorage>, #[account(mut)]pub signer: Signer<'info>, pub system_program: Program<'info, System>, } #[account]pub struct MyStorage { x: u64, } When increasing the account size, if you do not want to clear the account data, you need to set realloc::zero = false If you need to set all account data to zero, use realloc::zero = true 。 ![]() Summarize Solana's account rent mechanism is an important part of its resource management. Through the design of rent and exempt balance, it ensures that network storage resources are properly utilized. For developers, reasonable handling of account rental settings is the basis for building stable and reliable applications. ; For users, understanding the rental mechanism helps them better manage and maintain their account assets. Author: Yu Wenjie Editor:Chloe Reviewed by: Ssugar END ![]() Recommended in the past EOA+SCA=? ![]() Interacting with the SPL Token Program using the Anchor framework ![]() Tornado Cash: How to hide on-chain privacy ![]() Full analysis of ERC4626 expansion attack and defense solutions ![]() Search the official account and follow us [ChainSecLabs] ![]() Disclaimer This article is intended to share knowledge and experience related to security technology. The content is for learning and research reference only. The technical means, tools or operating procedures mentioned in the article are based on public information and the author's personal understanding. They do not represent any official position, nor do they constitute specific operating suggestions for readers. Please do not use the technology described in this article for any illegal purpose or you will do so at your own risk. The author strictly prohibits and resolutely opposes all illegal activities such as cyber attacks, illegal intrusions, and data theft, and does not assume any direct or indirect liability arising from improper use of the article content by readers. If third-party tools or materials are cited in the article, the copyright belongs to the original author. If there is any infringement or inappropriateness, please contact us in time and we will deal with it as soon as possible. Network security is related to law and ethics. Readers are asked to learn independently and apply it rationally on the premise of legal compliance. |