Understanding Solana’s Account Model From a Web2 Perspective

Understanding Solana’s Account Model From a Web2 Perspective

从 Web2 视角理解 Solana 的账户模型

In a previous article I posted on my #100daysofsolana journey, I wrote about how everything on Solana is an account. I spoke briefly about the difference between wallet accounts and program accounts. You can read the article here. In this article, we will go more in depth because I now have more knowledge after inspecting various accounts over the past few days. 在我之前的 #100daysofsolana 系列文章中,我曾写过 Solana 上的一切皆为账户。我简要讨论了钱包账户和程序账户之间的区别(你可以在此处阅读该文章)。在本文中,我将进行更深入的探讨,因为在过去几天检查了各种账户后,我有了更深入的了解。

Everything is an account. But not all accounts are the same. Unlike Ethereum, which separates wallet accounts from smart contracts, Solana uses the same model for all accounts. They all have the same five fields, but what differs is the meaning and value of those fields. 一切皆为账户,但并非所有账户都相同。与将钱包账户与智能合约区分开来的以太坊不同,Solana 对所有账户使用相同的模型。它们都拥有相同的五个字段,但不同之处在于这些字段的含义和数值。

The Account’s Five Fields

账户的五个字段

Every account on the Solana network has these five fields. Here’s what they actually mean: Solana 网络上的每个账户都包含以下五个字段。它们的具体含义如下:

  1. Lamports: This is the field that carries information on how much the account holds. Solana stores balances in lamports. 1 SOL = 1 billion lamports. Sometimes, depending on how you are viewing the account structure, this field shows up as Balance with the value in SOL. Just know it’s showing the same thing in a more human-readable format.

  2. Lamports: 该字段携带账户持有金额的信息。Solana 以 lamports 为单位存储余额。1 SOL = 10 亿 lamports。有时,根据你查看账户结构的方式,该字段会显示为“余额”(Balance),数值以 SOL 为单位。只需知道它以更易读的格式显示了相同的内容即可。

  3. Data: The data field refers to the storage space the account occupies. In Web2, this is similar to a database entry. It is stored as a raw byte array. For example, the Token Program’s data field looks like this in the raw output:

  4. Data: 数据字段指账户占用的存储空间。在 Web2 中,这类似于数据库条目。它以原始字节数组的形式存储。例如,Token Program 的数据字段在原始输出中如下所示: 0000: 02 00 00 00 27 f1 90 b1 e8 ca df f9 f8 fc 45 cb 0010: d3 af 98 b8 8e 5f ac 42 0d 97 dd 37 ce 71 4c 44 In a more readable format, it shows up as Length: 36 (0x24) bytes. Wallet accounts have an empty or zero data field because they are simple SOL holders. They don’t store anything beyond a balance. But program accounts, token accounts, and NFTs all have data because they store state. 以更易读的格式显示为 Length: 36 (0x24) bytes。钱包账户的数据字段为空或为零,因为它们只是简单的 SOL 持有者,除了余额之外不存储任何内容。但程序账户、代币账户和 NFT 都有数据,因为它们存储了状态。

This is also where something important happens: Programs on Solana don’t store their own state inside themselves. A program’s executable code lives in one account, and the data it reads or writes lives in completely separate accounts. So when you see a program account with only 36 bytes, that’s not where the user balances are, those live in their own individual accounts that the program manages externally. 这里有一个重要的点:Solana 上的程序不会在自身内部存储状态。程序的执行代码存在于一个账户中,而它读写的数据则存在于完全独立的账户中。因此,当你看到一个只有 36 字节的程序账户时,那并不是用户余额所在的地方;用户余额存在于程序外部管理的独立账户中。

  1. Owner: This field tells us which program controls and is able to modify the account. Wallet accounts are owned by the System Program. Program accounts are typically owned by the BPF Loader. Built-in system programs, like the System Program itself, are owned by the Native Loader. Solana has a security model in place such that a program can only write to the accounts it owns.

  2. Owner: 该字段告诉我们哪个程序控制并能够修改该账户。钱包账户由系统程序(System Program)拥有。程序账户通常由 BPF 加载器(BPF Loader)拥有。内置系统程序(如系统程序本身)由原生加载器(Native Loader)拥有。Solana 拥有一套安全模型,即程序只能写入其拥有的账户。

  3. Executable: This is a boolean field that returns true or false. It tells us whether the account has a set of instructions written in code that it can run. Program accounts have executable: true because they run programmed instructions. Other accounts like wallets and token accounts have executable: false because they don’t run code, they just hold state.

  4. Executable: 这是一个返回 true 或 false 的布尔字段。它告诉我们该账户是否拥有一组可以运行的代码指令。程序账户的 executabletrue,因为它们运行编程指令。其他账户(如钱包和代币账户)的 executablefalse,因为它们不运行代码,只存储状态。

  5. Rent Epoch: Solana has a rent system; a cost for storing data on-chain. The rent epoch field was originally designed to show when the next rent would be deducted from the balance. Currently, accounts have been made rent-exempt by being funded with enough SOL upfront, which made this field less important.

  6. Rent Epoch: Solana 有一套租金系统,即在链上存储数据的成本。Rent Epoch 字段最初旨在显示下一次从余额中扣除租金的时间。目前,账户通过预先存入足够的 SOL 来实现免租,这使得该字段的重要性降低了。

