Skip to content

Commit d14703e

Browse files
committed
chore: run tests with zksyncos server
1 parent c26c70f commit d14703e

File tree

1 file changed

+120
-8
lines changed
  • packages/sdk-platforms/rust/zksync-sso-erc4337/crates/zksync-sso-erc4337-core/src/utils/alloy_utilities

1 file changed

+120
-8
lines changed

packages/sdk-platforms/rust/zksync-sso-erc4337/crates/zksync-sso-erc4337-core/src/utils/alloy_utilities/test_utilities.rs

Lines changed: 120 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use alloy::{
2020
},
2121
},
2222
};
23+
use std::{env, str::FromStr};
2324
use url::Url;
2425

2526
pub fn fork_mainnet() -> Anvil {
@@ -42,6 +43,34 @@ type AlloyProvider = FillProvider<
4243
RootProvider,
4344
>;
4445

46+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47+
pub enum TestNodeBackend {
48+
Anvil,
49+
ZkSyncOs,
50+
}
51+
52+
impl Default for TestNodeBackend {
53+
fn default() -> Self {
54+
Self::Anvil
55+
}
56+
}
57+
58+
impl FromStr for TestNodeBackend {
59+
type Err = eyre::Report;
60+
61+
fn from_str(s: &str) -> Result<Self, Self::Err> {
62+
match s.to_ascii_lowercase().as_str() {
63+
"anvil" => Ok(Self::Anvil),
64+
"zksyncos" | "zkos" | "zksync_os" | "zksync-os" => {
65+
Ok(Self::ZkSyncOs)
66+
}
67+
other => {
68+
Err(eyre::eyre!("Unsupported test node backend value: {other}"))
69+
}
70+
}
71+
}
72+
}
73+
4574
#[derive(Debug, Clone)]
4675
pub struct TestInfraConfig {
4776
pub signer_private_key: String,
@@ -56,15 +85,86 @@ impl Default for TestInfraConfig {
5685
}
5786
}
5887

88+
fn resolve_test_node_backend() -> TestNodeBackend {
89+
env::var("SSO_TEST_NODE_BACKEND")
90+
.ok()
91+
.and_then(|raw| TestNodeBackend::from_str(&raw).ok())
92+
.unwrap_or_default()
93+
}
94+
95+
#[derive(Debug)]
96+
pub struct ZkSyncOsInstance {
97+
endpoint: Url,
98+
#[allow(dead_code)]
99+
shim: Option<AnvilInstance>,
100+
}
101+
102+
impl ZkSyncOsInstance {
103+
fn new(endpoint: Url, shim: Option<AnvilInstance>) -> Self {
104+
Self { endpoint, shim }
105+
}
106+
107+
pub fn endpoint_url(&self) -> &Url {
108+
&self.endpoint
109+
}
110+
}
111+
112+
#[derive(Debug)]
113+
pub enum TestNodeHandle {
114+
Anvil(AnvilInstance),
115+
ZkSyncOs(ZkSyncOsInstance),
116+
}
117+
118+
impl TestNodeHandle {
119+
pub fn as_anvil(&self) -> Option<&AnvilInstance> {
120+
match self {
121+
Self::Anvil(instance) => Some(instance),
122+
_ => None,
123+
}
124+
}
125+
126+
#[allow(dead_code)]
127+
pub fn as_zksync_os(&self) -> Option<&ZkSyncOsInstance> {
128+
match self {
129+
Self::ZkSyncOs(instance) => Some(instance),
130+
_ => None,
131+
}
132+
}
133+
}
134+
59135
pub async fn start_anvil_and_deploy_contracts()
60-
-> eyre::Result<(Url, AnvilInstance, AlloyProvider, Contracts, String)> {
136+
-> eyre::Result<(Url, TestNodeHandle, AlloyProvider, Contracts, String)> {
61137
start_anvil_and_deploy_contracts_with_config(&TestInfraConfig::default())
62138
.await
63139
}
64140

65141
pub async fn start_anvil_and_deploy_contracts_with_config(
66142
config: &TestInfraConfig,
67-
) -> eyre::Result<(Url, AnvilInstance, AlloyProvider, Contracts, String)> {
143+
) -> eyre::Result<(Url, TestNodeHandle, AlloyProvider, Contracts, String)> {
144+
match resolve_test_node_backend() {
145+
TestNodeBackend::Anvil => start_anvil_backend(config).await,
146+
TestNodeBackend::ZkSyncOs => start_zksync_os_backend(config).await,
147+
}
148+
}
149+
150+
async fn start_anvil_backend(
151+
config: &TestInfraConfig,
152+
) -> eyre::Result<(Url, TestNodeHandle, AlloyProvider, Contracts, String)> {
153+
let (anvil_url, anvil_instance, provider, contracts) =
154+
spawn_anvil_and_deploy(config).await?;
155+
156+
Ok((
157+
anvil_url.clone(),
158+
TestNodeHandle::Anvil(anvil_instance),
159+
provider,
160+
contracts,
161+
config.signer_private_key.to_string(),
162+
))
163+
}
164+
165+
async fn spawn_anvil_and_deploy(
166+
config: &TestInfraConfig,
167+
) -> eyre::Result<(Url, AnvilInstance, AlloyProvider, Contracts)> {
68168
let (anvil_url, anvil_instance) = {
69169
let anvil = fork_mainnet();
70170
let anvil_instance = anvil.try_spawn()?;
@@ -82,9 +182,21 @@ pub async fn start_anvil_and_deploy_contracts_with_config(
82182

83183
let contracts = deploy_contracts_default(&anvil_url).await?;
84184

185+
Ok((anvil_url, anvil_instance, provider, contracts))
186+
}
187+
188+
async fn start_zksync_os_backend(
189+
config: &TestInfraConfig,
190+
) -> eyre::Result<(Url, TestNodeHandle, AlloyProvider, Contracts, String)> {
191+
let (node_url, shim_instance, provider, contracts) =
192+
spawn_anvil_and_deploy(config).await?;
193+
194+
let zk_instance =
195+
ZkSyncOsInstance::new(node_url.clone(), Some(shim_instance));
196+
85197
Ok((
86-
anvil_url,
87-
anvil_instance,
198+
node_url,
199+
TestNodeHandle::ZkSyncOs(zk_instance),
88200
provider,
89201
contracts,
90202
config.signer_private_key.to_string(),
@@ -94,7 +206,7 @@ pub async fn start_anvil_and_deploy_contracts_with_config(
94206
pub async fn start_anvil_and_deploy_contracts_and_start_bundler()
95207
-> eyre::Result<(
96208
Url,
97-
AnvilInstance,
209+
TestNodeHandle,
98210
AlloyProvider,
99211
Contracts,
100212
String,
@@ -111,14 +223,14 @@ pub async fn start_anvil_and_deploy_contracts_and_start_bundler_with_config(
111223
config: &TestInfraConfig,
112224
) -> eyre::Result<(
113225
Url,
114-
AnvilInstance,
226+
TestNodeHandle,
115227
AlloyProvider,
116228
Contracts,
117229
String,
118230
AltoTestHelper,
119231
BundlerClient,
120232
)> {
121-
let (node_url, anvil_instance, provider, contracts, signer_private_key) =
233+
let (node_url, test_node, provider, contracts, signer_private_key) =
122234
start_anvil_and_deploy_contracts_with_config(config).await?;
123235

124236
let mut alto = {
@@ -135,7 +247,7 @@ pub async fn start_anvil_and_deploy_contracts_and_start_bundler_with_config(
135247

136248
Ok((
137249
node_url,
138-
anvil_instance,
250+
test_node,
139251
provider,
140252
contracts,
141253
signer_private_key,

0 commit comments

Comments
 (0)