Browse Source

:bug: Fixed case where the same component is used more than once in the system-input

Danilo Guanabara 2 months ago
parent
commit
9ac0e855a7

+ 0 - 1
Cargo.toml

@@ -8,7 +8,6 @@ members = [
     "crates/programs/world",
     "crates/programs/world",
     "crates/types",
     "crates/types",
     "examples/*",
     "examples/*",
-    "examples/system-with-many-components",
 ]
 ]
 
 
 [workspace.package]
 [workspace.package]

+ 2 - 1
clients/typescript/test/framework.ts

@@ -80,7 +80,7 @@ export class Framework {
     var worldReport: number = 0;
     var worldReport: number = 0;
     for (let index in log) {
     for (let index in log) {
       let line = log[index];
       let line = log[index];
-      if (line.includes(" consumed ")) {        
+      if (line.includes(" consumed ")) {
         if (!line.includes("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n")) {
         if (!line.includes("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n")) {
           cpi.push(this.consume(line));
           cpi.push(this.consume(line));
         } else {
         } else {
@@ -93,4 +93,5 @@ export class Framework {
     console.log(`Total CPI Consumed: ${total}`);
     console.log(`Total CPI Consumed: ${total}`);
     console.log(`Number of Instructions: ${numberOfInstructions}`);
     console.log(`Number of Instructions: ${numberOfInstructions}`);
     console.log(`World Report: ${worldReport}`);
     console.log(`World Report: ${worldReport}`);
+  }
 }
 }

+ 6 - 6
clients/typescript/test/intermediate-level/ecs.ts

@@ -307,9 +307,9 @@ export function ecs(framework: Framework) {
       let transactionResponse: any;
       let transactionResponse: any;
       do {
       do {
         transactionResponse =
         transactionResponse =
-        await framework.provider.connection.getTransaction(signature, {
-          commitment: "confirmed",
-        });
+          await framework.provider.connection.getTransaction(signature, {
+            commitment: "confirmed",
+          });
       } while (transactionResponse?.meta?.logMessages === undefined);
       } while (transactionResponse?.meta?.logMessages === undefined);
       framework.report(transactionResponse?.meta?.logMessages);
       framework.report(transactionResponse?.meta?.logMessages);
     });
     });
@@ -331,9 +331,9 @@ export function ecs(framework: Framework) {
       let transactionResponse: any;
       let transactionResponse: any;
       do {
       do {
         transactionResponse =
         transactionResponse =
-        await framework.provider.connection.getTransaction(signature, {
-          commitment: "confirmed",
-        });
+          await framework.provider.connection.getTransaction(signature, {
+            commitment: "confirmed",
+          });
       } while (transactionResponse?.meta?.logMessages === undefined);
       } while (transactionResponse?.meta?.logMessages === undefined);
       framework.report(transactionResponse?.meta?.logMessages);
       framework.report(transactionResponse?.meta?.logMessages);
     });
     });

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

@@ -1,3 +1,5 @@
+use std::collections::HashSet;
+
 use proc_macro::TokenStream;
 use proc_macro::TokenStream;
 
 
 use quote::quote;
 use quote::quote;
@@ -61,8 +63,9 @@ pub fn system_input(_attr: TokenStream, item: TokenStream) -> TokenStream {
         })
         })
         .collect();
         .collect();
 
 
-    let bolt_accounts = fields.iter().map(|f| {
-        let field_type = &f.ty;
+    let unique_fields = fields.iter().map(|f| f.ty.clone()).collect::<HashSet<_>>();
+    let bolt_accounts = unique_fields.iter().map(|f| {
+        let field_type = &f;
         quote! {
         quote! {
             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) }>;
             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) }>;
         }
         }

+ 41 - 37
crates/programs/world/src/lib.rs

@@ -494,27 +494,29 @@ fn apply_impl<'info>(
             data.copy_from_slice(component.try_borrow_data()?.as_ref());
             data.copy_from_slice(component.try_borrow_data()?.as_ref());
         }
         }
 
 
-        bolt_component::cpi::set_owner(
-            CpiContext::new_with_signer(
-                program.to_account_info(),
-                bolt_component::cpi::accounts::SetOwner {
+        if component.owner != bolt_system.key {
+            bolt_component::cpi::set_owner(
+                CpiContext::new_with_signer(
+                    program.to_account_info(),
+                    bolt_component::cpi::accounts::SetOwner {
+                        cpi_auth: cpi_auth.to_account_info(),
+                        component: component.to_account_info(),
+                    },
+                    &[World::cpi_auth_seeds().as_slice()],
+                ),
+                *bolt_system.key,
+            )?;
+
+            bolt_system::cpi::set_data(CpiContext::new_with_signer(
+                bolt_system.to_account_info(),
+                bolt_system::cpi::accounts::SetData {
                     cpi_auth: cpi_auth.to_account_info(),
                     cpi_auth: cpi_auth.to_account_info(),
+                    buffer: buffer.to_account_info(),
                     component: component.to_account_info(),
                     component: component.to_account_info(),
                 },
                 },
                 &[World::cpi_auth_seeds().as_slice()],
                 &[World::cpi_auth_seeds().as_slice()],
-            ),
-            *bolt_system.key,
-        )?;
-
-        bolt_system::cpi::set_data(CpiContext::new_with_signer(
-            bolt_system.to_account_info(),
-            bolt_system::cpi::accounts::SetData {
-                cpi_auth: cpi_auth.to_account_info(),
-                buffer: buffer.to_account_info(),
-                component: component.to_account_info(),
-            },
-            &[World::cpi_auth_seeds().as_slice()],
-        ))?;
+            ))?;
+        }
     }
     }
 
 
     bolt_system::cpi::bolt_execute(
     bolt_system::cpi::bolt_execute(
@@ -529,31 +531,33 @@ fn apply_impl<'info>(
             data.copy_from_slice(component.try_borrow_data()?.as_ref());
             data.copy_from_slice(component.try_borrow_data()?.as_ref());
         }
         }
 
 
-        bolt_system::cpi::set_owner(
-            CpiContext::new_with_signer(
-                bolt_system.to_account_info(),
-                bolt_system::cpi::accounts::SetOwner {
+        if *component.owner != program.key() {
+            bolt_system::cpi::set_owner(
+                CpiContext::new_with_signer(
+                    bolt_system.to_account_info(),
+                    bolt_system::cpi::accounts::SetOwner {
+                        cpi_auth: cpi_auth.to_account_info(),
+                        component: component.to_account_info(),
+                    },
+                    &[World::cpi_auth_seeds().as_slice()],
+                ),
+                program.key(),
+            )?;
+
+            if *component.owner != program.key() {
+                return Err(WorldError::InvalidComponentOwner.into());
+            }
+
+            bolt_component::cpi::set_data(CpiContext::new_with_signer(
+                program.to_account_info(),
+                bolt_component::cpi::accounts::SetData {
                     cpi_auth: cpi_auth.to_account_info(),
                     cpi_auth: cpi_auth.to_account_info(),
+                    buffer: buffer.to_account_info(),
                     component: component.to_account_info(),
                     component: component.to_account_info(),
                 },
                 },
                 &[World::cpi_auth_seeds().as_slice()],
                 &[World::cpi_auth_seeds().as_slice()],
-            ),
-            program.key(),
-        )?;
-
-        if *component.owner != program.key() {
-            return Err(WorldError::InvalidComponentOwner.into());
+            ))?;
         }
         }
-
-        bolt_component::cpi::set_data(CpiContext::new_with_signer(
-            program.to_account_info(),
-            bolt_component::cpi::accounts::SetData {
-                cpi_auth: cpi_auth.to_account_info(),
-                buffer: buffer.to_account_info(),
-                component: component.to_account_info(),
-            },
-            &[World::cpi_auth_seeds().as_slice()],
-        ))?;
     }
     }
 
 
     buffer.realloc(0, false)?;
     buffer.realloc(0, false)?;

+ 2 - 3
examples/system-with-few-components/src/lib.rs

@@ -7,8 +7,8 @@ declare_id!("A3kNNSgmkTNA5V1qtnrbtNeqKrYHNxUMCTkqTDaQzE97");
 #[system]
 #[system]
 pub mod system_with_few_components {
 pub mod system_with_few_components {
 
 
-    pub fn execute(ctx: Context<Components>, _args_p: Vec<u8>) -> Result<Components> {
-        Ok(ctx.accounts)
+    pub fn execute(_ctx: Context<Components>, _args_p: Vec<u8>) -> Result<()> {
+        Ok(())
     }
     }
 
 
     #[system_input]
     #[system_input]
@@ -19,5 +19,4 @@ pub mod system_with_few_components {
         pub large4: Large,
         pub large4: Large,
         pub large5: Large,
         pub large5: Large,
     }
     }
-
 }
 }

+ 2 - 2
examples/system-with-many-components/src/lib.rs

@@ -6,8 +6,8 @@ declare_id!("Hi4sMEb3uXhWCiLyrF7t3Z384an7YZsTj774cabAAPQB");
 #[system]
 #[system]
 pub mod system_with_many_components {
 pub mod system_with_many_components {
 
 
-    pub fn execute(ctx: Context<Components>, _args_p: Vec<u8>) -> Result<Components> {
-        Ok(ctx.accounts)
+    pub fn execute(_ctx: Context<Components>, _args_p: Vec<u8>) -> Result<()> {
+        Ok(())
     }
     }
 
 
     #[system_input]
     #[system_input]