Week4 of #100DaysOfSolana: Solana's Account Model
Week 4 of #100DaysOfSolana: Solana’s Account Model
#100DaysOfSolana 第四周:Solana 的账户模型
If you come from Web2, Solana’s account model can feel strange at first. The first time I inspected an account with the CLI, I saw fields like lamports, owner, and data all sitting together and thought, “Wait, where is the smart contract state stored?” That question is the whole lesson. 如果你来自 Web2 背景,Solana 的账户模型起初可能会让你感到陌生。我第一次使用 CLI 查看账户时,看到了 lamports、owner 和 data 等字段并列在一起,当时我想:“等等,智能合约的状态存储在哪里?” 这个问题正是理解 Solana 的核心。
On Solana, everything is an account. Wallets are accounts. Program code lives in accounts. Application state lives in accounts. There is no separate world of contract accounts versus externally owned accounts like you may have seen elsewhere. Solana uses one flat key-value store where the key is a 32-byte address and the value is the account itself. 在 Solana 上,一切皆为账户。钱包是账户,程序代码存储在账户中,应用程序状态也存储在账户中。这里没有像其他链那样将合约账户与外部拥有账户(EOA)区分开。Solana 使用一个扁平的键值存储,其中键是 32 字节的地址,值就是账户本身。
That design clicked for me when I started thinking about accounts like files in a filesystem. Each file has metadata, contents, and permissions. Solana accounts work the same way: the address is the filename, the owner is the program allowed to manage it, the data is the file contents, the balance is the lamports stored alongside it, the executable flag tells you whether the file is runnable code or just data. That analogy is not perfect, but it gets you close fast. 当我开始把账户想象成文件系统中的文件时,这种设计就豁然开朗了。每个文件都有元数据、内容和权限。Solana 账户的工作方式如出一辙:地址是文件名,所有者(owner)是允许管理它的程序,数据(data)是文件内容,余额(balance)是随附存储的 lamports,可执行标志(executable flag)则告诉你该文件是可运行的代码还是纯数据。这个类比虽然不完全精确,但能让你快速理解。
The System Program is like the operating system kernel. It creates accounts, assigns ownership, and handles basic bookkeeping. Other programs then read and write the accounts they own. Every Solana account has the same five fields: System Program(系统程序)就像操作系统内核。它负责创建账户、分配所有权并处理基本的记账工作。其他程序则负责读取和写入它们所拥有的账户。每个 Solana 账户都包含相同的五个字段:
- lamports: the balance stored in the account. One SOL equals 1 billion lamports.
- data: a byte array that can hold arbitrary state.
- owner: the program that controls the account.
- executable: whether the account contains runnable program code.
- rent_epoch: a legacy field that is now effectively retired and set to the maximum value.
- lamports:账户中存储的余额。1 SOL 等于 10 亿 lamports。
- data:可以存储任意状态的字节数组。
- owner:控制该账户的程序。
- executable:账户是否包含可运行的程序代码。
- rent_epoch:一个遗留字段,目前实际上已弃用并被设置为最大值。
The most important rule is ownership. Only the owner program can modify an account’s data or debit its lamports. That means a token program can update token accounts it owns, but nobody else can casually rewrite those bytes. At the same time, anyone can credit lamports to a writable account. That combination is simple, but it creates a very clear security model. 最重要的规则是所有权。只有所有者程序才能修改账户的数据或扣除其 lamports。这意味着代币程序可以更新它拥有的代币账户,但其他人无法随意重写这些字节。同时,任何人都可以向可写账户存入 lamports。这种组合虽然简单,却构建了一个非常清晰的安全模型。
What surprised me most was the statelessness of programs. In Web2 terms, a program is not a server that keeps its own memory forever. It is more like a web server binary that reads from and writes to a database. The executable account stores the code, and separate data accounts store the application state. That separation is why the account model matters so much: if you understand who owns the account and what data it contains, you understand how the program behaves. 最让我惊讶的是程序的无状态性。用 Web2 的术语来说,程序不是一个永久保留自身内存的服务器,它更像是一个从数据库读取和写入数据的 Web 服务器二进制文件。可执行账户存储代码,而独立的数据账户存储应用程序状态。这种分离正是账户模型如此重要的原因:如果你了解谁拥有该账户以及它包含什么数据,你就了解了程序的行为方式。
Here is the kind of output I kept seeing while exploring accounts: Each field tells a story. A zero-length data field means the account is just holding value. The owner 11111111111111111111111111111111 is the System Program. executable: false means this is not a program account. And the huge rentEpoch value is a clue that rent exemption has taken over the old rent schedule.
在探索账户时,我经常看到这样的输出:每个字段都在讲述一个故事。长度为零的数据字段意味着该账户仅用于持有价值。所有者 11111111111111111111111111111111 是系统程序。executable: false 意味着这不是一个程序账户。而巨大的 rentEpoch 值则暗示了“租金豁免”机制已经取代了旧的租金计划。
Rent exemption is another detail that makes Solana feel different from a traditional backend. Every account must hold a minimum lamport balance proportional to its data size if it wants to stay on-chain. If the account drops below that threshold, it risks being purged over time. For a tiny basic account, the minimum is around 0.00089 SOL, though the exact value depends on the account size. You can check it with solana rent or the getMinimumBalanceForRentExemption RPC method.
租金豁免是另一个让 Solana 有别于传统后端的细节。如果账户想要留在链上,就必须持有与其数据大小成比例的最低 lamport 余额。如果账户余额低于该阈值,它就有可能随时间推移被清除。对于一个微小的基础账户,最低余额约为 0.00089 SOL,具体数值取决于账户大小。你可以使用 solana rent 命令或 getMinimumBalanceForRentExemption RPC 方法进行查询。
That idea matters because it forces you to think about storage costs up front. If you create a larger state account, you need to fund it accordingly. In Web2, disk space is usually someone else’s problem. On Solana, the cost of storage is part of the design. 这个概念很重要,因为它迫使你在开发初期就考虑存储成本。如果你创建一个较大的状态账户,就需要为其提供相应的资金。在 Web2 中,磁盘空间通常是别人的问题;而在 Solana 上,存储成本是设计的一部分。
The account model also explains why Solana feels so composable. Programs do not hide their state inside private memory. They operate on explicit accounts that can be inspected, passed around, and reasoned about independently. Once I stopped thinking in terms of “a contract with internal state” and started thinking in terms of “a program that manages files,” the architecture made much more sense. 账户模型也解释了为什么 Solana 具有如此高的可组合性。程序不会将状态隐藏在私有内存中,它们操作的是显式的账户,这些账户可以被独立检查、传递和分析。一旦我不再用“带有内部状态的合约”来思考,而是转而用“管理文件的程序”来思考,整个架构就变得容易理解多了。
If you are a Web2 developer, that is the mental model I would keep: Solana is a filesystem for programmable state. Accounts are the files. Programs are the executables. The System Program is the kernel. And ownership is the permission system that keeps everything safe. That framing turned Solana from something mysterious into something structured. The account model is not just an implementation detail. It is the core abstraction that makes the rest of Solana understandable. 如果你是一名 Web2 开发者,我建议你保持这样的思维模型:Solana 是一个用于可编程状态的文件系统。账户是文件,程序是可执行文件,系统程序是内核,而所有权是保障一切安全的权限系统。这种框架将 Solana 从神秘变得结构化。账户模型不仅仅是一个实现细节,它是让 Solana 其他部分变得可理解的核心抽象。