Skip to content

Commit 51c0485

Browse files
authored
Merge pull request #538 from hirosystems/develop
chore(release): publish v1.4.0
2 parents ccf7823 + 0a5a0a7 commit 51c0485

File tree

37 files changed

+2846
-1279
lines changed

37 files changed

+2846
-1279
lines changed

Cargo.lock

Lines changed: 1330 additions & 382 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/chainhook-cli/Cargo.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "chainhook"
3-
version = "1.3.1"
3+
version = "1.4.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -18,8 +18,7 @@ rand = "0.8.5"
1818
chainhook-sdk = { version = "0.12.1", default-features = false, features = [
1919
"zeromq",
2020
], path = "../chainhook-sdk" }
21-
hiro-system-kit = "0.3.1"
22-
# clarinet-files = { path = "../../../clarinet/components/clarinet-files" }
21+
hiro-system-kit = "0.3.1"
2322
# hiro-system-kit = { path = "../../../clarinet/components/hiro-system-kit" }
2423
clap = { version = "3.2.23", features = ["derive"], optional = true }
2524
clap_generate = { version = "3.0.3", optional = true }
@@ -31,7 +30,7 @@ reqwest = { version = "0.11", default-features = false, features = [
3130
"json",
3231
"rustls-tls",
3332
] }
34-
tokio = { version = "=1.24", features = ["full"] }
33+
tokio = { version = "1.35.1", features = ["full"] }
3534
futures-util = "0.3.24"
3635
flate2 = "1.0.24"
3736
tar = "0.4.38"
@@ -52,7 +51,6 @@ features = ["lz4", "snappy"]
5251
[dev-dependencies]
5352
criterion = "0.3"
5453
redis = "0.21.5"
55-
clarity-vm = "=2.1.1"
5654
hex = "0.4.3"
5755
test-case = "3.1.0"
5856
serial_test = "2.0.0"

components/chainhook-cli/src/archive/mod.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub async fn download_tsv_file(config: &Config) -> Result<(), String> {
2121
println!("{}", e.to_string());
2222
});
2323

24-
let remote_sha_url = config.expected_remote_stacks_tsv_sha256();
24+
let remote_sha_url = config.expected_remote_stacks_tsv_sha256()?;
2525
let res = reqwest::get(&remote_sha_url)
2626
.await
2727
.or(Err(format!("Failed to GET from '{}'", &remote_sha_url)))?
@@ -34,7 +34,7 @@ pub async fn download_tsv_file(config: &Config) -> Result<(), String> {
3434

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

37-
let file_url = config.expected_remote_stacks_tsv_url();
37+
let file_url = config.expected_remote_stacks_tsv_url()?;
3838
let res = reqwest::get(&file_url)
3939
.await
4040
.or(Err(format!("Failed to GET from '{}'", &file_url)))?;
@@ -55,14 +55,17 @@ pub async fn download_tsv_file(config: &Config) -> Result<(), String> {
5555
Ok(0) => break,
5656
Ok(n) => {
5757
if let Err(e) = file.write_all(&buffer[..n]) {
58-
let err =
59-
format!("unable to update compressed archive: {}", e.to_string());
60-
return Err(err);
58+
return Err(format!(
59+
"unable to update compressed archive: {}",
60+
e.to_string()
61+
));
6162
}
6263
}
6364
Err(e) => {
64-
let err = format!("unable to write compressed archive: {}", e.to_string());
65-
return Err(err);
65+
return Err(format!(
66+
"unable to write compressed archive: {}",
67+
e.to_string()
68+
));
6669
}
6770
}
6871
}
@@ -83,12 +86,11 @@ pub async fn download_tsv_file(config: &Config) -> Result<(), String> {
8386
.map_err(|e| format!("unable to download stacks archive: {}", e.to_string()))?;
8487
}
8588
drop(tx);
86-
8789
tokio::task::spawn_blocking(|| decoder_thread.join())
8890
.await
89-
.unwrap()
90-
.unwrap()
91-
.unwrap();
91+
.map_err(|e| format!("failed to spawn thread: {e}"))?
92+
.map_err(|e| format!("decoder thread failed when downloading tsv: {:?}", e))?
93+
.map_err(|e| format!("failed to download tsv: {}", e))?;
9294
}
9395

9496
Ok(())
@@ -124,11 +126,14 @@ impl Read for ChannelRead {
124126
}
125127
}
126128

