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
@@ -25,7 +25,7 @@ use mio::deprecated::{Handler, EventLoop};
|
||||
use mio::udp::*;
|
||||
use hash::keccak;
|
||||
use ethereum_types::{H256, H520};
|
||||
use rlp::{UntrustedRlp, RlpStream, encode_list};
|
||||
use rlp::{Rlp, RlpStream, encode_list};
|
||||
use node_table::*;
|
||||
use network::{Error, ErrorKind};
|
||||
use io::{StreamToken, IoContext};
|
||||
@@ -259,7 +259,7 @@ impl Discovery {
|
||||
fn send_packet(&mut self, packet_id: u8, address: &SocketAddr, payload: &[u8]) -> Result<(), Error> {
|
||||
let mut rlp = RlpStream::new();
|
||||
rlp.append_raw(&[packet_id], 1);
|
||||
let source = UntrustedRlp::new(payload);
|
||||
let source = Rlp::new(payload);
|
||||
rlp.begin_list(source.item_count()? + 1);
|
||||
for i in 0 .. source.item_count()? {
|
||||
rlp.append_raw(source.at(i)?.as_raw(), 1);
|
||||
@@ -382,7 +382,7 @@ impl Discovery {
|
||||
let node_id = recover(&signature.into(), &keccak(signed))?;
|
||||
|
||||
let packet_id = signed[0];
|
||||
let rlp = UntrustedRlp::new(&signed[1..]);
|
||||
let rlp = Rlp::new(&signed[1..]);
|
||||
match packet_id {
|
||||
PACKET_PING => self.on_ping(&rlp, &node_id, &from, &hash_signed),
|
||||
PACKET_PONG => self.on_pong(&rlp, &node_id, &from),
|
||||
@@ -409,7 +409,7 @@ impl Discovery {
|
||||
entry.endpoint.is_allowed(&self.ip_filter) && entry.id != self.id
|
||||
}
|
||||
|
||||
fn on_ping(&mut self, rlp: &UntrustedRlp, node: &NodeId, from: &SocketAddr, echo_hash: &[u8]) -> Result<Option<TableUpdates>, Error> {
|
||||
fn on_ping(&mut self, rlp: &Rlp, node: &NodeId, from: &SocketAddr, echo_hash: &[u8]) -> Result<Option<TableUpdates>, Error> {
|
||||
trace!(target: "discovery", "Got Ping from {:?}", &from);
|
||||
let source = NodeEndpoint::from_rlp(&rlp.at(1)?)?;
|
||||
let dest = NodeEndpoint::from_rlp(&rlp.at(2)?)?;
|
||||
@@ -433,7 +433,7 @@ impl Discovery {
|
||||
Ok(Some(TableUpdates { added: added_map, removed: HashSet::new() }))
|
||||
}
|
||||
|
||||
fn on_pong(&mut self, rlp: &UntrustedRlp, node: &NodeId, from: &SocketAddr) -> Result<Option<TableUpdates>, Error> {
|
||||
fn on_pong(&mut self, rlp: &Rlp, node: &NodeId, from: &SocketAddr) -> Result<Option<TableUpdates>, Error> {
|
||||
trace!(target: "discovery", "Got Pong from {:?}", &from);
|
||||
// TODO: validate pong packet in rlp.val_at(1)
|
||||
let dest = NodeEndpoint::from_rlp(&rlp.at(0)?)?;
|
||||
@@ -448,7 +448,7 @@ impl Discovery {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn on_find_node(&mut self, rlp: &UntrustedRlp, _node: &NodeId, from: &SocketAddr) -> Result<Option<TableUpdates>, Error> {
|
||||
fn on_find_node(&mut self, rlp: &Rlp, _node: &NodeId, from: &SocketAddr) -> Result<Option<TableUpdates>, Error> {
|
||||
trace!(target: "discovery", "Got FindNode from {:?}", &from);
|
||||
let target: NodeId = rlp.val_at(0)?;
|
||||
let timestamp: u64 = rlp.val_at(1)?;
|
||||
@@ -481,7 +481,7 @@ impl Discovery {
|
||||
packets.collect()
|
||||
}
|
||||
|
||||
fn on_neighbours(&mut self, rlp: &UntrustedRlp, _node: &NodeId, from: &SocketAddr) -> Result<Option<TableUpdates>, Error> {
|
||||
fn on_neighbours(&mut self, rlp: &Rlp, _node: &NodeId, from: &SocketAddr) -> Result<Option<TableUpdates>, Error> {
|
||||
// TODO: validate packet
|
||||
let mut added = HashMap::new();
|
||||
trace!(target: "discovery", "Got {} Neighbours from {:?}", rlp.at(0)?.item_count()?, &from);
|
||||
@@ -725,7 +725,7 @@ mod tests {
|
||||
discovery2.on_packet(&ping_data.payload, ep1.address.clone()).ok();
|
||||
let pong_data = discovery2.send_queue.pop_front().unwrap();
|
||||
let data = &pong_data.payload[(32 + 65)..];
|
||||
let rlp = UntrustedRlp::new(&data[1..]);
|
||||
let rlp = Rlp::new(&data[1..]);
|
||||
assert_eq!(ping_data.payload[0..32], rlp.val_at::<Vec<u8>>(1).unwrap()[..])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user