-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add explore state machine to expand population of routing table (#934)
* Improve query capabilities * go mod tidy * Review feedback * go mod tidy * Move coord packages to internal (#933) * Move coord and kadt packages to internal * go mod tidy * go fmt * Move kadt out of internal and add RoutingTable interface * Add explore state machine to expand population of routing table * Refactor schedule into separate type * Add generation of random peer id for a given cpl * go mod tidy * Add prefixmap generator * Use constants for various query ids * go mod tidy * Wire explore state machine into routing behaviour * Remove some unnecessary conversions * PR review updates
- Loading branch information
Showing
19 changed files
with
5,471 additions
and
76 deletions.
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.