cli: Make conflicting account names a compile-time error (#2621)

This commit is contained in:
acheron 2023-09-05 17:30:53 +02:00 committed by GitHub
parent b9fa898384
commit a1e4453a0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 121 additions and 0 deletions

View File

@ -2501,6 +2501,22 @@ fn generate_idl_build(no_docs: bool) -> Result<Vec<Idl>> {
})
.collect::<Vec<_>>();
// Verify IDLs are valid
for idl in &idls {
let full_path_account = idl
.accounts
.iter()
.find(|account| account.name.contains("::"));
if let Some(account) = full_path_account {
return Err(anyhow!(
"Conflicting accounts names are not allowed.\nProgram: {}\nAccount: {}",
idl.name,
account.name
));
}
}
Ok(idls)
}

View File

@ -7,6 +7,7 @@ docs = "Docs111111111111111111111111111111111111111"
external = "Externa1111111111111111111111111111111111111"
generics = "Generics111111111111111111111111111111111111"
idl = "id11111111111111111111111111111111111111111"
idl_build_features = "id1Bui1dFeatures111111111111111111111111111"
relations_derivation = "Re1ationsDerivation111111111111111111111111"
non_existent = { address = "NonExistent11111111111111111111111111111111", idl = "non-existent.json" }
numbers_123 = { address = "Numbers111111111111111111111111111111111111", idl = "idls/relations_build.json" }

View File

@ -0,0 +1,20 @@
[package]
name = "idl-build-features"
version = "0.1.0"
description = "Created with Anchor"
rust-version = "1.60"
edition = "2021"
[lib]
crate-type = ["cdylib", "lib"]
name = "idl_build_features"
[features]
no-entrypoint = []
no-idl = []
cpi = ["no-entrypoint"]
idl-build = ["anchor-lang/idl-build"]
default = []
[dependencies]
anchor-lang = { path = "../../../../lang" }

View File

@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -0,0 +1,47 @@
use anchor_lang::prelude::*;
declare_id!("id1Bui1dFeatures111111111111111111111111111");
#[program]
pub mod idl_build_features {
use super::*;
pub fn full_path(
ctx: Context<FullPath>,
my_struct: MyStruct,
some_module_my_struct: some_module::MyStruct,
) -> Result<()> {
ctx.accounts.account.my_struct = my_struct;
ctx.accounts.account.some_module_my_struct = some_module_my_struct;
Ok(())
}
}
#[derive(Accounts)]
pub struct FullPath<'info> {
#[account(zero)]
pub account: Account<'info, FullPathAccount>,
}
#[account]
pub struct FullPathAccount {
pub my_struct: MyStruct,
pub some_module_my_struct: some_module::MyStruct,
}
mod some_module {
use super::*;
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct MyStruct {
pub data: u8,
}
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct MyStruct {
pub u8: u8,
pub u16: u16,
pub u32: u32,
pub u64: u64,
}

View File

@ -0,0 +1,35 @@
import * as anchor from "@coral-xyz/anchor";
import { assert } from "chai";
import { IdlBuildFeatures } from "../target/types/idl_build_features";
describe("idl-build features", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace
.idlBuildFeatures as anchor.Program<IdlBuildFeatures>;
it("Can use full module path types", async () => {
const kp = anchor.web3.Keypair.generate();
const outerMyStructArg = { u8: 1, u16: 2, u32: 3, u64: new anchor.BN(4) };
const someModuleMyStructArg = { data: 5 };
await program.methods
.fullPath(outerMyStructArg, someModuleMyStructArg)
.accounts({ account: kp.publicKey })
.preInstructions([
await program.account.fullPathAccount.createInstruction(kp),
])
.signers([kp])
.rpc();
const fullPathAccount = await program.account.fullPathAccount.fetch(
kp.publicKey
);
assert.strictEqual(fullPathAccount.myStruct.u8, outerMyStructArg.u8);
assert.strictEqual(fullPathAccount.myStruct.u16, outerMyStructArg.u16);
assert.strictEqual(fullPathAccount.myStruct.u32, outerMyStructArg.u32);
assert(fullPathAccount.myStruct.u64.eq(outerMyStructArg.u64));
assert.deepEqual(fullPathAccount.someModuleMyStruct, someModuleMyStructArg);
});
});