Skip to content

Commit

Permalink
feat(iota-framework): store system display objects in the system state
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriyr committed Nov 5, 2024
1 parent cff774d commit 0fa27f6
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,39 @@ module iota_system::iota_system {
self.active_validator_addresses()
}

/// Returns the IOTA system admin capability reference.
public(package) fun load_iota_system_admin_cap(self: &mut IotaSystemState): &IotaSystemAdminCap {
self.load_system_state().iota_system_admin_cap()
}

/// Add an object with the specified key to the extra fields collection.
public(package) fun add_extra_field<K: copy + drop + store, V: store>(
wrapper: &mut IotaSystemState,
key: K,
value: V,
) {
let self = load_system_state_mut(wrapper);
self.add_extra_field(key, value);
}

/// Immutable borrows the value associated with the key in the extra fields.
public(package) fun borrow_extra_field<K: copy + drop + store, V: store>(
wrapper: &mut IotaSystemState,
key: K,
): &V {
let self = load_system_state(wrapper);
self.borrow_extra_field(key)
}

/// Mutable borrows the value associated with the key in the extra fields.
public(package) fun borrow_extra_field_mut<K: copy + drop + store, V: store>(
wrapper: &mut IotaSystemState,
key: K,
): &mut V {
let self = load_system_state_mut(wrapper);
self.borrow_extra_field_mut(key)
}

#[allow(unused_function)]
/// This function should be called at the end of an epoch, and advances the system to the next epoch.
/// It does the following things:
Expand Down Expand Up @@ -560,10 +593,6 @@ module iota_system::iota_system {
inner
}

public(package) fun load_iota_system_admin_cap(self: &mut IotaSystemState): &IotaSystemAdminCap {
self.load_system_state().iota_system_admin_cap()
}

#[allow(unused_function)]
/// Returns the voting power of the active validators, values are voting power in the scale of 10000.
fun validator_voting_powers(wrapper: &mut IotaSystemState): VecMap<address, u64> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module iota_system::iota_system_display {

use std::string::String;
use std::string::{Self, String};

use iota::display::{Self, Display};

Expand All @@ -21,7 +21,7 @@ module iota_system::iota_system_display {
display::system_new<T>(sys_admin_cap, ctx)
}

/// Create a new Display<T> object with a set of fields using `IotaSystemAdminCap`.
/// Create a new `Display<T>` object with a set of fields using `IotaSystemAdminCap`.
public(package) fun system_new_with_fields<T: key>(
iota_system: &mut IotaSystemState,
fields: vector<String>,
Expand All @@ -34,4 +34,40 @@ module iota_system::iota_system_display {
// Create a `Display` object with fields.
display::system_new_with_fields<T>(sys_admin_cap, fields, values, ctx)
}

/// Add a display object to the system state store.
public(package) fun add_display_object<T: key>(
iota_system: &mut IotaSystemState,
display: Display<T>
) {
// Get a display object unique key.
let key = display_object_key<T>();

// Store the display object.
iota_system.add_extra_field(key, display);
}

/// Borrow an immutable display object from the system state store.
public(package) fun borrow_display_object<T: key>(iota_system: &mut IotaSystemState): &Display<T> {
// Get a display object unique key.
let key = display_object_key<T>();

// Borrow the display object.
iota_system.borrow_extra_field(key)
}

/// Borrow a mutable display object from the system state store.
public(package) fun borrow_display_object_mut<T: key>(iota_system: &mut IotaSystemState): &mut Display<T> {
// Get a display object unique key.
let key = display_object_key<T>();

// Borrow the display object.
iota_system.borrow_extra_field_mut(key)
}

/// Return a fully qualified type name with the original package IDs
/// that is used as a display object key.
fun display_object_key<T>(): String {
string::from_ascii(std::type_name::get_with_original_ids<T>().into_string())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,31 @@ module iota_system::iota_system_state_inner {
}
}

/// Add an object with the specified key to the extra fields collection.
public(package) fun add_extra_field<K: copy + drop + store, V: store>(
self: &mut IotaSystemStateV1,
key: K,
value: V,
) {
self.extra_fields.add(key, value);
}

/// Immutable borrows the value associated with the key in the extra fields.
public(package) fun borrow_extra_field<K: copy + drop + store, V: store>(
self: &IotaSystemStateV1,
key: K,
): &V {
self.extra_fields.borrow(key)
}

/// Mutable borrows the value associated with the key in the extra fields.
public(package) fun borrow_extra_field_mut<K: copy + drop + store, V: store>(
self: &mut IotaSystemStateV1,
key: K,
): &mut V {
self.extra_fields.borrow_mut(key)
}

// ==== validator metadata management functions ====

/// Create a new `UnverifiedValidatorOperationCap`, transfer it to the
Expand Down
Binary file modified crates/iota-framework/packages_compiled/iota-system
Binary file not shown.
36 changes: 33 additions & 3 deletions crates/iota-framework/published_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,15 @@ report_validator_impl
undo_report_validator_impl
fun
0x3::iota_system_state_inner
add_extra_field
public(package) fun
0x3::iota_system_state_inner
borrow_extra_field
public(package) fun
0x3::iota_system_state_inner
borrow_extra_field_mut
public(package) fun
0x3::iota_system_state_inner
rotate_operation_cap
public(package) fun
0x3::iota_system_state_inner
Expand Down Expand Up @@ -889,6 +898,18 @@ pool_exchange_rates
active_validator_addresses
public fun
0x3::iota_system
load_iota_system_admin_cap
public(package) fun
0x3::iota_system
add_extra_field
public(package) fun
0x3::iota_system
borrow_extra_field
public(package) fun
0x3::iota_system
borrow_extra_field_mut
public(package) fun
0x3::iota_system
advance_epoch
fun
0x3::iota_system
Expand All @@ -901,9 +922,6 @@ load_system_state_mut
load_inner_maybe_upgrade
fun
0x3::iota_system
load_iota_system_admin_cap
public(package) fun
0x3::iota_system
validator_voting_powers
fun
0x3::iota_system
Expand Down Expand Up @@ -1006,6 +1024,18 @@ system_new
system_new_with_fields
public(package) fun
0x3::iota_system_display
add_display_object
public(package) fun
0x3::iota_system_display
borrow_display_object
public(package) fun
0x3::iota_system_display
borrow_display_object_mut
public(package) fun
0x3::iota_system_display
display_object_key
fun
0x3::iota_system_display
secp256k1_ecrecover
public fun
0x2::ecdsa_k1
Expand Down

0 comments on commit 0fa27f6

Please sign in to comment.