refactor: decode Cycles from little-endian bytes via TryFrom - #11102
Merged
Conversation
`Cycles` was decoded from its little-endian byte representation through an infallible `From<&Vec<u8>>` impl, which left it with no way to report a length mismatch to its caller. Replace it with `TryFrom<&[u8]>`. The two protobuf conversions built on it, `state.queues.v1.Cycles` and `canister_state_bits.v1.CyclesAccount`, become `TryFrom` impls returning `ProxyDecodeError::ValueOutOfRange`, so a length mismatch is now reported like any other proto decoding error. This matches how `NominalCycles` and `CompoundCycles` already decode in the same crate. The `try_from_option_field` call sites (`Request::cycles_payment`, `Response::cycles_refund`, `CallContext::available_cycles`, `CanisterStateBits::cycles_balance`, `Refund::amount`) need no change: they already required a `TryFrom` whose error converts into `ProxyDecodeError` and were satisfied through the blanket impl. The callers that used the infallible impls directly now propagate the error instead: `CompoundCycles::real`, `Callback::cycles_sent`, the three `RefundStatus` fields, and four `CanisterStateBits` fields in `state_layout`. The encoding is unchanged, so this only affects the decoding failure path. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors Cycles decoding to report invalid byte lengths instead of panicking.
Changes:
- Adds fallible byte-slice and protobuf conversions.
- Propagates decode errors through state deserialization.
- Updates tests and direct decoding call sites.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
rs/types/types/src/methods.rs |
Propagates callback cycle decode errors. |
rs/types/types/src/canister_http.rs |
Validates refund cycle fields. |
rs/types/cycles/src/cycles.rs |
Implements fallible decoding and tests. |
rs/types/cycles/src/compound_cycles.rs |
Propagates real-cycle decode errors. |
rs/state_layout/src/state_layout/proto.rs |
Validates persisted cycle fields. |
rs/execution_environment/tests/hypervisor.rs |
Updates test decoding calls. |
rs/embedders/tests/system_api.rs |
Updates system API test decoding. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
✅ No security or compliance issues detected. Reviewed everything up to dbb9ec6. Security Overview
Detected Code Changes
|
alin-at-dfinity
approved these changes
Aug 11, 2026
Every caller of the little-endian decoding impl holds a `Vec<u8>`, so a `TryFrom<&[u8]>` impl forced all of them to spell out an `as_slice()`. Take a `&Vec<u8>` instead and drop the 13 `as_slice()` calls. A blanket `impl<T: AsRef<[u8]>> TryFrom<T> for Cycles`, which would have covered both, is not possible: it conflicts with the `impl<T, U: Into<T>> TryFrom<U> for T` in `core` (E0119). The doc comment on the impl records this. The private `try_from_le_bytes` helper used by the two protobuf conversions now takes its `Vec<u8>` by value, which its callers already own; a `&Vec<u8>` parameter would trip `clippy::ptr_arg`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Cycleswas decoded from its little-endian byte representation through an infallibleFrom<&Vec<u8>>impl, which left it with no way to report a length mismatch to its caller. Replace it withTryFrom<&Vec<u8>>.The two protobuf conversions built on it,
state.queues.v1.Cyclesandcanister_state_bits.v1.CyclesAccount, becomeTryFromimpls returningProxyDecodeError::ValueOutOfRange, so a length mismatch is now reported like any other proto decoding error. This matches howNominalCyclesandCompoundCyclesalready decode in the same crate.The
try_from_option_fieldcall sites (Request::cycles_payment,Response::cycles_refund,CallContext::available_cycles,CanisterStateBits::cycles_balance,Refund::amount) need no change: they already required aTryFromwhose error converts intoProxyDecodeErrorand were satisfied through the blanket impl. The callers that used the infallible impls directly now propagate the error instead:CompoundCycles::real,Callback::cycles_sent, the threeRefundStatusfields, and fourCanisterStateBitsfields instate_layout.The encoding is unchanged, so this only affects the decoding failure path.