Merge pull request #121 from aeyakovenko/helpers
requests to packets utility function
This commit is contained in:
commit
d20c952f92
|
@ -36,7 +36,7 @@ pub struct AccountantSkel<W: Write + Send + 'static> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
|
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub enum Request {
|
pub enum Request {
|
||||||
Transaction(Transaction),
|
Transaction(Transaction),
|
||||||
GetBalance { key: PublicKey },
|
GetBalance { key: PublicKey },
|
||||||
|
@ -290,11 +290,32 @@ impl<W: Write + Send + 'static> AccountantSkel<W> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn to_packets(r: &packet::PacketRecycler, reqs: Vec<Request>) -> Vec<SharedPackets> {
|
||||||
|
let mut out = vec![];
|
||||||
|
for rrs in reqs.chunks(packet::NUM_PACKETS) {
|
||||||
|
let p = r.allocate();
|
||||||
|
p.write()
|
||||||
|
.unwrap()
|
||||||
|
.packets
|
||||||
|
.resize(rrs.len(), Default::default());
|
||||||
|
for (i, o) in rrs.iter().zip(p.write().unwrap().packets.iter_mut()) {
|
||||||
|
let v = serialize(&i).expect("serialize request");
|
||||||
|
let len = v.len();
|
||||||
|
o.data[..len].copy_from_slice(&v);
|
||||||
|
o.meta.size = len;
|
||||||
|
}
|
||||||
|
out.push(p);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use accountant_skel::Request;
|
use accountant_skel::{to_packets, Request};
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
use ecdsa;
|
use ecdsa;
|
||||||
|
use packet::{PacketRecycler, NUM_PACKETS};
|
||||||
use transaction::{memfind, test_tx};
|
use transaction::{memfind, test_tx};
|
||||||
#[test]
|
#[test]
|
||||||
fn test_layout() {
|
fn test_layout() {
|
||||||
|
@ -302,6 +323,24 @@ mod tests {
|
||||||
let tx = serialize(&tr).unwrap();
|
let tx = serialize(&tr).unwrap();
|
||||||
let packet = serialize(&Request::Transaction(tr)).unwrap();
|
let packet = serialize(&Request::Transaction(tr)).unwrap();
|
||||||
assert_matches!(memfind(&packet, &tx), Some(ecdsa::TX_OFFSET));
|
assert_matches!(memfind(&packet, &tx), Some(ecdsa::TX_OFFSET));
|
||||||
|
assert_matches!(memfind(&packet, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), None);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_to_packets() {
|
||||||
|
let tr = Request::Transaction(test_tx());
|
||||||
|
let re = PacketRecycler::default();
|
||||||
|
let rv = to_packets(&re, vec![tr.clone(); 1]);
|
||||||
|
assert_eq!(rv.len(), 1);
|
||||||
|
assert_eq!(rv[0].read().unwrap().packets.len(), 1);
|
||||||
|
|
||||||
|
let rv = to_packets(&re, vec![tr.clone(); NUM_PACKETS]);
|
||||||
|
assert_eq!(rv.len(), 1);
|
||||||
|
assert_eq!(rv[0].read().unwrap().packets.len(), NUM_PACKETS);
|
||||||
|
|
||||||
|
let rv = to_packets(&re, vec![tr.clone(); NUM_PACKETS + 1]);
|
||||||
|
assert_eq!(rv.len(), 2);
|
||||||
|
assert_eq!(rv[0].read().unwrap().packets.len(), NUM_PACKETS);
|
||||||
|
assert_eq!(rv[1].read().unwrap().packets.len(), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,7 @@ mod tests {
|
||||||
fn test_ticking_historian() {
|
fn test_ticking_historian() {
|
||||||
let zero = Hash::default();
|
let zero = Hash::default();
|
||||||
let hist = Historian::new(&zero, Some(20));
|
let hist = Historian::new(&zero, Some(20));
|
||||||
sleep(Duration::from_millis(30));
|
sleep(Duration::from_millis(300));
|
||||||
hist.sender.send(Signal::Tick).unwrap();
|
hist.sender.send(Signal::Tick).unwrap();
|
||||||
drop(hist.sender);
|
drop(hist.sender);
|
||||||
let entries: Vec<Entry> = hist.receiver.iter().collect();
|
let entries: Vec<Entry> = hist.receiver.iter().collect();
|
||||||
|
|
|
@ -11,7 +11,7 @@ pub type SharedBlob = Arc<RwLock<Blob>>;
|
||||||
pub type PacketRecycler = Recycler<Packets>;
|
pub type PacketRecycler = Recycler<Packets>;
|
||||||
pub type BlobRecycler = Recycler<Blob>;
|
pub type BlobRecycler = Recycler<Blob>;
|
||||||
|
|
||||||
const NUM_PACKETS: usize = 1024 * 8;
|
pub const NUM_PACKETS: usize = 1024 * 8;
|
||||||
const BLOB_SIZE: usize = 64 * 1024;
|
const BLOB_SIZE: usize = 64 * 1024;
|
||||||
pub const PACKET_DATA_SIZE: usize = 256;
|
pub const PACKET_DATA_SIZE: usize = 256;
|
||||||
pub const NUM_BLOBS: usize = (NUM_PACKETS * PACKET_DATA_SIZE) / BLOB_SIZE;
|
pub const NUM_BLOBS: usize = (NUM_PACKETS * PACKET_DATA_SIZE) / BLOB_SIZE;
|
||||||
|
|
Loading…
Reference in New Issue