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

fix: use PathBuf instead of String to compatible different platforms and non-Unicode sequences in filepath #65

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 9 additions & 4 deletions host/src/bin/op-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::{Path, PathBuf};

use alloy_sol_types::{sol, SolInterface};
use anyhow::Result;
use clap::Parser;
Expand Down Expand Up @@ -43,18 +45,21 @@ struct Args {
#[clap(short, long, require_equals = true, num_args = 0..=1, default_missing_value = "host/testdata")]
/// Use a local directory as a cache for RPC calls. Accepts a custom directory.
/// [default: host/testdata]
cache: Option<String>,
cache: Option<PathBuf>,

#[clap(long, require_equals = true)]
/// L2 block number to begin from
block_no: u64,
}

fn cache_file_path(cache_path: &String, network: &str, block_no: u64, ext: &str) -> String {
format!("{}/{}/{}.{}", cache_path, network, block_no, ext)
fn cache_file_path(cache_path: &Path, network: &str, block_no: u64, ext: &str) -> PathBuf {
cache_path
.join(network)
.join(block_no.to_string())
.with_extension(ext)
}

fn op_cache_path(args: &Args, block_no: u64) -> Option<String> {
fn op_cache_path(args: &Args, block_no: u64) -> Option<PathBuf> {
args.cache
.as_ref()
.map(|dir| cache_file_path(dir, "optimism", block_no, "json.gz"))
Expand Down
15 changes: 11 additions & 4 deletions host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

extern crate core;

use std::{fmt::Debug, time::Instant};
use std::{
fmt::Debug,
path::{Path, PathBuf},
time::Instant,
};

use anyhow::{Context, Result};
use bonsai_sdk::alpha as bonsai_sdk;
Expand Down Expand Up @@ -47,7 +51,7 @@ struct Args {
#[clap(short, long, require_equals = true, num_args = 0..=1, default_missing_value = "host/testdata")]
/// Use a local directory as a cache for RPC calls. Accepts a custom directory.
/// [default: host/testdata]
cache: Option<String>,
cache: Option<PathBuf>,

#[clap(
short,
Expand Down Expand Up @@ -81,8 +85,11 @@ struct Args {
profile: bool,
}

fn cache_file_path(cache_path: &String, network: &String, block_no: u64, ext: &str) -> String {
format!("{}/{}/{}.{}", cache_path, network, block_no, ext)
fn cache_file_path(cache_path: &Path, network: &str, block_no: u64, ext: &str) -> PathBuf {
cache_path
.join(network)
.join(block_no.to_string())
.with_extension(ext)
}

#[tokio::main]
Expand Down
5 changes: 4 additions & 1 deletion host/tests/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ fn block_cli_ethereum(#[files("testdata/ethereum/*.json.gz")] path: PathBuf) {
fn empty_blocks(#[files("testdata/ethereum/*.json.gz")] path: PathBuf) {
let block_no = u64::from_str(file_prefix(&path)).unwrap();
// Set block cache directory
let rpc_cache = Some(format!("testdata/ethereum/{}.json.gz", block_no));
let rpc_cache = Some(PathBuf::from(format!(
"testdata/ethereum/{}.json.gz",
block_no
)));
// Fetch all of the preflight data
let init =
EthereumStrategy::run_preflight(ETH_MAINNET_CHAIN_SPEC.clone(), rpc_cache, None, block_no)
Expand Down
6 changes: 3 additions & 3 deletions lib/src/host/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::Debug;
use std::{fmt::Debug, path::PathBuf};

use anyhow::{anyhow, Context, Result};
use ethers_core::types::{
Expand Down Expand Up @@ -60,7 +60,7 @@ pub trait Preflight<E: TxEssence> {
/// It returns all the data required to build and validate the block.
fn run_preflight(
chain_spec: ChainSpec,
cache_path: Option<String>,
cache_path: Option<PathBuf>,
rpc_url: Option<String>,
block_no: u64,
) -> Result<Data<E>>;
Expand All @@ -74,7 +74,7 @@ where
{
fn run_preflight(
chain_spec: ChainSpec,
cache_path: Option<String>,
cache_path: Option<PathBuf>,
rpc_url: Option<String>,
block_no: u64,
) -> Result<Data<N::TxEssence>> {
Expand Down
4 changes: 3 additions & 1 deletion lib/src/host/provider/cached_rpc_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::PathBuf;

use anyhow::Result;
use ethers_core::types::{Block, Bytes, EIP1186ProofResponse, Transaction, H256, U256};

Expand All @@ -26,7 +28,7 @@ pub struct CachedRpcProvider {
}

impl CachedRpcProvider {
pub fn new(cache_path: String, rpc_url: String) -> Result<Self> {
pub fn new(cache_path: PathBuf, rpc_url: String) -> Result<Self> {
let cache = match FileProvider::read_from_file(cache_path.clone()) {
Ok(provider) => provider,
Err(_) => FileProvider::empty(cache_path),
Expand Down
9 changes: 5 additions & 4 deletions lib/src/host/provider/file_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
collections::HashMap,
fs::File,
io::{Read, Write},
path::{Path, PathBuf},
};

use anyhow::{anyhow, Result};
Expand All @@ -29,7 +30,7 @@ use super::{AccountQuery, BlockQuery, MutProvider, ProofQuery, Provider, Storage
#[derive(Deserialize, Serialize)]
pub struct FileProvider {
#[serde(skip)]
file_path: String,
file_path: PathBuf,
#[serde(skip)]
dirty: bool,
#[serde_as(as = "Vec<(_, _)>")]
Expand All @@ -49,7 +50,7 @@ pub struct FileProvider {
}

impl FileProvider {
pub fn empty(file_path: String) -> Self {
pub fn empty(file_path: PathBuf) -> Self {
FileProvider {
file_path,
dirty: false,
Expand All @@ -63,7 +64,7 @@ impl FileProvider {
}
}

pub fn read_from_file(file_path: String) -> Result<Self> {
pub fn read_from_file(file_path: PathBuf) -> Result<Self> {
let mut buf = vec![];
let mut decoder = flate2::read::GzDecoder::new(File::open(&file_path)?);
decoder.read_to_end(&mut buf)?;
Expand All @@ -75,7 +76,7 @@ impl FileProvider {
Ok(out)
}

pub fn save_to_file(&self, file_path: &String) -> Result<()> {
pub fn save_to_file(&self, file_path: &Path) -> Result<()> {
if self.dirty {
let mut encoder = flate2::write::GzEncoder::new(
File::create(file_path)?,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/host/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeSet;
use std::{collections::BTreeSet, path::PathBuf};

use anyhow::{anyhow, Result};
use ethers_core::types::{Block, Bytes, EIP1186ProofResponse, Transaction, H160, H256, U256};
Expand Down Expand Up @@ -69,7 +69,7 @@ pub trait MutProvider: Provider {
fn insert_storage(&mut self, query: StorageQuery, val: H256);
}

pub fn new_file_provider(file_path: String) -> Result<Box<dyn Provider>> {
pub fn new_file_provider(file_path: PathBuf) -> Result<Box<dyn Provider>> {
let provider = file_provider::FileProvider::read_from_file(file_path)?;

Ok(Box::new(provider))
Expand All @@ -81,14 +81,14 @@ pub fn new_rpc_provider(rpc_url: String) -> Result<Box<dyn Provider>> {
Ok(Box::new(provider))
}

pub fn new_cached_rpc_provider(cache_path: String, rpc_url: String) -> Result<Box<dyn Provider>> {
pub fn new_cached_rpc_provider(cache_path: PathBuf, rpc_url: String) -> Result<Box<dyn Provider>> {
let provider = cached_rpc_provider::CachedRpcProvider::new(cache_path, rpc_url)?;

Ok(Box::new(provider))
}

pub fn new_provider(
cache_path: Option<String>,
cache_path: Option<PathBuf>,
rpc_url: Option<String>,
) -> Result<Box<dyn Provider>> {
match (cache_path, rpc_url) {
Expand Down