Skip to content

Commit

Permalink
refactor(minor-service-registry)!: refactor weights (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcobb23 authored Mar 16, 2024
1 parent a8a7df7 commit 1b76958
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 47 deletions.
17 changes: 6 additions & 11 deletions contracts/multisig-prover/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use multisig::{key::PublicKey, msg::Signer, worker_set::WorkerSet};

use axelar_wasm_std::{snapshot, VerificationStatus};
use connection_router_api::{ChainName, CrossChainId, Message};
use service_registry::state::{WeightedWorker, Worker};
use service_registry::state::WeightedWorker;

use crate::{
contract::START_MULTISIG_REPLY_ID,
Expand Down Expand Up @@ -113,30 +113,25 @@ fn get_workers_info(deps: &DepsMut, config: &Config) -> Result<WorkersInfo, Cont
chain_name: config.chain_name.clone(),
};

let weighted_workers: Vec<WeightedWorker> =
let workers: Vec<WeightedWorker> =
deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: config.service_registry.to_string(),
msg: to_json_binary(&active_workers_query)?,
}))?;

let workers: Vec<Worker> = weighted_workers
.into_iter()
.map(|weighted_worker| weighted_worker.worker)
.collect();

let participants = workers
.clone()
.into_iter()
.map(service_registry::state::Worker::try_into)
.collect::<Result<Vec<snapshot::Participant>, _>>()?;
.map(service_registry::state::WeightedWorker::into)
.collect::<Vec<snapshot::Participant>>();

let snapshot =
snapshot::Snapshot::new(config.signing_threshold, participants.clone().try_into()?);

let mut pub_keys = vec![];
for worker in &workers {
for participant in &participants {
let pub_key_query = multisig::msg::QueryMsg::GetPublicKey {
worker_address: worker.address.to_string(),
worker_address: participant.address.to_string(),
key_type: config.key_type,
};
let pub_key: PublicKey = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
let workers = get_operators(deps)
.into_iter()
.map(|op| WeightedWorker {
worker: Worker {
worker_info: Worker {
address: op.address,
bonding_state: BondingState::Bonded {
amount: op.weight.try_into().unwrap(),
Expand Down
6 changes: 2 additions & 4 deletions contracts/service-registry/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractErr
pub mod query {
use connection_router_api::ChainName;

use crate::state::{
AuthorizationState, WeightedWorker, WORKERS, WORKERS_PER_CHAIN, WORKER_WEIGHT,
};
use crate::state::{WeightedWorker, WORKERS, WORKERS_PER_CHAIN, WORKER_WEIGHT};

use super::*;

Expand All @@ -400,7 +398,7 @@ pub mod query {
})
.filter(|worker| worker.authorization_state == AuthorizationState::Authorized)
.map(|worker| WeightedWorker {
worker,
worker_info: worker,
weight: WORKER_WEIGHT, // all workers have an identical const weight for now
})
.collect();
Expand Down
27 changes: 8 additions & 19 deletions contracts/service-registry/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cosmwasm_schema::cw_serde;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{Addr, Timestamp, Uint128, Uint256};
use cosmwasm_std::{Addr, Timestamp, Uint128};
use cw_storage_plus::{Item, Map};

#[cw_serde]
Expand Down Expand Up @@ -39,29 +39,18 @@ pub struct Worker {

#[cw_serde]
pub struct WeightedWorker {
pub worker: Worker,
pub worker_info: Worker,
pub weight: nonempty::Uint256,
}

/// For now, all workers have equal weight, regardless of amount bonded
pub const WORKER_WEIGHT: nonempty::Uint256 = nonempty::Uint256::one();

impl TryFrom<Worker> for Participant {
type Error = ContractError;

// TODO: change this to accept WeightedWorker and just extract the weight
fn try_from(worker: Worker) -> Result<Participant, ContractError> {
match worker.bonding_state {
BondingState::Bonded { amount: _ } => Ok(Self {
address: worker.address,
// Weight is set to one to ensure all workers have same weight. In the future, it should be derived from amount bonded
// If the weight is changed to a non-constant value, the signing session completed event from multisig and the signature
// optimization during proof construction may require re-evaluation, so that relayers could take advantage of late
// signatures to get a more optimized version of the proof.
weight: Uint256::one()
.try_into()
.expect("violated invariant: weight must not be zero"),
}),
_ => Err(ContractError::InvalidBondingState(worker.bonding_state)),
impl From<WeightedWorker> for Participant {
fn from(worker: WeightedWorker) -> Participant {
Self {
weight: worker.weight,
address: worker.worker_info.address,
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions contracts/service-registry/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn register_chain_support() {
assert_eq!(
workers,
vec![WeightedWorker {
worker: Worker {
worker_info: Worker {
address: worker,
bonding_state: BondingState::Bonded {
amount: min_worker_bond
Expand Down Expand Up @@ -541,7 +541,7 @@ fn register_for_multiple_chains_deregister_for_first_one() {
assert_eq!(
workers,
vec![WeightedWorker {
worker: Worker {
worker_info: Worker {
address: worker.clone(),
bonding_state: BondingState::Bonded {
amount: min_worker_bond
Expand Down Expand Up @@ -643,7 +643,7 @@ fn register_support_for_a_chain_deregister_support_for_another_chain() {
assert_eq!(
workers,
vec![WeightedWorker {
worker: Worker {
worker_info: Worker {
address: worker,
bonding_state: BondingState::Bonded {
amount: min_worker_bond
Expand Down Expand Up @@ -753,7 +753,7 @@ fn register_deregister_register_support_for_single_chain() {
assert_eq!(
workers,
vec![WeightedWorker {
worker: Worker {
worker_info: Worker {
address: worker,
bonding_state: BondingState::Bonded {
amount: min_worker_bond
Expand Down Expand Up @@ -1348,7 +1348,7 @@ fn bond_before_authorize() {
assert_eq!(
workers,
vec![WeightedWorker {
worker: Worker {
worker_info: Worker {
address: worker,
bonding_state: BondingState::Bonded {
amount: min_worker_bond
Expand Down Expand Up @@ -1453,7 +1453,7 @@ fn unbond_then_rebond() {
assert_eq!(
workers,
vec![WeightedWorker {
worker: Worker {
worker_info: Worker {
address: worker,
bonding_state: BondingState::Bonded {
amount: min_worker_bond
Expand Down
2 changes: 1 addition & 1 deletion contracts/voting-verifier/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ mod test {
&workers()
.into_iter()
.map(|w| WeightedWorker {
worker: w,
worker_info: w,
weight: WORKER_WEIGHT,
})
.collect::<Vec<WeightedWorker>>(),
Expand Down
9 changes: 4 additions & 5 deletions contracts/voting-verifier/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,16 @@ fn take_snapshot(deps: Deps, chain: &ChainName) -> Result<snapshot::Snapshot, Co
chain_name: chain.clone(),
};

let weighted_workers: Vec<WeightedWorker> =
let workers: Vec<WeightedWorker> =
deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: config.service_registry_contract.to_string(),
msg: to_json_binary(&active_workers_query)?,
}))?;

let participants = weighted_workers
let participants = workers
.into_iter()
.map(|weighted_worker| weighted_worker.worker)
.map(service_registry::state::Worker::try_into)
.collect::<Result<Vec<snapshot::Participant>, _>>()?;
.map(service_registry::state::WeightedWorker::into)
.collect::<Vec<snapshot::Participant>>();

Ok(snapshot::Snapshot::new(
config.voting_threshold,
Expand Down

0 comments on commit 1b76958

Please sign in to comment.