Browse Source

:sparkles: Component Authority owned by the world program

Gabriele Picco 1 year ago
parent
commit
48fed658b5

+ 6 - 11
crates/bolt-lang/attribute/bolt-program/src/lib.rs

@@ -110,15 +110,10 @@ fn generate_initialize(component_type: &Type) -> (TokenStream2, TokenStream2) {
     (
         quote! {
             #[automatically_derived]
-            pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
-                ctx.accounts.data.set_inner(<#component_type>::default());
-                if let Some(authority) = &ctx.accounts.authority {
-                    if authority.key != ctx.accounts.payer.key {
-                        panic!("The authority does not match the payer.");
-                    }
-                    ctx.accounts.data.bolt_metadata.authority = *authority.key;
-                }
-                Ok(())
+            pub fn initialize(ctx: Context<Initialize>) -> Result<Vec<u8>> {
+                let mut serialized_data = Vec::new();
+                <#component_type>::default().serialize(&mut serialized_data).expect("Failed to serialize");
+                Ok(serialized_data)
             }
         },
         quote! {
@@ -127,8 +122,8 @@ fn generate_initialize(component_type: &Type) -> (TokenStream2, TokenStream2) {
             pub struct Initialize<'info>  {
                 #[account(mut)]
                 pub payer: Signer<'info>,
-                #[account(init_if_needed, payer = payer, space = <#component_type>::size(), seeds = [<#component_type>::seed(), entity.key().as_ref()], bump)]
-                pub data: Account<'info, #component_type>,
+                #[account(init_if_needed, owner=World::id(), payer = payer, space = <#component_type>::size(), seeds = [<#component_type>::seed(), entity.key().as_ref()], bump)]
+                pub data: AccountInfo<'info>,
                 #[account()]
                 pub entity: Account<'info, Entity>,
                 #[account()]

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

@@ -16,8 +16,6 @@ struct SystemTransform;
 /// ```ignore
 /// #[system]
 /// pub mod system_fly {
-///     use super::*;
-///
 ///     pub fn execute(ctx: Context<Component>, _args: Vec<u8>) -> Result<Position> {
 ///         let pos = Position {
 ///             x: ctx.accounts.position.x,

+ 1 - 0
crates/bolt-lang/src/lib.rs

@@ -8,6 +8,7 @@ pub use bolt_attribute_bolt_system::system;
 pub use bolt_system;
 pub use world;
 pub use world::Entity;
+pub use world::program::World;
 
 pub use serde;
 pub use serde::{Deserialize as BoltDeserialize, Serialize as BoltSerialize};

+ 1 - 1
examples/component-position/src/lib.rs

@@ -7,5 +7,5 @@ declare_id!("Fn1JzzEdyb55fsyduWS94mYHizGhJZuhvjX6DVvrmGbQ");
 pub struct Position {
     pub x: i64,
     pub y: i64,
-    pub z: i64,
+    pub z: i64
 }

+ 0 - 2
examples/system-apply-velocity/src/lib.rs

@@ -20,8 +20,6 @@ pub mod system_apply_velocity {
 
 #[derive(Accounts)]
 pub struct Component<'info> {
-    #[account()]
     pub velocity: Account<'info, Velocity>,
-    #[account()]
     pub position: Account<'info, Position>,
 }

+ 2 - 2
examples/system-simple-movement/src/lib.rs

@@ -4,9 +4,10 @@ declare_id!("FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA");
 
 #[system]
 pub mod system_simple_movement {
+
     pub fn execute(ctx: Context<Component>, args_p: Vec<u8>) -> Result<Position> {
         let args = parse_args::<Args>(&args_p);
-
+        
         let mut position = Position::from_account_info(&ctx.accounts.position)?;
 
         // Compute the new position based on the direction
@@ -26,7 +27,6 @@ pub mod system_simple_movement {
 // Define the Account to parse from the component
 #[derive(Accounts)]
 pub struct Component<'info> {
-    /// CHECK: check that the component is the expected account
     pub position: AccountInfo<'info>,
 }
 

+ 10 - 11
programs/bolt-component/src/lib.rs

@@ -1,4 +1,5 @@
 use anchor_lang::prelude::*;
+use std::str::FromStr;
 
 declare_id!("CmP2djJgABZ4cRokm4ndxuq6LerqpNHLBsaUv2XKEJua");
 
@@ -6,15 +7,13 @@ declare_id!("CmP2djJgABZ4cRokm4ndxuq6LerqpNHLBsaUv2XKEJua");
 pub mod bolt_component {
     use super::*;
 
-    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
-        // ctx.accounts.data.to_account_info().assign(::ID); TODO: Test delegate to the world program
-        if let Some(authority) = &ctx.accounts.authority {
-            if authority.key != ctx.accounts.payer.key {
-                panic!("Authority mismatch");
-            }
-            ctx.accounts.data.bolt_metadata.authority = *authority.key;
-        }
-        Ok(())
+    pub fn initialize(_ctx: Context<Initialize>) -> Result<Vec<u8>> {
+        let mut component = Component::default();
+        component.bolt_metadata.authority = Pubkey::from_str("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n").unwrap();
+        let mut serialized_data = Vec::new();
+        anchor_lang::AccountSerialize::try_serialize(&component, &mut serialized_data).expect("Failed to serialize");
+        //component.serialize(&mut serialized_data).expect("Failed to serialize");
+        Ok(serialized_data)
     }
 
     pub fn apply(_ctx: Context<Apply>, _args: Vec<u8>) -> Result<()> {
@@ -56,8 +55,8 @@ pub mod bolt_component {
 pub struct Initialize<'info> {
     #[account(mut)]
     pub payer: Signer<'info>,
-    #[account(init_if_needed, payer = payer, space = Component::size(), seeds = [Component::seed(), entity.key().as_ref()], bump)]
-    pub data: Account<'info, Component>,
+    #[account(init_if_needed, owner = Pubkey::from_str("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n").unwrap(), payer = payer, space = Component::size(), seeds = [Component::seed(), entity.key().as_ref()], bump)]
+    pub data: AccountInfo<'info>,
     #[account()]
     /// CHECK: A generic entity account
     pub entity: AccountInfo<'info>,

+ 0 - 1
programs/bolt-system/src/lib.rs

@@ -1,6 +1,5 @@
 use anchor_lang::prelude::borsh::{BorshDeserialize, BorshSerialize};
 use anchor_lang::prelude::*;
-//use bolt_helpers_system_template::*;
 
 declare_id!("7X4EFsDJ5aYTcEjKzJ94rD8FRKgQeXC89fkpeTS4KaqP");
 

+ 3 - 1
programs/world/src/lib.rs

@@ -37,7 +37,9 @@ pub mod world {
     }
 
     pub fn initialize_component(ctx: Context<InitializeComponent>) -> Result<()> {
-        bolt_component::cpi::initialize(ctx.accounts.build())?;
+        let data = bolt_component::cpi::initialize(ctx.accounts.build())?;
+        let component_data = &mut *ctx.accounts.data.data.borrow_mut();
+        component_data.copy_from_slice(&data.get());
         Ok(())
     }