Skip to content

Commit

Permalink
chore: upgrade to build with rust 1.80 (#631)
Browse files Browse the repository at this point in the history
- bump time crate, seeing build failure in
https://github.com/Homebrew/homebrew-core/actions/runs/10126855440/job/28023138251
relates to time-rs/time#693
- also update toolchain and run the cargo fmt/clippy fixes

---------

Signed-off-by: Rui Chen <rui@chenrui.dev>
  • Loading branch information
chenrui333 committed Sep 17, 2024
1 parent 2d08f72 commit b24ddc6
Show file tree
Hide file tree
Showing 46 changed files with 1,311 additions and 1,375 deletions.
1,033 changes: 518 additions & 515 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions components/chainhook-cli/src/archive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn default_tsv_sha_file_path(network: &StacksNetwork) -> String {
pub async fn download_tsv_file(config: &Config) -> Result<(), String> {
let mut destination_path = config.expected_cache_path();
std::fs::create_dir_all(&destination_path).unwrap_or_else(|e| {
println!("{}", e.to_string());
println!("{}", e);
});

let remote_sha_url = config.expected_remote_stacks_tsv_sha256()?;
Expand All @@ -32,7 +32,7 @@ pub async fn download_tsv_file(config: &Config) -> Result<(), String> {
let mut local_sha_file_path = destination_path.clone();
local_sha_file_path.push(default_tsv_sha_file_path(&config.network.stacks_network));

write_file_content_at_path(&local_sha_file_path, &res.to_vec())?;
write_file_content_at_path(&local_sha_file_path, &res)?;

let file_url = config.expected_remote_stacks_tsv_url()?;
let res = reqwest::get(&file_url)
Expand All @@ -57,14 +57,14 @@ pub async fn download_tsv_file(config: &Config) -> Result<(), String> {
if let Err(e) = file.write_all(&buffer[..n]) {
return Err(format!(
"unable to update compressed archive: {}",
e.to_string()
e
));
}
}
Err(e) => {
return Err(format!(
"unable to write compressed archive: {}",
e.to_string()
e
));
}
}
Expand All @@ -76,14 +76,14 @@ pub async fn download_tsv_file(config: &Config) -> Result<(), String> {
while let Some(item) = stream.next().await {
let chunk = match item {
Ok(i) => Ok(i),
Err(e) => Err(format!("Error while downloading file {}", e.to_string())),
Err(e) => Err(format!("Error while downloading file {}", e)),
}?;
if chunk.is_empty() {
continue;
}
tx.send_async(chunk.to_vec())
.await
.map_err(|e| format!("unable to download stacks archive: {}", e.to_string()))?;
.map_err(|e| format!("unable to download stacks archive: {}", e))?;
}
drop(tx);
tokio::task::spawn_blocking(|| decoder_thread.join())
Expand Down Expand Up @@ -153,7 +153,7 @@ pub async fn download_stacks_dataset_if_required(
let local_version_is_latest = remote_response
.to_ascii_lowercase()
.starts_with(&local[0..32]);
local_version_is_latest == false
!local_version_is_latest
}
(_, _) => {
info!(
Expand All @@ -173,7 +173,7 @@ pub async fn download_stacks_dataset_if_required(
}

info!(ctx.expect_logger(), "Downloading {}", url);
match download_tsv_file(&config).await {
match download_tsv_file(config).await {
Ok(_) => {}
Err(e) => return Err(e),
}
Expand Down
4 changes: 2 additions & 2 deletions components/chainhook-cli/src/archive/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const SHA256_HASH: &str = "49ca5f80b2a1303e7f7e98a4f9d39efeb35fd9f3696c4cd9615e0

#[get("/stacks_blocks.tsv.sha256")]
fn get_sha256() -> String {
format!("{SHA256_HASH}")
SHA256_HASH.to_string()
}

#[get("/stacks_blocks.tsv.gz")]
Expand Down Expand Up @@ -60,7 +60,7 @@ async fn it_downloads_stacks_dataset_if_required() {
let _ = hiro_system_kit::thread_named("Start tsv service")
.spawn(move || {
let future = start_service(port);
let _ = hiro_system_kit::nestable_block_on(future);
hiro_system_kit::nestable_block_on(future);
})
.expect("unable to spawn thread");

Expand Down
15 changes: 6 additions & 9 deletions components/chainhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,9 @@ pub fn main() {
}
};

match hiro_system_kit::nestable_block_on(handle_command(opts, ctx.clone())) {
Err(e) => {
crit!(ctx.expect_logger(), "{e}");
process::exit(1);
}
Ok(_) => {}
if let Err(e) = hiro_system_kit::nestable_block_on(handle_command(opts, ctx.clone())) {
crit!(ctx.expect_logger(), "{e}");
process::exit(1);
}
}

Expand Down Expand Up @@ -481,15 +478,15 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
// need to create
if let Some(dirp) = PathBuf::from(&path).parent() {
std::fs::create_dir_all(dirp).unwrap_or_else(|e| {
println!("{}", e.to_string());
println!("{}", e);
});
}
let mut f = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&path)
.map_err(|e| format!("{}", e.to_string()))?;
.map_err(|e| format!("{}", e))?;
use std::io::Write;
let _ = f.write_all(content.as_bytes());
} else {
Expand Down Expand Up @@ -882,7 +879,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
pub fn load_predicate_from_path(
predicate_path: &str,
) -> Result<ChainhookSpecificationNetworkMap, String> {
let file = std::fs::File::open(&predicate_path)
let file = std::fs::File::open(predicate_path)
.map_err(|e| format!("unable to read file {}\n{:?}", predicate_path, e))?;
let mut file_reader = BufReader::new(file);
let mut file_buffer = vec![];
Expand Down
2 changes: 1 addition & 1 deletion components/chainhook-cli/src/config/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ tsv_file_url = "https://archive.hiro.so/{network}/stacks-blockchain-api/{network
mode = mode.as_str(),
network = network.to_lowercase(),
);
return conf;
conf
}
22 changes: 9 additions & 13 deletions components/chainhook-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Config {
let config_file: ConfigFile = match toml::from_slice(&file_buffer) {
Ok(s) => s,
Err(e) => {
return Err(format!("Config file malformatted {}", e.to_string()));
return Err(format!("Config file malformatted {}", e));
}
};
Config::from_config_file(config_file)
Expand Down Expand Up @@ -137,7 +137,7 @@ impl Config {
};

let mut event_sources = vec![];
for source in config_file.event_source.unwrap_or(vec![]).iter_mut() {
for source in config_file.event_source.unwrap_or_default().iter_mut() {
if let Some(dst) = source.tsv_file_path.take() {
let mut file_path = PathBuf::new();
file_path.push(dst);
Expand Down Expand Up @@ -168,20 +168,16 @@ impl Config {
Some(pox_config) => PoxConfig {
first_burnchain_block_height: pox_config
.first_burnchain_block_height
.unwrap_or(default_pox_config.first_burnchain_block_height)
.into(),
.unwrap_or(default_pox_config.first_burnchain_block_height),
prepare_phase_len: pox_config
.prepare_phase_len
.unwrap_or(default_pox_config.prepare_phase_len)
.into(),
.unwrap_or(default_pox_config.prepare_phase_len),
reward_phase_len: pox_config
.reward_phase_len
.unwrap_or(default_pox_config.reward_phase_len)
.into(),
.unwrap_or(default_pox_config.reward_phase_len),
rewarded_addresses_per_block: pox_config
.rewarded_addresses_per_block
.unwrap_or(default_pox_config.rewarded_addresses_per_block)
.into(),
.unwrap_or(default_pox_config.rewarded_addresses_per_block),
},
},
http_api: match config_file.http_api {
Expand Down Expand Up @@ -260,7 +256,7 @@ impl Config {
_ => {}
}
}
return false;
false
}

pub fn add_local_stacks_tsv_source(&mut self, file_path: &PathBuf) {
Expand Down Expand Up @@ -335,7 +331,7 @@ impl Config {
remote_tsv_present_locally = true;
}
}
rely_on_remote_tsv == true && remote_tsv_present_locally == false
rely_on_remote_tsv && !remote_tsv_present_locally
}

pub fn default(
Expand All @@ -348,7 +344,7 @@ impl Config {
(true, false, false, _) => Config::devnet_default(),
(false, true, false, _) => Config::testnet_default(),
(false, false, true, _) => Config::mainnet_default(),
(false, false, false, Some(config_path)) => Config::from_file_path(&config_path)?,
(false, false, false, Some(config_path)) => Config::from_file_path(config_path)?,
_ => Err("Invalid combination of arguments".to_string())?,
};
Ok(config)
Expand Down
14 changes: 7 additions & 7 deletions components/chainhook-cli/src/config/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn config_from_file_allows_setting_disabled_fields() {
// http_api and monitoring are optional, so they are disabled in generated config file
generated_config_file.http_api = Some(PredicatesApiConfigFile {
http_port: Some(0),
database_uri: Some(format!("")),
database_uri: Some(String::new()),
display_logs: Some(false),
disabled: Some(false),
});
Expand Down Expand Up @@ -78,7 +78,7 @@ fn is_http_api_enabled_handles_both_modes() {
assert!(!config.is_http_api_enabled());
config.http_api = PredicatesApi::On(PredicatesApiConfig {
http_port: 0,
database_uri: format!(""),
database_uri: String::new(),
display_logs: false,
});
assert!(config.is_http_api_enabled());
Expand All @@ -95,22 +95,22 @@ fn should_download_remote_stacks_tsv_handles_both_modes() {
let mut config = Config::default(true, false, false, &None).unwrap();

config.event_sources = vec![url_src.clone(), path_src.clone()];
assert_eq!(config.should_download_remote_stacks_tsv(), false);
assert!(!config.should_download_remote_stacks_tsv());

config.event_sources = vec![path_src.clone()];
assert_eq!(config.should_download_remote_stacks_tsv(), false);
assert!(!config.should_download_remote_stacks_tsv());

config.event_sources = vec![];
assert_eq!(config.should_download_remote_stacks_tsv(), false);
assert!(!config.should_download_remote_stacks_tsv());

config.event_sources = vec![url_src.clone()];
assert_eq!(config.should_download_remote_stacks_tsv(), true);
assert!(config.should_download_remote_stacks_tsv());
}

#[test]
fn expected_remote_stacks_tsv_base_url_panics_if_missing() {
let url_src = EventSourceConfig::StacksTsvUrl(super::UrlConfig {
file_url: format!("test"),
file_url: "test".to_string(),
});
let mut config = Config::default(true, false, false, &None).unwrap();

Expand Down
29 changes: 13 additions & 16 deletions components/chainhook-cli/src/scan/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
let bitcoin_rpc = match Client::new(&config.network.bitcoind_rpc_url, auth) {
Ok(con) => con,
Err(message) => {
return Err(format!("Bitcoin RPC error: {}", message.to_string()));
return Err(format!("Bitcoin RPC error: {}", message));
}
};

Expand All @@ -51,7 +51,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
Err(e) => {
return Err(format!(
"unable to retrieve Bitcoin chain tip ({})",
e.to_string()
e
));
}
};
Expand Down Expand Up @@ -105,14 +105,11 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
let mut loop_did_trigger = false;
while let Some(current_block_height) = block_heights_to_scan.pop_front() {
if let Some(kill_signal) = kill_signal.clone() {
match kill_signal.read() {
Ok(kill_signal) => {
// if true, we're received the kill signal, so break out of the loop
if *kill_signal {
return Ok(PredicateScanResult::Deregistered);
}
if let Ok(kill_signal) = kill_signal.read() {
// if true, we're received the kill signal, so break out of the loop
if *kill_signal {
return Ok(PredicateScanResult::Deregistered);
}
Err(_) => {}
}
}
if let Some(ref mut predicates_db_conn) = predicates_db_conn {
Expand Down Expand Up @@ -143,7 +140,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
Err(e) => {
return Err(format!(
"unable to retrieve Bitcoin chain tip ({})",
e.to_string()
e
));
}
};
Expand Down Expand Up @@ -216,7 +213,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
res.unwrap_err()
));
} else {
return Err(format!("Scan aborted (consecutive action errors >= 3)"));
return Err("Scan aborted (consecutive action errors >= 3)".to_string());
}
}
}
Expand Down Expand Up @@ -260,7 +257,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
return Ok(PredicateScanResult::Expired);
}

return Ok(PredicateScanResult::ChainTipReached);
Ok(PredicateScanResult::ChainTipReached)
}

pub async fn process_block_with_predicates(
Expand All @@ -278,7 +275,7 @@ pub async fn process_block_with_predicates(
let (predicates_triggered, _predicates_evaluated, _predicates_expired) =
evaluate_bitcoin_chainhooks_on_chain_event(&chain_event, predicates, ctx);

execute_predicates_action(predicates_triggered, &event_observer_config, &ctx).await
execute_predicates_action(predicates_triggered, event_observer_config, ctx).await
}

pub async fn execute_predicates_action<'a>(
Expand All @@ -290,7 +287,7 @@ pub async fn execute_predicates_action<'a>(
let mut proofs = HashMap::new();
for trigger in hits.into_iter() {
if trigger.chainhook.include_proof {
gather_proofs(&trigger, &mut proofs, &config, &ctx);
gather_proofs(&trigger, &mut proofs, config, ctx);
}
let predicate_uuid = &trigger.chainhook.uuid;
match handle_bitcoin_hook_action(trigger, &proofs) {
Expand All @@ -304,10 +301,10 @@ pub async fn execute_predicates_action<'a>(
actions_triggered += 1;
match action {
BitcoinChainhookOccurrence::Http(request, _) => {
send_request(request, 10, 3, &ctx).await?
send_request(request, 10, 3, ctx).await?
}
BitcoinChainhookOccurrence::File(path, bytes) => {
file_append(path, bytes, &ctx)?
file_append(path, bytes, ctx)?
}
BitcoinChainhookOccurrence::Data(_payload) => {}
};
Expand Down
Loading

0 comments on commit b24ddc6

Please sign in to comment.