github.com/ethereum/go-ethereum@v1.16.1/trie/trienode/proof.go (about) 1 // Copyright 2017 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 trienode 18 19 import ( 20 "errors" 21 "sync" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/crypto" 25 "github.com/ethereum/go-ethereum/ethdb" 26 "github.com/ethereum/go-ethereum/rlp" 27 ) 28 29 // ProofSet stores a set of trie nodes. It implements trie.Database and can also 30 // act as a cache for another trie.Database. 31 type ProofSet struct { 32 nodes map[string][]byte 33 order []string 34 35 dataSize int 36 lock sync.RWMutex 37 } 38 39 // NewProofSet creates an empty node set 40 func NewProofSet() *ProofSet { 41 return &ProofSet{ 42 nodes: make(map[string][]byte), 43 } 44 } 45 46 // Put stores a new node in the set 47 func (db *ProofSet) Put(key []byte, value []byte) error { 48 db.lock.Lock() 49 defer db.lock.Unlock() 50 51 if _, ok := db.nodes[string(key)]; ok { 52 return nil 53 } 54 keystr := string(key) 55 56 db.nodes[keystr] = common.CopyBytes(value) 57 db.order = append(db.order, keystr) 58 db.dataSize += len(value) 59 60 return nil 61 } 62 63 // Delete removes a node from the set 64 func (db *ProofSet) Delete(key []byte) error { 65 db.lock.Lock() 66 defer db.lock.Unlock() 67 68 delete(db.nodes, string(key)) 69 return nil 70 } 71 72 func (db *ProofSet) DeleteRange(start, end []byte) error { 73 panic("not supported") 74 } 75 76 // Get returns a stored node 77 func (db *ProofSet) Get(key []byte) ([]byte, error) { 78 db.lock.RLock() 79 defer db.lock.RUnlock() 80 81 if entry, ok := db.nodes[string(key)]; ok { 82 return entry, nil 83 } 84 return nil, errors.New("not found") 85 } 86 87 // Has returns true if the node set contains the given key 88 func (db *ProofSet) Has(key []byte) (bool, error) { 89 _, err := db.Get(key) 90 return err == nil, nil 91 } 92 93 // KeyCount returns the number of nodes in the set 94 func (db *ProofSet) KeyCount() int { 95 db.lock.RLock() 96 defer db.lock.RUnlock() 97 98 return len(db.nodes) 99 } 100 101 // DataSize returns the aggregated data size of nodes in the set 102 func (db *ProofSet) DataSize() int { 103 db.lock.RLock() 104 defer db.lock.RUnlock() 105 106 return db.dataSize 107 } 108 109 // List converts the node set to a slice of bytes. 110 func (db *ProofSet) List() [][]byte { 111 db.lock.RLock() 112 defer db.lock.RUnlock() 113 114 values := make([][]byte, len(db.order)) 115 for i, key := range db.order { 116 values[i] = db.nodes[key] 117 } 118 return values 119 } 120 121 // Store writes the contents of the set to the given database 122 func (db *ProofSet) Store(target ethdb.KeyValueWriter) { 123 db.lock.RLock() 124 defer db.lock.RUnlock() 125 126 for key, value := range db.nodes { 127 target.Put([]byte(key), value) 128 } 129 } 130 131 // ProofList stores an ordered list of trie nodes. It implements ethdb.KeyValueWriter. 132 type ProofList []rlp.RawValue 133 134 // Store writes the contents of the list to the given database 135 func (n ProofList) Store(db ethdb.KeyValueWriter) { 136 for _, node := range n { 137 db.Put(crypto.Keccak256(node), node) 138 } 139 } 140 141 // Set converts the node list to a ProofSet 142 func (n ProofList) Set() *ProofSet { 143 db := NewProofSet() 144 n.Store(db) 145 return db 146 } 147 148 // Put stores a new node at the end of the list 149 func (n *ProofList) Put(key []byte, value []byte) error { 150 *n = append(*n, value) 151 return nil 152 } 153 154 // Delete panics as there's no reason to remove a node from the list. 155 func (n *ProofList) Delete(key []byte) error { 156 panic("not supported") 157 } 158 159 // DataSize returns the aggregated data size of nodes in the list 160 func (n ProofList) DataSize() int { 161 var size int 162 for _, node := range n { 163 size += len(node) 164 } 165 return size 166 }