secure_trie.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2015 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. package trie
  17. import (
  18. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/logger"
  20. "github.com/ethereum/go-ethereum/logger/glog"
  21. )
  22. var secureKeyPrefix = []byte("secure-key-")
  23. const secureKeyLength = 11 + 32 // Length of the above prefix + 32byte hash
  24. // SecureTrie wraps a trie with key hashing. In a secure trie, all
  25. // access operations hash the key using keccak256. This prevents
  26. // calling code from creating long chains of nodes that
  27. // increase the access time.
  28. //
  29. // Contrary to a regular trie, a SecureTrie can only be created with
  30. // New and must have an attached database. The database also stores
  31. // the preimage of each key.
  32. //
  33. // SecureTrie is not safe for concurrent use.
  34. type SecureTrie struct {
  35. trie Trie
  36. hashKeyBuf [secureKeyLength]byte
  37. secKeyBuf [200]byte
  38. secKeyCache map[string][]byte
  39. secKeyCacheOwner *SecureTrie // Pointer to self, replace the key cache on mismatch
  40. }
  41. // NewSecure creates a trie with an existing root node from db.
  42. //
  43. // If root is the zero hash or the sha3 hash of an empty string, the
  44. // trie is initially empty. Otherwise, New will panic if db is nil
  45. // and returns MissingNodeError if the root node cannot be found.
  46. // Accessing the trie loads nodes from db on demand.
  47. func NewSecure(root common.Hash, db Database) (*SecureTrie, error) {
  48. if db == nil {
  49. panic("NewSecure called with nil database")
  50. }
  51. trie, err := New(root, db)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return &SecureTrie{
  56. trie: *trie,
  57. }, nil
  58. }
  59. // Get returns the value for key stored in the trie.
  60. // The value bytes must not be modified by the caller.
  61. func (t *SecureTrie) Get(key []byte) []byte {
  62. res, err := t.TryGet(key)
  63. if err != nil && glog.V(logger.Error) {
  64. glog.Errorf("Unhandled trie error: %v", err)
  65. }
  66. return res
  67. }
  68. // TryGet returns the value for key stored in the trie.
  69. // The value bytes must not be modified by the caller.
  70. // If a node was not found in the database, a MissingNodeError is returned.
  71. func (t *SecureTrie) TryGet(key []byte) ([]byte, error) {
  72. return t.trie.TryGet(t.hashKey(key))
  73. }
  74. // Update associates key with value in the trie. Subsequent calls to
  75. // Get will return value. If value has length zero, any existing value
  76. // is deleted from the trie and calls to Get will return nil.
  77. //
  78. // The value bytes must not be modified by the caller while they are
  79. // stored in the trie.
  80. func (t *SecureTrie) Update(key, value []byte) {
  81. if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
  82. glog.Errorf("Unhandled trie error: %v", err)
  83. }
  84. }
  85. // TryUpdate associates key with value in the trie. Subsequent calls to
  86. // Get will return value. If value has length zero, any existing value
  87. // is deleted from the trie and calls to Get will return nil.
  88. //
  89. // The value bytes must not be modified by the caller while they are
  90. // stored in the trie.
  91. //
  92. // If a node was not found in the database, a MissingNodeError is returned.
  93. func (t *SecureTrie) TryUpdate(key, value []byte) error {
  94. hk := t.hashKey(key)
  95. err := t.trie.TryUpdate(hk, value)
  96. if err != nil {
  97. return err
  98. }
  99. t.getSecKeyCache()[string(hk)] = common.CopyBytes(key)
  100. return nil
  101. }
  102. // Delete removes any existing value for key from the trie.
  103. func (t *SecureTrie) Delete(key []byte) {
  104. if err := t.TryDelete(key); err != nil && glog.V(logger.Error) {
  105. glog.Errorf("Unhandled trie error: %v", err)
  106. }
  107. }
  108. // TryDelete removes any existing value for key from the trie.
  109. // If a node was not found in the database, a MissingNodeError is returned.
  110. func (t *SecureTrie) TryDelete(key []byte) error {
  111. hk := t.hashKey(key)
  112. delete(t.getSecKeyCache(), string(hk))
  113. return t.trie.TryDelete(hk)
  114. }
  115. // GetKey returns the sha3 preimage of a hashed key that was
  116. // previously used to store a value.
  117. func (t *SecureTrie) GetKey(shaKey []byte) []byte {
  118. if key, ok := t.getSecKeyCache()[string(shaKey)]; ok {
  119. return key
  120. }
  121. key, _ := t.trie.db.Get(t.secKey(shaKey))
  122. return key
  123. }
  124. // Commit writes all nodes and the secure hash pre-images to the trie's database.
  125. // Nodes are stored with their sha3 hash as the key.
  126. //
  127. // Committing flushes nodes from memory. Subsequent Get calls will load nodes
  128. // from the database.
  129. func (t *SecureTrie) Commit() (root common.Hash, err error) {
  130. return t.CommitTo(t.trie.db)
  131. }
  132. func (t *SecureTrie) Hash() common.Hash {
  133. return t.trie.Hash()
  134. }
  135. func (t *SecureTrie) Root() []byte {
  136. return t.trie.Root()
  137. }
  138. func (t *SecureTrie) Iterator() *Iterator {
  139. return t.trie.Iterator()
  140. }
  141. func (t *SecureTrie) NodeIterator() *NodeIterator {
  142. return NewNodeIterator(&t.trie)
  143. }
  144. // CommitTo writes all nodes and the secure hash pre-images to the given database.
  145. // Nodes are stored with their sha3 hash as the key.
  146. //
  147. // Committing flushes nodes from memory. Subsequent Get calls will load nodes from
  148. // the trie's database. Calling code must ensure that the changes made to db are
  149. // written back to the trie's attached database before using the trie.
  150. func (t *SecureTrie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
  151. if len(t.getSecKeyCache()) > 0 {
  152. for hk, key := range t.secKeyCache {
  153. if err := db.Put(t.secKey([]byte(hk)), key); err != nil {
  154. return common.Hash{}, err
  155. }
  156. }
  157. t.secKeyCache = make(map[string][]byte)
  158. }
  159. return t.trie.CommitTo(db)
  160. }
  161. // secKey returns the database key for the preimage of key, as an ephemeral buffer.
  162. // The caller must not hold onto the return value because it will become
  163. // invalid on the next call to hashKey or secKey.
  164. func (t *SecureTrie) secKey(key []byte) []byte {
  165. buf := append(t.secKeyBuf[:0], secureKeyPrefix...)
  166. buf = append(buf, key...)
  167. return buf
  168. }
  169. // hashKey returns the hash of key as an ephemeral buffer.
  170. // The caller must not hold onto the return value because it will become
  171. // invalid on the next call to hashKey or secKey.
  172. func (t *SecureTrie) hashKey(key []byte) []byte {
  173. h := newHasher()
  174. h.sha.Reset()
  175. h.sha.Write(key)
  176. buf := h.sha.Sum(t.hashKeyBuf[:0])
  177. returnHasherToPool(h)
  178. return buf
  179. }
  180. // getSecKeyCache returns the current secure key cache, creating a new one if
  181. // ownership changed (i.e. the current secure trie is a copy of another owning
  182. // the actual cache).
  183. func (t *SecureTrie) getSecKeyCache() map[string][]byte {
  184. if t != t.secKeyCacheOwner {
  185. t.secKeyCacheOwner = t
  186. t.secKeyCache = make(map[string][]byte)
  187. }
  188. return t.secKeyCache
  189. }