solana项目counter,测试过程中执行报错记录分享

跟随HackQuest部署counter项目,使用 Solana 官方提供的 playgroud 。这个平台让我们的部署和测试过程变得更加简便高效。

合约代码

lib.rs中复制以下代码

use anchor_lang::prelude::*;
use std::ops::DerefMut;declare_id!("CVQCRMyzWNr8MbNhzjbfPu9YVvr97onF48Lc9ZwXotpW");#[program]
pub mod counter {use super::*;pub fn initialize(ctx: Context<Initialize>) -> Result<()> {let counter = ctx.accounts.counter.deref_mut();let bump = ctx.bumps.counter;*counter = Counter {authority: *ctx.accounts.authority.key,count: 0,bump,};Ok(())}pub fn increment(ctx: Context<Increment>) -> Result<()> {require_keys_eq!(ctx.accounts.authority.key(),ctx.accounts.counter.authority,ErrorCode::Unauthorized);ctx.accounts.counter.count += 1;Ok(())}
}#[derive(Accounts)]
pub struct Initialize<'info> {#[account(init,payer = authority,space = Counter::SIZE,seeds = [b"counter"],bump)]counter: Account<'info, Counter>,#[account(mut)]authority: Signer<'info>,system_program: Program<'info, System>,
}#[derive(Accounts)]
pub struct Increment<'info> {#[account(mut,seeds = [b"counter"],bump = counter.bump)]counter: Account<'info, Counter>,authority: Signer<'info>,
}#[account]
pub struct Counter {pub authority: Pubkey,pub count: u64,pub bump: u8,
}impl Counter {pub const SIZE: usize = 8 + 32 + 8 + 1;
}#[error_code]
pub enum ErrorCode {#[msg("You are not authorized to perform this action.")]Unauthorized,
}

交换代码

client.ts中复制以下代码

const wallet = pg.wallet;
const program = pg.program;
const counterSeed = Buffer.from("counter");const counterPubkey = await web3.PublicKey.findProgramAddressSync([counterSeed],pg.PROGRAM_ID
);const initializeTx = await pg.program.methods.initialize().accounts({counter: counterPubkey[0],authority: pg.wallet.publicKey,systemProgram: web3.SystemProgram.programId,}).rpc();let counterAccount = await program.account.counter.fetch(counterPubkey[0]);
console.log("account after initializing ==> ", Number(counterAccount.count));const incrementTx = await pg.program.methods.increment().accounts({counter: counterPubkey[0],authority: pg.wallet.publicKey,}).rpc();counterAccount = await program.account.counter.fetch(counterPubkey[0]);
console.log("account after increasing ==>", Number(counterAccount.count));

部署和发布

  • 切换到左侧工具栏第二个按钮并点击 build
  • 点击deploy, 终端出现 “Deployment successful.” 即为部署成功。(这大约会消耗2~3个sol)

测试交互

切换到左侧第一个文件按钮并点击 Run ,运行client.ts文件,输出如图
在这里插入图片描述

再次执行就会开始报错

Running client…
client.ts:
Uncaught error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0

各种求助之后,修改初始化代码,输出报错日志

try {// Initialize accountconst initializeTx = await pg.program.methods.initialize().accounts({counter: counterPubkey,authority: pg.wallet.publicKey,systemProgram: web3.SystemProgram.programId,}).rpc();} catch (error) {console.error("Error fetching counter account:", error);}

报错日志如下:

  logs: [ 'Program CVQCRMyzWNr8MbNhzjbfPu9YVvr97onF48Lc9ZwXotpW invoke [1]','Program log: Instruction: Initialize','Program 11111111111111111111111111111111 invoke [2]','Allocate: account Address { address: 8sKt2NcKbN9E2SUE6E3NgfzwGi5ByBZxHQdCw27bMef1, base: None } already in use','Program 11111111111111111111111111111111 failed: custom program error: 0x0','Program CVQCRMyzWNr8MbNhzjbfPu9YVvr97onF48Lc9ZwXotpW consumed 4867 of 200000 compute units','Program CVQCRMyzWNr8MbNhzjbfPu9YVvr97onF48Lc9ZwXotpW failed: custom program error: 0x0' ],programErrorStack: { stack: [ [Object], [Object] ] } }

根据日志信息,关键的错误是:

'Allocate: account Address { address: 8sKt2NcKbN9E2SUE6E3NgfzwGi5ByBZxHQdCw27bMef1, base: None } already in use'

这表示你尝试分配的账户地址 8sKt2NcKbN9E2SUE6E3NgfzwGi5ByBZxHQdCw27bMef1 已经被使用,因此无法进行新的初始化。这通常发生在你已经为该地址创建了账户,但可能在再次执行时未考虑到这一点。

修改initializeTx代码

