Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: solana cluster connection #405

Merged
merged 9 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 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 }
rlp = { workspace = true }
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;
198 changes: 198 additions & 0 deletions contracts/solana/programs/cluster-connection/src/contexts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
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)]
pub struct RevertMessage<'info> {
#[account(mut)]
pub relayer: Signer<'info>,

pub system_program: Program<'info, System>,

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

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

#[derive(Accounts)]
pub struct SetConfigItem<'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 relayer: Signer<'info>,

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

/// Fee
#[account(
init_if_needed,
payer = relayer,
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 = relayer @ ConnectionError::OnlyRelayer,
)]
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 relayer: Signer<'info>,

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

#[derive(Accounts)]
pub struct GetConfigItem<'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 relayer: Signer<'info>,

pub system_program: Program<'info, System>,

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

#[account(
init,
payer = relayer,
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>,
}
19 changes: 19 additions & 0 deletions contracts/solana/programs/cluster-connection/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anchor_lang::prelude::*;

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

#[msg("Only relayer")]
OnlyRelayer,

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

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

#[msg("Validators Must Be Greater Than Threshold")]
ValidatorsMustBeGreaterThanThreshold,
}
10 changes: 10 additions & 0 deletions contracts/solana/programs/cluster-connection/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![allow(non_snake_case)]

use anchor_lang::prelude::*;

#[event]
pub struct SendMessage {
pub targetNetwork: String,
pub connSn: u128,
pub msg: Vec<u8>,
}
Loading
Loading