Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Light server improvements and protocol adjustments #3801

Merged
merged 7 commits into from
Dec 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions ethcore/light/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Light client implementation. Used for raw data queries as well as the header
//! sync.
//! Light client implementation. Stores data from light sync

use std::sync::Arc;

Expand All @@ -29,17 +28,18 @@ use ethcore::transaction::SignedTransaction;
use ethcore::blockchain_info::BlockChainInfo;

use io::IoChannel;
use util::hash::H256;
use util::hash::{H256, H256FastMap};
use util::{Bytes, Mutex};

use provider::Provider;
use request;

/// Light client implementation.
pub struct Client {
engine: Arc<Engine>,
_engine: Arc<Engine>,
header_queue: HeaderQueue,
message_channel: Mutex<IoChannel<ClientIoMessage>>,
_message_channel: Mutex<IoChannel<ClientIoMessage>>,
tx_pool: Mutex<H256FastMap<SignedTransaction>>,
}

impl Client {
Expand All @@ -55,12 +55,17 @@ impl Client {
false
}

/// Import a local transaction.
pub fn import_own_transaction(&self, tx: SignedTransaction) {
self.tx_pool.lock().insert(tx.hash(), tx);
}

/// Fetch a vector of all pending transactions.
pub fn pending_transactions(&self) -> Vec<SignedTransaction> {
vec![]
self.tx_pool.lock().values().cloned().collect()
}

/// Inquire about the status of a given block.
/// Inquire about the status of a given block (or header).
pub fn status(&self, _id: BlockId) -> BlockStatus {
BlockStatus::Unknown
}
Expand Down
3 changes: 1 addition & 2 deletions ethcore/light/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
//! It starts by performing a header-only sync, verifying random samples
//! of members of the chain to varying degrees.

// TODO: remove when integrating with the rest of parity.
#![allow(dead_code)]
#![deny(missing_docs)]

pub mod client;
pub mod net;
Expand Down
136 changes: 68 additions & 68 deletions ethcore/light/src/net/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,95 +26,95 @@ use request::Request;
/// disconnecting peers. This is used as a generalization of the portions
/// of a p2p network which the light protocol structure makes use of.
pub trait IoContext {
/// Send a packet to a specific peer.
fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>);
/// Send a packet to a specific peer.
fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>);

/// Respond to a peer's message. Only works if this context is a byproduct
/// of a packet handler.
fn respond(&self, packet_id: u8, packet_body: Vec<u8>);
/// Respond to a peer's message. Only works if this context is a byproduct
/// of a packet handler.
fn respond(&self, packet_id: u8, packet_body: Vec<u8>);

/// Disconnect a peer.
fn disconnect_peer(&self, peer: PeerId);
/// Disconnect a peer.
fn disconnect_peer(&self, peer: PeerId);

/// Disable a peer -- this is a disconnect + a time-out.
fn disable_peer(&self, peer: PeerId);
/// Disable a peer -- this is a disconnect + a time-out.
fn disable_peer(&self, peer: PeerId);

/// Get a peer's protocol version.
fn protocol_version(&self, peer: PeerId) -> Option<u8>;
/// Get a peer's protocol version.
fn protocol_version(&self, peer: PeerId) -> Option<u8>;
}

impl<'a> IoContext for NetworkContext<'a> {
fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>) {
if let Err(e) = self.send(peer, packet_id, packet_body) {
debug!(target: "les", "Error sending packet to peer {}: {}", peer, e);
}
}

fn respond(&self, packet_id: u8, packet_body: Vec<u8>) {
if let Err(e) = self.respond(packet_id, packet_body) {
debug!(target: "les", "Error responding to peer message: {}", e);
}
}

fn disconnect_peer(&self, peer: PeerId) {
NetworkContext::disconnect_peer(self, peer);
}

fn disable_peer(&self, peer: PeerId) {
NetworkContext::disable_peer(self, peer);
}

fn protocol_version(&self, peer: PeerId) -> Option<u8> {
self.protocol_version(self.subprotocol_name(), peer)
}
fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>) {
if let Err(e) = self.send(peer, packet_id, packet_body) {
debug!(target: "les", "Error sending packet to peer {}: {}", peer, e);
}
}

fn respond(&self, packet_id: u8, packet_body: Vec<u8>) {
if let Err(e) = self.respond(packet_id, packet_body) {
debug!(target: "les", "Error responding to peer message: {}", e);
}
}

fn disconnect_peer(&self, peer: PeerId) {
NetworkContext::disconnect_peer(self, peer);
}

fn disable_peer(&self, peer: PeerId) {
NetworkContext::disable_peer(self, peer);
}

fn protocol_version(&self, peer: PeerId) -> Option<u8> {
self.protocol_version(self.subprotocol_name(), peer)
}
}

/// Context for a protocol event.
pub trait EventContext {
/// Get the peer relevant to the event e.g. message sender,
/// disconnected/connected peer.
fn peer(&self) -> PeerId;
/// Get the peer relevant to the event e.g. message sender,
/// disconnected/connected peer.
fn peer(&self) -> PeerId;

/// Make a request from a peer.
fn request_from(&self, peer: PeerId, request: Request) -> Result<ReqId, Error>;
/// Make a request from a peer.
fn request_from(&self, peer: PeerId, request: Request) -> Result<ReqId, Error>;

/// Make an announcement of new capabilities to the rest of the peers.
// TODO: maybe just put this on a timer in LightProtocol?
fn make_announcement(&self, announcement: Announcement);
/// Make an announcement of new capabilities to the rest of the peers.
// TODO: maybe just put this on a timer in LightProtocol?
fn make_announcement(&self, announcement: Announcement);

/// Disconnect a peer.
fn disconnect_peer(&self, peer: PeerId);
/// Disconnect a peer.
fn disconnect_peer(&self, peer: PeerId);

/// Disable a peer.
fn disable_peer(&self, peer: PeerId);
/// Disable a peer.
fn disable_peer(&self, peer: PeerId);
}

/// Concrete implementation of `EventContext` over the light protocol struct and
/// an io context.
pub struct Ctx<'a> {
/// Io context to enable immediate response to events.
pub io: &'a IoContext,
/// Protocol implementation.
pub proto: &'a LightProtocol,
/// Relevant peer for event.
pub peer: PeerId,
/// Io context to enable immediate response to events.
pub io: &'a IoContext,
/// Protocol implementation.
pub proto: &'a LightProtocol,
/// Relevant peer for event.
pub peer: PeerId,
}

impl<'a> EventContext for Ctx<'a> {
fn peer(&self) -> PeerId { self.peer }
fn request_from(&self, peer: PeerId, request: Request) -> Result<ReqId, Error> {
self.proto.request_from(self.io, &peer, request)
}

fn make_announcement(&self, announcement: Announcement) {
self.proto.make_announcement(self.io, announcement);
}

fn disconnect_peer(&self, peer: PeerId) {
self.io.disconnect_peer(peer);
}

fn disable_peer(&self, peer: PeerId) {
self.io.disable_peer(peer);
}
fn peer(&self) -> PeerId { self.peer }
fn request_from(&self, peer: PeerId, request: Request) -> Result<ReqId, Error> {
self.proto.request_from(self.io, &peer, request)
}

fn make_announcement(&self, announcement: Announcement) {
self.proto.make_announcement(self.io, announcement);
}

fn disconnect_peer(&self, peer: PeerId) {
self.io.disconnect_peer(peer);
}

fn disable_peer(&self, peer: PeerId) {
self.io.disable_peer(peer);
}
}
Loading