Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-aptos committed Oct 18, 2023
1 parent 65b233d commit 03c003f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module aptos_framework::aggregator_v2 {
/// The generic type supplied to the aggregator is not supported.
const EUNSUPPORTED_AGGREGATOR_TYPE: u64 = 7;

/// Arguments passed to concat exceed max limit of 256 bytes (for prefix and suffix together)
/// Arguments passed to concat exceed max limit of 256 bytes (for prefix and suffix together).
const ECONCAT_STRING_LENGTH_TOO_LARGE: u64 = 8;

/// The native aggregator function, that is in the move file, is not yet supported.
Expand All @@ -37,7 +37,7 @@ module aptos_framework::aggregator_v2 {
/// Represents an integer which supports parallel additions and subtractions
/// across multiple transactions. See the module description for more details.
///
/// Currently supported types for Element are u64 and u128.
/// Currently supported types for IntElement are u64 and u128.
struct Aggregator<IntElement> has store, drop {
value: IntElement,
max_value: IntElement,
Expand All @@ -57,14 +57,14 @@ module aptos_framework::aggregator_v2 {

/// Creates new aggregator, with given 'max_value'.
///
/// Currently supported types for Element are u64 and u128.
/// Currently supported types for IntElement are u64 and u128.
/// EAGGREGATOR_ELEMENT_TYPE_NOT_SUPPORTED raised if called with a different type.
public native fun create_aggregator<IntElement: copy + drop>(max_value: IntElement): Aggregator<IntElement>;

/// Creates new aggregator, without any 'max_value' on top of the implicit bound restriction
/// due to the width of the type (i.e. MAX_U64 for u64, MAX_U128 for u128).
///
/// Currently supported types for Element are u64 and u128.
/// Currently supported types for IntElement are u64 and u128.
/// EAGGREGATOR_ELEMENT_TYPE_NOT_SUPPORTED raised if called with a different type.
public native fun create_unbounded_aggregator<IntElement: copy + drop>(): Aggregator<IntElement>;

Expand All @@ -73,7 +73,7 @@ module aptos_framework::aggregator_v2 {
public native fun try_add<IntElement>(aggregator: &mut Aggregator<IntElement>, value: IntElement): bool;

// Adds `value` to aggregator, uncoditionally.
// If addition would exceed the max_value, EAGGREGATOR_OVERFLOW exception will be thrown
// If addition would exceed the max_value, EAGGREGATOR_OVERFLOW exception will be thrown.
public fun add<IntElement>(aggregator: &mut Aggregator<IntElement>, value: IntElement) {
assert!(try_add(aggregator, value), error::out_of_range(EAGGREGATOR_OVERFLOW));
}
Expand All @@ -82,8 +82,8 @@ module aptos_framework::aggregator_v2 {
/// If subtraction would result in a negative value, `false` is returned, and aggregator value is left unchanged.
public native fun try_sub<IntElement>(aggregator: &mut Aggregator<IntElement>, value: IntElement): bool;

// Adds `value` to aggregator, uncoditionally.
// If subtraction would result in a negative value, EAGGREGATOR_UNDERFLOW exception will be thrown
// Subtracts `value` to aggregator, uncoditionally.
// If subtraction would result in a negative value, EAGGREGATOR_UNDERFLOW exception will be thrown.
public fun sub<IntElement>(aggregator: &mut Aggregator<IntElement>, value: IntElement) {
assert!(try_sub(aggregator, value), error::out_of_range(EAGGREGATOR_UNDERFLOW));
}
Expand Down Expand Up @@ -118,7 +118,7 @@ module aptos_framework::aggregator_v2 {
public native fun string_concat<IntElement>(before: String, snapshot: &AggregatorSnapshot<IntElement>, after: String): AggregatorSnapshot<String>;

#[test]
public fun test_aggregator() {
fun test_aggregator() {
let agg = create_aggregator(10);
assert!(try_add(&mut agg, 5), 1);
assert!(try_add(&mut agg, 5), 2);
Expand All @@ -135,7 +135,7 @@ module aptos_framework::aggregator_v2 {
}

#[test]
public fun test_correct_read() {
fun test_correct_read() {
let snapshot = create_snapshot(42);
assert!(read_snapshot(&snapshot) == 42, 0);

Expand All @@ -145,28 +145,30 @@ module aptos_framework::aggregator_v2 {

#[test]
#[expected_failure(abort_code = 0x030009, location = Self)]
public fun test_copy_not_yet_supported() {
fun test_copy_not_yet_supported() {
let snapshot = create_snapshot(42);
copy_snapshot(&snapshot);
}

#[test]
public fun test_string_concat1() {
fun test_string_concat1() {
let snapshot = create_snapshot(42);
let snapshot2 = string_concat(std::string::utf8(b"before"), &snapshot, std::string::utf8(b"after"));
assert!(read_snapshot(&snapshot2) == std::string::utf8(b"before42after"), 0);
}

#[test]
#[expected_failure(abort_code = 0x030005, location = Self)]
public fun test_string_concat_from_string_not_supported() {
fun test_string_concat_from_string_not_supported() {
let snapshot = create_snapshot<String>(std::string::utf8(b"42"));
string_concat(std::string::utf8(b"before"), &snapshot, std::string::utf8(b"after"));
}

// Tests commented out, as flag used in rust cannot be disabled.

// #[test(fx = @std)]
// #[expected_failure(abort_code = 0x030006, location = Self)]
// public fun test_snapshot_feature_not_enabled(fx: &signer) {
// fun test_snapshot_feature_not_enabled(fx: &signer) {
// use std::features;
// use aptos_framework::reconfiguration;
// let feature = features::get_aggregator_v2_api_feature();
Expand All @@ -177,7 +179,7 @@ module aptos_framework::aggregator_v2 {

// #[test(fx = @std)]
// #[expected_failure(abort_code = 0x030006, location = Self)]
// public fun test_aggregator_feature_not_enabled(fx: &signer) {
// fun test_aggregator_feature_not_enabled(fx: &signer) {
// use std::features;
// use aptos_framework::reconfiguration;
// let feature = features::get_aggregator_v2_api_feature();
Expand All @@ -188,11 +190,11 @@ module aptos_framework::aggregator_v2 {

#[test]
#[expected_failure(abort_code = 0x030007, location = Self)]
public fun test_aggregator_invalid_type1() {
fun test_aggregator_invalid_type1() {
create_unbounded_aggregator<u8>();
}

public fun test_aggregator_valid_type() {
fun test_aggregator_valid_type() {
create_unbounded_aggregator<u64>();
create_unbounded_aggregator<u128>();
create_aggregator<u64>(5);
Expand All @@ -201,14 +203,14 @@ module aptos_framework::aggregator_v2 {

#[test]
#[expected_failure(abort_code = 0x030005, location = Self)]
public fun test_snpashot_invalid_type1() {
fun test_snpashot_invalid_type1() {
use std::option;
create_snapshot(option::some(42));
}

#[test]
#[expected_failure(abort_code = 0x030005, location = Self)]
public fun test_snpashot_invalid_type2() {
fun test_snpashot_invalid_type2() {
create_snapshot(vector[42]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ spec aptos_framework::aggregator_v2 {
pragma opaque;
}

spec create_unbounded_aggregator {
// TODO: temporary mockup.
pragma opaque;
}

spec try_add {
// TODO: temporary mockup.
pragma opaque;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub const EAGGREGATOR_API_NOT_ENABLED: u64 = 0x03_0006;
/// The generic type supplied to the aggregators is not supported.
pub const EUNSUPPORTED_AGGREGATOR_TYPE: u64 = 0x03_0007;

/// Arguments passed to concat exceed max limit of 256 bytes (for prefix and suffix together)
/// Arguments passed to concat exceed max limit of 256 bytes (for prefix and suffix together).
pub const ECONCAT_STRING_LENGTH_TOO_LARGE: u64 = 0x03_0008;

/// The native aggregator function, that is in the move file, is not yet supported.
Expand All @@ -57,21 +57,19 @@ fn is_string_type(context: &SafeNativeContext, type_arg: &Type) -> SafeNativeRes
}

/// Given the native function argument and a type, returns a tuple of its
/// fields: (`aggregator id`, `limit`).
/// fields: (`aggregator id`, `max_value`).
pub fn get_aggregator_fields_by_type(
ty_arg: &Type,
agg: &StructRef,
) -> SafeNativeResult<(u128, u128)> {
match ty_arg {
Type::U128 => {
// Get aggregator information and a value to add.
let (id, limit) = get_aggregator_fields_u128(agg)?;
Ok((id, limit))
let (id, max_value) = get_aggregator_fields_u128(agg)?;
Ok((id, max_value))
},
Type::U64 => {
// Get aggregator information and a value to add.
let (id, limit) = get_aggregator_fields_u64(agg)?;
Ok((id as u128, limit as u128))
let (id, max_value) = get_aggregator_fields_u64(agg)?;
Ok((id as u128, max_value as u128))
},
_ => Err(SafeNativeError::Abort {
abort_code: EUNSUPPORTED_AGGREGATOR_TYPE,
Expand Down Expand Up @@ -166,7 +164,7 @@ impl SnapshotType {
},
)])))
},
// ty_arg cannot be Integer, if value is String
// Type cannot be Integer, if value is String
_ => Err(SafeNativeError::Abort {
abort_code: EUNSUPPORTED_AGGREGATOR_SNAPSHOT_TYPE,
}),
Expand Down Expand Up @@ -255,7 +253,7 @@ macro_rules! abort_if_not_enabled {
}

/***************************************************************************************************
* native fun create_aggregator<Element>(max_value: Element): Aggregator<Element>;
* native fun create_aggregator<IntElement>(max_value: IntElement): Aggregator<IntElement>;
**************************************************************************************************/

fn native_create_aggregator(
Expand All @@ -266,6 +264,7 @@ fn native_create_aggregator(
abort_if_not_enabled!(context);

debug_assert_eq!(args.len(), 1);
debug_assert_eq!(ty_args.len(), 1);

context.charge(AGGREGATOR_V2_CREATE_AGGREGATOR_BASE)?;
let max_value = pop_value_by_type(&ty_args[0], &mut args)?;
Expand All @@ -290,6 +289,7 @@ fn native_create_unbounded_aggregator(
abort_if_not_enabled!(context);

debug_assert_eq!(args.len(), 0);
debug_assert_eq!(ty_args.len(), 1);

context.charge(AGGREGATOR_V2_CREATE_AGGREGATOR_BASE)?;
let max_value = {
Expand All @@ -313,7 +313,7 @@ fn native_create_unbounded_aggregator(
}

/***************************************************************************************************
* native fun try_add<Element>(aggregator: &mut Aggregator<Element>, value: Element): bool;
* native fun try_add<IntElement>(aggregator: &mut Aggregator<IntElement>, value: IntElement): bool;
**************************************************************************************************/
fn native_try_add(
context: &mut SafeNativeContext,
Expand All @@ -323,6 +323,7 @@ fn native_try_add(
abort_if_not_enabled!(context);

debug_assert_eq!(args.len(), 2);
debug_assert_eq!(ty_args.len(), 1);
context.charge(AGGREGATOR_V2_TRY_ADD_BASE)?;

let input = pop_value_by_type(&ty_args[0], &mut args)?;
Expand All @@ -344,7 +345,7 @@ fn native_try_add(
}

/***************************************************************************************************
* native fun try_sub<Element>(aggregator: &mut Aggregator<Element>, value: Element): bool;
* native fun try_sub<IntElement>(aggregator: &mut Aggregator<IntElement>, value: IntElement): bool;
**************************************************************************************************/
fn native_try_sub(
context: &mut SafeNativeContext,
Expand All @@ -354,6 +355,7 @@ fn native_try_sub(
abort_if_not_enabled!(context);

debug_assert_eq!(args.len(), 2);
debug_assert_eq!(ty_args.len(), 1);
context.charge(AGGREGATOR_V2_TRY_SUB_BASE)?;

let input = pop_value_by_type(&ty_args[0], &mut args)?;
Expand All @@ -374,7 +376,7 @@ fn native_try_sub(
}

/***************************************************************************************************
* native fun read<Element>(aggregator: &Aggregator<Element>): Element;
* native fun read<IntElement>(aggregator: &Aggregator<IntElement>): IntElement;
**************************************************************************************************/

fn native_read(
Expand All @@ -385,6 +387,7 @@ fn native_read(
abort_if_not_enabled!(context);

debug_assert_eq!(args.len(), 1);
debug_assert_eq!(ty_args.len(), 1);
context.charge(AGGREGATOR_V2_READ_BASE)?;

let (agg_value, agg_max_value) =
Expand All @@ -401,7 +404,7 @@ fn native_read(
}

/***************************************************************************************************
* native fun snapshot(aggregator: &Aggregator): AggregatorSnapshot<u128>;
* native fun snapshot<IntElement>(aggregator: &Aggregator<IntElement>): AggregatorSnapshot<IntElement>;
**************************************************************************************************/

fn native_snapshot(
Expand All @@ -412,6 +415,7 @@ fn native_snapshot(
abort_if_not_enabled!(context);

debug_assert_eq!(args.len(), 1);
debug_assert_eq!(ty_args.len(), 1);
context.charge(AGGREGATOR_V2_SNAPSHOT_BASE)?;

let (agg_value, _agg_max_value) =
Expand All @@ -425,7 +429,7 @@ fn native_snapshot(
}

/***************************************************************************************************
* native fun create_snapshot(value: Element): AggregatorSnapshot<Element>;
* native fun create_snapshot<Element>(value: Element): AggregatorSnapshot<Element>
**************************************************************************************************/

fn native_create_snapshot(
Expand All @@ -450,7 +454,7 @@ fn native_create_snapshot(
}

/***************************************************************************************************
* native fun copy_snapshot(snapshot: AggregatorSnapshot<Element>): AggregatorSnapshot<Element>;
* native fun copy_snapshot<Element>(snapshot: &AggregatorSnapshot<Element>): AggregatorSnapshot<Element>
**************************************************************************************************/

fn native_copy_snapshot(
Expand Down Expand Up @@ -478,7 +482,7 @@ fn native_copy_snapshot(
}

/***************************************************************************************************
* native fun read_snapshot(snapshot: AggregatorSnapshot<Element>): Element;
* native fun read_snapshot<Element>(snapshot: &AggregatorSnapshot<Element>): Element;
**************************************************************************************************/

fn native_read_snapshot(
Expand All @@ -503,7 +507,7 @@ fn native_read_snapshot(
}

/***************************************************************************************************
* native fun native fun string_concat<Element>(before: String, snapshot: &AggregatorSnapshot<Element>, after: String): AggregatorSnapshot<String>;
* native fun string_concat<IntElement>(before: String, snapshot: &AggregatorSnapshot<IntElement>, after: String): AggregatorSnapshot<String>;
**************************************************************************************************/

fn native_string_concat(
Expand Down

0 comments on commit 03c003f

Please sign in to comment.