Skip to content

Commit

Permalink
Merge branch 'master' into dani/test-setup-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and DaniPopes committed Nov 20, 2024
2 parents 41a601a + 7538c4e commit d6bef86
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/nextest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ jobs:
- name: Test
env:
SVM_TARGET_PLATFORM: ${{ matrix.svm_target_platform }}
HTTP_ARCHIVE_URLS: ${{ secrets.HTTP_ARCHIVE_URLS }}
WS_ARCHIVE_URLS: ${{ secrets.WS_ARCHIVE_URLS }}
run: cargo nextest run ${{ matrix.flags }}
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ async fn can_impersonate_in_fork() {

// <https://etherscan.io/block/14608400>
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_total_difficulty_fork() {
let (api, handle) = spawn(fork_config()).await;

Expand Down
18 changes: 13 additions & 5 deletions crates/config/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ impl UnresolvedEnvVarError {
pub fn try_resolve(&self) -> Result<String, Self> {
interpolate(&self.unresolved)
}

fn is_simple(&self) -> bool {
RE_PLACEHOLDER.captures_iter(&self.unresolved).count() <= 1
}
}

impl fmt::Display for UnresolvedEnvVarError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Failed to resolve env var `{}` in `{}`: {}",
self.var, self.unresolved, self.source
)
write!(f, "environment variable `{}` ", self.var)?;
f.write_str(match self.source {
VarError::NotPresent => "not found",
VarError::NotUnicode(_) => "is not valid unicode",
})?;
if !self.is_simple() {
write!(f, " in `{}`", self.unresolved)?;
}
Ok(())
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/evm/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ serde_json.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["time", "macros"] }
tracing.workspace = true
url.workspace = true

[dev-dependencies]
foundry-test-utils.workspace = true
9 changes: 8 additions & 1 deletion crates/evm/core/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use foundry_common::{provider::ProviderBuilder, ALCHEMY_FREE_TIER_CUPS};
use foundry_config::{Chain, Config};
use revm::primitives::{BlockEnv, CfgEnv, TxEnv};
use serde::{Deserialize, Deserializer, Serialize};
use url::Url;

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct EvmOpts {
Expand Down Expand Up @@ -102,7 +103,13 @@ impl EvmOpts {
)
.await
.wrap_err_with(|| {
format!("Could not instantiate forked environment with fork url: {fork_url}")
let mut err_msg = "Could not instantiate forked environment".to_string();
if let Ok(url) = Url::parse(fork_url) {
if let Some(provider) = url.host() {
err_msg.push_str(&format!(" with provider {provider}"));
}
}
err_msg
})
}

Expand Down
4 changes: 1 addition & 3 deletions crates/forge/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,7 @@ pub struct TestSetup {

impl TestSetup {
pub fn failed(reason: String) -> Self {
let mut this = Self::default();
this.reason = Some(reason);
this
Self { reason: Some(reason), ..Default::default() }
}

pub fn extend(&mut self, raw: RawCallResult, trace_kind: TraceKind) {
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/cli/ext_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use foundry_test_utils::util::ExtTester;

#[test]
fn forge_std() {
ExtTester::new("foundry-rs", "forge-std", "1d0766bc5d814f117c7b1e643828f7d85024fb51")
ExtTester::new("foundry-rs", "forge-std", "2b59872eee0b8088ddcade39fe8c041e17bb79c0")
// Skip fork tests.
.args(["--nmc", "Fork"])
.run();
Expand Down
28 changes: 27 additions & 1 deletion crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ Compiler run successful!
Ran 1 test for test/Contract.t.sol:USDTCallingTest
[PASS] test() ([GAS])
Traces:
[9406] USDTCallingTest::test()
[..] USDTCallingTest::test()
├─ [0] VM::createSelectFork("[..]")
│ └─ ← [Return] 0
├─ [3110] 0xdAC17F958D2ee523a2206206994597C13D831ec7::name() [staticcall]
Expand Down Expand Up @@ -2639,3 +2639,29 @@ contract ScrollForkTest is Test {
cmd.args(["test", "--mt", "test_roll_scroll_fork_to_tx", "--evm-version", "cancun"])
.assert_success();
});

// Test that only provider is included in failed fork error.
forgetest_init!(test_display_provider_on_error, |prj, cmd| {
prj.add_test(
"ForkTest.t.sol",
r#"
import {Test} from "forge-std/Test.sol";
contract ForkTest is Test {
function test_fork_err_message() public {
vm.createSelectFork("https://eth-mainnet.g.alchemy.com/v2/DUMMY_KEY");
}
}
"#,
)
.unwrap();

cmd.args(["test", "--mt", "test_fork_err_message"]).assert_failure().stdout_eq(str![[r#"
...
Ran 1 test for test/ForkTest.t.sol:ForkTest
[FAIL: vm.createSelectFork: Could not instantiate forked environment with provider eth-mainnet.g.alchemy.com;] test_fork_err_message() ([GAS])
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; [ELAPSED]
...
"#]]);
});
38 changes: 31 additions & 7 deletions crates/test-utils/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
use foundry_config::{NamedChain, NamedChain::Optimism};
use rand::seq::SliceRandom;
use std::sync::{
atomic::{AtomicUsize, Ordering},
LazyLock,
use std::{
env,
sync::{
atomic::{AtomicUsize, Ordering},
LazyLock,
},
};

/// Env var key for ws archive endpoints.
const ENV_WS_ARCHIVE_ENDPOINTS: &str = "WS_ARCHIVE_URLS";
/// Env var key for http archive endpoints.
const ENV_HTTP_ARCHIVE_ENDPOINTS: &str = "HTTP_ARCHIVE_URLS";

// List of general purpose infura keys to rotate through
static INFURA_KEYS: LazyLock<Vec<&'static str>> = LazyLock::new(|| {
let mut keys = vec![
Expand Down Expand Up @@ -118,14 +126,30 @@ pub fn next_ws_endpoint(chain: NamedChain) -> String {

/// Returns endpoint that has access to archive state
pub fn next_http_archive_rpc_endpoint() -> String {
let idx = next() % ALCHEMY_KEYS.len();
format!("https://eth-mainnet.g.alchemy.com/v2/{}", ALCHEMY_KEYS[idx])
next_archive_endpoint(false)
}

/// Returns endpoint that has access to archive state
pub fn next_ws_archive_rpc_endpoint() -> String {
let idx = next() % ALCHEMY_KEYS.len();
format!("wss://eth-mainnet.g.alchemy.com/v2/{}", ALCHEMY_KEYS[idx])
next_archive_endpoint(true)
}

/// Returns endpoint that has access to archive state, http or ws.
/// Use env vars (comma separated urls) or default inline keys (Alchemy for ws, Infura for http).
fn next_archive_endpoint(is_ws: bool) -> String {
let env_urls = if is_ws { ENV_WS_ARCHIVE_ENDPOINTS } else { ENV_HTTP_ARCHIVE_ENDPOINTS };

let rpc_env_vars = env::var(env_urls).unwrap_or_default();
if !rpc_env_vars.is_empty() {
let urls = rpc_env_vars.split(',').collect::<Vec<&str>>();
urls.choose(&mut rand::thread_rng()).unwrap().to_string()
} else if is_ws {
let idx = next() % ALCHEMY_KEYS.len();
format!("wss://eth-mainnet.g.alchemy.com/v2/{}", ALCHEMY_KEYS[idx])
} else {
let idx = next() % INFURA_KEYS.len();
format!("https://mainnet.infura.io/v3/{}", INFURA_KEYS[idx])
}
}

/// Returns the next etherscan api key
Expand Down
4 changes: 1 addition & 3 deletions testdata/default/cheats/RpcUrls.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ contract RpcUrlTest is DSTest {
// can set env and return correct url
function testCanSetAndGetURLAndAllUrls() public {
// this will fail because alias is not set
vm._expectCheatcodeRevert(
"Failed to resolve env var `RPC_ENV_ALIAS` in `${RPC_ENV_ALIAS}`: environment variable not found"
);
vm._expectCheatcodeRevert("environment variable `RPC_ENV_ALIAS` not found");
string[2][] memory _urls = vm.rpcUrls();

string memory url = vm.rpcUrl("mainnet");
Expand Down

0 comments on commit d6bef86

Please sign in to comment.