github.com/klaytn/klaytn@v1.12.1/storage/statedb/database_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package statedb
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/klaytn/klaytn/common"
    23  	"github.com/klaytn/klaytn/storage/database"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var (
    28  	childHash     = common.HexToHash("1341655") // 20190805 in hexadecimal
    29  	parentHash    = common.HexToHash("1343A3F") // 20199999 in hexadecimal
    30  	childExtHash  = childHash.ExtendZero()
    31  	parentExtHash = parentHash.ExtendZero()
    32  )
    33  
    34  func TestDatabase_Reference(t *testing.T) {
    35  	memDB := database.NewMemoryDBManager()
    36  	db := NewDatabaseWithNewCache(memDB, &TrieNodeCacheConfig{CacheType: CacheTypeLocal, LocalCacheSizeMiB: 128})
    37  
    38  	assert.Equal(t, memDB, db.DiskDB())
    39  	assert.Equal(t, 1, len(db.nodes)) // {} : {}
    40  
    41  	db.Reference(childExtHash, parentExtHash)
    42  	assert.Equal(t, 1, len(db.nodes)) // {} : {}
    43  
    44  	child := &cachedNode{}
    45  	parent := &cachedNode{}
    46  	db.nodes[childExtHash] = child
    47  	db.nodes[parentExtHash] = parent
    48  
    49  	// Call Reference after updating db.nodes
    50  	db.Reference(childExtHash, parentExtHash)
    51  	assert.Equal(t, 3, len(db.nodes))
    52  	assert.Equal(t, uint64(1), child.parents)
    53  	assert.Equal(t, uint64(1), parent.children[childExtHash])
    54  
    55  	// Just calling Reference does not have effect
    56  	db.Reference(childExtHash, parentExtHash)
    57  	assert.Equal(t, 3, len(db.nodes))
    58  	assert.Equal(t, uint64(1), child.parents)
    59  	assert.Equal(t, uint64(1), parent.children[childExtHash])
    60  }
    61  
    62  func TestDatabase_DeReference(t *testing.T) {
    63  	memDB := database.NewMemoryDBManager()
    64  	db := NewDatabaseWithNewCache(memDB, &TrieNodeCacheConfig{CacheType: CacheTypeLocal, LocalCacheSizeMiB: 128})
    65  	assert.Equal(t, 1, len(db.nodes)) // {} : {}
    66  
    67  	db.Dereference(parentHash)
    68  	assert.Equal(t, 1, len(db.nodes)) // {} : {}
    69  	assert.Equal(t, uint64(0), db.gcnodes)
    70  	assert.Equal(t, common.StorageSize(0), db.gcsize)
    71  
    72  	child := &cachedNode{}
    73  	parent := &cachedNode{}
    74  	db.nodes[childExtHash] = child
    75  	db.nodes[parentExtHash] = parent
    76  
    77  	db.Reference(childExtHash, parentExtHash)
    78  	assert.Equal(t, 3, len(db.nodes))
    79  	assert.Equal(t, uint64(1), child.parents)
    80  	assert.Equal(t, uint64(1), parent.children[childExtHash])
    81  	assert.Equal(t, uint64(0), db.gcnodes)
    82  	assert.Equal(t, common.StorageSize(0), db.gcsize)
    83  
    84  	db.Dereference(parentHash)
    85  	assert.Equal(t, 1, len(db.nodes))
    86  	assert.Equal(t, uint64(0), child.parents)
    87  	assert.Equal(t, uint64(0), parent.children[childExtHash])
    88  	assert.Equal(t, uint64(2), db.gcnodes)
    89  	assert.Equal(t, common.StorageSize(64), db.gcsize)
    90  }
    91  
    92  func TestDatabase_Size(t *testing.T) {
    93  	memDB := database.NewMemoryDBManager()
    94  	db := NewDatabaseWithNewCache(memDB, &TrieNodeCacheConfig{CacheType: CacheTypeLocal, LocalCacheSizeMiB: 128})
    95  
    96  	totalMemorySize, _, preimagesSize := db.Size()
    97  	assert.Equal(t, common.StorageSize(0), totalMemorySize)
    98  	assert.Equal(t, common.StorageSize(0), preimagesSize)
    99  
   100  	child := &cachedNode{}
   101  	parent := &cachedNode{}
   102  	db.nodes[childExtHash] = child
   103  	db.nodes[parentExtHash] = parent
   104  
   105  	db.Reference(childExtHash, parentExtHash)
   106  
   107  	totalMemorySize, _, preimagesSize = db.Size()
   108  	assert.Equal(t, common.StorageSize(128), totalMemorySize)
   109  	assert.Equal(t, common.StorageSize(0), preimagesSize)
   110  
   111  	db.preimagesSize += 100
   112  	totalMemorySize, _, preimagesSize = db.Size()
   113  	assert.Equal(t, common.StorageSize(128), totalMemorySize)
   114  	assert.Equal(t, common.StorageSize(100), preimagesSize)
   115  }
   116  
   117  func TestCache(t *testing.T) {
   118  	memDB := database.NewMemoryDBManager()
   119  	db := NewDatabaseWithNewCache(memDB, &TrieNodeCacheConfig{CacheType: CacheTypeLocal, LocalCacheSizeMiB: 10})
   120  
   121  	for i := 0; i < 100; i++ {
   122  		key, value := common.MakeRandomBytes(256), common.MakeRandomBytes(63*1024) // fastcache can store entrie under 64KB
   123  		db.trieNodeCache.Set(key, value)
   124  		rValue, found := db.trieNodeCache.Has(key)
   125  
   126  		assert.Equal(t, true, found)
   127  		assert.Equal(t, value, rValue)
   128  	}
   129  }