Skip to content

Commit

Permalink
test: rotate rinkeby keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed May 24, 2022
1 parent c5d49c4 commit e4cdc0a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 27 deletions.
9 changes: 2 additions & 7 deletions anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use ethers::{
};
use std::sync::Arc;

// import helper module that provides rotating rpc endpoints
#[path = "../../../cli/test-utils/src/rpc.rs"]
mod rpc;
use foundry_utils::rpc;

abigen!(Greeter, "test-data/greeter.json");

Expand Down Expand Up @@ -261,15 +259,12 @@ async fn can_deploy_greeter_on_rinkeby_fork() {
NodeConfig::test()
.with_port(next_port())
.with_eth_rpc_url(Some(rpc::next_rinkeby_http_rpc_endpoint()))
.silent()
.with_fork_block_number(Some(10074295u64)),
.silent(),
)
.await;
let provider = handle.http_provider();
let wallet = handle.dev_wallets().next().unwrap();
let from = wallet.address();
let client = Arc::new(SignerMiddleware::new(provider, wallet));
assert_eq!(client.get_transaction_count(from, None).await.unwrap(), 5845u64.into());

let greeter_contract = Greeter::deploy(Arc::clone(&client), "Hello World!".to_string())
.unwrap()
Expand Down
3 changes: 0 additions & 3 deletions cli/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ pub mod util;
pub use util::{TestCommand, TestProject};

pub use ethers_solc;

pub mod rpc;
pub use rpc::next_http_rpc_endpoint;
4 changes: 2 additions & 2 deletions cli/test-utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ macro_rules! forgetest_external {
// Skip fork tests if the RPC url is not set.
if $fork_block > 0 && std::env::var("ETH_RPC_URL").is_err() {
eprintln!("Skipping test {}. ETH_RPC_URL is not set.", $repo);
return
return;
};

let (prj, mut cmd) = $crate::util::setup_forge(stringify!($test), $style);
Expand Down Expand Up @@ -170,7 +170,7 @@ macro_rules! forgetest_external {
]);
cmd.set_env("FOUNDRY_FUZZ_RUNS", "1");

let next_eth_rpc_url = $crate::next_http_rpc_endpoint();
let next_eth_rpc_url = foundry_utils::rpc::next_http_rpc_endpoint();
if $fork_block > 0 {
cmd.set_env("FOUNDRY_ETH_RPC_URL", next_eth_rpc_url);
cmd.set_env("FOUNDRY_FORK_BLOCK_NUMBER", stringify!($fork_block));
Expand Down
3 changes: 2 additions & 1 deletion cli/tests/it/cast.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Contains various tests for checking cast commands

use foundry_cli_test_utils::{
casttest, next_http_rpc_endpoint,
casttest,
util::{TestCommand, TestProject},
};
use foundry_utils::rpc::next_http_rpc_endpoint;

// tests that the `cast find-block` command works correctly
casttest!(finds_block, |_: TestProject, mut cmd: TestCommand| {
Expand Down
3 changes: 2 additions & 1 deletion cli/tests/it/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use ethers::solc::{
};
use foundry_cli_test_utils::{
ethers_solc::PathStyle,
forgetest, forgetest_ignore, forgetest_init, next_http_rpc_endpoint,
forgetest, forgetest_ignore, forgetest_init,
util::{pretty_err, read_string, TestCommand, TestProject},
};
use foundry_config::{parse_with_profile, BasicConfig, Chain, Config, SolidityErrorCode};
use foundry_utils::rpc::next_http_rpc_endpoint;
use std::fs;
use yansi::Paint;

Expand Down
8 changes: 2 additions & 6 deletions forge/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,12 +1096,8 @@ mod tests {

#[test]
fn test_fork() {
let rpc_url = std::env::var("ETH_RPC_URL");
if rpc_url.is_err() {
eprintln!("Skipping test, ETH_RPC_URL is not set.");
return
}
let mut runner = forked_runner(&(rpc_url.unwrap()));
let rpc_url = foundry_utils::rpc::next_http_archive_rpc_endpoint();
let mut runner = forked_runner(&rpc_url);
let suite_result = runner.test(&Filter::new(".*", ".*", ".*fork"), None, true).unwrap();

for (_, SuiteResult { test_results, .. }) in suite_result {
Expand Down
2 changes: 2 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use std::{
time::Duration,
};

pub mod rpc;

static SELECTOR_DATABASE_URL: &str = "https://sig.eth.samczsun.com/api/v1/signatures";

pub enum SelectorOrSig {
Expand Down
20 changes: 13 additions & 7 deletions cli/test-utils/src/rpc.rs → utils/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ const fn num_keys() -> usize {
///
/// This will rotate all available rpc endpoints
pub fn next_http_rpc_endpoint() -> String {
next_rpc_endpoint("mainnet")
}

/// Returns the next _rinkeby_ rpc endpoint in inline
///
/// This will rotate all available rpc endpoints
pub fn next_rinkeby_http_rpc_endpoint() -> String {
next_rpc_endpoint("rinkeby")
}

pub fn next_rpc_endpoint(network: &str) -> String {
let idx = next() % num_keys();
if idx < INFURA_KEYS.len() {
format!("https://mainnet.infura.io/v3/{}", INFURA_KEYS[idx])
format!("https://{}.infura.io/v3/{}", network, INFURA_KEYS[idx])
} else {
let idx = idx - INFURA_KEYS.len();
format!("https://eth-mainnet.alchemyapi.io/v2/{}", ALCHEMY_MAINNET_KEYS[idx])
format!("https://eth-{}.alchemyapi.io/v2/{}", network, ALCHEMY_MAINNET_KEYS[idx])
}
}

Expand All @@ -55,11 +66,6 @@ pub fn next_http_archive_rpc_endpoint() -> String {
format!("https://eth-mainnet.alchemyapi.io/v2/{}", ALCHEMY_MAINNET_KEYS[idx])
}

pub fn next_rinkeby_http_rpc_endpoint() -> String {
// infura keys don't have access to archive state
"https://eth-rinkeby.alchemyapi.io/v2/9VWGraLx0tMiSWx05WH-ywgSVmMxs66W".to_string()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit e4cdc0a

Please sign in to comment.