Browse Source

:construction: Unifying Buffer & Component delegation instruction

Danilo Guanabara 1 tháng trước cách đây
mục cha
commit
fcd82ad5fb

+ 50 - 30
clients/typescript/src/delegation/delegate.ts

@@ -174,56 +174,76 @@ export async function DelegateComponent({
   delegationProgram?: web3.PublicKey;
   systemProgram?: web3.PublicKey;
 }): Promise<{
-  instructions: TransactionInstruction[];
+  instruction: TransactionInstruction;
   transaction: Transaction;
   componentPda: PublicKey;
 }> {
   const componentPda = FindComponentPda({ componentId, entity, seed });
+  const componentProgram = componentId;
   const componentBuffer = FindBufferPda(componentPda);
-  const delegateComponentIx = createDelegateInstruction({
-    payer,
-    entity,
-    account: componentPda,
-    ownerProgram: componentId,
-    buffer,
-    delegationRecord,
-    delegationMetadata,
-    delegationProgram,
-    systemProgram,
-  });
+  systemProgram = systemProgram ?? web3.SystemProgram.programId;
+  buffer =
+    buffer ??
+    delegateBufferPdaFromDelegatedAccountAndOwnerProgram(
+      componentPda,
+      componentProgram,
+    );
+  delegationRecord =
+    delegationRecord ??
+    delegationRecordPdaFromDelegatedAccount(componentBuffer);
+  delegationMetadata =
+    delegationMetadata ??
+    delegationMetadataPdaFromDelegatedAccount(componentBuffer);
+  delegationProgram = delegationProgram ?? new PublicKey(DELEGATION_PROGRAM_ID);
+  const worldProgram = new PublicKey(worldIdl.address);
 
-  const componentBufferDelegationRecord =
+  const bufferDelegationRecord =
     delegationRecordPdaFromDelegatedAccount(componentBuffer);
 
-  const componentBufferDelegationMetadata =
+  const bufferDelegationMetadata =
     delegationMetadataPdaFromDelegatedAccount(componentBuffer);
 
-  const componentBufferBuffer =
-    delegateBufferPdaFromDelegatedAccountAndOwnerProgram(
-      componentBuffer,
-      new PublicKey(worldIdl.address),
-    );
+  const bufferBuffer = delegateBufferPdaFromDelegatedAccountAndOwnerProgram(
+    componentBuffer,
+    worldProgram,
+  );
+
+  console.log("Payer: " + payer);
+  console.log("Component: " + componentPda);
+  console.log("ComponentBuffer: " + componentBuffer);
+  console.log("Component Program: " + componentProgram);
+  console.log("Buffer: " + buffer);
+  console.log("delegationRecord: " + delegationRecord);
+  console.log("delegationMetadata: " + delegationMetadata);
+  console.log("delegationProgram: " + delegationProgram);
+  console.log("systemProgram: " + systemProgram);
+  console.log("entity: " + entity);
+  console.log("worldProgram: " + worldProgram);
 
   const program = new Program(worldIdl as Idl) as unknown as Program;
-  const delegateBufferComponentIx = await program.methods
-    .delegateBuffer(0, null)
+  const delegateComponentIx = await program.methods
+    .delegateComponent(0, null)
     .accounts({
       payer,
       component: componentPda,
       componentBuffer,
-      ownerProgram: worldIdl.address,
-      buffer: componentBufferBuffer,
-      delegationRecord: componentBufferDelegationRecord,
-      delegationMetadata: componentBufferDelegationMetadata,
-      delegationProgram: DELEGATION_PROGRAM_ID,
+      componentProgram,
+      buffer,
+      delegationRecord,
+      delegationMetadata,
+      delegationProgram,
+      systemProgram,
+      entity,
+      worldProgram,
+      bufferBuffer,
+      bufferDelegationRecord,
+      bufferDelegationMetadata,
     })
     .instruction();
 
   return {
-    instructions: [delegateComponentIx, delegateBufferComponentIx], // TODO: Make it a single instruction again using the World program as a proxy.
-    transaction: new Transaction()
-      .add(delegateComponentIx)
-      .add(delegateBufferComponentIx),
+    instruction: delegateComponentIx,
+    transaction: new Transaction().add(delegateComponentIx),
     componentPda,
   };
 }

+ 30 - 11
clients/typescript/src/generated/idl/world.json

@@ -225,16 +225,16 @@
       "args": []
     },
     {
-      "name": "delegate_buffer",
+      "name": "delegate_component",
       "discriminator": [
-        121,
-        134,
-        252,
-        65,
-        64,
-        83,
-        93,
-        91
+        191,
+        212,
+        179,
+        193,
+        178,
+        94,
+        119,
+        93
       ],
       "accounts": [
         {
@@ -242,14 +242,15 @@
           "signer": true
         },
         {
-          "name": "component"
+          "name": "component",
+          "writable": true
         },
         {
           "name": "component_buffer",
           "writable": true
         },
         {
-          "name": "owner_program"
+          "name": "component_program"
         },
         {
           "name": "buffer",
@@ -269,6 +270,24 @@
         {
           "name": "system_program",
           "address": "11111111111111111111111111111111"
+        },
+        {
+          "name": "entity"
+        },
+        {
+          "name": "world_program"
+        },
+        {
+          "name": "buffer_buffer",
+          "writable": true
+        },
+        {
+          "name": "buffer_delegation_record",
+          "writable": true
+        },
+        {
+          "name": "buffer_delegation_metadata",
+          "writable": true
         }
       ],
       "args": [

+ 22 - 3
clients/typescript/src/generated/types/world.ts

@@ -180,8 +180,8 @@ export type World = {
       args: [];
     },
     {
-      name: "delegateBuffer";
-      discriminator: [121, 134, 252, 65, 64, 83, 93, 91];
+      name: "delegateComponent";
+      discriminator: [191, 212, 179, 193, 178, 94, 119, 93];
       accounts: [
         {
           name: "payer";
@@ -189,13 +189,14 @@ export type World = {
         },
         {
           name: "component";
+          writable: true;
         },
         {
           name: "componentBuffer";
           writable: true;
         },
         {
-          name: "ownerProgram";
+          name: "componentProgram";
         },
         {
           name: "buffer";
@@ -216,6 +217,24 @@ export type World = {
           name: "systemProgram";
           address: "11111111111111111111111111111111";
         },
+        {
+          name: "entity";
+        },
+        {
+          name: "worldProgram";
+        },
+        {
+          name: "bufferBuffer";
+          writable: true;
+        },
+        {
+          name: "bufferDelegationRecord";
+          writable: true;
+        },
+        {
+          name: "bufferDelegationMetadata";
+          writable: true;
+        },
       ];
       args: [
         {

+ 3 - 1
clients/typescript/test/intermediate-level/acceleration.ts

@@ -50,11 +50,13 @@ export function acceleration(framework: Framework) {
         componentId: framework.exampleComponentPosition.programId,
       });
 
+      console.log("Payer: " + framework.provider.wallet.publicKey);
+      console.log("Entity: " + framework.acceleratedEntityPda);
       await framework.provider.sendAndConfirm(
         delegateComponent.transaction,
         [],
         {
-          skipPreflight: true,
+          skipPreflight: false,
           commitment: "confirmed",
         },
       );

+ 3 - 3
clients/typescript/test/intermediate-level/index.ts

@@ -8,8 +8,8 @@ import { acceleration } from "./acceleration";
 describe("Intermediate level API", () => {
   const framework: Framework = new Framework();
   world(framework);
-  ecs(framework);
-  session(framework);
+  // ecs(framework);
+  // session(framework);
   acceleration(framework);
-  permissioning(framework);
+  // permissioning(framework);
 });

+ 1 - 1
clients/typescript/test/main.ts

@@ -1,2 +1,2 @@
-import "./low-level";
+// import "./low-level";
 import "./intermediate-level";

+ 31 - 0
crates/programs/bolt-component/src/lib.rs

@@ -21,6 +21,37 @@ pub mod bolt_component {
     pub fn set_data(_ctx: Context<SetData>) -> Result<()> {
         Ok(())
     }
+
+    pub fn delegate(_ctx: Context<DelegateInput>, _commit_frequency_ms: u32, _validator: Option<Pubkey>) -> Result<()> {
+        Ok(())
+    }
+}
+
+
+#[derive(Accounts)]
+pub struct DelegateInput<'info> {
+    pub payer: Signer<'info>,
+    /// CHECK:
+    #[account()]
+    pub entity: AccountInfo<'info>,
+    /// CHECK:
+    #[account(mut)]
+    pub account: AccountInfo<'info>,
+    /// CHECK:`
+    pub owner_program: AccountInfo<'info>,
+    /// CHECK:
+    #[account(mut)]
+    pub buffer: AccountInfo<'info>,
+    /// CHECK:`
+    #[account(mut)]
+    pub delegation_record: AccountInfo<'info>,
+    /// CHECK:`
+    #[account(mut)]
+    pub delegation_metadata: AccountInfo<'info>,
+    /// CHECK:`
+    pub delegation_program: AccountInfo<'info>,
+    /// CHECK:`
+    pub system_program: AccountInfo<'info>,
 }
 
 #[derive(Accounts)]

+ 42 - 8
crates/programs/world/src/lib.rs

@@ -288,16 +288,16 @@ pub mod world {
         Ok(())
     }
 
-    pub fn delegate_buffer(ctx: Context<DelegateBuffer>, commit_frequency_ms: u32, validator: Option<Pubkey>) -> Result<()> {
+    pub fn delegate_component(ctx: Context<DelegateComponent>, commit_frequency_ms: u32, validator: Option<Pubkey>) -> Result<()> {
         let pda_seeds: &[&[u8]] = &[b"buffer", &ctx.accounts.component.key().to_bytes()];
 
         let del_accounts = ephemeral_rollups_sdk::cpi::DelegateAccounts {
             payer: &ctx.accounts.payer,
             pda: &ctx.accounts.component_buffer,
-            owner_program: &ctx.accounts.owner_program,
-            buffer: &ctx.accounts.buffer,
-            delegation_record: &ctx.accounts.delegation_record,
-            delegation_metadata: &ctx.accounts.delegation_metadata,
+            owner_program: &ctx.accounts.world_program,
+            buffer: &ctx.accounts.buffer_buffer,
+            delegation_record: &ctx.accounts.buffer_delegation_record,
+            delegation_metadata: &ctx.accounts.buffer_delegation_metadata,
             delegation_program: &ctx.accounts.delegation_program,
             system_program: &ctx.accounts.system_program,
         };
@@ -313,20 +313,39 @@ pub mod world {
             config,
         )?;
 
+        bolt_component::cpi::delegate(
+            CpiContext::new(
+                ctx.accounts.component_program.to_account_info(),
+                bolt_component::cpi::accounts::DelegateInput {
+                    payer: ctx.accounts.payer.to_account_info(),
+                    entity: ctx.accounts.entity.to_account_info(),
+                    account: ctx.accounts.component.to_account_info(),
+                    owner_program: ctx.accounts.component_program.to_account_info(),
+                    buffer: ctx.accounts.buffer.to_account_info(),
+                    delegation_metadata: ctx.accounts.delegation_metadata.to_account_info(),
+                    delegation_record: ctx.accounts.delegation_record.to_account_info(),
+                    delegation_program: ctx.accounts.delegation_program.to_account_info(),
+                    system_program: ctx.accounts.system_program.to_account_info()
+                }
+            ),
+            commit_frequency_ms,
+            validator
+        )?;
+
         Ok(())
     }
 
     #[derive(Accounts)]
-    pub struct DelegateBuffer<'info> {
+    pub struct DelegateComponent<'info> {
         pub payer: Signer<'info>,
         /// CHECK:
-        #[account()]
+        #[account(mut)]
         pub component: AccountInfo<'info>,
         /// CHECK:
         #[account(mut)]
         pub component_buffer: AccountInfo<'info>,
         /// CHECK:`
-        pub owner_program: AccountInfo<'info>,
+        pub component_program: AccountInfo<'info>,
         /// CHECK:
         #[account(mut)]
         pub buffer: AccountInfo<'info>,
@@ -340,6 +359,21 @@ pub mod world {
         pub delegation_program: AccountInfo<'info>,
         /// CHECK:`
         pub system_program: Program<'info, System>,
+        /// CHECK:
+        #[account()]
+        pub entity: AccountInfo<'info>,
+        /// CHECK:`
+        pub world_program: AccountInfo<'info>,
+        /// CHECK:
+        #[account(mut)]
+        pub buffer_buffer: AccountInfo<'info>,
+        /// CHECK:`
+        #[account(mut)]
+        pub buffer_delegation_record: AccountInfo<'info>,
+        /// CHECK:`
+        #[account(mut)]
+        pub buffer_delegation_metadata: AccountInfo<'info>,
+    
     }
 
     pub fn destroy_component(ctx: Context<DestroyComponent>) -> Result<()> {

+ 3 - 3
tests/script.sh

@@ -8,8 +8,8 @@ sleep 5
 echo "Running TypeScript tests..."
 yarn run ts-mocha -p ./tsconfig.json -t 1000000 clients/typescript/test/main.ts
 
-echo "Running C# tests..."
-cd clients/csharp/Solana.Unity.Bolt.Test
-dotnet run --configuration Release
+# echo "Running C# tests..."
+# cd clients/csharp/Solana.Unity.Bolt.Test
+# dotnet run --configuration Release
 
 echo "Tests completed."