Skip to content

Commit

Permalink
feat(minor-service-registry): use coordinator client
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcobb23 committed Sep 10, 2024
1 parent a58c3ac commit 65f8691
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
8 changes: 5 additions & 3 deletions contracts/service-registry/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use axelar_wasm_std::{address, permission_control, FnExt};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Addr, BankMsg, Binary, Coin, Deps, DepsMut, Empty, Env, MessageInfo,
QueryRequest, Response, Storage, WasmQuery,
to_json_binary, Addr, BankMsg, Binary, Coin, Deps, DepsMut, Empty, Env, MessageInfo, Response,
Storage,
};
use error_stack::{bail, Report, ResultExt};

Expand Down Expand Up @@ -190,7 +190,9 @@ mod test {
use cosmwasm_std::testing::{
mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_std::{coins, from_json, CosmosMsg, Empty, OwnedDeps, StdResult, Uint128};
use cosmwasm_std::{
coins, from_json, CosmosMsg, Empty, OwnedDeps, StdResult, Uint128, WasmQuery,
};
use router_api::ChainName;

use super::*;
Expand Down
55 changes: 34 additions & 21 deletions contracts/service-registry/src/contract/execute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use axelar_wasm_std::nonempty;
use error_stack::Result;
use router_api::ChainName;

use super::*;
Expand All @@ -21,7 +22,7 @@ pub fn register_service(
SERVICES.update(
deps.storage,
key,
|service| -> Result<Service, ContractError> {
|service| -> std::result::Result<Service, ContractError> {
match service {
None => Ok(Service {
name: service_name,
Expand Down Expand Up @@ -49,14 +50,15 @@ pub fn update_verifier_authorization_status(
auth_state: AuthorizationState,
) -> Result<Response, ContractError> {
SERVICES
.may_load(deps.storage, &service_name)?
.may_load(deps.storage, &service_name)
.change_context(ContractError::StorageError)?
.ok_or(ContractError::ServiceNotFound)?;

for verifier in verifiers {
VERIFIERS.update(
deps.storage,
(&service_name, &verifier.clone()),
|sw| -> Result<Verifier, ContractError> {
|sw| -> std::result::Result<Verifier, ContractError> {
match sw {
Some(mut verifier) => {
verifier.authorization_state = auth_state.clone();
Expand All @@ -82,7 +84,8 @@ pub fn bond_verifier(
service_name: String,
) -> Result<Response, ContractError> {
let service = SERVICES
.may_load(deps.storage, &service_name)?
.may_load(deps.storage, &service_name)
.change_context(ContractError::StorageError)?
.ok_or(ContractError::ServiceNotFound)?;

let bond: Option<nonempty::Uint128> = if !info.funds.is_empty() {
Expand All @@ -92,7 +95,8 @@ pub fn bond_verifier(
.find(|coin| coin.denom == service.bond_denom)
.ok_or(ContractError::WrongDenom)?
.amount
.try_into()?,
.try_into()
.map_err(ContractError::from)?,
)
} else {
None // sender can rebond currently unbonding funds by just sending no new funds
Expand All @@ -101,7 +105,7 @@ pub fn bond_verifier(
VERIFIERS.update(
deps.storage,
(&service_name.clone(), &info.sender.clone()),
|sw| -> Result<Verifier, ContractError> {
|sw| -> std::result::Result<Verifier, ContractError> {
match sw {
Some(verifier) => Ok(verifier.bond(bond)?),
None => Ok(Verifier {
Expand All @@ -126,7 +130,8 @@ pub fn register_chains_support(
chains: Vec<ChainName>,
) -> Result<Response, ContractError> {
SERVICES
.may_load(deps.storage, &service_name)?
.may_load(deps.storage, &service_name)
.change_context(ContractError::StorageError)?
.ok_or(ContractError::ServiceNotFound)?;

state::register_chains_support(
Expand All @@ -146,7 +151,8 @@ pub fn deregister_chains_support(
chains: Vec<ChainName>,
) -> Result<Response, ContractError> {
SERVICES
.may_load(deps.storage, &service_name)?
.may_load(deps.storage, &service_name)
.change_context(ContractError::StorageError)?
.ok_or(ContractError::ServiceNotFound)?;

state::deregister_chains_support(deps.storage, service_name.clone(), chains, info.sender)?;
Expand All @@ -161,24 +167,27 @@ pub fn unbond_verifier(
service_name: String,
) -> Result<Response, ContractError> {
let service = SERVICES
.may_load(deps.storage, &service_name)?
.may_load(deps.storage, &service_name)
.change_context(ContractError::StorageError)?
.ok_or(ContractError::ServiceNotFound)?;

let verifier = VERIFIERS
.may_load(deps.storage, (&service_name, &info.sender))?
.may_load(deps.storage, (&service_name, &info.sender))
.change_context(ContractError::StorageError)?
.ok_or(ContractError::VerifierNotFound)?;

let query = coordinator::msg::QueryMsg::ReadyToUnbond {
worker_address: verifier.address.clone(),
};
let ready_to_unbond = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: service.coordinator_contract.into(),
msg: to_json_binary(&query)?,
}))?;
let coordinator: coordinator::client::Client =
client::Client::new(deps.querier, &service.coordinator_contract).into();

let ready_to_unbond = coordinator
.ready_to_unbond(verifier.address.clone())
.change_context(ContractError::FailedToUnbondVerifier)?;

let verifier = verifier.unbond(ready_to_unbond, env.block.time)?;

VERIFIERS.save(deps.storage, (&service_name, &info.sender), &verifier)?;
VERIFIERS
.save(deps.storage, (&service_name, &info.sender), &verifier)
.change_context(ContractError::StorageError)?;

Ok(Response::new())
}
Expand All @@ -190,17 +199,21 @@ pub fn claim_stake(
service_name: String,
) -> Result<Response, ContractError> {
let service = SERVICES
.may_load(deps.storage, &service_name)?
.may_load(deps.storage, &service_name)
.change_context(ContractError::StorageError)?
.ok_or(ContractError::ServiceNotFound)?;

let verifier = VERIFIERS
.may_load(deps.storage, (&service_name, &info.sender))?
.may_load(deps.storage, (&service_name, &info.sender))
.change_context(ContractError::StorageError)?
.ok_or(ContractError::VerifierNotFound)?;

let (verifier, released_bond) =
verifier.claim_stake(env.block.time, service.unbonding_period_days as u64)?;

VERIFIERS.save(deps.storage, (&service_name, &info.sender), &verifier)?;
VERIFIERS
.save(deps.storage, (&service_name, &info.sender), &verifier)
.change_context(ContractError::StorageError)?;

Ok(Response::new().add_message(BankMsg::Send {
to_address: info.sender.into(),
Expand Down
9 changes: 9 additions & 0 deletions contracts/service-registry/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ pub enum ContractError {
NotEnoughVerifiers,
#[error("verifier is jailed")]
VerifierJailed,
#[error("failed to unbond verifier")]
FailedToUnbondVerifier,

// Generic error to wrap cw_storage_plus errors
// This should only be used for things that shouldn't happen, such as encountering
// an error when loading data that should load successfully. For errors that can
// happen in the normal course of things, use a more descriptive error
#[error("storage error")]
StorageError,
}

0 comments on commit 65f8691

Please sign in to comment.