Replace legacy Rlp with UntrustedRlp and use in ethcore rlp views (#8316)
* WIP * Replace Rlp with UntrustedRlp in views, explicity unwrap with expect First pass to get it to compile. Need to figure out whether to do this or to propogate Errors upstream, which would require many more changes to dependent code. If we do this way we are assuming that the views are always used in a context where the rlp is trusted to be valid e.g. when reading from our own DB. So need to fid out whether views are used with data received from an untrusted (e.g. extrernal peer). * Remove original Rlp impl, rename UntrustedRlp -> Rlp * Create rlp views with view! macro to record debug info Views are assumed to be over valid rlp, so if there is a decoding error we record where the view was created in the first place and report it in the expect * Use $crate in view! macro to avoid import, fix tests * Expect valid rlp in decode functions for now * Replace spaces with tabs in new file * Add doc tests for creating views with macro * Update rlp docs to reflect removing of UntrustedRlp * Replace UntrustedRlp usages in private-tx merge
This commit is contained in:
committed by
Afri Schoedon
parent
db7a8c4ac7
commit
a04c5b180a
@@ -20,7 +20,7 @@ use std::fmt;
|
||||
use std::time::{self, SystemTime, Duration, Instant};
|
||||
|
||||
use ethereum_types::{H256, H512};
|
||||
use rlp::{self, DecoderError, RlpStream, UntrustedRlp};
|
||||
use rlp::{self, DecoderError, RlpStream, Rlp};
|
||||
use smallvec::SmallVec;
|
||||
use tiny_keccak::{keccak256, Keccak};
|
||||
|
||||
@@ -85,7 +85,7 @@ impl rlp::Encodable for Topic {
|
||||
}
|
||||
|
||||
impl rlp::Decodable for Topic {
|
||||
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
||||
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
|
||||
use std::cmp;
|
||||
|
||||
rlp.decoder().decode_value(|bytes| match bytes.len().cmp(&4) {
|
||||
@@ -145,7 +145,7 @@ fn append_topics<'a>(s: &'a mut RlpStream, topics: &[Topic]) -> &'a mut RlpStrea
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_topics(rlp: UntrustedRlp) -> Result<SmallVec<[Topic; 4]>, DecoderError> {
|
||||
fn decode_topics(rlp: Rlp) -> Result<SmallVec<[Topic; 4]>, DecoderError> {
|
||||
if rlp.is_list() {
|
||||
rlp.iter().map(|r| r.as_val::<Topic>()).collect()
|
||||
} else {
|
||||
@@ -212,7 +212,7 @@ impl rlp::Encodable for Envelope {
|
||||
}
|
||||
|
||||
impl rlp::Decodable for Envelope {
|
||||
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
||||
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
|
||||
if rlp.item_count()? != 5 { return Err(DecoderError::RlpIncorrectListLen) }
|
||||
|
||||
Ok(Envelope {
|
||||
@@ -332,7 +332,7 @@ impl Message {
|
||||
}
|
||||
|
||||
/// Decode message from RLP and check for validity against system time.
|
||||
pub fn decode(rlp: UntrustedRlp, now: SystemTime) -> Result<Self, Error> {
|
||||
pub fn decode(rlp: Rlp, now: SystemTime) -> Result<Self, Error> {
|
||||
let envelope: Envelope = rlp.as_val()?;
|
||||
let encoded_size = rlp.as_raw().len();
|
||||
let hash = H256(keccak256(rlp.as_raw()));
|
||||
@@ -418,7 +418,7 @@ impl Message {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::time::{self, Duration, SystemTime};
|
||||
use rlp::UntrustedRlp;
|
||||
use rlp::Rlp;
|
||||
use smallvec::SmallVec;
|
||||
|
||||
fn unix_time(x: u64) -> SystemTime {
|
||||
@@ -481,7 +481,7 @@ mod tests {
|
||||
|
||||
for i in 0..30 {
|
||||
let now = unix_time(100_000 - i);
|
||||
Message::decode(UntrustedRlp::new(&*encoded), now).unwrap();
|
||||
Message::decode(Rlp::new(&*encoded), now).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ mod tests {
|
||||
let encoded = ::rlp::encode(&envelope);
|
||||
|
||||
let now = unix_time(100_000 - 1_000);
|
||||
Message::decode(UntrustedRlp::new(&*encoded), now).unwrap();
|
||||
Message::decode(Rlp::new(&*encoded), now).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -516,6 +516,6 @@ mod tests {
|
||||
let encoded = ::rlp::encode(&envelope);
|
||||
|
||||
let now = unix_time(95_000);
|
||||
Message::decode(UntrustedRlp::new(&*encoded), now).unwrap();
|
||||
Message::decode(Rlp::new(&*encoded), now).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ use ethereum_types::{H256, H512};
|
||||
use network::{self, HostInfo, NetworkContext, NodeId, PeerId, ProtocolId, TimerToken};
|
||||
use ordered_float::OrderedFloat;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use rlp::{DecoderError, RlpStream, UntrustedRlp};
|
||||
use rlp::{DecoderError, RlpStream, Rlp};
|
||||
|
||||
use message::{Message, Error as MessageError};
|
||||
|
||||
@@ -506,7 +506,7 @@ impl<T: MessageHandler> Network<T> {
|
||||
}
|
||||
|
||||
// handle status packet from peer.
|
||||
fn on_status(&self, peer: &PeerId, _status: UntrustedRlp)
|
||||
fn on_status(&self, peer: &PeerId, _status: Rlp)
|
||||
-> Result<(), Error>
|
||||
{
|
||||
let peers = self.peers.read();
|
||||
@@ -523,7 +523,7 @@ impl<T: MessageHandler> Network<T> {
|
||||
}
|
||||
}
|
||||
|
||||
fn on_messages(&self, peer: &PeerId, message_packet: UntrustedRlp)
|
||||
fn on_messages(&self, peer: &PeerId, message_packet: Rlp)
|
||||
-> Result<(), Error>
|
||||
{
|
||||
let mut messages_vec = {
|
||||
@@ -568,7 +568,7 @@ impl<T: MessageHandler> Network<T> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_pow_requirement(&self, peer: &PeerId, requirement: UntrustedRlp)
|
||||
fn on_pow_requirement(&self, peer: &PeerId, requirement: Rlp)
|
||||
-> Result<(), Error>
|
||||
{
|
||||
use byteorder::{ByteOrder, BigEndian};
|
||||
@@ -604,7 +604,7 @@ impl<T: MessageHandler> Network<T> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_topic_filter(&self, peer: &PeerId, filter: UntrustedRlp)
|
||||
fn on_topic_filter(&self, peer: &PeerId, filter: Rlp)
|
||||
-> Result<(), Error>
|
||||
{
|
||||
let peers = self.peers.read();
|
||||
@@ -661,7 +661,7 @@ impl<T: MessageHandler> Network<T> {
|
||||
}
|
||||
|
||||
fn on_packet<C: ?Sized + Context>(&self, io: &C, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
let rlp = UntrustedRlp::new(data);
|
||||
let rlp = Rlp::new(data);
|
||||
let res = match packet_id {
|
||||
packet::STATUS => self.on_status(peer, rlp),
|
||||
packet::MESSAGES => self.on_messages(peer, rlp),
|
||||
|
||||
Reference in New Issue
Block a user