Skip to content

Commit

Permalink
add relations derivation to idl generation through compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
kklas committed Mar 10, 2023
1 parent 9c9035c commit 9c7fb82
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,9 @@ fn idl_parse(
fn idl_build(no_docs: bool) -> Result<()> {
let no_docs = if no_docs { "TRUE" } else { "FALSE" };

let cfg = Config::discover(&ConfigOverride::default())?.expect("Not in workspace.");
let seeds_feature = if cfg.features.seeds { "TRUE" } else { "FALSE" };

let exit = std::process::Command::new("cargo")
.args([
"test",
Expand All @@ -2036,6 +2039,7 @@ fn idl_build(no_docs: bool) -> Result<()> {
"--quiet",
])
.env("ANCHOR_IDL_GEN_NO_DOCS", no_docs)
.env("ANCHOR_IDL_GEN_SEEDS_FEATURE", seeds_feature)
.stderr(Stdio::inherit())
.output()
.map_err(|e| anyhow::format_err!("{}", e.to_string()))?;
Expand Down
11 changes: 9 additions & 2 deletions lang/syn/src/idl/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ pub fn get_no_docs() -> bool {
.unwrap_or(false)
}

#[inline(always)]
pub fn get_seeds_feature() -> bool {
std::option_env!("ANCHOR_IDL_GEN_SEEDS_FEATURE")
.map(|val| val == "TRUE")
.unwrap_or(false)
}

// Returns TokenStream for IdlType enum and the syn::TypePath for the defined
// type if any.
// Returns Err when the type wasn't parsed successfully.
Expand Down Expand Up @@ -643,7 +650,7 @@ pub fn gen_idl_gen_impl_for_accounts_strct(
_ => quote! {None},
};
let pda = quote!{None}; // TODO
let relations = quote!{Vec::new()}; // TODO
let relations = super::parse::relations::parse(acc, get_seeds_feature());

let acc_type_path = match &acc.ty {
crate::Ty::Account(ty) => Some(&ty.account_type_path),
Expand All @@ -659,7 +666,7 @@ pub fn gen_idl_gen_impl_for_accounts_strct(
is_optional: #is_optional,
docs: #docs,
pda: #pda,
relations: #relations,
relations: vec![#(#relations.into()),*],
})
}, acc_type_path)
}
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/idl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "idl-gen")]
pub mod gen;
#[cfg(feature = "idl-parse")]
#[cfg(any(feature = "idl-parse", feature = "idl-gen"))]
pub mod parse;
#[cfg(any(feature = "idl-types", feature = "idl-gen", feature = "idl-parse"))]
pub mod types;
3 changes: 2 additions & 1 deletion tests/idl-generation/Anchor.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[features]
seeds = false
seeds = true

[programs.localnet]
idl = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
idl_2 = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
Expand Down
5 changes: 4 additions & 1 deletion tests/idl-generation/gen_testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ anchor idl parse --file src/lib.rs > ../../tests/testdata/idl_parse_exp.json
anchor idl build > ../../tests/testdata/idl_gen_exp.json

cd ../generics
anchor idl build > ../../tests/testdata/generics_gen_exp.json
anchor idl build > ../../tests/testdata/generics_gen_exp.json

cd ../relations-derivation
anchor idl build > ../../tests/testdata/relations_gen_exp.json
22 changes: 22 additions & 0 deletions tests/idl-generation/programs/relations-derivation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "relations-derivation"
version = "0.1.0"
description = "Created with Anchor"
rust-version = "1.60"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "relations_derivation"

[features]
no-entrypoint = []
no-idl = []
cpi = ["no-entrypoint"]
idl-gen = [
"anchor-lang/idl-gen",
]
default = []

[dependencies]
anchor-lang = { path = "../../../../lang" }
2 changes: 2 additions & 0 deletions tests/idl-generation/programs/relations-derivation/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
68 changes: 68 additions & 0 deletions tests/idl-generation/programs/relations-derivation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! The typescript example serves to show how one would setup an Anchor
//! workspace with TypeScript tests and migrations.

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

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

