Skip to content

Commit b9d9cc8

Browse files
committed
🐛 Fixed case where the same component is used more than once in the system-input
1 parent 8b1cc22 commit b9d9cc8

File tree

3 files changed

+48
-41
lines changed
  • crates
    • bolt-lang/attribute/system-input/src
    • programs/world/src
  • examples/system-with-10-components/src

3 files changed

+48
-41
lines changed

crates/bolt-lang/attribute/system-input/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use proc_macro::TokenStream;
24

35
use quote::quote;
@@ -61,8 +63,9 @@ pub fn system_input(_attr: TokenStream, item: TokenStream) -> TokenStream {
6163
})
6264
.collect();
6365

64-
let bolt_accounts = fields.iter().map(|f| {
65-
let field_type = &f.ty;
66+
let unique_fields = fields.iter().map(|f| f.ty.clone()).collect::<HashSet<_>>();
67+
let bolt_accounts = unique_fields.iter().map(|f| {
68+
let field_type = &f;
6669
quote! {
6770
pub type #field_type = bolt_lang::account::BoltAccount<super::#field_type, { bolt_lang::account::pubkey_p0(crate::ID) }, { bolt_lang::account::pubkey_p1(crate::ID) }>;
6871
}

crates/programs/world/src/lib.rs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -494,27 +494,29 @@ fn apply_impl<'info>(
494494
data.copy_from_slice(component.try_borrow_data()?.as_ref());
495495
}
496496

497-
bolt_component::cpi::set_owner(
498-
CpiContext::new_with_signer(
499-
program.to_account_info(),
500-
bolt_component::cpi::accounts::SetOwner {
497+
if component.owner != bolt_system.key {
498+
bolt_component::cpi::set_owner(
499+
CpiContext::new_with_signer(
500+
program.to_account_info(),
501+
bolt_component::cpi::accounts::SetOwner {
502+
cpi_auth: cpi_auth.to_account_info(),
503+
component: component.to_account_info(),
504+
},
505+
&[World::cpi_auth_seeds().as_slice()],
506+
),
507+
*bolt_system.key,
508+
)?;
509+
510+
bolt_system::cpi::set_data(CpiContext::new_with_signer(
511+
bolt_system.to_account_info(),
512+
bolt_system::cpi::accounts::SetData {
501513
cpi_auth: cpi_auth.to_account_info(),
514+
buffer: buffer.to_account_info(),
502515
component: component.to_account_info(),
503516
},
504517
&[World::cpi_auth_seeds().as_slice()],
505-
),
506-
*bolt_system.key,
507-
)?;
508-
509-
bolt_system::cpi::set_data(CpiContext::new_with_signer(
510-
bolt_system.to_account_info(),
511-
bolt_system::cpi::accounts::SetData {
512-
cpi_auth: cpi_auth.to_account_info(),
513-
buffer: buffer.to_account_info(),
514-
component: component.to_account_info(),
515-
},
516-
&[World::cpi_auth_seeds().as_slice()],
517-
))?;
518+
))?;
519+
}
518520
}
519521

520522
bolt_system::cpi::bolt_execute(
@@ -529,31 +531,33 @@ fn apply_impl<'info>(
529531
data.copy_from_slice(component.try_borrow_data()?.as_ref());
530532
}
531533

532-
bolt_system::cpi::set_owner(
533-
CpiContext::new_with_signer(
534-
bolt_system.to_account_info(),
535-
bolt_system::cpi::accounts::SetOwner {
534+
if *component.owner != program.key() {
535+
bolt_system::cpi::set_owner(
536+
CpiContext::new_with_signer(
537+
bolt_system.to_account_info(),
538+
bolt_system::cpi::accounts::SetOwner {
539+
cpi_auth: cpi_auth.to_account_info(),
540+
component: component.to_account_info(),
541+
},
542+
&[World::cpi_auth_seeds().as_slice()],
543+
),
544+
program.key(),
545+
)?;
546+
547+
if *component.owner != program.key() {
548+
return Err(WorldError::InvalidComponentOwner.into());
549+
}
550+
551+
bolt_component::cpi::set_data(CpiContext::new_with_signer(
552+
program.to_account_info(),
553+
bolt_component::cpi::accounts::SetData {
536554
cpi_auth: cpi_auth.to_account_info(),
555+
buffer: buffer.to_account_info(),
537556
component: component.to_account_info(),
538557
},
539558
&[World::cpi_auth_seeds().as_slice()],
540-
),
541-
program.key(),
542-
)?;
543-
544-
if *component.owner != program.key() {
545-
return Err(WorldError::InvalidComponentOwner.into());
559+
))?;
546560
}
547-
548-
bolt_component::cpi::set_data(CpiContext::new_with_signer(
549-
program.to_account_info(),
550-
bolt_component::cpi::accounts::SetData {
551-
cpi_auth: cpi_auth.to_account_info(),
552-
buffer: buffer.to_account_info(),
553-
component: component.to_account_info(),
554-
},
555-
&[World::cpi_auth_seeds().as_slice()],
556-
))?;
557561
}
558562

559563
buffer.realloc(0, false)?;

examples/system-with-10-components/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ declare_id!("C69UYWaXBQXUbhHQGtG8pB7DHSgh2z5Sm9ifyAnM1kkt");
66
#[system]
77
pub mod with_10_components {
88

9-
pub fn execute(ctx: Context<Components>, _args_p: Vec<u8>) -> Result<Components> {
10-
Ok(ctx.accounts)
9+
pub fn execute(_ctx: Context<Components>, _args_p: Vec<u8>) -> Result<()> {
10+
Ok(())
1111
}
1212

1313
#[system_input]

0 commit comments

Comments
 (0)