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

506 switch to stable rust toolchain channel #513

Merged
merged 39 commits into from
May 7, 2024

Conversation

b-yap
Copy link
Contributor

@b-yap b-yap commented Apr 19, 2024

closes #506

  • mocktopus is a lib for rust nightly alone.
    Best to constraint its import in test only, until we plan to fully refactor the code and remove it entirely.
    • added a script build-check cmd-all to build (with stable) the project without the currency's testing-utils.
  • 💥 the tests in pallet redeem is mocking functions from pallet currency. I had to put a wrapper to be able to mock it properly.
    • 💀 This is an ugly approach, but it works well.

How to begin the review:

  1. scripts/build-check cmd-all:
    • builds all features of all packages except the testing-utils of currency pallet
  2. rust-toolchain.toml
    • instead of nightly, use a not-the-latest stable version: 1.77.0
  3. .github/workflows/ci-dev.yml and .github/workflows/ci-main.yml
    • need to install the nightly version, and I chose 2023-12-29 2024-02-09
    • as stated above, nightly is required for testing.
    • fmt only works for nightly
    • for building on stable version, use the cmd-all script instead
  4. Cargo.lock:
    • had to updated based on the issue stated on the previous point
  5. all README.md s
    • all cargo tests need to override rust version with +nightly or a nightly version of your choice
  6. clients/README.md
  7. clients/runner/src/runner.rs
    • cleanup not used warnings. (need your help on this @gianfra-t)
  8. clients/stellar-relay-lib/Cargo.toml:
    • to fix sole running of tests in stellar-relay-lib,
    • need the testing-utils feature of wallet to access the functions for retrieving the secret keys
  9. clients/wallet/src/lib.rs:
    • need access to the keys when running tests only for wallet ( cargo test)
  10. clients/wallet/src/keys.rs
    • when running the example in stellar-relay-lib, it failed with file not found.
  11. pallets/currency/Cargo.toml:
    • mocktopus only in cfg test.
  12. pallets/currency/src/amount.rs:
    • allow mocktopus only in cfg test
  13. 💥 pallets/redeem/src/ext.rs 💥:
    • Again this is quite ugly but mockable feature of currency's Amount is inaccessible.
    • I wrapped it in a trait 🔻 AmountExt 🔻 where we can mock the ff methods of Amount:
      • fn lock_on()
      • fn burn_from()
      • fn unlock_on()
      • fn transfer()
    • implement the trait ^ by calling the actual methods of Amount
  14. 💥 pallets/redeem/src/lib.rs 💥 :
    • replace the call of Amount methods, with methods of trait AmountExt:
      • fn _lock_on()
      • fn _burn_from()
      • fn _unlock_on()
      • fn _transfer()
  15. 💥 pallets/redeem/src/tests.rs 💥 :
    • replace the call of Amount methods, with methods of trait AmountExt
  16. all pallets/*/rpc/Cargo.toml:
    • jsonrpsee will need the client feature
  17. files not specifically mentioned, are to resolve fmt and clippy issues.

@b-yap b-yap changed the title switch to stable rust toolchain channel 506 switch to stable rust toolchain channel Apr 19, 2024
@b-yap b-yap requested a review from a team April 24, 2024 09:24
# Call `rustup show` as a hack so that the toolchain defined in rust-toolchain.toml is installed
run: rustup show

- name: Remove rust-toolchain.toml for nightly
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Installing the nightly (and even setting it to default) will still be overriden by the rust-toolchain.toml file, hence it needs to be removed.
See https://github.com/pendulum-chain/spacewalk/actions/runs/8751187110/job/24020665297#step:9:33

args: --all-features --tests --benches --examples -- -A clippy::all -W clippy::correctness -A forgetting_copy_types -A forgetting_references
Copy link
Contributor Author

@b-yap b-yap Apr 24, 2024

Choose a reason for hiding this comment

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

we cannot use --all-targets here because the test must use the nightly version, while the libs and bins must use the stable version

@@ -183,6 +183,7 @@ impl Runner {
// The set permissions are: -rwx------
.mode(0o700)
.create(true)
.truncate(true)
Copy link
Contributor Author

@b-yap b-yap Apr 24, 2024

Choose a reason for hiding this comment

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

a warning popped up on nightly version 2024-02-09. truncate(true) was suggested.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a good suggestion. I just checked at the docs for this option and it will essentially ensure that the opened file is clean, which is what we want since we will save the new binary in there.

In any case at the end of the operation the checksums will be compared, just in case.

@@ -515,10 +513,6 @@ impl RunnerExt for Runner {
&self.opts.download_path
}

fn parachain_url(&self) -> String {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was found to be unused. I also checked, and found it unused anywhere in the code. Please confirm @gianfra-t

Copy link
Contributor

Choose a reason for hiding this comment

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

We remove some logic from the original runner code, so it makes sense that some methods are not used. I think it is fine!

@@ -568,10 +562,6 @@ impl RunnerExt for Runner {
Runner::try_load_downloaded_binary(self, release)
}

async fn get_request_bytes_wrapper(&self, url: String) -> Result<Bytes, Error> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I think it is fine to remove.

@@ -80,7 +80,7 @@ async fn read_message_from_stellar(connector: &mut Connector) -> Result<Xdr, Err
// 1. the length of the next stellar message
// 2. the remaining bytes of the previous stellar message
match connector.tcp_stream.read(&mut buff_for_reading).await {
Ok(size) if size == 0 => continue,
Ok(0) => continue,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion from version nightly-2024-02-09

@@ -35,8 +35,7 @@ const BASE_BACKOFF_DELAY_IN_SECS: u64 = 10;
pub fn horizon_url(is_public_network: bool, is_need_fallback: bool) -> &'static str {
if is_public_network {
if is_need_fallback {
let other_urls =
vec!["https://horizon.stellarx.com", "https://horizon.stellar.lobstr.co"];
let other_urls = ["https://horizon.stellarx.com", "https://horizon.stellar.lobstr.co"];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion from version nightly-2024-02-09 to use the array directly

@@ -7,7 +7,11 @@ use std::env;
// previously passed to the environment.
// A variable not .env but passed to the environment will not be overridden.
fn get_env_variables(key: &str) -> Option<String> {
dotenv::from_filename("../.env").ok();
if let None = dotenv::from_filename("../.env").ok() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

had a problem with running the example in stellar-relay-lib.
I moved the env file to the root directory; that's why I added a condition to try looking at the current directory.

@@ -8,7 +8,7 @@ pub use task::*;
mod cache;
pub mod error;
mod horizon;
#[cfg(feature = "testing-utils")]
#[cfg(any(test, feature = "testing-utils"))]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

keys are also used in the unit test of this lib, so I added test.

@@ -104,11 +102,14 @@ impl pallet_balances::Config for Test {
parameter_types! {
pub const GetNativeCurrencyId: CurrencyId = DEFAULT_NATIVE_CURRENCY;
pub const GetRelayChainCurrencyId: CurrencyId = DEFAULT_COLLATERAL_CURRENCY;
#[cfg(feature = "runtime-benchmarks")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this causes error to building/testing other features.
for example:

cargo +nightly test -p vault --features integration-test`

actually fails with a duplicate lang item issue.

cfg macros should be done outside of parameter_types! macros.

@@ -15,7 +15,7 @@ pub struct DataKey {

impl PartialOrd<Self> for DataKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.blockchain.partial_cmp(&other.blockchain)
Some(self.cmp(other))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a suggestion in version nightly-2024-02-09

@TorstenStueber
Copy link
Member

Hey @b-yap, looks like a lot of work went into this. Thanks!

I see that a couple of changes are also pure formatting changes. How is that? Did the formatter settings change or did we not properly use a formatter before?

@b-yap
Copy link
Contributor Author

b-yap commented Apr 24, 2024

@TorstenStueber I did not touch the fmt toml file. I think these are all new preferences from the new version?
Like removing ; of a return statement, or removing { } for single statements.

Because I ran cargo +nightly-2024-02-09 fmt --all after the CI failed.

@TorstenStueber
Copy link
Member

Hmm, is this expected that the default formatting changes between different versions of rust (and therefore rust fmt)? Or did we not properly format before the change?

@b-yap
Copy link
Contributor Author

b-yap commented Apr 24, 2024

@TorstenStueber I confirmed it by running
cargo +nightly-2023-04-15 fmt --all on a clean main branch, and there were no changes. We did it properly.

Screenshot 2024-04-24 at 11 25 08 PM

^ In this screenshot, the differences occurred after cargo +nightly-2024-02-09 fmt --all ; the formatting changes are coming from the new version.

@TorstenStueber
Copy link
Member

Interesting and unexpected. Looking again, there seem to be only a few changes that are only due to formatting. One of them is this and it is clear that this it enacts the trailing_semicolon = false rule. However, trailing_semicolon was already defined to be false before your change (see on main). So it looks like the formatter did not properly format all files before your change. Maybe a bug in that version of rust. But all good now!

README.md Outdated
@@ -78,7 +78,7 @@ The handling of redeem events is implemented in `clients/vault/src/redeem.rs`.
## Run all tests

```
cargo test --lib --features standalone-metadata -- --nocapture
cargo +nightly --lib --features standalone-metadata -- --nocapture
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be cargo +nightly test... right? I am not sure about the order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right! 🤦🏽‍♀️ My bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

b-yap added a commit that referenced this pull request Apr 29, 2024
CI test will be confirmed on #513
Copy link
Member

@ebma ebma left a comment

Choose a reason for hiding this comment

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

Nice changes 👍

  • In all the READMEs we now mention +nightly-<some_version>. This is okay and if we want to change it in the future, we can just do a find-and-replace search to change it without much effort. However, I'm still curious if there is a way for us to change it to +nightly but have cargo run it with a specific version. Can we define both stable and nightly in the rust-toolchain.toml or similar so that both versions are fixed?
  • We also need to make sure that the changes are applied to our other compilation pipeline in the other infrastructure.

.github/workflows/ci-main.yml Show resolved Hide resolved
.github/workflows/ci-main.yml Show resolved Hide resolved
clients/README.md Outdated Show resolved Hide resolved
@@ -77,22 +95,24 @@ You can also find an example for setting these variables in the `.env.example` f

### Running the tests

**Note** Tests should run using Rust **_`nightly-2023-12-29`_** version. Make sure to install and add the target `wasm32-unknown-unknown`.
Copy link
Member

Choose a reason for hiding this comment

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

In the top-level README, you put nightly-2024-02-09. Let's try to use the same here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to "nightly";

@@ -526,12 +528,12 @@ mod self_redeem {
) -> DispatchResult {
// burn the tokens that the vault no longer is backing
consumed_issued_tokens
.lock_on(account_id)
._lock_on(account_id)
Copy link
Member

Choose a reason for hiding this comment

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

Can we find a way not to have to call functions with this underscore? I understand you created an extra trait to make this mockable. Can we move that trait next to the actual definition and unify It so that we don't need the underscore?
Otherwise, this creates some confusion and it's not easy to see without context why this is necessary.

Copy link
Contributor Author

@b-yap b-yap May 2, 2024

Choose a reason for hiding this comment

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

I agree; in a few weeks I will have forgotten this.

I have reverted the code to allow mocktopus in pallet currency's feature: testing-utils.
That means cargo b --all-features will not work.

I created a script build-check cmd-all to build all known packages for stable rust version.
testing-utils will only compile on nightly version; or when running cargo test.

pallets/vault-registry/README.md Outdated Show resolved Hide resolved
@gianfra-t
Copy link
Contributor

It looks very good 👍!
I am surprised why the mockable macro works different between versions and now it doesn't allow access to the trait anymore. In any case I think the wrapper trait workaround is not that "ugly" and solves the problem.

The only thing remaining to me is to make it work with the --features runtime-benchmarks flag.

b-yap added a commit that referenced this pull request Apr 29, 2024
@b-yap b-yap force-pushed the 506-switch-to-stable-rust-toolchain-channel branch from ebe7d97 to e3d0644 Compare April 29, 2024 13:22
b-yap added a commit that referenced this pull request Apr 30, 2024
@b-yap
Copy link
Contributor Author

b-yap commented Apr 30, 2024

@ebma

In all the READMEs we now mention +nightly-<some_version>.

  • Testing using the latest version of nightly is fine; as long as it's installed. The danger is testing using an older version. I changed to +nightly in the READMEs.

Can we define both stable and nightly in the rust-toolchain.toml or similar so that both versions are fixed?

  • I checked the profile and there's nothing there.
    It's not even possible per package; I doubt it can differentiate between build and test.
  • I tried cargo aliases but the override is not a "subcommand", so:
         error: no such command: `+nightly`
    
       Cargo does not handle `+toolchain` directives.
       Did you mean to invoke `cargo` through `rustup` instead?
    

@b-yap b-yap requested a review from ebma May 2, 2024 10:19
b-yap and others added 20 commits May 7, 2024 14:01
and use a variable to assign value of the nightly version in CIs
Co-authored-by: Marcel Ebert <mail@marcel-ebert.de>
…ob/24506180390?pr=513#step:11:3011

cargo build is too heavy; opting with check instead
also added `set -e` to make sure the script exits upon the first error found
…ob/24521111869?pr=513#step:14:20,

cleanup the cmd-all command of clippy for `runner`
add more comments/explanations
@b-yap b-yap force-pushed the 506-switch-to-stable-rust-toolchain-channel branch from 38547a6 to 1a5c3d6 Compare May 7, 2024 06:01
@b-yap
Copy link
Contributor Author

b-yap commented May 7, 2024

@ebma @gianfra-t I had to rebase.

@b-yap b-yap requested a review from ebma May 7, 2024 06:03
Copy link
Member

@ebma ebma left a comment

Choose a reason for hiding this comment

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

I think we can merge this now once the CI passed.

Great job @b-yap 👌

Copy link
Contributor

@gianfra-t gianfra-t left a comment

Choose a reason for hiding this comment

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

Thanks @b-yap looks really good!

@b-yap b-yap merged commit 1c3ea1c into main May 7, 2024
2 checks passed
@b-yap b-yap deleted the 506-switch-to-stable-rust-toolchain-channel branch May 7, 2024 12:40
@ebma
Copy link
Member

ebma commented May 7, 2024

@zoveress as part of this PR, the CI workflows we do for Spacewalk changed. Do you think you are able to adjust the Gitlab testing pipeline based on the contents of this file? Otherwise please reach out and we are happy to help. The current testing pipeline will not work anymore on Gitlab because we switched to a different Rust toolchain and also had to run the CI a bit differently.

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.

Switch to 'stable' rust toolchain channel
4 participants