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

enhance mithril/cardano node communication #1348

Merged
merged 51 commits into from
Nov 29, 2023

Conversation

falcucci
Copy link
Collaborator

@falcucci falcucci commented Nov 14, 2023

Content

Includes:

  • a new pallas observer;
  • get block epoch number using the new pallas observer;
  • uses the current cardano-cli as a default fallback for the remaining methods;

Pre-submit checklist

  • Branch
    • Tests are provided (if possible)
    • Crates versions are updated (if relevant)
    • Commit sequence broadly makes sense
    • Key commits have useful messages
  • PR
    • No clippy warnings in the CI
    • Self-reviewed the diff
    • Useful pull request description
    • Reviewer requested

Comments

The automatic fallback mode has been implemented to make sure we progressively rollout the new integration.

Issue(s)

fixes #1315

the `cardano-cli-chain-observer` is the default for now
…-network` packages in the `cargo.lock` file from `git+https://github.com/txpipe/pallas.git#2b5f22def2a76341337417533b7116e13ac9880c` to `git+https://github.com/txpipe/pallas.git?branch=main#c772da69ec04d04df7d59da878f486c04ec16f11`.

this allows us to access the most up-to-date version of the packages and ensure that we are always using the most recent version.

```diff
diff --git a/cargo.lock b/cargo.lock
index d8e09d5b9dea..cc1ac0e50355 100644
--- a/cargo.lock
+++ b/cargo.lock
@@ -2737,7 +2737,7 @@ dependencies = [
 [[package]]
 name = "pallas-codec"
 version = "0.19.1"
-source = "git+https://github.com/txpipe/pallas.git#2b5f22def2a76341337417533b7116e13ac9880c"
+source = "git+https://github.com/txpipe/pallas.git?branch=main#c772da69ec04d04df7d59da878f486c04ec16f11"
 dependencies = [
  "hex",
  "minicbor",
@@ -2748,7 +2748,7 @@ dependencies = [
 [[package]]
 name = "pallas-crypto"
 version = "0.19.1"
-source = "git+https://github.com/txpipe/pallas.git#2b5f22def2a76341337417533b7116e13ac9880c"
+source = "git+https://github.com/txpipe/pallas.git?branch=main#c772da69ec04d04df7d59da878f486c04ec16f11"
 dependencies = [
  "cryptoxide",
  "hex",
@@ -2761,7 +2761,7 @@ dependencies = [
 [[package]]
 name = "pallas-network"
 version = "0.19.1"
-source = "git+https://github.com/txpipe/pallas.git#2b5f22def2a76341337417533b7116e13ac9880c"
+source = "git+https://github.com/txpipe/pallas.git?branch=main#c772da69ec04d04df7d59da878f486c04ec16f11"
 dependencies = [
  "byteorder",
  "hex",
```
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

cargo-doc found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

clippy found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

