|
| 1 | +// Copyright 2023 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 triedb |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/ethereum/go-ethereum/common" |
| 24 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 25 | + "github.com/ethereum/go-ethereum/triedb/hashdb" |
| 26 | +) |
| 27 | + |
| 28 | +// TestDatabasePreimages tests the preimage functionality of the trie database. |
| 29 | +func TestDatabasePreimages(t *testing.T) { |
| 30 | + // Create a database with preimages enabled |
| 31 | + memDB := rawdb.NewMemoryDatabase() |
| 32 | + config := &Config{ |
| 33 | + Preimages: true, |
| 34 | + HashDB: hashdb.Defaults, |
| 35 | + } |
| 36 | + db := NewDatabase(memDB, config) |
| 37 | + defer db.Close() |
| 38 | + |
| 39 | + // Test inserting and retrieving preimages |
| 40 | + preimages := make(map[common.Hash][]byte) |
| 41 | + for i := 0; i < 10; i++ { |
| 42 | + data := []byte{byte(i), byte(i + 1), byte(i + 2)} |
| 43 | + hash := common.BytesToHash(data) |
| 44 | + preimages[hash] = data |
| 45 | + } |
| 46 | + |
| 47 | + // Insert preimages into the database |
| 48 | + db.InsertPreimage(preimages) |
| 49 | + |
| 50 | + // Verify all preimages are retrievable |
| 51 | + for hash, data := range preimages { |
| 52 | + retrieved := db.Preimage(hash) |
| 53 | + if retrieved == nil { |
| 54 | + t.Errorf("Preimage for %x not found", hash) |
| 55 | + } |
| 56 | + if !bytes.Equal(retrieved, data) { |
| 57 | + t.Errorf("Preimage data mismatch: got %x want %x", retrieved, data) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Test non-existent preimage |
| 62 | + nonExistentHash := common.HexToHash("deadbeef") |
| 63 | + if data := db.Preimage(nonExistentHash); data != nil { |
| 64 | + t.Errorf("Unexpected preimage data for non-existent hash: %x", data) |
| 65 | + } |
| 66 | + |
| 67 | + // Force preimage commit and verify again |
| 68 | + db.WritePreimages() |
| 69 | + for hash, data := range preimages { |
| 70 | + retrieved := db.Preimage(hash) |
| 71 | + if retrieved == nil { |
| 72 | + t.Errorf("Preimage for %x not found after forced commit", hash) |
| 73 | + } |
| 74 | + if !bytes.Equal(retrieved, data) { |
| 75 | + t.Errorf("Preimage data mismatch after forced commit: got %x want %x", retrieved, data) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments