-
Notifications
You must be signed in to change notification settings - Fork 233
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
Add explore state machine to expand population of routing table #934
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
86285a0
Improve query capabilities
iand d5f37fe
go mod tidy
iand ac0e316
Review feedback
iand a9f130e
go mod tidy
iand 13934e2
Move coord packages to internal (#933)
iand 41ec1f2
Add explore state machine to expand population of routing table
iand 9c57a11
Merge branch 'v2-develop' into v2-sm-explore
iand d57064e
Refactor schedule into separate type
iand cc04196
Add generation of random peer id for a given cpl
iand 5544ee3
go mod tidy
iand 6a314fb
Add prefixmap generator
iand 5e163c5
Use constants for various query ids
iand 85413a7
Merge branch 'v2-develop' into v2-sm-explore
iand 32f1920
go mod tidy
iand bc0ec84
Wire explore state machine into routing behaviour
iand 9927b5f
Remove some unnecessary conversions
iand b9cf374
PR review updates
iand 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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cplutil | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/binary" | ||
"fmt" | ||
|
||
mh "github.com/multiformats/go-multihash" | ||
|
||
"github.com/libp2p/go-libp2p-kad-dht/v2/kadt" | ||
) | ||
|
||
//go:generate go run ./gen.go | ||
|
||
// GenRandPeerID generates a random [kadt.PeerID] whose key has a common prefix length of exactly cpl with the supplied key. | ||
// Ported from go-libp2p-kbucket | ||
func GenRandPeerID(k kadt.Key, cpl int) (kadt.PeerID, error) { | ||
if cpl > 15 { | ||
return "", fmt.Errorf("cannot generate peer ID for Cpl greater than 15") | ||
} | ||
|
||
targetPrefix := prefix(k, cpl) | ||
|
||
// Convert to a known peer ID. | ||
key := keyPrefixMap[targetPrefix] | ||
id := [32 + 2]byte{mh.SHA2_256, 32} | ||
binary.BigEndian.PutUint32(id[2:], key) | ||
return kadt.PeerID(string(id[:])), nil | ||
} | ||
|
||
type keybit interface { | ||
Bit(i int) uint | ||
} | ||
|
||
// prefix generates random bits that have a common prefix length of exactly cpl with the supplied key. | ||
func prefix(k keybit, cpl int) uint16 { | ||
var p uint16 | ||
// copy the first cpl+1 bits so we can flip the last one | ||
for i := 0; i < cpl+1; i++ { | ||
bit := uint16(k.Bit(i)) << (15 - i) | ||
p |= bit | ||
} | ||
|
||
// flip the bit at cpl (cpl 5 means bits 0-4 must be the same) | ||
mask := uint16(1) << (15 - cpl) | ||
p ^= mask | ||
|
||
if cpl < 15 { | ||
// pad with random data | ||
var buf [2]byte | ||
_, _ = rand.Read(buf[:]) | ||
r := binary.BigEndian.Uint16(buf[:]) | ||
|
||
mask = (^uint16(0)) << (15 - cpl) | ||
p = (p & mask) | (r & ^mask) | ||
} | ||
return p | ||
} |
Oops, something went wrong.
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.
also:
// TODO: expose more config