diff --git a/core/src/cluster_info.rs b/core/src/cluster_info.rs index 82242c172..8624c651c 100644 --- a/core/src/cluster_info.rs +++ b/core/src/cluster_info.rs @@ -1454,7 +1454,7 @@ mod tests { use crate::test_tx::test_tx; use solana_sdk::signature::{Keypair, KeypairUtil}; use std::collections::HashSet; - use std::net::{IpAddr, Ipv4Addr, SocketAddr}; + use std::net::{IpAddr, Ipv4Addr}; use std::sync::{Arc, RwLock}; #[test] diff --git a/core/src/contact_info.rs b/core/src/contact_info.rs index ca32bf4ba..62b19302d 100644 --- a/core/src/contact_info.rs +++ b/core/src/contact_info.rs @@ -51,10 +51,10 @@ impl Eq for ContactInfo {} #[macro_export] macro_rules! socketaddr { ($ip:expr, $port:expr) => { - SocketAddr::from((Ipv4Addr::from($ip), $port)) + std::net::SocketAddr::from((Ipv4Addr::from($ip), $port)) }; ($str:expr) => {{ - let a: SocketAddr = $str.parse().unwrap(); + let a: std::net::SocketAddr = $str.parse().unwrap(); a }}; } diff --git a/core/src/crds.rs b/core/src/crds.rs index fbb896600..9dc35393b 100644 --- a/core/src/crds.rs +++ b/core/src/crds.rs @@ -165,13 +165,12 @@ impl Crds { mod test { use super::*; use crate::contact_info::ContactInfo; - use crate::crds_value::LeaderId; use solana_sdk::signature::{Keypair, KeypairUtil}; #[test] fn test_insert() { let mut crds = Crds::default(); - let val = CrdsValue::LeaderId(LeaderId::default()); + let val = CrdsValue::ContactInfo(ContactInfo::default()); assert_eq!(crds.insert(val.clone(), 0).ok(), Some(None)); assert_eq!(crds.table.len(), 1); assert!(crds.table.contains_key(&val.label())); @@ -180,7 +179,7 @@ mod test { #[test] fn test_update_old() { let mut crds = Crds::default(); - let val = CrdsValue::LeaderId(LeaderId::default()); + let val = CrdsValue::ContactInfo(ContactInfo::default()); assert_eq!(crds.insert(val.clone(), 0), Ok(None)); assert_eq!(crds.insert(val.clone(), 1), Err(CrdsError::InsertFailed)); assert_eq!(crds.table[&val.label()].local_timestamp, 0); @@ -188,9 +187,9 @@ mod test { #[test] fn test_update_new() { let mut crds = Crds::default(); - let original = CrdsValue::LeaderId(LeaderId::default()); + let original = CrdsValue::ContactInfo(ContactInfo::new_localhost(Pubkey::default(), 0)); assert_matches!(crds.insert(original.clone(), 0), Ok(_)); - let val = CrdsValue::LeaderId(LeaderId::new(Pubkey::default(), Pubkey::default(), 1)); + let val = CrdsValue::ContactInfo(ContactInfo::new_localhost(Pubkey::default(), 1)); assert_eq!( crds.insert(val.clone(), 1).unwrap().unwrap().value, original @@ -198,9 +197,9 @@ mod test { assert_eq!(crds.table[&val.label()].local_timestamp, 1); } #[test] - fn test_update_timestsamp() { + fn test_update_timestamp() { let mut crds = Crds::default(); - let val = CrdsValue::LeaderId(LeaderId::default()); + let val = CrdsValue::ContactInfo(ContactInfo::new_localhost(Pubkey::default(), 0)); assert_eq!(crds.insert(val.clone(), 0), Ok(None)); crds.update_label_timestamp(&val.label(), 1); @@ -209,7 +208,7 @@ mod test { let val2 = CrdsValue::ContactInfo(ContactInfo::default()); assert_eq!(val2.label().pubkey(), val.label().pubkey()); - assert_matches!(crds.insert(val2.clone(), 0), Ok(None)); + assert_matches!(crds.insert(val2.clone(), 0), Ok(Some(_))); crds.update_record_timestamp(val.label().pubkey(), 2); assert_eq!(crds.table[&val.label()].local_timestamp, 2); @@ -231,7 +230,7 @@ mod test { #[test] fn test_find_old_records() { let mut crds = Crds::default(); - let val = CrdsValue::LeaderId(LeaderId::default()); + let val = CrdsValue::ContactInfo(ContactInfo::default()); assert_eq!(crds.insert(val.clone(), 1), Ok(None)); assert!(crds.find_old_labels(0).is_empty()); @@ -241,7 +240,7 @@ mod test { #[test] fn test_remove() { let mut crds = Crds::default(); - let val = CrdsValue::LeaderId(LeaderId::default()); + let val = CrdsValue::ContactInfo(ContactInfo::default()); assert_matches!(crds.insert(val.clone(), 1), Ok(_)); assert_eq!(crds.find_old_labels(1), vec![val.label()]); @@ -250,48 +249,50 @@ mod test { } #[test] fn test_equal() { - let key = Keypair::new(); - let v1 = VersionedCrdsValue::new( - 1, - CrdsValue::LeaderId(LeaderId::new(key.pubkey(), Pubkey::default(), 0)), - ); - let v2 = VersionedCrdsValue::new( - 1, - CrdsValue::LeaderId(LeaderId::new(key.pubkey(), Pubkey::default(), 0)), - ); + let val = CrdsValue::ContactInfo(ContactInfo::default()); + let v1 = VersionedCrdsValue::new(1, val.clone()); + let v2 = VersionedCrdsValue::new(1, val); + assert_eq!(v1, v2); assert!(!(v1 != v2)); - assert!(v1 == v2); } #[test] fn test_hash_order() { - let key = Keypair::new(); let v1 = VersionedCrdsValue::new( 1, - CrdsValue::LeaderId(LeaderId::new(key.pubkey(), Pubkey::default(), 0)), - ); - let v2 = VersionedCrdsValue::new( - 1, - CrdsValue::LeaderId(LeaderId::new(key.pubkey(), key.pubkey(), 0)), + CrdsValue::ContactInfo(ContactInfo::new_localhost(Pubkey::default(), 0)), ); + let v2 = VersionedCrdsValue::new(1, { + let mut contact_info = ContactInfo::new_localhost(Pubkey::default(), 0); + contact_info.rpc = socketaddr!("0.0.0.0:0"); + CrdsValue::ContactInfo(contact_info) + }); + + assert_eq!(v1.value.label(), v2.value.label()); + assert_eq!(v1.value.wallclock(), v2.value.wallclock()); + assert_ne!(v1.value_hash, v2.value_hash); assert!(v1 != v2); assert!(!(v1 == v2)); if v1 > v2 { - assert!(v2 < v1) + assert!(v1 > v2); + assert!(v2 < v1); + } else if v2 > v1 { + assert!(v1 < v2); + assert!(v2 > v1); } else { - assert!(v2 > v1) + panic!("bad PartialOrd implementation?"); } } #[test] fn test_wallclock_order() { - let key = Keypair::new(); let v1 = VersionedCrdsValue::new( 1, - CrdsValue::LeaderId(LeaderId::new(key.pubkey(), Pubkey::default(), 1)), + CrdsValue::ContactInfo(ContactInfo::new_localhost(Pubkey::default(), 1)), ); let v2 = VersionedCrdsValue::new( 1, - CrdsValue::LeaderId(LeaderId::new(key.pubkey(), Pubkey::default(), 0)), + CrdsValue::ContactInfo(ContactInfo::new_localhost(Pubkey::default(), 0)), ); + assert_eq!(v1.value.label(), v2.value.label()); assert!(v1 > v2); assert!(!(v1 < v2)); assert!(v1 != v2); @@ -301,13 +302,13 @@ mod test { fn test_label_order() { let v1 = VersionedCrdsValue::new( 1, - CrdsValue::LeaderId(LeaderId::new(Keypair::new().pubkey(), Pubkey::default(), 0)), + CrdsValue::ContactInfo(ContactInfo::new_localhost(Keypair::new().pubkey(), 0)), ); let v2 = VersionedCrdsValue::new( 1, - CrdsValue::LeaderId(LeaderId::new(Keypair::new().pubkey(), Pubkey::default(), 0)), + CrdsValue::ContactInfo(ContactInfo::new_localhost(Keypair::new().pubkey(), 0)), ); - assert!(v1 != v2); + assert_ne!(v1, v2); assert!(!(v1 == v2)); assert!(!(v1 < v2)); assert!(!(v1 > v2)); diff --git a/core/src/crds_gossip_pull.rs b/core/src/crds_gossip_pull.rs index 4bb8f5cb6..8a8cb9fb3 100644 --- a/core/src/crds_gossip_pull.rs +++ b/core/src/crds_gossip_pull.rs @@ -209,7 +209,6 @@ impl CrdsGossipPull { mod test { use super::*; use crate::contact_info::ContactInfo; - use crate::crds_value::LeaderId; use solana_sdk::signature::{Keypair, KeypairUtil}; #[test] @@ -325,17 +324,20 @@ mod test { let node_id = entry.label().pubkey(); let mut node = CrdsGossipPull::default(); node_crds.insert(entry.clone(), 0).unwrap(); + let new = CrdsValue::ContactInfo(ContactInfo::new_localhost(Keypair::new().pubkey(), 0)); node_crds.insert(new.clone(), 0).unwrap(); let mut dest = CrdsGossipPull::default(); let mut dest_crds = Crds::default(); - let new = CrdsValue::ContactInfo(ContactInfo::new_localhost(Keypair::new().pubkey(), 0)); + let new_id = Keypair::new().pubkey(); + let new = CrdsValue::ContactInfo(ContactInfo::new_localhost(new_id, 1)); dest_crds.insert(new.clone(), 0).unwrap(); // node contains a key from the dest node, but at an older local timestamp - let dest_id = new.label().pubkey(); - let same_key = CrdsValue::LeaderId(LeaderId::new(dest_id, dest_id, 1)); + let same_key = CrdsValue::ContactInfo(ContactInfo::new_localhost(new_id, 0)); + assert_eq!(same_key.label(), new.label()); + assert!(same_key.wallclock() < new.wallclock()); node_crds.insert(same_key.clone(), 0).unwrap(); assert_eq!( node_crds diff --git a/core/src/crds_value.rs b/core/src/crds_value.rs index 759d622ef..073dfa228 100644 --- a/core/src/crds_value.rs +++ b/core/src/crds_value.rs @@ -1,5 +1,4 @@ use crate::contact_info::ContactInfo; -use bincode::serialize; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, Signable, Signature}; use solana_sdk::transaction::Transaction; @@ -12,16 +11,6 @@ pub enum CrdsValue { ContactInfo(ContactInfo), /// * Merge Strategy - Latest wallclock is picked Vote(Vote), - /// * Merge Strategy - Latest wallclock is picked - LeaderId(LeaderId), -} - -#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)] -pub struct LeaderId { - pub id: Pubkey, - pub signature: Signature, - pub leader_id: Pubkey, - pub wallclock: u64, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] @@ -30,35 +19,6 @@ pub struct Vote { pub wallclock: u64, } -impl Signable for LeaderId { - fn pubkey(&self) -> Pubkey { - self.id - } - - fn signable_data(&self) -> Vec { - #[derive(Serialize)] - struct SignData { - id: Pubkey, - leader_id: Pubkey, - wallclock: u64, - } - let data = SignData { - id: self.id, - leader_id: self.leader_id, - wallclock: self.wallclock, - }; - serialize(&data).expect("unable to serialize LeaderId") - } - - fn get_signature(&self) -> Signature { - self.signature - } - - fn set_signature(&mut self, signature: Signature) { - self.signature = signature - } -} - impl Signable for Vote { fn sign(&mut self, _keypair: &Keypair) {} @@ -87,7 +47,6 @@ impl Signable for Vote { pub enum CrdsValueLabel { ContactInfo(Pubkey), Vote(Pubkey), - LeaderId(Pubkey), } impl fmt::Display for CrdsValueLabel { @@ -95,7 +54,6 @@ impl fmt::Display for CrdsValueLabel { match self { CrdsValueLabel::ContactInfo(_) => write!(f, "ContactInfo({})", self.pubkey()), CrdsValueLabel::Vote(_) => write!(f, "Vote({})", self.pubkey()), - CrdsValueLabel::LeaderId(_) => write!(f, "LeaderId({})", self.pubkey()), } } } @@ -105,18 +63,6 @@ impl CrdsValueLabel { match self { CrdsValueLabel::ContactInfo(p) => *p, CrdsValueLabel::Vote(p) => *p, - CrdsValueLabel::LeaderId(p) => *p, - } - } -} - -impl LeaderId { - pub fn new(id: Pubkey, leader_id: Pubkey, wallclock: u64) -> Self { - LeaderId { - id, - signature: Signature::default(), - leader_id, - wallclock, } } } @@ -139,7 +85,6 @@ impl CrdsValue { match self { CrdsValue::ContactInfo(contact_info) => contact_info.wallclock, CrdsValue::Vote(vote) => vote.wallclock, - CrdsValue::LeaderId(leader_id) => leader_id.wallclock, } } pub fn label(&self) -> CrdsValueLabel { @@ -148,7 +93,6 @@ impl CrdsValue { CrdsValueLabel::ContactInfo(contact_info.pubkey()) } CrdsValue::Vote(vote) => CrdsValueLabel::Vote(vote.pubkey()), - CrdsValue::LeaderId(leader_id) => CrdsValueLabel::LeaderId(leader_id.pubkey()), } } pub fn contact_info(&self) -> Option<&ContactInfo> { @@ -157,12 +101,6 @@ impl CrdsValue { _ => None, } } - pub fn leader_id(&self) -> Option<&LeaderId> { - match self { - CrdsValue::LeaderId(leader_id) => Some(leader_id), - _ => None, - } - } pub fn vote(&self) -> Option<&Vote> { match self { CrdsValue::Vote(vote) => Some(vote), @@ -170,12 +108,8 @@ impl CrdsValue { } } /// Return all the possible labels for a record identified by Pubkey. - pub fn record_labels(key: Pubkey) -> [CrdsValueLabel; 3] { - [ - CrdsValueLabel::ContactInfo(key), - CrdsValueLabel::Vote(key), - CrdsValueLabel::LeaderId(key), - ] + pub fn record_labels(key: Pubkey) -> [CrdsValueLabel; 2] { + [CrdsValueLabel::ContactInfo(key), CrdsValueLabel::Vote(key)] } } @@ -184,14 +118,12 @@ impl Signable for CrdsValue { match self { CrdsValue::ContactInfo(contact_info) => contact_info.sign(keypair), CrdsValue::Vote(vote) => vote.sign(keypair), - CrdsValue::LeaderId(leader_id) => leader_id.sign(keypair), }; } fn verify(&self) -> bool { match self { CrdsValue::ContactInfo(contact_info) => contact_info.verify(), CrdsValue::Vote(vote) => vote.verify(), - CrdsValue::LeaderId(leader_id) => leader_id.verify(), } } @@ -199,7 +131,6 @@ impl Signable for CrdsValue { match self { CrdsValue::ContactInfo(contact_info) => contact_info.pubkey(), CrdsValue::Vote(vote) => vote.pubkey(), - CrdsValue::LeaderId(leader_id) => leader_id.pubkey(), } } @@ -226,24 +157,18 @@ mod test { #[test] fn test_labels() { - let mut hits = [false; 3]; + let mut hits = [false; 2]; // this method should cover all the possible labels for v in &CrdsValue::record_labels(Pubkey::default()) { match v { CrdsValueLabel::ContactInfo(_) => hits[0] = true, CrdsValueLabel::Vote(_) => hits[1] = true, - CrdsValueLabel::LeaderId(_) => hits[2] = true, } } assert!(hits.iter().all(|x| *x)); } #[test] fn test_keys_and_values() { - let v = CrdsValue::LeaderId(LeaderId::default()); - let key = v.clone().leader_id().unwrap().id; - assert_eq!(v.wallclock(), 0); - assert_eq!(v.label(), CrdsValueLabel::LeaderId(key)); - let v = CrdsValue::ContactInfo(ContactInfo::default()); assert_eq!(v.wallclock(), 0); let key = v.clone().contact_info().unwrap().id; @@ -258,8 +183,8 @@ mod test { fn test_signature() { let keypair = Keypair::new(); let fake_keypair = Keypair::new(); - let leader = LeaderId::new(keypair.pubkey(), Pubkey::default(), timestamp()); - let mut v = CrdsValue::LeaderId(leader); + let mut v = + CrdsValue::ContactInfo(ContactInfo::new_localhost(keypair.pubkey(), timestamp())); v.sign(&keypair); assert!(v.verify()); v.sign(&fake_keypair); diff --git a/core/src/lib.rs b/core/src/lib.rs index 0189b1db1..acc9fa72e 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -16,6 +16,8 @@ pub mod chacha; pub mod chacha_cuda; pub mod client; pub mod cluster_info_vote_listener; +#[macro_use] +pub mod contact_info; pub mod crds; pub mod crds_gossip; pub mod crds_gossip_error; @@ -23,8 +25,6 @@ pub mod crds_gossip_pull; pub mod crds_gossip_push; pub mod crds_value; #[macro_use] -pub mod contact_info; -#[macro_use] pub mod blocktree; pub mod blockstream; pub mod blockstream_service; diff --git a/core/src/rpc.rs b/core/src/rpc.rs index d220a533d..32e7ec71e 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -412,7 +412,6 @@ mod tests { use solana_sdk::hash::{hash, Hash}; use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::system_transaction::SystemTransaction; - use std::net::SocketAddr; use std::thread; fn start_rpc_handler_with_tx(pubkey: Pubkey) -> (MetaIoHandler, Meta, Hash, Keypair) { diff --git a/core/src/rpc_request.rs b/core/src/rpc_request.rs index e0c8030e9..32607e37b 100644 --- a/core/src/rpc_request.rs +++ b/core/src/rpc_request.rs @@ -203,7 +203,7 @@ mod tests { use jsonrpc_core::{Error, IoHandler, Params}; use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation, ServerBuilder}; use serde_json::Number; - use std::net::{Ipv4Addr, SocketAddr}; + use std::net::Ipv4Addr; use std::sync::mpsc::channel; use std::thread; diff --git a/replicator/src/main.rs b/replicator/src/main.rs index 00218ce48..467cae575 100644 --- a/replicator/src/main.rs +++ b/replicator/src/main.rs @@ -4,7 +4,7 @@ use solana::contact_info::ContactInfo; use solana::replicator::Replicator; use solana::socketaddr; use solana_sdk::signature::{read_keypair, Keypair, KeypairUtil}; -use std::net::{Ipv4Addr, SocketAddr}; +use std::net::Ipv4Addr; use std::process::exit; use std::sync::Arc;