Skip to content

Commit

Permalink
Merge branch 'main' into feat/stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusVorwald authored Nov 7, 2024
2 parents 1d54b27 + 9c03a0b commit d86e9fe
Show file tree
Hide file tree
Showing 28 changed files with 1,090 additions and 206 deletions.
6 changes: 6 additions & 0 deletions contracts/solana/libs/xcall-lib/src/xcall_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ pub struct HandleMessageArgs {
pub from_nid: String,
pub message: Vec<u8>,
pub sequence_no: u128,
pub conn_sn: u128,
}

#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)]
pub struct HandleRequestArgs {
pub from_nid: String,
pub msg_payload: Vec<u8>,
pub conn_sn: u128,
}

#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)]
pub struct HandleResultArgs {
pub from_nid: String,
pub msg_payload: Vec<u8>,
pub sequence_no: u128,
pub conn_sn: u128,
}

#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)]
Expand All @@ -48,4 +51,7 @@ pub struct HandleErrorArgs {
#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)]
pub struct HandleForcedRollback {
pub req_id: u128,
pub from_nid: String,
pub conn_sn: u128,
pub connection: Pubkey,
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ pub fn call_xcall_handle_message<'info>(
from_nid: String,
message: Vec<u8>,
sequence_no: u128,
conn_sn: u128,
) -> Result<()> {
let mut data = vec![];
let args = xcall_type::HandleMessageArgs {
from_nid,
message,
sequence_no,
conn_sn,
};
args.serialize(&mut data)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::{
prelude::*,
solana_program::{
instruction::Instruction,
program::{get_return_data, invoke},
program::{get_return_data, invoke, invoke_signed},
system_program,
},
};
Expand Down Expand Up @@ -35,8 +35,8 @@ pub fn query_send_message_accounts<'info>(
})
}

