tests for request module
This commit is contained in:
parent
01977e60aa
commit
4e94f43644
@ -57,6 +57,7 @@ impl From<Box<TrieError>> for Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Request for a header by number.
|
/// Request for a header by number.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct HeaderByNumber {
|
pub struct HeaderByNumber {
|
||||||
/// The header's number.
|
/// The header's number.
|
||||||
pub num: u64,
|
pub num: u64,
|
||||||
@ -90,6 +91,7 @@ impl HeaderByNumber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Request for a header by hash.
|
/// Request for a header by hash.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct HeaderByHash(pub H256);
|
pub struct HeaderByHash(pub H256);
|
||||||
|
|
||||||
impl HeaderByHash {
|
impl HeaderByHash {
|
||||||
@ -104,6 +106,7 @@ impl HeaderByHash {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Request for a block, with header and precomputed hash.
|
/// Request for a block, with header and precomputed hash.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Body {
|
pub struct Body {
|
||||||
/// The block's header.
|
/// The block's header.
|
||||||
pub header: encoded::Header,
|
pub header: encoded::Header,
|
||||||
@ -137,6 +140,7 @@ impl Body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Request for a block's receipts with header for verification.
|
/// Request for a block's receipts with header for verification.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct BlockReceipts(pub encoded::Header);
|
pub struct BlockReceipts(pub encoded::Header);
|
||||||
|
|
||||||
impl BlockReceipts {
|
impl BlockReceipts {
|
||||||
@ -153,6 +157,7 @@ impl BlockReceipts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Request for an account structure.
|
/// Request for an account structure.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Account {
|
pub struct Account {
|
||||||
/// Header for verification.
|
/// Header for verification.
|
||||||
pub header: encoded::Header,
|
pub header: encoded::Header,
|
||||||
@ -182,3 +187,150 @@ impl Account {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use util::{MemoryDB, Address, H256, FixedHash};
|
||||||
|
use util::trie::{Trie, TrieMut, TrieDB, SecTrieDB, TrieDBMut, SecTrieDBMut};
|
||||||
|
use util::trie::recorder::{BasicRecorder, Recorder};
|
||||||
|
|
||||||
|
use ethcore::header::Header;
|
||||||
|
use ethcore::encoded;
|
||||||
|
use ethcore::receipt::Receipt;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_header_by_number() {
|
||||||
|
let mut root = H256::default();
|
||||||
|
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 mut trie = TrieDBMut::new(&mut db, &mut root);
|
||||||
|
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 trie = TrieDB::new(&db, &root).unwrap();
|
||||||
|
let key = ::rlp::encode(&10_000u64);
|
||||||
|
let mut recorder = BasicRecorder::new();
|
||||||
|
|
||||||
|
trie.get_recorded(&*key, &mut recorder).unwrap().unwrap();
|
||||||
|
|
||||||
|
recorder.drain().into_iter().map(|r| r.data).collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
|
||||||
|
let req = HeaderByNumber {
|
||||||
|
num: 10_000,
|
||||||
|
cht_root: root,
|
||||||
|
};
|
||||||
|
|
||||||
|
let raw_header = ::rlp::encode(&header);
|
||||||
|
|
||||||
|
assert!(req.check_response(&*raw_header, &proof[..]).is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_header_by_hash() {
|
||||||
|
let mut header = Header::new();
|
||||||
|
header.set_number(10_000);
|
||||||
|
header.set_extra_data(b"test_header".to_vec());
|
||||||
|
let hash = header.hash();
|
||||||
|
let raw_header = ::rlp::encode(&header);
|
||||||
|
|
||||||
|
assert!(HeaderByHash(hash).check_response(&*raw_header).is_ok())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_body() {
|
||||||
|
use rlp::{RlpStream, Stream};
|
||||||
|
|
||||||
|
let header = Header::new();
|
||||||
|
let mut body_stream = RlpStream::new_list(2);
|
||||||
|
body_stream.begin_list(0).begin_list(0);
|
||||||
|
|
||||||
|
let req = Body {
|
||||||
|
header: encoded::Header::new(::rlp::encode(&header).to_vec()),
|
||||||
|
hash: header.hash(),
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(req.check_response(&*body_stream.drain()).is_ok())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_receipts() {
|
||||||
|
let receipts = (0..5).map(|_| Receipt {
|
||||||
|
state_root: H256::random(),
|
||||||
|
gas_used: 21_000u64.into(),
|
||||||
|
log_bloom: Default::default(),
|
||||||
|
logs: Vec::new(),
|
||||||
|
}).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let mut header = Header::new();
|
||||||
|
let receipts_root = ::util::triehash::ordered_trie_root(
|
||||||
|
receipts.iter().map(|x| ::rlp::encode(x).to_vec())
|
||||||
|
);
|
||||||
|
|
||||||
|
header.set_receipts_root(receipts_root);
|
||||||
|
|
||||||
|
let req = BlockReceipts(encoded::Header::new(::rlp::encode(&header).to_vec()));
|
||||||
|
|
||||||
|
assert!(req.check_response(&receipts).is_ok())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check_state_proof() {
|
||||||
|
let mut root = H256::default();
|
||||||
|
let mut db = MemoryDB::new();
|
||||||
|
let mut header = Header::new();
|
||||||
|
header.set_number(123_456);
|
||||||
|
header.set_extra_data(b"test_header".to_vec());
|
||||||
|
|
||||||
|
let addr = Address::random();
|
||||||
|
let rand_acc = || {
|
||||||
|
let mut stream = RlpStream::new_list(4);
|
||||||
|
stream.append(&2u64)
|
||||||
|
.append(&100_000_000u64)
|
||||||
|
.append(&H256::random())
|
||||||
|
.append(&H256::random());
|
||||||
|
|
||||||
|
stream.out()
|
||||||
|
};
|
||||||
|
{
|
||||||
|
let mut trie = SecTrieDBMut::new(&mut db, &mut root);
|
||||||
|
for _ in 0..100 {
|
||||||
|
let address = Address::random();
|
||||||
|
trie.insert(&*address, &rand_acc()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
trie.insert(&*addr, &rand_acc()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let proof = {
|
||||||
|
let trie = SecTrieDB::new(&db, &root).unwrap();
|
||||||
|
let mut recorder = BasicRecorder::new();
|
||||||
|
|
||||||
|
trie.get_recorded(&*addr, &mut recorder).unwrap().unwrap();
|
||||||
|
|
||||||
|
recorder.drain().into_iter().map(|r| r.data).collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
|
||||||
|
header.set_state_root(root.clone());
|
||||||
|
|
||||||
|
let req = Account {
|
||||||
|
header: encoded::Header::new(::rlp::encode(&header).to_vec()),
|
||||||
|
address: addr,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(req.check_response(&proof[..]).is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user