Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvja committed Oct 31, 2023
2 parents 446a8f0 + ed47cad commit 4fa5645
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 78 deletions.
5 changes: 2 additions & 3 deletions common/blockchain/src/candidates/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,17 +380,16 @@ impl TestCtx {
fn test(&mut self, pubkey: u8, stake: u8) {
let old_state = self.candidates.clone();
let pubkey = MockPubKey(u32::from(pubkey));
let stake = u128::from(stake);

let this = self as *mut TestCtx;
let res = std::panic::catch_unwind(|| {
// SAFETY: It’s test code. I don’t care. ;) It’s probably safe but
// self.candidates may be in inconsistent state. This is fine since
// we’re panicking anyway.
let this = unsafe { &mut *this };
match u128::from(stake) {
match stake {
0 => this.test_remove(pubkey),
_ => this.test_update(pubkey.clone(), stake),
_ => this.test_update(pubkey.clone(), u128::from(stake)),
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ impl ExecutionContext for IbcStorage<'_, '_> {
let mut store = self.0.borrow_mut();
store
.private
.connection_to_client
.insert(conn_id.to_string(), client_connection_path.0.to_string());
.client_to_connection
.insert(client_connection_path.0.to_string(), conn_id.to_string());
Ok(())
}

Expand Down
58 changes: 23 additions & 35 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub mod solana_ibc {

pub fn deliver(
ctx: Context<Deliver>,
messages: Vec<AnyCheck>,
messages: Vec<ibc::core::MsgEnvelope>,
) -> Result<()> {
msg!("Called deliver method");
let _sender = ctx.accounts.sender.to_account_info();
Expand All @@ -61,35 +61,28 @@ pub mod solana_ibc {
let mut store = IbcStorage(Rc::new(RefCell::new(inner)));
let mut router = store.clone();

let mut errors = Vec::new();
for msg in messages {
let msg = ibc_proto::google::protobuf::Any {
type_url: msg.type_url,
value: msg.value,
};
let res = ibc::core::MsgEnvelope::try_from(msg).and_then(|msg| {
let result =
ibc::core::dispatch(&mut store, &mut router, msg.clone());
if let MsgEnvelope::Packet(packet) = msg {
// store the packet if not exists
// TODO(dhruvja) Store in a PDA with channelId, portId and Sequence
let mut inner_store = store.0.borrow_mut();
match inner_store
.packets
.0
.iter()
.find(|&pack| &packet == pack)
{
Some(_) => (),
None => inner_store.packets.0.push(packet),
}
let errors = messages
.into_iter()
.map(|msg| {
let result = ibc::core::dispatch(&mut store, &mut router, msg.clone());
if let MsgEnvelope::Packet(packet) = msg {
// store the packet if not exists
// TODO(dhruvja) Store in a PDA with channelId, portId and Sequence
let mut inner_store = store.0.borrow_mut();
match inner_store
.packets
.0
.iter()
.find(|&pack| &packet == pack)
{
Some(_) => (),
None => inner_store.packets.0.push(packet),
}
}
result
});
if let Err(err) = res {
errors.push(err);
}
}
})
.filter_map(core::result::Result::err)
.collect::<Vec<_>>();

// Drop refcount on store so we can unwrap the Rc object below.
core::mem::drop(router);
Expand All @@ -107,6 +100,7 @@ pub mod solana_ibc {
Ok(())
}
}

#[derive(Accounts)]
pub struct Deliver<'info> {
#[account(mut)]
Expand All @@ -126,12 +120,6 @@ pub struct EmitIBCEvent {
pub ibc_event: Vec<u8>,
}

#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, PartialEq)]
pub struct AnyCheck {
pub type_url: String,
pub value: Vec<u8>,
}

pub type InnerHeight = (u64, u64);
pub type HostHeight = InnerHeight;
pub type SolanaTimestamp = u64;
Expand Down Expand Up @@ -224,7 +212,7 @@ pub struct PrivateStorage {
pub connections: BTreeMap<InnerConnectionId, InnerConnectionEnd>,
pub channel_ends: BTreeMap<(InnerPortId, InnerChannelId), InnerChannelEnd>,
// Contains the client id corresponding to the connectionId
pub connection_to_client: BTreeMap<InnerConnectionId, InnerClientId>,
pub client_to_connection: BTreeMap<InnerClientId, InnerConnectionId>,
/// The port and channel id tuples of the channels.
pub port_channel_id_set: Vec<(InnerPortId, InnerChannelId)>,
pub channel_counter: u64,
Expand Down
75 changes: 37 additions & 38 deletions solana/solana-ibc/programs/solana-ibc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ use ibc::mock::client_state::MockClientState;
use ibc::mock::consensus_state::MockConsensusState;
use ibc::mock::header::MockHeader;
use ibc_proto::google::protobuf::Any;
use ibc_proto::protobuf::Protobuf;

use crate::{
accounts, instruction, AnyCheck, PrivateStorage, ID, PACKET_SEED,
SOLANA_IBC_STORAGE_SEED, TRIE_SEED,
accounts, instruction, PrivateStorage, ID, SOLANA_IBC_STORAGE_SEED,
TRIE_SEED, PACKET_SEED,
};

const CLIENT_CREATE_CLIENT: &str = "/ibc.core.client.v1.MsgCreateClient";
const CONNECTION_OPEN_INIT: &str =
"/ibc.core.connection.v1.MsgConnectionOpenInit";
const IBC_TRIE_PREFIX: &[u8] = b"ibc/";

fn airdrop(client: &RpcClient, account: Pubkey, lamports: u64) -> Signature {
Expand All @@ -52,6 +48,14 @@ fn create_mock_client_and_cs_state() -> (MockClientState, MockConsensusState) {
(mock_client_state, mock_cs_state)
}

macro_rules! make_message {
($msg:expr, $($variant:path),+ $(,)?) => {{
let message = $msg;
$( let message = $variant(message); )*
message
}}
}

#[test]
#[ignore = "Requires local validator to run"]
fn anchor_test_deliver() -> Result<()> {
Expand Down Expand Up @@ -80,17 +84,15 @@ fn anchor_test_deliver() -> Result<()> {

let (mock_client_state, mock_cs_state) = create_mock_client_and_cs_state();
let _client_id = ClientId::new(mock_client_state.client_type(), 0).unwrap();
let create_client_msg = MsgCreateClient::new(
Any::from(mock_client_state),
Any::from(mock_cs_state),
ibc::Signer::from(authority.pubkey().to_string()),
);
let messages = AnyCheck {
type_url: CLIENT_CREATE_CLIENT.to_string(),
value: create_client_msg.encode_vec(),
};

let all_messages = [messages].to_vec();
let messages = vec![make_message!(
MsgCreateClient::new(
Any::from(mock_client_state),
Any::from(mock_cs_state),
ibc::Signer::from(authority.pubkey().to_string()),
),
ibc::core::ics02_client::msgs::ClientMsg::CreateClient,
ibc::core::MsgEnvelope::Client,
)];

let sig = program
.request()
Expand All @@ -101,7 +103,7 @@ fn anchor_test_deliver() -> Result<()> {
system_program: system_program::ID,
packets,
})
.args(instruction::Deliver { messages: all_messages })
.args(instruction::Deliver { messages })
.payer(authority.clone())
.signer(&*authority)
.send_with_spinner_and_config(RpcSendTransactionConfig {
Expand All @@ -123,25 +125,22 @@ fn anchor_test_deliver() -> Result<()> {
let commitment_prefix: CommitmentPrefix =
IBC_TRIE_PREFIX.to_vec().try_into().unwrap();

let open_init_msg = MsgConnectionOpenInit {
client_id_on_a: ClientId::new(mock_client_state.client_type(), 0)
.unwrap(),
version: Some(Version::default()),
counterparty: Counterparty::new(
counter_party_client_id,
None,
commitment_prefix,
),
delay_period: Duration::from_secs(5),
signer: ibc::Signer::from(authority.pubkey().to_string()),
};

let new_message = AnyCheck {
type_url: CONNECTION_OPEN_INIT.to_string(),
value: open_init_msg.encode_vec(),
};

let all_messages = [new_message].to_vec();
let messages = vec![make_message!(
MsgConnectionOpenInit {
client_id_on_a: ClientId::new(mock_client_state.client_type(), 0)
.unwrap(),
version: Some(Version::default()),
counterparty: Counterparty::new(
counter_party_client_id,
None,
commitment_prefix,
),
delay_period: Duration::from_secs(5),
signer: ibc::Signer::from(authority.pubkey().to_string()),
},
ibc::core::ics03_connection::msgs::ConnectionMsg::OpenInit,
ibc::core::MsgEnvelope::Connection,
)];

let sig = program
.request()
Expand All @@ -152,7 +151,7 @@ fn anchor_test_deliver() -> Result<()> {
system_program: system_program::ID,
packets,
})
.args(instruction::Deliver { messages: all_messages })
.args(instruction::Deliver { messages })
.payer(authority.clone())
.signer(&*authority)
.send_with_spinner_and_config(RpcSendTransactionConfig {
Expand Down

0 comments on commit 4fa5645

Please sign in to comment.