Merge pull request #223 from rlkelly/202__rust_refactor

202  rust refactor
This commit is contained in:
Greg Fitzgerald 2018-05-15 08:44:47 -06:00 committed by GitHub
commit cd262cf860
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 12 deletions

View File

@ -286,8 +286,8 @@ impl Bank {
.entry(tx_sig) .entry(tx_sig)
{ {
e.get_mut().apply_witness(&Witness::Signature(from)); e.get_mut().apply_witness(&Witness::Signature(from));
if let Some(ref payment) = e.get().final_payment() { if let Some(payment) = e.get().final_payment() {
apply_payment(&self.balances, payment); apply_payment(&self.balances, &payment);
e.remove_entry(); e.remove_entry();
} }
}; };

View File

@ -3,7 +3,7 @@
//! repair partitions. //! repair partitions.
//! //!
//! This CRDT only supports a very limited set of types. A map of PublicKey -> Versioned Struct. //! This CRDT only supports a very limited set of types. A map of PublicKey -> Versioned Struct.
//! The last version is always picked durring an update. //! The last version is always picked during an update.
//! //!
//! The network is arranged in layers: //! The network is arranged in layers:
//! //!
@ -89,7 +89,7 @@ pub struct Crdt {
/// should respond with all the identities that are greater then the /// should respond with all the identities that are greater then the
/// request's `update_index` in this list /// request's `update_index` in this list
local: HashMap<PublicKey, u64>, local: HashMap<PublicKey, u64>,
/// The value of the remote update index that i have last seen /// The value of the remote update index that I have last seen
/// This Node will ask external nodes for updates since the value in this list /// This Node will ask external nodes for updates since the value in this list
pub remote: HashMap<PublicKey, u64>, pub remote: HashMap<PublicKey, u64>,
pub update_index: u64, pub update_index: u64,
@ -169,7 +169,7 @@ impl Crdt {
transmit_index: &mut u64, transmit_index: &mut u64,
) -> Result<()> { ) -> Result<()> {
let (me, table): (ReplicatedData, Vec<ReplicatedData>) = { let (me, table): (ReplicatedData, Vec<ReplicatedData>) = {
// copy to avoid locking durring IO // copy to avoid locking during IO
let robj = obj.read().expect("'obj' read lock in pub fn broadcast"); let robj = obj.read().expect("'obj' read lock in pub fn broadcast");
info!("broadcast table {}", robj.table.len()); info!("broadcast table {}", robj.table.len());
let cloned_table: Vec<ReplicatedData> = robj.table.values().cloned().collect(); let cloned_table: Vec<ReplicatedData> = robj.table.values().cloned().collect();
@ -192,12 +192,12 @@ impl Crdt {
}) })
.collect(); .collect();
if nodes.len() < 1 { if nodes.len() < 1 {
return Err(Error::CrdtToSmall); return Err(Error::CrdtTooSmall);
} }
info!("nodes table {}", nodes.len()); info!("nodes table {}", nodes.len());
info!("blobs table {}", blobs.len()); info!("blobs table {}", blobs.len());
// enumerate all the blobs, those are the indecies // enumerate all the blobs, those are the indices
// transmit them to nodes, starting from a different node // transmit them to nodes, starting from a different node
let orders: Vec<_> = blobs let orders: Vec<_> = blobs
.iter() .iter()
@ -245,7 +245,7 @@ impl Crdt {
/// We need to avoid having obj locked while doing any io, such as the `send_to` /// We need to avoid having obj locked while doing any io, such as the `send_to`
pub fn retransmit(obj: &Arc<RwLock<Self>>, blob: &SharedBlob, s: &UdpSocket) -> Result<()> { pub fn retransmit(obj: &Arc<RwLock<Self>>, blob: &SharedBlob, s: &UdpSocket) -> Result<()> {
let (me, table): (ReplicatedData, Vec<ReplicatedData>) = { let (me, table): (ReplicatedData, Vec<ReplicatedData>) = {
// copy to avoid locking durring IO // copy to avoid locking during IO
let s = obj.read().expect("'obj' read lock in pub fn retransmit"); let s = obj.read().expect("'obj' read lock in pub fn retransmit");
(s.table[&s.me].clone(), s.table.values().cloned().collect()) (s.table[&s.me].clone(), s.table.values().cloned().collect())
}; };
@ -317,7 +317,7 @@ impl Crdt {
pub fn window_index_request(&self, ix: u64) -> Result<(SocketAddr, Vec<u8>)> { pub fn window_index_request(&self, ix: u64) -> Result<(SocketAddr, Vec<u8>)> {
if self.table.len() <= 1 { if self.table.len() <= 1 {
return Err(Error::CrdtToSmall); return Err(Error::CrdtTooSmall);
} }
let mut n = (Self::random() as usize) % self.table.len(); let mut n = (Self::random() as usize) % self.table.len();
while self.table.values().nth(n).unwrap().id == self.me { while self.table.values().nth(n).unwrap().id == self.me {
@ -337,7 +337,7 @@ impl Crdt {
fn gossip_request(&self) -> Result<(SocketAddr, Protocol)> { fn gossip_request(&self) -> Result<(SocketAddr, Protocol)> {
let options: Vec<_> = self.table.values().filter(|v| v.id != self.me).collect(); let options: Vec<_> = self.table.values().filter(|v| v.id != self.me).collect();
if options.len() < 1 { if options.len() < 1 {
return Err(Error::CrdtToSmall); return Err(Error::CrdtTooSmall);
} }
let n = (Self::random() as usize) % options.len(); let n = (Self::random() as usize) % options.len();
let v = options[n].clone(); let v = options[n].clone();
@ -439,7 +439,7 @@ impl Crdt {
Protocol::RequestUpdates(v, reqdata) => { Protocol::RequestUpdates(v, reqdata) => {
trace!("RequestUpdates {}", v); trace!("RequestUpdates {}", v);
let addr = reqdata.gossip_addr; let addr = reqdata.gossip_addr;
// only lock for this call, dont lock durring IO `sock.send_to` or `sock.recv_from` // only lock for this call, dont lock during IO `sock.send_to` or `sock.recv_from`
let (from, ups, data) = obj.read() let (from, ups, data) = obj.read()
.expect("'obj' read lock in RequestUpdates") .expect("'obj' read lock in RequestUpdates")
.get_updates_since(v); .get_updates_since(v);

View File

@ -18,7 +18,7 @@ pub enum Error {
BankError(bank::BankError), BankError(bank::BankError),
SendError, SendError,
Services, Services,
CrdtToSmall, CrdtTooSmall,
GenericError, GenericError,
} }