secure_trie.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "hash"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/crypto/sha3"
  21. )
  22. var secureKeyPrefix = []byte("secure-key-")
  23. // SecureTrie wraps a trie with key hashing. In a secure trie, all
  24. // access operations hash the key using keccak256. This prevents
  25. // calling code from creating long chains of nodes that
  26. // increase the access time.
  27. //
  28. // Contrary to a regular trie, a SecureTrie can only be created with
  29. // New and must have an attached database. The database also stores
  30. // the preimage of each key.
  31. //
  32. // SecureTrie is not safe for concurrent use.
  33. type SecureTrie struct {
  34. *Trie
  35. hash hash.Hash
  36. secKeyBuf []byte
  37. hashKeyBuf []byte
  38. }
  39. // NewSecure creates a trie with an existing root node from db.
  40. //
  41. // If root is the zero hash or the sha3 hash of an empty string, the
  42. // trie is initially empty. Otherwise, New will panics if db is nil
  43. // and returns ErrMissingRoot if the root node cannpt be found.
  44. // Accessing the trie loads nodes from db on demand.
  45. func NewSecure(root common.Hash, db Database) (*SecureTrie, error) {
  46. if db == nil {
  47. panic("NewSecure called with nil database")
  48. }
  49. trie, err := New(root, db)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &SecureTrie{Trie: trie}, nil
  54. }
  55. // Get returns the value for key stored in the trie.
  56. // The value bytes must not be modified by the caller.
  57. func (t *SecureTrie) Get(key []byte) []byte {
  58. return t.Trie.Get(t.hashKey(key))
  59. }
  60. // Update associates key with value in the trie. Subsequent calls to
  61. // Get will return value. If value has length zero, any existing value
  62. // is deleted from the trie and calls to Get will return nil.
  63. //
  64. // The value bytes must not be modified by the caller while they are
  65. // stored in the trie.
  66. func (t *SecureTrie) Update(key, value []byte) {
  67. hk := t.hashKey(key)
  68. t.Trie.Update(hk, value)
  69. t.Trie.db.Put(t.secKey(hk), key)
  70. }
  71. // Delete removes any existing value for key from the trie.
  72. func (t *SecureTrie) Delete(key []byte) {
  73. t.Trie.Delete(t.hashKey(key))
  74. }
  75. // GetKey returns the sha3 preimage of a hashed key that was
  76. // previously used to store a value.
  77. func (t *SecureTrie) GetKey(shaKey []byte) []byte {
  78. key, _ := t.Trie.db.Get(t.secKey(shaKey))
  79. return key
  80. }
  81. func (t *SecureTrie) secKey(key []byte) []byte {
  82. t.secKeyBuf = append(t.secKeyBuf[:0], secureKeyPrefix...)
  83. t.secKeyBuf = append(t.secKeyBuf, key...)
  84. return t.secKeyBuf
  85. }
  86. func (t *SecureTrie) hashKey(key []byte) []byte {
  87. if t.hash == nil {
  88. t.hash = sha3.NewKeccak256()
  89. t.hashKeyBuf = make([]byte, 32)
  90. }
  91. t.hash.Reset()
  92. t.hash.Write(key)
  93. t.hashKeyBuf = t.hash.Sum(t.hashKeyBuf[:0])
  94. return t.hashKeyBuf
  95. }