Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBC rate limiting: Updated the contract to cosmwasm 1.1 and Uint256 for amounts #2950

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions x/ibc-rate-limit/contracts/rate-limiter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ exclude = [
[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
Expand All @@ -43,14 +32,14 @@ optimize = """docker run --rm -v "$(pwd)":/code \
"""

[dependencies]
cosmwasm-std = "1.0.0"
cosmwasm-storage = "1.0.0"
cosmwasm-std = "1.1.0"
cosmwasm-storage = "1.1.0"
cosmwasm-schema = "1.1.0"
cw-storage-plus = "0.13.2"
cw2 = "0.13.2"
schemars = "0.8.8"
serde = { version = "1.0.137", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.31" }

[dev-dependencies]
cosmwasm-schema = "1.0.0"
cw-multi-test = "0.13.2"
13 changes: 13 additions & 0 deletions x/ibc-rate-limit/contracts/rate-limiter/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use cosmwasm_schema::write_api;

use rate_limiter::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, SudoMsg};

fn main() {
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,
sudo: SudoMsg,
migrate: MigrateMsg,
}
}
71 changes: 37 additions & 34 deletions x/ibc-rate-limit/contracts/rate-limiter/src/contract_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{contract::*, ContractError};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{from_binary, Addr, Attribute};
use cosmwasm_std::{from_binary, Addr, Attribute, Uint256};

use crate::helpers::tests::verify_query_response;
use crate::msg::{InstantiateMsg, PathMsg, QueryMsg, QuotaMsg, SudoMsg};
Expand Down Expand Up @@ -52,8 +52,8 @@ fn consume_allowance() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let res = sudo(deps.as_mut(), mock_env(), msg).unwrap();

Expand All @@ -64,8 +64,8 @@ fn consume_allowance() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let err = sudo(deps.as_mut(), mock_env(), msg).unwrap_err();
assert!(matches!(err, ContractError::RateLimitExceded { .. }));
Expand All @@ -91,14 +91,14 @@ fn symetric_flows_dont_consume_allowance() {
let send_msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let recv_msg = SudoMsg::RecvPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};

let res = sudo(deps.as_mut(), mock_env(), send_msg.clone()).unwrap();
Expand Down Expand Up @@ -154,8 +154,8 @@ fn asymetric_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 60,
channel_value: 3_000_u32.into(),
funds: 60_u32.into(),
};
let res = sudo(deps.as_mut(), mock_env(), msg).unwrap();
let Attribute { key, value } = &res.attributes[4];
Expand All @@ -166,8 +166,8 @@ fn asymetric_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 60,
channel_value: 3_000_u32.into(),
funds: 60_u32.into(),
};

let res = sudo(deps.as_mut(), mock_env(), msg).unwrap();
Expand All @@ -180,8 +180,8 @@ fn asymetric_quotas() {
let recv_msg = SudoMsg::RecvPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 30,
channel_value: 3_000_u32.into(),
funds: 30_u32.into(),
};
let res = sudo(deps.as_mut(), mock_env(), recv_msg).unwrap();
let Attribute { key, value } = &res.attributes[3];
Expand All @@ -195,8 +195,8 @@ fn asymetric_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 60,
channel_value: 3_000_u32.into(),
funds: 60_u32.into(),
};
let err = sudo(deps.as_mut(), mock_env(), msg.clone()).unwrap_err();
assert!(matches!(err, ContractError::RateLimitExceded { .. }));
Expand All @@ -205,8 +205,8 @@ fn asymetric_quotas() {
let msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 30,
channel_value: 3_000_u32.into(),
funds: 30_u32.into(),
};
let res = sudo(deps.as_mut(), mock_env(), msg.clone()).unwrap();
let Attribute { key, value } = &res.attributes[3];
Expand Down Expand Up @@ -246,8 +246,8 @@ fn query_state() {
assert_eq!(value[0].quota.max_percentage_send, 10);
assert_eq!(value[0].quota.max_percentage_recv, 10);
assert_eq!(value[0].quota.duration, RESET_TIME_WEEKLY);
assert_eq!(value[0].flow.inflow, 0);
assert_eq!(value[0].flow.outflow, 0);
assert_eq!(value[0].flow.inflow, Uint256::from(0_u32));
assert_eq!(value[0].flow.outflow, Uint256::from(0_u32));
assert_eq!(
value[0].flow.period_end,
env.block.time.plus_seconds(RESET_TIME_WEEKLY)
Expand All @@ -256,16 +256,16 @@ fn query_state() {
let send_msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
sudo(deps.as_mut(), mock_env(), send_msg.clone()).unwrap();

let recv_msg = SudoMsg::RecvPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 30,
channel_value: 3_000_u32.into(),
funds: 30_u32.into(),
};
sudo(deps.as_mut(), mock_env(), recv_msg.clone()).unwrap();

Expand All @@ -277,8 +277,8 @@ fn query_state() {
"weekly",
(10, 10),
RESET_TIME_WEEKLY,
30,
300,
30_u32.into(),
300_u32.into(),
env.block.time.plus_seconds(RESET_TIME_WEEKLY),
);
}
Expand Down Expand Up @@ -317,8 +317,8 @@ fn bad_quotas() {
"bad_quota",
(100, 100),
200,
0,
0,
0_u32.into(),
0_u32.into(),
env.block.time.plus_seconds(200),
);
}
Expand All @@ -343,21 +343,24 @@ fn undo_send() {
let send_msg = SudoMsg::SendPacket {
channel_id: format!("channel"),
denom: format!("denom"),
channel_value: 3_000,
funds: 300,
channel_value: 3_000_u32.into(),
funds: 300_u32.into(),
};
let undo_msg = SudoMsg::UndoSend {
channel_id: format!("channel"),
denom: format!("denom"),
funds: 300,
funds: 300_u32.into(),
};

sudo(deps.as_mut(), mock_env(), send_msg.clone()).unwrap();

let trackers = RATE_LIMIT_TRACKERS
.load(&deps.storage, ("channel".to_string(), "denom".to_string()))
.unwrap();
assert_eq!(trackers.first().unwrap().flow.outflow, 300);
assert_eq!(
trackers.first().unwrap().flow.outflow,
Uint256::from(300_u32)
);
let period_end = trackers.first().unwrap().flow.period_end;
let channel_value = trackers.first().unwrap().quota.channel_value;

Expand All @@ -366,7 +369,7 @@ fn undo_send() {
let trackers = RATE_LIMIT_TRACKERS
.load(&deps.storage, ("channel".to_string(), "denom".to_string()))
.unwrap();
assert_eq!(trackers.first().unwrap().flow.outflow, 0);
assert_eq!(trackers.first().unwrap().flow.outflow, Uint256::from(0_u32));
assert_eq!(trackers.first().unwrap().flow.period_end, period_end);
assert_eq!(trackers.first().unwrap().quota.channel_value, channel_value);
}
12 changes: 6 additions & 6 deletions x/ibc-rate-limit/contracts/rate-limiter/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ mod tests {
"daily",
(3, 5),
1600,
0,
0,
0_u32.into(),
0_u32.into(),
env.block.time.plus_seconds(1600),
);

Expand Down Expand Up @@ -208,8 +208,8 @@ mod tests {
"daily",
(3, 5),
1600,
0,
0,
0_u32.into(),
0_u32.into(),
env.block.time.plus_seconds(1600),
);

Expand Down Expand Up @@ -241,8 +241,8 @@ mod tests {
"different",
(50, 30),
5000,
0,
0,
0_u32.into(),
0_u32.into(),
env.block.time.plus_seconds(5000),
);
}
Expand Down
6 changes: 3 additions & 3 deletions x/ibc-rate-limit/contracts/rate-limiter/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl RateLimitingContract {
}

pub mod tests {
use cosmwasm_std::Timestamp;
use cosmwasm_std::{Timestamp, Uint256};

use crate::state::RateLimit;

Expand All @@ -46,8 +46,8 @@ pub mod tests {
quota_name: &str,
send_recv: (u32, u32),
duration: u64,
inflow: u128,
outflow: u128,
inflow: Uint256,
outflow: Uint256,
period_end: Timestamp,
) {
assert_eq!(value.quota.name, quota_name);
Expand Down
Loading