CHT calculations for full nodes

This commit is contained in:
Robert Habermeier 2017-01-16 17:10:30 +01:00
parent 31aae4ed78
commit b960152d5b
2 changed files with 44 additions and 3 deletions

View File

@ -112,7 +112,7 @@ impl OnDemand {
// dispatch the request, completing the request if no peers available. // dispatch the request, completing the request if no peers available.
fn dispatch_header_by_number(&self, ctx: &BasicContext, req: request::HeaderByNumber, sender: Sender<encoded::Header>) { fn dispatch_header_by_number(&self, ctx: &BasicContext, req: request::HeaderByNumber, sender: Sender<encoded::Header>) {
let num = req.num; let num = req.num;
let cht_num = ::client::cht::block_to_cht_number(req.num); let cht_num = ::cht::block_to_cht_number(req.num);
let les_req = LesRequest::HeaderProofs(les_request::HeaderProofs { let les_req = LesRequest::HeaderProofs(les_request::HeaderProofs {
requests: vec![les_request::HeaderProof { requests: vec![les_request::HeaderProof {
cht_number: cht_num, cht_number: cht_num,

View File

@ -226,8 +226,49 @@ impl<T: ProvingBlockChainClient + ?Sized> Provider for T {
self.code_by_hash(req.account_key, BlockId::Hash(req.block_hash)) self.code_by_hash(req.account_key, BlockId::Hash(req.block_hash))
} }
fn header_proof(&self, _req: request::HeaderProof) -> Option<(encoded::Header, Vec<Bytes>)> { fn header_proof(&self, req: request::HeaderProof) -> Option<(encoded::Header, Vec<Bytes>)> {
None use util::MemoryDB;
use util::trie::{Trie, TrieMut, TrieDB, TrieDBMut, Recorder};
if req.cht_number != ::cht::block_to_cht_number(req.block_number) {
debug!(target: "les_provider", "Requested CHT number mismatch with block number.");
return None;
}
let mut memdb = MemoryDB::new();
let mut root = H256::default();
let mut needed_hdr = None;
{
let mut t = TrieDBMut::new(&mut memdb, &mut root);
let start_num = ::cht::start_number(req.cht_number);
for i in (0..::cht::SIZE).map(|x| x + start_num) {
match self.block_header(BlockId::Number(i)) {
None => return None,
Some(hdr) => {
t.insert(
&*::rlp::encode(&i),
&*::rlp::encode(&hdr.hash()),
).expect("fresh in-memory database is infallible; qed");
if i == req.block_number { needed_hdr = Some(hdr) }
}
}
}
}
let needed_hdr = needed_hdr.expect("`needed_hdr` always set in loop, number checked before; qed");
let mut recorder = Recorder::new();
let t = TrieDB::new(&memdb, &root)
.expect("Same DB and root as just produced by TrieDBMut; qed");
if let Err(e) = t.get_with(&*::rlp::encode(&req.block_number), &mut recorder) {
debug!(target: "les_provider", "Error looking up number in freshly-created CHT: {}", e);
return None;
}
// TODO: cache calculated CHT if possible.
let proof = recorder.drain().into_iter().map(|x| x.data).collect();
Some((needed_hdr, proof))
} }
fn ready_transactions(&self) -> Vec<PendingTransaction> { fn ready_transactions(&self) -> Vec<PendingTransaction> {