-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
negentropy: fuzz testing, move accumulator to vector package.
- Loading branch information
Showing
13 changed files
with
205 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package negentropy_test | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/binary" | ||
"fmt" | ||
"math/rand/v2" | ||
"slices" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/nbd-wtf/go-nostr" | ||
"github.com/nbd-wtf/go-nostr/nip77/negentropy" | ||
"github.com/nbd-wtf/go-nostr/nip77/negentropy/storage/vector" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func FuzzWhatever(f *testing.F) { | ||
var sectors uint = 1 | ||
var sectorSizeAvg uint = 10 | ||
var pctChance uint = 5 | ||
var frameSizeLimit uint = 0 | ||
f.Add(sectors, sectorSizeAvg, pctChance, frameSizeLimit) | ||
f.Fuzz(func(t *testing.T, sectors uint, sectorSizeAvg uint, pctChance uint, frameSizeLimit uint) { | ||
rand := rand.New(rand.NewPCG(1, 1000)) | ||
sectorSizeAvg += 1 // prevent divide by zero | ||
frameSizeLimit += 4096 | ||
pctChance = pctChance % 100 | ||
|
||
// prepare the two sides | ||
s1 := vector.New() | ||
l1 := make([]string, 0, 500) | ||
neg1 := negentropy.New(s1, int(frameSizeLimit)) | ||
s2 := vector.New() | ||
l2 := make([]string, 0, 500) | ||
neg2 := negentropy.New(s2, int(frameSizeLimit)) | ||
|
||
start := 0 | ||
for s := 0; s < int(sectors); s++ { | ||
diff := rand.Uint() % sectorSizeAvg | ||
if rand.IntN(2) == 0 { | ||
diff = -diff | ||
} | ||
sectorSize := sectorSizeAvg + diff | ||
|
||
for i := 0; i < int(sectorSize); i++ { | ||
item := start + i | ||
|
||
rnd := sha256.Sum256(binary.BigEndian.AppendUint64(nil, uint64(item))) | ||
id := fmt.Sprintf("%x%056d", rnd[0:4], item) | ||
|
||
if rand.IntN(100) < int(pctChance) { | ||
s1.Insert(nostr.Timestamp(item), id) | ||
l1 = append(l1, id) | ||
} | ||
if rand.IntN(100) < int(pctChance) { | ||
id := fmt.Sprintf("%064d", item) | ||
s2.Insert(nostr.Timestamp(item), id) | ||
l2 = append(l2, id) | ||
} | ||
} | ||
|
||
start += int(sectorSize) | ||
} | ||
|
||
// fmt.Println(neg1.Name(), "initial", l1) | ||
// fmt.Println(neg2.Name(), "initial", l2) | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(2) | ||
go func() { | ||
for item := range neg1.Haves { | ||
// fmt.Println("have", item) | ||
l2 = append(l2, item) | ||
} | ||
wg.Done() | ||
}() | ||
go func() { | ||
for item := range neg1.HaveNots { | ||
// fmt.Println("havenot", item) | ||
l1 = append(l1, item) | ||
} | ||
wg.Done() | ||
}() | ||
|
||
msg := neg1.Start() | ||
next := neg2 | ||
|
||
for { | ||
var err error | ||
// fmt.Println(next.Name(), "handling", msg) | ||
msg, err = next.Reconcile(msg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if msg == "" { | ||
break | ||
} | ||
|
||
if next == neg1 { | ||
next = neg2 | ||
} else { | ||
next = neg1 | ||
} | ||
} | ||
|
||
wg.Wait() | ||
slices.Sort(l1) | ||
l1 = slices.Compact(l1) | ||
slices.Sort(l2) | ||
l2 = slices.Compact(l2) | ||
require.ElementsMatch(t, l1, l2) | ||
}) | ||
} |
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,49 @@ | ||
package vector | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/binary" | ||
"encoding/hex" | ||
|
||
"github.com/nbd-wtf/go-nostr/nip77/negentropy" | ||
) | ||
|
||
type Accumulator struct { | ||
Buf [32 + 8]byte // leave 8 bytes at the end as a slack for use in GetFingerprint append() | ||
} | ||
|
||
func (acc *Accumulator) Reset() { | ||
for i := 0; i < 32; i++ { | ||
acc.Buf[i] = 0 | ||
} | ||
} | ||
|
||
func (acc *Accumulator) AddAccumulator(other Accumulator) { | ||
acc.AddBytes(other.Buf[:32]) | ||
} | ||
|
||
func (acc *Accumulator) AddBytes(other []byte) { | ||
var currCarry, nextCarry uint32 | ||
|
||
for i := 0; i < 8; i++ { | ||
offset := i * 4 | ||
orig := binary.LittleEndian.Uint32(acc.Buf[offset:]) | ||
otherV := binary.LittleEndian.Uint32(other[offset:]) | ||
|
||
next := orig + currCarry + otherV | ||
if next < orig || next < otherV { | ||
nextCarry = 1 | ||
} | ||
|
||
binary.LittleEndian.PutUint32(acc.Buf[offset:32], next&0xFFFFFFFF) | ||
currCarry = nextCarry | ||
nextCarry = 0 | ||
} | ||
} | ||
|
||
func (acc *Accumulator) GetFingerprint(n int) string { | ||
input := acc.Buf[:32] | ||
input = append(input, negentropy.EncodeVarInt(n)...) | ||
hash := sha256.Sum256(input) | ||
return hex.EncodeToString(hash[:negentropy.FingerprintSize]) | ||
} |
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,5 @@ | ||
go test fuzz v1 | ||
uint(165) | ||
uint(108) | ||
uint(72) | ||
uint(54) |
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,5 @@ | ||
go test fuzz v1 | ||
uint(26) | ||
uint(0) | ||
uint(58) | ||
uint(70) |
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,5 @@ | ||
go test fuzz v1 | ||
uint(1) | ||
uint(17) | ||
uint(5) | ||
uint(4044) |
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,5 @@ | ||
go test fuzz v1 | ||
uint(182) | ||
uint(303) | ||
uint(75) | ||
uint(25) |
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,5 @@ | ||
go test fuzz v1 | ||
uint(17) | ||
uint(17) | ||
uint(39) | ||
uint(4115) |
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