Skip to content

Commit

Permalink
feat(cmc): Increase cycles_limit by 3x. (#3093)
Browse files Browse the repository at this point in the history
Prices recently increased, and users have started running into the
previous limit.

This is a much simpler alternative to (and mutually exclusive with)
#3087
  • Loading branch information
daniel-wong-dfinity-org authored Dec 12, 2024
1 parent 09fa8e6 commit c89c23e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
10 changes: 9 additions & 1 deletion rs/nns/cmc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ const MAX_MEMO_LENGTH: usize = 32;
/// This is the minimum amount needed for creating a canister as of October 2023.
const CREATE_CANISTER_MIN_CYCLES: u64 = 100_000_000_000;

/// Prior to 2024-12-10, we used 50e15, but legitimate users started running
/// into this. At that time, prices had recently gone up, so we resolved to
/// increase this by 3x.
const DEFAULT_CYCLES_LIMIT: u128 = 150e15 as u128;

thread_local! {
static STATE: RefCell<Option<State>> = const { RefCell::new(None) };
static LIMITER_REJECT_COUNT: Cell<u64> = const { Cell::new(0_u64) };
Expand Down Expand Up @@ -336,7 +341,7 @@ impl Default for State {
ICP_XDR_CONVERSION_RATE_CACHE_SIZE
]),
cycles_per_xdr: DEFAULT_CYCLES_PER_XDR.into(),
cycles_limit: 50_000_000_000_000_000u128.into(), // == 50 Pcycles/hour
cycles_limit: Cycles::from(DEFAULT_CYCLES_LIMIT),
limiter: limiter::Limiter::new(resolution, max_age),
total_cycles_minted: Cycles::zero(),
blocks_notified: BTreeMap::new(),
Expand Down Expand Up @@ -2279,6 +2284,9 @@ fn post_upgrade(maybe_args: Option<CyclesCanisterInitPayload>) {
new_state.cycles_ledger_canister_id = args.cycles_ledger_canister_id;
}

// Delete after release.
new_state.cycles_limit = Cycles::new(DEFAULT_CYCLES_LIMIT);

STATE.with(|state| state.replace(Some(new_state)));
}

Expand Down
25 changes: 14 additions & 11 deletions rs/nns/integration_tests/src/cycles_minting_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1413,41 +1413,44 @@ fn cmc_notify_top_up_invalid() {

#[test]
fn cmc_notify_top_up_rate_limited() {
let account = AccountIdentifier::new(*TEST_USER1_PRINCIPAL, None);
let icpts = Tokens::new(1_000, 0).unwrap();
let state_machine = state_machine_builder_for_nns_tests().build();

let account = AccountIdentifier::new(*TEST_USER1_PRINCIPAL, None);
// The only requirement here is to have sufficient funds. Other than that,
// the precise number here does not matter.
let balance = Tokens::new(1e6 as u64, 0).unwrap();
let nns_init_payloads = NnsInitPayloadsBuilder::new()
.with_test_neurons()
.with_ledger_account(account, icpts)
.with_ledger_account(account, balance)
.build();
setup_nns_canisters(&state_machine, nns_init_payloads);

// First top-up should succeed since it's 30P - less than the 50P/hr limit.
// First top-up should succeed since it's 90P - less than the 150P/hr limit.
let cycles = notify_top_up(
&state_machine,
GOVERNANCE_CANISTER_ID,
Tokens::new(300, 0).unwrap(),
Tokens::new(900, 0).unwrap(),
)
.unwrap();
assert_eq!(cycles, Cycles::new(30_000_000_000_000_000u128));
assert_eq!(cycles, Cycles::new(90e15 as u128));

// Second top-up should also succeed after 1 hour.
state_machine.advance_time(Duration::from_secs(4000));
let cycles = notify_top_up(
&state_machine,
GOVERNANCE_CANISTER_ID,
Tokens::new(300, 0).unwrap(),
Tokens::new(900, 0).unwrap(),
)
.unwrap();
assert_eq!(cycles, Cycles::new(30_000_000_000_000_000u128));
assert_eq!(cycles, Cycles::new(90e15 as u128));

// Third top-up should fail since the rate limit is 50P cycles per hour, and less than an hour
// has passed.
// Third top-up should fail since the rate limit is 150e15 cycles per hour,
// and less than an hour has passed.
state_machine.advance_time(Duration::from_secs(3000));
let error = notify_top_up(
&state_machine,
GOVERNANCE_CANISTER_ID,
Tokens::new(300, 0).unwrap(),
Tokens::new(900, 0).unwrap(),
)
.unwrap_err();
assert_matches!(error, NotifyError::Refunded { reason, .. } if reason.contains("try again later"));
Expand Down

0 comments on commit c89c23e

Please sign in to comment.