127-
pub async fn download_stacks_dataset_if_required(config: &mut Config, ctx: &Context) -> bool {
129+
pub async fn download_stacks_dataset_if_required(
130+
config: &mut Config,
131+
ctx: &Context,
132+
) -> Result<bool, String> {
128133
if config.is_initial_ingestion_required() {
129134
// Download default tsv.
130135
if config.rely_on_remote_stacks_tsv() && config.should_download_remote_stacks_tsv() {
131-
let url = config.expected_remote_stacks_tsv_url();
136+
let url = config.expected_remote_stacks_tsv_url()?;
132137
let mut tsv_file_path = config.expected_cache_path();
133138
tsv_file_path.push(default_tsv_file_path(&config.network.stacks_network));
134139
let mut tsv_sha_file_path = config.expected_cache_path();
@@ -137,7 +142,7 @@ pub async fn download_stacks_dataset_if_required(config: &mut Config, ctx: &Cont
137142
// Download archive if not already present in cache
138143
// Load the local
139144
let local_sha_file = read_file_content_at_path(&tsv_sha_file_path);
140-
let sha_url = config.expected_remote_stacks_tsv_sha256();
145+
let sha_url = config.expected_remote_stacks_tsv_sha256()?;
141146

142147
let remote_sha_file = match reqwest::get(&sha_url).await {
143148
Ok(response) => response.bytes().await,
@@ -164,28 +169,25 @@ pub async fn download_stacks_dataset_if_required(config: &mut Config, ctx: &Cont
164169
"Stacks archive file already up to date"
165170
);
166171
config.add_local_stacks_tsv_source(&tsv_file_path);
167-
return false;
172+
return Ok(false);
168173
}
169174

170175
info!(ctx.expect_logger(), "Downloading {}", url);
171176
match download_tsv_file(&config).await {
172177
Ok(_) => {}
173-
Err(e) => {
174-
error!(ctx.expect_logger(), "{}", e);
175-
std::process::exit(1);
176-
}
178+
Err(e) => return Err(e),
177179
}
178180
info!(ctx.expect_logger(), "Successfully downloaded tsv file");
179181
config.add_local_stacks_tsv_source(&tsv_file_path);
180182
}
181-
true
183+
Ok(true)
182184
} else {
183185
info!(
184186
ctx.expect_logger(),
185187
"Streaming blocks from stacks-node {}",
186188
config.network.get_stacks_node_config().rpc_url
187189
);
188-
false
190+
Ok(false)
189191
}
190192
}
191193

components/chainhook-cli/src/archive/tests/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ async fn it_downloads_stacks_dataset_if_required() {
7272
tracer: false,
7373
};
7474
let mut config_clone = config.clone();
75-
assert!(download_stacks_dataset_if_required(&mut config, &ctx).await);
76-
assert!(!download_stacks_dataset_if_required(&mut config_clone, &ctx).await);
75+
assert!(download_stacks_dataset_if_required(&mut config, &ctx)
76+
.await
77+
.unwrap());
78+
assert!(
79+
!download_stacks_dataset_if_required(&mut config_clone, &ctx)
80+
.await
81+
.unwrap()
82+
);
7783

7884
let mut tsv_file_path = config.expected_cache_path();
7985
tsv_file_path.push(default_tsv_file_path(&config.network.stacks_network));

components/chainhook-cli/src/cli/mod.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::config::Config;
33
use crate::scan::bitcoin::scan_bitcoin_chainstate_via_rpc_using_predicate;
44
use crate::scan::stacks::{
55
consolidate_local_stacks_chainstate_using_csv, scan_stacks_chainstate_via_csv_using_predicate,
6+
scan_stacks_chainstate_via_rocksdb_using_predicate,
67
};
78
use crate::service::http_api::document_predicate_api_server;
89
use crate::service::Service;
@@ -276,14 +277,14 @@ pub fn main() {
276277
let opts: Opts = match Opts::try_parse() {
277278
Ok(opts) => opts,
278279
Err(e) => {
279-
error!(ctx.expect_logger(), "{e}");
280+
crit!(ctx.expect_logger(), "{e}");
280281
process::exit(1);
281282
}
282283
};
283284

284285
match hiro_system_kit::nestable_block_on(handle_command(opts, ctx.clone())) {
285286
Err(e) => {
286-
error!(ctx.expect_logger(), "{e}");
287+
crit!(ctx.expect_logger(), "{e}");
287288
process::exit(1);
288289
}
289290
Ok(_) => {}
@@ -310,7 +311,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
310311
info!(ctx.expect_logger(), "Starting service...",);
311312

312313
let mut service = Service::new(config, ctx);
313-
return service.run(predicates).await;
314+
return service.run(predicates, None).await;
314315
}
315316
},
316317
Command::Config(subcmd) => match subcmd {
@@ -485,14 +486,35 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
485486
));
486487
}
487488
};
488-
// TODO: if a stacks.rocksdb is present, use it.
489-
// TODO: update Stacks archive file if required.
490-
scan_stacks_chainstate_via_csv_using_predicate(
491-
&predicate_spec,
492-
&mut config,
493-
&ctx,
494-
)
495-
.await?;
489+
match open_readonly_stacks_db_conn(&config.expected_cache_path(), &ctx) {
490+
Ok(db_conn) => {
491+
let _ = consolidate_local_stacks_chainstate_using_csv(
492+
&mut config,
493+
&ctx,
494+
)
495+
.await;
496+
scan_stacks_chainstate_via_rocksdb_using_predicate(
497+
&predicate_spec,
498+
None,
499+
&db_conn,
500+
&config,
501+
&ctx,
502+
)
503+
.await?;
504+
}
505+
Err(e) => {
506+
info!(
507+
ctx.expect_logger(),
508+
"Could not open db. This will greatly increase scan times. Error: {}", e
509+
);
510+
scan_stacks_chainstate_via_csv_using_predicate(
511+
&predicate_spec,
512+
&mut config,
513+
&ctx,
514+
)
515+
.await?;
516+
}
517+
};
496518
}
497519
}
498520
}

components/chainhook-cli/src/config/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,13 @@ impl Config {
256256
}
257257
}
258258

259-
pub fn expected_local_stacks_tsv_file(&self) -> &PathBuf {
259+
pub fn expected_local_stacks_tsv_file(&self) -> Result<&PathBuf, String> {
260260
for source in self.event_sources.iter() {
261261
if let EventSourceConfig::StacksTsvPath(config) = source {
262-
return &config.file_path;
262+
return Ok(&config.file_path);
263263
}
264264
}
265-
panic!("expected local-tsv source")
265+
Err("could not find expected local tsv source")?
266266
}
267267

268268
pub fn expected_cache_path(&self) -> PathBuf {
@@ -271,21 +271,23 @@ impl Config {
271271
destination_path
272272
}
273273

274-
fn expected_remote_stacks_tsv_base_url(&self) -> &String {
274+
fn expected_remote_stacks_tsv_base_url(&self) -> Result<&String, String> {
275275
for source in self.event_sources.iter() {
276276
if let EventSourceConfig::StacksTsvUrl(config) = source {
277-
return &config.file_url;
277+
return Ok(&config.file_url);
278278
}
279279
}
280-
panic!("expected remote-tsv source")
280+
Err("could not find expected remote tsv source")?
281281
}
282282

283-
pub fn expected_remote_stacks_tsv_sha256(&self) -> String {
284-
format!("{}.sha256", self.expected_remote_stacks_tsv_base_url())
283+
pub fn expected_remote_stacks_tsv_sha256(&self) -> Result<String, String> {
284+
self.expected_remote_stacks_tsv_base_url()
285+
.map(|url| format!("{}.sha256", url))
285286
}
286287

287-
pub fn expected_remote_stacks_tsv_url(&self) -> String {
288-
format!("{}.gz", self.expected_remote_stacks_tsv_base_url())
288+
pub fn expected_remote_stacks_tsv_url(&self) -> Result<String, String> {
289+
self.expected_remote_stacks_tsv_base_url()
290+
.map(|url| format!("{}.gz", url))
289291
}
290292

291293
pub fn rely_on_remote_stacks_tsv(&self) -> bool {

components/chainhook-cli/src/config/tests/mod.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,48 @@ fn should_download_remote_stacks_tsv_handles_both_modes() {
108108
}
109109

110110
#[test]
111-
#[should_panic(expected = "expected remote-tsv source")]
112111
fn expected_remote_stacks_tsv_base_url_panics_if_missing() {
113112
let url_src = EventSourceConfig::StacksTsvUrl(super::UrlConfig {
114113
file_url: format!("test"),
115114
});
116115
let mut config = Config::default(true, false, false, &None).unwrap();
117116

118117
config.event_sources = vec![url_src.clone()];
119-
assert_eq!(config.expected_remote_stacks_tsv_base_url(), "test");
118+
match config.expected_remote_stacks_tsv_base_url() {
119+
Ok(tsv_url) => assert_eq!(tsv_url, "test"),
120+
Err(e) => {
121+
panic!("expected tsv file: {e}")
122+
}
123+
}
120124

121125
config.event_sources = vec![];
122-
config.expected_remote_stacks_tsv_base_url();
126+
match config.expected_remote_stacks_tsv_base_url() {
127+
Ok(tsv_url) => panic!("expected no tsv file, found {}", tsv_url),
128+
Err(e) => assert_eq!(e, "could not find expected remote tsv source".to_string()),
129+
};
123130
}
124131

125132
#[test]
126-
#[should_panic(expected = "expected local-tsv source")]
127-
fn expected_local_stacks_tsv_base_url_panics_if_missing() {
133+
fn expected_local_stacks_tsv_base_url_errors_if_missing() {
128134
let path = PathBuf::from("test");
129135
let path_src = EventSourceConfig::StacksTsvPath(PathConfig {
130136
file_path: path.clone(),
131137
});
132138
let mut config = Config::default(true, false, false, &None).unwrap();
133139

134140
config.event_sources = vec![path_src.clone()];
135-
assert_eq!(config.expected_local_stacks_tsv_file(), &path);
141+
match config.expected_local_stacks_tsv_file() {
142+
Ok(tsv_path) => assert_eq!(tsv_path, &path),
143+
Err(e) => {
144+
panic!("expected tsv file: {e}")
145+
}
146+
}
136147

137148
config.event_sources = vec![];
138-
config.expected_local_stacks_tsv_file();
149+
match config.expected_local_stacks_tsv_file() {
150+
Ok(tsv_path) => panic!("expected no tsv file, found {}", tsv_path.to_string_lossy()),
151+
Err(e) => assert_eq!(e, "could not find expected local tsv source".to_string()),
152+
};
139153
}
140154

141155
#[test]

0 commit comments

Comments
 (0)