Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: add sysvar custom error and failing test case #1535

Merged
merged 3 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ incremented for features.

## [Unreleased]

### Features

* lang: Add new `AccountSysvarMismatch` error code and test cases for sysvars ([#1535](https://github.com/project-serum/anchor/pull/1535)).

## [0.22.1] - 2022-02-28

### Fixes
Expand Down
11 changes: 7 additions & 4 deletions lang/src/accounts/sysvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ impl<'info, T: solana_program::sysvar::Sysvar + fmt::Debug> fmt::Debug for Sysva

impl<'info, T: solana_program::sysvar::Sysvar> Sysvar<'info, T> {
pub fn from_account_info(acc_info: &AccountInfo<'info>) -> Result<Sysvar<'info, T>> {
Ok(Sysvar {
info: acc_info.clone(),
account: T::from_account_info(acc_info)?,
})
match T::from_account_info(acc_info) {
Ok(val) => Ok(Sysvar {
info: acc_info.clone(),
account: val,
}),
Err(_) => Err(ErrorCode::AccountSysvarMismatch.into()),
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ pub enum ErrorCode {
/// 3014 - The given account is not the associated token account
#[msg("The given account is not the associated token account")]
AccountNotAssociatedTokenAccount,
/// 3015 - The given public key does not match the required sysvar
#[msg("The given public key does not match the required sysvar")]
AccountSysvarMismatch,

// State.
/// 4000 - The given state account does not have the correct address
Expand Down
21 changes: 20 additions & 1 deletion tests/sysvars/tests/sysvars.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const anchor = require("@project-serum/anchor");
const assert = require("assert");

describe("sysvars", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.Provider.local());
const program = anchor.workspace.Sysvars;

it("Is initialized!", async () => {
const program = anchor.workspace.Sysvars;
const tx = await program.rpc.sysvars({
accounts: {
clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
Expand All @@ -15,4 +16,22 @@ describe("sysvars", () => {
});
console.log("Your transaction signature", tx);
});

it('Fails when the wrote pubkeys are provided', async () => {
try {
await program.rpc.sysvars({
callensm marked this conversation as resolved.
Show resolved Hide resolved
accounts: {
clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
stakeHistory: anchor.web3.SYSVAR_REWARDS_PUBKEY,
},
})
assert.ok(false)
} catch (err) {
const errMsg = "The given public key does not match the required sysvar";
assert.strictEqual(err.toString(), errMsg);
assert.strictEqual(err.msg, errMsg);
assert.strictEqual(err.code, 3015);
}
})
});
5 changes: 5 additions & 0 deletions ts/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const LangErrorCode = {
AccountNotInitialized: 3012,
AccountNotProgramData: 3013,
AccountNotAssociatedTokenAccount: 3014,
AccountSysvarMismatch: 3015,
// State.
StateInvalidAddress: 4000,

Expand Down Expand Up @@ -227,6 +228,10 @@ const LangErrorMessage = new Map([
LangErrorCode.AccountNotAssociatedTokenAccount,
"The given account is not the associated token account",
],
[
LangErrorCode.AccountSysvarMismatch,
"The given public key does not match the required sysvar",
],

// State.
[
Expand Down