Solana 计数器教程:账户数据的读写

  • 0xE
  • 发布于 11小时前
  • 阅读 105

本文基于前述 Solana 账户初始化内容,扩展了如何通过 Anchor 框架实现账户数据的读写操作,通过新增 set() 函数展示数据写入流程,优化代码结构,并介绍了使用命令行和程序内方法读取账户数据的技术细节,最终实现了一个简单的计数器功能。

在前文《Solana 与 Anchor 中的账户初始化》中,我们探讨了如何初始化账户以持久化存储数据。本文将展示如何向已初始化的账户写入数据,并读取其内容。

以下基于之前的代码,新增 set() 函数,用于更新 MyStorage 中的 x 值:

use anchor_lang::prelude::*;
use std::mem::size_of;

declare_id!("9XZGDi1imvGFV3bLuDJK1bkUht51GPCRsVxMe4DpqWEo");

#[program]
pub mod basic_storage {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        Ok(())
    }

    // 新增:写入 x 值
    pub fn set(ctx: Context<Set>, new_x: u64) -> Result<()> {
        ctx.accounts.my_storage.x = new_x;
        Ok(())
    }
}

// 新增:Set 账户结构
#[derive(Accounts)]
pub struct Set<'info> {
    #[account(mut, seeds = [], bump)]
    pub my_storage: Account<'info, MyStorage>,
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(
        init,
        payer = signer,
        space = size_of::<MyStorage>() + 8,
        seeds = [],
        bump
    )]
    pub my_storage: Account<'info, MyStorage>,
    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[account]
pub struct MyStorage {
    x: u64,
}

测试代码更新,新增调用 set(10):

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { BasicStorage } from "../target/types/basic_storage";
import { BN } from "@coral-xyz/anchor";

describe("basic_storage", () => {
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.BasicStorage as Program<BasicStorage>;

  it("Is initialized!", async () => {
    const seeds = []
    const [myStorage, _bump] = anchor.web3.PublicKey.findProgramAddressSync(seeds, program.programId);

    console.log("the storage account address is", myStorage.toBase58());

    await program.methods.initialize().accounts({ myStorage: myStorage }).rpc();
  });

  it("set the value", async () => {
    const seeds = []
    const [myStorage, _bump] = anchor.web3.PublicKey.findProgramAddressSync(seeds, program.programId);

    await program.methods.set(new BN(10)).accounts({ myStorage: myStorage }).rpc();

  })

});

解析 set() 函数

写入逻辑

ctx.accounts.my_storage.x = new_x 的工作原理:

  • ctx.accounts 提供对 Set 结构中账户的访问权限,由 #[derive(Accounts)] 宏自动实现。
  • my_storage 标记为 mut,允许修改其字段 x。
  • Account<'info, MyStorage> 类型确保 my_storage 是 MyStorage 结构体的引用。

幕后流程

  1. 测试客户端传递 myStorage 账户地址。
  2. set() 加载账户数据,反序列化后更新 x,再序列化并存储。

Set 结构

Set 仅需一个可变引用 my_storage,比 Initialize 更简洁:

  • mut 表示数据可写。
  • seeds = [] 和 bump 用于验证账户地址,确保客户端提供的是程序拥有的账户。
  • bump 是 PDA 生成时的偏移量,防止地址与有效公钥冲突,暂视为样板。

注意:尽管程序可推导地址,Solana 运行时要求客户端显式提供账户,后续文章将详解。


优化 set() 的写法

若需更新多个字段,重复 ctx.accounts.my_storage 显得冗余:

ctx.accounts.my_storage.x = new_x;
ctx.accounts.my_storage.y = new_y;

可使用 Rust 的可变引用(&mut)简化:

pub fn set(ctx: Context&lt;Set>, new_x: u64) -> Result&lt;()> {
    let my_storage = &mut ctx.accounts.my_storage;  // 获取可变引用
    my_storage.x = new_x;
    Ok(())
}

查看账户数据

命令行查询

在本地验证器运行时,使用以下命令查看账户:

solana account 2m3p5Y58CDYnJ9k32tVRW9vY4M8Vfabk6eqM7zgQLKRh # 替换为测试输出的地址

输出示例:

Public Key: 2m3p5Y58CDYnJ9k32tVRW9vY4M8Vfabk6eqM7zgQLKRh
Balance: 0.00100224 SOL
Owner: 9XZGDi1imvGFV3bLuDJK1bkUht51GPCRsVxMe4DpqWEo
Executable: false
Rent Epoch: 18446744073709551615
Length: 16 (0x10) bytes
0000:   1c f2 3b 85  43 19 31 28  0a 00 00 00  00 00 00 00   ..;.C.1(........
  • 前 8 字节(1c f2 3b 85 43 19 31 28)为鉴别器。
  • 后面 0a 为 x 的值,0a 即十进制的 10。

程序内读取

添加 print_x() 函数读取 x:

pub fn print_x(ctx: Context&lt;PrintX>) -> Result&lt;()> {
    let x = ctx.accounts.my_storage.x;
    msg!("The value of x is {}", x);
    Ok(())
}

#[derive(Accounts)]
pub struct PrintX&lt;'info> {
    pub my_storage: Account&lt;'info, MyStorage>, // 无需 mut,仅读取
}

测试中添加以下行:

await program.methods.printX().accounts({myStorage: myStorage}).rpc();

运行后,Solana 日志将显示 x 的值: The value of x is 10。


【笔记配套代码】 https://github.com/0xE1337/rareskills_evm_to_solana 【参考资料】 https://learnblockchain.cn/column/119 https://www.rareskills.io/solana-tutorial

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
0xE
0xE
0x59f6...a17e
17年进入币圈,Web3 开发者。刨根问底探链上真相,品味坎坷悟 Web3 人生。