Skip to content

Commit

Permalink
refactor: move bin prefix logic to new file (#2468)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Sep 6, 2021
1 parent 1c0dee1 commit 2376885
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 84 deletions.
95 changes: 95 additions & 0 deletions pkg/topology/kademlia/binprefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package kademlia

import (
"math"
"math/bits"

"github.com/ethersphere/bee/pkg/swarm"
)

// generateCommonBinPrefixes generates the common bin prefixes
// used by the bin balancer.
func (k *Kad) generateCommonBinPrefixes() {
bitCombinationsCount := int(math.Pow(2, float64(k.bitSuffixLength)))
bitSuffixes := make([]uint8, bitCombinationsCount)

for i := 0; i < bitCombinationsCount; i++ {
bitSuffixes[i] = uint8(i)
}

addr := swarm.MustParseHexAddress(k.base.String())
addrBytes := addr.Bytes()
_ = addrBytes

binPrefixes := k.commonBinPrefixes

// copy base address
for i := range binPrefixes {
binPrefixes[i] = make([]swarm.Address, bitCombinationsCount)
}

for i := range binPrefixes {
for j := range binPrefixes[i] {
pseudoAddrBytes := make([]byte, len(k.base.Bytes()))
copy(pseudoAddrBytes, k.base.Bytes())
binPrefixes[i][j] = swarm.NewAddress(pseudoAddrBytes)
}
}

for i := range binPrefixes {
for j := range binPrefixes[i] {
pseudoAddrBytes := binPrefixes[i][j].Bytes()

if len(pseudoAddrBytes) < 1 {
continue
}

// flip first bit for bin
indexByte, posBit := i/8, i%8
if hasBit(bits.Reverse8(pseudoAddrBytes[indexByte]), uint8(posBit)) {
pseudoAddrBytes[indexByte] = bits.Reverse8(clearBit(bits.Reverse8(pseudoAddrBytes[indexByte]), uint8(posBit)))
} else {
pseudoAddrBytes[indexByte] = bits.Reverse8(setBit(bits.Reverse8(pseudoAddrBytes[indexByte]), uint8(posBit)))
}

// set pseudo suffix
bitSuffixPos := k.bitSuffixLength - 1
for l := i + 1; l < i+k.bitSuffixLength+1; l++ {
index, pos := l/8, l%8

if hasBit(bitSuffixes[j], uint8(bitSuffixPos)) {
pseudoAddrBytes[index] = bits.Reverse8(setBit(bits.Reverse8(pseudoAddrBytes[index]), uint8(pos)))
} else {
pseudoAddrBytes[index] = bits.Reverse8(clearBit(bits.Reverse8(pseudoAddrBytes[index]), uint8(pos)))
}

bitSuffixPos--
}

// clear rest of the bits
for l := i + k.bitSuffixLength + 1; l < len(pseudoAddrBytes)*8; l++ {
index, pos := l/8, l%8
pseudoAddrBytes[index] = bits.Reverse8(clearBit(bits.Reverse8(pseudoAddrBytes[index]), uint8(pos)))
}
}
}
}

// Clears the bit at pos in n.
func clearBit(n, pos uint8) uint8 {
mask := ^(uint8(1) << pos)
return n & mask
}

// Sets the bit at pos in the integer n.
func setBit(n, pos uint8) uint8 {
return n | 1<<pos
}

func hasBit(n, pos uint8) bool {
return n&(1<<pos) > 0
}
84 changes: 0 additions & 84 deletions pkg/topology/kademlia/kademlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
random "crypto/rand"
"encoding/json"
"errors"
"math"
"math/big"
"math/bits"
"sync"
"time"

Expand Down Expand Up @@ -151,88 +149,6 @@ func New(
return k
}

func (k *Kad) generateCommonBinPrefixes() {
bitCombinationsCount := int(math.Pow(2, float64(k.bitSuffixLength)))
bitSuffixes := make([]uint8, bitCombinationsCount)

for i := 0; i < bitCombinationsCount; i++ {
bitSuffixes[i] = uint8(i)
}

addr := swarm.MustParseHexAddress(k.base.String())
addrBytes := addr.Bytes()
_ = addrBytes

binPrefixes := k.commonBinPrefixes

// copy base address
for i := range binPrefixes {
binPrefixes[i] = make([]swarm.Address, bitCombinationsCount)
}

for i := range binPrefixes {
for j := range binPrefixes[i] {
pseudoAddrBytes := make([]byte, len(k.base.Bytes()))
copy(pseudoAddrBytes, k.base.Bytes())
binPrefixes[i][j] = swarm.NewAddress(pseudoAddrBytes)
}
}

for i := range binPrefixes {
for j := range binPrefixes[i] {
pseudoAddrBytes := binPrefixes[i][j].Bytes()

if len(pseudoAddrBytes) < 1 {
continue
}

// flip first bit for bin
indexByte, posBit := i/8, i%8
if hasBit(bits.Reverse8(pseudoAddrBytes[indexByte]), uint8(posBit)) {
pseudoAddrBytes[indexByte] = bits.Reverse8(clearBit(bits.Reverse8(pseudoAddrBytes[indexByte]), uint8(posBit)))
} else {
pseudoAddrBytes[indexByte] = bits.Reverse8(setBit(bits.Reverse8(pseudoAddrBytes[indexByte]), uint8(posBit)))
}

// set pseudo suffix
bitSuffixPos := k.bitSuffixLength - 1
for l := i + 1; l < i+k.bitSuffixLength+1; l++ {
index, pos := l/8, l%8

if hasBit(bitSuffixes[j], uint8(bitSuffixPos)) {
pseudoAddrBytes[index] = bits.Reverse8(setBit(bits.Reverse8(pseudoAddrBytes[index]), uint8(pos)))
} else {
pseudoAddrBytes[index] = bits.Reverse8(clearBit(bits.Reverse8(pseudoAddrBytes[index]), uint8(pos)))
}

bitSuffixPos--
}

// clear rest of the bits
for l := i + k.bitSuffixLength + 1; l < len(pseudoAddrBytes)*8; l++ {
index, pos := l/8, l%8
pseudoAddrBytes[index] = bits.Reverse8(clearBit(bits.Reverse8(pseudoAddrBytes[index]), uint8(pos)))
}
}
}
}

// Clears the bit at pos in n.
func clearBit(n, pos uint8) uint8 {
mask := ^(uint8(1) << pos)
return n & mask
}

// Sets the bit at pos in the integer n.
func setBit(n, pos uint8) uint8 {
return n | 1<<pos
}

func hasBit(n, pos uint8) bool {
return n&(1<<pos) > 0
}

// peerConnInfo groups necessary fields needed to create a connection.
type peerConnInfo struct {
po uint8
addr swarm.Address
Expand Down

0 comments on commit 2376885

Please sign in to comment.