cht proof checker, use it in on_demand
This commit is contained in:
parent
197695414e
commit
3a7248b964
@ -23,7 +23,7 @@
|
|||||||
use ethcore::ids::BlockId;
|
use ethcore::ids::BlockId;
|
||||||
use util::{Bytes, H256, U256, HashDB, MemoryDB};
|
use util::{Bytes, H256, U256, HashDB, MemoryDB};
|
||||||
use util::trie::{self, TrieMut, TrieDBMut, Trie, TrieDB, Recorder};
|
use util::trie::{self, TrieMut, TrieDBMut, Trie, TrieDB, Recorder};
|
||||||
use rlp::{Stream, RlpStream};
|
use rlp::{Stream, RlpStream, UntrustedRlp, View};
|
||||||
|
|
||||||
// encode a key.
|
// encode a key.
|
||||||
macro_rules! key {
|
macro_rules! key {
|
||||||
@ -136,6 +136,30 @@ pub fn compute_root<I>(cht_num: u64, iterable: I) -> Option<H256>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check a proof for a CHT.
|
||||||
|
/// Given a set of a trie nodes, a number to query, and a trie root,
|
||||||
|
/// verify the given trie branch and extract the canonical hash and total difficulty.
|
||||||
|
// TODO: better support for partially-checked queries.
|
||||||
|
pub fn check_proof(proof: &[Bytes], num: u64, root: H256) -> Option<(H256, U256)> {
|
||||||
|
let mut db = MemoryDB::new();
|
||||||
|
|
||||||
|
for node in proof { db.insert(&node[..]); }
|
||||||
|
let res = match TrieDB::new(&db, &root) {
|
||||||
|
Err(_) => return None,
|
||||||
|
Ok(trie) => trie.get_with(&key!(num), |val: &[u8]| {
|
||||||
|
let rlp = UntrustedRlp::new(val);
|
||||||
|
rlp.val_at::<H256>(0)
|
||||||
|
.and_then(|h| rlp.val_at::<U256>(1).map(|td| (h, td)))
|
||||||
|
.ok()
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
match res {
|
||||||
|
Ok(Some(Some((hash, td)))) => Some((hash, td)),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Convert a block number to a CHT number.
|
/// Convert a block number to a CHT number.
|
||||||
/// Returns `None` for `block_num` == 0, `Some` otherwise.
|
/// Returns `None` for `block_num` == 0, `Some` otherwise.
|
||||||
pub fn block_to_cht_number(block_num: u64) -> Option<u64> {
|
pub fn block_to_cht_number(block_num: u64) -> Option<u64> {
|
||||||
|
@ -29,7 +29,7 @@ use futures::sync::oneshot;
|
|||||||
use network::PeerId;
|
use network::PeerId;
|
||||||
|
|
||||||
use net::{Handler, Status, Capabilities, Announcement, EventContext, BasicContext, ReqId};
|
use net::{Handler, Status, Capabilities, Announcement, EventContext, BasicContext, ReqId};
|
||||||
use util::{Bytes, RwLock};
|
use util::{Bytes, RwLock, U256};
|
||||||
use types::les_request::{self as les_request, Request as LesRequest};
|
use types::les_request::{self as les_request, Request as LesRequest};
|
||||||
|
|
||||||
pub mod request;
|
pub mod request;
|
||||||
@ -79,7 +79,7 @@ struct Peer {
|
|||||||
|
|
||||||
// Attempted request info and sender to put received value.
|
// Attempted request info and sender to put received value.
|
||||||
enum Pending {
|
enum Pending {
|
||||||
HeaderByNumber(request::HeaderByNumber, Sender<encoded::Header>), // num + CHT root
|
HeaderByNumber(request::HeaderByNumber, Sender<(encoded::Header, U256)>), // num + CHT root
|
||||||
HeaderByHash(request::HeaderByHash, Sender<encoded::Header>),
|
HeaderByHash(request::HeaderByHash, Sender<encoded::Header>),
|
||||||
Block(request::Body, Sender<encoded::Block>),
|
Block(request::Body, Sender<encoded::Block>),
|
||||||
BlockReceipts(request::BlockReceipts, Sender<Vec<Receipt>>),
|
BlockReceipts(request::BlockReceipts, Sender<Vec<Receipt>>),
|
||||||
@ -105,14 +105,15 @@ impl Default for OnDemand {
|
|||||||
|
|
||||||
impl OnDemand {
|
impl OnDemand {
|
||||||
/// Request a header by block number and CHT root hash.
|
/// Request a header by block number and CHT root hash.
|
||||||
pub fn header_by_number(&self, ctx: &BasicContext, req: request::HeaderByNumber) -> Response<encoded::Header> {
|
/// Returns the header and the total difficulty.
|
||||||
|
pub fn header_by_number(&self, ctx: &BasicContext, req: request::HeaderByNumber) -> Response<(encoded::Header, U256)> {
|
||||||
let (sender, receiver) = oneshot::channel();
|
let (sender, receiver) = oneshot::channel();
|
||||||
self.dispatch_header_by_number(ctx, req, sender);
|
self.dispatch_header_by_number(ctx, req, sender);
|
||||||
Response(receiver)
|
Response(receiver)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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, U256)>) {
|
||||||
let num = req.num;
|
let num = req.num;
|
||||||
let cht_num = match ::cht::block_to_cht_number(req.num) {
|
let cht_num = match ::cht::block_to_cht_number(req.num) {
|
||||||
Some(cht_num) => cht_num,
|
Some(cht_num) => cht_num,
|
||||||
|
@ -21,7 +21,7 @@ use ethcore::encoded;
|
|||||||
use ethcore::receipt::Receipt;
|
use ethcore::receipt::Receipt;
|
||||||
|
|
||||||
use rlp::{RlpStream, Stream, UntrustedRlp, View};
|
use rlp::{RlpStream, Stream, UntrustedRlp, View};
|
||||||
use util::{Address, Bytes, HashDB, H256};
|
use util::{Address, Bytes, HashDB, H256, U256};
|
||||||
use util::memorydb::MemoryDB;
|
use util::memorydb::MemoryDB;
|
||||||
use util::sha3::Hashable;
|
use util::sha3::Hashable;
|
||||||
use util::trie::{Trie, TrieDB, TrieError};
|
use util::trie::{Trie, TrieDB, TrieError};
|
||||||
@ -66,24 +66,16 @@ pub struct HeaderByNumber {
|
|||||||
|
|
||||||
impl HeaderByNumber {
|
impl HeaderByNumber {
|
||||||
/// Check a response with a header and cht proof.
|
/// Check a response with a header and cht proof.
|
||||||
pub fn check_response(&self, header: &[u8], proof: &[Bytes]) -> Result<encoded::Header, Error> {
|
pub fn check_response(&self, header: &[u8], proof: &[Bytes]) -> Result<(encoded::Header, U256), Error> {
|
||||||
use util::trie::{Trie, TrieDB};
|
let (expected_hash, td) = match ::cht::check_proof(proof, self.num, self.cht_root) {
|
||||||
|
Some((expected_hash, td)) => (expected_hash, td),
|
||||||
// check the proof
|
None => return Err(Error::BadProof),
|
||||||
let mut db = MemoryDB::new();
|
|
||||||
|
|
||||||
for node in proof { db.insert(&node[..]); }
|
|
||||||
let key = ::rlp::encode(&self.num);
|
|
||||||
|
|
||||||
let expected_hash: H256 = match TrieDB::new(&db, &self.cht_root).and_then(|t| t.get(&*key))? {
|
|
||||||
Some(val) => ::rlp::decode(&val),
|
|
||||||
None => return Err(Error::BadProof)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// and compare the hash to the found header.
|
// and compare the hash to the found header.
|
||||||
let found_hash = header.sha3();
|
let found_hash = header.sha3();
|
||||||
match expected_hash == found_hash {
|
match expected_hash == found_hash {
|
||||||
true => Ok(encoded::Header::new(header.to_vec())),
|
true => Ok((encoded::Header::new(header.to_vec()), td)),
|
||||||
false => Err(Error::WrongHash(expected_hash, found_hash)),
|
false => Err(Error::WrongHash(expected_hash, found_hash)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -191,51 +183,44 @@ impl Account {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use util::{MemoryDB, Address, H256, FixedHash};
|
use util::{MemoryDB, Address, H256, FixedHash};
|
||||||
use util::trie::{Trie, TrieMut, TrieDB, SecTrieDB, TrieDBMut, SecTrieDBMut};
|
use util::trie::{Trie, TrieMut, SecTrieDB, SecTrieDBMut};
|
||||||
use util::trie::recorder::Recorder;
|
use util::trie::recorder::Recorder;
|
||||||
|
|
||||||
|
use ethcore::client::{BlockChainClient, TestBlockChainClient, EachBlockWith};
|
||||||
use ethcore::header::Header;
|
use ethcore::header::Header;
|
||||||
use ethcore::encoded;
|
use ethcore::encoded;
|
||||||
use ethcore::receipt::Receipt;
|
use ethcore::receipt::Receipt;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_header_by_number() {
|
fn check_header_by_number() {
|
||||||
let mut root = H256::default();
|
use ::cht;
|
||||||
let mut db = MemoryDB::new();
|
|
||||||
let mut header = Header::new();
|
|
||||||
header.set_number(10_000);
|
|
||||||
header.set_extra_data(b"test_header".to_vec());
|
|
||||||
|
|
||||||
{
|
let test_client = TestBlockChainClient::new();
|
||||||
let mut trie = TrieDBMut::new(&mut db, &mut root);
|
test_client.add_blocks(10500, EachBlockWith::Nothing);
|
||||||
for i in (0..2048u64).map(|x| x + 8192) {
|
|
||||||
let hash = if i == 10_000 {
|
|
||||||
header.hash()
|
|
||||||
} else {
|
|
||||||
H256::random()
|
|
||||||
};
|
|
||||||
trie.insert(&*::rlp::encode(&i), &*::rlp::encode(&hash)).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let proof = {
|
let cht = {
|
||||||
let trie = TrieDB::new(&db, &root).unwrap();
|
let fetcher = |id| {
|
||||||
let key = ::rlp::encode(&10_000u64);
|
let hdr = test_client.block_header(id).unwrap();
|
||||||
let mut recorder = Recorder::new();
|
let td = test_client.block_total_difficulty(id).unwrap();
|
||||||
|
Some(cht::BlockInfo {
|
||||||
trie.get_with(&*key, &mut recorder).unwrap().unwrap();
|
hash: hdr.hash(),
|
||||||
|
parent_hash: hdr.parent_hash(),
|
||||||
recorder.drain().into_iter().map(|r| r.data).collect::<Vec<_>>()
|
total_difficulty: td,
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
cht::build(cht::block_to_cht_number(10_000).unwrap(), fetcher).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
let proof = cht.prove(10_000, 0).unwrap().unwrap();
|
||||||
let req = HeaderByNumber {
|
let req = HeaderByNumber {
|
||||||
num: 10_000,
|
num: 10_000,
|
||||||
cht_root: root,
|
cht_root: cht.root(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let raw_header = ::rlp::encode(&header);
|
let raw_header = test_client.block_header(::ethcore::ids::BlockId::Number(10_000)).unwrap();
|
||||||
|
|
||||||
assert!(req.check_response(&*raw_header, &proof[..]).is_ok());
|
assert!(req.check_response(&raw_header.into_inner(), &proof[..]).is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user