Skip to content

Commit

Permalink
fix: storage getter method
Browse files Browse the repository at this point in the history
  • Loading branch information
bishalbikram committed May 8, 2024
1 parent f97ac2d commit 4fa2334
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
36 changes: 16 additions & 20 deletions contracts/soroban/contracts/centralized-connection/src/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,31 @@ impl CentralizedConnection {
}

pub fn admin(e: &Env) -> Result<Address, ContractError> {
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<Address, ContractError> {
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<Address, ContractError> {
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<u128, ContractError> {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
27 changes: 10 additions & 17 deletions contracts/soroban/contracts/mock-dapp-multi/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,24 @@ impl MockDapp {
}

pub fn get_connections(e: &Env, network_id: String) -> Result<Vec<Connection>, 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<Address, ContractError> {
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<u128, ContractError> {
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<u128, ContractError> {
Expand Down
4 changes: 3 additions & 1 deletion contracts/soroban/contracts/xcall/src/execute_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions contracts/soroban/contracts/xcall/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 4 additions & 9 deletions contracts/soroban/contracts/xcall/src/messages/cs_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use crate::types::result::CSMessageResult;
#[derive(Clone, Copy, Debug)]
pub enum CSMessageType {
CSMessageRequest = 1,
CSMessageResult = 0,
CSMessageResult = 2,
}

impl From<CSMessageType> for u32 {
fn from(value: CSMessageType) -> Self {
match value {
CSMessageType::CSMessageRequest => 1,
CSMessageType::CSMessageResult => 0,
CSMessageType::CSMessageResult => 2,
}
}
}
Expand All @@ -25,7 +25,7 @@ impl From<u32> for CSMessageType {
fn from(value: u32) -> Self {
match value {
1 => CSMessageType::CSMessageRequest,
0 => CSMessageType::CSMessageResult,
2 => CSMessageType::CSMessageResult,
_ => panic!("Invalid message type"),
}
}
Expand Down Expand Up @@ -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,
Expand Down
55 changes: 19 additions & 36 deletions contracts/soroban/contracts/xcall/src/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,26 @@ impl Xcall {
Ok(())
}
}

pub fn admin(e: &Env) -> Result<Address, ContractError> {
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<Config, ContractError> {
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<Address, ContractError> {
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 {
Expand All @@ -55,27 +53,17 @@ impl Xcall {
}

pub fn default_connection(e: &Env, nid: String) -> Result<Address, ContractError> {
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<Rollback, ContractError> {
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 {
Expand All @@ -94,15 +82,10 @@ impl Xcall {
}

pub fn get_proxy_request(e: &Env, req_id: u128) -> Result<CSMessageRequest, ContractError> {
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<CSMessageRequest> {
Expand Down
5 changes: 0 additions & 5 deletions contracts/soroban/contracts/xcall/src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bytes> = Vec::new(&e);

Expand Down

0 comments on commit 4fa2334

Please sign in to comment.