github.com/ylsGit/go-ethereum@v1.6.5/light/trie.go (about)

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