let counterAccount;
try {counterAccount = await program.account.counter.fetch(counterPubkey);console.log("Counter account already exists:", counterAccount);
} catch (error) {if (error.message.includes("Account does not exist")) {console.log("Counter account does not exist, proceeding with initialization...");// Initialize accountconst initializeTx = await pg.program.methods.initialize().accounts({counter: counterPubkey,authority: pg.wallet.publicKey,systemProgram: web3.SystemProgram.programId,}).rpc();} else {console.error("Error fetching counter account:", error);}
}

执行结果:
在这里插入图片描述

修复后完整client.ts代码

const wallet = pg.wallet;
const program = pg.program;
const counterSeed = Buffer.from("counter");const [counterPubkey, bump] = web3.PublicKey.findProgramAddressSync([counterSeed],pg.PROGRAM_ID
);let counterAccount;
try {counterAccount = await program.account.counter.fetch(counterPubkey);console.log("Counter account already exists:", counterAccount);
} catch (error) {if (error.message.includes("Account does not exist")) {console.log("Counter account does not exist, proceeding with initialization...");// Initialize accountconst initializeTx = await pg.program.methods.initialize().accounts({counter: counterPubkey,authority: pg.wallet.publicKey,systemProgram: web3.SystemProgram.programId,}).rpc();} else {console.error("Error fetching counter account:", error);}
}const incrementTx = await pg.program.methods.increment().accounts({counter: counterPubkey,authority: pg.wallet.publicKey,}).rpc();counterAccount = await program.account.counter.fetch(counterPubkey);
console.log("account after increasing ==>", Number(counterAccount.count));

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/145170.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

扣子智能体实战:一键生成公众号图文,AI时代文盲也能写公众号,赚钱秘籍

文章目录 一&#xff0c;需求简述二&#xff0c;智能体制作1&#xff0c;智能体人设和技能2&#xff0c;流程开发2.1 设置开始节点2.2 增加一个生成标题的大模型节点2.3 增加一个代码节点 2.4 增加一个插件节点用以生成文章配图2.4 增加一个大模型节点-根据标题和思路生成文章大…

树与图的深度优先遍历(dfs的图论中的应用)

模板题 846. 树的重心 给定一颗树&#xff0c;树中包含 nn 个结点&#xff08;编号 1∼n&#xff09;和 n−1条无向边。 请你找到树的重心&#xff0c;并输出将重心删除后&#xff0c;剩余各个连通块中点数的最大值。 重心定义&#xff1a;重心是指树中的一个结点&#xff…

EmptyDir-数据存储

1.EmptyDir EmptyDir是最基础的Volume类型&#xff0c;一个EmptyDir就是Host上的一个空目录。 EmptyDir是在Pod被分配到Node时创建的&#xff0c;它的初始内容为空&#xff0c;并且无须指定宿主机上对应的目录文件&#xff0c;因为kubernetes会自动分配一个目录&#xff0c;当…

无监督神经组合优化的扩散模型框架

文章目录 Abstract1. Introduction2. Problem Description2.1 无监督神经组合优化3. Neural Probabilistic Optimization Objective for Approximate Likelihood Models3.1 具有联合变分上界的训练扩散模型Abstract 从离散集合的不可处理分布中进行采样,而不依赖相应的训练数据…

基于Springboot的助学金管理系统设计与实现

文未可获取一份本项目的java源码和数据库参考。 一、研究背景 利用计算机来实现助学金管理系统&#xff0c;已经成为一种趋势&#xff0c;相比传统的手工管理方式&#xff0c;利用软件进行助学金管理系统&#xff0c;有着执行快&#xff0c;可行性高、容量存储大&#xff0c;…

15.多线程概述一(下篇)

目录 1.进程与线程 2.实现多线程方式一&#xff1a;继承Thread类【应用】 3.实现多线程方式二&#xff1a;实现Runnable接口【应用】 4.实现多线程方式三&#xff1a;实现Callable接口【应用】 5.三种实现方式的对比与套路 6.设置和获取线程名称/线程对象【应用】 7.线程优先级…

devops的道法术器

devops的道法术器 道、法、术、器自上而下是系统思考的层次&#xff0c;自下而上是解决问题的层次 “道”是目标、价值观&#xff0c;对价值的定位。 快速交付价值&#xff0c;灵活响应变化&#xff0c;这是从价值层面的追求&#xff0c;或者是从第一性原理的角度来讲&#xf…

赋能企业沟通:2024年专业IM即时通讯软件的重要性不可小觑!

随着数字经济的快速发展&#xff0c;企业的沟通与协作方式正以前所未有的速度发生着变化。特别是在经历了全球疫情之后&#xff0c;远程工作和灵活办公成为了常态&#xff0c;而即使在疫情结束后&#xff0c;这种趋势也没有消退。企业对于高效、便捷、实时的沟通需求日益增长&a…

13_Python的高阶函数

高阶函数 高阶函数是Python编程中一个非常强大和有用的特性&#xff0c;它们允许程序员编写更简洁、更抽象的代码。 Python中的高阶函数是那些至少满足以下一个条件的函数&#xff1a; 接受一个或多个函数作为输入&#xff08;也就是说&#xff0c;它的参数之一是函数&#…

EI-BISYNCH协议,欧陆2000系列设备读取数据

EI-Bisynch是一种基于ANSI X3.28-2.5 A4标准的专有协议&#xff0c;用于消息框架。尽管其名称中包含“Bisynch”&#xff0c;但它实际上是一种基于ASCII的异步协议。数据通过7位数据位、偶校验和1个停止位进行传输。 4.1 术语解释 4.1.1 地址 每个仪器都有一个可配置的地址&…

大模型推理性能优化

LLM 推理的核心指标 首 Token 延迟(决定了用户体验) 延迟:从输入到输出最后一个 token 的延迟 吞吐量:每秒针对所有请求生成的 token 数(针对所有并发请求) 推理的性能卡点 1. KV-Cache 大小导致并发能力受限 LLM推理的过程是一个自回归的过程,前 i 次的token会作为…

Linux StableDiffusion下载外网插件失败, 自己下载安装

(sd) zhouyueubun:/data/sd-webui-aki-v4.9$ python webui.py 先看看使用插件时报的错 看截图就知道是SmilingWolf/wd-v1-4-vit-tagger-v2包不存在 先加载本地包&#xff0c;由于本地包没有&#xff0c;自动下载外网的包&#xff0c;需要科学上网访问外网网站哈。 https://h…

【千帆AppBuilder】零代码+组件+代码节点方式实现AI应用《法定退休年龄计算器》

欢迎来到《小5讲堂》 这是《千帆》系列文章&#xff0c;每篇文章将以博主理解的角度展开讲解。 温馨提示&#xff1a;博主能力有限&#xff0c;理解水平有限&#xff0c;若有不对之处望指正&#xff01; 目录 背景创建应用基本信息角色指令引导信息 组件整体界面开始节点代码节…

tomcat服务搭建部署ujcms网站

tomcat服务搭建部署ujcms网站 关闭selinux和防火墙 setenforce 0 && systemctl stop firewalld安装java环境 #卸载原有java8环境 yum remove java*#上传java软件包&#xff0c;并解压缩 tar -xf openjdk-11.0.1_linux-x64_bin.tar.gz && mv jdk-11.0.1 jdk11…

绝缘子缺陷检测数据集

绝缘子缺陷检测数据集&#xff0c;2800张高清照片&#xff0c;已打好标签txt格式&#xff0c;可直接进行目标检测。7类标签&#xff1a;玻璃绝缘子&#xff0c;玻璃片脏污&#xff0c;玻璃片缺损&#xff0c;聚合物片脏污&#xff0c;聚合物片缺损&#xff0c;聚合物绝缘子&…

机器学习笔记(一)初识机器学习

1.定义 机器学习是一门多学科交叉专业&#xff0c;涵盖概率论知识&#xff0c;统计学知识&#xff0c;近似理论知识和复杂算法知识&#xff0c;使用计算机作为工具并致力于真实实时的模拟人类学习方式&#xff0c;并将现有内容进行知识结构划分来有效提高学习效率。 机器学习有…

JavaSE--零基础的开始笔记01:下载JDK以及Path环境变量的 配置

一.Java概述(觉得没必要的可以直接跳过)&#xff1a; Java是sun公司1995年推出&#xff0c;2009年被oracle收购又称为“甲骨文公司”。java之父&#xff1a;詹姆斯.高斯林 java是一门高级语言&#xff0c;接近人类语言程序易懂 。流行度很高&#xff0c;商业占用率高&#xf…

Java知识点小结3:内存回收

文章目录 对象引用强引用软引用&#xff08;SoftReference&#xff09;弱引用&#xff08;WeakReference&#xff09;考一考 虚引用&#xff08;PhantomReference&#xff09;总结 垃圾回收新生代老年代永生代 内存管理小技巧尽量使用直接量使用StringBuilder和StringBuffer进行…

【我的 PWN 学习手札】Tcache dup

前言 Tcache dup&#xff0c;实际上是 tcache 的 double free&#xff0c;能达到 UAF 的效果&#xff0c;实现 Tcache poisoning。 一、Tcache dup 早期 tcache 没有检查 double free&#xff0c;也没有对 counts 做检查。 对同一个大小落在 Tcachebin 的 chunk 进行 doubl…

鸿蒙媒体开发系列07——AVRecorder音频录制

如果你也对鸿蒙开发感兴趣&#xff0c;加入“Harmony自习室”吧&#xff01;扫描下方名片&#xff0c;关注公众号&#xff0c;公众号更新更快&#xff0c;同时也有更多学习资料和技术讨论群。 1、概述 在HarmonyOS系统中&#xff0c;多种API都提供了音频录制开发的支持&#x…