Intro to the Solana Account models
What is the account model?
The way that smart contracts track state, e.g.:
token balance
who owns a NFT
……
The Account Model
The account model has below attributes:
lamports (1 lamports = 0.000000001 SOL)
who is the owner?
executable?
data (bytes)
rent_epoch (Next time to pay rent)
There are 3 types of Accounts:
Data accounts:
- for store data
Program accounts:
- for storing executable programs
Native accounts:
- to indicate native programs on Solana such as System, Stake, and Vote
Rent
Everything should have a cost. We write data on chain by data account, there is a cost too. We need to pay rent for the account which depends on byte size.
Accounts which maintain a minimum balance equivalent to 2 years of rent payments are exempt. (This is the usual way to do)
A brief comparison with ETH's model
ETH's account model:
variables only direct R/W within the same contract
expose value through function
Solana's account model:
has its address.
only writable by the owner program.
readable globally.
Example of usage
The data account's address is generated by hashing some seeds along with the program address.
For different users, the signer address is included in seeds, so each per-user-data-records can be stored in a definite account per user.
Why Solana’s account model is so different?
Solana's transaction has a nature that:
You must specify all accounts' addresses involved in R/W during the transaction.
So Solana nodes know beforehand what accounts will be R/W for transactions. It can then manage to run transactions parallelly.
This is one of the magic why Solana has such a highly optimized performance.
Reference: