This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Refactor module & runtime error handing #2880
Closed
Closed
Changes from 29 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
ba48a0a
srml-system checks
xlc 52c6b90
wip
xlc bd7228f
more modules compiles
xlc c0cece1
node-runtime checks
xlc a93fedd
build.sh passes
xlc 3039e7c
include dispatch error in failed event
xlc c5c2e5a
revert some unnecessary changes
xlc 1198fb7
refactor based on comments
xlc ed20244
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc 1f964d1
more compile error fixes
xlc 83975c0
avoid unnecessary into
xlc e03db0c
reorder code
xlc 7e01497
fixes some tests
xlc a6c989b
manually implement encode & decode to avoid i8 workaround
xlc 9d2dbbb
more test fixes
xlc 31c9c84
more fixes
xlc 6cad33a
more error fixes
xlc ba42e6a
Apply suggestions from code review
xlc 0f01cc4
address comments
xlc cce7c08
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc c7ae1b1
test for DispatchError encoding
xlc 0e424c1
tyep alias for democracy
xlc a145f27
make error printable
xlc ef4eecd
line width
xlc 33668d8
fix balances tests
xlc d128a1a
fix executive test
xlc a4a8cdc
fix system tests
xlc f4387fb
bump version
xlc ef2eaa6
ensure consistent method signature
xlc 31c998f
Apply suggestions from code review
xlc d6b94b0
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc 51ec628
changes based on review
xlc 88887ee
Add issue number for TODOs
xlc 550c1e0
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc e8324c2
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc c147a8e
fix
xlc 633a482
line width
xlc b61e137
fix test
xlc 1ca74ed
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc 3d2c850
Update core/sr-primitives/src/lib.rs
xlc 8330acf
Update core/sr-primitives/src/traits.rs
xlc d41955a
Update srml/council/src/motions.rs
xlc 66187b3
Update srml/council/src/motions.rs
xlc 7489d3a
update based on review
xlc 57f7958
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc 6e5c3ce
More concrete macro matching
xlc 2d9794a
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc 1f5faac
Merge remote-tracking branch 'upstream/master' into improve-error-res…
xlc efe953a
fix test build issue
xlc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,18 +48,41 @@ pub mod transaction_validity; | |
| /// Re-export these since they're only "kind of" generic. | ||
| pub use generic::{DigestItem, Digest}; | ||
|
|
||
| /// A message indicating an invalid signature in extrinsic. | ||
| pub const BAD_SIGNATURE: &str = "bad signature in extrinsic"; | ||
|
|
||
| /// Full block error message. | ||
| /// | ||
| /// This allows modules to indicate that given transaction is potentially valid | ||
| /// in the future, but can't be executed in the current state. | ||
| /// Note this error should be returned early in the execution to prevent DoS, | ||
| /// cause the fees are not being paid if this error is returned. | ||
| /// | ||
| /// Example: block gas limit is reached (the transaction can be retried in the next block though). | ||
| pub const BLOCK_FULL: &str = "block size limit is reached"; | ||
| #[cfg_attr(test, derive(PartialEq, Debug))] | ||
| /// Error type | ||
| pub enum Error { | ||
| /// Unknown error | ||
| /// This exists only to make implementation easier. Should be avoid as much as possible. | ||
| Unknown(&'static str), | ||
xlc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// Indicating an invalid signature in extrinsic. | ||
| BadSignature, | ||
| /// Full block error. | ||
| /// | ||
| /// This allows modules to indicate that given transaction is potentially valid | ||
| /// in the future, but can't be executed in the current state. | ||
| /// Note this error should be returned early in the execution to prevent DoS, | ||
| /// cause the fees are not being paid if this error is returned. | ||
| /// | ||
| /// Example: block gas limit is reached (the transaction can be retried in the next block though). | ||
| BlockFull | ||
xlc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Exists for for backward compatibility purpose. | ||
| impl Into<&'static str> for Error{ | ||
xlc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fn into(self) -> &'static str { | ||
| match self { | ||
| Error::Unknown(val) => val, | ||
| Error::BadSignature => "bad signature in extrinsic", | ||
| Error::BlockFull => "block size limit is reached", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<&'static str> for Error { | ||
| fn from(val: &'static str) -> Error { | ||
| Error::Unknown(val) | ||
| } | ||
| } | ||
|
|
||
| /// Justification type. | ||
| pub type Justification = Vec<u8>; | ||
|
|
@@ -486,22 +509,15 @@ impl From<ed25519::Signature> for AnySignature { | |
| } | ||
| } | ||
|
|
||
| #[derive(Eq, PartialEq, Clone, Copy, Decode)] | ||
| #[cfg_attr(feature = "std", derive(Debug, Serialize))] | ||
| #[repr(u8)] | ||
| /// Outcome of a valid extrinsic application. Capable of being sliced. | ||
| pub enum ApplyOutcome { | ||
| /// Successful application (extrinsic reported no issue). | ||
| Success = 0, | ||
| /// Failed application (extrinsic was probably a no-op other than fees). | ||
| Fail = 1, | ||
| } | ||
|
|
||
| impl codec::Encode for ApplyOutcome { | ||
| fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R { | ||
| f(&[*self as u8]) | ||
| } | ||
| } | ||
| #[derive(Eq, PartialEq, Clone, Copy, Encode, Decode)] | ||
| #[cfg_attr(feature = "std", derive(Debug, Serialize))] | ||
xlc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// Outcome of a valid extrinsic application. Capable of being sliced. | ||
| pub enum ApplyOutcome { | ||
| /// Successful application (extrinsic reported no issue). | ||
| Success, | ||
| /// Failed application (extrinsic was probably a no-op other than fees). | ||
| Fail(DispatchError), | ||
| } | ||
|
|
||
| #[derive(Eq, PartialEq, Clone, Copy, Decode)] | ||
| #[cfg_attr(feature = "std", derive(Debug, Serialize))] | ||
|
|
@@ -520,12 +536,40 @@ pub enum ApplyError { | |
| FullBlock = 255, | ||
| } | ||
|
|
||
| impl codec::Encode for ApplyError { | ||
| impl Encode for ApplyError { | ||
| fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R { | ||
| f(&[*self as u8]) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Eq, PartialEq, Clone, Copy)] | ||
| #[cfg_attr(feature = "std", derive(Debug, Serialize))] | ||
| /// Reason why a dispatch call failed | ||
| pub struct DispatchError { | ||
| /// Module index, matching the metadata module index | ||
| pub module: u8, | ||
| /// Module specific error value | ||
| pub error: u8, | ||
| /// Optional error message. | ||
| pub message: Option<&'static str>, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| impl Encode for DispatchError { | ||
xlc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R { | ||
| f(&[self.module, self.error]) | ||
| } | ||
| } | ||
|
|
||
| impl Decode for DispatchError { | ||
| fn decode<R: codec::Input>(input: &mut R) -> Option<Self> { | ||
| Some(DispatchError { | ||
| module: input.read_byte()?, | ||
| error: input.read_byte()?, | ||
| message: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Result from attempt to apply an extrinsic. | ||
| pub type ApplyResult = Result<ApplyOutcome, ApplyError>; | ||
|
|
||
|
|
@@ -644,6 +688,7 @@ impl traits::Extrinsic for OpaqueExtrinsic { | |
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::DispatchError; | ||
| use crate::codec::{Encode, Decode}; | ||
|
|
||
| macro_rules! per_thing_mul_upper_test { | ||
|
|
@@ -742,4 +787,21 @@ mod tests { | |
| ((Into::<U256>::into(std::u128::MAX) * 999_999u32) / 1_000_000u32).as_u128() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn dispatch_error_encoding() { | ||
| let error = DispatchError { | ||
| module: 1, | ||
| error: 2, | ||
| message: Some("error message"), | ||
| }; | ||
| let encoded = error.encode(); | ||
| let decoded = DispatchError::decode(&mut &*encoded).unwrap(); | ||
| assert_eq!(encoded, vec![1, 2]); | ||
| assert_eq!(decoded, DispatchError { | ||
| module: 1, | ||
| error: 2, | ||
| message: None, | ||
| }); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.