github.com/ethereum/go-ethereum@v1.16.1/core/state/snapshot/difflayer_test.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 snapshot
    18  
    19  import (
    20  	"bytes"
    21  	crand "crypto/rand"
    22  	"math/rand"
    23  	"testing"
    24  
    25  	"github.com/VictoriaMetrics/fastcache"
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/crypto"
    28  	"github.com/ethereum/go-ethereum/ethdb/memorydb"
    29  )
    30  
    31  func copyAccounts(accounts map[common.Hash][]byte) map[common.Hash][]byte {
    32  	copy := make(map[common.Hash][]byte)
    33  	for hash, blob := range accounts {
    34  		copy[hash] = blob
    35  	}
    36  	return copy
    37  }
    38  
    39  func copyStorage(storage map[common.Hash]map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {
    40  	copy := make(map[common.Hash]map[common.Hash][]byte)
    41  	for accHash, slots := range storage {
    42  		copy[accHash] = make(map[common.Hash][]byte)
    43  		for slotHash, blob := range slots {
    44  			copy[accHash][slotHash] = blob
    45  		}
    46  	}
    47  	return copy
    48  }
    49  
    50  // TestMergeBasics tests some simple merges
    51  func TestMergeBasics(t *testing.T) {
    52  	var (
    53  		accounts = make(map[common.Hash][]byte)
    54  		storage  = make(map[common.Hash]map[common.Hash][]byte)
    55  	)
    56  	// Fill up a parent
    57  	for i := 0; i < 100; i++ {
    58  		h := randomHash()
    59  		data := randomAccount()
    60  
    61  		accounts[h] = data
    62  		if rand.Intn(4) == 0 {
    63  			accounts[h] = nil
    64  		}
    65  		if rand.Intn(2) == 0 {
    66  			accStorage := make(map[common.Hash][]byte)
    67  			value := make([]byte, 32)
    68  			crand.Read(value)
    69  			accStorage[randomHash()] = value
    70  			storage[h] = accStorage
    71  		}
    72  	}
    73  	// Add some (identical) layers on top
    74  	parent := newDiffLayer(emptyLayer(), common.Hash{}, copyAccounts(accounts), copyStorage(storage))
    75  	child := newDiffLayer(parent, common.Hash{}, copyAccounts(accounts), copyStorage(storage))
    76  	child = newDiffLayer(child, common.Hash{}, copyAccounts(accounts), copyStorage(storage))
    77  	child = newDiffLayer(child, common.Hash{}, copyAccounts(accounts), copyStorage(storage))
    78  	child = newDiffLayer(child, common.Hash{}, copyAccounts(accounts), copyStorage(storage))
    79  
    80  	// And flatten
    81  	merged := (child.flatten()).(*diffLayer)
    82  
    83  	{ // Check account lists
    84  		if have, want := len(merged.accountList), 0; have != want {
    85  			t.Errorf("accountList wrong: have %v, want %v", have, want)
    86  		}
    87  		if have, want := len(merged.AccountList()), len(accounts); have != want {
    88  			t.Errorf("AccountList() wrong: have %v, want %v", have, want)
    89  		}
    90  		if have, want := len(merged.accountList), len(accounts); have != want {
    91  			t.Errorf("accountList [2] wrong: have %v, want %v", have, want)
    92  		}
    93  	}
    94  	{ // Check storage lists
    95  		i := 0
    96  		for aHash, sMap := range storage {
    97  			if have, want := len(merged.storageList), i; have != want {
    98  				t.Errorf("[1] storageList wrong: have %v, want %v", have, want)
    99  			}
   100  			list := merged.StorageList(aHash)
   101  			if have, want := len(list), len(sMap); have != want {
   102  				t.Errorf("[2] StorageList() wrong: have %v, want %v", have, want)
   103  			}
   104  			if have, want := len(merged.storageList[aHash]), len(sMap); have != want {
   105  				t.Errorf("storageList wrong: have %v, want %v", have, want)
   106  			}
   107  			i++
   108  		}
   109  	}
   110  }
   111  
   112  // TestMergeDelete tests some deletion
   113  func TestMergeDelete(t *testing.T) {
   114  	storage := make(map[common.Hash]map[common.Hash][]byte)
   115  
   116  	// Fill up a parent
   117  	h1 := common.HexToHash("0x01")
   118  	h2 := common.HexToHash("0x02")
   119  
   120  	flip := func() map[common.Hash][]byte {
   121  		return map[common.Hash][]byte{
   122  			h1: randomAccount(),
   123  			h2: nil,
   124  		}
   125  	}
   126  	flop := func() map[common.Hash][]byte {
   127  		return map[common.Hash][]byte{
   128  			h1: nil,
   129  			h2: randomAccount(),
   130  		}
   131  	}
   132  	// Add some flipAccs-flopping layers on top
   133  	parent := newDiffLayer(emptyLayer(), common.Hash{}, flip(), storage)
   134  	child := parent.Update(common.Hash{}, flop(), storage)
   135  	child = child.Update(common.Hash{}, flip(), storage)
   136  	child = child.Update(common.Hash{}, flop(), storage)
   137  	child = child.Update(common.Hash{}, flip(), storage)
   138  	child = child.Update(common.Hash{}, flop(), storage)
   139  	child = child.Update(common.Hash{}, flip(), storage)
   140  
   141  	if data, _ := child.Account(h1); data == nil {
   142  		t.Errorf("last diff layer: expected %x account to be non-nil", h1)
   143  	}
   144  	if data, _ := child.Account(h2); data != nil {
   145  		t.Errorf("last diff layer: expected %x account to be nil", h2)
   146  	}
   147  
   148  	// And flatten
   149  	merged := (child.flatten()).(*diffLayer)
   150  
   151  	if data, _ := merged.Account(h1); data == nil {
   152  		t.Errorf("merged layer: expected %x account to be non-nil", h1)
   153  	}
   154  	if data, _ := merged.Account(h2); data != nil {
   155  		t.Errorf("merged layer: expected %x account to be nil", h2)
   156  	}
   157  	// If we add more granular metering of memory, we can enable this again,
   158  	// but it's not implemented for now
   159  	//if have, want := merged.memory, child.memory; have != want {
   160  	//	t.Errorf("mem wrong: have %d, want %d", have, want)
   161  	//}
   162  }
   163  
   164  // This tests that if we create a new account, and set a slot, and then merge
   165  // it, the lists will be correct.
   166  func TestInsertAndMerge(t *testing.T) {
   167  	// Fill up a parent
   168  	var (
   169  		acc    = common.HexToHash("0x01")
   170  		slot   = common.HexToHash("0x02")
   171  		parent *diffLayer
   172  		child  *diffLayer
   173  	)
   174  	{
   175  		var (
   176  			accounts = make(map[common.Hash][]byte)
   177  			storage  = make(map[common.Hash]map[common.Hash][]byte)
   178  		)
   179  		parent = newDiffLayer(emptyLayer(), common.Hash{}, accounts, storage)
   180  	}
   181  	{
   182  		var (
   183  			accounts = make(map[common.Hash][]byte)
   184  			storage  = make(map[common.Hash]map[common.Hash][]byte)
   185  		)
   186  		accounts[acc] = randomAccount()
   187  		storage[acc] = make(map[common.Hash][]byte)
   188  		storage[acc][slot] = []byte{0x01}
   189  		child = newDiffLayer(parent, common.Hash{}, accounts, storage)
   190  	}
   191  	// And flatten
   192  	merged := (child.flatten()).(*diffLayer)
   193  	{ // Check that slot value is present
   194  		have, _ := merged.Storage(acc, slot)
   195  		if want := []byte{0x01}; !bytes.Equal(have, want) {
   196  			t.Errorf("merged slot value wrong: have %x, want %x", have, want)
   197  		}
   198  	}
   199  }
   200  
   201  func emptyLayer() *diskLayer {
   202  	return &diskLayer{
   203  		diskdb: memorydb.New(),
   204  		cache:  fastcache.New(500 * 1024),
   205  	}
   206  }
   207  
   208  // BenchmarkSearch checks how long it takes to find a non-existing key
   209  // BenchmarkSearch-6   	  200000	     10481 ns/op (1K per layer)
   210  // BenchmarkSearch-6   	  200000	     10760 ns/op (10K per layer)
   211  // BenchmarkSearch-6   	  100000	     17866 ns/op
   212  //
   213  // BenchmarkSearch-6   	  500000	      3723 ns/op (10k per layer, only top-level RLock()
   214  func BenchmarkSearch(b *testing.B) {
   215  	// First, we set up 128 diff layers, with 1K items each
   216  	fill := func(parent snapshot) *diffLayer {
   217  		var (
   218  			accounts = make(map[common.Hash][]byte)
   219  			storage  = make(map[common.Hash]map[common.Hash][]byte)
   220  		)
   221  		for i := 0; i < 10000; i++ {
   222  			accounts[randomHash()] = randomAccount()
   223  		}
   224  		return newDiffLayer(parent, common.Hash{}, accounts, storage)
   225  	}
   226  	var layer snapshot
   227  	layer = emptyLayer()
   228  	for i := 0; i < 128; i++ {
   229  		layer = fill(layer)
   230  	}
   231  	key := crypto.Keccak256Hash([]byte{0x13, 0x38})
   232  	b.ResetTimer()
   233  	for i := 0; i < b.N; i++ {
   234  		layer.AccountRLP(key)
   235  	}
   236  }
   237  
   238  // BenchmarkSearchSlot checks how long it takes to find a non-existing key
   239  // - Number of layers: 128
   240  // - Each layers contains the account, with a couple of storage slots
   241  // BenchmarkSearchSlot-6   	  100000	     14554 ns/op
   242  // BenchmarkSearchSlot-6   	  100000	     22254 ns/op (when checking parent root using mutex)
   243  // BenchmarkSearchSlot-6   	  100000	     14551 ns/op (when checking parent number using atomic)
   244  // With bloom filter:
   245  // BenchmarkSearchSlot-6   	 3467835	       351 ns/op
   246  func BenchmarkSearchSlot(b *testing.B) {
   247  	// First, we set up 128 diff layers, with 1K items each
   248  	accountKey := crypto.Keccak256Hash([]byte{0x13, 0x37})
   249  	storageKey := crypto.Keccak256Hash([]byte{0x13, 0x37})
   250  	accountRLP := randomAccount()
   251  	fill := func(parent snapshot) *diffLayer {
   252  		var (
   253  			accounts = make(map[common.Hash][]byte)
   254  			storage  = make(map[common.Hash]map[common.Hash][]byte)
   255  		)
   256  		accounts[accountKey] = accountRLP
   257  
   258  		accStorage := make(map[common.Hash][]byte)
   259  		for i := 0; i < 5; i++ {
   260  			value := make([]byte, 32)
   261  			crand.Read(value)
   262  			accStorage[randomHash()] = value
   263  			storage[accountKey] = accStorage
   264  		}
   265  		return newDiffLayer(parent, common.Hash{}, accounts, storage)
   266  	}
   267  	var layer snapshot
   268  	layer = emptyLayer()
   269  	for i := 0; i < 128; i++ {
   270  		layer = fill(layer)
   271  	}
   272  	b.ResetTimer()
   273  	for i := 0; i < b.N; i++ {
   274  		layer.Storage(accountKey, storageKey)
   275  	}
   276  }
   277  
   278  // With accountList and sorting
   279  // BenchmarkFlatten-6   	      50	  29890856 ns/op
   280  //
   281  // Without sorting and tracking accountList
   282  // BenchmarkFlatten-6   	     300	   5511511 ns/op
   283  func BenchmarkFlatten(b *testing.B) {
   284  	fill := func(parent snapshot) *diffLayer {
   285  		var (
   286  			accounts = make(map[common.Hash][]byte)
   287  			storage  = make(map[common.Hash]map[common.Hash][]byte)
   288  		)
   289  		for i := 0; i < 100; i++ {
   290  			accountKey := randomHash()
   291  			accounts[accountKey] = randomAccount()
   292  
   293  			accStorage := make(map[common.Hash][]byte)
   294  			for i := 0; i < 20; i++ {
   295  				value := make([]byte, 32)
   296  				crand.Read(value)
   297  				accStorage[randomHash()] = value
   298  			}
   299  			storage[accountKey] = accStorage
   300  		}
   301  		return newDiffLayer(parent, common.Hash{}, accounts, storage)
   302  	}
   303  	b.ResetTimer()
   304  	for i := 0; i < b.N; i++ {
   305  		b.StopTimer()
   306  		var layer snapshot
   307  		layer = emptyLayer()
   308  		for i := 1; i < 128; i++ {
   309  			layer = fill(layer)
   310  		}
   311  		b.StartTimer()
   312  
   313  		for i := 1; i < 128; i++ {
   314  			dl, ok := layer.(*diffLayer)
   315  			if !ok {
   316  				break
   317  			}
   318  			layer = dl.flatten()
   319  		}
   320  		b.StopTimer()
   321  	}
   322  }
   323  
   324  // This test writes ~324M of diff layers to disk, spread over
   325  // - 128 individual layers,
   326  // - each with 200 accounts
   327  // - containing 200 slots
   328  //
   329  // BenchmarkJournal-6   	       1	1471373923 ns/ops
   330  // BenchmarkJournal-6   	       1	1208083335 ns/op // bufio writer
   331  func BenchmarkJournal(b *testing.B) {
   332  	fill := func(parent snapshot) *diffLayer {
   333  		var (
   334  			accounts = make(map[common.Hash][]byte)
   335  			storage  = make(map[common.Hash]map[common.Hash][]byte)
   336  		)
   337  		for i := 0; i < 200; i++ {
   338  			accountKey := randomHash()
   339  			accounts[accountKey] = randomAccount()
   340  
   341  			accStorage := make(map[common.Hash][]byte)
   342  			for i := 0; i < 200; i++ {
   343  				value := make([]byte, 32)
   344  				crand.Read(value)
   345  				accStorage[randomHash()] = value
   346  			}
   347  			storage[accountKey] = accStorage
   348  		}
   349  		return newDiffLayer(parent, common.Hash{}, accounts, storage)
   350  	}
   351  	layer := snapshot(emptyLayer())
   352  	for i := 1; i < 128; i++ {
   353  		layer = fill(layer)
   354  	}
   355  	b.ResetTimer()
   356  
   357  	for i := 0; i < b.N; i++ {
   358  		layer.Journal(new(bytes.Buffer))
   359  	}
   360  }