-
Notifications
You must be signed in to change notification settings - Fork 985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support client mode in kademlia #2184
Closed
Closed
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
f8cf486
Adding config parameter to support client mode
1a042f8
Merge branch 'master' into kademlia-client-mode
3e759a0
example for showing peer interaction in kademlia
b8cbe62
finally able to discover other peers
fc218a0
Add `Mode` enum for kademlia client/server mode
e58e544
Remove confirmation code from `inject_fully_negotiated_inbound`
af1fcbd
Correct `client` value for network behaviour handler
c944ecb
Merge branch 'kademlia-client-mode' into kademlia-example
4be8fca
refactoring
e213350
Refactoring complete
e47148d
Add example for running kademlia in `client` mode
cdaa0a1
Merge commit with master
1a09907
removing .vscode
46167a1
Add better documentation and comments
8bf3ef4
replaced example with test in kad-behaviour
9ef4c4c
Changes to client mode test
c387f41
remove kad-example from cargo.toml
5b21774
Better checks for client mode test
5ea645e
Fix client mode test
aa6500f
Refactor kademlia client mode test
146405f
Rename to check variables
2cea417
Final fix for `client_mode` test.
54331e5
remove commented code
9d23741
Merge branch 'master' into kademlia-client-mode
b202c5b
Add some basic comments.
8234a17
Changes to test. Currently failing.
d5e6509
Changes to `client_mode` test. Currently failing.
06c1a30
Correct variable name.
d420fa6
Add checks to see if PUT and GET are working.
190c3dd
Add correct checks for PUT and GET operations.
ab97b10
Merge branch `master` into `kademlia-client-mode`
f3259b1
update prost dependencies
f308b0c
Replace `any` with `all` for server peer check.
8f33ba1
Merge branch `master` into `kademlia-client-mode`
520fb8e
Extra comments
d52c6af
Fix merge conflict with master
81eae5f
New test
f0c2fc3
Remove `allow_listening` logic.
f0bf7df
Fix gitignore
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1318,6 +1318,119 @@ fn network_behaviour_inject_address_change() { | |||||
); | ||||||
} | ||||||
|
||||||
#[futures_test::test] | ||||||
async fn client_mode_test() { | ||||||
let mut addrs = Vec::new(); | ||||||
let mut peers = Vec::new(); | ||||||
|
||||||
// Create server peers. | ||||||
let server1 = build_node(); | ||||||
addrs.push(server1.0); | ||||||
let mut server1 = server1.1; | ||||||
|
||||||
let server2 = build_node(); | ||||||
addrs.push(server2.0); | ||||||
let mut server2 = server2.1; | ||||||
|
||||||
// Create a client peer. | ||||||
let mut cfg = KademliaConfig::default(); | ||||||
cfg.set_mode(Mode::Server); | ||||||
let client = build_node_with_config(cfg); | ||||||
addrs.push(client.0); | ||||||
let mut client = client.1; | ||||||
|
||||||
// Fitler out peer Ids. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
peers.push(*server1.local_peer_id()); | ||||||
peers.push(*server2.local_peer_id()); | ||||||
peers.push(*client.local_peer_id()); | ||||||
|
||||||
// Connect server1 and server2. | ||||||
server1 | ||||||
.behaviour_mut() | ||||||
.add_address(&peers[1], addrs[1].clone()); | ||||||
|
||||||
server2 | ||||||
.behaviour_mut() | ||||||
.add_address(&peers[0], addrs[0].clone()); | ||||||
|
||||||
// Put a record from a server peer. | ||||||
let key = Key::new(&"123".to_string()); | ||||||
let record = Record::new(key.clone(), vec![4, 5, 6]); | ||||||
|
||||||
let put_qid = server1 | ||||||
.behaviour_mut() | ||||||
.put_record(record.clone(), Quorum::One) | ||||||
.unwrap(); | ||||||
|
||||||
loop { | ||||||
futures::select! { | ||||||
event = server1.next() => if let Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted { | ||||||
result: QueryResult::PutRecord(result), | ||||||
id: qid, | ||||||
.. | ||||||
})) = event { | ||||||
assert_eq!(qid, put_qid); | ||||||
match result { | ||||||
Ok(PutRecordOk { key: actual_key }) => { | ||||||
if actual_key == key { | ||||||
// Dial a server peer from the client peer. | ||||||
// client.dial_addr(addrs[1].clone()); | ||||||
client | ||||||
.behaviour_mut() | ||||||
.add_address(&peers[1], addrs[1].clone()); | ||||||
|
||||||
client.behaviour_mut().get_record(&key, Quorum::One); | ||||||
} | ||||||
}, | ||||||
Err(e) => panic!("{:?}", e), | ||||||
} | ||||||
}, | ||||||
_event = server2.next() => {}, | ||||||
event = client.next() => if let Some(SwarmEvent::Behaviour(KademliaEvent::OutboundQueryCompleted{ | ||||||
result: QueryResult::GetRecord(result), | ||||||
.. | ||||||
})) = event { | ||||||
match result { | ||||||
Ok(GetRecordOk { records: actual_record, .. }) => { | ||||||
assert_eq!(&record.key, &actual_record[0].record.key); | ||||||
|
||||||
let mut records = Vec::new(); | ||||||
|
||||||
// Read the routing table. | ||||||
for bucket in client.behaviour_mut().kbuckets() { | ||||||
for record in bucket.iter() { | ||||||
records.push(*record.node.key.preimage()); | ||||||
} | ||||||
} | ||||||
|
||||||
assert!(records.contains(&peers[1])); | ||||||
|
||||||
records.clear(); | ||||||
for bucket in server1.behaviour_mut().kbuckets() { | ||||||
for record in bucket.iter() { | ||||||
records.push(*record.node.key.preimage()); | ||||||
} | ||||||
} | ||||||
|
||||||
assert!(!records.contains(&peers[2])); | ||||||
|
||||||
records.clear(); | ||||||
for bucket in server2.behaviour_mut().kbuckets() { | ||||||
for record in bucket.iter() { | ||||||
records.push(*record.node.key.preimage()); | ||||||
} | ||||||
} | ||||||
|
||||||
assert!(!records.contains(&peers[2])); | ||||||
break; | ||||||
}, | ||||||
Err(e) => panic!("{:?}", e), | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn get_providers() { | ||||||
fn prop(key: record::Key) { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -46,6 +46,19 @@ pub const DEFAULT_PROTO_NAME: &[u8] = b"/ipfs/kad/1.0.0"; | |||||||
/// The default maximum size for a varint length-delimited packet. | ||||||||
pub const DEFAULT_MAX_PACKET_SIZE: usize = 16 * 1024; | ||||||||
|
||||||||
#[derive(Debug, Clone)] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
/// See [`crate::KademliaConfig::set_mode`]. | ||||||||
pub enum Mode { | ||||||||
Client, | ||||||||
Server, | ||||||||
} | ||||||||
|
||||||||
impl Default for Mode { | ||||||||
fn default() -> Self { | ||||||||
Mode::Server | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
/// Status of our connection to a node reported by the Kademlia protocol. | ||||||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] | ||||||||
pub enum KadConnectionType { | ||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit confused. Why is the client initialized with
Mode::Server
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, that's just a typing mistake on my side. Anyways, this test should fail when the client is in
Server
mode, which it is not. I have some doubts regarding the wayKademlia
works, which I have expressed here. Could you please help me out with those?