github.com/klaytn/klaytn@v1.10.2/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 ) 31 32 func TestDatabase_Reference(t *testing.T) { 33 memDB := database.NewMemoryDBManager() 34 db := NewDatabaseWithNewCache(memDB, &TrieNodeCacheConfig{CacheType: CacheTypeLocal, LocalCacheSizeMiB: 128}) 35 36 assert.Equal(t, memDB, db.DiskDB()) 37 assert.Equal(t, 1, len(db.nodes)) // {} : {} 38 39 db.Reference(childHash, parentHash) 40 assert.Equal(t, 1, len(db.nodes)) // {} : {} 41 42 child := &cachedNode{} 43 parent := &cachedNode{} 44 db.nodes[childHash] = child 45 db.nodes[parentHash] = parent 46 47 // Call Reference after updating db.nodes 48 db.Reference(childHash, parentHash) 49 assert.Equal(t, 3, len(db.nodes)) 50 assert.Equal(t, uint64(1), child.parents) 51 assert.Equal(t, uint64(1), parent.children[childHash]) 52 53 // Just calling Reference does not have effect 54 db.Reference(childHash, parentHash) 55 assert.Equal(t, 3, len(db.nodes)) 56 assert.Equal(t, uint64(1), child.parents) 57 assert.Equal(t, uint64(1), parent.children[childHash]) 58 } 59 60 func TestDatabase_DeReference(t *testing.T) { 61 memDB := database.NewMemoryDBManager() 62 db := NewDatabaseWithNewCache(memDB, &TrieNodeCacheConfig{CacheType: CacheTypeLocal, LocalCacheSizeMiB: 128}) 63 assert.Equal(t, 1, len(db.nodes)) // {} : {} 64 65 db.Dereference(parentHash) 66 assert.Equal(t, 1, len(db.nodes)) // {} : {} 67 assert.Equal(t, uint64(0), db.gcnodes) 68 assert.Equal(t, common.StorageSize(0), db.gcsize) 69 70 child := &cachedNode{} 71 parent := &cachedNode{} 72 db.nodes[childHash] = child 73 db.nodes[parentHash] = parent 74 75 db.Reference(childHash, parentHash) 76 assert.Equal(t, 3, len(db.nodes)) 77 assert.Equal(t, uint64(1), child.parents) 78 assert.Equal(t, uint64(1), parent.children[childHash]) 79 assert.Equal(t, uint64(0), db.gcnodes) 80 assert.Equal(t, common.StorageSize(0), db.gcsize) 81 82 db.Dereference(parentHash) 83 assert.Equal(t, 1, len(db.nodes)) 84 assert.Equal(t, uint64(0), child.parents) 85 assert.Equal(t, uint64(0), parent.children[childHash]) 86 assert.Equal(t, uint64(2), db.gcnodes) 87 assert.Equal(t, common.StorageSize(64), db.gcsize) 88 } 89 90 func TestDatabase_Size(t *testing.T) { 91 memDB := database.NewMemoryDBManager() 92 db := NewDatabaseWithNewCache(memDB, &TrieNodeCacheConfig{CacheType: CacheTypeLocal, LocalCacheSizeMiB: 128}) 93 94 totalMemorySize, _, preimagesSize := db.Size() 95 assert.Equal(t, common.StorageSize(0), totalMemorySize) 96 assert.Equal(t, common.StorageSize(0), preimagesSize) 97 98 child := &cachedNode{} 99 parent := &cachedNode{} 100 db.nodes[childHash] = child 101 db.nodes[parentHash] = parent 102 103 db.Reference(childHash, parentHash) 104 105 totalMemorySize, _, preimagesSize = db.Size() 106 assert.Equal(t, common.StorageSize(128), totalMemorySize) 107 assert.Equal(t, common.StorageSize(0), preimagesSize) 108 109 db.preimagesSize += 100 110 totalMemorySize, _, preimagesSize = db.Size() 111 assert.Equal(t, common.StorageSize(128), totalMemorySize) 112 assert.Equal(t, common.StorageSize(100), preimagesSize) 113 } 114 115 func TestDatabase_SecureKey(t *testing.T) { 116 secKey1 := secureKey(childHash) 117 copiedSecKey := make([]byte, len(secKey1)) 118 copy(copiedSecKey, secKey1) 119 120 secKey2 := secureKey(parentHash) 121 122 assert.Equal(t, secKey1, copiedSecKey) // after the next call of secureKey, secKey1 became different from the copied 123 assert.NotEqual(t, secKey1, secKey2) // secKey1 has changed into secKey2 as they are created from the different buffer 124 } 125 126 func TestCache(t *testing.T) { 127 memDB := database.NewMemoryDBManager() 128 db := NewDatabaseWithNewCache(memDB, &TrieNodeCacheConfig{CacheType: CacheTypeLocal, LocalCacheSizeMiB: 10}) 129 130 for i := 0; i < 100; i++ { 131 key, value := common.MakeRandomBytes(256), common.MakeRandomBytes(63*1024) // fastcache can store entrie under 64KB 132 db.trieNodeCache.Set(key, value) 133 rValue, found := db.trieNodeCache.Has(key) 134 135 assert.Equal(t, true, found) 136 assert.Equal(t, value, rValue) 137 } 138 }