Skip to content

Commit

Permalink
Merge #4459
Browse files Browse the repository at this point in the history
4459: Add some docs on existing unit tests r=sydhds a=sydhds

* [ ] document all added functions
* [ ] try in sandbox /simulation/labnet
  * [ ] if part of node-launch, checked using the `resync_check` flag
* [ ] unit tests on the added/changed features
  * [ ] make tests compile
  * [ ] make tests pass 
* [ ] add logs allowing easy debugging in case the changes caused problems
* [ ] if the API has changed, update the API specification

Co-authored-by: sydhds <sydhds@gmail.com>
  • Loading branch information
bors[bot] and sydhds authored Oct 17, 2023
2 parents fb0f9cb + c3a898e commit ffad3ac
Show file tree
Hide file tree
Showing 6 changed files with 524 additions and 81 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions massa-async-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ massa-proto-rs = {workspace = true, "features" = ["tonic"]}

[dev-dependencies]
tempfile = {workspace = true} # BOM UPGRADE Revert to "3.3" if problem
parking_lot = {workspace = true}
assert_matches = { workspace = true }
massa_ledger_worker = {workspace = true}
massa_db_worker = {workspace = true}
192 changes: 118 additions & 74 deletions massa-async-pool/src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,38 +86,6 @@ impl Default for AsyncPoolChangesSerializer {
}

