Skip to content
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

[do not merge] Experimenting with static strategy #789

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f1c871c
add strategy objects
nbaztec Dec 12, 2024
b38109d
remove unused parts
nbaztec Dec 12, 2024
57f32f2
fix test build
nbaztec Dec 12, 2024
06aedf4
remove use_zk completely, fix ExecutorStrategy trait
nbaztec Dec 13, 2024
5d77f8c
fix deadlock
nbaztec Dec 14, 2024
6c00749
clippy
nbaztec Dec 14, 2024
b5fd952
fix tests, startup bug
nbaztec Dec 15, 2024
9009698
switch to try_lock to prevent deadlocks
nbaztec Dec 15, 2024
bee659d
revert try_lock
nbaztec Dec 15, 2024
b5b7e5f
noop instead of panic on zksync methods for evm, clippy
nbaztec Dec 15, 2024
5ee99fc
fix warp and roll
nbaztec Dec 15, 2024
2151459
fix script
nbaztec Dec 15, 2024
e9fbf13
make call immutable
nbaztec Dec 15, 2024
7b23078
deep clone strategies on clone
nbaztec Dec 16, 2024
095903b
trigger ci
nbaztec Dec 16, 2024
da60354
revert unintended change
nbaztec Dec 16, 2024
f740454
Merge branch 'main' into nish-abstraction-strategy
nbaztec Dec 16, 2024
193f955
fix zk cheatcodes in evm context
nbaztec Dec 16, 2024
a506866
fix get_code, remove unintended sleep
nbaztec Dec 16, 2024
0e9c64f
fix script test, clippy
nbaztec Dec 16, 2024
589c8ba
Merge branch 'main' into nish-abstraction-strategy
nbaztec Dec 16, 2024
f0d0f9d
fix zk_env
nbaztec Dec 16, 2024
d80312f
guard zk provider
nbaztec Dec 16, 2024
bdba380
use blocking provider call
nbaztec Dec 16, 2024
53d5d9a
Merge branch 'main' into nish-abstraction-strategy
nbaztec Dec 16, 2024
1876c01
use Box instead of Arc<Mutex<T>>
nbaztec Dec 17, 2024
d92ad57
chore: snapshot
popzxc Dec 18, 2024
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
Prev Previous commit
Next Next commit
switch to try_lock to prevent deadlocks
  • Loading branch information
nbaztec committed Dec 15, 2024
commit 90096986c344a34d0cf9e44e3442e8263d7663a1
16 changes: 8 additions & 8 deletions crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Cheatcode for getNonce_0Call {
let Self { account } = self;

let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.cheatcode_get_nonce(ccx, *account)
}
}
Expand Down Expand Up @@ -353,7 +353,7 @@ impl Cheatcode for rollCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { newHeight } = self;
let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.cheatcode_roll(ccx, *newHeight)
}
}
Expand All @@ -377,7 +377,7 @@ impl Cheatcode for warpCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { newTimestamp } = self;
let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.cheatcode_warp(ccx, *newTimestamp)
}
}
Expand Down Expand Up @@ -414,7 +414,7 @@ impl Cheatcode for dealCall {
let Self { account: address, newBalance: new_balance } = *self;

let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.cheatcode_deal(ccx, address, new_balance)
}
}
Expand All @@ -424,7 +424,7 @@ impl Cheatcode for etchCall {
let Self { target, newRuntimeBytecode } = self;

let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.cheatcode_etch(ccx, *target, newRuntimeBytecode)
}
}
Expand All @@ -433,7 +433,7 @@ impl Cheatcode for resetNonceCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { account } = self;
let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.cheatcode_reset_nonce(ccx, *account)
}
}
Expand All @@ -443,7 +443,7 @@ impl Cheatcode for setNonceCall {
let Self { account, newNonce } = *self;

let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.cheatcode_set_nonce(ccx, account, newNonce)
}
}
Expand All @@ -453,7 +453,7 @@ impl Cheatcode for setNonceUnsafeCall {
let Self { account, newNonce } = *self;

let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.cheatcode_set_nonce_unsafe(ccx, account, newNonce)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cheatcodes/src/evm/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Cheatcode for mockCall_0Call {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { callee, data, returnData } = self;
let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.mock_call(ccx, *callee, data, returnData)
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Cheatcode for mockCallRevert_0Call {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { callee, data, revertData } = self;
let strategy = ccx.state.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.mock_call_revert(ccx, *callee, data, revertData)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cheatcodes/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl Cheatcode for getCodeCall {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { artifactPath: path } = self;
let strategy = state.strategy.clone();
let guard = strategy.lock().expect("failed acquiring strategy");
let guard = strategy.try_lock().expect("failed acquiring strategy");
guard.get_artifact_code(state, path, false)
}
}
Expand Down
11 changes: 8 additions & 3 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ impl Cheatcodes {
}

let strategy = self.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
if let Some(result) = guard.zksync_try_create(self, ecx, &input, executor) {
return Some(result);
}
Expand Down Expand Up @@ -1181,7 +1181,7 @@ where {
}

let strategy = self.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
if let Some(result) = guard.zksync_try_call(self, ecx, &call, executor) {
return Some(result);
}
Expand Down Expand Up @@ -1291,7 +1291,12 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {

#[inline]
fn step_end(&mut self, interpreter: &mut Interpreter, ecx: Ecx) {
if self.strategy.lock().expect("failed acquiring strategy").pre_step_end(interpreter, ecx) {
if self
.strategy
.try_lock()
.expect("failed acquiring strategy")
.pre_step_end(interpreter, ecx)
{
return;
}

Expand Down
6 changes: 5 additions & 1 deletion crates/cheatcodes/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ impl Cheatcode for zkVmCall {

impl Cheatcode for zkVmSkipCall {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
ccx.state.strategy.lock().expect("failed acquiring strategy").zksync_cheatcode_skip_zkvm()
ccx.state
.strategy
.try_lock()
.expect("failed acquiring strategy")
.zksync_cheatcode_skip_zkvm()
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/evm/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ impl DatabaseExt for Backend {
caller_account.into()
});

self.strategy.lock().expect("failed acquiring strategy").update_fork_db(
self.strategy.try_lock().expect("failed acquiring strategy").update_fork_db(
BackendStrategyForkInfo {
active_fork: self.active_fork(),
active_type: current_fork_type,
Expand Down Expand Up @@ -1810,7 +1810,7 @@ impl BackendInner {
// we initialize a _new_ `ForkDB` but keep the state of persistent accounts
let mut new_db = ForkDB::new(backend);
for addr in self.persistent_accounts.iter().copied() {
strategy.lock().expect("failed acquiring strategy").merge_db_account_data(
strategy.try_lock().expect("failed acquiring strategy").merge_db_account_data(
addr,
&active.db,
&mut new_db,
Expand Down
11 changes: 7 additions & 4 deletions crates/evm/evm/src/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl Executor {
pub fn set_balance(&mut self, address: Address, amount: U256) -> BackendResult<()> {
trace!(?address, ?amount, "setting account balance");
let strategy = self.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.set_balance(self, address, amount)
}

Expand All @@ -220,7 +220,7 @@ impl Executor {
/// Set the nonce of an account.
pub fn set_nonce(&mut self, address: Address, nonce: u64) -> BackendResult<()> {
let strategy = self.strategy.clone();
let mut guard = strategy.lock().expect("failed acquiring strategy");
let mut guard = strategy.try_lock().expect("failed acquiring strategy");
guard.set_nonce(self, address, nonce)
}

Expand Down Expand Up @@ -254,7 +254,10 @@ impl Executor {

#[inline]
pub fn set_transaction_other_fields(&mut self, other_fields: OtherFields) {
self.strategy.lock().expect("failed acquiring strategy").set_inspect_context(other_fields);
self.strategy
.try_lock()
.expect("failed acquiring strategy")
.set_inspect_context(other_fields);
}

/// Deploys a contract and commits the new state to the underlying database.
Expand Down Expand Up @@ -437,7 +440,7 @@ impl Executor {
backend.is_initialized = false;
backend.spec_id = env.spec_id();

let result = self.strategy.lock().expect("failed acquiring strategy").call_inspect(
let result = self.strategy.try_lock().expect("failed acquiring strategy").call_inspect(
&mut backend,
&mut env,
&mut inspector,
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/src/executors/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl TracingExecutor {
) -> Self {
let db = Backend::spawn(
fork,
strategy.lock().expect("failed acquiring strategy").new_backend_strategy(),
strategy.try_lock().expect("failed acquiring strategy").new_backend_strategy(),
);
let trace_mode =
TraceMode::Call.with_debug(debug).with_decode_internal(if decode_internal {
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl MultiContractRunner {
// The DB backend that serves all the data.
let db = Backend::spawn(
self.fork.take(),
self.strategy.lock().expect("failed acquiring strategy").new_backend_strategy(),
self.strategy.try_lock().expect("failed acquiring strategy").new_backend_strategy(),
);

let find_timer = Instant::now();
Expand Down
6 changes: 5 additions & 1 deletion crates/forge/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ impl ContractRunner<'_> {
// nonce.
if let Some(cheatcodes) = &mut self.executor.inspector.cheatcodes {
debug!("test contract deployed");
cheatcodes.strategy.lock().expect("failed acquiring strategy").base_contract_deployed();
cheatcodes
.strategy
.try_lock()
.expect("failed acquiring strategy")
.base_contract_deployed();
}

// Optionally call the `setUp` function
Expand Down
7 changes: 5 additions & 2 deletions crates/script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,10 @@ impl ScriptConfig {
let fork = self.evm_opts.get_fork(&self.config, env.clone());
let backend = Backend::spawn(
fork,
strategy.lock().expect("failed acquiring strategy").new_backend_strategy(),
strategy
.try_lock()
.expect("failed acquiring strategy")
.new_backend_strategy(),
);
self.backends.insert(fork_url.clone(), backend.clone());
backend
Expand All @@ -612,7 +615,7 @@ impl ScriptConfig {
// to cache the backend for.
Backend::spawn(
None,
strategy.lock().expect("failed acquiring strategy").new_backend_strategy(),
strategy.try_lock().expect("failed acquiring strategy").new_backend_strategy(),
)
};

Expand Down
6 changes: 5 additions & 1 deletion crates/script/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ impl ScriptRunner {
// nonce.
if let Some(cheatcodes) = &mut self.executor.inspector.cheatcodes {
debug!("script deployed");
cheatcodes.strategy.lock().expect("failed acquiring strategy").base_contract_deployed();
cheatcodes
.strategy
.try_lock()
.expect("failed acquiring strategy")
.base_contract_deployed();
}

// Optionally call the `setUp` function
Expand Down
Loading