github.com/klaytn/klaytn@v1.12.1/storage/statedb/hasher_test.go (about) 1 // Copyright 2023 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 "github.com/stretchr/testify/require" 26 ) 27 28 func checkHasherHash(t *testing.T, name string, tc testNodeEncodingTC, opts *hasherOpts, onRoot bool) { 29 common.ResetExtHashCounterForTest(0xccccddddeeee00) 30 memDB := database.NewMemoryDBManager() 31 db := NewDatabase(memDB) 32 33 h := newHasher(opts) 34 defer returnHasherToPool(h) 35 36 hashed, cached := h.hashNode(tc.expanded, db, false, onRoot) 37 t.Logf("tc[%s] %s", name, hashed) 38 assert.Equal(t, hashNode(tc.hash), hashed, name) 39 40 cachedHash, _ := cached.cache() 41 assert.Equal(t, hashNode(tc.hash), cachedHash, name) 42 43 hash := common.BytesToExtHash(tc.hash) 44 inserted := db.nodes[hash] 45 require.NotNil(t, inserted) 46 assert.Equal(t, tc.inserted, inserted.node, name) 47 48 db.Cap(0) 49 encoded, _ := memDB.ReadTrieNode(hash) 50 assert.Equal(t, tc.encoded, encoded, name) 51 } 52 53 func TestHasherHashTC(t *testing.T) { 54 optsLegacy := &hasherOpts{} 55 optsState := &hasherOpts{pruning: true} 56 optsStorage := &hasherOpts{pruning: true, storageRoot: true} 57 58 for name, tc := range collapsedNodeTCs_unext() { 59 checkHasherHash(t, name, tc, optsLegacy, true) 60 checkHasherHash(t, name, tc, optsLegacy, false) 61 } 62 for name, tc := range resolvedNodeTCs_unext() { 63 checkHasherHash(t, name, tc, optsLegacy, true) 64 checkHasherHash(t, name, tc, optsLegacy, false) 65 } 66 67 for name, tc := range collapsedNodeTCs_extroot() { 68 checkHasherHash(t, name, tc, optsState, true) 69 } 70 for name, tc := range resolvedNodeTCs_extroot() { 71 checkHasherHash(t, name, tc, optsState, true) 72 } 73 74 for name, tc := range collapsedNodeTCs_exthash() { 75 checkHasherHash(t, name, tc, optsState, false) 76 checkHasherHash(t, name, tc, optsStorage, true) 77 checkHasherHash(t, name, tc, optsStorage, false) 78 } 79 for name, tc := range resolvedNodeTCs_exthash() { 80 checkHasherHash(t, name, tc, optsState, false) 81 checkHasherHash(t, name, tc, optsStorage, true) 82 checkHasherHash(t, name, tc, optsStorage, false) 83 } 84 }