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

Update code snippets and move to rust workspace #302

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

0xLucca
Copy link
Collaborator

@0xLucca 0xLucca commented Jan 8, 2025

This PR reorganizes the code snippets in the zero-to-hero tutorial to be part of the Rustr workspace

@0xLucca
Copy link
Collaborator Author

0xLucca commented Jan 8, 2025

@UtkarshBhardwaj007 I have migrated the existing code for the current tutorials. I'll wait until the benchmarking PR is merged so I can include that here

@UtkarshBhardwaj007
Copy link
Collaborator

@UtkarshBhardwaj007 I have migrated the existing code for the current tutorials. I'll wait until the benchmarking PR is merged so I can include that here

Thanks, this is great. I believe you mean this PR? It is merged now and you should be able to use it. I'll review once you do :)

@0xLucca 0xLucca marked this pull request as ready for review January 9, 2025 15:34
@0xLucca 0xLucca requested a review from a team as a code owner January 9, 2025 15:34
frame-system = { version = "38.0.0", default-features = false }
scale-info = { version = "2.11.1", default-features = false }
sp-runtime = { version = "39.0.5", default-features = false }
color-print = { version = "0.3.4" }
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, if you use the umbrella crate here, you will have a much easier time updating dependencies.

@@ -0,0 +1,193 @@
use crate::{
Copy link
Contributor

Choose a reason for hiding this comment

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

Double checking: are all of these files actually needed?

It seems like this step of the tutorial is about benchmarking.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we want to compile the runtime without modifying the template src code we need it

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

I see a LOT of code being pulled here, sometimes multiple copies of the same. We need to double check if this is the right pattern.

I mean, why do we need code related to XCM config, a full working runtime and so on here?

Let's establish a framework around how we should do this task that is sane, minimal but still achieves to goal.

Imagine you have the old school doc in front of you, with a hardcoded code in the markdown file.

The question you should be asking is this:

  • What is the "smallest working rust project" that I can add, that can contain this snippet and make sure it is correct?

And only create that.

The only bottleneck/exception to this is that you might want to reference snippets of code in the original parachain template that this tutorial is based on, in which case we can embed the correct version of this template that we are relying on as a git submodule to this repo. This will actually improve our flow, as it will be super clear: "this toturial is based on the parachain template commit XXX", and every time there is a new parachain template release, we can also update it here.

@kianenigma
Copy link
Contributor

Screenshot 2025-01-10 at 16 08 04

can we hide the extra lines in this with mkdocs preprocessors?

@UtkarshBhardwaj007
Copy link
Collaborator

UtkarshBhardwaj007 commented Jan 13, 2025

@0xLucca @nhussein11 Adding the points that we discussed here for your review @kianenigma @albertov19 :

  • We will ensure that every bit of code we write/migrate-to-workspace has tests and is buildable. The rendering would be identical to what the tutorials already have (except in case of bugs/issues). All GitHub workflows should succeed for a PR to be merged.
  • We will stick to the 1-crate-approach as much as possible. Meaning we will try to have all the code of a tutorial (and all its steps) in one crate and we’ll have separate crates for separate tutorials in the common workspace and use pre-processing to generate the code snippets on the tutorials website.
  • However, in some cases, pre-processing might not be enough as we might be editing some code we wrote in earlier steps for a tutorial. In these cases, we will create a new crate for a step in a tutorial on a need-basis trying to minimise code duplication.
  • We will try to have the minimum code needed to build and test. We will try to exclude template/boilerplate code as much as possible. However, if we end up needing to mock dependencies or having to implement elaborate work-arounds to just get rid of template files, we would rather include these files in the code to allow for successful builds and tests (similar to what is expected in an actual production environment) but hide these files from the tutorials using mkdocs pre-processing.
  • For licenses, we will have the MIT-0 license at the top of all the code files we create. We are unsure of what to do about the files that are pulled in from the templates (for example). I think we should add license headers to these as well? @kianenigma @albertov19
  • A "good-to-have" requirement is to use the umbrella crate for imports as it re-exports a lot of the commonly used pallets.

@0xLucca Feel free to add to this in case I missed something :)

