Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into zjma/jwk_consensus_sm…
Browse files Browse the repository at this point in the history
…oke_tests
  • Loading branch information
zjma committed Feb 5, 2024
2 parents 3df6061 + c5a1b59 commit c24fd72
Show file tree
Hide file tree
Showing 255 changed files with 7,533 additions and 5,972 deletions.
32 changes: 31 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions aptos-move/aptos-aggregator/src/delayed_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

use crate::{
delta_change_set::{DeltaOp, DeltaWithMax},
types::{
code_invariant_error, DelayedFieldValue, DelayedFieldsSpeculativeError, PanicOr,
SnapshotToStringFormula,
},
types::{code_invariant_error, DelayedFieldValue, DelayedFieldsSpeculativeError, PanicOr},
};
use aptos_types::delayed_fields::SnapshotToStringFormula;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DelayedApplyChange<I: Clone> {
Expand Down Expand Up @@ -400,7 +398,7 @@ mod test {
delta: DeltaWithMax::new(SignedU128::Positive(3), 100),
});
let snapshot_change_2 = Apply(SnapshotDelta {
base_aggregator: DelayedFieldID::new(1),
base_aggregator: DelayedFieldID::new_for_test_for_u64(1),
delta: DeltaWithMax::new(SignedU128::Positive(2), 100),
});

Expand All @@ -411,7 +409,7 @@ mod test {
assert_eq!(
result.unwrap(),
Apply(SnapshotDelta {
base_aggregator: DelayedFieldID::new(1),
base_aggregator: DelayedFieldID::new_for_test_for_u64(1),
delta: DeltaWithMax::new(SignedU128::Positive(5), 100)
})
);
Expand Down
74 changes: 55 additions & 19 deletions aptos-move/aptos-aggregator/src/delayed_field_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ use crate::{
resolver::DelayedFieldResolver,
types::{
code_invariant_error, expect_ok, DelayedFieldID, DelayedFieldValue,
DelayedFieldsSpeculativeError, PanicOr, ReadPosition, SnapshotToStringFormula,
SnapshotValue,
DelayedFieldsSpeculativeError, PanicOr, ReadPosition,
},
};
use aptos_types::delayed_fields::{
calculate_width_for_constant_string, calculate_width_for_integer_embeded_string,
SnapshotToStringFormula,
};
use move_binary_format::errors::PartialVMResult;
use std::collections::{btree_map::Entry, BTreeMap};

Expand Down Expand Up @@ -163,6 +166,7 @@ impl DelayedFieldData {
&mut self,
aggregator_id: DelayedFieldID,
max_value: u128,
width: u32,
resolver: &dyn DelayedFieldResolver,
) -> PartialVMResult<DelayedFieldID> {
let aggregator = self.delayed_fields.get(&aggregator_id);
Expand Down Expand Up @@ -199,43 +203,75 @@ impl DelayedFieldData {
},
};

let snapshot_id = resolver.generate_delayed_field_id();
let snapshot_id = resolver.generate_delayed_field_id(width);
self.delayed_fields.insert(snapshot_id, change);
Ok(snapshot_id)
}

pub fn create_new_snapshot(
&mut self,
value: SnapshotValue,
value: u128,
width: u32,
resolver: &dyn DelayedFieldResolver,
) -> DelayedFieldID {
let change = DelayedChange::Create(value.into());
let snapshot_id = resolver.generate_delayed_field_id();
let change = DelayedChange::Create(DelayedFieldValue::Snapshot(value));
let snapshot_id = resolver.generate_delayed_field_id(width);

self.delayed_fields.insert(snapshot_id, change);
snapshot_id
}

pub fn create_new_derived(
&mut self,
value: Vec<u8>,
resolver: &dyn DelayedFieldResolver,
) -> PartialVMResult<DelayedFieldID> {
// cast shouldn't fail because we assert on low limit for value before this call.
let width =
u32::try_from(calculate_width_for_constant_string(value.len())).map_err(|_| {
code_invariant_error("Calculated DerivedStringSnapshot width exceeds u32")
})?;
let change = DelayedChange::Create(DelayedFieldValue::Derived(value));
let snapshot_id = resolver.generate_delayed_field_id(width);

self.delayed_fields.insert(snapshot_id, change);
Ok(snapshot_id)
}

pub fn read_snapshot(
&mut self,
snapshot_id: DelayedFieldID,
resolver: &dyn DelayedFieldResolver,
) -> PartialVMResult<SnapshotValue> {
Ok(SnapshotValue::try_from(self.read_value(
snapshot_id,
resolver,
ReadPosition::AfterCurrentTxn,
)?)?)
) -> PartialVMResult<u128> {
Ok(self
.read_value(snapshot_id, resolver, ReadPosition::AfterCurrentTxn)?
.into_snapshot_value()?)
}

