Skip to content

Commit

Permalink
fix(minor-rewards)!: handle signing rewards for different chains (#253)
Browse files Browse the repository at this point in the history
* fix(minor-rewards)!: handle signing rewards for different chains

The rewards contract can not distinguish signing events for different chains. All signing events are added to a single tally,
since all signing events are handled by one multisig contract.

This pr associates rewards with a (chain name, contract address) pair.

* address comments

* lint and fix test
  • Loading branch information
haiyizxx authored Feb 14, 2024
1 parent f362e07 commit 4f00800
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 292 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 51 additions & 14 deletions contracts/multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {

#[cfg(test)]
mod tests {
use serde_json::from_str;
use std::vec;

use crate::{
Expand All @@ -121,14 +122,13 @@ mod tests {
};

use super::*;
use connection_router::state::ChainName;
use cosmwasm_std::{
from_binary,
testing::{mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage},
Addr, Empty, OwnedDeps, Uint256, WasmMsg,
};

use serde_json::from_str;

const INSTANTIATOR: &str = "inst";
const PROVER: &str = "prover";
const REWARDS_CONTRACT: &str = "rewards";
Expand Down Expand Up @@ -183,6 +183,7 @@ mod tests {
deps: DepsMut,
sender: &str,
worker_set_id: &str,
chain_name: ChainName,
) -> Result<Response, axelar_wasm_std::ContractError> {
let info = mock_info(sender, &[]);
let env = mock_env();
Expand All @@ -191,7 +192,7 @@ mod tests {
let msg = ExecuteMsg::StartSigningSession {
worker_set_id: worker_set_id.to_string(),
msg: message.clone(),
chain_name: "Ethereum".to_string().try_into().unwrap(),
chain_name,
sig_verifier: None,
};
execute(deps, env, info, msg)
Expand Down Expand Up @@ -369,7 +370,12 @@ mod tests {
.into_iter()
.enumerate()
{
let res = do_start_signing_session(deps.as_mut(), PROVER, &subkey);
let res = do_start_signing_session(
deps.as_mut(),
PROVER,
&subkey,
"mock-chain".parse().unwrap(),
);

assert!(res.is_ok());

Expand Down Expand Up @@ -423,7 +429,12 @@ mod tests {
let sender = "someone else";

for worker_set_id in [ecdsa_subkey, ed25519_subkey] {
let res = do_start_signing_session(deps.as_mut(), sender, &worker_set_id);
let res = do_start_signing_session(
deps.as_mut(),
sender,
&worker_set_id,
"mock-chain".parse().unwrap(),
);

assert_eq!(
res.unwrap_err().to_string(),
Expand All @@ -438,16 +449,20 @@ mod tests {
let (mut deps, ecdsa_subkey, ed25519_subkey) = setup();
do_authorize_caller(deps.as_mut(), Addr::unchecked(PROVER)).unwrap();

let chain_name: ChainName = "mock-chain".parse().unwrap();

for (key_type, worker_set_id, signers, session_id) in
signature_test_data(&ecdsa_subkey, &ed25519_subkey)
{
do_start_signing_session(deps.as_mut(), PROVER, worker_set_id).unwrap();
do_start_signing_session(deps.as_mut(), PROVER, worker_set_id, chain_name.clone())
.unwrap();

let signer = signers.get(0).unwrap().to_owned();

let expected_rewards_msg = WasmMsg::Execute {
contract_addr: REWARDS_CONTRACT.to_string(),
msg: to_binary(&rewards::msg::ExecuteMsg::RecordParticipation {
chain_name: chain_name.clone(),
event_id: session_id.to_string().try_into().unwrap(),
worker_address: signer.address.clone().into(),
})
Expand Down Expand Up @@ -505,7 +520,8 @@ mod tests {
for (key_type, subkey, signers, session_id) in
signature_test_data(&ecdsa_subkey, &ed25519_subkey)
{
do_start_signing_session(deps.as_mut(), PROVER, subkey).unwrap();
do_start_signing_session(deps.as_mut(), PROVER, subkey, "mock-chain".parse().unwrap())
.unwrap();

let signer = signers.get(0).unwrap().to_owned();
do_sign(deps.as_mut(), mock_env(), session_id, &signer).unwrap();
Expand Down Expand Up @@ -554,10 +570,12 @@ mod tests {
let (mut deps, ecdsa_subkey, ed25519_subkey) = setup();
do_authorize_caller(deps.as_mut(), Addr::unchecked(PROVER)).unwrap();

let chain_name: ChainName = "mock-chain".parse().unwrap();

for (_key_type, subkey, signers, session_id) in
signature_test_data(&ecdsa_subkey, &ed25519_subkey)
{
do_start_signing_session(deps.as_mut(), PROVER, subkey).unwrap();
do_start_signing_session(deps.as_mut(), PROVER, subkey, chain_name.clone()).unwrap();

let signer = signers.get(0).unwrap().to_owned();
do_sign(deps.as_mut(), mock_env(), session_id, &signer).unwrap();
Expand All @@ -572,6 +590,7 @@ mod tests {
let expected_rewards_msg = WasmMsg::Execute {
contract_addr: REWARDS_CONTRACT.to_string(),
msg: to_binary(&rewards::msg::ExecuteMsg::RecordParticipation {
chain_name: chain_name.clone(),
event_id: session_id.to_string().try_into().unwrap(),
worker_address: signer.address.clone().into(),
})
Expand Down Expand Up @@ -603,7 +622,8 @@ mod tests {
for (_key_type, subkey, signers, session_id) in
signature_test_data(&ecdsa_subkey, &ed25519_subkey)
{
do_start_signing_session(deps.as_mut(), PROVER, subkey).unwrap();
do_start_signing_session(deps.as_mut(), PROVER, subkey, "mock-chain".parse().unwrap())
.unwrap();

let signer = signers.get(0).unwrap().to_owned();
do_sign(deps.as_mut(), mock_env(), session_id, &signer).unwrap();
Expand All @@ -621,7 +641,7 @@ mod tests {
assert_eq!(
res.unwrap_err().to_string(),
axelar_wasm_std::ContractError::from(ContractError::SigningSessionClosed {
session_id: session_id
session_id
})
.to_string()
)
Expand All @@ -632,7 +652,13 @@ mod tests {
fn submit_signature_wrong_session_id() {
let (mut deps, ecdsa_subkey, _) = setup();
do_authorize_caller(deps.as_mut(), Addr::unchecked(PROVER)).unwrap();
do_start_signing_session(deps.as_mut(), PROVER, &ecdsa_subkey).unwrap();
do_start_signing_session(
deps.as_mut(),
PROVER,
&ecdsa_subkey,
"mock-chain".parse().unwrap(),
)
.unwrap();

let invalid_session_id = Uint64::zero();
let signer = ecdsa_test_data::signers().get(0).unwrap().to_owned();
Expand All @@ -655,7 +681,8 @@ mod tests {
for (_key_type, subkey, signers, session_id) in
signature_test_data(&ecdsa_subkey, &ed25519_subkey)
{
do_start_signing_session(deps.as_mut(), PROVER, subkey).unwrap();
do_start_signing_session(deps.as_mut(), PROVER, subkey, "mock-chain".parse().unwrap())
.unwrap();

do_sign(
deps.as_mut(),
Expand Down Expand Up @@ -942,15 +969,25 @@ mod tests {
do_authorize_caller(deps.as_mut(), Addr::unchecked(PROVER)).unwrap();

for worker_set_id in [ecdsa_subkey.clone(), ed25519_subkey.clone()] {
let res = do_start_signing_session(deps.as_mut(), PROVER, &worker_set_id);
let res = do_start_signing_session(
deps.as_mut(),
PROVER,
&worker_set_id,
"mock-chain".parse().unwrap(),
);

assert!(res.is_ok());
}

// unauthorize
do_unauthorize_caller(deps.as_mut(), Addr::unchecked(PROVER)).unwrap();
for worker_set_id in [ecdsa_subkey, ed25519_subkey] {
let res = do_start_signing_session(deps.as_mut(), PROVER, &worker_set_id);
let res = do_start_signing_session(
deps.as_mut(),
PROVER,
&worker_set_id,
"mock-chain".parse().unwrap(),
);

assert_eq!(
res.unwrap_err().to_string(),
Expand Down
25 changes: 15 additions & 10 deletions contracts/multisig/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ pub fn start_signing_session(

let expires_at = env.block.height + config.block_expiry;

let signing_session =
SigningSession::new(session_id, worker_set_id.clone(), msg.clone(), expires_at);
let signing_session = SigningSession::new(
session_id,
worker_set_id.clone(),
chain_name.clone(),
msg.clone(),
expires_at,
);

SIGNING_SESSIONS.save(deps.storage, session_id.into(), &signing_session)?;

Expand Down Expand Up @@ -95,8 +100,7 @@ pub fn submit_signature(
let state_changed = old_state != session.state;

signing_response(
session_id,
session.state,
session,
state_changed,
info.sender,
signature,
Expand Down Expand Up @@ -175,8 +179,7 @@ pub fn require_governance(deps: &DepsMut, sender: Addr) -> Result<(), ContractEr
}

fn signing_response(
session_id: Uint64,
session_state: MultisigState,
session: SigningSession,
state_changed: bool,
signer: Addr,
signature: Signature,
Expand All @@ -185,7 +188,9 @@ fn signing_response(
let rewards_msg = WasmMsg::Execute {
contract_addr: rewards_contract,
msg: to_binary(&rewards::msg::ExecuteMsg::RecordParticipation {
event_id: session_id
chain_name: session.chain_name,
event_id: session
.id
.to_string()
.try_into()
.expect("couldn't convert session_id to nonempty string"),
Expand All @@ -195,7 +200,7 @@ fn signing_response(
};

let event = Event::SignatureSubmitted {
session_id,
session_id: session.id,
participant: signer,
signature,
};
Expand All @@ -204,12 +209,12 @@ fn signing_response(
.add_message(rewards_msg)
.add_event(event.into());

if let MultisigState::Completed { completed_at } = session_state {
if let MultisigState::Completed { completed_at } = session.state {
if state_changed {
// only send event if state changed
response = response.add_event(
Event::SigningCompleted {
session_id,
session_id: session.id,
completed_at,
}
.into(),
Expand Down
29 changes: 24 additions & 5 deletions contracts/multisig/src/signing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use connection_router::state::ChainName;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Uint256, Uint64};

Expand All @@ -14,16 +15,24 @@ use crate::{
pub struct SigningSession {
pub id: Uint64,
pub worker_set_id: String,
pub chain_name: ChainName,
pub msg: MsgToSign,
pub state: MultisigState,
pub expires_at: u64,
}

impl SigningSession {
pub fn new(session_id: Uint64, worker_set_id: String, msg: MsgToSign, expires_at: u64) -> Self {
pub fn new(
session_id: Uint64,
worker_set_id: String,
chain_name: ChainName,
msg: MsgToSign,
expires_at: u64,
) -> Self {
Self {
id: session_id,
worker_set_id,
chain_name,
msg,
state: MultisigState::Pending,
expires_at,
Expand Down Expand Up @@ -113,8 +122,13 @@ mod tests {

let message: MsgToSign = ecdsa_test_data::message().try_into().unwrap();
let expires_at = 12345;
let session =
SigningSession::new(Uint64::one(), worker_set_id, message.clone(), expires_at);
let session = SigningSession::new(
Uint64::one(),
worker_set_id,
"mock-chain".parse().unwrap(),
message.clone(),
expires_at,
);

let signatures: HashMap<String, Signature> = signers
.iter()
Expand Down Expand Up @@ -146,8 +160,13 @@ mod tests {

let message: MsgToSign = ed25519_test_data::message().try_into().unwrap();
let expires_at = 12345;
let session =
SigningSession::new(Uint64::one(), worker_set_id, message.clone(), expires_at);
let session = SigningSession::new(
Uint64::one(),
worker_set_id,
"mock-chain".parse().unwrap(),
message.clone(),
expires_at,
);

let signatures: HashMap<String, Signature> = signers
.iter()
Expand Down
1 change: 1 addition & 0 deletions contracts/rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ optimize = """docker run --rm -v "$(pwd)":/code \
[dependencies]
axelar-wasm-std = { workspace = true }
axelar-wasm-std-derive = { workspace = true }
connection-router = { workspace = true, features = ["library"] }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
Expand Down
Loading

0 comments on commit 4f00800

Please sign in to comment.