Commit 146e1744 authored by Steven Allen's avatar Steven Allen

feat: add kbucket prefix map

parent 3752ea01
...@@ -108,3 +108,5 @@ func (b *Bucket) Split(cpl int, target ID) *Bucket { ...@@ -108,3 +108,5 @@ func (b *Bucket) Split(cpl int, target ID) *Bucket {
} }
return newbuck return newbuck
} }
//go:generate go run ./generate
This source diff could not be displayed because it is too large. You can view the blob instead.
package main
import (
"crypto/sha256"
"encoding/binary"
"fmt"
"os"
"strings"
mh "github.com/multiformats/go-multihash"
)
const bits = 16
const target = 1 << bits
const idLen = 32 + 2
func main() {
pkg := os.Getenv("GOPACKAGE")
file := os.Getenv("GOFILE")
targetFile := strings.TrimSuffix(file, ".go") + "_prefixmap.go"
ids := new([target]uint64)
found := new([target]bool)
count := int32(0)
out := make([]byte, 32)
inp := [idLen]byte{mh.SHA2_256, 32}
hasher := sha256.New()
for i := uint64(0); count < target; i++ {
binary.BigEndian.PutUint64(inp[idLen-8:], i)
hasher.Write(inp[:])
out = hasher.Sum(out[:0])
hasher.Reset()
prefix := binary.BigEndian.Uint32(out) >> (32 - bits)
if !found[prefix] {
found[prefix] = true
ids[prefix] = i
count++
}
}
f, err := os.Create(targetFile)
if err != nil {
panic(err)
}
printf := func(s string, args ...interface{}) {
_, err := fmt.Fprintf(f, s, args...)
if err != nil {
panic(err)
}
}
printf("package %s\n\n", pkg)
printf("// Code generated by generate/generate_map.go DO NOT EDIT\n")
printf("var keyPrefixMap = [...]uint32{")
for i, j := range ids[:] {
if i%16 == 0 {
printf("\n\t")
} else {
printf(" ")
}
printf("%d,", j)
}
printf("\n}")
f.Close()
}
...@@ -6,4 +6,5 @@ require ( ...@@ -6,4 +6,5 @@ require (
github.com/libp2p/go-libp2p-core v0.0.1 github.com/libp2p/go-libp2p-core v0.0.1
github.com/libp2p/go-libp2p-peerstore v0.1.0 github.com/libp2p/go-libp2p-peerstore v0.1.0
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16 github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16
github.com/multiformats/go-multihash v0.0.1
) )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment