receipt tests
This commit is contained in:
parent
ab131b134b
commit
8d7523a027
@ -177,6 +177,7 @@ pub struct ChainSync {
|
|||||||
have_common_block: bool,
|
have_common_block: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RlpResponseResult = Result<Option<(PacketId, RlpStream)>, PacketDecodeError>;
|
||||||
|
|
||||||
impl ChainSync {
|
impl ChainSync {
|
||||||
/// Create a new instance of syncing strategy.
|
/// Create a new instance of syncing strategy.
|
||||||
@ -845,7 +846,7 @@ impl ChainSync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Respond to GetBlockHeaders request
|
/// Respond to GetBlockHeaders request
|
||||||
fn return_block_headers(&self, io: &mut SyncIo, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
fn return_block_headers(io: &SyncIo, r: &UntrustedRlp) -> RlpResponseResult {
|
||||||
// Packet layout:
|
// Packet layout:
|
||||||
// [ block: { P , B_32 }, maxHeaders: P, skip: P, reverse: P in { 0 , 1 } ]
|
// [ block: { P , B_32 }, maxHeaders: P, skip: P, reverse: P in { 0 , 1 } ]
|
||||||
let max_headers: usize = try!(r.val_at(1));
|
let max_headers: usize = try!(r.val_at(1));
|
||||||
@ -892,18 +893,16 @@ impl ChainSync {
|
|||||||
}
|
}
|
||||||
let mut rlp = RlpStream::new_list(count as usize);
|
let mut rlp = RlpStream::new_list(count as usize);
|
||||||
rlp.append_raw(&data, count as usize);
|
rlp.append_raw(&data, count as usize);
|
||||||
io.respond(BLOCK_HEADERS_PACKET, rlp.out()).unwrap_or_else(|e|
|
|
||||||
debug!(target: "sync", "Error sending headers: {:?}", e));
|
|
||||||
trace!(target: "sync", "-> GetBlockHeaders: returned {} entries", count);
|
trace!(target: "sync", "-> GetBlockHeaders: returned {} entries", count);
|
||||||
Ok(())
|
Ok(Some((BLOCK_HEADERS_PACKET, rlp)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Respond to GetBlockBodies request
|
/// Respond to GetBlockBodies request
|
||||||
fn return_block_bodies(&self, io: &mut SyncIo, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
fn return_block_bodies(io: &SyncIo, r: &UntrustedRlp) -> RlpResponseResult {
|
||||||
let mut count = r.item_count();
|
let mut count = r.item_count();
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
debug!(target: "sync", "Empty GetBlockBodies request, ignoring.");
|
debug!(target: "sync", "Empty GetBlockBodies request, ignoring.");
|
||||||
return Ok(());
|
return Ok(None);
|
||||||
}
|
}
|
||||||
trace!(target: "sync", "-> GetBlockBodies: {} entries", count);
|
trace!(target: "sync", "-> GetBlockBodies: {} entries", count);
|
||||||
count = min(count, MAX_BODIES_TO_SEND);
|
count = min(count, MAX_BODIES_TO_SEND);
|
||||||
@ -917,18 +916,16 @@ impl ChainSync {
|
|||||||
}
|
}
|
||||||
let mut rlp = RlpStream::new_list(added);
|
let mut rlp = RlpStream::new_list(added);
|
||||||
rlp.append_raw(&data, added);
|
rlp.append_raw(&data, added);
|
||||||
io.respond(BLOCK_BODIES_PACKET, rlp.out()).unwrap_or_else(|e|
|
|
||||||
debug!(target: "sync", "Error sending headers: {:?}", e));
|
|
||||||
trace!(target: "sync", "-> GetBlockBodies: returned {} entries", added);
|
trace!(target: "sync", "-> GetBlockBodies: returned {} entries", added);
|
||||||
Ok(())
|
Ok(Some((BLOCK_BODIES_PACKET, rlp)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Respond to GetNodeData request
|
/// Respond to GetNodeData request
|
||||||
fn return_node_data(&self, io: &mut SyncIo, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
fn return_node_data(io: &SyncIo, r: &UntrustedRlp) -> RlpResponseResult {
|
||||||
let mut count = r.item_count();
|
let mut count = r.item_count();
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
debug!(target: "sync", "Empty GetNodeData request, ignoring.");
|
debug!(target: "sync", "Empty GetNodeData request, ignoring.");
|
||||||
return Ok(());
|
return Ok(None);
|
||||||
}
|
}
|
||||||
count = min(count, MAX_NODE_DATA_TO_SEND);
|
count = min(count, MAX_NODE_DATA_TO_SEND);
|
||||||
let mut added = 0usize;
|
let mut added = 0usize;
|
||||||
@ -941,33 +938,44 @@ impl ChainSync {
|
|||||||
}
|
}
|
||||||
let mut rlp = RlpStream::new_list(added);
|
let mut rlp = RlpStream::new_list(added);
|
||||||
rlp.append_raw(&data, added);
|
rlp.append_raw(&data, added);
|
||||||
io.respond(NODE_DATA_PACKET, rlp.out()).unwrap_or_else(|e|
|
Ok(Some((NODE_DATA_PACKET, rlp)))
|
||||||
debug!(target: "sync", "Error sending headers: {:?}", e));
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Respond to GetReceipts request
|
fn return_receipts(io: &SyncIo, rlp: &UntrustedRlp) -> RlpResponseResult {
|
||||||
fn return_receipts(&self, io: &mut SyncIo, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
let mut count = rlp.item_count();
|
||||||
let mut count = r.item_count();
|
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
debug!(target: "sync", "Empty GetReceipts request, ignoring.");
|
debug!(target: "sync", "Empty GetReceipts request, ignoring.");
|
||||||
return Ok(());
|
return Ok(None);
|
||||||
}
|
}
|
||||||
count = min(count, MAX_RECEIPTS_TO_SEND);
|
count = min(count, MAX_RECEIPTS_TO_SEND);
|
||||||
let mut added = 0usize;
|
let mut added = 0usize;
|
||||||
let mut data = Bytes::new();
|
let mut data = Bytes::new();
|
||||||
for i in 0..count {
|
for i in 0..count {
|
||||||
if let Some(mut hdr) = io.chain().block_receipts(&try!(r.val_at::<H256>(i))) {
|
if let Some(mut hdr) = io.chain().block_receipts(&try!(rlp.val_at::<H256>(i))) {
|
||||||
data.append(&mut hdr);
|
data.append(&mut hdr);
|
||||||
added += 1;
|
added += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut rlp = RlpStream::new_list(added);
|
let mut rlp_result = RlpStream::new_list(added);
|
||||||
rlp.append_raw(&data, added);
|
rlp_result.append_raw(&data, added);
|
||||||
io.respond(RECEIPTS_PACKET, rlp.out()).unwrap_or_else(|e|
|
Ok(Some((RECEIPTS_PACKET, rlp_result)))
|
||||||
debug!(target: "sync", "Error sending headers: {:?}", e));
|
}
|
||||||
|
|
||||||
|
fn return_rlp<FRlp, FError>(&self, io: &mut SyncIo, rlp: &UntrustedRlp, rlp_func: FRlp, error_func: FError) -> Result<(), PacketDecodeError>
|
||||||
|
where FRlp : Fn(&SyncIo, &UntrustedRlp) -> RlpResponseResult,
|
||||||
|
FError : FnOnce(UtilError) -> String
|
||||||
|
{
|
||||||
|
let response = rlp_func(io, rlp);
|
||||||
|
match response {
|
||||||
|
Err(e) => Err(e),
|
||||||
|
Ok(Some((packet_id, rlp_stream))) => {
|
||||||
|
io.respond(packet_id, rlp_stream.out()).unwrap_or_else(
|
||||||
|
|e| debug!(target: "sync", "{:?}", error_func(e)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
_ => Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Dispatch incoming requests and responses
|
/// Dispatch incoming requests and responses
|
||||||
pub fn on_packet(&mut self, io: &mut SyncIo, peer: PeerId, packet_id: u8, data: &[u8]) {
|
pub fn on_packet(&mut self, io: &mut SyncIo, peer: PeerId, packet_id: u8, data: &[u8]) {
|
||||||
@ -975,14 +983,26 @@ impl ChainSync {
|
|||||||
let result = match packet_id {
|
let result = match packet_id {
|
||||||
STATUS_PACKET => self.on_peer_status(io, peer, &rlp),
|
STATUS_PACKET => self.on_peer_status(io, peer, &rlp),
|
||||||
TRANSACTIONS_PACKET => self.on_peer_transactions(io, peer, &rlp),
|
TRANSACTIONS_PACKET => self.on_peer_transactions(io, peer, &rlp),
|
||||||
GET_BLOCK_HEADERS_PACKET => self.return_block_headers(io, &rlp),
|
|
||||||
BLOCK_HEADERS_PACKET => self.on_peer_block_headers(io, peer, &rlp),
|
BLOCK_HEADERS_PACKET => self.on_peer_block_headers(io, peer, &rlp),
|
||||||
GET_BLOCK_BODIES_PACKET => self.return_block_bodies(io, &rlp),
|
|
||||||
BLOCK_BODIES_PACKET => self.on_peer_block_bodies(io, peer, &rlp),
|
BLOCK_BODIES_PACKET => self.on_peer_block_bodies(io, peer, &rlp),
|
||||||
NEW_BLOCK_PACKET => self.on_peer_new_block(io, peer, &rlp),
|
NEW_BLOCK_PACKET => self.on_peer_new_block(io, peer, &rlp),
|
||||||
NEW_BLOCK_HASHES_PACKET => self.on_peer_new_hashes(io, peer, &rlp),
|
NEW_BLOCK_HASHES_PACKET => self.on_peer_new_hashes(io, peer, &rlp),
|
||||||
GET_NODE_DATA_PACKET => self.return_node_data(io, &rlp),
|
|
||||||
GET_RECEIPTS_PACKET => self.return_receipts(io, &rlp),
|
GET_BLOCK_BODIES_PACKET => self.return_rlp(io, &rlp,
|
||||||
|
ChainSync::return_block_bodies,
|
||||||
|
|e| format!("Error sending block bodies: {:?}", e)),
|
||||||
|
|
||||||
|
GET_BLOCK_HEADERS_PACKET => self.return_rlp(io, &rlp,
|
||||||
|
ChainSync::return_block_headers,
|
||||||
|
|e| format!("Error sending block headers: {:?}", e)),
|
||||||
|
|
||||||
|
GET_RECEIPTS_PACKET => self.return_rlp(io, &rlp,
|
||||||
|
ChainSync::return_receipts,
|
||||||
|
|e| format!("Error sending receipts: {:?}", e)),
|
||||||
|
|
||||||
|
GET_NODE_DATA_PACKET => self.return_rlp(io, &rlp,
|
||||||
|
ChainSync::return_node_data,
|
||||||
|
|e| format!("Error sending nodes: {:?}", e)),
|
||||||
_ => {
|
_ => {
|
||||||
debug!(target: "sync", "Unknown packet {}", packet_id);
|
debug!(target: "sync", "Unknown packet {}", packet_id);
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -1021,17 +1041,36 @@ mod tests {
|
|||||||
use util::*;
|
use util::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn return_receipts() {
|
fn return_receipts_empty() {
|
||||||
let mut client = TestBlockChainClient::new();
|
let mut client = TestBlockChainClient::new();
|
||||||
let mut queue:VecDeque<TestPacket> = VecDeque::new();
|
let mut queue = VecDeque::new();
|
||||||
let mut io = TestIo::new(& mut client, & mut queue, None);
|
let io = TestIo::new(&mut client, &mut queue, None);
|
||||||
|
|
||||||
let sync = ChainSync::new();
|
let result = ChainSync::return_receipts(&io, &UntrustedRlp::new(&[0xc0]));
|
||||||
let data = vec![0xc0];
|
|
||||||
let rlp = UntrustedRlp::new(&data);
|
|
||||||
|
|
||||||
let result = sync.return_receipts(& mut io, &rlp);
|
|
||||||
|
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn return_receipts() {
|
||||||
|
let mut client = TestBlockChainClient::new();
|
||||||
|
let mut queue = VecDeque::new();
|
||||||
|
let io = TestIo::new(&mut client, &mut queue, None);
|
||||||
|
|
||||||
|
let mut receipt_list = RlpStream::new_list(4);
|
||||||
|
receipt_list.append(&H256::from("0000000000000000000000000000000000000000000000005555555555555555"));
|
||||||
|
receipt_list.append(&H256::from("ff00000000000000000000000000000000000000000000000000000000000000"));
|
||||||
|
receipt_list.append(&H256::from("fff0000000000000000000000000000000000000000000000000000000000000"));
|
||||||
|
receipt_list.append(&H256::from("aff0000000000000000000000000000000000000000000000000000000000000"));
|
||||||
|
|
||||||
|
// it returns rlp ONLY for hashes started with "f"
|
||||||
|
let result = ChainSync::return_receipts(&io, &UntrustedRlp::new(&receipt_list.out()));
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
let rlp_result = result.unwrap();
|
||||||
|
assert!(rlp_result.is_some());
|
||||||
|
|
||||||
|
// the length of two rlp-encoded receipts
|
||||||
|
assert_eq!(597, rlp_result.unwrap().1.out().len());
|
||||||
|
}
|
||||||
}
|
}
|
@ -5,6 +5,7 @@ use ethcore::header::{Header as BlockHeader, BlockNumber};
|
|||||||
use ethcore::error::*;
|
use ethcore::error::*;
|
||||||
use io::SyncIo;
|
use io::SyncIo;
|
||||||
use chain::{ChainSync};
|
use chain::{ChainSync};
|
||||||
|
use ethcore::receipt::Receipt;
|
||||||
|
|
||||||
pub struct TestBlockChainClient {
|
pub struct TestBlockChainClient {
|
||||||
pub blocks: RwLock<HashMap<H256, Bytes>>,
|
pub blocks: RwLock<HashMap<H256, Bytes>>,
|
||||||
@ -120,7 +121,17 @@ impl BlockChainClient for TestBlockChainClient {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn block_receipts(&self, _h: &H256) -> Option<Bytes> {
|
fn block_receipts(&self, hash: &H256) -> Option<Bytes> {
|
||||||
|
// starts with 'f' ?
|
||||||
|
if *hash > H256::from("f000000000000000000000000000000000000000000000000000000000000000") {
|
||||||
|
let receipt = Receipt::new(
|
||||||
|
H256::zero(),
|
||||||
|
U256::zero(),
|
||||||
|
vec![]);
|
||||||
|
let mut rlp = RlpStream::new();
|
||||||
|
rlp.append(&receipt);
|
||||||
|
return Some(rlp.out());
|
||||||
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user