forked from cerc-io/plugeth
cmd, les, tests: remove light client code (#28586)
* cmd, les, tests: remove light client code This commit removes the light client (LES) code. Since the merge the light client has been broken and it is hard to maintain it alongside the normal client. We decided it would be best to remove it for now and maybe rework and reintroduce it in the future. * cmd, eth: remove some more mentions of light mode * cmd: re-add flags and mark as deprecated * cmd: warn the user about deprecated flags * eth: better error message
This commit is contained in:
@@ -1,411 +0,0 @@
|
||||
// Copyright 2021 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 les
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/core/txpool"
|
||||
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
l "github.com/ethereum/go-ethereum/les"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
)
|
||||
|
||||
var (
|
||||
bankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||
bankAddr = crypto.PubkeyToAddress(bankKey.PublicKey)
|
||||
bankFunds = new(big.Int).Mul(big.NewInt(100), big.NewInt(params.Ether))
|
||||
|
||||
testChainLen = 256
|
||||
testContractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056")
|
||||
|
||||
chain *core.BlockChain
|
||||
addresses []common.Address
|
||||
txHashes []common.Hash
|
||||
|
||||
chtTrie *trie.Trie
|
||||
bloomTrie *trie.Trie
|
||||
chtKeys [][]byte
|
||||
bloomKeys [][]byte
|
||||
)
|
||||
|
||||
func makechain() (bc *core.BlockChain, addresses []common.Address, txHashes []common.Hash) {
|
||||
gspec := &core.Genesis{
|
||||
Config: params.TestChainConfig,
|
||||
Alloc: core.GenesisAlloc{bankAddr: {Balance: bankFunds}},
|
||||
GasLimit: 100000000,
|
||||
}
|
||||
signer := types.HomesteadSigner{}
|
||||
_, blocks, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), testChainLen,
|
||||
func(i int, gen *core.BlockGen) {
|
||||
var (
|
||||
tx *types.Transaction
|
||||
addr common.Address
|
||||
)
|
||||
nonce := uint64(i)
|
||||
if i%4 == 0 {
|
||||
tx, _ = types.SignTx(types.NewContractCreation(nonce, big.NewInt(0), 200000, big.NewInt(params.GWei), testContractCode), signer, bankKey)
|
||||
addr = crypto.CreateAddress(bankAddr, nonce)
|
||||
} else {
|
||||
addr = common.BigToAddress(big.NewInt(int64(i)))
|
||||
tx, _ = types.SignTx(types.NewTransaction(nonce, addr, big.NewInt(10000), params.TxGas, big.NewInt(params.GWei), nil), signer, bankKey)
|
||||
}
|
||||
gen.AddTx(tx)
|
||||
addresses = append(addresses, addr)
|
||||
txHashes = append(txHashes, tx.Hash())
|
||||
})
|
||||
bc, _ = core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
|
||||
if _, err := bc.InsertChain(blocks); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func makeTries() (chtTrie *trie.Trie, bloomTrie *trie.Trie, chtKeys, bloomKeys [][]byte) {
|
||||
chtTrie = trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase(), trie.HashDefaults))
|
||||
bloomTrie = trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase(), trie.HashDefaults))
|
||||
for i := 0; i < testChainLen; i++ {
|
||||
// The element in CHT is <big-endian block number> -> <block hash>
|
||||
key := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(key, uint64(i+1))
|
||||
chtTrie.MustUpdate(key, []byte{0x1, 0xf})
|
||||
chtKeys = append(chtKeys, key)
|
||||
|
||||
// The element in Bloom trie is <2 byte bit index> + <big-endian block number> -> bloom
|
||||
key2 := make([]byte, 10)
|
||||
binary.BigEndian.PutUint64(key2[2:], uint64(i+1))
|
||||
bloomTrie.MustUpdate(key2, []byte{0x2, 0xe})
|
||||
bloomKeys = append(bloomKeys, key2)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
chain, addresses, txHashes = makechain()
|
||||
chtTrie, bloomTrie, chtKeys, bloomKeys = makeTries()
|
||||
}
|
||||
|
||||
type fuzzer struct {
|
||||
chain *core.BlockChain
|
||||
pool *txpool.TxPool
|
||||
|
||||
chainLen int
|
||||
addresses []common.Address
|
||||
txs []common.Hash
|
||||
nonce uint64
|
||||
|
||||
chtKeys [][]byte
|
||||
bloomKeys [][]byte
|
||||
chtTrie *trie.Trie
|
||||
bloomTrie *trie.Trie
|
||||
|
||||
input io.Reader
|
||||
exhausted bool
|
||||
}
|
||||
|
||||
func newFuzzer(input []byte) *fuzzer {
|
||||
pool := legacypool.New(legacypool.DefaultConfig, chain)
|
||||
txpool, _ := txpool.New(new(big.Int).SetUint64(legacypool.DefaultConfig.PriceLimit), chain, []txpool.SubPool{pool})
|
||||
|
||||
return &fuzzer{
|
||||
chain: chain,
|
||||
chainLen: testChainLen,
|
||||
addresses: addresses,
|
||||
txs: txHashes,
|
||||
chtTrie: chtTrie,
|
||||
bloomTrie: bloomTrie,
|
||||
chtKeys: chtKeys,
|
||||
bloomKeys: bloomKeys,
|
||||
nonce: uint64(len(txHashes)),
|
||||
pool: txpool,
|
||||
input: bytes.NewReader(input),
|
||||
}
|
||||
}
|
||||
|
||||
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) randomByte() byte {
|
||||
d := f.read(1)
|
||||
return d[0]
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomBool() bool {
|
||||
d := f.read(1)
|
||||
return d[0]&1 == 1
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomInt(max int) int {
|
||||
if max == 0 {
|
||||
return 0
|
||||
}
|
||||
if max <= 256 {
|
||||
return int(f.randomByte()) % max
|
||||
}
|
||||
var a uint16
|
||||
if err := binary.Read(f.input, binary.LittleEndian, &a); err != nil {
|
||||
f.exhausted = true
|
||||
}
|
||||
return int(a % uint16(max))
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomX(max int) uint64 {
|
||||
var a uint16
|
||||
if err := binary.Read(f.input, binary.LittleEndian, &a); err != nil {
|
||||
f.exhausted = true
|
||||
}
|
||||
if a < 0x8000 {
|
||||
return uint64(a%uint16(max+1)) - 1
|
||||
}
|
||||
return (uint64(1)<<(a%64+1) - 1) & (uint64(a) * 343897772345826595)
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomBlockHash() common.Hash {
|
||||
h := f.chain.GetCanonicalHash(uint64(f.randomInt(3 * f.chainLen)))
|
||||
if h != (common.Hash{}) {
|
||||
return h
|
||||
}
|
||||
return common.BytesToHash(f.read(common.HashLength))
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomAddress() []byte {
|
||||
i := f.randomInt(3 * len(f.addresses))
|
||||
if i < len(f.addresses) {
|
||||
return f.addresses[i].Bytes()
|
||||
}
|
||||
return f.read(common.AddressLength)
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomCHTTrieKey() []byte {
|
||||
i := f.randomInt(3 * len(f.chtKeys))
|
||||
if i < len(f.chtKeys) {
|
||||
return f.chtKeys[i]
|
||||
}
|
||||
return f.read(8)
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomBloomTrieKey() []byte {
|
||||
i := f.randomInt(3 * len(f.bloomKeys))
|
||||
if i < len(f.bloomKeys) {
|
||||
return f.bloomKeys[i]
|
||||
}
|
||||
return f.read(10)
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomTxHash() common.Hash {
|
||||
i := f.randomInt(3 * len(f.txs))
|
||||
if i < len(f.txs) {
|
||||
return f.txs[i]
|
||||
}
|
||||
return common.BytesToHash(f.read(common.HashLength))
|
||||
}
|
||||
|
||||
func (f *fuzzer) BlockChain() *core.BlockChain {
|
||||
return f.chain
|
||||
}
|
||||
|
||||
func (f *fuzzer) TxPool() *txpool.TxPool {
|
||||
return f.pool
|
||||
}
|
||||
|
||||
func (f *fuzzer) ArchiveMode() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *fuzzer) AddTxsSync() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *fuzzer) GetHelperTrie(typ uint, index uint64) *trie.Trie {
|
||||
if typ == 0 {
|
||||
return f.chtTrie
|
||||
} else if typ == 1 {
|
||||
return f.bloomTrie
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type dummyMsg struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (d dummyMsg) Decode(val interface{}) error {
|
||||
return rlp.DecodeBytes(d.data, val)
|
||||
}
|
||||
|
||||
func (f *fuzzer) doFuzz(msgCode uint64, packet interface{}) {
|
||||
enc, err := rlp.EncodeToBytes(packet)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
version := f.randomInt(3) + 2 // [LES2, LES3, LES4]
|
||||
peer, closeFn := l.NewFuzzerPeer(version)
|
||||
defer closeFn()
|
||||
fn, _, _, err := l.Les3[msgCode].Handle(dummyMsg{enc})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fn(f, peer, func() bool { return true })
|
||||
}
|
||||
|
||||
func fuzz(input []byte) int {
|
||||
// We expect some large inputs
|
||||
if len(input) < 100 {
|
||||
return -1
|
||||
}
|
||||
f := newFuzzer(input)
|
||||
if f.exhausted {
|
||||
return -1
|
||||
}
|
||||
for !f.exhausted {
|
||||
switch f.randomInt(8) {
|
||||
case 0:
|
||||
req := &l.GetBlockHeadersPacket{
|
||||
Query: l.GetBlockHeadersData{
|
||||
Amount: f.randomX(l.MaxHeaderFetch + 1),
|
||||
Skip: f.randomX(10),
|
||||
Reverse: f.randomBool(),
|
||||
},
|
||||
}
|
||||
if f.randomBool() {
|
||||
req.Query.Origin.Hash = f.randomBlockHash()
|
||||
} else {
|
||||
req.Query.Origin.Number = uint64(f.randomInt(f.chainLen * 2))
|
||||
}
|
||||
f.doFuzz(l.GetBlockHeadersMsg, req)
|
||||
|
||||
case 1:
|
||||
req := &l.GetBlockBodiesPacket{Hashes: make([]common.Hash, f.randomInt(l.MaxBodyFetch+1))}
|
||||
for i := range req.Hashes {
|
||||
req.Hashes[i] = f.randomBlockHash()
|
||||
}
|
||||
f.doFuzz(l.GetBlockBodiesMsg, req)
|
||||
|
||||
case 2:
|
||||
req := &l.GetCodePacket{Reqs: make([]l.CodeReq, f.randomInt(l.MaxCodeFetch+1))}
|
||||
for i := range req.Reqs {
|
||||
req.Reqs[i] = l.CodeReq{
|
||||
BHash: f.randomBlockHash(),
|
||||
AccountAddress: f.randomAddress(),
|
||||
}
|
||||
}
|
||||
f.doFuzz(l.GetCodeMsg, req)
|
||||
|
||||
case 3:
|
||||
req := &l.GetReceiptsPacket{Hashes: make([]common.Hash, f.randomInt(l.MaxReceiptFetch+1))}
|
||||
for i := range req.Hashes {
|
||||
req.Hashes[i] = f.randomBlockHash()
|
||||
}
|
||||
f.doFuzz(l.GetReceiptsMsg, req)
|
||||
|
||||
case 4:
|
||||
req := &l.GetProofsPacket{Reqs: make([]l.ProofReq, f.randomInt(l.MaxProofsFetch+1))}
|
||||
for i := range req.Reqs {
|
||||
if f.randomBool() {
|
||||
req.Reqs[i] = l.ProofReq{
|
||||
BHash: f.randomBlockHash(),
|
||||
AccountAddress: f.randomAddress(),
|
||||
Key: f.randomAddress(),
|
||||
FromLevel: uint(f.randomX(3)),
|
||||
}
|
||||
} else {
|
||||
req.Reqs[i] = l.ProofReq{
|
||||
BHash: f.randomBlockHash(),
|
||||
Key: f.randomAddress(),
|
||||
FromLevel: uint(f.randomX(3)),
|
||||
}
|
||||
}
|
||||
}
|
||||
f.doFuzz(l.GetProofsV2Msg, req)
|
||||
|
||||
case 5:
|
||||
req := &l.GetHelperTrieProofsPacket{Reqs: make([]l.HelperTrieReq, f.randomInt(l.MaxHelperTrieProofsFetch+1))}
|
||||
for i := range req.Reqs {
|
||||
switch f.randomInt(3) {
|
||||
case 0:
|
||||
// Canonical hash trie
|
||||
req.Reqs[i] = l.HelperTrieReq{
|
||||
Type: 0,
|
||||
TrieIdx: f.randomX(3),
|
||||
Key: f.randomCHTTrieKey(),
|
||||
FromLevel: uint(f.randomX(3)),
|
||||
AuxReq: uint(2),
|
||||
}
|
||||
case 1:
|
||||
// Bloom trie
|
||||
req.Reqs[i] = l.HelperTrieReq{
|
||||
Type: 1,
|
||||
TrieIdx: f.randomX(3),
|
||||
Key: f.randomBloomTrieKey(),
|
||||
FromLevel: uint(f.randomX(3)),
|
||||
AuxReq: 0,
|
||||
}
|
||||
default:
|
||||
// Random trie
|
||||
req.Reqs[i] = l.HelperTrieReq{
|
||||
Type: 2,
|
||||
TrieIdx: f.randomX(3),
|
||||
Key: f.randomCHTTrieKey(),
|
||||
FromLevel: uint(f.randomX(3)),
|
||||
AuxReq: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
f.doFuzz(l.GetHelperTrieProofsMsg, req)
|
||||
|
||||
case 6:
|
||||
req := &l.SendTxPacket{Txs: make([]*types.Transaction, f.randomInt(l.MaxTxSend+1))}
|
||||
signer := types.HomesteadSigner{}
|
||||
for i := range req.Txs {
|
||||
var nonce uint64
|
||||
if f.randomBool() {
|
||||
nonce = uint64(f.randomByte())
|
||||
} else {
|
||||
nonce = f.nonce
|
||||
f.nonce += 1
|
||||
}
|
||||
req.Txs[i], _ = types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(10000), params.TxGas, big.NewInt(1000000000*int64(f.randomByte())), nil), signer, bankKey)
|
||||
}
|
||||
f.doFuzz(l.SendTxV2Msg, req)
|
||||
|
||||
case 7:
|
||||
req := &l.GetTxStatusPacket{Hashes: make([]common.Hash, f.randomInt(l.MaxTxStatus+1))}
|
||||
for i := range req.Hashes {
|
||||
req.Hashes[i] = f.randomTxHash()
|
||||
}
|
||||
f.doFuzz(l.GetTxStatusMsg, req)
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
// Copyright 2023 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 les
|
||||
|
||||
import "testing"
|
||||
|
||||
func Fuzz(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
fuzz(data)
|
||||
})
|
||||
}
|
||||
@@ -1,333 +0,0 @@
|
||||
// Copyright 2021 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 vflux
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"math"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/mclock"
|
||||
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
||||
"github.com/ethereum/go-ethereum/les/vflux"
|
||||
vfs "github.com/ethereum/go-ethereum/les/vflux/server"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
var (
|
||||
debugMode = false
|
||||
doLog = func(msg string, ctx ...interface{}) {
|
||||
if !debugMode {
|
||||
return
|
||||
}
|
||||
log.Info(msg, ctx...)
|
||||
}
|
||||
)
|
||||
|
||||
type fuzzer struct {
|
||||
peers [256]*clientPeer
|
||||
disconnectList []*clientPeer
|
||||
input io.Reader
|
||||
exhausted bool
|
||||
activeCount, activeCap uint64
|
||||
maxCount, maxCap uint64
|
||||
}
|
||||
|
||||
type clientPeer struct {
|
||||
fuzzer *fuzzer
|
||||
node *enode.Node
|
||||
freeID string
|
||||
timeout time.Duration
|
||||
|
||||
balance vfs.ConnectedBalance
|
||||
capacity uint64
|
||||
}
|
||||
|
||||
func (p *clientPeer) Node() *enode.Node {
|
||||
return p.node
|
||||
}
|
||||
|
||||
func (p *clientPeer) FreeClientId() string {
|
||||
return p.freeID
|
||||
}
|
||||
|
||||
func (p *clientPeer) InactiveAllowance() time.Duration {
|
||||
return p.timeout
|
||||
}
|
||||
|
||||
func (p *clientPeer) UpdateCapacity(newCap uint64, requested bool) {
|
||||
origin, originTotal := p.capacity, p.fuzzer.activeCap
|
||||
p.fuzzer.activeCap -= p.capacity
|
||||
if p.capacity != 0 {
|
||||
p.fuzzer.activeCount--
|
||||
}
|
||||
p.capacity = newCap
|
||||
p.fuzzer.activeCap += p.capacity
|
||||
if p.capacity != 0 {
|
||||
p.fuzzer.activeCount++
|
||||
}
|
||||
doLog("Update capacity", "peer", p.node.ID(), "origin", origin, "cap", newCap, "origintotal", originTotal, "total", p.fuzzer.activeCap, "requested", requested)
|
||||
}
|
||||
|
||||
func (p *clientPeer) Disconnect() {
|
||||
origin, originTotal := p.capacity, p.fuzzer.activeCap
|
||||
p.fuzzer.disconnectList = append(p.fuzzer.disconnectList, p)
|
||||
p.fuzzer.activeCap -= p.capacity
|
||||
if p.capacity != 0 {
|
||||
p.fuzzer.activeCount--
|
||||
}
|
||||
p.capacity = 0
|
||||
p.balance = nil
|
||||
doLog("Disconnect", "peer", p.node.ID(), "origin", origin, "origintotal", originTotal, "total", p.fuzzer.activeCap)
|
||||
}
|
||||
|
||||
func newFuzzer(input []byte) *fuzzer {
|
||||
f := &fuzzer{
|
||||
input: bytes.NewReader(input),
|
||||
}
|
||||
for i := range f.peers {
|
||||
f.peers[i] = &clientPeer{
|
||||
fuzzer: f,
|
||||
node: enode.SignNull(new(enr.Record), enode.ID{byte(i)}),
|
||||
freeID: string([]byte{byte(i)}),
|
||||
timeout: f.randomDelay(),
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
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) randomByte() byte {
|
||||
d := f.read(1)
|
||||
return d[0]
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomBool() bool {
|
||||
d := f.read(1)
|
||||
return d[0]&1 == 1
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomInt(max int) int {
|
||||
if max == 0 {
|
||||
return 0
|
||||
}
|
||||
if max <= 256 {
|
||||
return int(f.randomByte()) % max
|
||||
}
|
||||
var a uint16
|
||||
if err := binary.Read(f.input, binary.LittleEndian, &a); err != nil {
|
||||
f.exhausted = true
|
||||
}
|
||||
return int(a % uint16(max))
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomTokenAmount(signed bool) int64 {
|
||||
x := uint64(f.randomInt(65000))
|
||||
x = x * x * x * x
|
||||
|
||||
if signed && (x&1) == 1 {
|
||||
if x <= math.MaxInt64 {
|
||||
return -int64(x)
|
||||
}
|
||||
return math.MinInt64
|
||||
}
|
||||
if x <= math.MaxInt64 {
|
||||
return int64(x)
|
||||
}
|
||||
return math.MaxInt64
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomDelay() time.Duration {
|
||||
delay := f.randomByte()
|
||||
if delay < 128 {
|
||||
return time.Duration(delay) * time.Second
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (f *fuzzer) randomFactors() vfs.PriceFactors {
|
||||
return vfs.PriceFactors{
|
||||
TimeFactor: float64(f.randomByte()) / 25500,
|
||||
CapacityFactor: float64(f.randomByte()) / 255,
|
||||
RequestFactor: float64(f.randomByte()) / 255,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fuzzer) connectedBalanceOp(balance vfs.ConnectedBalance, id enode.ID) {
|
||||
switch f.randomInt(3) {
|
||||
case 0:
|
||||
cost := uint64(f.randomTokenAmount(false))
|
||||
balance.RequestServed(cost)
|
||||
doLog("Serve request cost", "id", id, "amount", cost)
|
||||
case 1:
|
||||
posFactor, negFactor := f.randomFactors(), f.randomFactors()
|
||||
balance.SetPriceFactors(posFactor, negFactor)
|
||||
doLog("Set price factor", "pos", posFactor, "neg", negFactor)
|
||||
case 2:
|
||||
balance.GetBalance()
|
||||
balance.GetRawBalance()
|
||||
balance.GetPriceFactors()
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fuzzer) atomicBalanceOp(balance vfs.AtomicBalanceOperator, id enode.ID) {
|
||||
switch f.randomInt(3) {
|
||||
case 0:
|
||||
amount := f.randomTokenAmount(true)
|
||||
balance.AddBalance(amount)
|
||||
doLog("Add balance", "id", id, "amount", amount)
|
||||
case 1:
|
||||
pos, neg := uint64(f.randomTokenAmount(false)), uint64(f.randomTokenAmount(false))
|
||||
balance.SetBalance(pos, neg)
|
||||
doLog("Set balance", "id", id, "pos", pos, "neg", neg)
|
||||
case 2:
|
||||
balance.GetBalance()
|
||||
balance.GetRawBalance()
|
||||
balance.GetPriceFactors()
|
||||
}
|
||||
}
|
||||
|
||||
func fuzzClientPool(input []byte) int {
|
||||
if len(input) > 10000 {
|
||||
return -1
|
||||
}
|
||||
f := newFuzzer(input)
|
||||
if f.exhausted {
|
||||
return 0
|
||||
}
|
||||
clock := &mclock.Simulated{}
|
||||
db := memorydb.New()
|
||||
pool := vfs.NewClientPool(db, 10, f.randomDelay(), clock, func() bool { return true })
|
||||
pool.Start()
|
||||
defer pool.Stop()
|
||||
|
||||
count := 0
|
||||
for !f.exhausted && count < 1000 {
|
||||
count++
|
||||
switch f.randomInt(11) {
|
||||
case 0:
|
||||
i := int(f.randomByte())
|
||||
f.peers[i].balance = pool.Register(f.peers[i])
|
||||
doLog("Register peer", "id", f.peers[i].node.ID())
|
||||
case 1:
|
||||
i := int(f.randomByte())
|
||||
f.peers[i].Disconnect()
|
||||
doLog("Disconnect peer", "id", f.peers[i].node.ID())
|
||||
case 2:
|
||||
f.maxCount = uint64(f.randomByte())
|
||||
f.maxCap = uint64(f.randomByte())
|
||||
f.maxCap *= f.maxCap
|
||||
|
||||
count, cap := pool.Limits()
|
||||
pool.SetLimits(f.maxCount, f.maxCap)
|
||||
doLog("Set limits", "maxcount", f.maxCount, "maxcap", f.maxCap, "origincount", count, "oricap", cap)
|
||||
case 3:
|
||||
bias := f.randomDelay()
|
||||
pool.SetConnectedBias(f.randomDelay())
|
||||
doLog("Set connection bias", "bias", bias)
|
||||
case 4:
|
||||
pos, neg := f.randomFactors(), f.randomFactors()
|
||||
pool.SetDefaultFactors(pos, neg)
|
||||
doLog("Set default factors", "pos", pos, "neg", neg)
|
||||
case 5:
|
||||
pos, neg := uint64(f.randomInt(50000)), uint64(f.randomInt(50000))
|
||||
pool.SetExpirationTCs(pos, neg)
|
||||
doLog("Set expiration constants", "pos", pos, "neg", neg)
|
||||
case 6:
|
||||
var (
|
||||
index = f.randomByte()
|
||||
reqCap = uint64(f.randomByte())
|
||||
bias = f.randomDelay()
|
||||
requested = f.randomBool()
|
||||
)
|
||||
pool.SetCapacity(f.peers[index].node, reqCap, bias, requested)
|
||||
doLog("Set capacity", "id", f.peers[index].node.ID(), "reqcap", reqCap, "bias", bias, "requested", requested)
|
||||
case 7:
|
||||
index := f.randomByte()
|
||||
if balance := f.peers[index].balance; balance != nil {
|
||||
f.connectedBalanceOp(balance, f.peers[index].node.ID())
|
||||
}
|
||||
case 8:
|
||||
index := f.randomByte()
|
||||
pool.BalanceOperation(f.peers[index].node.ID(), f.peers[index].freeID, func(balance vfs.AtomicBalanceOperator) {
|
||||
count := f.randomInt(4)
|
||||
for i := 0; i < count; i++ {
|
||||
f.atomicBalanceOp(balance, f.peers[index].node.ID())
|
||||
}
|
||||
})
|
||||
case 9:
|
||||
pool.TotalTokenAmount()
|
||||
pool.GetExpirationTCs()
|
||||
pool.Active()
|
||||
pool.Limits()
|
||||
pool.GetPosBalanceIDs(f.peers[f.randomByte()].node.ID(), f.peers[f.randomByte()].node.ID(), f.randomInt(100))
|
||||
case 10:
|
||||
req := vflux.CapacityQueryReq{
|
||||
Bias: uint64(f.randomByte()),
|
||||
AddTokens: make([]vflux.IntOrInf, f.randomInt(vflux.CapacityQueryMaxLen+1)),
|
||||
}
|
||||
for i := range req.AddTokens {
|
||||
v := vflux.IntOrInf{Type: uint8(f.randomInt(4))}
|
||||
if v.Type < 2 {
|
||||
v.Value = *big.NewInt(f.randomTokenAmount(false))
|
||||
}
|
||||
req.AddTokens[i] = v
|
||||
}
|
||||
reqEnc, err := rlp.EncodeToBytes(&req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
p := int(f.randomByte())
|
||||
if p < len(reqEnc) {
|
||||
reqEnc[p] = f.randomByte()
|
||||
}
|
||||
pool.Handle(f.peers[f.randomByte()].node.ID(), f.peers[f.randomByte()].freeID, vflux.CapacityQueryName, reqEnc)
|
||||
}
|
||||
|
||||
for _, peer := range f.disconnectList {
|
||||
pool.Unregister(peer)
|
||||
doLog("Unregister peer", "id", peer.node.ID())
|
||||
}
|
||||
f.disconnectList = nil
|
||||
if d := f.randomDelay(); d > 0 {
|
||||
clock.Run(d)
|
||||
}
|
||||
doLog("Clientpool stats in fuzzer", "count", f.activeCap, "maxcount", f.maxCount, "cap", f.activeCap, "maxcap", f.maxCap)
|
||||
activeCount, activeCap := pool.Active()
|
||||
doLog("Clientpool stats in pool", "count", activeCount, "cap", activeCap)
|
||||
if activeCount != f.activeCount || activeCap != f.activeCap {
|
||||
panic(nil)
|
||||
}
|
||||
if f.activeCount > f.maxCount || f.activeCap > f.maxCap {
|
||||
panic(nil)
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
// Copyright 2023 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 vflux
|
||||
|
||||
import "testing"
|
||||
|
||||
func FuzzClientPool(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
fuzzClientPool(data)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user