github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/tests/fuzzers/trie/trie-fuzzer.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package trie
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/binary"
    22  	"fmt"
    23  
    24  	"github.com/kisexp/xdchain/common"
    25  	"github.com/kisexp/xdchain/ethdb/memorydb"
    26  	"github.com/kisexp/xdchain/trie"
    27  )
    28  
    29  // randTest performs random trie operations.
    30  // Instances of this test are created by Generate.
    31  type randTest []randTestStep
    32  
    33  type randTestStep struct {
    34  	op    int
    35  	key   []byte // for opUpdate, opDelete, opGet
    36  	value []byte // for opUpdate
    37  	err   error  // for debugging
    38  }
    39  
    40  type proofDb struct{}
    41  
    42  func (proofDb) Put(key []byte, value []byte) error {
    43  	return nil
    44  }
    45  
    46  func (proofDb) Delete(key []byte) error {
    47  	return nil
    48  }
    49  
    50  const (
    51  	opUpdate = iota
    52  	opDelete
    53  	opGet
    54  	opCommit
    55  	opHash
    56  	opReset
    57  	opItercheckhash
    58  	opProve
    59  	opMax // boundary value, not an actual op
    60  )
    61  
    62  type dataSource struct {
    63  	input  []byte
    64  	reader *bytes.Reader
    65  }
    66  
    67  func newDataSource(input []byte) *dataSource {
    68  	return &dataSource{
    69  		input, bytes.NewReader(input),
    70  	}
    71  }
    72  func (ds *dataSource) ReadByte() (byte, error) {
    73  	if b, err := ds.reader.ReadByte(); err != nil {
    74  		return 0, err
    75  	} else {
    76  		return b, nil
    77  	}
    78  }
    79  func (ds *dataSource) adaptReadByte() byte {
    80  	b, _ := ds.ReadByte()
    81  	return b
    82  }
    83  func (ds *dataSource) Read(buf []byte) (int, error) {
    84  	return ds.reader.Read(buf)
    85  }
    86  func (ds *dataSource) Ended() bool {
    87  	return ds.reader.Len() == 0
    88  }
    89  
    90  func Generate(input []byte) randTest {
    91  
    92  	var allKeys [][]byte
    93  	r := newDataSource(input)
    94  	genKey := func() []byte {
    95  
    96  		if len(allKeys) < 2 || r.adaptReadByte() < 0x0f {
    97  			// new key
    98  			key := make([]byte, r.adaptReadByte()%50)
    99  			r.Read(key)
   100  			allKeys = append(allKeys, key)
   101  			return key
   102  		}
   103  		// use existing key
   104  		return allKeys[int(r.adaptReadByte())%len(allKeys)]
   105  	}
   106  
   107  	var steps randTest
   108  
   109  	for i := 0; !r.Ended(); i++ {
   110  
   111  		step := randTestStep{op: int(r.adaptReadByte()) % opMax}
   112  		switch step.op {
   113  		case opUpdate:
   114  			step.key = genKey()
   115  			step.value = make([]byte, 8)
   116  			binary.BigEndian.PutUint64(step.value, uint64(i))
   117  		case opGet, opDelete, opProve:
   118  			step.key = genKey()
   119  		}
   120  		steps = append(steps, step)
   121  		if len(steps) > 500 {
   122  			break
   123  		}
   124  	}
   125  
   126  	return steps
   127  }
   128  
   129  // The function must return
   130  // 1 if the fuzzer should increase priority of the
   131  //    given input during subsequent fuzzing (for example, the input is lexically
   132  //    correct and was parsed successfully);
   133  // -1 if the input must not be added to corpus even if gives new coverage; and
   134  // 0  otherwise
   135  // other values are reserved for future use.
   136  func Fuzz(input []byte) int {
   137  	program := Generate(input)
   138  	if len(program) == 0 {
   139  		return 0
   140  	}
   141  	if err := runRandTest(program); err != nil {
   142  		panic(err)
   143  	}
   144  	return 1
   145  }
   146  
   147  func runRandTest(rt randTest) error {
   148  
   149  	triedb := trie.NewDatabase(memorydb.New())
   150  
   151  	tr, _ := trie.New(common.Hash{}, triedb)
   152  	values := make(map[string]string) // tracks content of the trie
   153  
   154  	for i, step := range rt {
   155  		switch step.op {
   156  		case opUpdate:
   157  			tr.Update(step.key, step.value)
   158  			values[string(step.key)] = string(step.value)
   159  		case opDelete:
   160  			tr.Delete(step.key)
   161  			delete(values, string(step.key))
   162  		case opGet:
   163  			v := tr.Get(step.key)
   164  			want := values[string(step.key)]
   165  			if string(v) != want {
   166  				rt[i].err = fmt.Errorf("mismatch for key 0x%x, got 0x%x want 0x%x", step.key, v, want)
   167  			}
   168  		case opCommit:
   169  			_, rt[i].err = tr.Commit(nil)
   170  		case opHash:
   171  			tr.Hash()
   172  		case opReset:
   173  			hash, err := tr.Commit(nil)
   174  			if err != nil {
   175  				return err
   176  			}
   177  			newtr, err := trie.New(hash, triedb)
   178  			if err != nil {
   179  				return err
   180  			}
   181  			tr = newtr
   182  		case opItercheckhash:
   183  			checktr, _ := trie.New(common.Hash{}, triedb)
   184  			it := trie.NewIterator(tr.NodeIterator(nil))
   185  			for it.Next() {
   186  				checktr.Update(it.Key, it.Value)
   187  			}
   188  			if tr.Hash() != checktr.Hash() {
   189  				return fmt.Errorf("hash mismatch in opItercheckhash")
   190  			}
   191  		case opProve:
   192  			rt[i].err = tr.Prove(step.key, 0, proofDb{})
   193  		}
   194  		// Abort the test on error.
   195  		if rt[i].err != nil {
   196  			return rt[i].err
   197  		}
   198  	}
   199  	return nil
   200  }