github.com/calmw/ethereum@v0.1.1/tests/fuzzers/stacktrie/trie_fuzzer.go (about)

     1  // Copyright 2020 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 stacktrie
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/binary"
    22  	"errors"
    23  	"fmt"
    24  	"hash"
    25  	"io"
    26  	"sort"
    27  
    28  	"github.com/calmw/ethereum/common"
    29  	"github.com/calmw/ethereum/core/rawdb"
    30  	"github.com/calmw/ethereum/core/types"
    31  	"github.com/calmw/ethereum/crypto"
    32  	"github.com/calmw/ethereum/ethdb"
    33  	"github.com/calmw/ethereum/trie"
    34  	"github.com/calmw/ethereum/trie/trienode"
    35  	"golang.org/x/crypto/sha3"
    36  )
    37  
    38  type fuzzer struct {
    39  	input     io.Reader
    40  	exhausted bool
    41  	debugging bool
    42  }
    43  
    44  func (f *fuzzer) read(size int) []byte {
    45  	out := make([]byte, size)
    46  	if _, err := f.input.Read(out); err != nil {
    47  		f.exhausted = true
    48  	}
    49  	return out
    50  }
    51  
    52  func (f *fuzzer) readSlice(min, max int) []byte {
    53  	var a uint16
    54  	binary.Read(f.input, binary.LittleEndian, &a)
    55  	size := min + int(a)%(max-min)
    56  	out := make([]byte, size)
    57  	if _, err := f.input.Read(out); err != nil {
    58  		f.exhausted = true
    59  	}
    60  	return out
    61  }
    62  
    63  // spongeDb is a dummy db backend which accumulates writes in a sponge
    64  type spongeDb struct {
    65  	sponge hash.Hash
    66  	debug  bool
    67  }
    68  
    69  func (s *spongeDb) Has(key []byte) (bool, error)             { panic("implement me") }
    70  func (s *spongeDb) Get(key []byte) ([]byte, error)           { return nil, errors.New("no such elem") }
    71  func (s *spongeDb) Delete(key []byte) error                  { panic("implement me") }
    72  func (s *spongeDb) NewBatch() ethdb.Batch                    { return &spongeBatch{s} }
    73  func (s *spongeDb) NewBatchWithSize(size int) ethdb.Batch    { return &spongeBatch{s} }
    74  func (s *spongeDb) NewSnapshot() (ethdb.Snapshot, error)     { panic("implement me") }
    75  func (s *spongeDb) Stat(property string) (string, error)     { panic("implement me") }
    76  func (s *spongeDb) Compact(start []byte, limit []byte) error { panic("implement me") }
    77  func (s *spongeDb) Close() error                             { return nil }
    78  
    79  func (s *spongeDb) Put(key []byte, value []byte) error {
    80  	if s.debug {
    81  		fmt.Printf("db.Put %x : %x\n", key, value)
    82  	}
    83  	s.sponge.Write(key)
    84  	s.sponge.Write(value)
    85  	return nil
    86  }
    87  func (s *spongeDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { panic("implement me") }
    88  
    89  // spongeBatch is a dummy batch which immediately writes to the underlying spongedb
    90  type spongeBatch struct {
    91  	db *spongeDb
    92  }
    93  
    94  func (b *spongeBatch) Put(key, value []byte) error {
    95  	b.db.Put(key, value)
    96  	return nil
    97  }
    98  func (b *spongeBatch) Delete(key []byte) error             { panic("implement me") }
    99  func (b *spongeBatch) ValueSize() int                      { return 100 }
   100  func (b *spongeBatch) Write() error                        { return nil }
   101  func (b *spongeBatch) Reset()                              {}
   102  func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil }
   103  
   104  type kv struct {
   105  	k, v []byte
   106  }
   107  type kvs []kv
   108  
   109  func (k kvs) Len() int {
   110  	return len(k)
   111  }
   112  
   113  func (k kvs) Less(i, j int) bool {
   114  	return bytes.Compare(k[i].k, k[j].k) < 0
   115  }
   116  
   117  func (k kvs) Swap(i, j int) {
   118  	k[j], k[i] = k[i], k[j]
   119  }
   120  
   121  // Fuzz is the fuzzing entry-point.
   122  // The function must return
   123  //
   124  //   - 1 if the fuzzer should increase priority of the
   125  //     given input during subsequent fuzzing (for example, the input is lexically
   126  //     correct and was parsed successfully);
   127  //   - -1 if the input must not be added to corpus even if gives new coverage; and
   128  //   - 0 otherwise
   129  //
   130  // other values are reserved for future use.
   131  func Fuzz(data []byte) int {
   132  	f := fuzzer{
   133  		input:     bytes.NewReader(data),
   134  		exhausted: false,
   135  	}
   136  	return f.fuzz()
   137  }
   138  
   139  func Debug(data []byte) int {
   140  	f := fuzzer{
   141  		input:     bytes.NewReader(data),
   142  		exhausted: false,
   143  		debugging: true,
   144  	}
   145  	return f.fuzz()
   146  }
   147  
   148  func (f *fuzzer) fuzz() int {
   149  	// This spongeDb is used to check the sequence of disk-db-writes
   150  	var (
   151  		spongeA = &spongeDb{sponge: sha3.NewLegacyKeccak256()}
   152  		dbA     = trie.NewDatabase(rawdb.NewDatabase(spongeA))
   153  		trieA   = trie.NewEmpty(dbA)
   154  		spongeB = &spongeDb{sponge: sha3.NewLegacyKeccak256()}
   155  		dbB     = trie.NewDatabase(rawdb.NewDatabase(spongeB))
   156  		trieB   = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
   157  			rawdb.WriteTrieNode(spongeB, owner, path, hash, blob, dbB.Scheme())
   158  		})
   159  		vals        kvs
   160  		useful      bool
   161  		maxElements = 10000
   162  		// operate on unique keys only
   163  		keys = make(map[string]struct{})
   164  	)
   165  	// Fill the trie with elements
   166  	for i := 0; !f.exhausted && i < maxElements; i++ {
   167  		k := f.read(32)
   168  		v := f.readSlice(1, 500)
   169  		if f.exhausted {
   170  			// If it was exhausted while reading, the value may be all zeroes,
   171  			// thus 'deletion' which is not supported on stacktrie
   172  			break
   173  		}
   174  		if _, present := keys[string(k)]; present {
   175  			// This key is a duplicate, ignore it
   176  			continue
   177  		}
   178  		keys[string(k)] = struct{}{}
   179  		vals = append(vals, kv{k: k, v: v})
   180  		trieA.MustUpdate(k, v)
   181  		useful = true
   182  	}
   183  	if !useful {
   184  		return 0
   185  	}
   186  	// Flush trie -> database
   187  	rootA, nodes := trieA.Commit(false)
   188  	if nodes != nil {
   189  		dbA.Update(rootA, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
   190  	}
   191  	// Flush memdb -> disk (sponge)
   192  	dbA.Commit(rootA, false)
   193  
   194  	// Stacktrie requires sorted insertion
   195  	sort.Sort(vals)
   196  	for _, kv := range vals {
   197  		if f.debugging {
   198  			fmt.Printf("{\"%#x\" , \"%#x\"} // stacktrie.Update\n", kv.k, kv.v)
   199  		}
   200  		trieB.MustUpdate(kv.k, kv.v)
   201  	}
   202  	rootB := trieB.Hash()
   203  	trieB.Commit()
   204  	if rootA != rootB {
   205  		panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootB))
   206  	}
   207  	sumA := spongeA.sponge.Sum(nil)
   208  	sumB := spongeB.sponge.Sum(nil)
   209  	if !bytes.Equal(sumA, sumB) {
   210  		panic(fmt.Sprintf("sequence differ: (trie) %x != %x (stacktrie)", sumA, sumB))
   211  	}
   212  
   213  	// Ensure all the nodes are persisted correctly
   214  	var (
   215  		nodeset = make(map[string][]byte) // path -> blob
   216  		trieC   = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) {
   217  			if crypto.Keccak256Hash(blob) != hash {
   218  				panic("invalid node blob")
   219  			}
   220  			if owner != (common.Hash{}) {
   221  				panic("invalid node owner")
   222  			}
   223  			nodeset[string(path)] = common.CopyBytes(blob)
   224  		})
   225  		checked int
   226  	)
   227  	for _, kv := range vals {
   228  		trieC.MustUpdate(kv.k, kv.v)
   229  	}
   230  	rootC, _ := trieC.Commit()
   231  	if rootA != rootC {
   232  		panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootC))
   233  	}
   234  	trieA, _ = trie.New(trie.TrieID(rootA), dbA)
   235  	iterA := trieA.NodeIterator(nil)
   236  	for iterA.Next(true) {
   237  		if iterA.Hash() == (common.Hash{}) {
   238  			if _, present := nodeset[string(iterA.Path())]; present {
   239  				panic("unexpected tiny node")
   240  			}
   241  			continue
   242  		}
   243  		nodeBlob, present := nodeset[string(iterA.Path())]
   244  		if !present {
   245  			panic("missing node")
   246  		}
   247  		if !bytes.Equal(nodeBlob, iterA.NodeBlob()) {
   248  			panic("node blob is not matched")
   249  		}
   250  		checked += 1
   251  	}
   252  	if checked != len(nodeset) {
   253  		panic("node number is not matched")
   254  	}
   255  	return 1
   256  }