pub fn init_base(ctx: Context<InitBase>) -> Result<()> {
ctx.accounts.account.my_account = ctx.accounts.my_account.key();
ctx.accounts.account.bump = ctx.bumps["account"];
Ok(())
}
pub fn test_relation(_ctx: Context<TestRelation>) -> Result<()> {
Ok(())
}
}

#[derive(Accounts)]
pub struct InitBase<'info> {
/// CHECK: yeah I know
#[account(mut)]
my_account: Signer<'info>,
#[account(
init,
payer = my_account,
seeds = [b"seed"],
space = 100,
bump,
)]
account: Account<'info, MyAccount>,
system_program: Program<'info, System>
}

#[derive(Accounts)]
pub struct Nested<'info> {
/// CHECK: yeah I know
my_account: UncheckedAccount<'info>,
#[account(
has_one = my_account,
seeds = [b"seed"],
bump = account.bump
)]
account: Account<'info, MyAccount>,
}

#[derive(Accounts)]
pub struct TestRelation<'info> {
/// CHECK: yeah I know
my_account: UncheckedAccount<'info>,
#[account(
has_one = my_account,
seeds = [b"seed"],
bump = account.bump
)]
account: Account<'info, MyAccount>,
nested: Nested<'info>,
}


#[account]
pub struct MyAccount {
pub my_account: Pubkey,
pub bump: u8
}
16 changes: 14 additions & 2 deletions tests/idl-generation/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ anchor idl build > $TMPDIR/idl_gen_act.json
cd ../generics
anchor idl build > $TMPDIR/generics_gen_act.json

cd ../relations-derivation
anchor idl build > $TMPDIR/relations_gen_act.json

cd ../..
echo "----------------------------------------------------"
echo "idl parse before > after"
Expand All @@ -37,5 +40,14 @@ echo ""
diff -y --color tests/testdata/generics_gen_exp.json $TMPDIR/generics_gen_act.json
GEN_GENERICS_RETCODE=$?

# returns 0 when ok, 1, 2, or 3 when outputs differ
exit $((PARSE_RETCODE+GEN_RETCODE+GEN_GENERICS_RETCODE))
echo ""
echo ""
echo "----------------------------------------------------"
echo "idl relations build before > after"
echo "----------------------------------------------------"
echo ""
diff -y --color tests/testdata/relations_gen_exp.json $TMPDIR/relations_gen_act.json
GEN_RELATIONS_RETCODE=$?

# returns 0 when ok, or a positive integer when there are differences
exit $((PARSE_RETCODE+GEN_RETCODE+GEN_GENERICS_RETCODE+GEN_RELATIONS_RETCODE))
83 changes: 83 additions & 0 deletions tests/idl-generation/tests/testdata/relations_gen_exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"version": "0.1.0",
"name": "relations_derivation",
"instructions": [
{
"name": "initBase",
"accounts": [
{
"name": "myAccount",
"isMut": true,
"isSigner": true
},
{
"name": "account",
"isMut": true,
"isSigner": false
},
{
"name": "systemProgram",
"isMut": false,
"isSigner": false
}
],
"args": []
},
{
"name": "testRelation",
"accounts": [
{
"name": "myAccount",
"isMut": false,
"isSigner": false
},
{
"name": "account",
"isMut": false,
"isSigner": false,
"relations": [
"my_account"
]
},
{
"name": "nested",
"accounts": [
{
"name": "myAccount",
"isMut": false,
"isSigner": false
},
{
"name": "account",
"isMut": false,
"isSigner": false,
"relations": [
"my_account"
]
}
]
}
],
"args": []
}
],
"accounts": [
{
"name": "MyAccount",
"full_path": "relations_derivation::MyAccount",
"type": {
"kind": "struct",
"fields": [
{
"name": "myAccount",
"type": "publicKey"
},
{
"name": "bump",
"type": "u8"
}
]
}
}
]
}

0 comments on commit 9c7fb82

Please sign in to comment.