*changed mapping of service to use client address instead of cosmos addr

This commit is contained in:
mx
2023-01-06 18:46:45 +01:00
parent 8b7ec0756e
commit 0ab0efd974
3 changed files with 46 additions and 21 deletions
@@ -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<String>,
owner: Addr
) -> Result<Response, ContractError> {
@@ -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<Response, ContractError> {
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<Response, ContractError> {
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();
@@ -21,13 +21,14 @@ pub struct InstantiateMsg {
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum ExecuteMsg {
Announce { client_address: Addr, whitelist: Vec<String>, owner: Addr },
Delete { },
Announce { client_address: String, whitelist: Vec<String>, 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,
}
@@ -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<String>,
pub uptime_score: u8,
pub owner: Addr
@@ -19,4 +19,4 @@ pub struct Config {
// pub const ADMINS: Item<Vec<Addr>> = Item::new("admins");
pub const CONFIG: Item<Config> = Item::new("config");
// pub const SERVICES: Item<Vec<Service>> = Item::new("services");
pub const SERVICES: Map<&Addr, Service> = Map::new("services");
pub const SERVICES: Map<String, Service> = Map::new("services");