Understand zero copy accounts in Solana
Solana is a popular blockchain platform that admits zero cost accounts, also known as #[zero copy)] or #[zero copy (insecure))]. In this article,
#[Copy (zero copy)] (Sure)
The most common variant of the zero copy account is #[account (zero copy)]
. This guy ensures that any change. The #[Copy (insecure)) variant is similar but uses an insecure approach.
#[account (zero copy (insecure))
The less common variant of the zero copy account is#[zero copy (insecure))]. This type allows changes without any security verification, however, this variant should only be used with caution and in specific scenarios.
Differences
The main difference between these two variants lies in their behavior:
(SAFE): Any change made within the account is reflected in all chains by using a safe and immutable block.
: The changes are allowed within the account without any security verification. This variant should only be used with caution and in specific scenarios.
When using each
Use#[zero copy)]When:
*
*
*
Use#[account (zero copy (insecure))]When:
.
** Sing to write an insecure code in oxide?
Yes, it is possible to write an insecure code directly in Rust. However, using zero copy accounts is generally safer than writing without processing. In addition, you can use Libana-Program and Anchor
to work with zero copy accounts safely.
Conclusion
The differences between the differences between the zero copy accounts. Wear
Example code
Here is an example of how to use a zero cost account in Rust:
`oxide
Use Solana_program :: {
ACCOUNT_INFO :: {Next_account_info, Accountinfo},
Entrypoint :: Programult,
Program_eror :: printerror,
};
use anchor_lag :: {account_info, entrypoint};
// Define and work to deposit money on the account
Fn deposit (account_id: & accouinfo <'_>, quantity: u64) -> programresult {
// Obtain the current account balance
Let mut balance = next_account_info (account_)?;
// deposit the specified amount
balance.amount += quantity;
// Save the balance updated to the account
Nextacountinfo :: Set (account_, and balance)?;
Printeror! (“Successful deposit”);
OK (())
}
// Define and zero cost account
#[Zero Copy (insecure))]
Zerocopyacount structure {
Deposit: U64,
}
Anchor impirogram for zerocopyacount {
Type Systemprograminfo = Accountinfo <'_>;
Type programid = programid;
Write programmer = imprinteror;
Fn new () -> self :: programid {
// create a new program identification
0x1234567890ABCDEF
}
Fn call (& self, quantity: u64) -> programresult {
Deposit (self.