Skip to content

Commit

Permalink
feat(solana): adds Message
Browse files Browse the repository at this point in the history
  • Loading branch information
allemanfredi committed Nov 29, 2024
1 parent bfc7770 commit 7ce941b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
3 changes: 2 additions & 1 deletion packages/solana/programs/debridge-reporter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ idl-build = ["anchor-lang/idl-build"]
[dependencies]
anchor-lang = "0.30.1"
debridge-solana-sdk = "1.0.1"
slots = { path = "../../shared/slots" }
slots = { path = "../../shared/slots" }
message = { path = "../../shared/message" }
28 changes: 5 additions & 23 deletions packages/solana/programs/debridge-reporter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use debridge_solana_sdk::sending;
use slots::get_slot;

declare_id!("2TvQ6gqQGAifdV2cQ1f8zHGYV2t6wPUNTKzpcALt8rX7");

pub mod contexts;

pub use contexts::*;

#[derive(BorshSerialize)]
pub struct Message {
pub ids: Vec<[u8; 32]>,
pub hashes: Vec<[u8; 32]>,
}

#[program]
pub mod debridge_reporter {
use super::*;
use debridge_solana_sdk::sending;
use message::Message;
use slots::get_slot;

pub fn dispatch_slot(
ctx: Context<DispatchSlot>,
target_chain_id: [u8; 32],
receiver: Vec<u8>,
slot_number: u64,
) -> Result<()> {
let (number, hash) = get_slot(&ctx.accounts.slot_hashes, slot_number)?;

let ids: Vec<[u8; 32]> = vec![u64_to_u8_32(number)];
let hashes: Vec<[u8; 32]> = vec![hash];
let message = Message { ids, hashes };

let slot_hash = get_slot(&ctx.accounts.slot_hashes, slot_number)?;
let message = Message::from((slot_number, slot_hash));
sending::invoke_send_message(
message.try_to_vec()?,
target_chain_id,
Expand All @@ -40,14 +30,6 @@ pub mod debridge_reporter {
ctx.remaining_accounts,
)
.map_err(ProgramError::from)?;

Ok(())
}
}

pub fn u64_to_u8_32(number: u64) -> [u8; 32] {
let mut bytes = [0u8; 32];
let number_bytes = number.to_be_bytes();
bytes[24..].copy_from_slice(&number_bytes);
bytes
}
16 changes: 16 additions & 0 deletions packages/solana/shared/message/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "message"
version = "0.1.0"
description = "Message struct to use within Hashi"
edition = "2021"

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

[dependencies]
anchor-lang = "0.30.1"
26 changes: 26 additions & 0 deletions packages/solana/shared/message/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use std::convert::From;

#[derive(BorshDeserialize, BorshSerialize)]
pub struct Message {
pub ids: Vec<[u8; 32]>,
pub hashes: Vec<[u8; 32]>,
}

impl From<(u64, [u8; 32])> for Message {
fn from(value: (u64, [u8; 32])) -> Self {
let (id, hash) = value;
Message {
ids: vec![u64_to_u8_32(id)],
hashes: vec![hash],
}
}
}

fn u64_to_u8_32(number: u64) -> [u8; 32] {
let mut bytes = [0u8; 32];
let number_bytes = number.to_be_bytes();
bytes[24..].copy_from_slice(&number_bytes);
bytes
}
4 changes: 2 additions & 2 deletions packages/solana/shared/slots/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::sysvar;

pub fn get_slot(slot_hashes: &AccountInfo, slot_number: u64) -> Result<(u64, [u8; 32])> {
pub fn get_slot(slot_hashes: &AccountInfo, slot_number: u64) -> Result<[u8; 32]> {
if *slot_hashes.key != sysvar::slot_hashes::ID {
return Err(error!(ErrorCode::InvalidSlotHashesSysVar));
}
Expand All @@ -24,7 +24,7 @@ pub fn get_slot(slot_hashes: &AccountInfo, slot_number: u64) -> Result<(u64, [u8
};

if current_slot_number == slot_number {
return Ok((current_slot_number, current_slot_hash));
return Ok(current_slot_hash);
}

pos += 32;
Expand Down

0 comments on commit 7ce941b

Please sign in to comment.