impl Serializer<AsyncPoolChanges> for AsyncPoolChangesSerializer {
/// ## Example
/// ```
/// use std::ops::Bound::Included;
/// use massa_serialization::Serializer;
/// use massa_models::{address::Address, amount::Amount, slot::Slot};
/// use std::str::FromStr;
/// use massa_async_pool::{AsyncMessage, AsyncPoolChanges, AsyncPoolChangesSerializer};
/// use massa_ledger_exports::SetUpdateOrDelete;
///
/// let message = AsyncMessage::new(
/// Slot::new(1, 0),
/// 0,
/// Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
/// Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
/// String::from("test"),
/// 10000000,
/// Amount::from_str("1").unwrap(),
/// Amount::from_str("1").unwrap(),
/// Slot::new(2, 0),
/// Slot::new(3, 0),
/// vec![1, 2, 3, 4],
/// None,
/// None
/// );
/// let mut changes = AsyncPoolChanges::default();
/// changes
/// .0
/// .insert(message.compute_id(), SetUpdateOrDelete::Set(message));
/// let mut serialized = Vec::new();
/// let serializer = AsyncPoolChangesSerializer::new();
/// serializer.serialize(&changes, &mut serialized).unwrap();
/// ```
fn serialize(
&self,
value: &AsyncPoolChanges,
Expand Down Expand Up @@ -184,48 +152,6 @@ impl AsyncPoolChangesDeserializer {
}

impl Deserializer<AsyncPoolChanges> for AsyncPoolChangesDeserializer {
/// ## Example
/// ```
/// use std::ops::Bound::Included;
/// use massa_serialization::{Serializer, Deserializer, DeserializeError};
/// use massa_models::{address::Address, amount::Amount, slot::Slot};
/// use std::str::FromStr;
/// use massa_async_pool::{AsyncMessage, AsyncMessageTrigger, AsyncPoolChanges, AsyncPoolChangesSerializer, AsyncPoolChangesDeserializer};
/// use massa_ledger_exports::SetUpdateOrDelete;
///
/// let message = AsyncMessage::new(
/// Slot::new(1, 0),
/// 0,
/// Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
/// Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
/// String::from("test"),
/// 10000000,
/// Amount::from_str("1").unwrap(),
/// Amount::from_str("1").unwrap(),
/// Slot::new(2, 0),
/// Slot::new(3, 0),
/// vec![1, 2, 3, 4],
/// Some(AsyncMessageTrigger {
/// address: Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
/// datastore_key: Some(vec![1, 2, 3, 4]),
/// }),
/// None
/// );
/// let mut changes = AsyncPoolChanges::default();
/// changes
/// .0
/// .insert(message.compute_id(), SetUpdateOrDelete::Set(message.clone()));
/// changes
/// .0
/// .insert(message.compute_id(), SetUpdateOrDelete::Delete);
/// let mut serialized = Vec::new();
/// let serializer = AsyncPoolChangesSerializer::new();
/// let deserializer = AsyncPoolChangesDeserializer::new(32, 10000, 10000, 100000, 100000);
/// serializer.serialize(&changes, &mut serialized).unwrap();
/// let (rest, changes_deser) = deserializer.deserialize::<DeserializeError>(&serialized).unwrap();
/// assert!(rest.is_empty());
/// assert_eq!(changes, changes_deser);
/// ```
fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
&self,
buffer: &'a [u8],
Expand Down Expand Up @@ -299,3 +225,121 @@ impl AsyncPoolChanges {
self.apply(change);
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use massa_ledger_exports::SetUpdateOrDelete;
use massa_models::{address::Address, amount::Amount, slot::Slot};
use massa_serialization::{DeserializeError, Deserializer, Serializer};

use crate::message::AsyncMessageTrigger;

use assert_matches::assert_matches;

use super::*;

fn get_message() -> AsyncMessage {
let message = AsyncMessage::new(
Slot::new(1, 0),
0,
Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
String::from("test"),
10000000,
Amount::from_str("1").unwrap(),
Amount::from_str("1").unwrap(),
Slot::new(2, 0),
Slot::new(3, 0),
vec![1, 2, 3, 4],
Some(AsyncMessageTrigger {
address: Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x")
.unwrap(),
datastore_key: Some(vec![1, 2, 3, 4]),
}),
None,
);
return message;
}

#[test]
fn test_changes_ser_deser() {
// Async pool changes serialization && deserialization

let message = get_message();
let mut changes = AsyncPoolChanges::default();
changes.0.insert(
message.compute_id(),
SetUpdateOrDelete::Set(message.clone()),
);

let mut message2 = message.clone();
message2.fee = Amount::from_str("2").unwrap();
assert_ne!(message.compute_id(), message2.compute_id());

let mut message3 = message.clone();
message3.fee = Amount::from_str("3").unwrap();
assert_ne!(message.compute_id(), message3.compute_id());

changes
.0
.insert(message2.compute_id(), SetUpdateOrDelete::Delete);

let mut update3 = AsyncMessageUpdate::default();
update3.coins = SetOrKeep::Set(Amount::from_str("3").unwrap());

changes
.0
.insert(message3.compute_id(), SetUpdateOrDelete::Update(update3));

assert_eq!(changes.0.len(), 3);

let mut serialized = Vec::new();
let serializer = AsyncPoolChangesSerializer::new();
let deserializer = AsyncPoolChangesDeserializer::new(32, 10000, 10000, 100000, 100000);
serializer.serialize(&changes, &mut serialized).unwrap();
let (rest, changes_deser) = deserializer
.deserialize::<DeserializeError>(&serialized)
.unwrap();
assert!(rest.is_empty());
assert_eq!(changes, changes_deser);
}

#[test]
fn test_pool_changes_push() {
// AsyncPoolChanges, push_add/push_delete/push_activate

let message = get_message();
assert_eq!(message.can_be_executed, false);

let mut changes = AsyncPoolChanges::default();

changes.push_add(message.compute_id(), message.clone());
assert_eq!(changes.0.len(), 1);
assert_matches!(
changes.0.get(&message.compute_id()),
Some(&SetUpdateOrDelete::Set(..))
);

changes.push_activate(message.compute_id());
assert_eq!(changes.0.len(), 1);
let value = changes.0.get(&message.compute_id()).unwrap();
match value {
SetUpdateOrDelete::Set(msg) => {
assert_eq!(msg.can_be_executed, true);
}
_ => {
panic!("Unexpect value");
}
}

changes.push_delete(message.compute_id());
// Len is still 1, but value has changed
assert_eq!(changes.0.len(), 1);
assert_eq!(
changes.0.get(&message.compute_id()),
Some(&SetUpdateOrDelete::Delete)
);
}
}
14 changes: 8 additions & 6 deletions massa-async-pool/src/mapping_grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ impl From<AsyncMessageUpdate> for grpc_model::AsyncMessageUpdate {
)),
}),
},
//TODO remove unwrap
trigger: match value.trigger {
SetOrKeep::Set(value) => Some(grpc_model::SetOrKeepAsyncMessageTrigger {
change: Some(grpc_model::set_or_keep_async_message_trigger::Change::Set(
value.map(|trigger| trigger.into()).unwrap(),
)),
}),
SetOrKeep::Set(value) => match value {
None => Some(grpc_model::SetOrKeepAsyncMessageTrigger { change: None }),
Some(trigger) => Some(grpc_model::SetOrKeepAsyncMessageTrigger {
change: Some(grpc_model::set_or_keep_async_message_trigger::Change::Set(
trigger.into(),
)),
}),
},
SetOrKeep::Keep => Some(grpc_model::SetOrKeepAsyncMessageTrigger {
change: Some(grpc_model::set_or_keep_async_message_trigger::Change::Keep(
grpc_model::Empty {},
Expand Down
22 changes: 21 additions & 1 deletion massa-async-pool/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,9 @@ mod tests {

#[test]
fn no_apply_update_message() {
// Init an AsyncMessage, try to apply an update (with no modifications at all)
// Original async message and 'updated' async message should be ==

let mut msg = AsyncMessage {
emission_slot: Slot::new(0, 0),
emission_index: 0,
Expand Down Expand Up @@ -1067,6 +1070,9 @@ mod tests {

#[test]
fn apply_update_message() {
// Init an AsyncMessage, try to apply an update (with modifications for all fields)
// Original async message and 'updated' async message should be !=

let mut msg = AsyncMessage {
emission_slot: Slot::new(0, 0),
emission_index: 0,
Expand Down Expand Up @@ -1145,6 +1151,8 @@ mod tests {

#[test]
fn apply_update_on_update_message() {
// Apply AsyncMessageUpdate on a AsyncMessageUpdate then apply to an AsyncMessage

let mut msg = AsyncMessage {
emission_slot: Slot::new(0, 0),
emission_index: 0,
Expand Down Expand Up @@ -1240,6 +1248,8 @@ mod tests {

#[test]
fn lower_limit_ser_deser_id() {
// Serialize then Deserialize an AsyncMessageId (with lowest values)

let id: AsyncMessageId = (std::cmp::Reverse(Ratio::new(0, 1)), Slot::new(0, 0), 0);

let mut buffer = Vec::new();
Expand All @@ -1255,6 +1265,8 @@ mod tests {

#[test]
fn higher_limit_ser_deser_id() {
// Serialize then Deserialize an AsyncMessageId (with highest values)

let id: AsyncMessageId = (
std::cmp::Reverse(Ratio::new(u64::MAX, u64::MAX)),
Slot::new(u64::MAX, THREAD_COUNT - 1),
Expand All @@ -1275,6 +1287,7 @@ mod tests {
#[test]
fn wrong_denom_ser_deser_id() {
let id: AsyncMessageId = (
// Ratio with 0 as a denominator -> will fail to deserialize
std::cmp::Reverse(Ratio::new_raw(u64::MAX, 0)),
Slot::new(u64::MAX, THREAD_COUNT - 1),
u64::MAX,
Expand All @@ -1291,6 +1304,8 @@ mod tests {

#[test]
fn lower_limit_ser_deser_message() {
// Serialize then Deserialize an AsyncMessage (with lowest values)

let msg = AsyncMessage {
emission_slot: Slot::new(0, 0),
emission_index: 0,
Expand Down Expand Up @@ -1328,6 +1343,8 @@ mod tests {

#[test]
fn higher_limit_ser_deser_message() {
// Serialize then Deserialize an AsyncMessage (with max values in most of its fields)

let msg = AsyncMessage {
emission_slot: Slot::new(u64::MAX, THREAD_COUNT - 1),
emission_index: u64::MAX,
Expand Down Expand Up @@ -1452,7 +1469,9 @@ mod tests {
}

#[test]
fn bad_serialization_version_message() {
fn bad_serialization_message() {
// Serialize an AsyncMessage, write some crap in the result and try to deserialize it

let message = AsyncMessage::new(
Slot::new(1, 2),
0,
Expand Down Expand Up @@ -1484,6 +1503,7 @@ mod tests {
MAX_DATASTORE_KEY_LENGTH as u32,
false,
);
// Will break the deserialization
serialized[1] = 50;
message_deserializer
.deserialize::<DeserializeError>(&serialized)
Expand Down
Loading

0 comments on commit ffad3ac

Please sign in to comment.