trie.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 light
  17. import (
  18. "context"
  19. "github.com/ethereum/go-ethereum/crypto"
  20. "github.com/ethereum/go-ethereum/ethdb"
  21. "github.com/ethereum/go-ethereum/trie"
  22. )
  23. // LightTrie is an ODR-capable wrapper around trie.SecureTrie
  24. type LightTrie struct {
  25. trie *trie.SecureTrie
  26. id *TrieID
  27. odr OdrBackend
  28. db ethdb.Database
  29. }
  30. // NewLightTrie creates a new LightTrie instance. It doesn't instantly try to
  31. // access the db or network and retrieve the root node, it only initializes its
  32. // encapsulated SecureTrie at the first actual operation.
  33. func NewLightTrie(id *TrieID, odr OdrBackend, useFakeMap bool) *LightTrie {
  34. return &LightTrie{
  35. // SecureTrie is initialized before first request
  36. id: id,
  37. odr: odr,
  38. db: odr.Database(),
  39. }
  40. }
  41. // retrieveKey retrieves a single key, returns true and stores nodes in local
  42. // database if successful
  43. func (t *LightTrie) retrieveKey(ctx context.Context, key []byte) bool {
  44. r := &TrieRequest{Id: t.id, Key: crypto.Keccak256(key)}
  45. return t.odr.Retrieve(ctx, r) == nil
  46. }
  47. // do tries and retries to execute a function until it returns with no error or
  48. // an error type other than MissingNodeError
  49. func (t *LightTrie) do(ctx context.Context, key []byte, fn func() error) error {
  50. err := fn()
  51. for err != nil {
  52. if _, ok := err.(*trie.MissingNodeError); !ok {
  53. return err
  54. }
  55. if !t.retrieveKey(ctx, key) {
  56. break
  57. }
  58. err = fn()
  59. }
  60. return err
  61. }
  62. // Get returns the value for key stored in the trie.
  63. // The value bytes must not be modified by the caller.
  64. func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) {
  65. err = t.do(ctx, key, func() (err error) {
  66. if t.trie == nil {
  67. t.trie, err = trie.NewSecure(t.id.Root, t.db, 0)
  68. }
  69. if err == nil {
  70. res, err = t.trie.TryGet(key)
  71. }
  72. return
  73. })
  74. return
  75. }
  76. // Update associates key with value in the trie. Subsequent calls to
  77. // Get will return value. If value has length zero, any existing value
  78. // is deleted from the trie and calls to Get will return nil.
  79. //
  80. // The value bytes must not be modified by the caller while they are
  81. // stored in the trie.
  82. func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) {
  83. err = t.do(ctx, key, func() (err error) {
  84. if t.trie == nil {
  85. t.trie, err = trie.NewSecure(t.id.Root, t.db, 0)
  86. }
  87. if err == nil {
  88. err = t.trie.TryUpdate(key, value)
  89. }
  90. return
  91. })
  92. return
  93. }
  94. // Delete removes any existing value for key from the trie.
  95. func (t *LightTrie) Delete(ctx context.Context, key []byte) (err error) {
  96. err = t.do(ctx, key, func() (err error) {
  97. if t.trie == nil {
  98. t.trie, err = trie.NewSecure(t.id.Root, t.db, 0)
  99. }
  100. if err == nil {
  101. err = t.trie.TryDelete(key)
  102. }
  103. return
  104. })
  105. return
  106. }