Skip to content

Commit

Permalink
Merge pull request #1266 from iotaledger/sc-platform/fix-test-outputs…
Browse files Browse the repository at this point in the history
…-generation

fix(iota-genesis-builder): Settle random generation of test data only snapshot
  • Loading branch information
lzpap authored Jul 22, 2024
2 parents ade40e6 + f1c6b04 commit e9ee3d4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 61 deletions.
17 changes: 10 additions & 7 deletions crates/iota-framework/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ fn build_packages_with_move_config(
/// * Replace html tags and use Docusaurus components where needed.
fn relocate_docs(prefix: &str, files: &[(String, String)], output: &mut BTreeMap<String, String>) {
// Turn on multi-line mode so that `.` matches newlines, consume from the start
// of the file to beginning of the heading, then capture the heading as three different parts and
// replace with the yaml tag for docusaurus, add the Link import and the title anchor,
// so the tile can be linked to. E.g., ```
// of the file to beginning of the heading, then capture the heading as three
// different parts and replace with the yaml tag for docusaurus, add the
// Link import and the title anchor, so the tile can be linked to. E.g., ```
// -<a name="0x2_display"></a>
// -
// -# Module `0x2::display`
Expand Down Expand Up @@ -267,16 +267,19 @@ fn relocate_docs(prefix: &str, files: &[(String, String)], output: &mut BTreeMap
new_path.to_string_lossy().to_string()
};

// Replace a-tags with Link to register anchors in Docusaurus (we have to use the `id` attribute as `name` is deprecated and not existing in Link component)
let content = link_from_regex.replace_all(&file_content, r#"<Link id="$1"></Link>"#);
// Replace a-tags with Link to register anchors in Docusaurus (we have to use
// the `id` attribute as `name` is deprecated and not existing in Link
// component)
let content = link_from_regex.replace_all(file_content, r#"<Link id="$1"></Link>"#);

// Replace a-tags with href for Link tags to enable link and anchor checking. We need to make sure that `to` path don't contain extensions in a later step.
// Replace a-tags with href for Link tags to enable link and anchor checking. We
// need to make sure that `to` path don't contain extensions in a later step.
let content = link_to_regex.replace_all(&content, r#"<Link to="$1">$2</Link>"#);

// Escape `{` in <code> and add new lines as this is a requirement from mdx
let content = code_regex.replace_all(&content, |caps: &regex::Captures| {
let code_content = caps.get(1).unwrap().as_str();
format!("<code>\n{}</code>", code_content.replace("{", "\\{"))
format!("<code>\n{}</code>", code_content.replace('{', "\\{"))
});

// Wrap types like '<IOTA>', '<T>' and more in backticks as they are seen as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use iota_sdk::{
},
},
};
use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{rngs::StdRng, Rng};

use crate::stardust::{
test_outputs::{MERGE_MILESTONE_INDEX, MERGE_TIMESTAMP_SECS},
Expand All @@ -30,20 +30,17 @@ const MNEMONIC: &str = "few hood high omit camp keep burger give happy iron evol
const COIN_TYPE: u32 = 4218;
const OWNING_ALIAS_COUNT: u32 = 10;

pub(crate) async fn outputs(randomness_seed: u64) -> anyhow::Result<Vec<(OutputHeader, Output)>> {
pub(crate) async fn outputs(rng: &mut StdRng) -> anyhow::Result<Vec<(OutputHeader, Output)>> {
let mut outputs = Vec::new();
let secret_manager = MnemonicSecretManager::try_from_mnemonic(MNEMONIC)?;

// create a randomized ownership dependency tree
let mut rng = StdRng::seed_from_u64(randomness_seed);

let alias_owners = secret_manager
.generate_ed25519_addresses(COIN_TYPE, 0, 0..OWNING_ALIAS_COUNT, None)
.await?;

// create 10 different alias outputs with each owning various other assets
for alias_owner in alias_owners {
let alias_output_header = random_output_header(&mut rng);
let alias_output_header = random_output_header(rng);

let alias_output = AliasOutputBuilder::new_with_amount(
1_000_000,
Expand All @@ -69,14 +66,14 @@ pub(crate) async fn outputs(randomness_seed: u64) -> anyhow::Result<Vec<(OutputH
match rng.gen_range(0..=3) {
0 => {
// alias
let (output_header, alias) = random_alias_output(&mut rng, owning_addr)?;
let (output_header, alias) = random_alias_output(rng, owning_addr)?;
owning_addresses
.push_back((depth + 1, AliasAddress::new(*alias.alias_id()).into()));
outputs.push((output_header, alias.into()));
}
1 => {
// nft
let (output_header, nft) = random_nft_output(&mut rng, owning_addr)?;
let (output_header, nft) = random_nft_output(rng, owning_addr)?;
owning_addresses.push_back((
depth + 1,
nft.nft_address(&output_header.output_id()).into(),
Expand All @@ -85,14 +82,14 @@ pub(crate) async fn outputs(randomness_seed: u64) -> anyhow::Result<Vec<(OutputH
}
2 => {
// basic
let (output_header, basic) = random_basic_output(&mut rng, owning_addr)?;
let (output_header, basic) = random_basic_output(rng, owning_addr)?;
outputs.push((output_header, basic.into()));
}
3 => {
// foundry
if let Address::Alias(owning_addr) = owning_addr {
let (output_header, foundry) =
random_foundry_output(&mut rng, &mut serial_number, owning_addr)?;
random_foundry_output(rng, &mut serial_number, owning_addr)?;
outputs.push((output_header, foundry.into()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use iota_sdk::types::block::{
},
};
use iota_types::timelock::timelock::VESTED_REWARD_ID_PREFIX;
use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{rngs::StdRng, Rng};

use super::to_micros;
use crate::stardust::types::{
Expand Down Expand Up @@ -81,19 +81,18 @@ pub(crate) fn new_vested_output(
}

pub fn outputs(
randomness_seed: u64,
rng: &mut StdRng,
vested_index: &mut u32,
delegator: Ed25519Address,
) -> anyhow::Result<Vec<(OutputHeader, Output)>> {
let mut rng = StdRng::seed_from_u64(randomness_seed);
let mut new_outputs = Vec::new();

// Add gas coins to delegator
for _ in 0..DELEGATOR_GAS_COIN_NUM {
new_outputs.push(new_simple_basic_output(
DELEGATOR_GAS_COIN_AMOUNT_PER_OUTPUT,
delegator,
&mut rng,
rng,
)?);
}

Expand All @@ -104,7 +103,7 @@ pub fn outputs(
DELEGATOR_TIMELOCKS_AMOUNT_PER_OUTPUT,
delegator,
Some(MERGE_TIMESTAMP_SECS + TIMELOCK_MAX_ENDING_TIME),
&mut rng,
rng,
)?);
*vested_index -= 1;
}
Expand Down
24 changes: 12 additions & 12 deletions crates/iota-genesis-builder/src/stardust/test_outputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,21 @@ pub async fn add_snapshot_test_outputs<const VERIFY: bool>(
let mut new_header = parser.header.clone();
let mut vested_index = u32::MAX;

let mut rng = StdRng::seed_from_u64(randomness_seed);
let mut new_outputs = [
alias_ownership::outputs(randomness_seed).await?,
stardust_mix::outputs(randomness_seed, &mut vested_index).await?,
vesting_schedule_entity::outputs(randomness_seed, &mut vested_index).await?,
vesting_schedule_iota_airdrop::outputs(randomness_seed, &mut vested_index).await?,
vesting_schedule_portfolio_mix::outputs(randomness_seed, &mut vested_index).await?,
alias_ownership::outputs(&mut rng).await?,
stardust_mix::outputs(&mut rng, &mut vested_index).await?,
vesting_schedule_entity::outputs(&mut rng, &mut vested_index).await?,
vesting_schedule_iota_airdrop::outputs(&mut rng, &mut vested_index).await?,
vesting_schedule_portfolio_mix::outputs(&mut rng, &mut vested_index).await?,
]
.concat();

let new_temp_amount = new_outputs.iter().map(|o| o.1.amount()).sum::<u64>();

new_outputs.append(&mut if let Some(delegator) = delegator.into() {
add_only_test_outputs(
randomness_seed,
&mut rng,
&mut vested_index,
delegator,
with_sampling.then_some(&mut parser),
Expand Down Expand Up @@ -148,19 +149,18 @@ fn add_all_previous_outputs_and_test_outputs<R: Read>(
/// with a timelocked staking. Optionally some samples from the previous
/// snapshot are taken too.
fn add_only_test_outputs<R: Read>(
randomness_seed: u64,
rng: &mut StdRng,
vested_index: &mut u32,
delegator: Ed25519Address,
parser: Option<&mut HornetSnapshotParser<R>>,
new_temp_amount: u64,
) -> anyhow::Result<Vec<(OutputHeader, Output)>> {
let mut rng = StdRng::seed_from_u64(randomness_seed);
// Needed outputs for delegator
let mut new_outputs = delegator_outputs::outputs(randomness_seed, vested_index, delegator)?;
let mut new_outputs = delegator_outputs::outputs(rng, vested_index, delegator)?;

// Additional sample outputs
if let Some(parser) = parser {
new_outputs.append(&mut with_sampling(parser, &mut rng)?)
new_outputs.append(&mut with_sampling(parser, rng)?)
}

// Add all the remainder tokens to the zero address
Expand All @@ -174,11 +174,11 @@ fn add_only_test_outputs<R: Read>(
new_outputs.push(new_simple_basic_output(
remainder_per_output,
zero_address,
&mut rng,
rng,
)?);
}
if difference > 0 {
new_outputs.push(new_simple_basic_output(difference, zero_address, &mut rng)?);
new_outputs.push(new_simple_basic_output(difference, zero_address, rng)?);
}

Ok(new_outputs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use iota_sdk::{
},
},
};
use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{rngs::StdRng, Rng};

use crate::stardust::{
test_outputs::new_vested_output,
Expand Down Expand Up @@ -166,10 +166,9 @@ const STARDUST_MIX: &[StardustWallet] = &[
];

pub(crate) async fn outputs(
randomness_seed: u64,
rng: &mut StdRng,
vested_index: &mut u32,
) -> anyhow::Result<Vec<(OutputHeader, Output)>> {
let mut rng = StdRng::seed_from_u64(randomness_seed);
let mut outputs = Vec::new();

for wallet in STARDUST_MIX {
Expand All @@ -190,7 +189,7 @@ pub(crate) async fn outputs(

// Random add up to 2 aliases with foundry and native tokens
let (alias_foundry_outputs, native_tokens_for_basic_outputs) =
random_alias_foundry_native_token(address, &mut rng)?;
random_alias_foundry_native_token(address, rng)?;
let native_tokens_for_nft_outputs = native_tokens_for_basic_outputs.clone();
outputs.extend(alias_foundry_outputs);

Expand All @@ -199,14 +198,14 @@ pub(crate) async fn outputs(
OUTPUT_IOTA_AMOUNT,
address,
None,
&mut rng,
rng,
)?);
*vested_index -= 1;
outputs.extend(new_basic_or_nft_outputs(
OutputBuilder::Basic(BasicOutputBuilder::new_with_amount(OUTPUT_IOTA_AMOUNT)),
address,
native_tokens_for_basic_outputs,
&mut rng,
rng,
)?);
outputs.extend(new_basic_or_nft_outputs(
OutputBuilder::Nft(NftOutputBuilder::new_with_amount(
Expand All @@ -215,7 +214,7 @@ pub(crate) async fn outputs(
)),
address,
native_tokens_for_nft_outputs,
&mut rng,
rng,
)?);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use iota_sdk::{
client::secret::{mnemonic::MnemonicSecretManager, SecretManage},
types::block::output::Output,
};
use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{rngs::StdRng, Rng};

use crate::stardust::{
test_outputs::{new_vested_output, MERGE_TIMESTAMP_SECS},
Expand All @@ -22,14 +22,12 @@ const VESTING_WEEKS: usize = 208;
const VESTING_WEEKS_FREQUENCY: usize = 2;

pub(crate) async fn outputs(
randomness_seed: u64,
rng: &mut StdRng,
vested_index: &mut u32,
) -> anyhow::Result<Vec<(OutputHeader, Output)>> {
let mut outputs = Vec::new();
let secret_manager = MnemonicSecretManager::try_from_mnemonic(MNEMONIC)?;

let mut rng = StdRng::seed_from_u64(randomness_seed);

let address = secret_manager
.generate_ed25519_addresses(COIN_TYPE, 0, 0..1, None)
.await?[0];
Expand All @@ -48,7 +46,7 @@ pub(crate) async fn outputs(
initial_unlock_amount,
address,
None,
&mut rng,
rng,
)?);
*vested_index -= 1;

Expand All @@ -60,7 +58,7 @@ pub(crate) async fn outputs(
vested_amount,
address,
Some(timelock),
&mut rng,
rng,
)?);
*vested_index -= 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use iota_sdk::{
client::secret::{mnemonic::MnemonicSecretManager, SecretManage},
types::block::output::Output,
};
use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{rngs::StdRng, Rng};

use crate::stardust::{
test_outputs::{new_vested_output, MERGE_TIMESTAMP_SECS},
Expand All @@ -29,7 +29,7 @@ const VESTING_WEEKS: usize = 104;
const VESTING_WEEKS_FREQUENCY: usize = 2;

pub(crate) async fn outputs(
randomness_seed: u64,
rng: &mut StdRng,
vested_index: &mut u32,
) -> anyhow::Result<Vec<(OutputHeader, Output)>> {
let now = SystemTime::now()
Expand All @@ -38,8 +38,6 @@ pub(crate) async fn outputs(
let mut outputs = Vec::new();
let secret_manager = MnemonicSecretManager::try_from_mnemonic(MNEMONIC)?;

let mut rng = StdRng::seed_from_u64(randomness_seed);

for account_index in 0..ACCOUNTS {
for address_index in 0..ADDRESSES_PER_ACCOUNT {
let address = secret_manager
Expand Down Expand Up @@ -72,7 +70,7 @@ pub(crate) async fn outputs(
initial_unlock_amount,
address,
None,
&mut rng,
rng,
)?);
*vested_index -= 1;
}
Expand All @@ -88,7 +86,7 @@ pub(crate) async fn outputs(
vested_amount,
address,
Some(timelock),
&mut rng,
rng,
)?);
*vested_index -= 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use iota_sdk::{
},
},
};
use rand::{rngs::StdRng, Rng, SeedableRng};
use rand::{rngs::StdRng, Rng};

use crate::stardust::{
test_outputs::{new_vested_output, MERGE_MILESTONE_INDEX, MERGE_TIMESTAMP_SECS},
Expand All @@ -39,14 +39,12 @@ const ADDRESSES: &[[u32; 3]] = &[
];

pub(crate) async fn outputs(
randomness_seed: u64,
rng: &mut StdRng,
vested_index: &mut u32,
) -> anyhow::Result<Vec<(OutputHeader, Output)>> {
let mut outputs = Vec::new();
let secret_manager = MnemonicSecretManager::try_from_mnemonic(MNEMONIC)?;

let mut rng = StdRng::seed_from_u64(randomness_seed);

for [account_index, internal, address_index] in ADDRESSES {
let address = secret_manager
.generate_ed25519_addresses(
Expand All @@ -59,14 +57,14 @@ pub(crate) async fn outputs(

match address_index {
0 => {
add_random_basic_output(&mut outputs, &mut rng, address)?;
add_random_basic_output(&mut outputs, rng, address)?;
}
1 => {
add_vested_outputs(&mut outputs, &mut rng, vested_index, address)?;
add_vested_outputs(&mut outputs, rng, vested_index, address)?;
}
2 => {
add_random_basic_output(&mut outputs, &mut rng, address)?;
add_vested_outputs(&mut outputs, &mut rng, vested_index, address)?;
add_random_basic_output(&mut outputs, rng, address)?;
add_vested_outputs(&mut outputs, rng, vested_index, address)?;
}
_ => unreachable!(),
}
Expand Down

0 comments on commit e9ee3d4

Please sign in to comment.