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

[Automation] Enumerable map enhancements #148

Open
wants to merge 7 commits into
base: feature/automation-networks
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ The lenght of the transaction hash.

// Tasks that are active during next epoch and are not cancled
// current_time shows the start time of the current new epoch.
<b>if</b> (task.state != <a href="automation_registry_state.md#0x1_automation_registry_state_CANCELLED">CANCELLED</a> && task.expiry_time &gt; (current_time + epoch_interval_secs) ) {
<b>if</b> (task.state != <a href="automation_registry_state.md#0x1_automation_registry_state_CANCELLED">CANCELLED</a> && task.expiry_time &gt; (current_time + epoch_interval_secs)) {
gas_committed_for_next_epoch = gas_committed_for_next_epoch + task.max_gas_amount;
};

Expand Down Expand Up @@ -564,7 +564,7 @@ Committed gas-limit is updated by reducing it with the max-gas-amount of the can
<summary>Implementation</summary>


<pre><code><b>public</b> (<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_cancel_task">cancel_task</a>(owner: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, id: u64): <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationTaskMetaData">AutomationTaskMetaData</a> <b>acquires</b> <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a> {
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_cancel_task">cancel_task</a>(owner: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, id: u64): <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationTaskMetaData">AutomationTaskMetaData</a> <b>acquires</b> <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a> {
<b>let</b> state = <b>borrow_global_mut</b>&lt;<a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a>&gt;(@supra_framework);
<b>assert</b>!(<a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_contains">enumerable_map::contains</a>(&state.tasks, id), <a href="automation_registry_state.md#0x1_automation_registry_state_EAUTOMATION_TASK_NOT_FOUND">EAUTOMATION_TASK_NOT_FOUND</a>);

Expand Down Expand Up @@ -607,7 +607,7 @@ If the committed gas amount for the next epoch is greater then the new gas limit
<summary>Implementation</summary>


<pre><code><b>public</b> (<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_update_automation_gas_limit">update_automation_gas_limit</a>(
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_update_automation_gas_limit">update_automation_gas_limit</a>(
supra_framework: &<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
automation_gas_limit: u64
) <b>acquires</b> <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a> {
Expand Down Expand Up @@ -647,16 +647,11 @@ as cancellation takes effect in the next epoch only.
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_get_active_task_ids">get_active_task_ids</a>(): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt; <b>acquires</b> <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a> {
<b>let</b> state = <b>borrow_global</b>&lt;<a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a>&gt;(@supra_framework);

<b>let</b> active_task_ids = <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>[];
<b>let</b> ids = <a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_get_map_list">enumerable_map::get_map_list</a>(&state.tasks);

<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_for_each">vector::for_each</a>(ids, |id| {
<b>let</b> task = <a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_get_value_ref">enumerable_map::get_value_ref</a>(&state.tasks, id);
<b>if</b> (task.state != <a href="automation_registry_state.md#0x1_automation_registry_state_PENDING">PENDING</a>) {
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_push_back">vector::push_back</a>(&<b>mut</b> active_task_ids, id);
};
});
<b>return</b> active_task_ids
<a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_filter_map">enumerable_map::filter_map</a>(&state.tasks, |task| {
<b>let</b> task: <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationTaskMetaData">AutomationTaskMetaData</a> = task; // we need <b>to</b> define task type here <b>to</b> avoid compiler <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error">error</a>
<b>if</b> (task.state != <a href="automation_registry_state.md#0x1_automation_registry_state_PENDING">PENDING</a>) (<b>true</b>, task.id)
<b>else</b> (<b>false</b>, task.id)
})
}
</code></pre>

Expand All @@ -681,7 +676,7 @@ Error will be returned if entry with specified ID does not exist.
<summary>Implementation</summary>


<pre><code><b>public</b> (<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_get_task_details">get_task_details</a>(id: u64): <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationTaskMetaData">AutomationTaskMetaData</a> <b>acquires</b> <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a> {
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_get_task_details">get_task_details</a>(id: u64): <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationTaskMetaData">AutomationTaskMetaData</a> <b>acquires</b> <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a> {
<b>let</b> automation_task_metadata = <b>borrow_global</b>&lt;<a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a>&gt;(@supra_framework);
<b>assert</b>!(<a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_contains">enumerable_map::contains</a>(&automation_task_metadata.tasks, id), <a href="automation_registry_state.md#0x1_automation_registry_state_EAUTOMATION_TASK_NOT_FOUND">EAUTOMATION_TASK_NOT_FOUND</a>);
<a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_get_value">enumerable_map::get_value</a>(&automation_task_metadata.tasks, id)
Expand Down Expand Up @@ -713,7 +708,7 @@ Checks whether there is an active task in registry with specified input task id.
<b>if</b> (<a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_contains">enumerable_map::contains</a>(&automation_task_metadata.tasks, id)) {
<b>let</b> value = <a href="../../supra-stdlib/doc/enumerable_map.md#0x1_enumerable_map_get_value_ref">enumerable_map::get_value_ref</a>(&automation_task_metadata.tasks, id);
value.state != <a href="automation_registry_state.md#0x1_automation_registry_state_PENDING">PENDING</a>
} <b>else</b> {
} <b>else</b> {
<b>false</b>
}
}
Expand All @@ -739,7 +734,7 @@ Returns next task index in registry
<summary>Implementation</summary>


<pre><code><b>public</b> (<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_get_next_task_index">get_next_task_index</a>(): u64 <b>acquires</b> <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a> {
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="automation_registry_state.md#0x1_automation_registry_state_get_next_task_index">get_next_task_index</a>(): u64 <b>acquires</b> <a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a> {
<b>let</b> state = <b>borrow_global</b>&lt;<a href="automation_registry_state.md#0x1_automation_registry_state_AutomationRegistryState">AutomationRegistryState</a>&gt;(@supra_framework);
state.current_index
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ module supra_framework::automation_registry_state {

use std::signer;
use std::vector;
use supra_framework::event;

use supra_std::enumerable_map::{Self, EnumerableMap};

use supra_framework::event;
use supra_framework::system_addresses;
use supra_framework::timestamp;

#[test_only]
use supra_framework::account;

Expand Down Expand Up @@ -126,7 +127,7 @@ module supra_framework::automation_registry_state {

// Tasks that are active during next epoch and are not cancled
// current_time shows the start time of the current new epoch.
if (task.state != CANCELLED && task.expiry_time > (current_time + epoch_interval_secs) ) {
if (task.state != CANCELLED && task.expiry_time > (current_time + epoch_interval_secs)) {
gas_committed_for_next_epoch = gas_committed_for_next_epoch + task.max_gas_amount;
};

Expand Down Expand Up @@ -189,7 +190,7 @@ module supra_framework::automation_registry_state {
/// - pending, it is removed form the list.
/// - cancelled, an error is reported
/// Committed gas-limit is updated by reducing it with the max-gas-amount of the cancelled task.
public (friend) fun cancel_task(owner: &signer, id: u64): AutomationTaskMetaData acquires AutomationRegistryState {
public(friend) fun cancel_task(owner: &signer, id: u64): AutomationTaskMetaData acquires AutomationRegistryState {
let state = borrow_global_mut<AutomationRegistryState>(@supra_framework);
assert!(enumerable_map::contains(&state.tasks, id), EAUTOMATION_TASK_NOT_FOUND);

Expand All @@ -212,7 +213,7 @@ module supra_framework::automation_registry_state {

/// Update Automation gas limit.
/// If the committed gas amount for the next epoch is greater then the new gas limit, then error is reported.
public (friend) fun update_automation_gas_limit(
public(friend) fun update_automation_gas_limit(
supra_framework: &signer,
automation_gas_limit: u64
) acquires AutomationRegistryState {
Expand All @@ -232,21 +233,16 @@ module supra_framework::automation_registry_state {
public(friend) fun get_active_task_ids(): vector<u64> acquires AutomationRegistryState {
let state = borrow_global<AutomationRegistryState>(@supra_framework);

let active_task_ids = vector[];
let ids = enumerable_map::get_map_list(&state.tasks);

vector::for_each(ids, |id| {
let task = enumerable_map::get_value_ref(&state.tasks, id);
if (task.state != PENDING) {
vector::push_back(&mut active_task_ids, id);
};
});
return active_task_ids
enumerable_map::filter_map(&state.tasks, |task| {
let task: AutomationTaskMetaData = task; // we need to define task type here to avoid compiler error
if (task.state != PENDING) (true, task.id)
else (false, task.id)
})
}

/// Retrieves the details of a automation task entry by its ID.
/// Error will be returned if entry with specified ID does not exist.
public (friend) fun get_task_details(id: u64): AutomationTaskMetaData acquires AutomationRegistryState {
public(friend) fun get_task_details(id: u64): AutomationTaskMetaData acquires AutomationRegistryState {
let automation_task_metadata = borrow_global<AutomationRegistryState>(@supra_framework);
assert!(enumerable_map::contains(&automation_task_metadata.tasks, id), EAUTOMATION_TASK_NOT_FOUND);
enumerable_map::get_value(&automation_task_metadata.tasks, id)
Expand All @@ -258,18 +254,19 @@ module supra_framework::automation_registry_state {
if (enumerable_map::contains(&automation_task_metadata.tasks, id)) {
let value = enumerable_map::get_value_ref(&automation_task_metadata.tasks, id);
value.state != PENDING
} else {
} else {
false
}
}

#[test_only]
fun has_task_with_id(id: u64): bool acquires AutomationRegistryState {
let automation_task_metadata = borrow_global<AutomationRegistryState>(@supra_framework);
enumerable_map::contains(&automation_task_metadata.tasks, id)
}

/// Returns next task index in registry
public (friend) fun get_next_task_index(): u64 acquires AutomationRegistryState {
public(friend) fun get_next_task_index(): u64 acquires AutomationRegistryState {
let state = borrow_global<AutomationRegistryState>(@supra_framework);
state.current_index
}
Expand Down Expand Up @@ -335,11 +332,11 @@ module supra_framework::automation_registry_state {
let account = account::create_account_for_test(@0x123456);
register(&account,
PAYLOAD,
100,
10,
20,
1,
PARENT_HASH,
100,
10,
20,
1,
PARENT_HASH,
);
assert!(1 == get_next_task_index(), 1);
assert!(10 == get_gas_committed_for_next_epoch(), 1)
Expand Down
Loading
Loading