Skip to content

Inner revert #1807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions e2e-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ readme.workspace = true
repository.workspace = true

[dependencies]
alloy-primitives = "1.0.0"
anyhow.workspace = true
futures.workspace = true
hex.workspace = true
scale-value = "0.18.0"
serde_yaml = "0.9.34"
subxt.workspace = true
subxt-signer.workspace = true
tar = "0.4.44"
tc-cli= { path = "../tc-cli", features = ["testnet", "develop"] }
tempfile = "3.19.1"
Expand Down
61 changes: 38 additions & 23 deletions e2e-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tempfile::TempDir;
use testcontainers::{
core::{ContainerAsync, IntoContainerPort, Mount},
runners::AsyncRunner,
GenericImage, ImageExt,
ContainerRequest, GenericImage, ImageExt,
};
use tracing_subscriber::filter::EnvFilter;
use zstd::{Decoder, Encoder};
Expand All @@ -37,18 +37,20 @@ pub struct TestEnvBuilder {
chronicles: HashMap<NetworkId, Vec<Container>>,
config: ConfigYaml,
prices: HashMap<NetworkId, (String, f64)>,
snapshot: PathBuf,
snapshot: Option<PathBuf>,
}

impl TestEnvBuilder {
pub async fn new(snapshot: PathBuf) -> Result<Self> {
pub async fn new(snapshot: Option<PathBuf>) -> Result<Self> {
try_init_logger();
let temp = TempDir::new()?;
if snapshot.exists() {
tracing::info!("found snapshot, applying {}", snapshot.display());
unarchive(&snapshot, temp.path())?;
} else {
tracing::info!("no snapshot found {}", snapshot.display());
if let Some(snapshot) = snapshot.as_ref() {
if snapshot.exists() {
tracing::info!("found snapshot, applying {}", snapshot.display());
unarchive(snapshot, temp.path())?;
} else {
tracing::info!("no snapshot found {}", snapshot.display());
}
}
let network = temp
.path()
Expand Down Expand Up @@ -199,6 +201,21 @@ impl TestEnvBuilder {
network: NetworkId,
shard_size: u16,
shard_threshold: u16,
) -> Result<()> {
let image = GenericImage::new("ghcr.io/foundry-rs/foundry", "latest")
.with_exposed_port(8545.tcp())
.with_env_var("ANVIL_IP_ADDR", "0.0.0.0").with_cmd([
"anvil -b=6 --steps-tracing --order=fifo --base-fee=0 --no-request-size-limit --slots-in-an-epoch 1 --state /state/anvil -s 7",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is block time 6 seconds necessary? wont it make test fast if we reduce it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can try, but I don't think it makes the test faster

]);
self.add_evm_custom(network, shard_size, shard_threshold, image).await
}

pub async fn add_evm_custom(
&mut self,
network: NetworkId,
shard_size: u16,
shard_threshold: u16,
image: ContainerRequest<GenericImage>,
) -> Result<()> {
// add chain to docker compose
let chain_name = format!("chain-evm-{network}");
Expand All @@ -207,15 +224,10 @@ impl TestEnvBuilder {
let chain_name = format!("{}-{chain_name}", &self.network);
let guard = PORT_LOCK.lock().unwrap();
let chain_port = pick_free_port()?;
let chain = GenericImage::new("ghcr.io/foundry-rs/foundry", "latest")
.with_exposed_port(8545.tcp())
let chain = image
.with_mapped_port(chain_port, 8545.tcp())
.with_container_name(&chain_name)
.with_network(self.network.clone())
.with_env_var("ANVIL_IP_ADDR", "0.0.0.0")
.with_cmd([
"anvil -b=6 --steps-tracing --order=fifo --base-fee=0 --no-request-size-limit --slots-in-an-epoch 1 --state /state/anvil -s 7",
])
.with_mount(Mount::bind_mount(chain_mount.to_str().unwrap(), "/state"))
.start()
.await?;
Expand Down Expand Up @@ -326,7 +338,7 @@ pub struct TestEnv {
validator: Container,
chains: HashMap<NetworkId, Container>,
chronicles: HashMap<NetworkId, Vec<Container>>,
snapshot: PathBuf,
snapshot: Option<PathBuf>,
}

impl TestEnv {
Expand All @@ -339,7 +351,7 @@ impl TestEnv {
snapshot.push_str(".tar.zst");
let snapshot_path = std::env::temp_dir().join(snapshot);
let (shard_size, shard_threshold) = if tss { (2, 2) } else { (1, 1) };
let mut builder = TestEnvBuilder::new(snapshot_path).await?;
let mut builder = TestEnvBuilder::new(Some(snapshot_path)).await?;
match backend {
Backend::Evm => {
builder.add_evm(0, shard_size, shard_threshold).await?;
Expand All @@ -354,7 +366,8 @@ impl TestEnv {
},
}
let env = builder.build().await?;
let tc = Tester::new().await?;
let mut tc = Tester::new().await?;
tc.setup_test().await?;
env.snapshot().await?;
Ok((env, tc))
}
Expand All @@ -379,11 +392,14 @@ impl TestEnv {
}

async fn snapshot(&self) -> Result<()> {
if self.snapshot.exists() {
tracing::info!("snapshot found {}", self.snapshot.display());
let Some(snapshot) = self.snapshot.as_ref() else {
return Ok(());
};
if snapshot.exists() {
tracing::info!("snapshot found {}", snapshot.display());
return Ok(());
}
tracing::info!("taking snapshot {}", self.snapshot.display());
tracing::info!("taking snapshot {}", snapshot.display());

for chronicle in self.chronicles.values().flatten() {
chronicle.stop().await?;
Expand All @@ -393,7 +409,7 @@ impl TestEnv {
}
self.validator.stop().await?;

archive(self.env(), &self.snapshot)?;
archive(self.env(), snapshot)?;

self.validator.start().await?;
for chain in self.chains.values() {
Expand Down Expand Up @@ -428,11 +444,10 @@ impl Tester {
try_init_logger();
let env = std::env::var("TC_CLI_ENV").context("TC_CLI_ENV not set")?;
let env = Path::new(&env).to_path_buf();
let mut tc =
let tc =
Tc::from_env(env.clone(), "config.yaml", Sender::default(), env.join("tc-cli-tx.redb"))
.await
.context("Error creating Tc client")?;
tc.setup_test().await?;
Ok(Self { tc })
}
}
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/tests/_smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result;
use e2e_tests::{Backend, TestEnv, Tester};

async fn test_smoke(tc: Tester) -> Result<()> {
tc.exec_smoke(0, 1, vec![42]).await?;
tc.exec_smoke(0, 1, vec![42], None).await?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/tests/cctp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn test_cctp(mut tc: Tester) -> Result<()> {
message: msg_data,
extra_data: [0u8; 32].to_vec(),
};
let msg = tc.exec_smoke(0, 1, cctp_payload.encode()).await?;
let msg = tc.exec_smoke(0, 1, cctp_payload.encode(), None).await?;
let attested = CCTPMessage::from_bytes(&msg.bytes).map_err(|e| anyhow::anyhow!("{:?}", e))?;
assert!(!attested.attestation.is_empty());
assert!(attested.extra_data == cctp_payload.extra_data);
Expand Down
101 changes: 101 additions & 0 deletions e2e-tests/tests/chains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use alloy_primitives::Address;
use anyhow::Result;
use e2e_tests::{Container, TestEnv, TestEnvBuilder, Tester};
use scale_value::Composite;
use subxt::config::substrate::{AccountId32, BlakeTwo256};
use subxt::config::Hasher;
use subxt::dynamic::Value;
use subxt::{OnlineClient, PolkadotConfig};
use subxt_signer::sr25519::dev;
use testcontainers::{core::IntoContainerPort, ContainerRequest, GenericImage, ImageExt};
use time_primitives::Address32;

#[derive(Clone, Copy, Debug)]
enum Chain {
Astar,
}

impl Chain {
fn into_image(self) -> ContainerRequest<GenericImage> {
GenericImage::new("staketechnologies/astar-collator", "v5.28.0-rerun")
.with_exposed_port(8545.tcp())
.with_cmd([
"astar-collator",
"--chain=dev",
"--rpc-cors=all",
"--rpc-external",
"--rpc-port=8545",
"--enable-evm-rpc",
"--alice",
"--tmp",
])
}

async fn fund(self, container: &Container, address: Address32, amount: u128) -> Result<()> {
let url = format!(
"ws://{}:{}",
container.get_host().await?,
container.get_host_port_ipv4(8545.tcp()).await?
);

// convert address
let dest = {
let address = Address::from_word(address.into());
let mut data = [0u8; 24];
data[0..4].copy_from_slice(b"evm:");
data[4..24].copy_from_slice(&address[..]);
let hash = BlakeTwo256::hash(&data);
AccountId32::from(Into::<[u8; 32]>::into(hash))
};

// Build the transfer transaction
let payload = subxt::tx::dynamic(
"Balances",
"transfer_allow_death",
vec![
Value::variant("Id", Composite::Unnamed(vec![Value::from_bytes(dest)])),
amount.into(),
],
);
OnlineClient::<PolkadotConfig>::from_insecure_url(url)
.await?
.tx()
.sign_and_submit_then_watch_default(&payload, &dev::alice())
.await?
.wait_for_finalized_success()
.await?;
Ok(())
}

async fn setup(self) -> Result<(TestEnv, Tester)> {
let mut builder = TestEnvBuilder::new(None).await?;
builder.add_evm_custom(0, 1, 1, self.into_image()).await?;
builder.add_evm_custom(1, 1, 1, self.into_image()).await?;
let env = builder.build().await?;
let mut tc = Tester::new().await?;
self.fund(env.chain_container(0)?, tc.address(Some(0))?, tc.parse_balance(Some(0), "11.")?)
.await?;
self.fund(env.chain_container(1)?, tc.address(Some(1))?, tc.parse_balance(Some(1), "11.")?)
.await?;
tc.setup_test().await?;
Ok((env, tc))
}
}

async fn test_inner_revert(tc: Tester) -> Result<()> {
tc.exec_smoke(0, 1, vec![42], Some(0)).await?;
Ok(())
}

#[tokio::test]
#[ignore]
async fn inner_revert() -> Result<()> {
let tc = Tester::new().await?;
test_inner_revert(tc).await
}

#[tokio::test]
async fn inner_revert_astar() -> Result<()> {
let (_env, tc) = Chain::Astar.setup().await?;
test_inner_revert(tc).await
}
2 changes: 1 addition & 1 deletion e2e-tests/tests/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn test_gateway_payments(tc: Tester) -> Result<()> {
let total_balance = tc.total_gateway_balance(block_hash).await?;
tc.println(None, format!("shard registration msgs cost {}$", total_funds - total_balance))
.await?;
let _ = tc.exec_smoke(0, 1, vec![42]).await?;
let _ = tc.exec_smoke(0, 1, vec![42], None).await?;
let (block_hash, _) = tc.latest_block().await?;
tc.assert_reimbursement(block_hash).await?;
let total_balance_after = tc.total_gateway_balance(block_hash).await?;
Expand Down
6 changes: 3 additions & 3 deletions e2e-tests/tests/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn chronicle_restart(env: &TestEnv, tc: Tester) -> Result<()> {
}

// Re-run smoke test: should still work
tc.exec_smoke(0, 1, vec![42]).await?;
tc.exec_smoke(0, 1, vec![42], None).await?;

Ok(())
}
Expand Down Expand Up @@ -62,7 +62,7 @@ async fn chain_restart(env: &TestEnv, tc: Tester) -> Result<()> {
}

// Re-run smoke test: should still work
tc.exec_smoke(0, 1, vec![42]).await?;
tc.exec_smoke(0, 1, vec![42], None).await?;

Ok(())
}
Expand All @@ -73,7 +73,7 @@ async fn validator_restart(env: &TestEnv, tc: Tester) -> Result<()> {
env.validator_container().start().await?;

// Re-run smoke test: should still work
tc.exec_smoke(0, 1, vec![42]).await?;
tc.exec_smoke(0, 1, vec![42], None).await?;

Ok(())
}
Expand Down
13 changes: 4 additions & 9 deletions tc-cli/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ impl Benchmark {
}
}

async fn route_stats(
&self,
src: NetworkId,
dest: NetworkId,
block_hash: BlockHash,
) -> Result<RouteStats> {
async fn route_stats(&self, src: NetworkId, dest: NetworkId) -> Result<RouteStats> {
let src_addr = self.tc.tester(src)?.0;
let dest_addr = self.tc.tester(dest)?.0;
let gas_limit = self
Expand All @@ -100,18 +95,18 @@ impl Benchmark {
.await?;
let gas_cost = self
.tc
.estimate_message_cost(src, dest, gas_limit, self.payload.clone(), block_hash)
.estimate_message_cost(src, dest, gas_limit, self.payload.clone())
.await?;
let msg_cost = self.tc.balance_to_usd(src, gas_cost)?;
Ok(RouteStats::new(src_addr, dest_addr, gas_limit, gas_cost, msg_cost))
}

pub async fn add_routes(&mut self, block_hash: BlockHash) -> Result<()> {
pub async fn add_routes(&mut self) -> Result<()> {
let routes = FuturesUnordered::new();
for src in self.tc.iter() {
for dest in self.tc.iter() {
if src != dest {
let fut = self.route_stats(src, dest, block_hash);
let fut = self.route_stats(src, dest);
routes.push(async move {
let route = fut.await?;
Ok::<_, anyhow::Error>((src, dest, route))
Expand Down
Loading
Loading