From 52abbc06435eec38578cf4bbf0742174c2aa2d36 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 4 Nov 2016 23:50:56 +0100 Subject: [PATCH] experiment with answering requests --- ethcore/light/src/net/mod.rs | 28 ++++++++++++++++++++-------- ethcore/light/src/provider.rs | 1 - 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/ethcore/light/src/net/mod.rs b/ethcore/light/src/net/mod.rs index ac0d1f98d..390c6a35b 100644 --- a/ethcore/light/src/net/mod.rs +++ b/ethcore/light/src/net/mod.rs @@ -29,7 +29,7 @@ use std::collections::{HashMap, HashSet}; use std::sync::atomic::{AtomicUsize, Ordering}; use provider::Provider; -use request::Request; +use request::{self, Request}; const TIMEOUT: TimerToken = 0; const TIMEOUT_INTERVAL_MS: u64 = 1000; @@ -206,16 +206,28 @@ impl LightProtocol { // Handle a request for block headers. fn get_block_headers(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) { - const MAX_HEADERS: usize = 512; + const MAX_HEADERS: u64 = 512; let req_id: u64 = try_dc!(io, *peer, data.val_at(0)); - let block: (u64, H256) = try_dc!(io, *peer, data.at(1).and_then(|block_list| { - Ok((try!(block_list.val_at(0)), try!(block_list.val_at(1)))) - })); - let max = ::std::cmp::min(MAX_HEADERS, try_dc!(io, *peer, data.val_at(2))); - let reverse: bool = try_dc!(io, *peer, data.val_at(3)); + let req = request::Headers { + block: try_dc!(io, *peer, data.at(1).and_then(|block_list| { + Ok((try!(block_list.val_at(0)), try!(block_list.val_at(1)))) + })), + max: ::std::cmp::min(MAX_HEADERS, try_dc!(io, *peer, data.val_at(2))), + skip: try_dc!(io, *peer, data.val_at(3)), + reverse: try_dc!(io, *peer, data.val_at(4)), + }; - unimplemented!() + let res = self.provider.block_headers(req); + + let mut res_stream = RlpStream::new_list(2 + res.len()); + res_stream.append(&req_id); + res_stream.append(&0u64); // TODO: Buffer Flow. + for raw_header in res { + res_stream.append_raw(&raw_header, 1); + } + + try_dc!(io, *peer, io.respond(packet::BLOCK_HEADERS, res_stream.out())) } // Receive a response for block headers. diff --git a/ethcore/light/src/provider.rs b/ethcore/light/src/provider.rs index de0fe3e2e..6d4fb2ea0 100644 --- a/ethcore/light/src/provider.rs +++ b/ethcore/light/src/provider.rs @@ -20,7 +20,6 @@ use ethcore::transaction::SignedTransaction; use ethcore::blockchain_info::BlockChainInfo; use util::Bytes; -use util::hash::H256; use request;