Skip to content
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
32 changes: 29 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ members = [
"crates/api_provider",
"crates/node",
"crates/data_node",
"crates/testgen",
"crates/error_decoder",
Comment on lines +15 to +16
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question before I read the entire PR – can you elaborate why we need separate crates? 🙏 It's not clear from the PR description

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testgen hosts the base & shared parts of the external binary communication. error_decoder hosts the relevant logic to error decoding while depending on testgen. tx evaluation will do the same (another pr).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, yes, that's clear, but I guess I meant more like: why separate crates, and not just separate modules? Something like that

Copy link
Copy Markdown
Collaborator Author

@ginnun ginnun May 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way looked cleaner to me, it gives better separation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modules also provide separation, I was just curious why 😇

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logical grouping I've chosen is what functionality a piece of code is providing, instead of how they are implemented underneath. To me, a crate is more natural in this case. For example, evaluator crate (in another PR) contains external and native implementation in one crate, but as separate modules.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I dug a little and the consensus seems to be:

  • Use modules for organization, visibility, and local flexibility.
  • Use crates when you want a real dependency/compilation boundary, especially one that improves reuse, build parallelism, or architectural separation.
    • I.e. do not split into crates just because "more modular" sounds better, the dependency graph has to buy you compilation time.

Based on:

"crates/gateway",
"crates/sdk_bridge",
"crates/integration_tests",
Expand All @@ -32,6 +34,7 @@ bf-build-utils = { path = "crates/build_utils", package = "blockfrost-platform-b
bf-common = { path = "crates/common", package = "blockfrost-platform-common" }
bf-data-node = { path = "crates/data_node", package = "blockfrost-platform-data-node" }
bf-node = { path = "crates/node", package = "blockfrost-platform-node" }
bf-testgen = { path = "crates/testgen", package = "blockfrost-platform-testgen" }
bip39 = "2.2.2"
blockfrost = { version = "1.2.3", default-features = false, features = [
"native-tls",
Expand Down
1 change: 1 addition & 0 deletions crates/build_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod git;
pub mod target;
pub mod testgen_hs;
21 changes: 21 additions & 0 deletions crates/build_utils/src/target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pub const fn os() -> &'static str {
if cfg!(target_os = "macos") {
"darwin"
} else if cfg!(target_os = "linux") {
"linux"
} else if cfg!(target_os = "windows") {
"windows"
} else {
panic!("Unsupported OS")
}
}

pub const fn arch() -> &'static str {
if cfg!(target_arch = "x86_64") {
"x86_64"
} else if cfg!(target_arch = "aarch64") {
"aarch64"
} else {
panic!("Unsupported architecture")
}
}
23 changes: 4 additions & 19 deletions crates/build_utils/src/testgen_hs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,10 @@ pub fn ensure() {
return;
}

let testgen_lib_version = "10.6.3.0";

let target_os = if cfg!(target_os = "macos") {
"darwin"
} else if cfg!(target_os = "linux") {
"linux"
} else if cfg!(target_os = "windows") {
"windows"
} else {
panic!("Unsupported OS");
};
let testgen_lib_version = "10.6.3.1";

let arch = if cfg!(target_arch = "x86_64") {
"x86_64"
} else if cfg!(target_arch = "aarch64") {
"aarch64"
} else {
panic!("Unsupported architecture");
};
let target_os = super::target::os();
let arch = super::target::arch();

let suffix = if target_os == "windows" {
".zip"
Expand All @@ -44,7 +29,7 @@ pub fn ensure() {

let file_name = format!("testgen-hs-{testgen_lib_version}-{arch}-{target_os}");
let download_url = format!(
"https://github.com/input-output-hk/testgen-hs/releases/download/{testgen_lib_version}/{file_name}{suffix}"
"https://github.com/blockfrost/testgen-hs/releases/download/{testgen_lib_version}/{file_name}{suffix}"
);

println!("Looking for {file_name}");
Expand Down
26 changes: 26 additions & 0 deletions crates/error_decoder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "blockfrost-platform-error-decoder"
version.workspace = true
license.workspace = true
edition.workspace = true
build = "build.rs"

[dependencies]
bf-common.workspace = true
bf-testgen.workspace = true
hex.workspace = true
serde_json.workspace = true
tokio.workspace = true

[features]
tarpaulin = []

[build-dependencies]
bf-build-utils.workspace = true

[dev-dependencies]
bf-testgen = { workspace = true, features = ["test-utils"] }
sysinfo.workspace = true
pallas-network.workspace = true
pallas-hardano.workspace = true
pallas-codec.workspace = true
5 changes: 5 additions & 0 deletions crates/error_decoder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# error decoder

This package contains external error decoder for tx submit errors. Historically used to decode and generate error messages that are returned from our API.
Now it's only used to test `pallas-hardano` based implementation and generate random test cases. `pallas-hardano` vs ledger error generating tests are also in this crate.
Related `pallas-hardano` part initially implemented in this repository and later merged into pallas.
3 changes: 3 additions & 0 deletions crates/error_decoder/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
bf_build_utils::testgen_hs::ensure();
Comment thread
ginnun marked this conversation as resolved.
}
113 changes: 113 additions & 0 deletions crates/error_decoder/src/external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use bf_common::errors::AppError;
use bf_testgen::testgen::{Testgen, TestgenResponse, Variant};

#[derive(Clone)]
pub struct ExternalDecoder {
testgen: Testgen,
}
impl ExternalDecoder {
pub fn spawn() -> Result<Self, AppError> {
let testgen = Testgen::spawn(Variant::DeserializeStream)
.map_err(|err| AppError::Server(format!("Failed to spawn ExternalDecoder: {err}")))?;

Ok(Self { testgen })
}

pub async fn decode(&self, input: &[u8]) -> Result<serde_json::Value, String> {
match self.testgen.decode(input).await {
Ok(resp) => match resp {
TestgenResponse::Ok(value) => Ok(value),
TestgenResponse::Err(err) => Err(err.to_string()),
},
Err(err) => Err(err),
}
}

/// This function is called at startup, so that we make sure that the worker is reasonable.
pub async fn startup_sanity_test(&self) -> Result<(), String> {
let input = hex::decode("8182068182028200a0").map_err(|err| err.to_string())?;
let result = self.decode(&input).await;
let expected = serde_json::json!({
"contents": {
"contents": {
"contents": {
"era": "ShelleyBasedEraConway",
"error": [
"ConwayCertsFailure (WithdrawalsNotInRewardsCERTS (Withdrawals {unWithdrawals = fromList []}))"
],
"kind": "ShelleyTxValidationError"
},
"tag": "TxValidationErrorInCardanoMode"
},
"tag": "TxCmdTxSubmitValidationError"
},
"tag": "TxSubmitFail"
});

if result == Ok(expected) {
Ok(())
} else {
Err(format!(
"ExternalDecoder: startup_sanity_test failed: {result:?}"
))
}
}

/// A single global [`ExternalDecoder`] that you can cheaply use in tests.
#[cfg(all(test, not(feature = "tarpaulin")))]
pub fn instance() -> Self {
GLOBAL_INSTANCE.clone()
}
}

#[cfg(all(test, not(feature = "tarpaulin")))]
static GLOBAL_INSTANCE: std::sync::LazyLock<ExternalDecoder> =
std::sync::LazyLock::new(|| ExternalDecoder::spawn().expect("Failed to spawn ExternalDecoder"));

#[cfg(test)]
mod tests {
// The CBOR test cases are covered by `crates/error_decoder/src/tests/specific.rs`,
// which already cross-validates the Rust (pallas-hardano) implementation against the
// Haskell (testgen-hs) external decoder. This module only contains tests that are
// unique to the external decoder itself (e.g. crash-recovery behaviour).
#[cfg(not(feature = "tarpaulin"))]
use super::*;

#[tokio::test]
//#[tracing_test::traced_test]
#[cfg(not(feature = "tarpaulin"))]
async fn test_sanity() {
let decoder = ExternalDecoder::spawn().unwrap();

// Wait for it to come up:
decoder.startup_sanity_test().await.unwrap();

// Now, kill our child to test the restart logic:
sysinfo::System::new_all()
.process(sysinfo::Pid::from_u32(decoder.testgen.child_pid().unwrap()))
.unwrap()
.kill();

tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

let input = hex::decode("8182068183051a000c275b1a000b35ec").unwrap();
let result = decoder.decode(&input).await;

assert_eq!(
result,
Ok(serde_json::json!({"contents":
{"contents":
{"contents":
{"era": "ShelleyBasedEraConway", "error":
["ConwayTreasuryValueMismatch (Mismatch {mismatchSupplied = Coin 734700, mismatchExpected = Coin 796507})"],
"kind": "ShelleyTxValidationError"
},
"tag": "TxValidationErrorInCardanoMode"
}, "tag": "TxCmdTxSubmitValidationError"
},
"tag": "TxSubmitFail"
}
))
);
}
}
3 changes: 3 additions & 0 deletions crates/error_decoder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod external;
#[cfg(test)]
mod tests;
4 changes: 4 additions & 0 deletions crates/error_decoder/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(test)]
mod random;
#[cfg(test)]
pub(crate) mod specific;
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use super::*;
#![cfg(not(feature = "tarpaulin"))]

use bf_testgen::tests::{CaseType, check_generated_cases};
use pallas_hardano::display::haskell_error::serialize_error;
use pallas_network::miniprotocols::localtxsubmission::TxValidationError;

#[test]
#[allow(non_snake_case)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,80 @@ done
```
*/

use super::verify_one;
#[cfg(test)]
use pallas_network::miniprotocols::localtxsubmission::TxValidationError;

/// This function takes a CBOR-encoded `ApplyTxErr`, and verifies our
/// deserializer against the Haskell one. Use it for specific cases.
///
/// Under `tarpaulin`, the external Haskell decoder (`testgen-hs`) is not
/// available, so we only exercise the Rust decode + serialize path (which is
/// what we want coverage for anyway).
#[cfg(test)]
pub(crate) async fn verify_one(cbor: &str) {
use pallas_hardano::display::haskell_error::serialize_error;

let cbor = hex::decode(cbor).unwrap();

let our_decoding = decode_error(&cbor);
let our_json = serialize_error(our_decoding).expect("Failed to serialize error");

#[cfg(not(feature = "tarpaulin"))]
{
use crate::external::ExternalDecoder;

let reference_json = match ExternalDecoder::instance().decode(&cbor).await {
Ok(value) => value,
Err(shared_decoder_err) => {
// Recover from a poisoned shared decoder process by retrying with a fresh one.
let fresh_decoder = ExternalDecoder::spawn()
.expect("Failed to spawn a fresh ExternalDecoder for retry");
fresh_decoder.decode(&cbor).await.unwrap_or_else(|fresh_decoder_err| {
panic!(
"Failed to decode reference JSON with both shared and fresh ExternalDecoder instances. shared_error={shared_decoder_err}, fresh_error={fresh_decoder_err}, cbor={}",
hex::encode(&cbor)
)
})
},
};

assert_json_eq!(reference_json, our_json);
}

// Under tarpaulin: just assert the Rust decoder didn't panic and produced valid JSON.
#[cfg(feature = "tarpaulin")]
{
let _ = our_json;
}
}
#[cfg(test)]
fn decode_error(bytes: &[u8]) -> TxValidationError {
use pallas_codec::minicbor;

let mut decoder = minicbor::Decoder::new(bytes);
decoder.decode().unwrap()
}

#[cfg(all(test, not(feature = "tarpaulin")))]
macro_rules! assert_json_eq {
($left:expr, $right:expr) => {
if $left != $right {
let left_pretty = serde_json::to_string_pretty(&$left).unwrap();
let right_pretty = serde_json::to_string_pretty(&$right).unwrap();
panic!(
concat!(
"assertion `left == right` failed\n",
" left:\n {}\n right:\n {}",
),
left_pretty.replace("\n", "\n "),
right_pretty.replace("\n", "\n "),
);
}
};
}

#[cfg(all(test, not(feature = "tarpaulin")))]
pub(crate) use assert_json_eq; // export it

#[tokio::test]
#[allow(non_snake_case)]
Expand Down
Loading
Loading