erigon-pulse/txpool/pool_fuzz_test.go

155 lines
4.1 KiB
Go
Raw Normal View History

2021-07-30 07:10:58 +00:00
//go:build gofuzzbeta
2021-07-29 08:23:17 +00:00
// +build gofuzzbeta
package txpool
import (
2021-07-30 14:52:23 +00:00
"encoding/binary"
2021-07-29 08:23:17 +00:00
"testing"
)
// https://blog.golang.org/fuzz-beta
// golang.org/s/draft-fuzzing-design
//gotip doc testing
//gotip doc testing.F
//gotip doc testing.F.Add
//gotip doc testing.F.Fuzz
2021-07-29 08:33:22 +00:00
// gotip test -trimpath -v -fuzz=Fuzz -fuzztime=10s ./txpool
2021-07-30 14:52:23 +00:00
/*
2021-07-30 07:10:58 +00:00
func FuzzTwoQueue(f *testing.F) {
f.Add([]uint8{0b11000, 0b00101, 0b000111})
2021-07-30 09:24:20 +00:00
f.Add([]uint8{0b10101, 0b11110, 0b11101, 0b10001})
2021-07-30 07:10:58 +00:00
f.Fuzz(func(t *testing.T, in []uint8) {
t.Parallel()
2021-07-30 09:31:45 +00:00
for i := range in {
if in[i] > 0b11111 {
t.Skip()
}
}
2021-07-30 09:24:20 +00:00
assert := assert.New(t)
{
sub := NewSubPool()
for _, i := range in {
sub.Add(&MetaTx{SubPool: SubPoolMarker(i & 0b11111)})
}
assert.Equal(len(in), sub.best.Len())
assert.Equal(len(in), sub.worst.Len())
assert.Equal(len(in), sub.Len())
var prevBest *uint8
i := sub.Len()
for sub.Len() > 0 {
best := uint8(sub.Best().SubPool)
assert.Equal(best, uint8(sub.PopBest().SubPool))
if prevBest != nil {
assert.LessOrEqual(best, *prevBest)
}
prevBest = &best
i--
}
assert.Zero(i)
2021-07-30 09:26:56 +00:00
assert.Zero(sub.Len())
assert.Zero(sub.best.Len())
assert.Zero(sub.worst.Len())
2021-07-30 07:10:58 +00:00
}
2021-07-30 09:24:20 +00:00
{
sub := NewSubPool()
for _, i := range in {
sub.Add(&MetaTx{SubPool: SubPoolMarker(i & 0b11111)})
}
var prev *uint8
i := sub.Len()
for sub.Len() > 0 {
worst := uint8(sub.Worst().SubPool)
assert.Equal(worst, uint8(sub.PopWorst().SubPool))
if prev != nil {
assert.GreaterOrEqual(worst, *prev)
}
prev = &worst
i--
}
assert.Zero(i)
2021-07-30 09:26:56 +00:00
assert.Zero(sub.Len())
assert.Zero(sub.best.Len())
assert.Zero(sub.worst.Len())
2021-07-30 07:10:58 +00:00
}
})
}
2021-07-30 14:52:23 +00:00
*/
func FuzzPromoteStep2(f *testing.F) {
var nNonce = [8]byte{1}
var nAddr = [20]byte{1}
2021-07-30 07:10:58 +00:00
2021-07-30 14:52:23 +00:00
f.Add([]uint8{0b11111, 0b10001, 0b10101, 0b00001, 0b00000}, []uint8{0b11111, 0b10001, 0b10101, 0b00001, 0b00000}, []uint8{0b11111, 0b10001, 0b10101, 0b00001, 0b00000}, nNonce[:], nAddr[:])
f.Add([]uint8{0b11111}, []uint8{0b11111}, []uint8{0b11110, 0b0, 0b1010}, nNonce[:], nAddr[:])
f.Add([]uint8{0b11000, 0b00101, 0b000111}, []uint8{0b11000, 0b00101, 0b000111}, []uint8{0b11000, 0b00101, 0b000111}, nNonce[:], nAddr[:])
f.Fuzz(func(t *testing.T, s1, s2, s3 []uint8, nonce []byte, sender []byte) {
2021-07-29 08:23:17 +00:00
t.Parallel()
2021-07-30 14:52:23 +00:00
if len(nonce) == 0 || len(nonce)%8 != 0 || len(sender) == 0 || len(sender)%20 != 0 {
t.Skip()
}
2021-07-30 09:31:45 +00:00
for i := range s1 {
if s1[i] > 0b11111 {
t.Skip()
}
}
for i := range s2 {
if s2[i] > 0b11111 {
t.Skip()
}
}
for i := range s3 {
if s3[i] > 0b11111 {
t.Skip()
}
}
2021-07-30 14:52:23 +00:00
iNonce, iSenders := 0, 0
2021-07-30 07:10:58 +00:00
pending, baseFee, queued := NewSubPool(), NewSubPool(), NewSubPool()
2021-07-30 14:52:23 +00:00
var ss [20]byte
for _, s := range s1 {
copy(ss[:], sender[(iSenders*20)%len(sender):])
2021-07-31 05:43:29 +00:00
mt := &MetaTx{SubPool: SubPoolMarker(s & 0b11111), Tx: &TxSlot{nonce: binary.BigEndian.Uint64(nonce[(iNonce*8)%len(nonce):]), sender: ss}}
pending.Add(mt, PendingSubPool)
2021-07-30 14:52:23 +00:00
iNonce++
iSenders++
2021-07-29 08:23:17 +00:00
}
2021-07-30 14:52:23 +00:00
for _, s := range s2 {
copy(ss[:], sender[(iSenders*20)%len(sender):])
2021-07-31 05:43:29 +00:00
mt := &MetaTx{SubPool: SubPoolMarker(s & 0b11111), Tx: &TxSlot{nonce: binary.BigEndian.Uint64(nonce[(iNonce*8)%len(nonce):]), sender: ss}}
baseFee.Add(mt, BaseFeeSubPool)
2021-07-30 14:52:23 +00:00
iNonce++
iSenders++
2021-07-29 08:23:17 +00:00
}
2021-07-31 05:43:29 +00:00
for _, s := range s3 {
2021-07-30 14:52:23 +00:00
copy(ss[:], sender[(iSenders*20)%len(sender):])
2021-07-31 05:43:29 +00:00
mt := &MetaTx{SubPool: SubPoolMarker(s & 0b11111), Tx: &TxSlot{nonce: binary.BigEndian.Uint64(nonce[(iNonce*8)%len(nonce):]), sender: ss}}
queued.Add(mt, QueuedSubPool)
2021-07-30 14:52:23 +00:00
iNonce++
iSenders++
2021-07-29 08:23:17 +00:00
}
2021-07-30 07:10:58 +00:00
PromoteStep(pending, baseFee, queued)
2021-07-29 08:23:17 +00:00
2021-07-29 08:33:22 +00:00
best, worst := pending.Best(), pending.Worst()
2021-07-29 08:48:23 +00:00
_ = best
2021-07-30 09:24:20 +00:00
if worst != nil && worst.SubPool < 0b11110 {
2021-07-30 07:10:58 +00:00
t.Fatalf("pending worst too small %b, input: \n%x\n%x\n%x", worst.SubPool, s1, s2, s3)
2021-07-29 08:23:17 +00:00
}
2021-07-30 03:28:03 +00:00
best, worst = baseFee.Best(), baseFee.Worst()
_ = best
2021-07-30 09:24:20 +00:00
if worst != nil && worst.SubPool < 0b11100 {
2021-07-30 07:10:58 +00:00
t.Fatalf("baseFee worst too small %b, input: \n%x\n%x\n%x", worst.SubPool, s1, s2, s3)
2021-07-30 03:28:03 +00:00
}
2021-07-30 07:10:58 +00:00
best, worst = queued.Best(), queued.Worst()
2021-07-30 03:28:03 +00:00
_ = best
2021-07-30 09:24:20 +00:00
if worst != nil && worst.SubPool < 0b10000 {
2021-07-30 07:10:58 +00:00
t.Fatalf("queued worst too small %b, input: \n%x\n%x\n%x", worst.SubPool, s1, s2, s3)
2021-07-30 03:28:03 +00:00
}
2021-07-29 08:23:17 +00:00
})
}