From 0ab0efd974fbf8a9256044fb92bf77603ed40402 Mon Sep 17 00:00:00 2001 From: mx Date: Fri, 6 Jan 2023 18:46:45 +0100 Subject: [PATCH] *changed mapping of service to use client address instead of cosmos addr --- .../src/contract.rs | 56 +++++++++++++------ .../service-provider-directory/src/msg.rs | 7 ++- .../service-provider-directory/src/state.rs | 4 +- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/contracts/service-provider-directory/src/contract.rs b/contracts/service-provider-directory/src/contract.rs index 8f7370d30e..f9e15eadf7 100644 --- a/contracts/service-provider-directory/src/contract.rs +++ b/contracts/service-provider-directory/src/contract.rs @@ -39,8 +39,8 @@ pub fn execute( match msg { Announce { client_address, whitelist, owner } => exec::announce(_deps, _info, client_address, whitelist, owner ), - Delete { } => exec::delete(_deps, _info), - // UpdateScore { } => exec::update_score(_deps, _info, ) // TODO once changed mapping from info.sender to client address ¬ + Delete { client_address } => exec::delete(_deps, _info, client_address), // TODO fix in line with the comment + UpdateScore { client_address, new_score } => exec::update_score(_deps, _info, client_address, new_score) // TODO once changed mapping from info.sender to client address ¬ } } @@ -50,7 +50,7 @@ mod exec { pub fn announce( deps: DepsMut, info: MessageInfo, - client_address: Addr, + client_address: String, whitelist: Vec, owner: Addr ) -> Result { @@ -62,21 +62,44 @@ mod exec { owner }; - SERVICES.save(deps.storage, &info.sender, &new_service)?; + SERVICES.save(deps.storage, client_address.clone(), &new_service)?; Ok(Response::new() .add_attribute("action", "service announced") ) } - // delete currently just removes the service mapped to the address of the contract caller - this is assuming a one-service per address model like mix nodes - // TODO change this to a one acct -> many services model + pub fn update_score( + deps: DepsMut, + info: MessageInfo, + client_address: String, + new_score: i8 + ) -> Result { + + let to_update = SERVICES.load(deps.storage, client_address.clone())?; + + // update score & save + + Ok(Response::new() + .add_attribute("action", "service updated") + ) + + } + + /* + * TODO change this so that it requires + * + * - that it comes from the updator role + */ pub fn delete( deps: DepsMut, info: MessageInfo, + client_address: String ) -> Result { - SERVICES.remove(deps.storage, &info.sender); + // ACL: check function call is coming from service.owner, if ! then fail + + SERVICES.remove(deps.storage, client_address.clone()); Ok(Response::new() .add_attribute("action", "service deleted") @@ -100,7 +123,6 @@ mod query { use super::*; - pub fn query_all( deps: Deps, _env: Env, @@ -109,7 +131,7 @@ mod query { .range(deps.storage, None, None, Order::Ascending) .map(|item| { item.map(|(owner, services)| ServicesInfo { - owner: owner.into(), + owner, services }) }) @@ -191,7 +213,7 @@ mod tests { Addr::unchecked("owner"), addr.clone(), &ExecuteMsg::Announce { - client_address: Addr::unchecked("client address"), + client_address: "nymAddress".to_owned(), whitelist: vec!["domain.url".to_owned(), "domain2.url".to_owned()], owner: Addr::unchecked("owner") }, @@ -214,7 +236,7 @@ mod tests { .unwrap(); let test_service: Service = Service { - client_address: Addr::unchecked("client address"), + client_address: "nymAddress".to_string(), whitelist: vec!["domain.url".to_owned(), "domain2.url".to_owned()], owner: Addr::unchecked("owner"), uptime_score: 0 @@ -222,7 +244,7 @@ mod tests { let expected = vec![ ServicesInfo { - owner: Addr::unchecked("owner"), + owner: "nymAddress".to_string(), services: test_service, } ]; @@ -256,7 +278,7 @@ mod tests { Addr::unchecked("owner"), addr.clone(), &ExecuteMsg::Announce { - client_address: Addr::unchecked("client address"), + client_address: "nymAddress".to_string(), whitelist: vec!["domain.url".to_owned(), "domain2.url".to_owned()], owner: Addr::unchecked("owner") }, @@ -269,7 +291,7 @@ mod tests { .unwrap(); let test_service: Service = Service { - client_address: Addr::unchecked("client address"), + client_address: "nymAddress".to_string(), whitelist: vec!["domain.url".to_owned(), "domain2.url".to_owned()], owner: Addr::unchecked("owner"), uptime_score: 0 @@ -277,7 +299,7 @@ mod tests { let expected = vec![ ServicesInfo { - owner: Addr::unchecked("owner"), + owner: "nymAddress".to_string(), services: test_service, } ]; @@ -293,7 +315,9 @@ mod tests { .execute_contract( Addr::unchecked("owner"), addr.clone(), - &ExecuteMsg::Delete { }, + &ExecuteMsg::Delete { + client_address: "nymAddress".to_string() + }, &[], ) .unwrap(); diff --git a/contracts/service-provider-directory/src/msg.rs b/contracts/service-provider-directory/src/msg.rs index cc3234ed67..b43755e6ff 100644 --- a/contracts/service-provider-directory/src/msg.rs +++ b/contracts/service-provider-directory/src/msg.rs @@ -21,13 +21,14 @@ pub struct InstantiateMsg { #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] pub enum ExecuteMsg { - Announce { client_address: Addr, whitelist: Vec, owner: Addr }, - Delete { }, + Announce { client_address: String, whitelist: Vec, owner: Addr }, + Delete { client_address: String }, + UpdateScore { client_address: String, new_score: i8 } } #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] pub struct ServicesInfo { - pub owner: Addr, + pub owner: String, pub services: Service, } diff --git a/contracts/service-provider-directory/src/state.rs b/contracts/service-provider-directory/src/state.rs index b5f41b1e10..58e4a41941 100644 --- a/contracts/service-provider-directory/src/state.rs +++ b/contracts/service-provider-directory/src/state.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] pub struct Service { - pub client_address: Addr, + pub client_address: String, pub whitelist: Vec, pub uptime_score: u8, pub owner: Addr @@ -19,4 +19,4 @@ pub struct Config { // pub const ADMINS: Item> = Item::new("admins"); pub const CONFIG: Item = Item::new("config"); // pub const SERVICES: Item> = Item::new("services"); -pub const SERVICES: Map<&Addr, Service> = Map::new("services"); +pub const SERVICES: Map = Map::new("services");