Skip to content

Commit 3acc2e7

Browse files
committed
implement fix for issue 1
1 parent c91007b commit 3acc2e7

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

examples/oft-evm-move-adapters/sources/shared_oft/oft_impl_config.move

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ module oft::oft_impl_config {
213213

214214
/// Checkpoint the in-flight amount for a given EID for the provided timestamp.
215215
/// This should whenever there is a change in rate limit or before consuming rate limit capacity
216-
public(friend) fun checkpoint_rate_limit_in_flight(eid: u32, timestamp: u64) acquires Config {
216+
public fun checkpoint_rate_limit_in_flight(eid: u32, timestamp: u64) acquires Config {
217217
let inflight = in_flight_at_time(eid, timestamp);
218218
let rate_limit = table::borrow_mut(&mut store_mut().rate_limit_by_eid, eid);
219219
rate_limit.in_flight_on_last_update = inflight;
@@ -308,6 +308,9 @@ module oft::oft_impl_config {
308308
public(friend) fun release_rate_limit_capacity(eid: u32, amount: u64) acquires Config {
309309
if (!has_rate_limit(eid)) return;
310310

311+
// Checkpoint to account for decay before modifying in_flight_on_last_update
312+
checkpoint_rate_limit_in_flight(eid, timestamp::now_seconds());
313+
311314
let rate_limit = table::borrow_mut(&mut store_mut().rate_limit_by_eid, eid);
312315
if (amount >= rate_limit.in_flight_on_last_update) {
313316
rate_limit.in_flight_on_last_update = 0;

examples/oft-evm-move-adapters/tests/shared_oft/oft_impl_config_tests.move

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#[test_only]
22
module oft::oft_impl_config_tests {
3-
use std::account::create_account_for_test;
3+
use std::account::{create_account_for_test, create_signer_for_test};
44
use std::event::was_event_emitted;
55
use std::string::utf8;
6+
use std::timestamp;
67
use std::vector;
78

89
use oft::oapp_store;
@@ -12,6 +13,7 @@ module oft::oft_impl_config_tests {
1213
assert_not_blocklisted,
1314
blocked_amount_redirected_event,
1415
blocklisting_disabled_event,
16+
checkpoint_rate_limit_in_flight,
1517
debit_view_with_possible_fee,
1618
fee_bps,
1719
fee_details_with_possible_fee,
@@ -279,6 +281,7 @@ module oft::oft_impl_config_tests {
279281
#[test]
280282
fun test_set_rate_limit_net() {
281283
setup();
284+
timestamp::set_time_has_started_for_testing(&create_signer_for_test(@std));
282285

283286
// No rate limit configured
284287
assert!(has_rate_limit(30100) == false, 2);
@@ -300,7 +303,10 @@ module oft::oft_impl_config_tests {
300303
assert!(in_flight_at_time(30100, 500) == 10000, 1);
301304
assert!(rate_limit_capacity_at_time(30100, 500) == 10000, 2);
302305

303-
// release most of remaining capacity
306+
// Release most of remaining capacity
307+
// release_rate_limit_capacity will checkpoint at now_seconds() (which is 0 after initialization)
308+
// At time 0: in_flight = 20000, so after release of 9000: in_flight_on_last_update = 11000, last_update = 0
309+
// At time 500: in_flight = 11000 - (500 * 20000 / 1000) = 11000 - 10000 = 1000
304310
release_rate_limit_capacity(30100, 9000);
305311
assert!(in_flight_at_time(30100, 500) == 1000, 1);
306312
assert!(rate_limit_capacity_at_time(30100, 500) == 19000, 2);
@@ -310,7 +316,9 @@ module oft::oft_impl_config_tests {
310316
assert!(in_flight_at_time(30100, 500) == 20000, 1);
311317
assert!(rate_limit_capacity_at_time(30100, 500) == 0, 2);
312318

313-
// release excess capacity (5x limit) - should not overshoot
319+
// Release excess capacity (5x limit) - should not overshoot
320+
// First checkpoint at 500 to get correct state, then release will checkpoint at now_seconds()
321+
checkpoint_rate_limit_in_flight(30100, 500);
314322
release_rate_limit_capacity(30100, 100_000);
315323
assert!(in_flight_at_time(30100, 500) == 0, 1);
316324
assert!(rate_limit_capacity_at_time(30100, 500) == 20000, 2);

0 commit comments

Comments
 (0)