-
Notifications
You must be signed in to change notification settings - Fork 7
Extract testgen-hs handling into dedicated crates #537
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a1d425c
chore: bump testgen-hs flake input to 10.6.3.1 and switch windows URL…
ginnun 72daec9
refactor: extract testgen-hs handling into bf-testgen and bf-error-de…
ginnun 38662c6
chore: switch testgen-hs flake input owner to blockfrost
ginnun a59da0b
test: add coverage for TestgenResponse deserialization
ginnun 75aee91
refactor(testgen): polish API types and extract constants
ginnun bc68d97
chore: bump blockfrost-tests flake input
ginnun 02c4a39
fix: Applied PR feedback
ginnun 83e9a94
Merge branch 'main' into refactor/testgen-hs-handling
michalrus 7f9ed88
fix(testgen): reset child pid after subprocess exit
ginnun 5763183
fix(testgen): formatting
ginnun 1544eb1
fix(testgen): some log levels changed to warning
ginnun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| pub mod git; | ||
| pub mod target; | ||
| pub mod testgen_hs; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| fn main() { | ||
| bf_build_utils::testgen_hs::ensure(); | ||
|
ginnun marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| )) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod external; | ||
| #[cfg(test)] | ||
| mod tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #[cfg(test)] | ||
| mod random; | ||
| #[cfg(test)] | ||
| pub(crate) mod specific; |
6 changes: 5 additions & 1 deletion
6
crates/node/src/cbor/tests/random.rs → crates/error_decoder/src/tests/random.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
testgenhosts the base & shared parts of the external binary communication.error_decoderhosts the relevant logic to error decoding while depending ontestgen. tx evaluation will do the same (another pr).There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😇
There was a problem hiding this comment.
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
crateis more natural in this case. For example,evaluatorcrate (in another PR) contains external and native implementation in one crate, but as separate modules.There was a problem hiding this comment.
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:
Based on: