-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ks-291: minor peer discovery optimization (#13439)
- Loading branch information
1 parent
77efaa2
commit 6aad45f
Showing
3 changed files
with
53 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package p2p | ||
|
||
import ( | ||
"encoding/binary" | ||
) | ||
|
||
// counter is a simple abstraction that can be used to generate unique peer group IDs. | ||
type counter struct { | ||
x uint64 | ||
} | ||
|
||
// Bytes returns the counter as a 32-byte array. | ||
func (g *counter) Bytes() [32]byte { | ||
var b [32]byte | ||
binary.BigEndian.PutUint64(b[24:], g.x) | ||
return b | ||
} | ||
|
||
// Inc increments the counter. | ||
func (g *counter) Inc() *counter { | ||
g.x++ | ||
return g | ||
} |
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,16 @@ | ||
package p2p | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_groupID(t *testing.T) { | ||
g := &counter{} | ||
assert.Equal(t, [32]byte{}, g.Bytes()) | ||
g.Inc() | ||
assert.Equal(t, [32]byte{31: 1}, g.Bytes()) | ||
g.Inc() | ||
assert.Equal(t, [32]byte{31: 2}, g.Bytes()) | ||
} |
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