generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(solana): rm dispatch_slot in favour of dispatch_root within debr…
…idge_reporter and rm slots
- Loading branch information
1 parent
3f611be
commit a1b35a0
Showing
15 changed files
with
266 additions
and
98 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,5 @@ idl-build = ["anchor-lang/idl-build"] | |
[dependencies] | ||
anchor-lang = "0.30.1" | ||
debridge-solana-sdk = { git = "ssh://[email protected]/debridge-finance/debridge-solana-sdk.git", version = "1.0.2" } | ||
slots = { path = "../../shared/slots" } | ||
message = { path = "../../shared/message" } | ||
message = { path = "../../shared/message" } | ||
snapshotter = { path = "../snapshotter", default-features = false, features = ["cpi"] } |
51 changes: 51 additions & 0 deletions
51
packages/solana/programs/debridge-reporter/src/contexts/dispatch_root.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
use crate::{error::ErrorCode, state::Config}; | ||
|
||
#[derive(Accounts)] | ||
pub struct DispatchRoot<'info> { | ||
/// The `config` account holds the snapshotter's configuration and state. | ||
/// | ||
/// - **PDA Derivation**: The account is a PDA derived using the `Config::SEED_PREFIX` as the seed. This ensures | ||
/// that the account address is uniquely identified and securely accessed by the program. | ||
/// | ||
/// - **Bump Seed**: The `bump` attribute is used to derive the PDA alongside the seeds. It guarantees that the PDA | ||
/// is valid and helps prevent collisions with other accounts. | ||
/// | ||
/// - **Read-Only**: The absence of the `mut` keyword indicates that this account will not be modified during | ||
/// the instruction execution. | ||
#[account( | ||
seeds = [Config::SEED_PREFIX], | ||
bump, | ||
)] | ||
/// Config account. | ||
pub config: Account<'info, Config>, | ||
|
||
/// The `snapshotter_config` account represents the snapshotter's configuration. | ||
/// | ||
/// - **Mutability**: Marked as `mut` to allow modifications during the instruction execution. | ||
/// - **Address Constraint**: Ensures that the provided `snapshotter_config` account matches the | ||
/// `snapshotter_config` specified within the `config` account. If there is a mismatch, | ||
/// the program will throw the `InvalidSnapshotterConfig` error. | ||
/// | ||
/// - **CHECK Annotation**: | ||
/// - **Purpose**: Indicates to Anchor that this account requires manual validation. | ||
/// - **Reason**: Since `UncheckedAccount` bypasses Anchor's type and ownership checks, the developer | ||
/// must ensure that the account is valid and correctly matches the expected configuration. | ||
/// | ||
/// ## Security Considerations: | ||
/// - **Manual Validation**: Developers must ensure that the `snapshotter_config` account is trustworthy | ||
/// and correctly corresponds to the one stored in `config`. Failing to do so can lead to unauthorized | ||
/// modifications or inconsistencies in the program's state. | ||
/// | ||
/// ## Error Handling: | ||
/// - **`ErrorCode::InvalidSnapshotterConfig`**: | ||
/// - **Trigger**: Thrown when the `snapshotter_config` account's public key does not match the one specified in `config`. | ||
/// - **Purpose**: Prevents unauthorized or incorrect accounts from being used, ensuring the integrity of the snapshotter's configuration. | ||
#[account( | ||
mut, | ||
address = config.snapshotter_config @ ErrorCode::InvalidSnapshotterConfig | ||
)] | ||
/// CHECK: Snapshotter program | ||
pub snapshotter_config: UncheckedAccount<'info>, | ||
} |
9 changes: 0 additions & 9 deletions
9
packages/solana/programs/debridge-reporter/src/contexts/dispatch_slot.rs
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
packages/solana/programs/debridge-reporter/src/contexts/initialize.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
use crate::state::Config; | ||
|
||
#[derive(Accounts)] | ||
pub struct Initialize<'info> { | ||
/// The `owner` is a Signer account, indicating that whoever calls this instruction | ||
/// must sign the transaction with their private key. This ensures that the initializer | ||
/// has authority. | ||
/// | ||
/// `#[account(mut)]` is used here because the `owner` account's lamports are used | ||
/// to fund the creation of the `config` account. | ||
/// | ||
/// ## Attributes | ||
/// | ||
/// - **`mut`**: Marks the `owner` account as mutable, allowing its lamports to be debited. | ||
#[account(mut)] | ||
/// Whoever initializes the config will be the owner of the program. Signer | ||
/// for creating the [`Config`] account. | ||
pub owner: Signer<'info>, | ||
|
||
/// The `config` account will be created (initialized) by this instruction. | ||
/// | ||
/// The `init` attribute indicates that this account should be created (allocated and assigned) | ||
/// and initialized with the given seeds, bump, and space. | ||
/// | ||
/// - **`payer = owner`**: The `owner` account pays for the rent and creation cost of this account. | ||
/// - **`seeds = [Config::SEED_PREFIX]`**: Sets the PDA (program-derived address) seed that | ||
/// uniquely identifies the `config` account. This ensures that the correct PDA is referenced. | ||
/// - **`bump`**: Used in combination with the seeds to find the PDA. The `bump` ensures a valid PDA | ||
/// that is not already taken by another account. | ||
/// - **`space = Config::MAXIMUM_SIZE`**: Allocates the appropriate amount of space for the `config` account's data based on the `Config` structure. | ||
/// | ||
/// ## Attributes | ||
/// | ||
/// - **`init`**: Indicates that this account should be initialized by the instruction. | ||
/// - **`payer = owner`**: Specifies that the `owner` account will pay for the account's rent and creation. | ||
/// - **`seeds = [Config::SEED_PREFIX]`**: Defines the seed used to derive the PDA for the `config` account. | ||
/// - **`bump`**: Provides the bump seed necessary for PDA derivation. | ||
/// - **`space = Config::MAXIMUM_SIZE`**: Allocates sufficient space for the `config` account's data. | ||
#[account( | ||
init, | ||
payer = owner, | ||
seeds = [Config::SEED_PREFIX], | ||
bump, | ||
space = Config::MAXIMUM_SIZE, | ||
)] | ||
/// Config account, which saves program data useful for other instructions. | ||
/// | ||
/// It is created on initialization, ensuring that all subsequent operations | ||
/// have a consistent place to store relevant configuration information. | ||
pub config: Account<'info, Config>, | ||
|
||
/// The `snapshotter_config` account represents the snapshotter's configuration. | ||
/// | ||
/// - **UncheckedAccount**: The `snapshotter_config` is marked as `UncheckedAccount`, meaning that Anchor will | ||
/// not perform any type or ownership checks on this account. It is the developer's responsibility to ensure | ||
/// that this account is valid and correctly matches the expected configuration. | ||
/// | ||
/// - **Purpose**: Represents the snapshotter's configuration. Although marked as `UncheckedAccount`, it should be correctly associated with the `Config` account to maintain program integrity. | ||
/// CHECK: Snapshotter program | ||
pub snapshotter_config: UncheckedAccount<'info>, | ||
|
||
/// The `system_program` is the program responsible for creating and allocating accounts. | ||
/// | ||
/// It is necessary whenever new accounts need to be created and funded. | ||
/// | ||
/// ## Attributes | ||
/// | ||
/// - **Standard System Program**: Required here to create and fund the `config` account. | ||
pub system_program: Program<'info, System>, | ||
} |
6 changes: 4 additions & 2 deletions
6
packages/solana/programs/debridge-reporter/src/contexts/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod dispatch_slot; | ||
pub mod dispatch_root; | ||
pub mod initialize; | ||
|
||
pub use dispatch_slot::*; | ||
pub use dispatch_root::*; | ||
pub use initialize::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use anchor_lang::prelude::error_code; | ||
|
||
#[error_code] | ||
pub enum ErrorCode { | ||
#[msg("InvalidSnapshotterConfig")] | ||
/// Invalid Snapshotter config | ||
InvalidSnapshotterConfig, | ||
|
||
#[msg("RootNotFinalized")] | ||
/// Snapshotter root not finalized | ||
RootNotFinalized, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/solana/programs/debridge-reporter/src/state/config.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
#[account] | ||
#[derive(Default)] | ||
pub struct Config { | ||
/// Snapshotter config address. | ||
/// | ||
/// - **Type**: `Pubkey` | ||
/// - **Purpose**: | ||
/// - Stores the public key of the `snapshotter_config` account. | ||
/// - This address is used to reference and interact with the snapshotter's configuration. | ||
/// - Ensures that the program can locate and validate the snapshotter's configuration during operations. | ||
pub snapshotter_config: Pubkey, | ||
} | ||
|
||
impl Config { | ||
/// `MAXIMUM_SIZE` defines the total byte size allocated for the `Config` account. | ||
/// | ||
/// Anchor requires specifying the exact account size to allocate storage on the blockchain. | ||
/// This constant ensures that the account is allocated enough space for all its fields. | ||
/// | ||
/// **Breakdown of `MAXIMUM_SIZE`:** | ||
/// - `8` bytes: Account discriminator (used by Anchor to identify account types). | ||
/// - `32` bytes: The `snapshotter_config` field, which is a `Pubkey` (32 bytes). | ||
/// | ||
/// **Total `MAXIMUM_SIZE`**: `8 + 32 = 40` bytes. | ||
/// | ||
/// **Note**: Currently, the `Config` struct contains only one field. If additional fields are added in the future, | ||
/// update the `MAXIMUM_SIZE` accordingly to accommodate the new data. | ||
pub const MAXIMUM_SIZE: usize = 8 // discriminator | ||
+ 32; // snapshotter_config | ||
|
||
/// `SEED_PREFIX` is a static byte array used as a seed for deriving the Program Derived Address (PDA) of the `Config` account. | ||
/// | ||
/// - **Type**: `&'static [u8; 6]` | ||
/// - **Value**: `b"config"` | ||
/// - **Purpose**: | ||
/// - Provides a unique and consistent seed used in PDA derivation. | ||
/// - Ensures that the PDA for the `Config` account is deterministic and can be reliably regenerated. | ||
/// - Helps prevent address collisions by using a well-known, stable prefix. | ||
pub const SEED_PREFIX: &'static [u8; 6] = b"config"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod config; | ||
|
||
pub use config::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.