erigon-pulse/tests/fuzzers/difficulty/difficulty-fuzz.go
ledgerwatch 12cde41772
Aleut support (Eip1559) (#1704)
* Where I am at

* Refactoring of transaction types

* More refactoring

* Use Homested signer in rpc daemon

* Unified signer

* Continue unified signer

* A bit more

* Fixes and down the rabbit hole...

* More tx pool fixes

* More refactoring fixes

* More fixes'

* more fixes

* More fixes

* More compile fixes

* More RLP hand-writing

* Finish RLP encoding/decoding of transactions

* Fixes to header encoding, start on protocol packets

* Transaction decoding

* Use DecodeTransaction function

* Decoding BlockBodyPacket

* Encode and decode for pool txs

* Start fixing tests

* Introduce SigningHash

* Fixes to SignHash

* RLP encoding fixes

* Fixes for encoding/decoding

* More test fixes

* Fix more tests

* More test fixes

* More test fixes

* Fix core tests

* More fixes for signer

* Fix for tx

* Fixes to string encoding/size

* Fix eip2930 test

* Fix rest of ./tests

* More fixes

* Fix compilation

* More test fixes

* More test fixes

* Test fixes

* More fixes

* Reuse EncodingSize in EncodeRLP for accessList

* Rearrange things in dynamic fee tx

* Add MarshalBinary

* More fixes

* Make V,R,S non-pointers

* More NPE fixes

* More fixes

* Receipt fixes

* Fix core/types

* Fix ./eth

* More compile fixes for tests

* More test fixes

* More test fixes

* Try to see lint errors better

* Try to see lint errors better

* Fix lint

* Debugging eip1559 test

* Fix TestEIP1559Transition test

* Fix NewBlockPacket encoding/decoding

* Fix calculation of TxHash

* Fix perf problem with senders

* Update aleut config values

* Try adding static peers

* Add staticpeers to defaul flags

* Change aleut networkID

* Fix test

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
2021-04-22 18:11:37 +01:00

148 lines
4.3 KiB
Go

// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package difficulty
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math/big"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/consensus/ethash"
"github.com/ledgerwatch/turbo-geth/core/types"
)
type fuzzer struct {
input io.Reader
exhausted bool
}
func (f *fuzzer) read(size int) []byte {
out := make([]byte, size)
if _, err := f.input.Read(out); err != nil {
f.exhausted = true
}
return out
}
func (f *fuzzer) readSlice(min, max int) []byte {
var a uint16
//nolint:errcheck
binary.Read(f.input, binary.LittleEndian, &a)
size := min + int(a)%(max-min)
out := make([]byte, size)
if _, err := f.input.Read(out); err != nil {
f.exhausted = true
}
return out
}
func (f *fuzzer) readUint64(min, max uint64) uint64 {
if min == max {
return min
}
var a uint64
if err := binary.Read(f.input, binary.LittleEndian, &a); err != nil {
f.exhausted = true
}
a = min + a%(max-min)
return a
}
func (f *fuzzer) readBool() bool {
return f.read(1)[0]&0x1 == 0
}
// The function must return
// 1 if the fuzzer should increase priority of the
// given input during subsequent fuzzing (for example, the input is lexically
// correct and was parsed successfully);
// -1 if the input must not be added to corpus even if gives new coverage; and
// 0 otherwise
// other values are reserved for future use.
func Fuzz(data []byte) int {
f := fuzzer{
input: bytes.NewReader(data),
exhausted: false,
}
return f.fuzz()
}
var minDifficulty = big.NewInt(0x2000)
type calculator func(time uint64, parentTime uint64, parentDifficulty *big.Int, parentNumber uint64, parentUncleHash common.Hash) *big.Int
type calculatorU256 func(time uint64, parent *types.Header) *big.Int
func (f *fuzzer) fuzz() int {
// A parent header
header := &types.Header{}
if f.readBool() {
header.UncleHash = types.EmptyUncleHash
}
// Difficulty can range between 0x2000 (2 bytes) and up to 32 bytes
{
diff := new(big.Int).SetBytes(f.readSlice(2, 32))
if diff.Cmp(minDifficulty) < 0 {
diff.Set(minDifficulty)
}
header.Difficulty = diff
}
// Number can range between 0 and up to 32 bytes (but not so that the child exceeds it)
{
// However, if we use astronomic numbers, then the bomb exp karatsuba calculation
// in the legacy methods)
// times out, so we limit it to fit within reasonable bounds
number := new(big.Int).SetBytes(f.readSlice(0, 4)) // 4 bytes: 32 bits: block num max 4 billion
header.Number = number
}
// Both parent and child time must fit within uint64
var time uint64
{
childTime := f.readUint64(1, 0xFFFFFFFFFFFFFFFF)
//fmt.Printf("childTime: %x\n",childTime)
delta := f.readUint64(1, childTime)
//fmt.Printf("delta: %v\n", delta)
pTime := childTime - delta
header.Time = pTime
time = childTime
}
// Bomb delay will never exceed uint64
bombDelay := f.readUint64(1, 0xFFFFFFFFFFFFFFFe)
if f.exhausted {
return 0
}
for i, pair := range []struct {
bigFn calculator
u256Fn calculatorU256
}{
{ethash.FrontierDifficultyCalulator, ethash.CalcDifficultyFrontierU256},
{ethash.HomesteadDifficultyCalulator, ethash.CalcDifficultyHomesteadU256},
{ethash.DynamicDifficultyCalculator(bombDelay), ethash.MakeDifficultyCalculatorU256(bombDelay)},
} {
want := pair.bigFn(time, header.Time, header.Difficulty, header.Number.Uint64(), header.UncleHash)
have := pair.u256Fn(time, header)
if want.Cmp(have) != 0 {
panic(fmt.Sprintf("pair %d: want %x have %x\nparent.Number: %x\np.Time: %x\nc.Time: %x\nBombdelay: %v\n", i, want, have,
header.Number, header.Time, time, bombDelay))
}
}
return 1
}