trie.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/common"
  19. "github.com/ethereum/go-ethereum/ethdb"
  20. "github.com/ethereum/go-ethereum/trie"
  21. "golang.org/x/net/context"
  22. )
  23. // LightTrie is an ODR-capable wrapper around trie.SecureTrie
  24. type LightTrie struct {
  25. trie *trie.SecureTrie
  26. originalRoot common.Hash
  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(root common.Hash, odr OdrBackend, useFakeMap bool) *LightTrie {
  34. return &LightTrie{
  35. // SecureTrie is initialized before first request
  36. originalRoot: root,
  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{root: t.originalRoot, key: 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, fallbackKey []byte, fn func() error) error {
  50. err := fn()
  51. for err != nil {
  52. mn, ok := err.(*trie.MissingNodeError)
  53. if !ok {
  54. return err
  55. }
  56. var key []byte
  57. if mn.PrefixLen+mn.SuffixLen > 0 {
  58. key = mn.Key
  59. } else {
  60. key = fallbackKey
  61. }
  62. if !t.retrieveKey(ctx, key) {
  63. break
  64. }
  65. err = fn()
  66. }
  67. return err
  68. }
  69. // Get returns the value for key stored in the trie.
  70. // The value bytes must not be modified by the caller.
  71. func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) {
  72. err = t.do(ctx, key, func() (err error) {
  73. if t.trie == nil {
  74. t.trie, err = trie.NewSecure(t.originalRoot, t.db, 0)
  75. }
  76. if err == nil {
  77. res, err = t.trie.TryGet(key)
  78. }
  79. return
  80. })
  81. return
  82. }
  83. // Update associates key with value in the trie. Subsequent calls to
  84. // Get will return value. If value has length zero, any existing value
  85. // is deleted from the trie and calls to Get will return nil.
  86. //
  87. // The value bytes must not be modified by the caller while they are
  88. // stored in the trie.
  89. func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) {
  90. err = t.do(ctx, key, func() (err error) {
  91. if t.trie == nil {
  92. t.trie, err = trie.NewSecure(t.originalRoot, t.db, 0)
  93. }
  94. if err == nil {
  95. err = t.trie.TryUpdate(key, value)
  96. }
  97. return
  98. })
  99. return
  100. }
  101. // Delete removes any existing value for key from the trie.
  102. func (t *LightTrie) Delete(ctx context.Context, key []byte) (err error) {
  103. err = t.do(ctx, key, func() (err error) {
  104. if t.trie == nil {
  105. t.trie, err = trie.NewSecure(t.originalRoot, t.db, 0)
  106. }
  107. if err == nil {
  108. err = t.trie.TryDelete(key)
  109. }
  110. return
  111. })
  112. return
  113. }