Skip to content

Commit

Permalink
fix: solana cluster connection init
Browse files Browse the repository at this point in the history
  • Loading branch information
Itshyphen committed Oct 7, 2024
1 parent 231a239 commit 7d0e578
Show file tree
Hide file tree
Showing 12 changed files with 1,116 additions and 0 deletions.
38 changes: 38 additions & 0 deletions contracts/solana/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions contracts/solana/programs/cluster-connection/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "cluster-connection"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "cluster_connection"

[features]
default = []
cpi = ["no-entrypoint"]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = { workspace = true, features = ["init-if-needed"] }

xcall-lib = { workspace = true }
ed25519-dalek = { version = "1.0.1" }
2 changes: 2 additions & 0 deletions contracts/solana/programs/cluster-connection/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const ACCOUNT_DISCRIMINATOR_SIZE: usize = 8;
289 changes: 289 additions & 0 deletions contracts/solana/programs/cluster-connection/src/contexts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
use anchor_lang::prelude::*;

use crate::{error::ConnectionError, state::*};

#[derive(Accounts)]
pub struct Initialize<'info> {
/// Rent payer
#[account(mut)]
pub signer: Signer<'info>,

/// System Program: Required for creating the centralized-connection config
pub system_program: Program<'info, System>,

/// Config
#[account(
init,
payer = signer,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump,
space = Config::LEN
)]
pub config: Account<'info, Config>,

#[account(
init,
payer = signer,
space = Authority::LEN,
seeds = [Authority::SEED_PREFIX.as_bytes()],
bump
)]
pub authority: Account<'info, Authority>,
}

#[derive(Accounts)]
#[instruction(to: String)]
pub struct SendMessage<'info> {
#[account(mut)]
pub signer: Signer<'info>,

pub system_program: Program<'info, System>,

#[account(
owner = config.xcall @ ConnectionError::OnlyXcall
)]
pub xcall: Signer<'info>,

#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
)]
pub config: Account<'info, Config>,

#[account(
seeds = [NetworkFee::SEED_PREFIX.as_bytes(), to.as_bytes()],
bump = network_fee.bump
)]
pub network_fee: Account<'info, NetworkFee>,
}

#[derive(Accounts)]
#[instruction(src_network: String, conn_sn: u128)]
pub struct RecvMessage<'info> {
#[account(mut)]
pub admin: Signer<'info>,

pub system_program: Program<'info, System>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,

#[account(
init,
payer = admin,
seeds = [Receipt::SEED_PREFIX.as_bytes(), src_network.as_bytes(), &conn_sn.to_be_bytes()],
space = Receipt::LEN,
bump
)]
pub receipt: Account<'info, Receipt>,

#[account(
seeds = [Authority::SEED_PREFIX.as_bytes()],
bump = authority.bump
)]
pub authority: Account<'info, Authority>,
}

#[derive(Accounts)]
pub struct RevertMessage<'info> {
#[account(mut)]
pub admin: Signer<'info>,

pub system_program: Program<'info, System>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,

#[account(
seeds = [Authority::SEED_PREFIX.as_bytes()],
bump = authority.bump
)]
pub authority: Account<'info, Authority>,
}

#[derive(Accounts)]
pub struct SetAdmin<'info> {
/// Transaction signer
#[account(mut)]
pub admin: Signer<'info>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,
}

#[derive(Accounts)]
#[instruction(network_id: String)]
pub struct SetFee<'info> {
/// Rent payer
#[account(mut)]
pub admin: Signer<'info>,

/// System Program: Required to create program-derived address
pub system_program: Program<'info, System>,

/// Fee
#[account(
init_if_needed,
payer = admin,
seeds = [NetworkFee::SEED_PREFIX.as_bytes(), network_id.as_bytes()],
bump,
space = NetworkFee::LEN
)]
pub network_fee: Account<'info, NetworkFee>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,
}

#[derive(Accounts)]
#[instruction(network_id: String)]
pub struct GetFee<'info> {
/// Fee
#[account(
seeds = [NetworkFee::SEED_PREFIX.as_bytes(), network_id.as_bytes()],
bump = network_fee.bump
)]
pub network_fee: Account<'info, NetworkFee>,
}

#[derive(Accounts)]
pub struct ClaimFees<'info> {
/// Rent payer
#[account(mut)]
pub admin: Signer<'info>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,
}

#[derive(Accounts)]
pub struct SetThreshold<'info> {
/// Transaction signer
#[account(mut)]
pub admin: Signer<'info>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,
}

#[derive(Accounts)]
pub struct GetThreshold<'info> {
/// Config
#[account(
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump
)]
pub config: Account<'info, Config>,
}

#[derive(Accounts)]
pub struct AddValidator<'info> {
/// Transaction signer
#[account(mut)]
pub admin: Signer<'info>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,
}

#[derive(Accounts)]
pub struct RemoveValidator<'info> {
/// Transaction signer
#[account(mut)]
pub admin: Signer<'info>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,
}

#[derive(Accounts)]
pub struct GetValidators<'info> {
/// Config
#[account(
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump
)]
pub config: Account<'info, Config>,
}

#[derive(Accounts)]
#[instruction(src_network: String, conn_sn: u128)]
pub struct ReceiveMessageWithSignatures<'info> {
#[account(mut)]
pub admin: Signer<'info>,

pub system_program: Program<'info, System>,

/// Config
#[account(
mut,
seeds = [Config::SEED_PREFIX.as_bytes()],
bump = config.bump,
has_one = admin @ ConnectionError::OnlyAdmin,
)]
pub config: Account<'info, Config>,

#[account(
init,
payer = admin,
seeds = [Receipt::SEED_PREFIX.as_bytes(), src_network.as_bytes(), &conn_sn.to_be_bytes()],
space = Receipt::LEN,
bump
)]
pub receipt: Account<'info, Receipt>,

#[account(
seeds = [Authority::SEED_PREFIX.as_bytes()],
bump = authority.bump
)]
pub authority: Account<'info, Authority>,
}
16 changes: 16 additions & 0 deletions contracts/solana/programs/cluster-connection/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use anchor_lang::prelude::*;

#[error_code]
pub enum ConnectionError {
#[msg("Only admin")]
OnlyAdmin,

#[msg("Only xcall")]
OnlyXcall,

#[msg("Admin Validator Cnnot Be Removed")]
AdminValidatorCnnotBeRemoved,

#[msg("Validators Must Be Greater Than Threshold")]
ValidatorsMustBeGreaterThanThreshold,
}
Loading

0 comments on commit 7d0e578

Please sign in to comment.