Skip to content

Commit

Permalink
check underflow for gas_committed_for_next_epoch before cancalling …
Browse files Browse the repository at this point in the history
…task
  • Loading branch information
nizam-supraoracles authored and Aregnaz Harutyunyan committed Jan 15, 2025
1 parent d011bf8 commit 76f21c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 15 additions & 1 deletion aptos-move/framework/supra-framework/doc/automation_registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,16 @@ The gas committed for next epoch value is overflow after adding new max gas



<a id="0x1_automation_registry_EGAS_COMMITTEED_VALUE_UNDERFLOW"></a>

The gas committed for next epoch value is underflow after remove old max gas


<pre><code><b>const</b> <a href="automation_registry.md#0x1_automation_registry_EGAS_COMMITTEED_VALUE_UNDERFLOW">EGAS_COMMITTEED_VALUE_UNDERFLOW</a>: u64 = 13;
</code></pre>



<a id="0x1_automation_registry_EINVALID_EXPIRY_TIME"></a>

Invalid expiry time: it cannot be earlier than the current time
Expand Down Expand Up @@ -989,7 +999,7 @@ Committed gas-limit is updated by reducing it with the max-gas-amount of the can
<b>let</b> <a href="automation_registry.md#0x1_automation_registry">automation_registry</a> = <b>borrow_global_mut</b>&lt;<a href="automation_registry.md#0x1_automation_registry_AutomationRegistry">AutomationRegistry</a>&gt;(@supra_framework);
<b>assert</b>!(<a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_contains">enumerable_map::contains</a>(&<a href="automation_registry.md#0x1_automation_registry">automation_registry</a>.tasks, id), <a href="automation_registry.md#0x1_automation_registry_EAUTOMATION_TASK_NOT_FOUND">EAUTOMATION_TASK_NOT_FOUND</a>);

<b>let</b> automation_task_metadata = <a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_get_value">enumerable_map::get_value</a>(&<a href="automation_registry.md#0x1_automation_registry">automation_registry</a>.tasks, id);
<b>let</b> automation_task_metadata = <a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_get_value">enumerable_map::get_value</a>(&<b>mut</b> <a href="automation_registry.md#0x1_automation_registry">automation_registry</a>.tasks, id);
<b>assert</b>!(automation_task_metadata.owner == <a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(owner), <a href="automation_registry.md#0x1_automation_registry_EUNAUTHORIZED_TASK_OWNER">EUNAUTHORIZED_TASK_OWNER</a>);
<b>assert</b>!(automation_task_metadata.state != <a href="automation_registry.md#0x1_automation_registry_CANCELLED">CANCELLED</a>, <a href="automation_registry.md#0x1_automation_registry_EALREADY_CANCELLED">EALREADY_CANCELLED</a>);
<b>if</b> (automation_task_metadata.state == <a href="automation_registry.md#0x1_automation_registry_PENDING">PENDING</a>) {
Expand All @@ -999,6 +1009,10 @@ Committed gas-limit is updated by reducing it with the max-gas-amount of the can
automation_task_metadata_mut.state = <a href="automation_registry.md#0x1_automation_registry_CANCELLED">CANCELLED</a>;
};

<b>assert</b>!(
<a href="automation_registry.md#0x1_automation_registry">automation_registry</a>.gas_committed_for_next_epoch &gt;= automation_task_metadata.max_gas_amount,
<a href="automation_registry.md#0x1_automation_registry_EGAS_COMMITTEED_VALUE_UNDERFLOW">EGAS_COMMITTEED_VALUE_UNDERFLOW</a>
);
// Adjust the gas committed for the next epoch by subtracting the gas amount of the cancelled task
<a href="automation_registry.md#0x1_automation_registry">automation_registry</a>.gas_committed_for_next_epoch = <a href="automation_registry.md#0x1_automation_registry">automation_registry</a>.gas_committed_for_next_epoch - automation_task_metadata.max_gas_amount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module supra_framework::automation_registry {
const EALREADY_CANCELLED: u64 = 11;
/// The gas committed for next epoch value is overflow after adding new max gas
const EGAS_COMMITTEED_VALUE_OVERFLOW: u64 = 12;
/// The gas committed for next epoch value is underflow after remove old max gas
const EGAS_COMMITTEED_VALUE_UNDERFLOW: u64 = 13;

/// The default automation task gas limit
const DEFAULT_AUTOMATION_GAS_LIMIT: u64 = 100_000_000;
Expand Down Expand Up @@ -347,7 +349,7 @@ module supra_framework::automation_registry {
let automation_registry = borrow_global_mut<AutomationRegistry>(@supra_framework);
assert!(enumerable_map::contains(&automation_registry.tasks, id), EAUTOMATION_TASK_NOT_FOUND);

let automation_task_metadata = enumerable_map::get_value(&automation_registry.tasks, id);
let automation_task_metadata = enumerable_map::get_value(&mut automation_registry.tasks, id);
assert!(automation_task_metadata.owner == signer::address_of(owner), EUNAUTHORIZED_TASK_OWNER);
assert!(automation_task_metadata.state != CANCELLED, EALREADY_CANCELLED);
if (automation_task_metadata.state == PENDING) {
Expand All @@ -357,6 +359,10 @@ module supra_framework::automation_registry {
automation_task_metadata_mut.state = CANCELLED;
};

assert!(
automation_registry.gas_committed_for_next_epoch >= automation_task_metadata.max_gas_amount,
EGAS_COMMITTEED_VALUE_UNDERFLOW
);
// Adjust the gas committed for the next epoch by subtracting the gas amount of the cancelled task
automation_registry.gas_committed_for_next_epoch = automation_registry.gas_committed_for_next_epoch - automation_task_metadata.max_gas_amount;

Expand Down

0 comments on commit 76f21c7

Please sign in to comment.