From 4fa2334923c1aa5929012cd6d7f940196f628928 Mon Sep 17 00:00:00 2001 From: bishalbikram Date: Thu, 9 May 2024 03:29:23 +0545 Subject: [PATCH] fix: storage getter method --- .../centralized-connection/src/event.rs | 6 +- .../centralized-connection/src/states.rs | 36 ++++++------ .../centralized-connection/src/test.rs | 4 +- .../centralized-connection/src/types.rs | 4 +- .../contracts/mock-dapp-multi/src/state.rs | 27 ++++----- .../contracts/xcall/src/execute_call.rs | 4 +- .../soroban/contracts/xcall/src/helpers.rs | 5 ++ .../xcall/src/messages/cs_message.rs | 13 ++--- .../soroban/contracts/xcall/src/states.rs | 55 +++++++------------ .../contracts/xcall/src/types/request.rs | 5 -- 10 files changed, 66 insertions(+), 93 deletions(-) diff --git a/contracts/soroban/contracts/centralized-connection/src/event.rs b/contracts/soroban/contracts/centralized-connection/src/event.rs index 2942c576..87a0e279 100644 --- a/contracts/soroban/contracts/centralized-connection/src/event.rs +++ b/contracts/soroban/contracts/centralized-connection/src/event.rs @@ -3,6 +3,10 @@ use soroban_sdk::{Bytes, Env, String}; use crate::types::SendMsgEvent; pub(crate) fn send_message(e: &Env, to: String, sn: u128, msg: Bytes) { - let emit_message = SendMsgEvent { to, sn, msg }; + let emit_message = SendMsgEvent { + target_network: to, + conn_sn: sn, + msg, + }; e.events().publish(("EmitMessage",), emit_message); } diff --git a/contracts/soroban/contracts/centralized-connection/src/states.rs b/contracts/soroban/contracts/centralized-connection/src/states.rs index 62d67f75..3559e806 100644 --- a/contracts/soroban/contracts/centralized-connection/src/states.rs +++ b/contracts/soroban/contracts/centralized-connection/src/states.rs @@ -15,35 +15,31 @@ impl CentralizedConnection { } pub fn admin(e: &Env) -> Result { - if let Some(addr) = e.storage().instance().get(&StorageKey::Admin) { - Ok(addr) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::Admin) + .ok_or(ContractError::Uninitialized) } pub fn get_xcall(e: &Env) -> Result { - if let Some(addr) = e.storage().instance().get(&StorageKey::Xcall) { - Ok(addr) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::Xcall) + .ok_or(ContractError::Uninitialized) } pub fn native_token(e: &Env) -> Result { - if let Some(addr) = e.storage().instance().get(&StorageKey::Xlm) { - Ok(addr) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::Xlm) + .ok_or(ContractError::Uninitialized) } pub fn get_conn_sn(e: &Env) -> Result { - if let Some(sn) = e.storage().instance().get(&StorageKey::ConnSn) { - Ok(sn) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::ConnSn) + .ok_or(ContractError::Uninitialized) } pub fn get_next_conn_sn(e: &Env) -> u128 { diff --git a/contracts/soroban/contracts/centralized-connection/src/test.rs b/contracts/soroban/contracts/centralized-connection/src/test.rs index 54459895..b1e89b33 100644 --- a/contracts/soroban/contracts/centralized-connection/src/test.rs +++ b/contracts/soroban/contracts/centralized-connection/src/test.rs @@ -251,8 +251,8 @@ fn test_send_message() { ); let emit_msg = SendMsgEvent { - to: ctx.nid.clone(), - sn: 1_u128, + target_network: ctx.nid.clone(), + conn_sn: 1_u128, msg: msg.clone(), }; let event = vec![&ctx.env, ctx.env.events().all().last_unchecked()]; diff --git a/contracts/soroban/contracts/centralized-connection/src/types.rs b/contracts/soroban/contracts/centralized-connection/src/types.rs index 0397f846..62d3bb32 100644 --- a/contracts/soroban/contracts/centralized-connection/src/types.rs +++ b/contracts/soroban/contracts/centralized-connection/src/types.rs @@ -21,7 +21,7 @@ pub struct InitializeMsg { #[contracttype] pub struct SendMsgEvent { - pub to: String, - pub sn: u128, + pub target_network: String, + pub conn_sn: u128, pub msg: Bytes, } diff --git a/contracts/soroban/contracts/mock-dapp-multi/src/state.rs b/contracts/soroban/contracts/mock-dapp-multi/src/state.rs index c194e0ed..2f7519d1 100644 --- a/contracts/soroban/contracts/mock-dapp-multi/src/state.rs +++ b/contracts/soroban/contracts/mock-dapp-multi/src/state.rs @@ -46,31 +46,24 @@ impl MockDapp { } pub fn get_connections(e: &Env, network_id: String) -> Result, ContractError> { - if let Some(connections) = e - .storage() + e.storage() .instance() .get(&StorageKey::Connections(network_id)) - { - Ok(connections) - } else { - Err(ContractError::ConnectionNotFound) - } + .ok_or(ContractError::ConnectionNotFound) } pub fn get_xcall_address(e: &Env) -> Result { - if let Some(xcall) = e.storage().instance().get(&StorageKey::XcallAddress) { - Ok(xcall) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::XcallAddress) + .ok_or(ContractError::Uninitialized) } pub fn get_sn(e: &Env) -> Result { - if let Some(sn) = e.storage().instance().get(&StorageKey::Sn) { - Ok(sn) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::Sn) + .ok_or(ContractError::Uninitialized) } pub fn get_next_sn(e: &Env) -> Result { diff --git a/contracts/soroban/contracts/xcall/src/execute_call.rs b/contracts/soroban/contracts/xcall/src/execute_call.rs index d55f6b6d..36f62b36 100644 --- a/contracts/soroban/contracts/xcall/src/execute_call.rs +++ b/contracts/soroban/contracts/xcall/src/execute_call.rs @@ -14,7 +14,9 @@ use crate::{ impl Xcall { pub fn execute_message(env: &Env, req_id: u128, data: Bytes) -> Result<(), ContractError> { let req = Self::get_proxy_request(&env, req_id)?; - if req.data() != &req.get_hash_data(&env) { + + let hash_data = Self::hash_data(&env, &data); + if &hash_data != req.data() { return Err(ContractError::DataMismatch); } Self::remove_proxy_request(&env, req_id); diff --git a/contracts/soroban/contracts/xcall/src/helpers.rs b/contracts/soroban/contracts/xcall/src/helpers.rs index 98a70879..64bbc3c8 100644 --- a/contracts/soroban/contracts/xcall/src/helpers.rs +++ b/contracts/soroban/contracts/xcall/src/helpers.rs @@ -49,6 +49,11 @@ impl Xcall { Ok(()) } + pub fn hash_data(e: &Env, data: &Bytes) -> Bytes { + let hash = e.crypto().keccak256(&data); + Bytes::from_array(&e, &hash.to_array()) + } + pub fn is_contract(e: &Env, address: &Address) -> bool { let bytes = address.to_string().to_xdr(&e); let char_index: u32 = bytes.get(SC_VALUE_START_INDEX).unwrap().into(); diff --git a/contracts/soroban/contracts/xcall/src/messages/cs_message.rs b/contracts/soroban/contracts/xcall/src/messages/cs_message.rs index 7d975556..d80da62c 100644 --- a/contracts/soroban/contracts/xcall/src/messages/cs_message.rs +++ b/contracts/soroban/contracts/xcall/src/messages/cs_message.rs @@ -9,14 +9,14 @@ use crate::types::result::CSMessageResult; #[derive(Clone, Copy, Debug)] pub enum CSMessageType { CSMessageRequest = 1, - CSMessageResult = 0, + CSMessageResult = 2, } impl From for u32 { fn from(value: CSMessageType) -> Self { match value { CSMessageType::CSMessageRequest => 1, - CSMessageType::CSMessageResult => 0, + CSMessageType::CSMessageResult => 2, } } } @@ -25,7 +25,7 @@ impl From for CSMessageType { fn from(value: u32) -> Self { match value { 1 => CSMessageType::CSMessageRequest, - 0 => CSMessageType::CSMessageResult, + 2 => CSMessageType::CSMessageResult, _ => panic!("Invalid message type"), } } @@ -76,13 +76,8 @@ impl CSMessage { return Err(ContractError::InvalidRlpLength); } + let message_type = decoder::decode_u32(&env, decoded.get(0).unwrap()).into(); let payload = decoded.get(1).unwrap(); - let msg_type = decoded.get(0).unwrap(); - let message_type = if msg_type.len() > 0 { - decoder::decode_u32(&env, decoded.get(0).unwrap()).into() - } else { - CSMessageType::CSMessageResult - }; Ok(Self { message_type, diff --git a/contracts/soroban/contracts/xcall/src/states.rs b/contracts/soroban/contracts/xcall/src/states.rs index 18bf6f6a..93265c0f 100644 --- a/contracts/soroban/contracts/xcall/src/states.rs +++ b/contracts/soroban/contracts/xcall/src/states.rs @@ -23,28 +23,26 @@ impl Xcall { Ok(()) } } + pub fn admin(e: &Env) -> Result { - if let Some(admin) = e.storage().instance().get(&StorageKey::Admin) { - Ok(admin) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::Admin) + .ok_or(ContractError::Uninitialized) } pub fn get_config(e: &Env) -> Result { - if let Some(config) = e.storage().instance().get(&StorageKey::Config) { - Ok(config) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::Config) + .ok_or(ContractError::Uninitialized) } pub fn get_fee_handler(e: &Env) -> Result { - if let Some(address) = e.storage().instance().get(&StorageKey::FeeHandler) { - Ok(address) - } else { - Err(ContractError::Uninitialized) - } + e.storage() + .instance() + .get(&StorageKey::FeeHandler) + .ok_or(ContractError::Uninitialized) } pub fn protocol_fee(e: &Env) -> u128 { @@ -55,27 +53,17 @@ impl Xcall { } pub fn default_connection(e: &Env, nid: String) -> Result { - if let Some(address) = e - .storage() + e.storage() .instance() .get(&StorageKey::DefaultConnections(nid)) - { - Ok(address) - } else { - Err(ContractError::NoDefaultConnection) - } + .ok_or(ContractError::NoDefaultConnection) } pub fn get_rollback(e: &Env, sequence_no: u128) -> Result { - if let Some(rollback) = e - .storage() + e.storage() .instance() .get(&StorageKey::Rollback(sequence_no)) - { - return rollback; - } else { - Err(ContractError::CallRequestNotFound) - } + .ok_or(ContractError::CallRequestNotFound) } pub fn get_successful_response(e: &Env, sn: u128) -> bool { @@ -94,15 +82,10 @@ impl Xcall { } pub fn get_proxy_request(e: &Env, req_id: u128) -> Result { - if let Some(req) = e - .storage() + e.storage() .instance() .get(&StorageKey::ProxyRequest(req_id)) - { - Ok(req) - } else { - Err(ContractError::InvalidRequestId) - } + .ok_or(ContractError::InvalidRequestId) } pub fn get_reply_state(e: &Env) -> Option { diff --git a/contracts/soroban/contracts/xcall/src/types/request.rs b/contracts/soroban/contracts/xcall/src/types/request.rs index 3a4ad1b8..1adbec0e 100644 --- a/contracts/soroban/contracts/xcall/src/types/request.rs +++ b/contracts/soroban/contracts/xcall/src/types/request.rs @@ -71,11 +71,6 @@ impl CSMessageRequest { self.data = Bytes::from_array(&e, &hash.to_array()) } - pub fn get_hash_data(&self, e: &Env) -> Bytes { - let hash = e.crypto().keccak256(self.data()); - Bytes::from_array(&e, &hash.to_array()) - } - pub fn encode(&self, e: &Env) -> Bytes { let mut list: Vec = Vec::new(&e);