Even more style
This commit is contained in:
parent
469b0a17dc
commit
9286a03656
@ -1,5 +1,3 @@
|
||||
//#![allow(dead_code)] //TODO: remove this after everything is done
|
||||
|
||||
use mio::*;
|
||||
use hash::*;
|
||||
use rlp::*;
|
||||
@ -129,12 +127,12 @@ impl Session {
|
||||
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));
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
@ -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::<String>().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::<String>().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::<String>().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']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||
where F: FnOnce(&[u8]) -> Result<T, DecoderError> {
|
||||
|
||||
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<Vec<Self>, DecoderError> {
|
||||
|
@ -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()) })
|
||||
},
|
||||
|
@ -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)
|
||||
|
26
src/uint.rs
26
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<U128> for U256 {
|
||||
|
||||
impl From<U256> for u64 {
|
||||
fn from(value: U256) -> u64 {
|
||||
value.low_u64()
|
||||
value.as_u64()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<U256> for u32 {
|
||||
fn from(value: U256) -> u32 {
|
||||
value.low_u32()
|
||||
value.as_u32()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user