Skip to content

Commit

Permalink
fix: not all identities were being used to broadcast documents in str…
Browse files Browse the repository at this point in the history
…ategy tests (#1852)
  • Loading branch information
pauldelucia authored Jul 21, 2024
2 parents 6e0842f + a4ad9c7 commit 9a177ff
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 105 deletions.
18 changes: 6 additions & 12 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,18 @@ pub trait CreateRandomDocument {
/// A `Result<Vec<(Document, Identity, Bytes32)>, ProtocolError>` which is `Ok` containing a vector of tuples
/// if successful, each tuple consisting of a Document, its associated Identity, and a Bytes32 value, or an error
/// if the operation fails.
fn random_documents_with_params(
fn random_documents_with_params<'i>(
&self,
count: u32,
identities: &[Identity],
identities: &[&'i Identity],
time_ms: Option<TimestampMillis>,
block_height: Option<BlockHeight>,
core_block_height: Option<CoreBlockHeight>,
document_field_fill_type: DocumentFieldFillType,
document_field_fill_size: DocumentFieldFillSize,
rng: &mut StdRng,
platform_version: &PlatformVersion,
) -> Result<Vec<(Document, Identity, Bytes32)>, ProtocolError>;
) -> Result<Vec<(Document, &'i Identity, Bytes32)>, ProtocolError>;
}

impl CreateRandomDocument for DocumentType {
Expand Down Expand Up @@ -286,18 +286,18 @@ impl CreateRandomDocument for DocumentType {
), // Add more cases as necessary for other variants
}
}
fn random_documents_with_params(
fn random_documents_with_params<'i>(
&self,
count: u32,
identities: &[Identity],
identities: &[&'i Identity],
time_ms: Option<TimestampMillis>,
block_height: Option<BlockHeight>,
core_block_height: Option<CoreBlockHeight>,
document_field_fill_type: DocumentFieldFillType,
document_field_fill_size: DocumentFieldFillSize,
rng: &mut StdRng,
platform_version: &PlatformVersion,
) -> Result<Vec<(Document, Identity, Bytes32)>, ProtocolError> {
) -> Result<Vec<(Document, &'i Identity, Bytes32)>, ProtocolError> {
match self {
DocumentType::V0(v0) => v0.random_documents_with_params(
count,
Expand Down Expand Up @@ -405,18 +405,18 @@ impl<'a> CreateRandomDocument for DocumentTypeRef<'a> {
}
}

fn random_documents_with_params(
fn random_documents_with_params<'i>(
&self,
count: u32,
identities: &[Identity],
identities: &[&'i Identity],
time_ms: Option<TimestampMillis>,
block_height: Option<BlockHeight>,
core_block_height: Option<CoreBlockHeight>,
document_field_fill_type: DocumentFieldFillType,
document_field_fill_size: DocumentFieldFillSize,
rng: &mut StdRng,
platform_version: &PlatformVersion,
) -> Result<Vec<(Document, Identity, Bytes32)>, ProtocolError> {
) -> Result<Vec<(Document, &'i Identity, Bytes32)>, ProtocolError> {
match self {
DocumentTypeRef::V0(v0) => v0.random_documents_with_params(
count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use platform_value::{Bytes32, Identifier};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use rand::SeedableRng;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::data_contract::document_type::methods::DocumentTypeV0Methods;
Expand Down Expand Up @@ -266,22 +266,28 @@ impl CreateRandomDocument for DocumentTypeV0 {
}

/// Creates `count` Documents with random data using the random number generator given.
fn random_documents_with_params(
fn random_documents_with_params<'i>(
&self,
count: u32,
identities: &[Identity],
identities: &[&'i Identity],
time_ms: Option<TimestampMillis>,
block_height: Option<BlockHeight>,
core_block_height: Option<CoreBlockHeight>,
document_field_fill_type: DocumentFieldFillType,
document_field_fill_size: DocumentFieldFillSize,
rng: &mut StdRng,
platform_version: &PlatformVersion,
) -> Result<Vec<(Document, Identity, Bytes32)>, ProtocolError> {
) -> Result<Vec<(Document, &'i Identity, Bytes32)>, ProtocolError> {
let mut vec = vec![];
for _i in 0..count {
let identity_num = rng.gen_range(0..identities.len());
let identity = identities.get(identity_num).unwrap().clone();

if identities.len() < count as usize {
return Err(ProtocolError::CorruptedCodeExecution(format!(
"not enough identities to create {count} documents"
)));
}

for i in 0..count {
let identity = identities[i as usize];
let entropy = Bytes32::random_with_rng(rng);
vec.push((
self.random_document_with_params(
Expand Down
10 changes: 5 additions & 5 deletions packages/rs-drive-abci/tests/strategy_tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,7 @@ mod tests {
.unwrap()
.unwrap()
),
"6e4fb02e2619715b4a190ee206b560a8d34830a721d02d29bb5731783ba2b08a".to_string()
"cae9c984e5fe548cc7a3edd720ab0f58a2d25e5a16071402605a9ff8cfe870c7".to_string()
)
}

Expand Down Expand Up @@ -2050,7 +2050,7 @@ mod tests {
.unwrap()
.unwrap()
),
"e3cd37a5139f08af5ae5933c71ba7f3d1a922291bb625e78c245f115034f8408".to_string()
"6cbe10061aac832a35ec0eb9b22826aebfcbe697cec76fe899afa1b1803dd2c7".to_string()
)
}

Expand Down Expand Up @@ -2393,7 +2393,7 @@ mod tests {

let outcome =
run_chain_for_strategy(&mut platform, block_count, strategy, config, 15, &mut None);
assert_eq!(outcome.identities.len() as u64, 470);
assert_eq!(outcome.identities.len() as u64, 472);
assert_eq!(outcome.masternode_identity_balances.len(), 100);
let balance_count = outcome
.masternode_identity_balances
Expand Down Expand Up @@ -2528,7 +2528,7 @@ mod tests {

let outcome =
run_chain_for_strategy(&mut platform, block_count, strategy, config, 15, &mut None);
assert_eq!(outcome.identities.len() as u64, 90);
assert_eq!(outcome.identities.len() as u64, 83);
assert_eq!(outcome.masternode_identity_balances.len(), 100);
let balance_count = outcome
.masternode_identity_balances
Expand Down Expand Up @@ -2679,7 +2679,7 @@ mod tests {

let outcome =
run_chain_for_strategy(&mut platform, block_count, strategy, config, 15, &mut None);
assert_eq!(outcome.identities.len() as u64, 97);
assert_eq!(outcome.identities.len() as u64, 79);
assert_eq!(outcome.masternode_identity_balances.len(), 100);
let balance_count = outcome
.masternode_identity_balances
Expand Down
34 changes: 27 additions & 7 deletions packages/rs-drive-abci/tests/strategy_tests/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,29 @@ impl NetworkStrategy {
let mut deleted = vec![];
for op in &self.strategy.operations {
if op.frequency.check_hit(rng) {
let count = rng.gen_range(op.frequency.times_per_block_range.clone());
let mut count = rng.gen_range(op.frequency.times_per_block_range.clone());
match &op.op_type {
OperationType::Document(DocumentOp {
action: DocumentAction::DocumentActionInsertRandom(fill_type, fill_size),
document_type,
contract,
}) => {
if current_identities.len() < count as usize {
count = current_identities.len() as u16;

tracing::warn!(
"Not enough identities to insert documents, reducing count to {}",
count
);
}

let current_identities_as_refs: Vec<&dpp::identity::Identity> =
current_identities.iter().collect();

let documents = document_type
.random_documents_with_params(
count as u32,
current_identities,
current_identities_as_refs.as_ref(),
Some(block_info.time_ms),
Some(block_info.height),
Some(block_info.core_height),
Expand Down Expand Up @@ -673,15 +685,20 @@ impl NetworkStrategy {
contract,
}) => {
let documents = if let Some(identifier) = identifier {
let held_identity = vec![current_identities
let held_identity = current_identities
.iter()
.find(|identity| identity.id() == identifier)
.expect("expected to find identifier, review strategy params")
.clone()];
.expect("expected to find identifier, review strategy params");

let mut eligible_identities = Vec::with_capacity(count as usize);
for _ in 0..count {
eligible_identities.push(held_identity);
}

document_type
.random_documents_with_params(
count as u32,
&held_identity,
&eligible_identities,
Some(block_info.time_ms),
Some(block_info.height),
Some(block_info.core_height),
Expand All @@ -692,10 +709,13 @@ impl NetworkStrategy {
)
.expect("expected random_documents_with_params")
} else {
let current_identities_as_refs: Vec<&dpp::identity::Identity> =
current_identities.iter().collect();

document_type
.random_documents_with_params(
count as u32,
current_identities,
current_identities_as_refs.as_ref(),
Some(block_info.time_ms),
Some(block_info.height),
Some(block_info.core_height),
Expand Down
35 changes: 16 additions & 19 deletions packages/rs-drive-abci/tests/strategy_tests/voting_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mod tests {
),
]),
Some(start_identities.first().unwrap().0.id()),
DocumentFieldFillType::FillIfNotRequired,
DocumentFieldFillType::DoNotFillIfNotRequired,
DocumentFieldFillSize::AnyDocumentFillSize,
),
document_type: document_type.clone(),
Expand All @@ -144,7 +144,7 @@ mod tests {
),
]),
Some(start_identities.last().unwrap().0.id()),
DocumentFieldFillType::FillIfNotRequired,
DocumentFieldFillType::DoNotFillIfNotRequired,
DocumentFieldFillSize::AnyDocumentFillSize,
),
document_type: document_type.clone(),
Expand Down Expand Up @@ -294,35 +294,32 @@ mod tests {
assert_eq!(
first_contender.document,
Some(vec![
0, 24, 85, 248, 135, 55, 81, 210, 5, 93, 112, 104, 77, 97, 177, 49, 255, 108, 242,
0, 83, 232, 168, 214, 145, 55, 49, 246, 246, 126, 99, 17, 108, 41, 18, 75, 231,
0, 23, 127, 36, 121, 9, 10, 2, 134, 166, 125, 106, 31, 103, 181, 99, 181, 21, 24,
237, 214, 238, 160, 70, 24, 41, 247, 214, 48, 253, 101, 112, 141, 41, 18, 75, 231,
232, 111, 151, 233, 89, 137, 74, 103, 169, 204, 7, 140, 62, 1, 6, 212, 191, 207,
191, 52, 188, 64, 58, 79, 9, 153, 37, 180, 1, 0, 7, 0, 0, 1, 135, 105, 8, 149, 152,
0, 0, 1, 135, 105, 8, 149, 152, 0, 0, 1, 135, 105, 8, 149, 152, 0, 7, 113, 117, 97,
110, 116, 117, 109, 7, 113, 117, 97, 110, 116, 117, 109, 1, 9, 112, 48, 81, 101,
48, 107, 49, 65, 122, 4, 100, 97, 115, 104, 48, 165, 41, 91, 32, 215, 12, 4, 215,
10, 9, 207, 71, 187, 248, 211, 105, 252, 147, 22, 127, 31, 203, 145, 6, 255, 132,
220, 231, 96, 76, 195, 34, 1, 41, 18, 75, 231, 232, 111, 151, 233, 89, 137, 74,
103, 169, 204, 7, 140, 62, 1, 6, 212, 191, 207, 191, 52, 188, 64, 58, 79, 9, 153,
37, 180, 0, 1, 0
110, 116, 117, 109, 7, 113, 117, 97, 110, 116, 117, 109, 0, 4, 100, 97, 115, 104,
97, 228, 155, 126, 30, 18, 134, 198, 154, 239, 219, 139, 51, 36, 53, 36, 212, 198,
180, 139, 46, 87, 130, 184, 52, 174, 119, 176, 192, 51, 239, 245, 34, 1, 41, 18,
75, 231, 232, 111, 151, 233, 89, 137, 74, 103, 169, 204, 7, 140, 62, 1, 6, 212,
191, 207, 191, 52, 188, 64, 58, 79, 9, 153, 37, 180, 0, 1, 1
])
);

assert_eq!(
second_contender.document,
Some(vec![
0, 23, 193, 35, 24, 227, 101, 215, 103, 217, 98, 152, 114, 80, 94, 3, 27, 65, 246,
202, 212, 59, 205, 101, 140, 243, 61, 26, 152, 167, 199, 96, 133, 139, 137, 72,
0, 73, 14, 33, 37, 147, 161, 211, 204, 106, 225, 123, 241, 7, 171, 156, 180, 101,
23, 94, 120, 119, 252, 247, 208, 133, 237, 47, 206, 39, 190, 17, 214, 139, 137, 72,
166, 128, 21, 1, 187, 224, 67, 30, 61, 153, 77, 207, 113, 207, 90, 42, 9, 57, 254,
81, 176, 230, 0, 7, 97, 153, 171, 164, 251, 1, 0, 7, 0, 0, 1, 135, 105, 8, 149,
152, 0, 0, 1, 135, 105, 8, 149, 152, 0, 0, 1, 135, 105, 8, 149, 152, 0, 7, 113,
117, 97, 110, 116, 117, 109, 7, 113, 117, 97, 110, 116, 117, 109, 1, 36, 65, 50,
104, 52, 88, 69, 66, 112, 116, 74, 101, 99, 48, 101, 98, 87, 53, 67, 52, 89, 106,
72, 119, 82, 81, 48, 51, 88, 54, 83, 99, 75, 103, 89, 111, 97, 4, 100, 97, 115,
104, 110, 35, 254, 120, 68, 194, 240, 23, 122, 207, 220, 40, 135, 147, 185, 9, 126,
239, 26, 0, 22, 196, 197, 243, 182, 218, 58, 240, 230, 102, 185, 157, 34, 1, 139,
137, 72, 166, 128, 21, 1, 187, 224, 67, 30, 61, 153, 77, 207, 113, 207, 90, 42, 9,
57, 254, 81, 176, 230, 0, 7, 97, 153, 171, 164, 251, 0, 1, 0
117, 97, 110, 116, 117, 109, 7, 113, 117, 97, 110, 116, 117, 109, 0, 4, 100, 97,
115, 104, 148, 190, 52, 134, 51, 240, 205, 206, 235, 85, 72, 228, 129, 105, 99,
173, 158, 47, 240, 100, 50, 213, 109, 127, 82, 26, 217, 2, 184, 229, 172, 129, 34,
1, 139, 137, 72, 166, 128, 21, 1, 187, 224, 67, 30, 61, 153, 77, 207, 113, 207, 90,
42, 9, 57, 254, 81, 176, 230, 0, 7, 97, 153, 171, 164, 251, 0, 1, 0
])
);

Expand Down
Loading

0 comments on commit 9a177ff

Please sign in to comment.