pub fn query_recv_message_accounts(
ctx: Context<QueryAccountsCtx>,
pub fn query_recv_message_accounts<'info>(
ctx: Context<'_, '_, '_, 'info, QueryAccountsCtx<'info>>,
src_network: String,
conn_sn: u128,
msg: Vec<u8>,
Expand Down Expand Up @@ -65,8 +65,8 @@ pub fn query_recv_message_accounts(
AccountMetadata::new(authority, false),
];

let mut xcall_account_metas = vec![];
let mut xcall_account_infos = vec![];
let mut xcall_account_metas = vec![AccountMeta::new_readonly(config.key(), true)];
let mut xcall_account_infos = vec![config.to_account_info()];

for (_, account) in ctx.remaining_accounts.iter().enumerate() {
if account.is_writable {
Expand All @@ -78,15 +78,19 @@ pub fn query_recv_message_accounts(
xcall_account_infos.push(account.to_account_info())
}

let ix_data = get_handle_message_ix_data(src_network, msg, sequence_no)?;
let ix_data = get_handle_message_ix_data(src_network, msg, sequence_no, conn_sn)?;

let ix = Instruction {
program_id: config.xcall,
accounts: xcall_account_metas,
data: ix_data,
};

invoke(&ix, &xcall_account_infos)?;
invoke_signed(
&ix,
&xcall_account_infos,
&[&[Config::SEED_PREFIX.as_bytes(), &[config.bump]]],
)?;

let (_, data) = get_return_data().unwrap();
let mut data_slice: &[u8] = &data;
Expand Down Expand Up @@ -171,12 +175,14 @@ pub fn get_handle_message_ix_data(
from_nid: String,
message: Vec<u8>,
sequence_no: u128,
conn_sn: u128,
) -> Result<Vec<u8>> {
let mut ix_args_data = vec![];
let ix_args = xcall_type::HandleMessageArgs {
from_nid,
message,
sequence_no,
conn_sn,
};
ix_args.serialize(&mut ix_args_data)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub mod centralized_connection {
msg: Vec<u8>,
sequence_no: u128,
) -> Result<()> {
helper::call_xcall_handle_message(ctx, src_network, msg, sequence_no)
helper::call_xcall_handle_message(ctx, src_network, msg, sequence_no, conn_sn)
}

pub fn revert_message<'info>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use crate::{state::*, xcall};
pub fn execute_forced_rollback<'info>(
ctx: Context<'_, '_, '_, 'info, ExecuteForcedRollbackCtx<'info>>,
req_id: u128,
from_nid: String,
conn_sn: u128,
connection: Pubkey,
) -> Result<()> {
let ix_data = xcall::get_handle_forced_rollback_ix_data(req_id)?;
let ix_data = xcall::get_handle_forced_rollback_ix_data(req_id, from_nid, conn_sn, connection)?;

xcall::call_xcall_handle_forced_rollback(
&ix_data,
Expand Down
5 changes: 4 additions & 1 deletion contracts/solana/programs/mock-dapp-multi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ pub mod mock_dapp_multi {
pub fn execute_forced_rollback<'info>(
ctx: Context<'_, '_, '_, 'info, ExecuteForcedRollbackCtx<'info>>,
req_id: u128,
from_nid: String,
conn_sn: u128,
connection: Pubkey,
) -> Result<()> {
instructions::execute_forced_rollback(ctx, req_id)
instructions::execute_forced_rollback(ctx, req_id, from_nid, conn_sn, connection)
}

#[allow(unused_variables)]
Expand Down
14 changes: 12 additions & 2 deletions contracts/solana/programs/mock-dapp-multi/src/xcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,19 @@ pub fn get_send_call_ix_data(msg: Vec<u8>, to: NetworkAddress) -> Result<Vec<u8>
Ok(ix_data)
}

pub fn get_handle_forced_rollback_ix_data(req_id: u128) -> Result<Vec<u8>> {
pub fn get_handle_forced_rollback_ix_data(
req_id: u128,
from_nid: String,
conn_sn: u128,
connection: Pubkey,
) -> Result<Vec<u8>> {
let mut ix_args_data = vec![];
let ix_args = xcall_type::HandleForcedRollback { req_id };
let ix_args = xcall_type::HandleForcedRollback {
req_id,
from_nid,
conn_sn,
connection,
};
ix_args.serialize(&mut ix_args_data)?;

let ix_data = helpers::get_instruction_data(HANDLE_FORCED_ROLLBACK_IX, ix_args_data);
Expand Down
2 changes: 2 additions & 0 deletions contracts/solana/programs/xcall/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct CallMessage {
pub sn: u128,
pub reqId: u128,
pub data: Vec<u8>,
pub connection: Pubkey,
pub connSn: u128,
}

#[event]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn execute_call<'info>(
}

#[derive(Accounts)]
#[instruction(req_id : u128)]
#[instruction(req_id : u128, from_nid: String, conn_sn: u128, connection: Pubkey)]
pub struct ExecuteCallCtx<'info> {
/// The account that signs and pays for the transaction. This account is mutable
/// because it will be debited for any fees or rent required during the transaction.
Expand Down Expand Up @@ -125,7 +125,7 @@ pub struct ExecuteCallCtx<'info> {
/// calls. The account is closed after use, with any remaining funds sent to the `admin`.
#[account(
mut,
seeds = [ProxyRequest::SEED_PREFIX.as_bytes(), &req_id.to_be_bytes()],
seeds = [ProxyRequest::SEED_PREFIX.as_bytes(), from_nid.as_bytes(), &conn_sn.to_be_bytes(), &connection.to_bytes()],
bump = proxy_request.bump,
close = admin
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn handle_forced_rollback<'info>(
}

#[derive(Accounts)]
#[instruction(req_id: u128)]
#[instruction(req_id: u128, from_nid: String, conn_sn: u128, connection: Pubkey)]
pub struct HandleForcedRollbackCtx<'info> {
/// The account that signs and pays for the transaction. This account is mutable because
/// it will be debited for any fees or rent required during the transaction.
Expand Down Expand Up @@ -111,7 +111,7 @@ pub struct HandleForcedRollbackCtx<'info> {
/// calls and is closed after use, with any remaining funds sent to the `admin`.
#[account(
mut,
seeds = [ProxyRequest::SEED_PREFIX.as_bytes(), &req_id.to_be_bytes()],
seeds = [ProxyRequest::SEED_PREFIX.as_bytes(), from_nid.as_bytes(), &conn_sn.to_be_bytes(), &connection.to_bytes()],
bump = proxy_request.bump,
close = admin
)]
Expand Down
Loading

0 comments on commit d86e9fe

Please sign in to comment.