trie.go 3.5 KB

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