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

PV63 receipts response #687

Merged
merged 11 commits into from
Mar 14, 2016
16 changes: 10 additions & 6 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ pub struct Client<V = CanonVerifier> where V: Verifier {
verifier: PhantomData<V>,
}

const HISTORY: u64 = 1000;
const CLIENT_DB_VER_STR: &'static str = "5.2";
const HISTORY: u64 = 1200;
const CLIENT_DB_VER_STR: &'static str = "5.3";

impl Client<CanonVerifier> {
/// Create a new client with given spec and DB path.
Expand Down Expand Up @@ -466,12 +466,16 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
}
}

fn state_data(&self, _hash: &H256) -> Option<Bytes> {
None
fn state_data(&self, hash: &H256) -> Option<Bytes> {
self.state_db.lock().unwrap().state(hash)
}

fn block_receipts(&self, _hash: &H256) -> Option<Bytes> {
None
fn block_receipts(&self, hash: &H256) -> Option<Bytes> {
self.chain.block_receipts(hash).and_then(|receipts| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.chain.block_receipts(hash).map(rlp::encode) is shorter

let mut rlp = RlpStream::new();
rlp.append(&receipts);
Some(rlp.out())
})
}

fn import_block(&self, bytes: Bytes) -> ImportResult {
Expand Down
5 changes: 3 additions & 2 deletions ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use header::{Header as BlockHeader, BlockNumber};
use filter::Filter;
use log_entry::LocalizedLogEntry;
use receipt::Receipt;
use extras::BlockReceipts;
use error::{ImportResult, Error};
use block_queue::BlockQueueInfo;
use block::ClosedBlock;
Expand Down Expand Up @@ -254,10 +255,10 @@ impl BlockChainClient for TestBlockChainClient {
fn block_receipts(&self, hash: &H256) -> Option<Bytes> {
// starts with 'f' ?
if *hash > H256::from("f000000000000000000000000000000000000000000000000000000000000000") {
let receipt = Receipt::new(
let receipt = BlockReceipts::new(vec![Receipt::new(
H256::zero(),
U256::zero(),
vec![]);
vec![])]);
let mut rlp = RlpStream::new();
rlp.append(&receipt);
return Some(rlp.out());
Expand Down
11 changes: 11 additions & 0 deletions ethcore/src/tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ fn imports_from_empty() {
client.flush_queue();
}

#[test]
fn returns_state_root_basic() {
let client_result = generate_dummy_client(6);
let client = client_result.reference();
let test_spec = get_test_spec();
let test_engine = test_spec.to_engine().unwrap();
let state_root = test_engine.spec().genesis_header().state_root;

assert!(client.state_data(&state_root).is_some());
}

#[test]
fn imports_good_block() {
let dir = RandomTempPath::new();
Expand Down
20 changes: 12 additions & 8 deletions sync/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const MAX_BODIES_TO_SEND: usize = 256;
const MAX_HEADERS_TO_SEND: usize = 512;
const MAX_NODE_DATA_TO_SEND: usize = 1024;
const MAX_RECEIPTS_TO_SEND: usize = 1024;
const MAX_RECEIPTS_HEADERS_TO_SEND: usize = 16;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so small?

const MAX_HEADERS_TO_REQUEST: usize = 512;
const MAX_BODIES_TO_REQUEST: usize = 256;
const MIN_PEERS_PROPAGATION: usize = 4;
Expand Down Expand Up @@ -1060,17 +1061,20 @@ impl ChainSync {
debug!(target: "sync", "Empty GetReceipts request, ignoring.");
return Ok(None);
}
count = min(count, MAX_RECEIPTS_TO_SEND);
let mut added = 0usize;
count = min(count, MAX_RECEIPTS_HEADERS_TO_SEND);
let mut added_headers = 0usize;
let mut added_receipts = 0usize;
let mut data = Bytes::new();
for i in 0..count {
if let Some(mut hdr) = io.chain().block_receipts(&try!(rlp.val_at::<H256>(i))) {
data.append(&mut hdr);
added += 1;
if let Some(mut receipts_bytes) = io.chain().block_receipts(&try!(rlp.val_at::<H256>(i))) {
data.append(&mut receipts_bytes);
added_receipts += receipts_bytes.len();
added_headers += 1;
if added_receipts > MAX_RECEIPTS_TO_SEND { break; }
}
}
let mut rlp_result = RlpStream::new_list(added);
rlp_result.append_raw(&data, added);
let mut rlp_result = RlpStream::new_list(added_headers);
rlp_result.append_raw(&data, added_headers);
Ok(Some((RECEIPTS_PACKET, rlp_result)))
}

Expand Down Expand Up @@ -1396,7 +1400,7 @@ mod tests {
assert!(rlp_result.is_some());

// the length of two rlp-encoded receipts
assert_eq!(597, rlp_result.unwrap().1.out().len());
assert_eq!(603, rlp_result.unwrap().1.out().len());

let mut sync = dummy_sync_with_peer(H256::new());
io.sender = Some(2usize);
Expand Down