pub fn string_concat(
pub fn read_derived(
&mut self,
snapshot_id: DelayedFieldID,
resolver: &dyn DelayedFieldResolver,
) -> PartialVMResult<Vec<u8>> {
Ok(self
.read_value(snapshot_id, resolver, ReadPosition::AfterCurrentTxn)?
.into_derived_value()?)
}

pub fn derive_string_concat(
&mut self,
snapshot_id: DelayedFieldID,
prefix: Vec<u8>,
suffix: Vec<u8>,
resolver: &dyn DelayedFieldResolver,
) -> PartialVMResult<DelayedFieldID> {
let snapshot = self.delayed_fields.get(&snapshot_id);
// cast shouldn't fail because we assert on low limit for prefix and suffix before this call.
let width = u32::try_from(calculate_width_for_integer_embeded_string(
prefix.len() + suffix.len(),
snapshot_id,
)?)
.map_err(|_| code_invariant_error("Calculated DerivedStringSnapshot width exceeds u32"))?;
let formula = SnapshotToStringFormula::Concat { prefix, suffix };

let change = match snapshot {
Expand All @@ -257,7 +293,7 @@ impl DelayedFieldData {
},
};

let new_id = resolver.generate_delayed_field_id();
let new_id = resolver.generate_delayed_field_id(width);
self.delayed_fields.insert(new_id, change);
Ok(new_id)
}
Expand All @@ -280,7 +316,7 @@ mod test {
fn test_aggregator_not_in_storage() {
let resolver = FakeAggregatorView::default();
let mut data = DelayedFieldData::default();
let id = DelayedFieldID::new(200);
let id = DelayedFieldID::new_for_test_for_u64(200);
let max_value = 700;

assert_err!(data.read_aggregator(id, &resolver));
Expand All @@ -299,7 +335,7 @@ mod test {
fn test_operations_on_new_aggregator() {
let resolver = FakeAggregatorView::default();
let mut data = DelayedFieldData::default();
let id = DelayedFieldID::new(200);
let id = DelayedFieldID::new_for_test_for_u64(200);
let max_value = 200;

data.create_new_aggregator(id);
Expand Down Expand Up @@ -354,7 +390,7 @@ mod test {
fn test_successful_operations_in_delta_mode() {
let mut resolver = FakeAggregatorView::default();
let mut data = DelayedFieldData::default();
let id = DelayedFieldID::new(200);
let id = DelayedFieldID::new_for_test_for_u64(200);
let max_value = 600;

resolver.set_from_aggregator_id(id, 100);
Expand Down Expand Up @@ -383,7 +419,7 @@ mod test {
fn test_aggregator_overflows() {
let mut resolver = FakeAggregatorView::default();
let mut data = DelayedFieldData::default();
let id = DelayedFieldID::new(600);
let id = DelayedFieldID::new_for_test_for_u64(600);
let max_value = 600;

resolver.set_from_aggregator_id(id, 100);
Expand Down Expand Up @@ -418,7 +454,7 @@ mod test {
fn test_aggregator_underflows() {
let mut resolver = FakeAggregatorView::default();
let mut data = DelayedFieldData::default();
let id = DelayedFieldID::new(600);
let id = DelayedFieldID::new_for_test_for_u64(600);
let max_value = 600;

resolver.set_from_aggregator_id(id, 200);
Expand Down
4 changes: 2 additions & 2 deletions aptos-move/aptos-aggregator/src/delta_change_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ mod test {
FakeAggregatorView,
};
use aptos_types::{
aggregator::PanicError,
delayed_fields::PanicError,
state_store::{state_key::StateKey, state_value::StateValue},
write_set::WriteOp,
};
Expand Down Expand Up @@ -532,7 +532,7 @@ mod test {
Err(code_invariant_error("Error message from BadStorage.").into())
}

fn generate_delayed_field_id(&self) -> Self::Identifier {
fn generate_delayed_field_id(&self, _width: u32) -> Self::Identifier {
unimplemented!("Irrelevant for the test")
}

Expand Down
1 change: 0 additions & 1 deletion aptos-move/aptos-aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub mod delta_change_set;
pub mod delta_math;
pub mod resolver;
pub mod types;
pub mod utils;

#[cfg(any(test, feature = "testing"))]
pub mod tests;
Expand Down
6 changes: 3 additions & 3 deletions aptos-move/aptos-aggregator/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
};
use aptos_types::{
aggregator::PanicError,
delayed_fields::PanicError,
state_store::{
state_key::StateKey,
state_value::{StateValue, StateValueMetadata},
Expand Down Expand Up @@ -164,7 +164,7 @@ pub trait TDelayedFieldView {

/// Returns a unique per-block identifier that can be used when creating a
/// new aggregator V2.
fn generate_delayed_field_id(&self) -> Self::Identifier;
fn generate_delayed_field_id(&self, width: u32) -> Self::Identifier;

/// Validate that given value (from aggregator structure) is a valid delayed field identifier,
/// and convert it to Self::Identifier if so.
Expand Down Expand Up @@ -251,7 +251,7 @@ where

/// Returns a unique per-block identifier that can be used when creating a
/// new aggregator V2.
fn generate_delayed_field_id(&self) -> Self::Identifier {
fn generate_delayed_field_id(&self, _width: u32) -> Self::Identifier {
unimplemented!("generate_delayed_field_id not implemented")
}

Expand Down
Loading

0 comments on commit c24fd72

Please sign in to comment.