-
Notifications
You must be signed in to change notification settings - Fork 13
Update Type Definitions to latest PolkadotJS #34
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
Changes from 16 commits
d3f2fc6
26fb7c6
6cbc5af
02f19e1
7e6e3f1
841a8a3
1f5d5b6
5d58622
c3cc791
022a859
ae81fb7
d4d67c9
6d45bf1
ff867b1
9441229
3de3101
c6bdd11
6dc9052
c288fd0
0b42103
d67d602
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -396,7 +396,7 @@ impl Decoder { | |
| RustTypeMarker::Array { size, ty } => { | ||
| log::trace!("Array::cursor={}", *cursor); | ||
| let mut decoded_arr = Vec::with_capacity(*size); | ||
| if *size == 0 as usize { | ||
| if *size == 0_usize { | ||
| log::trace!("Returning Empty Vector"); | ||
| return Ok(SubstrateType::Composite(Vec::new())); | ||
| } else { | ||
|
|
@@ -467,7 +467,7 @@ impl Decoder { | |
| self.decode_single(module, spec, v, data, cursor, true)? | ||
| } | ||
| }, | ||
| RustTypeMarker::Generic((outer, _)) => { | ||
| RustTypeMarker::Generic(outer, _) => { | ||
| log::trace!("Generic Type"); | ||
| // disregard 'inner' type of a generic | ||
| self.decode_single(module, spec, outer, data, cursor, is_compact)? | ||
|
|
@@ -732,34 +732,25 @@ impl Decoder { | |
| *cursor += 1; | ||
| Ok(Some(SubstrateType::GenericVote(vote))) | ||
| } | ||
| // Old Address Format for backwards-compatibility https://github.com/paritytech/substrate/pull/7380 | ||
| "Lookup" | "GenericAddress" | "GenericLookupSource" | "GenericAccountId" => { | ||
| // a specific type that is <T as StaticSource>::Lookup concatenated to just 'Lookup' | ||
| log::trace!("cursor={}, data length={}", cursor, data.len()); | ||
| let inc: usize; | ||
| // TODO: requires more investigation | ||
| // cursor increments for 0x00 .. 0xfe may be incorrect | ||
| match data[*cursor] { | ||
| 0x00..=0xef => { | ||
| inc = 0; | ||
| } | ||
| 0xfc => { | ||
| inc = 2; | ||
| } | ||
| 0xfd => { | ||
| inc = 4; | ||
| } | ||
| 0xfe => { | ||
| inc = 8; | ||
| } | ||
| 0xff => { | ||
| inc = 32; | ||
| } | ||
| _ => return Err(Error::Fail("Invalid Address".to_string())), | ||
| } | ||
|
|
||
| let val: substrate_types::Address = Decode::decode(&mut &data[*cursor..])?; | ||
| let val: substrate_types::Address = decode_old_address(data, cursor)?; | ||
|
|
||
| *cursor += inc + 1; // +1 for byte 0x00-0xff | ||
| Ok(Some(SubstrateType::Address(val))) | ||
| } | ||
| "GenericMultiAddress" => { | ||
| let val: substrate_types::Address = Decode::decode(&mut &data[*cursor..])?; | ||
| let cursor_offset = match &val { | ||
| substrate_types::Address::Id(_) => 32, | ||
| substrate_types::Address::Index(_) => 1, | ||
| substrate_types::Address::Raw(v) => v.len(), | ||
| substrate_types::Address::Address32(_) => 32, | ||
| substrate_types::Address::Address20(_) => 20, | ||
| }; | ||
| *cursor += cursor_offset; | ||
| Ok(Some(SubstrateType::Address(val))) | ||
| } | ||
| "Era" => { | ||
|
|
@@ -823,6 +814,49 @@ impl Decoder { | |
| } | ||
| } | ||
|
|
||
| /// Decodes old address pre-refactor (https://github.com/paritytech/substrate/pull/7380) | ||
| /// and converts it to a MultiAddress | ||
| fn decode_old_address(data: &[u8], cursor: &mut usize) -> Result<substrate_types::Address, Error> { | ||
|
Contributor
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. AFAIU there's nothing checks that It may panic then.
Contributor
Author
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. Yeah, This is part of the larger issue that the function
Contributor
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. Ok, cool |
||
| /// Kept around for backwards-compatibility with old address struct | ||
| fn need_more_than<T: PartialOrd>(a: T, b: T) -> Result<T, Error> { | ||
| if a < b { | ||
| Ok(b) | ||
| } else { | ||
| Err("Invalid range".into()) | ||
| } | ||
| } | ||
|
|
||
| let inc; | ||
| let addr = match data[*cursor] { | ||
| x @ 0x00..=0xef => { | ||
| inc = 0; | ||
| substrate_types::Address::Index(x as u32) | ||
| } | ||
| 0xfc => { | ||
| inc = 2; | ||
| substrate_types::Address::Index(need_more_than(0xef, u16::decode(&mut &data[(*cursor + 1)..])?)? as u32) | ||
| } | ||
| 0xfd => { | ||
| inc = 4; | ||
| substrate_types::Address::Index(need_more_than(0xffff, u32::decode(&mut &data[(*cursor + 1)..])?)?) | ||
| } | ||
| 0xfe => { | ||
| inc = 8; | ||
| substrate_types::Address::Index(need_more_than( | ||
| 0xffffffffu32, | ||
insipx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Decode::decode(&mut &data[(*cursor + 1)..])?, | ||
| )?) | ||
| } | ||
| 0xff => { | ||
| inc = 32; | ||
| substrate_types::Address::Id(Decode::decode(&mut &data[(*cursor + 1)..])?) | ||
| } | ||
| _ => return Err(Error::Fail("Invalid Address".to_string())), | ||
| }; | ||
| *cursor += inc + 1; // +1 for byte 0x00-0xff | ||
| Ok(addr) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ use onig::{Regex, Region, SearchOptions}; | |
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| enum RegexSet { | ||
| ArrayPrimitive, | ||
| BitSize, | ||
| ArrayStruct, | ||
| Vec, | ||
| Option, | ||
|
|
@@ -37,6 +38,8 @@ impl RegexSet { | |
| fn get_type(s: &str) -> Option<RegexSet> { | ||
| if rust_array_decl_prim().is_match(s) { | ||
| Some(RegexSet::ArrayPrimitive) | ||
| } else if rust_bit_size().is_match(s) { | ||
| Some(RegexSet::BitSize) | ||
| } else if rust_array_decl_struct().is_match(s) { | ||
| Some(RegexSet::ArrayStruct) | ||
| } else if rust_vec_decl().is_match(s) { | ||
|
|
@@ -61,6 +64,7 @@ impl RegexSet { | |
| fn parse_type(&self, s: &str) -> Option<RustTypeMarker> { | ||
| match self { | ||
| RegexSet::ArrayPrimitive => parse_primitive_array(s), | ||
| RegexSet::BitSize => parse_bit_size(s), | ||
| RegexSet::ArrayStruct => parse_struct_array(s), | ||
| RegexSet::Vec => parse_vec(s), | ||
| RegexSet::Option => parse_option(s), | ||
|
|
@@ -90,6 +94,10 @@ fn rust_array_decl_struct() -> Regex { | |
| Regex::new(r"^\[ *?([\w><]+) *?; *?(\d+) *?\]").expect("Primitive Regex expression invalid") | ||
| } | ||
|
|
||
| pub fn rust_bit_size() -> Regex { | ||
| Regex::new(r"^(Int|UInt)<([\w\d]+), ?([\w\d]+)>").expect("Regex expression should be infallible; qed") | ||
|
||
| } | ||
|
|
||
| /// Match a rust vector | ||
| /// allowed to be nested within, or have other (ie Option<>) nested within | ||
| pub fn rust_vec_decl() -> Regex { | ||
|
|
@@ -354,7 +362,40 @@ fn parse_generic(s: &str) -> Option<RustTypeMarker> { | |
| // account that a HeartBeat type in Polkadot is HeartBeat<T::BlockNumber> | ||
| let ty_inner = parse(ty_inner).expect("Must be a type; qed"); | ||
|
|
||
| Some(RustTypeMarker::Generic((Box::new(ty_outer), Box::new(ty_inner)))) | ||
| Some(RustTypeMarker::Generic(Box::new(ty_outer), Box::new(ty_inner))) | ||
| } | ||
|
|
||
| fn parse_bit_size(s: &str) -> Option<RustTypeMarker> { | ||
| let re = rust_bit_size(); | ||
| if !re.is_match(s) { | ||
| return None; | ||
| } | ||
|
|
||
| let ty = re.captures(s)?.at(1)?; | ||
| let size = re.captures(s)?.at(2)?; | ||
|
|
||
| match ty { | ||
| "UInt" => match size.parse::<usize>().expect("Should always be a number") { | ||
| 8 => Some(RustTypeMarker::U8), | ||
| 16 => Some(RustTypeMarker::U16), | ||
| 32 => Some(RustTypeMarker::U32), | ||
| 64 => Some(RustTypeMarker::U64), | ||
| 128 => Some(RustTypeMarker::U128), | ||
| s => Some(RustTypeMarker::Array { size: s, ty: Box::new(RustTypeMarker::U8) }), | ||
| }, | ||
| "Int" => match size.parse::<usize>().expect("Should always be number") { | ||
| 8 => Some(RustTypeMarker::I8), | ||
| 16 => Some(RustTypeMarker::I16), | ||
| 32 => Some(RustTypeMarker::I32), | ||
| 64 => Some(RustTypeMarker::I64), | ||
| 128 => Some(RustTypeMarker::I128), | ||
| s => Some(RustTypeMarker::Array { size: s, ty: Box::new(RustTypeMarker::U8) }), | ||
| }, | ||
| _ => { | ||
| log::warn!("Could not ascertain type of bit-size declaration"); | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// recursively parses a regex set | ||
|
|
@@ -674,7 +715,7 @@ mod tests { | |
| let re = rust_generic_decl(); | ||
| let caps = re.captures("GenericOuterType<GenericInnerType>").unwrap(); | ||
| assert_eq!( | ||
| vec![Some("GenericOuterType<GenericInnerType>"), Some("GenericOuterType"), Some("GenericInnerType")], | ||
| vec![Some("GenericOuterType<GenericInnerType>"), Some("GenericOuterType"), Some("GenericInnerType"),], | ||
| caps.iter().collect::<Vec<Option<&str>>>() | ||
| ); | ||
| } | ||
|
|
@@ -829,4 +870,13 @@ mod tests { | |
| let res = parse_vec(ty).unwrap(); | ||
| log::debug!("{:?}", res); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_parse_bit_size() { | ||
| pretty_env_logger::try_init(); | ||
| let ty = "UInt<128, Balance>"; | ||
| assert_eq!(parse_bit_size(ty).unwrap(), RustTypeMarker::U128); | ||
| let ty = "Int<64, Balance>"; | ||
| assert_eq!(parse_bit_size(ty).unwrap(), RustTypeMarker::I64); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.