From 9286a03656e7d65fde096e7b608fa1945d9edefd Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 8 Jan 2016 15:52:43 +0100 Subject: [PATCH] Even more style --- src/network/session.rs | 9 +++------ src/rlp/rlp.rs | 4 ++-- src/rlp/rlptraits.rs | 4 ++-- src/rlp/tests.rs | 12 ++++++------ src/rlp/untrusted_rlp.rs | 6 +++--- src/trie/node.rs | 4 ++-- src/trie/triedbmut.rs | 20 ++++++++++---------- src/uint.rs | 26 ++++++++++++++++++++++++-- 8 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/network/session.rs b/src/network/session.rs index a34f6ff1a..720902150 100644 --- a/src/network/session.rs +++ b/src/network/session.rs @@ -1,5 +1,3 @@ -//#![allow(dead_code)] //TODO: remove this after everything is done - use mio::*; use hash::*; use rlp::*; @@ -126,15 +124,15 @@ impl Session { } match packet_id { PACKET_HELLO => { - let rlp = UntrustedRlp::new(&packet.data[1..]); //TODO: validate rlp expected size + let rlp = UntrustedRlp::new(&packet.data[1..]); //TODO: validate rlp expected size try!(self.read_hello(&rlp, host)); Ok(SessionData::Ready) - } + }, PACKET_DISCONNECT => Err(Error::Disconnect(DisconnectReason::DisconnectRequested)), PACKET_PING => { try!(self.write_pong()); Ok(SessionData::None) - } + }, PACKET_GET_PEERS => Ok(SessionData::None), //TODO; PACKET_PEERS => Ok(SessionData::None), PACKET_USER ... PACKET_LAST => { @@ -226,7 +224,6 @@ impl Session { self.send(try!(Session::prepare(PACKET_PONG, 0))) } - fn disconnect(&mut self, reason: DisconnectReason) -> Error { let mut rlp = RlpStream::new(); rlp.append(&(PACKET_DISCONNECT as u32)); diff --git a/src/rlp/rlp.rs b/src/rlp/rlp.rs index ac830cc9c..2179643d1 100644 --- a/src/rlp/rlp.rs +++ b/src/rlp/rlp.rs @@ -29,8 +29,8 @@ impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view { } } - fn raw(&'view self) -> &'a [u8] { - self.rlp.raw() + fn as_raw(&'view self) -> &'a [u8] { + self.rlp.as_raw() } fn prototype(&self) -> Self::Prototype { diff --git a/src/rlp/rlptraits.rs b/src/rlp/rlptraits.rs index a38cf76e2..4ef8d2a53 100644 --- a/src/rlp/rlptraits.rs +++ b/src/rlp/rlptraits.rs @@ -32,11 +32,11 @@ pub trait View<'a, 'view>: Sized { /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; /// let rlp = Rlp::new(&data); - /// let dog = rlp.at(1).raw(); + /// let dog = rlp.at(1).as_raw(); /// assert_eq!(dog, &[0x83, b'd', b'o', b'g']); /// } /// ``` - fn raw(&'view self) -> &'a [u8]; + fn as_raw(&'view self) -> &'a [u8]; /// Get the prototype of the RLP. fn prototype(&self) -> Self::Prototype; diff --git a/src/rlp/tests.rs b/src/rlp/tests.rs index 49698dafb..7c2099124 100644 --- a/src/rlp/tests.rs +++ b/src/rlp/tests.rs @@ -19,19 +19,19 @@ fn rlp_at() { let cat = rlp.at(0).unwrap(); assert!(cat.is_data()); - assert_eq!(cat.raw(), &[0x83, b'c', b'a', b't']); + assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']); //assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_string()); assert_eq!(cat.as_val::().unwrap(), "cat".to_string()); let dog = rlp.at(1).unwrap(); assert!(dog.is_data()); - assert_eq!(dog.raw(), &[0x83, b'd', b'o', b'g']); + assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']); //assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_string()); assert_eq!(dog.as_val::().unwrap(), "dog".to_string()); let cat_again = rlp.at(0).unwrap(); assert!(cat_again.is_data()); - assert_eq!(cat_again.raw(), &[0x83, b'c', b'a', b't']); + assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']); //assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_string()); assert_eq!(cat_again.as_val::().unwrap(), "cat".to_string()); } @@ -61,18 +61,18 @@ fn rlp_iter() { let cat = iter.next().unwrap(); assert!(cat.is_data()); - assert_eq!(cat.raw(), &[0x83, b'c', b'a', b't']); + assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']); let dog = iter.next().unwrap(); assert!(dog.is_data()); - assert_eq!(dog.raw(), &[0x83, b'd', b'o', b'g']); + assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']); let none = iter.next(); assert!(none.is_none()); let cat_again = rlp.at(0).unwrap(); assert!(cat_again.is_data()); - assert_eq!(cat_again.raw(), &[0x83, b'c', b'a', b't']); + assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']); } } diff --git a/src/rlp/untrusted_rlp.rs b/src/rlp/untrusted_rlp.rs index 2afbbfa23..5a12cbc5e 100644 --- a/src/rlp/untrusted_rlp.rs +++ b/src/rlp/untrusted_rlp.rs @@ -79,7 +79,7 @@ impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view { } } - fn raw(&'view self) -> &'a [u8] { + fn as_raw(&'view self) -> &'a [u8] { self.bytes } @@ -294,7 +294,7 @@ impl<'a> Decoder for BasicDecoder<'a> { fn read_value(&self, f: F) -> Result where F: FnOnce(&[u8]) -> Result { - let bytes = self.rlp.raw(); + let bytes = self.rlp.as_raw(); match bytes.first().map(|&x| x) { // rlp is too short @@ -316,7 +316,7 @@ impl<'a> Decoder for BasicDecoder<'a> { } fn as_raw(&self) -> &[u8] { - self.rlp.raw() + self.rlp.as_raw() } fn as_list(&self) -> Result, DecoderError> { diff --git a/src/trie/node.rs b/src/trie/node.rs index 03f35a86d..b5745b66f 100644 --- a/src/trie/node.rs +++ b/src/trie/node.rs @@ -25,13 +25,13 @@ impl<'a> Node<'a> { // fed back into this function or inline RLP which can be fed back into this function). Prototype::List(2) => match NibbleSlice::from_encoded(r.at(0).data()) { (slice, true) => Node::Leaf(slice, r.at(1).data()), - (slice, false) => Node::Extension(slice, r.at(1).raw()), + (slice, false) => Node::Extension(slice, r.at(1).as_raw()), }, // branch - first 16 are nodes, 17th is a value (or empty). Prototype::List(17) => { let mut nodes: [&'a [u8]; 16] = unsafe { ::std::mem::uninitialized() }; for i in 0..16 { - nodes[i] = r.at(i).raw(); + nodes[i] = r.at(i).as_raw(); } Node::Branch(nodes, if r.at(16).is_empty() { None } else { Some(r.at(16).data()) }) }, diff --git a/src/trie/triedbmut.rs b/src/trie/triedbmut.rs index e04b7c758..783111f3a 100644 --- a/src/trie/triedbmut.rs +++ b/src/trie/triedbmut.rs @@ -309,22 +309,22 @@ impl<'db> TrieDBMut<'db> { /// removal instructions from the backing database. fn take_node<'a, 'rlp_view>(&'a self, rlp: &'rlp_view Rlp<'a>, journal: &mut Journal) -> &'a [u8] where 'a: 'rlp_view { if rlp.is_list() { - trace!("take_node {:?} (inline)", rlp.raw().pretty()); - rlp.raw() + trace!("take_node {:?} (inline)", rlp.as_raw().pretty()); + rlp.as_raw() } else if rlp.is_data() && rlp.size() == 32 { let h = rlp.as_val(); let r = self.db.lookup(&h).unwrap_or_else(||{ - println!("Node not found! rlp={:?}, node_hash={:?}", rlp.raw().pretty(), h); + println!("Node not found! rlp={:?}, node_hash={:?}", rlp.as_raw().pretty(), h); println!("Journal: {:?}", journal); panic!(); }); - trace!("take_node {:?} (indirect for {:?})", rlp.raw().pretty(), r); + trace!("take_node {:?} (indirect for {:?})", rlp.as_raw().pretty(), r); journal.delete_node_sha3(h); r } else { - trace!("take_node {:?} (???)", rlp.raw().pretty()); + trace!("take_node {:?} (???)", rlp.as_raw().pretty()); panic!("Empty or invalid node given?"); } } @@ -350,7 +350,7 @@ impl<'db> TrieDBMut<'db> { for i in 0..17 { match index == i { // not us - leave alone. - false => { s.append_raw(old_rlp.at(i).raw(), 1); }, + false => { s.append_raw(old_rlp.at(i).as_raw(), 1); }, // branch-leaf entry - just replace. true if i == 16 => { s.append(&value); }, // original had empty slot - place a leaf there. @@ -384,13 +384,13 @@ impl<'db> TrieDBMut<'db> { // not us - empty. _ if index != i => { s.append_empty_data(); }, // branch-value: just replace. - true if i == 16 => { s.append_raw(old_rlp.at(1).raw(), 1); }, + true if i == 16 => { s.append_raw(old_rlp.at(1).as_raw(), 1); }, // direct extension: just replace. - false if existing_key.len() == 1 => { s.append_raw(old_rlp.at(1).raw(), 1); }, + false if existing_key.len() == 1 => { s.append_raw(old_rlp.at(1).as_raw(), 1); }, // original has empty slot. true => journal.new_node(Self::compose_leaf(&existing_key.mid(1), old_rlp.at(1).data()), &mut s), // additional work required after branching. - false => journal.new_node(Self::compose_extension(&existing_key.mid(1), old_rlp.at(1).raw()), &mut s), + false => journal.new_node(Self::compose_extension(&existing_key.mid(1), old_rlp.at(1).as_raw()), &mut s), } }; self.augmented(&s.out(), partial, value, journal) @@ -422,7 +422,7 @@ impl<'db> TrieDBMut<'db> { trace!("partially-shared-prefix (exist={:?}; new={:?}; cp={:?}): AUGMENT-AT-END", existing_key.len(), partial.len(), cp); // low (farther from root) - let low = Self::compose_raw(&existing_key.mid(cp), old_rlp.at(1).raw(), is_leaf); + let low = Self::compose_raw(&existing_key.mid(cp), old_rlp.at(1).as_raw(), is_leaf); let augmented_low = self.augmented(&low, &partial.mid(cp), value, journal); // high (closer to root) diff --git a/src/uint.rs b/src/uint.rs index 6b2af4b3b..7fc11e2df 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -63,6 +63,28 @@ macro_rules! construct_uint { let &$name(ref arr) = self; arr[0] } + + /// Conversion to u32 with overflow checking + #[inline] + pub fn as_u32(&self) -> u32 { + let &$name(ref arr) = self; + if (arr[0] & (0xffffffffu64 << 32)) != 0 { + panic!("Intger overflow when casting U256") + } + self.as_u64() as u32 + } + + /// Conversion to u64 with overflow checking + #[inline] + pub fn as_u64(&self) -> u64 { + let &$name(ref arr) = self; + for i in 1..$n_words { + if arr[i] != 0 { + panic!("Intger overflow when casting U256") + } + } + arr[0] + } /// Return the least number of bits needed to represent the number #[inline] pub fn bits(&self) -> usize { @@ -442,13 +464,13 @@ impl From for U256 { impl From for u64 { fn from(value: U256) -> u64 { - value.low_u64() + value.as_u64() } } impl From for u32 { fn from(value: U256) -> u32 { - value.low_u32() + value.as_u32() } }