@albertov19
Copy link
Collaborator

Everything makes sense IMO. I'll let Kian comment a bit more on the Substrate/Rust technical side of things.

  • For licenses, we will have the MIT-0 license at the top of all the code files we create. We are unsure of what to do about the files that are pulled in from the templates (for example). I think we should add license headers to these as well? @kianenigma @albertov19
  • A "good-to-have" requirement is to use the umbrella crate for imports as it re-exports a lot of the commonly used pallets.

@0xLucca Feel free to add to this in case I missed something :)

If templates are within our control, we should add licenses to it. If they are external (OpenZeppelin for example,) they inherit the license of the code being imported.

@0xLucca
Copy link
Collaborator Author

0xLucca commented Jan 13, 2025

I'll work today on leaving just one crate and adjusting all the .md files to use that.

Regarding licenses, there are some files that we pull from the parachain template that do not have licenses (example), some are licensed as unlicensed (example) and others as Apache 2.0 (example).

For now, I'll just add the MIT-0 license to the files we created from scratch

@eshaben eshaben added B0 - Needs Review Pull request is ready for review C1 - Medium Medium priority task labels Jan 13, 2025
@eshaben eshaben added the A1 - Maintenance Major Pull request contains major updates to an existing page (i.e., adding a new section, reorgs, etc.) label Jan 13, 2025
@0xLucca
Copy link
Collaborator Author

0xLucca commented Jan 14, 2025

@kianenigma @UtkarshBhardwaj007 This is ready to review. I've moved all the code to a single crate and updated the md files

0xLucca and others added 2 commits January 15, 2025 10:07
Co-authored-by: Nicolás Hussein <80422357+nhussein11@users.noreply.github.com>
@0xLucca 0xLucca requested a review from nhussein11 January 15, 2025 13:13
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/Cargo.toml:24:30'
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/Cargo.toml:35:35'
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/Cargo.toml:22:28'
]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
]
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/Cargo.toml:30:30'

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's not hardcode things as much as possible. I know we have to do some workarounds with mkdocs to make this work but I think it is better than hardcoding things in the .md files.

--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/Cargo.toml::14'

--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/Cargo.toml:22:28'
]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
]
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/Cargo.toml:30:30'

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/src/lib.rs:203:203'
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs:20:23'
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs:36:47'
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs:57:57'
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs:218:218'

Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing one closing {. The extra tab before the first line is to ensure consistent formatting as mkdocs doesn't take care of that when we use code snippets.

@@ -0,0 +1,305 @@
// This is free and unencumbered software released into the public domain.
Copy link
Contributor

Choose a reason for hiding this comment

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

@0xLucca can you please do the exercise I proposed in #302 (review)?

What snippets of code do you want to add to the tutorial's markdown format? What is the smallest running code that can represent that?

I don't get why don't build a that is

  1. frame system
  2. pallet utility
  3. custom pallet
  4. the macros related to benchmarking

and that's it. Why should this simple tutorial have all of this runtime apis, xcm configs, weights files and so on?

Or anything else that you actually want to show?

These codes are all fairly independent of any template, and work on any of them.

If you have a good reason that any of this code actually depends on the full runtime, please tell me, but I am pretty sure there is none.

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

I would like to ask one more time to reevaluate the approach here and not copy paste the template here

https://github.com/polkadot-developers/polkadot-docs/pull/302/files#r1917045718

@@ -118,7 +119,8 @@ In this step, you will configure two essential components that are critical for
Add the following `Config` trait definition to your pallet:

```rust
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/src/lib.rs:33:42'
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs:45:53'
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see this is at various places. Could you please update this everywhere just for consistency.

--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs:179:186'
#[pallet::weight(0)]
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs:188:188'
}
```
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing {

```rust
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/src/lib.rs::203'
--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallets/custom-pallet/src/lib.rs::23'
Copy link
Collaborator

Choose a reason for hiding this comment

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

This includes the license. I don't think we need to show the license in the tutorials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A1 - Maintenance Major Pull request contains major updates to an existing page (i.e., adding a new section, reorgs, etc.) B0 - Needs Review Pull request is ready for review C1 - Medium Medium priority task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants