Skip to content

Commit

Permalink
load paths back into ScriptSequence and store rpc based on index
Browse files Browse the repository at this point in the history
  • Loading branch information
devanoneth committed May 8, 2023
1 parent bce187b commit 15d208f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
33 changes: 20 additions & 13 deletions cli/src/cmd/forge/script/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use ethers::{
types::transaction::eip2718::TypedTransaction,
};
use eyre::{ContextCompat, WrapErr};
use foundry_common::{fs, shell, RpcUrl, SELECTOR_LEN};
use foundry_common::{fs, shell, SELECTOR_LEN};
use foundry_config::Config;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap, VecDeque},
collections::{HashMap, VecDeque},
io::BufWriter,
path::{Path, PathBuf},
time::{SystemTime, UNIX_EPOCH},
Expand Down Expand Up @@ -48,20 +48,24 @@ pub struct ScriptSequence {
pub commit: Option<String>,
}

#[derive(Deserialize, Serialize, Clone, Default)]
pub struct TransactionWithMetadataSensitive {
pub rpc: Option<String>,
}

/// Sensitive info from the script sequence which is saved into the cache folder
#[derive(Deserialize, Serialize, Clone, Default)]
pub struct ScriptSequenceSensitive {
pub transactions_to_rpc: BTreeMap<TxHash, Option<RpcUrl>>,
pub transactions: VecDeque<TransactionWithMetadataSensitive>,
}

impl From<&mut ScriptSequence> for ScriptSequenceSensitive {
fn from(sequence: &mut ScriptSequence) -> Self {
ScriptSequenceSensitive {
transactions_to_rpc: sequence
transactions: sequence
.transactions
.iter()
.filter(|tx| tx.hash.is_some())
.map(|tx| (tx.hash.unwrap(), tx.rpc.clone()))
.map(|tx| TransactionWithMetadataSensitive { rpc: tx.rpc.clone() })
.collect(),
}
}
Expand Down Expand Up @@ -125,19 +129,22 @@ impl ScriptSequence {
broadcasted,
)?;

let mut script_sequence: Self = ethers::solc::utils::read_json_file(path)
let mut script_sequence: Self = ethers::solc::utils::read_json_file(&path)
.wrap_err(format!("Deployment not found for chain `{chain_id}`."))?;

let script_sequence_sensitive: ScriptSequenceSensitive =
ethers::solc::utils::read_json_file(sensitive_path).wrap_err(format!(
ethers::solc::utils::read_json_file(&sensitive_path).wrap_err(format!(
"Deployment's sensitive details not found for chain `{chain_id}`."
))?;

script_sequence.transactions.iter_mut().for_each(|tx| {
if tx.hash.is_some() {
tx.rpc = script_sequence_sensitive.transactions_to_rpc[&tx.hash.unwrap()].clone()
}
});
script_sequence
.transactions
.iter_mut()
.enumerate()
.for_each(|(i, tx)| tx.rpc = script_sequence_sensitive.transactions[i].rpc.clone());

script_sequence.path = path;
script_sequence.sensitive_path = sensitive_path;

Ok(script_sequence)
}
Expand Down
15 changes: 9 additions & 6 deletions cli/tests/it/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,10 @@ forgetest_async!(check_broadcast_log, |prj: TestProject, cmd: TestCommand| async
.await;

// Uncomment to recreate the broadcast log
// std::fs::copy("broadcast/Broadcast.t.sol/31337/run-latest.json",
// PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../testdata/fixtures/broadcast.log.json"
// ));
// std::fs::copy(
// "broadcast/Broadcast.t.sol/31337/run-latest.json",
// PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../testdata/fixtures/broadcast.log.json"
// ), );

// Check broadcast logs
// Ignore timestamp, blockHash, blockNumber, cumulativeGasUsed, effectiveGasPrice,
Expand All @@ -727,9 +728,11 @@ forgetest_async!(check_broadcast_log, |prj: TestProject, cmd: TestCommand| async
assert!(fixtures_log == run_log);

// Uncomment to recreate the sensitive log
// std::fs::copy("cache/Broadcast.t.sol/31337/run-latest.json",
// PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../testdata/fixtures/broadcast.sensitive.log.
// json" ));
// std::fs::copy(
// "cache/Broadcast.t.sol/31337/run-latest.json",
// PathBuf::from(env!("CARGO_MANIFEST_DIR"))
// .join("../testdata/fixtures/broadcast.sensitive.log.json"),
// );

// Check sensitive logs
// Ignore port number since it can change inbetween runs
Expand Down
28 changes: 20 additions & 8 deletions testdata/fixtures/broadcast.sensitive.log.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{
"transactions_to_rpc": {
"0x4a492e51c01833ab295b3bffee253c03f054c061ce2795390d159a6c22258234": "http://127.0.0.1:51071",
"0x58480c8e39cb861ec12c7e8c95def65920a757be223852e8d08f32b472cfe512": "http://127.0.0.1:51071",
"0x66c8b101ee0f30a82f398e82da941a0d007c3205f02449e386fe340e56e7e9a0": "http://127.0.0.1:51071",
"0xba07558b550c650b07b665781bb3a90d4e186c2c6a9eb79bd483127f53fa8dd7": "http://127.0.0.1:51071",
"0xd5ceb345dc52a62a759650fcb276ac348fb644fed75b1bb7208f3b2b09a9119e": "http://127.0.0.1:51071",
"0xf49f62416a761e0ad98cdbd64d41afac96f46a7632d2ab145173611102d8a849": "http://127.0.0.1:51071"
}
"transactions": [
{
"rpc": "http://127.0.0.1:50842"
},
{
"rpc": "http://127.0.0.1:50842"
},
{
"rpc": "http://127.0.0.1:50842"
},
{
"rpc": "http://127.0.0.1:50842"
},
{
"rpc": "http://127.0.0.1:50842"
},
{
"rpc": "http://127.0.0.1:50842"
}
]
}

0 comments on commit 15d208f

Please sign in to comment.