@falcucci falcucci changed the title enhance mithril/cardano node communication enhance mithril/cardano node communication [wip] Nov 14, 2023
```diff
diff --git a/mithril-common/src/chain_observer/pallas_observer.rs b/mithril-common/src/chain_observer/pallas_observer.rs
index b5a2c271e7db..43b3c1308aa1 100644
--- a/mithril-common/src/chain_observer/pallas_observer.rs
+++ b/mithril-common/src/chain_observer/pallas_observer.rs
@@ -26,7 +26,7 @@ impl pallaschainobserver {
         let fallback = cardanoclichainobserver::new(box::new(super::cardanoclirunner::new(
             cli_path.to_owned(),
             socket.to_owned(),
-            network.clone(),
+            network,
         )));

         self {
```
…allaschainobserver` impl in `mithril-common/src/chain_observer/pallas_observer.rs` to acquire and release the state query during the chain sync process. this ensures that the state query is properly handled and the system runs as expected.

```diff
diff --git a/mithril-common/src/chain_observer/pallas_observer.rs b/mithril-common/src/chain_observer/pallas_observer.rs
index 43b3c1308aa1..6d9affa7f873 100644
--- a/mithril-common/src/chain_observer/pallas_observer.rs
+++ b/mithril-common/src/chain_observer/pallas_observer.rs
@@ -69,6 +69,8 @@ impl chainobserver for pallaschainobserver {

         let statequery = client.statequery();

+        statequery.acquire(none).await.unwrap();
+
         // todo: maybe implicitely get the current era as default
         let era = queries_v16::get_current_era(statequery)
             .await
@@ -78,6 +80,9 @@ impl chainobserver for pallaschainobserver {
             .await
             .map_err(|err| chainobservererror::general(err.into()))?;

+        statequery.send_release().await.unwrap();
+        statequery.send_done().await.unwrap();
+
         client.chainsync().send_done().await.unwrap();
         client.abort();
```

this commit ensures that the state query is properly handled and the system runs as expected.
removes the `chain_observer_type` field from the `defaultconfiguration` struct defined in `configuration.rs`. this field was used to specify the type of chain observer adapter, but was unused and therefore can be safely removed.

the removed lines are as follows:

```
-    /// type of chain observer adapter.
-    pub chain_observer_type: string,
```

and the resulting code is:

```diff
 diff --git a/mithril-aggregator/src/configuration.rs b/mithril-aggregator/src/configuration.rs
index 97e9315c4a51..e233079cc759 100644
--- a/mithril-aggregator/src/configuration.rs
+++ b/mithril-aggregator/src/configuration.rs
@@ -273,9 +273,6 @@ pub struct defaultconfiguration {

     /// signer importer run interval default setting
     pub signer_importer_run_interval: u64,
-
-    /// type of chain observer adapter.
-    pub chain_observer_type: string,
 }

 impl default for defaultconfiguration {
@@ -289,7 +286,6 @@ impl default for defaultconfiguration {
             snapshot_store_type: "local".to_string(),
             snapshot_uploader_type: "gcp".to_string(),
             era_reader_adapter_type: "bootstrap".to_string(),
-            chain_observer_type: "cardano-cli-chain-observer".to_string(),
             reset_digests_cache: "false".to_string(),
             disable_digests_cache: "false".to_string(),
             snapshot_compression_algorithm: "zstandard".to_string(),
```

this commit removes the unnecessary field to simplify the configuration struct and avoid any potential confusion.
@falcucci falcucci force-pushed the feat/pallas-observer branch 2 times, most recently from 8a84226 to ab7eec6 Compare November 15, 2023 23:28
- removed the `epochno` type from the `localstate::queries_v16` module
- updated test code to use the new array format for the `epochno` type
- updated the `get_current_epoch_with_fallback` test to use the new array format for the `epochno` type
- updated the `get_epoch_range` test to use the new array format for the `epochno` type
- updated the `observer.get_current_epoch()` method to use the new array format for the `epochno` type
@jpraynaud jpraynaud requested review from ghubertpalo and removed request for scarmuega November 23, 2023 08:43
Copy link
Member

@jpraynaud jpraynaud left a comment

Choose a reason for hiding this comment

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

I have noticed some issues that need to be addressed before we can merge the PR 👍

But overall looks good, thanks!

mithril-common/src/chain_observer/cli_observer.rs Outdated Show resolved Hide resolved
mithril-common/src/test_utils/mod.rs Outdated Show resolved Hide resolved
mithril-common/src/test_utils/mod.rs Outdated Show resolved Hide resolved
mithril-aggregator/src/dependency_injection/builder.rs Outdated Show resolved Hide resolved
mithril-common/src/chain_observer/pallas_observer.rs Outdated Show resolved Hide resolved
mithril-common/src/chain_observer/pallas_observer.rs Outdated Show resolved Hide resolved
mithril-common/src/chain_observer/pallas_observer.rs Outdated Show resolved Hide resolved
mithril-common/src/chain_observer/pallas_observer.rs Outdated Show resolved Hide resolved
mithril-common/src/chain_observer/interface.rs Outdated Show resolved Hide resolved
@dlachaume
Copy link
Collaborator

@falcucci Your commits must be signed in order to be merged in the main branch, could you re-push them?

@dlachaume
Copy link
Collaborator

@falcucci When I run mithril-end-to-end locally from the feature branch, I have the following error:
Error: PallasChainObserverError: Failed to create new client: error connecting bearer

Are there any prerequisites before running the tests? Is it possible that it's something to do with my local environment?

@falcucci
Copy link
Collaborator Author

@falcucci Your commits must be signed in order to be merged in the main branch, could you re-push them?

@dlachaumepalo done!

@falcucci When I run mithril-end-to-end locally from the feature branch, I have the following error: Error: PallasChainObserverError: Failed to create new client: error connecting bearer

Are there any prerequisites before running the tests? Is it possible that it's something to do with my local environment?

@dlachaumepalo there is no changes to setup the tests, could you share the error trace and the command you use to run it?

is it getting the correct node.sock path to connect with?

that's werid cuz in the CI which is an isolated env works as expected

- added `pallaschainobserver` to `chain_observer` namespace
- added `pallaschainobserver::new_with_fallback` to create the observer with a fallback
- added a match for the `executionenvironment` in the `build_chain_observer` method to use the `pallaschainobserver` if in production mode.
@dlachaume
Copy link
Collaborator

@falcucci

@dlachaumepalo there is no changes to setup the tests, could you share the error trace and the command you use to run it?

cargo run -p mithril-end-to-end -- --bin-directory target/release/ --devnet-scripts-directory=./mithril-test-lab/mithril-devnet --skip-cardano-bin-download

Error: general error

Caused by:
    0: Failed to create new client
    1: error connecting bearer
    2: path must be shorter than libc::sockaddr_un.sun_path

In fact, it's fine. The problem was the same as the one you reported. Had to specify a shorter path for the parameter work-directory.

Thanks for your response Alex!

- move `test_cli_runner` module from `test_utils` to `chain_observer`
- change `testclirunner`'s visibility from `pub` to `pub(crate)`
- update usages of `testclirunner` in `chain_observer::cli_observer` and `chain_observer::pallas_observer` to refer to the new module path
- refactored of the test setup code into a separate `setup_server` function
- removed unnecessary `println!` call to improve readability
- added documentation to the `setup_server` function

```diff
diff --git a/mithril-common/src/chain_observer/pallas_observer.rs b/mithril-common/src/chain_observer/pallas_observer.rs
index fe7bc2f3c626..225b8398af5c 100644
--- a/mithril-common/src/chain_observer/pallas_observer.rs
+++ b/mithril-common/src/chain_observer/pallas_observer.rs
@@ -199,14 +199,13 @@ mod tests {
         temp_dir
     }

-    #[tokio::test]
-    async fn get_current_epoch_with_fallback() {
-        let server = tokio::spawn({
+    /// sets up a mock server.
+    async fn setup_server() -> tokio::task::joinhandle<()> {
+        tokio::spawn({
             async move {
                 let temp_dir = create_temp_dir("pallas_chain_observer_test");
                 let socket_path = temp_dir.join("node.socket").as_path().to_owned();
                 if socket_path.exists() {
-                    println!("removing previous socket");
                     fs::remove_file(&socket_path).expect("previous socket removal failed");
                 }

@@ -224,8 +223,12 @@ mod tests {
                 let result = mock_server(&mut server).await;
                 server.statequery().send_result(result).await.unwrap();
             }
-        });
+        })
+    }

+    #[tokio::test]
+    async fn get_current_epoch_with_fallback() {
+        let server = setup_server().await;
         let client = tokio::spawn(async move {
             let socket_path = std::env::temp_dir().join("pallas_chain_observer_test/node.socket");
             let fallback = cardanoclichainobserver::new(box::<testclirunner>::default());
@@ -234,10 +237,11 @@ mod tests {
                 cardanonetwork::testnet(10),
                 fallback,
             );
-            let epoch = observer.get_current_epoch().await.unwrap().unwrap();
-            assert_eq!(epoch, 8);
+            observer.get_current_epoch().await.unwrap().unwrap()
         });

-        _ = tokio::join!(client, server);
+        let (_, client_res) = tokio::join!(server, client);
+        let epoch = client_res.expect("client failed");
+        assert_eq!(epoch, 8);
     }
 }
```
@falcucci
Copy link
Collaborator Author

@jpraynaud all suggestions has been applied,

the only review left for discussion is #1348 (comment)

thank you :)

@falcucci
Copy link
Collaborator Author

bumped crates versions accordingly.

Copy link
Member

@jpraynaud jpraynaud left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Thanks for the good work @falcucci 👏

I left two last comments to address before we merge (we'll do that tomorrow morning).
Also your commits need to be signed to be merged 😉

mithril-common/src/chain_observer/pallas_observer.rs Outdated Show resolved Hide resolved
mithril-common/src/chain_observer/pallas_observer.rs Outdated Show resolved Hide resolved
the changes made in this commit are:
- the return type of `get_fallback()` has been changed from `stdresult<&cardanoclichainobserver>` to `&cardanoclichainobserver`
- the call to `map_err()` has been removed from the usage of `get_fallback()` within `get_current_datums()`, `get_current_stake_distribution()`, and `get_current_kes_period()`
- the return type of `get_current_datums()` has been changed from `stdresult<vec<txdatum>, chainobservererror>` to `result<vec<txdatum>, chainobservererror>`
@jpraynaud jpraynaud merged commit 64140b1 into input-output-hk:main Nov 29, 2023
31 checks passed
@jpraynaud
Copy link
Member

@scarmuega @falcucci the merged PR has been deployed on the testing-preview network.

It looks like it's working smoothly so far 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhance Mithril/Cardano node communication
4 participants