Skip to content

Commit

Permalink
feat(minor-voting-verifier): query poll by id (#505)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Gorenflo <[email protected]>
  • Loading branch information
haiyizxx and cgorenflo authored Jul 16, 2024
1 parent 681af1f commit e4220a6
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 11 deletions.
4 changes: 2 additions & 2 deletions contracts/voting-verifier/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use axelar_wasm_std::{
use multisig::verifier_set::VerifierSet;
use router_api::Message;

use crate::msg::{ExecuteMsg, MessageStatus, Poll, QueryMsg};
use crate::msg::{ExecuteMsg, MessageStatus, PollResponse, QueryMsg};

type Result<T> = error_stack::Result<T, Error>;

Expand Down Expand Up @@ -62,7 +62,7 @@ impl<'a> Client<'a> {
})
}

pub fn poll(&self, poll_id: PollId) -> Result<Poll> {
pub fn poll(&self, poll_id: PollId) -> Result<PollResponse> {
self.client
.query(&QueryMsg::GetPoll { poll_id })
.change_context_lazy(|| Error::QueryVotingVerifier(self.client.address.clone()))
Expand Down
4 changes: 2 additions & 2 deletions contracts/voting-verifier/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub fn execute(
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetPoll { poll_id: _ } => {
todo!()
QueryMsg::GetPoll { poll_id } => {
to_json_binary(&query::poll_response(deps, env.block.height, poll_id)?)
}

QueryMsg::GetMessagesStatus { messages } => {
Expand Down
17 changes: 12 additions & 5 deletions contracts/voting-verifier/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use cosmwasm_schema::{cw_serde, QueryResponses};

use axelar_wasm_std::voting::PollStatus;
use axelar_wasm_std::{
msg_id::MessageIdFormat,
nonempty,
voting::{PollId, Vote},
voting::{PollId, Vote, WeightedPoll},
MajorityThreshold, VerificationStatus,
};
use multisig::verifier_set::VerifierSet;
Expand Down Expand Up @@ -68,15 +69,21 @@ pub enum ExecuteMsg {
}

#[cw_serde]
pub struct Poll {
poll_id: PollId,
messages: Vec<Message>,
pub enum PollData {
Messages(Vec<Message>),
VerifierSet(VerifierSet),
}
#[cw_serde]
pub struct PollResponse {
pub poll: WeightedPoll,
pub data: PollData,
pub status: PollStatus,
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(Poll)]
#[returns(PollResponse)]
GetPoll { poll_id: PollId },

#[returns(Vec<MessageStatus>)]
Expand Down
77 changes: 75 additions & 2 deletions contracts/voting-verifier/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axelar_wasm_std::{
voting::{PollStatus, Vote},
voting::{PollId, PollStatus, Vote},
MajorityThreshold, VerificationStatus,
};
use cosmwasm_std::Deps;
Expand All @@ -11,7 +11,7 @@ use crate::{
state::{poll_messages, poll_verifier_sets, CONFIG},
};
use crate::{
msg::MessageStatus,
msg::{MessageStatus, PollData, PollResponse},
state::{self, Poll, PollContent, POLLS},
};

Expand Down Expand Up @@ -48,6 +48,42 @@ pub fn message_status(
))
}

pub fn poll_response(
deps: Deps,
current_block_height: u64,
poll_id: PollId,
) -> Result<PollResponse, ContractError> {
let poll = POLLS.load(deps.storage, poll_id)?;
let (data, status) = match &poll {
Poll::Messages(poll) => {
let msgs = poll_messages().idx.load_messages(deps.storage, poll_id)?;
assert_eq!(
poll.tallies.len(),
msgs.len(),
"data inconsistency for number of messages in poll {}",
poll.poll_id
);

(PollData::Messages(msgs), poll.status(current_block_height))
}
Poll::ConfirmVerifierSet(poll) => (
PollData::VerifierSet(
poll_verifier_sets()
.idx
.load_verifier_set(deps.storage, poll_id)?
.expect("verifier set not found in poll"),
),
poll.status(current_block_height),
),
};

Ok(PollResponse {
poll: poll.weighted_poll(),
data,
status,
})
}

pub fn verifier_set_status(
deps: Deps,
verifier_set: &VerifierSet,
Expand Down Expand Up @@ -122,7 +158,9 @@ mod tests {
voting::{PollId, Tallies, Vote, WeightedPoll},
Participant, Snapshot, Threshold,
};
use cosmwasm_std::testing::mock_env;
use cosmwasm_std::{testing::mock_dependencies, Addr, Uint128, Uint64};
use itertools::Itertools;
use router_api::CrossChainId;

use crate::state::PollContent;
Expand Down Expand Up @@ -245,6 +283,41 @@ mod tests {
);
}

#[test]
#[allow(clippy::cast_possible_truncation)]
fn poll_response() {
let mut deps = mock_dependencies();

let poll = poll(1);
POLLS
.save(
deps.as_mut().storage,
poll.poll_id,
&state::Poll::Messages(poll.clone()),
)
.unwrap();

let messages = (0..poll.poll_size as u32).map(message);
messages.clone().enumerate().for_each(|(idx, msg)| {
poll_messages()
.save(
deps.as_mut().storage,
&msg.hash(),
&PollContent::<Message>::new(msg, poll.poll_id, idx),
)
.unwrap()
});

assert_eq!(
PollResponse {
poll: poll.clone(),
data: PollData::Messages(messages.collect_vec()),
status: PollStatus::Expired
},
super::poll_response(deps.as_ref(), mock_env().block.height, poll.poll_id).unwrap()
);
}

fn message(id: u32) -> Message {
Message {
cc_id: CrossChainId {
Expand Down
17 changes: 17 additions & 0 deletions contracts/voting-verifier/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ impl Poll {
Poll::ConfirmVerifierSet(poll) => Ok(Poll::ConfirmVerifierSet(func(poll)?)),
}
}

pub fn weighted_poll(self) -> WeightedPoll {
match self {
Poll::Messages(poll) => poll,
Poll::ConfirmVerifierSet(poll) => poll,
}
}
}

#[cw_serde]
Expand Down Expand Up @@ -116,6 +123,16 @@ impl<'a> PollMessagesIndex<'a> {
_ => panic!("More than one message for poll_id and index_in_poll"),
}
}

pub fn load_messages(&self, storage: &dyn Storage, poll_id: PollId) -> StdResult<Vec<Message>> {
poll_messages()
.idx
.0
.sub_prefix(poll_id.to_string())
.range(storage, None, None, Order::Ascending)
.map(|item| item.map(|(_, poll_content)| poll_content.content))
.collect::<StdResult<Vec<_>>>()
}
}

const POLL_MESSAGES_PKEY_NAMESPACE: &str = "poll_messages";
Expand Down

0 comments on commit e4220a6

Please sign in to comment.