Repository: MystenLabs/sui
File: sui-execution/latest/sui-move-natives/src/protocol_config.rs (Rust)

Code before:
/***************************************************************************************************
 * native fun is_feature_enabled
 *
 * Implementation of the Move native function `protocol_config::is_feature_enabled(
 *      feature_flag_name: vector<u8>): bool`
 *
 * Checks if a protocol feature flag is enabled in the current protocol version.
 * Takes raw bytes for simplicity since this is internal framework use only.
 *
 * Gas cost: 0 (zero cost for framework-internal use)
 **************************************************************************************************/
pub fn is_feature_enabled(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.is_empty());
    debug_assert!(args.len() == 1);

    let feature_flag_name_bytes = pop_arg!(args, Vector);
    let bytes = feature_flag_name_bytes.to_vec_u8()?;

    let feature_flag_name = match String::from_utf8(bytes.to_vec()) {
        Ok(s) => s,
        Err(_) => {
            return Ok(NativeResult::err(
                context.gas_used(),
                INVALID_FEATURE_FLAG_NAME_ERROR,
            ));
        }
    };

    let protocol_config = &get_extension!(context, ObjectRuntime)?.protocol_config;

    // Use the auto-generated lookup_feature method to find the feature flag
    let is_enabled = match protocol_config.lookup_feature(feature_flag_name) {
        Some(value) => value,
        None => {
            return Ok(NativeResult::err(
                context.gas_used(),
                FEATURE_FLAG_NOT_FOUND_ERROR,
            ));
        }
    };

    Ok(NativeResult::ok(
        context.gas_used(),
        smallvec![Value::bool(is_enabled)],
    ))
}


Review comment (suggestion):
I wonder if we should just make this an invariant violation in some way (similar with the error below)

Code after:
 * Implementation of the Move native function `protocol_config::is_feature_enabled(
 *      feature_flag_name: vector<u8>): bool`
 *
 * Checks if a protocol feature flag is enabled in the current protocol version.
 *
 * Gas cost: 0 (zero cost for framework-internal use)
 **************************************************************************************************/
pub fn is_feature_enabled(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.is_empty());
    debug_assert!(args.len() == 1);

    let feature_flag_name_bytes = pop_arg!(args, Vector);
    let bytes = feature_flag_name_bytes.to_vec_u8()?;

    let protocol_config = &get_extension!(context, ObjectRuntime)?.protocol_config;

    let is_enabled = match String::from_utf8(bytes.to_vec()) {
        Ok(s) => {
            // Use the auto-generated lookup_feature method to find the feature flag
            match protocol_config.lookup_feature(s) {
                Some(value) => value,
                None => {
                    debug_assert!(false);
                    // We don't distinguish between feature flags that are not present and feature flags that are present but disabled.
                    // This is to handle the case where we accidentally shipped a framework upgrade that check a feature flag that is not present
                    // in the binary yet.
                    false
                }
            }
        }
        Err(_) => {
            debug_assert!(false);
            // Invalid UTF feature flags are treated as disabled feature flags.
            false
        }
    };

    Ok(NativeResult::ok(
        context.gas_used(),
        smallvec![Value::bool(is_enabled)],
    ))
}