use anyhow::{Context, Result, bail};
use super::funding::request_funds;
use super::invitations::create_invitation_code;
use super::{
assert_rejected_before_submission,
funded_deploy_request,
insert_unfunded_wallet,
is_deployed,
};
use crate::ClientConfig;
use crate::tests::network_transaction::add_network_counter_contract;
pub async fn test_allowlist_registered_account_can_deploy(
client_config: ClientConfig,
) -> Result<()> {
let mut client = client_config.into_client().await?;
client.wait_for_node().await;
let invitation_code = create_invitation_code().await?;
let account = insert_unfunded_wallet(&mut client, Some(&invitation_code))
.await
.context("failed to register the account on the network allowlist")?;
let deploy = funded_deploy_request(&mut client, &account).await?;
let transaction_id = client
.submit_new_transaction(account.id(), deploy)
.await
.context("the node rejected the deploy of a registered account")?;
client.wait_for_tx(transaction_id).await?;
assert!(
is_deployed(&client, &account).await?,
"a registered account should have been created on chain"
);
Ok(())
}
pub async fn test_allowlist_unregistered_account_is_rejected(
client_config: ClientConfig,
) -> Result<()> {
let mut client = client_config.into_client().await?;
client.wait_for_node().await;
let account = insert_unfunded_wallet(&mut client, None).await?;
request_funds(&account).await?;
let deploy = funded_deploy_request(&mut client, &account).await?;
let error = client
.submit_new_transaction(account.id(), deploy)
.await
.expect_err("an unregistered account should not be created");
assert_rejected_before_submission(&error, &account);
assert!(
!is_deployed(&client, &account).await?,
"a rejected account should not have been created on chain"
);
Ok(())
}
pub async fn test_allowlist_network_account_needs_no_registration(
client_config: ClientConfig,
) -> Result<()> {
let mut client = client_config.into_client().await?;
client.wait_for_node().await;
let account = add_network_counter_contract(&mut client, &[]).await?;
request_funds(&account).await?;
let deploy = funded_deploy_request(&mut client, &account).await?;
let transaction_id = client
.submit_new_transaction(account.id(), deploy)
.await
.context("a network account should deploy without being registered")?;
client.wait_for_tx(transaction_id).await?;
assert!(
is_deployed(&client, &account).await?,
"a network account should have been created on chain without registration"
);
Ok(())
}
pub async fn test_allowlist_is_enforced_per_batch(client_config: ClientConfig) -> Result<()> {
let mut client = client_config.into_client().await?;
client.wait_for_node().await;
let invitation_code = create_invitation_code().await?;
let registered = insert_unfunded_wallet(&mut client, Some(&invitation_code))
.await
.context("failed to register the account")?;
let unregistered = insert_unfunded_wallet(&mut client, None).await?;
request_funds(&unregistered).await?;
let registered_request = funded_deploy_request(&mut client, ®istered).await?;
let unregistered_request = funded_deploy_request(&mut client, &unregistered).await?;
let mut batch = client.new_transaction_batch();
batch.push(registered.id(), registered_request).await?;
batch.push(unregistered.id(), unregistered_request).await?;
let Err(error) = batch.submit().await else {
bail!("a batch creating an unregistered account should be refused")
};
assert_rejected_before_submission(&error, &unregistered);
assert!(
!is_deployed(&client, &unregistered).await?,
"the unregistered account should not have been created on chain"
);
assert!(
!is_deployed(&client, ®istered).await?,
"the rejected batch should not have created the registered account"
);
Ok(())
}