安全 Solana 生态 (3) — 程序升级

安全 Solana 生态 (3) — 程序升级

0. 回顾

1. 概述

在上一篇 博客 中,我们介绍了如何通过 invoke 函数和 invoke_signed 函数实现跨程序调用。在本篇文章中,我们将介绍另一个基础主题——程序升级。

2. 部署一个可升级的程序

默认情况下,所有部署在 Solana 上的程序都使用 BPFLoaderUpgradeable 加载器,这意味着它们可以被升级。此外,Solana 还提供了 --final 选项来确保部署的程序是不可变的。在这种情况下,将使用 BPFLoader2 加载器来部署程序。部署后,程序将无法升级。

BPFLoaderUpgradeable 加载器程序管理三种类型的账户:程序账户程序数据账户缓冲区账户。在部署过程中,首先会创建一个缓冲区账户。之后,Solana CLI 会加载目标程序的字节码并将其写入缓冲区账户。最终,存储在缓冲区账户中的字节码将被复制到程序数据账户中。

因此,缓冲区账户用于可升级 BPF 加载器在部署过程中临时存储字节码,程序数据账户存储程序的实际数据,而程序账户充当指向相应程序数据账户的代理。

接下来,我们将通过几个示例来介绍如何升级您的程序。本文使用的所有代码均可在 此处 找到。

2.1 代码审查 (Rectangle_Area)

在此示例合约中,我们在第 13 至 17 行定义了一个名为 Rectangle 的结构体,其中包含三个属性:widthheightarea。我们还为 Rectangle 定义了一个 area() 函数。area() 函数用于使用 widthheight 计算矩形的面积。

process_instruction() 函数中,我们首先使用 unpack_u32() 函数从指令数据中提取矩形的 widthheight(第 45-46 行)。在第 51 行,提取用于存储矩形数据的账户。在第 59 行,try_from_slice_unchecked() 函数使用 Rectangle 结构体的类型反序列化账户数据。接下来,我们将数据分配给结构体的相应字段并计算面积值(第 61-63 行)。最后一步是序列化数据,并将其写回数据账户。

部署的程序可在以下链接中找到。

https://explorer.solana.com/address/84mMqHRTQT6b2vfsD7XRxVKA3XMd7xoEXFdF6pLNw8y?cluster=testnet

2.2 发送交易

在客户端,我们首先发送一笔交易来创建一个用于存储数据的账户。我们将空间大小设置为 1,024 字节,以确保足够。

然后,我们发送另一笔交易来计算矩形的面积并将其存储到数据账户中。

交易可在以下链接中找到。

https://explorer.solana.com/tx/4PybjXGRpuPKpak7FAz4BKMbcQCWmway69zAxtTnpFRuTTg7onyxW7agSe6ETx44iAGexbgnBUa8WdzdTTQSawJ3?cluster=testnet

3. 升级

现在,我们想在程序中添加一个新函数,用于计算矩形的周长。

编译更新后的项目后,我们使用以下命令在链上升级已部署的程序。

solana program deploy /path/to/program.so --program-id <PROGRAM_ID>

合同可以直接升级,您可以在下方查看相关交易。

https://explorer.solana.com/tx/4Dm9v4zMiijKjQBhatx1D9xbV9PvMLdaonUWLaC2VwzkFvzdgorzbX5vsy4VQ7VxSUmqadftjiDzbyUmXgQchYmk?cluster=testnet

我们进一步向程序发送了一笔交易,以验证升级是否成功。结果显示函数运行良好。

相关交易可在下方找到。

https://explorer.solana.com/tx/21c2G7kPVktAtdUFkH3QwGVi7orajRmy5PJo1UxX1mmAMU68eUkNuLzWYJRBaTzwGi5DxeocYjHfpWiU4hcSFtpQ?cluster=testnet

4. 再次升级

这次我们要将计算出的周长存储到数据账户中。在这种情况下,我们在 Rectangle 结构体中添加了一个名为 perimeter 的新属性(第 18 行)。

由于数据账户的结构与之前不同,我们需要先从数据账户中读取数据。我们使用原始结构体(即 OldRectangle)反序列化存储的数据。之后,我们将相关属性分配给新结构体(即 CurrentRectangle)并初始化新属性(即 perimeter)。

最后,我们计算周长值并将其分配回 update_account,然后再将其序列化到 account_data 中。

升级交易可在下方找到。

https://explorer.solana.com/tx/5J3oKxZXtCi755gD7pMnVh48AFvmeVzRLPgJiyNn8JFeCKx1xpAfJtsi34zjyYKJmnMk8LtC3bcwfFSP7H2gtj5o?cluster=testnet

升级后,您可以使用 solana account <DataAccount> 命令检查存储在账户中的数据。您会发现属性存储在前 32 个字节中。整个升级过程完成。

5. 结论

在本文中,我们介绍了程序如何在 Solana 中进行升级。我们使用了不同的示例来说明详细过程。敬请关注,更多关于 Solana 的文章即将发布。

阅读本系列的其它文章:


关于 BlockSec

BlockSec 是一家开创性的区块链安全公司,由一群在全球享有盛誉的安全专家于 2021 年创立。公司致力于提升新兴 Web3 世界的安全性和可用性,以促进其大规模采用。为此,BlockSec 提供智能合约和 EVM 链 安全审计 服务,用于主动开发和阻止威胁的 Phalcon 平台,用于资金追踪和调查的 MetaSleuth 平台,以及用于 Web3 构建者在加密世界高效冲浪的 MetaSuites 扩展。

迄今为止,公司已为 MetaMask、Uniswap Foundation、Compound、Forta 和 PancakeSwap 等 300 多家尊贵客户提供服务,并在两轮融资中从 Matrix Partners、Vitalbridge Capital 和 Fenbushi Capital 等知名投资者那里获得了数千万美元的投资。

官方网站:https://blocksec.com/

官方 Twitter 账号:https://twitter.com/BlockSecTeam

Sign up for the latest updates
Weekly Web3 Security Incident Roundup | Feb 9 – Feb 15, 2026

Weekly Web3 Security Incident Roundup | Feb 9 – Feb 15, 2026

During the week of February 9 to February 15, 2026, three blockchain security incidents were reported with total losses of ~$657K. All incidents occurred on the BNB Smart Chain and involved flawed business logic in DeFi token contracts. The primary causes included an unchecked balance withdrawal from an intermediary contract that allowed donation-based inflation of a liquidity addition targeted by a sandwich attack, a post-swap deflationary clawback that returned sold tokens to the caller while draining pool reserves to create a repeatable price-manipulation primitive, and a token transfer override that burned tokens directly from a Uniswap V2 pair's balance and force-synced reserves within the same transaction to artificially inflate the token price.

Top 10 "Awesome" Security Incidents in 2025

Top 10 "Awesome" Security Incidents in 2025

To help the community learn from what happened, BlockSec selected ten incidents that stood out most this year. These cases were chosen not only for the scale of loss, but also for the distinct techniques involved, the unexpected twists in execution, and the new or underexplored attack surfaces they revealed.

#10 Panoptic Incident: XOR Linearity Breaks the Position Fingerprint Scheme

#10 Panoptic Incident: XOR Linearity Breaks the Position Fingerprint Scheme

On August 29, 2025, Panoptic disclosed a Cantina bounty finding and confirmed that, with support from Cantina and Seal911, it executed a rescue operation on August 25 to secure roughly $400K in funds. The issue stemmed from a flaw in Panoptic’s position fingerprint calculation algorithm, which could have enabled incorrect position identification and downstream fund risk.