Skip to content

Commit

Permalink
feat: improved system displays implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriyr committed Nov 8, 2024
1 parent 4860b6f commit 5235500
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 180 deletions.
21 changes: 19 additions & 2 deletions crates/iota-core/src/authority/authority_per_epoch_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ use iota_types::{
effects::TransactionEffects,
error::{IotaError, IotaResult},
executable_transaction::{TrustedExecutableTransaction, VerifiedExecutableTransaction},
iota_system_state::epoch_start_iota_system_state::{
EpochStartSystemState, EpochStartSystemStateTrait,
iota_system_state::{
display_object_key,
epoch_start_iota_system_state::{EpochStartSystemState, EpochStartSystemStateTrait},
},
message_envelope::TrustedEnvelope,
messages_checkpoint::{
Expand All @@ -62,6 +63,7 @@ use iota_types::{
};
use itertools::{Itertools, izip};
use move_bytecode_utils::module_cache::SyncModuleCache;
use move_core_types::language_storage::StructTag;
use parking_lot::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
use prometheus::IntCounter;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -959,6 +961,21 @@ impl AuthorityPerEpochStore {
self.epoch_start_configuration.bridge_committee_initiated()
}

pub fn system_display_object_created(&self, ty: StructTag, version: u16) -> bool {
let key = display_object_key(ty);

let object_version = self
.epoch_start_configuration
.system_display_object_version(&key)
.cloned();

let Some(object_version) = object_version else {
return false;
};

version <= object_version
}

pub fn get_parent_path(&self) -> PathBuf {
self.parent_path.clone()
}
Expand Down
24 changes: 21 additions & 3 deletions crates/iota-core/src/authority/epoch_start_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::fmt;
use std::{collections::HashMap, fmt};

use enum_dispatch::enum_dispatch;
use iota_config::{ExecutionCacheConfig, NodeConfig};
Expand All @@ -13,8 +13,9 @@ use iota_types::{
deny_list_v1::get_deny_list_obj_initial_shared_version,
epoch_data::EpochData,
error::IotaResult,
iota_system_state::epoch_start_iota_system_state::{
EpochStartSystemState, EpochStartSystemStateTrait,
iota_system_state::{
epoch_start_iota_system_state::{EpochStartSystemState, EpochStartSystemStateTrait},
get_system_display_objects,
},
messages_checkpoint::{CheckpointDigest, CheckpointTimestamp},
randomness_state::get_randomness_state_obj_initial_shared_version,
Expand All @@ -34,6 +35,8 @@ pub trait EpochStartConfigTrait {
fn coin_deny_list_obj_initial_shared_version(&self) -> SequenceNumber;
fn bridge_obj_initial_shared_version(&self) -> Option<SequenceNumber>;
fn bridge_committee_initiated(&self) -> bool;
fn system_display_object_versions(&self) -> impl Iterator<Item = (&String, &u16)>;
fn system_display_object_version(&self, key: &str) -> Option<&u16>;

fn execution_cache_type(&self) -> ExecutionCacheConfigType {
if self.flags().contains(&EpochFlag::WritebackCacheEnabled) {
Expand Down Expand Up @@ -115,6 +118,10 @@ impl EpochStartConfiguration {
let bridge_obj_initial_shared_version =
get_bridge_obj_initial_shared_version(object_store)?;
let bridge_committee_initiated = is_bridge_committee_initiated(object_store)?;
let system_display_object_versions = get_system_display_objects(object_store)?
.into_iter()
.map(|(key, display)| (key, display.version))
.collect();
Ok(Self::V1(EpochStartConfigurationV1 {
system_state,
epoch_digest,
Expand All @@ -124,6 +131,7 @@ impl EpochStartConfiguration {
coin_deny_list_obj_initial_shared_version,
bridge_obj_initial_shared_version,
bridge_committee_initiated,
system_display_object_versions,
}))
}

Expand All @@ -143,6 +151,7 @@ impl EpochStartConfiguration {
.coin_deny_list_obj_initial_shared_version,
bridge_obj_initial_shared_version: config.bridge_obj_initial_shared_version,
bridge_committee_initiated: config.bridge_committee_initiated,
system_display_object_versions: config.system_display_object_versions.clone(),
}),
_ => panic!(
"This function is only implemented for the latest version of EpochStartConfiguration"
Expand Down Expand Up @@ -174,6 +183,7 @@ pub struct EpochStartConfigurationV1 {
coin_deny_list_obj_initial_shared_version: SequenceNumber,
bridge_obj_initial_shared_version: Option<SequenceNumber>,
bridge_committee_initiated: bool,
system_display_object_versions: HashMap<String, u16>,
}

impl EpochStartConfigTrait for EpochStartConfigurationV1 {
Expand Down Expand Up @@ -208,4 +218,12 @@ impl EpochStartConfigTrait for EpochStartConfigurationV1 {
fn bridge_committee_initiated(&self) -> bool {
self.bridge_committee_initiated
}

fn system_display_object_versions(&self) -> impl Iterator<Item = (&String, &u16)> {
self.system_display_object_versions.iter()
}

fn system_display_object_version(&self, key: &str) -> Option<&u16> {
self.system_display_object_versions.get(key)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ module iota::display {
pub.from_package<T>()
}

/// Read the `id` field.
public fun id<T: key>(d: &Display<T>): &ID {
d.id.uid_as_inner()
}

/// Read the `version` field.
public fun version<T: key>(d: &Display<T>): u16 {
d.version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module iota::system_admin_cap {
const ENotSystemAddress: u64 = 1;

/// `IotaSystemAdminCap` allows to perform privileged IOTA system operations.
/// For example, packing and unpacking `TimeLock`s during staking, etc.
/// For example, packing and unpacking `TimeLock`s during staking,
/// creating `Display` objects without `Publisher`, etc.
public struct IotaSystemAdminCap has store {}

#[allow(unused_function)]
Expand Down
74 changes: 43 additions & 31 deletions crates/iota-framework/packages/iota-system/sources/iota_system.move
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@

module iota_system::iota_system {
use iota::balance::Balance;
use std::string::String;

use iota::coin::Coin;
use iota_system::staking_pool::StakedIota;
use iota::display::Display;
use iota::iota::{IOTA, IotaTreasuryCap};
use iota::table::Table;
use iota::system_admin_cap::IotaSystemAdminCap;
use iota_system::validator::ValidatorV1;
use iota_system::validator_cap::UnverifiedValidatorOperationCap;
use iota_system::iota_system_state_inner::{Self, SystemParametersV1, IotaSystemStateV1};
use iota_system::staking_pool::PoolTokenExchangeRate;
use iota::dynamic_field;
use iota::vec_map::VecMap;

use iota_system::iota_system_state_inner::{Self, SystemParametersV1, IotaSystemStateV1};
use iota_system::staking_pool::{PoolTokenExchangeRate, StakedIota};
use iota_system::validator::ValidatorV1;
use iota_system::validator_cap::UnverifiedValidatorOperationCap;

#[test_only] use iota::balance;
#[test_only] use iota_system::validator_set::ValidatorSetV1;
#[test_only] use iota::vec_set::VecSet;
Expand Down Expand Up @@ -505,41 +507,51 @@ module iota_system::iota_system {
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>(
/// Create an empty `Display` object with `IotaSystemAdminCap`.
public(package) fun new_system_display<T: key>(
wrapper: &mut IotaSystemState,
key: K,
value: V,
) {
let self = load_system_state_mut(wrapper);
self.add_extra_field(key, value);
ctx: &mut TxContext,
): Display<T> {
let self = wrapper.load_system_state();
self.new_system_display(ctx)
}

/// Immutable borrows the value associated with the key in the extra fields.
public(package) fun borrow_extra_field<K: copy + drop + store, V: store>(
/// Create a new `Display<T>` object with a set of fields using `IotaSystemAdminCap`.
public(package) fun new_system_display_with_fields<T: key>(
wrapper: &mut IotaSystemState,
key: K,
): &V {
let self = load_system_state(wrapper);
self.borrow_extra_field(key)
fields: vector<String>,
values: vector<String>,
ctx: &mut TxContext,
): Display<T> {
let self = wrapper.load_system_state();
self.new_system_display_with_fields(fields, values, ctx)
}

/// 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>(
/// Insert a display object.
public(package) fun insert_display_object<T: key>(
wrapper: &mut IotaSystemState,
key: K,
): &mut V {
let self = load_system_state_mut(wrapper);
self.borrow_extra_field_mut(key)
display: Display<T>,
) {
let self = wrapper.load_system_state_mut();
self.insert_display_object(display);
}

/// Returns true if there is an extra field associated with the key.
public(package) fun contains_extra_field<K: copy + drop + store>(
wrapper: &mut IotaSystemState,
key: K,
): bool {
let self = load_system_state_mut(wrapper);
self.contains_extra_field(key)
/// Borrow an immutable display object.
public(package) fun borrow_display_object<T: key>(wrapper: &mut IotaSystemState): &Display<T> {
let self = wrapper.load_system_state();
self.borrow_display_object<T>()
}

/// Borrow a mutable display object.
public(package) fun borrow_display_object_mut<T: key>(wrapper: &mut IotaSystemState): &mut Display<T> {
let self = wrapper.load_system_state_mut();
self.borrow_display_object_mut<T>()
}

/// Returns true if the related display object exists.
public(package) fun contains_display_object<T: key>(wrapper: &mut IotaSystemState): bool {
let self = wrapper.load_system_state();
self.contains_display_object<T>()
}

#[allow(unused_function)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
// SPDX-License-Identifier: Apache-2.0

module iota_system::iota_system_state_inner {
use std::string::{Self, String};

use iota::balance::{Self, Balance};
use iota::coin::Coin;
use iota::display::{Self, Display};
use iota::iota::{IOTA, IotaTreasuryCap};
use iota::system_admin_cap::IotaSystemAdminCap;
use iota::vec_map::{Self, VecMap};
use iota::vec_set::{Self, VecSet};
use iota::event;
use iota::table::Table;
use iota::bag::{Self, Bag};

use iota_system::validator::{Self, ValidatorV1};
use iota_system::validator_set::{Self, ValidatorSetV1};
use iota_system::validator_cap::{UnverifiedValidatorOperationCap, ValidatorOperationCap};
use iota_system::storage_fund::{Self, StorageFundV1};
use iota_system::staking_pool::{PoolTokenExchangeRate, StakedIota};
use iota::vec_map::{Self, VecMap};
use iota::vec_set::{Self, VecSet};
use iota::event;
use iota::table::Table;
use iota::bag::Bag;
use iota::bag;

// same as in validator_set
const ACTIVE_VALIDATOR_ONLY: u8 = 1;
Expand Down Expand Up @@ -104,6 +107,10 @@ module iota_system::iota_system_state_inner {

/// Unix timestamp of the current epoch start
epoch_start_timestamp_ms: u64,

/// A map contains the system display object IDs stored in the extra fields.
system_display_objects: VecMap<String, ID>,

/// Any extra fields that's not defined statically.
extra_fields: Bag,
}
Expand Down Expand Up @@ -171,6 +178,7 @@ module iota_system::iota_system_state_inner {
safe_mode_storage_rebates: 0,
safe_mode_non_refundable_storage_fee: 0,
epoch_start_timestamp_ms,
system_display_objects: vec_map::empty(),
extra_fields: bag::new(ctx),
};
system_state
Expand Down Expand Up @@ -442,37 +450,75 @@ 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);
/// Create an empty `Display` object with `IotaSystemAdminCap`.
public(package) fun new_system_display<T: key>(
self: &IotaSystemStateV1,
ctx: &mut TxContext,
): Display<T> {
display::system_new<T>(&self.iota_system_admin_cap, ctx)
}

/// Immutable borrows the value associated with the key in the extra fields.
public(package) fun borrow_extra_field<K: copy + drop + store, V: store>(
/// Create a new `Display<T>` object with a set of fields using `IotaSystemAdminCap`.
public(package) fun new_system_display_with_fields<T: key>(
self: &IotaSystemStateV1,
key: K,
): &V {
self.extra_fields.borrow(key)
fields: vector<String>,
values: vector<String>,
ctx: &mut TxContext,
): Display<T> {
display::system_new_with_fields<T>(&self.iota_system_admin_cap, fields, values, ctx)
}

/// 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>(
/// Insert a display object.
public(package) fun insert_display_object<T: key>(
self: &mut IotaSystemStateV1,
key: K,
): &mut V {
self.extra_fields.borrow_mut(key)
display: Display<T>,
) {
// Get a display object unique key.
let key = display_object_key<T>();
let display_id = *display.id();

// Store the display object.
self.system_display_objects.insert(key, display_id);
self.extra_fields.add(display_id, display);
}

/// Returns true if there is an extra field associated with the key.
public(package) fun contains_extra_field<K: copy + drop + store>(
self: &IotaSystemStateV1,
key: K,
): bool {
self.extra_fields.contains(key)
/// Borrow an immutable display object.
public(package) fun borrow_display_object<T: key>(self: &IotaSystemStateV1): &Display<T> {
// Get a display object unique key.
let key = display_object_key<T>();

// Get the id.
let display_id = *self.system_display_objects.get(&key);

// Borrow the display object.
self.extra_fields.borrow(display_id)
}

/// Borrow a mutable display object.
public(package) fun borrow_display_object_mut<T: key>(self: &mut IotaSystemStateV1): &mut Display<T> {
// Get a display object unique key.
let key = display_object_key<T>();

// Get the id.
let display_id = *self.system_display_objects.get(&key);

// Borrow the display object.
self.extra_fields.borrow_mut(display_id)
}

/// Returns true if the related display object exists.
public(package) fun contains_display_object<T: key>(self: &IotaSystemStateV1): bool {
// Get a display object unique key.
let key = display_object_key<T>();

// Check if the related display object exists.
self.system_display_objects.contains(&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())
}

// ==== validator metadata management functions ====
Expand Down
Loading

0 comments on commit 5235500

Please sign in to comment.