How to think about it as a Web2 developer

作为 Web2 开发人员该如何理解

We can compare Solana’s account model to how a React app makes calls to a backend REST API in Web2. Stay with me. The Solana program is like a backend API, and accounts are the databases the API reads or writes to. The frontend sends a request, and the backend (the program) executes what changes need to happen. On Solana, that request is called a transaction. Just like a REST API hits a POST or PUT endpoint, a Solana transaction invokes a specific program instruction. 我们可以将 Solana 的账户模型比作 Web2 中 React 应用调用后端 REST API 的方式。请听我解释:Solana 程序就像后端 API,而账户就是 API 读写的数据库。前端发送请求,后端(程序)执行所需的变更。在 Solana 上,该请求被称为“交易”(Transaction)。就像 REST API 访问 POST 或 PUT 端点一样,Solana 交易会调用特定的程序指令。

The main thing to remember is that neither system stores information inside the request handler. Instead, data is stored separately, in a database in Web2, or in accounts on Solana. These accounts are like rows in a database. Each one holds some information about the application, identified by a special key. 需要记住的核心点是,这两个系统都不会在请求处理程序内部存储信息。相反,数据是分开存储的——在 Web2 中存储在数据库中,在 Solana 中存储在账户中。这些账户就像数据库中的行。每一行都保存着关于应用程序的一些信息,并由一个特殊的键(Key)标识。

So when you look at a Solana account, you can think of it like a record in a database: 因此,当你查看 Solana 账户时,可以将其视为数据库中的一条记录:

  • The public key is like the identifier for the record
  • 公钥(Public Key)就像记录的标识符
  • The data field is the actual information in the record
  • 数据字段(Data)是记录中的实际信息
  • The lamports field is like a balance or credit attached to the record
  • Lamports 字段就像附加在记录上的余额或信用额度
  • The owner field is the service that is allowed to make changes to the record
  • 所有者字段(Owner)是允许对记录进行更改的服务

Solana’s ownership rules

Solana 的所有权规则

Like I mentioned earlier, in Solana only the owner of an account can modify it. This sounds straightforward, but let’s drive it home from both a Web2 and Solana perspective. Only the owner can modify an account, but anyone can credit it. Meaning anyone can send SOL to any account. This is the security model Solana enforces at the protocol level. 正如我之前提到的,在 Solana 中,只有账户的所有者才能修改它。这听起来很简单,但让我们从 Web2 和 Solana 的角度深入理解一下。只有所有者可以修改账户,但任何人都可以向其存入资金。这意味着任何人都可以向任何账户发送 SOL。这是 Solana 在协议层面强制执行的安全模型。

Just like in a REST API system, only the backend service that owns a database table is allowed to modify the rows. Other services can request to read the data, but they can’t write to it unless they’re authorized. Solana enforces the same idea, only the owner program is allowed to mutate the account’s data. So if a token account is owned by the Token Program, no other program can directly edit its balance. They must go through the Token Program’s instructions, just like a frontend must go through the correct API endpoint instead of writing directly to the database. 就像在 REST API 系统中,只有拥有数据库表的后端服务才被允许修改行数据。其他服务可以请求读取数据,但除非获得授权,否则无法写入。Solana 强制执行相同的理念:只有所有者程序才被允许修改账户数据。因此,如果一个代币账户由 Token Program 拥有,其他程序无法直接编辑其余额。它们必须通过 Token Program 的指令进行操作,就像前端必须通过正确的 API 端点而不是直接写入数据库一样。

What are rent exemptions?

什么是免租(Rent Exemptions)?

I mentioned that recently, accounts have been made rent-exempt. What does that actually mean? In Solana, every account is like data stored on-chain, and you have to pay some amount to keep it there. That’s rent. For a basic account with no extra data, it’s roughly 0.00089 SOL. But it’s not a fixed amount, it’s calculated based on the size of the data an account holds using the rent calculation formula. 我提到最近账户已经实现了免租。这到底意味着什么?在 Solana 中,每个账户就像存储在链上的数据,你必须支付一定的费用才能将其保留在那里,这就是租金。对于没有额外数据的基本账户,费用大约是 0.00089 SOL。但这并不是固定金额,而是根据账户持有数据的大小,使用租金计算公式得出的。

In older versions of the model, this rent was collected periodically. When an account didn’t have enough lamports, the network would reclaim it. But now, instead of paying periodically, you just fund the account with enough SOL to meet the minimum balance threshold. That makes it permanent. 在旧版本的模型中,租金是定期收取的。当账户没有足够的 lamports 时,网络会回收该账户。但现在,你无需定期支付,只需向账户存入足够的 SOL 以达到最低余额阈值即可。这使得账户可以永久存在。