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

feat(primitives): new type for sector number #585

Merged
merged 10 commits into from
Nov 21, 2024

Conversation

cernicc
Copy link
Member

@cernicc cernicc commented Nov 18, 2024

Description

Introduce a new type for SectorNumber that is used throughout the workspace. When you hold an instance of the SectorNumber you can know that the SectorNumber is in bounds and no additional checking is needed. The downside is, that there is much juggling between u64 and SectorNumber in tests. Outside of tests, the usage is almost similar as it was before.

The pr is big because of changes in tests.

Important

Let's break down the implementation and see if the safety benefits outweigh the additional complexity.

The SectorNumber needs to implement Decode. It's used by the runtime when decoding scale format to some runtime specific type. Implementation forwards the input to the u64 decoder and tries to initialize the SectorNumber.

If the decoding fails there is a nice error log on the node achieved by adding chain-error to the codec:
Bad input data provided to validate_transaction: Could not decode `RuntimeCall::StorageProvider.0`: Could not decode `Call::pre_commit_sectors::sectors`: Could not decode `SectorPreCommitInfo::sector_number`: Sector number is too large

The frontend gets a more cryptic error. I didn't find any approach that would made this error better. I think this is also a side effect of some unrelated error that happens while processing a block:

{
    "jsonrpc": "2.0",
    "error": {
        "code": 1002,
        "message": "Verification Error: Runtime error: Execution failed: Execution aborted due to trap: wasm trap: wasm `unreachable` instruction executed\nWASM backtrace:\nerror while executing at wasm backtrace:\n    0: 0x4354b2 - <unknown>!rust_begin_unwind\n    1: 0x3f877 - <unknown>!core::panicking::panic_fmt::h7d3847f6b13ee8e3\n    2: 0x1e08a7 - <unknown>!TaggedTransactionQueue_validate_transaction",
        "data": "RuntimeApi(\"Execution failed: Execution aborted due to trap: wasm trap: wasm `unreachable` instruction executed\\nWASM backtrace:\\nerror while executing at wasm backtrace:\\n    0: 0x4354b2 - <unknown>!rust_begin_unwind\\n    1: 0x3f877 - <unknown>!core::panicking::panic_fmt::h7d3847f6b13ee8e3\\n    2: 0x1e08a7 - <unknown>!TaggedTransactionQueue_validate_transaction\")"
    },
    "id": 51
}

The sector number also implements scale_decode::IntoVisitor and scale_decode::Visitor which is used by the subxt to decode types. This implementation replaces the auto derive of DecideAsType.

Checklist

  • Make sure that you described what this change does.
  • Have you tested this solution?
  • Did you document new (or modified) APIs?

@cernicc cernicc linked an issue Nov 18, 2024 that may be closed by this pull request
@cernicc cernicc marked this pull request as draft November 18, 2024 09:47
cli/polka-storage-provider/server/src/db.rs Outdated Show resolved Hide resolved
primitives/proofs/src/types.rs Outdated Show resolved Hide resolved
pallets/market/src/lib.rs Show resolved Hide resolved
pallets/storage-provider/src/lib.rs Outdated Show resolved Hide resolved
@cernicc cernicc marked this pull request as ready for review November 19, 2024 11:26
aidan46
aidan46 previously approved these changes Nov 19, 2024
cli/polka-storage-provider/server/src/db.rs Outdated Show resolved Hide resolved
cli/polka-storage-provider/server/src/db.rs Outdated Show resolved Hide resolved
cli/polka-storage-provider/server/src/db.rs Outdated Show resolved Hide resolved
pallets/storage-provider/src/deadline.rs Outdated Show resolved Hide resolved
pallets/storage-provider/src/partition.rs Outdated Show resolved Hide resolved
pallets/storage-provider/src/partition.rs Outdated Show resolved Hide resolved
th7nder
th7nder previously approved these changes Nov 20, 2024
@jmg-duarte jmg-duarte added the ready for review Review is needed label Nov 20, 2024
@jmg-duarte jmg-duarte added this to the Phase 3 milestone Nov 20, 2024
Copy link
Collaborator

@jmg-duarte jmg-duarte left a comment

Choose a reason for hiding this comment

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

LGTM but some food for thought

primitives/proofs/src/types.rs Outdated Show resolved Hide resolved
pallets/storage-provider/src/tests/pre_commit_sectors.rs Outdated Show resolved Hide resolved
pallets/storage-provider/src/tests/mod.rs Outdated Show resolved Hide resolved
th7nder
th7nder previously approved these changes Nov 21, 2024
@cernicc cernicc added ready for review Review is needed and removed ready for review Review is needed labels Nov 21, 2024
@cernicc cernicc added ready for review Review is needed and removed ready for review Review is needed labels Nov 21, 2024
@cernicc cernicc requested a review from th7nder November 21, 2024 09:10
@jmg-duarte jmg-duarte force-pushed the chore/129/refactor-sectornumber-in-primitives branch from b15d8d9 to 6e19ff8 Compare November 21, 2024 09:38
@jmg-duarte jmg-duarte added ready for review Review is needed and removed ready for review Review is needed labels Nov 21, 2024
@jmg-duarte jmg-duarte enabled auto-merge (squash) November 21, 2024 09:40
@jmg-duarte jmg-duarte added ready for review Review is needed and removed ready for review Review is needed labels Nov 21, 2024
Copy link
Collaborator

@jmg-duarte jmg-duarte left a comment

Choose a reason for hiding this comment

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

🚀

@jmg-duarte jmg-duarte merged commit 9d524a8 into develop Nov 21, 2024
7 of 10 checks passed
@jmg-duarte jmg-duarte deleted the chore/129/refactor-sectornumber-in-primitives branch November 21, 2024 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review Review is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor SectorNumber in primitives
4 participants