Skip to content

Conversation

thewuhxyz
Copy link
Contributor

@thewuhxyz thewuhxyz commented May 13, 2025

Status Type ⚠️ Core Change Issue
Ready Feature Yes Nil

Problem

What problem are you trying to solve?

Anchor is not the most adequate for real-time systems on solana. Bloated, slow, expensive

Solution

How did you solve the problem?

Rewrite pieces of bolt with zero-copy pinocchio with full backwards compatiblity

Before & After Screenshots

Screenshot 2025-05-11 at 19 54 12

Deploy Notes

cargo build-sbf over anchor build. anchor build for generating idls

New dependencies:

  • pinocchio : Pinocchio

Greptile Summary

This PR introduces a significant architectural change by implementing a Pinocchio-based alternative to the existing Anchor-based world program in the Bolt framework. The core motivation is performance optimization - addressing Anchor's limitations for real-time systems on Solana by replacing it with zero-copy operations.

Key Changes:

  • New Pinocchio World Program: A complete rewrite of the world program using the pinocchio framework, located in crates/pinocchio/world/. This includes all instruction handlers (add_entity, apply_system, initialize_component, etc.) rewritten with manual memory management and unsafe operations for performance.

  • CPI Interface Layer: A new bolt-cpi-interface crate that provides cross-program invocation interfaces for component operations (initialize, update, destroy) and system execution using Pinocchio's primitives instead of Anchor's CPI abstractions.

  • State Management Rewrite: Complete reimplementation of state structures (World, Entity, Registry, SystemWhitelist) using zero-copy Transmutable traits and manual discriminator handling.

  • Instruction Dispatching: Custom instruction dispatch system using u64 discriminators instead of Anchor's automatic deserialization, with manual byte parsing and unsafe pointer operations.

Architecture Integration:

The implementation maintains full backwards compatibility by preserving the same program ID, instruction discriminators, and account structures. This allows existing clients to work unchanged while benefiting from reduced compute unit costs. The new implementation coexists with the existing Anchor version, suggesting a gradual migration strategy where both can operate during the transition period.

The change affects the core ECS (Entity Component System) functionality of Bolt, including world management, entity creation, component lifecycle, and system execution - all critical paths for real-time blockchain gaming applications.

Confidence score: 1/5

  • This PR contains critical bugs that will cause runtime failures and prevent proper program execution
  • Multiple critical typos in discriminator constants create mismatches between instruction identification and execution paths
  • The combination of unsafe memory operations, complex manual memory management, and discriminator inconsistencies makes this unsafe to merge
  • Files with the most critical issues: crates/pinocchio/world/src/instructions/mod.rs, crates/pinocchio/world/src/state/world.rs, crates/pinocchio/world/src/consts.rs

@GabrielePicco GabrielePicco marked this pull request as ready for review July 30, 2025 09:42
@GabrielePicco GabrielePicco changed the title pinocchio world Reimplementation of the world program with Pinocchio Jul 30, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

34 files reviewed, 20 comments

Edit Code Review Bot Settings | Greptile

Comment on lines +38 to +39
pub const ADD_AUTHORIITY_DISCRIMINATOR: u64 = 13217455069452700133;
pub const REMOVE_AUTHORIITY_DISCRIMINATOR: u64 = 15585545156648003826;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo in constant names: 'AUTHORIITY' should be 'AUTHORITY'

Suggested change
pub const ADD_AUTHORIITY_DISCRIMINATOR: u64 = 13217455069452700133;
pub const REMOVE_AUTHORIITY_DISCRIMINATOR: u64 = 15585545156648003826;
pub const ADD_AUTHORITY_DISCRIMINATOR: u64 = 13217455069452700133;
pub const REMOVE_AUTHORITY_DISCRIMINATOR: u64 = 15585545156648003826;

ApproveSystem = APPROVE_SYSTEM_DISCRIMINATOR,
RemoveSystem = REMOVE_SYSTEM_DISCRIMINATOR,
AddEntity = ADD_ENTITY_DISCRIMINATOR,
InitilizeComponent = INITIALIZE_COMPONENT_DISCRIMINATOR,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo in enum variant: 'InitilizeComponent' should be 'InitializeComponent'

Suggested change
InitilizeComponent = INITIALIZE_COMPONENT_DISCRIMINATOR,
InitializeComponent = INITIALIZE_COMPONENT_DISCRIMINATOR,

@@ -0,0 +1 @@
pub const DISCRIMATOR_LENGTH: usize = 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo in constant name: 'DISCRIMATOR_LENGTH' should be 'DISCRIMINATOR_LENGTH' (missing 'IN'). This matches the naming convention used elsewhere and fixes the spelling error.

Suggested change
pub const DISCRIMATOR_LENGTH: usize = 8;
pub const DISCRIMINATOR_LENGTH: usize = 8;

Comment on lines +28 to +30
if *is_permissionless {
*is_permissionless = false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Missing semicolon after assignment

Suggested change
if *is_permissionless {
*is_permissionless = false
}
if *is_permissionless {
*is_permissionless = false;
}

Comment on lines +32 to +34
if !authorities.contains(authority_to_delete.key()) {
return Err(WorldError::InvalidAuthority.into());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Missing authority validation - should verify that the calling authority has permission to remove other authorities from the world

AccountMeta::readonly(self.instruction_sysvar_account.key()),
];

const DISCRIMATOR_LENGTH: usize = 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo: 'DISCRIMATOR_LENGTH' should be 'DISCRIMINATOR_LENGTH'

Suggested change
const DISCRIMATOR_LENGTH: usize = 8;
const DISCRIMINATOR_LENGTH: usize = 8;

@@ -0,0 +1,70 @@
use crate::consts::DISCRIMATOR_LENGTH;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo in constant name: 'DISCRIMATOR_LENGTH' should be 'DISCRIMINATOR_LENGTH'

Suggested change
use crate::consts::DISCRIMATOR_LENGTH;
use crate::consts::DISCRIMINATOR_LENGTH;

}
.invoke()?;

let return_data = get_return_data().ok_or(ProgramError::InvalidAccountData)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Return data validation only checks for existence, not minimum size. If system returns malformed data with less than 4 bytes, the subsequent parsing will fail.

return Err(ProgramError::MissingRequiredSignature);
}

let world_id = u64::from_le_bytes(unsafe { (data.as_ptr() as *const [u8; 8]).read() });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Unsafe pointer casting without length validation could cause memory safety issues if data is shorter than 8 bytes

Suggested change
let world_id = u64::from_le_bytes(unsafe { (data.as_ptr() as *const [u8; 8]).read() });
let world_id = u64::from_le_bytes(
data.get(..8)
.ok_or(ProgramError::InvalidInstructionData)?
.try_into()
.map_err(|_| ProgramError::InvalidInstructionData)?
);


let mut instruction_data = [0u8; 1024];

const DISCRIMATOR_LENGTH: usize = 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo in constant name: 'DISCRIMATOR_LENGTH' should be 'DISCRIMINATOR_LENGTH' to match standard spelling

Suggested change
const DISCRIMATOR_LENGTH: usize = 8;
const DISCRIMINATOR_LENGTH: usize = 8;

@notdanilo
Copy link
Contributor

How are you testing it? bolt test is running the previous world program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants