github.com/jimmyx0x/go-ethereum@v1.10.28/light/nodeset.go (about)

     1  // Copyright 2017 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  	"errors"
    21  	"sync"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/crypto"
    25  	"github.com/ethereum/go-ethereum/ethdb"
    26  	"github.com/ethereum/go-ethereum/rlp"
    27  )
    28  
    29  // NodeSet stores a set of trie nodes. It implements trie.Database and can also
    30  // act as a cache for another trie.Database.
    31  type NodeSet struct {
    32  	nodes map[string][]byte
    33  	order []string
    34  
    35  	dataSize int
    36  	lock     sync.RWMutex
    37  }
    38  
    39  // NewNodeSet creates an empty node set
    40  func NewNodeSet() *NodeSet {
    41  	return &NodeSet{
    42  		nodes: make(map[string][]byte),
    43  	}
    44  }
    45  
    46  // Put stores a new node in the set
    47  func (db *NodeSet) Put(key []byte, value []byte) error {
    48  	db.lock.Lock()
    49  	defer db.lock.Unlock()
    50  
    51  	if _, ok := db.nodes[string(key)]; ok {
    52  		return nil
    53  	}
    54  	keystr := string(key)
    55  
    56  	db.nodes[keystr] = common.CopyBytes(value)
    57  	db.order = append(db.order, keystr)
    58  	db.dataSize += len(value)
    59  
    60  	return nil
    61  }
    62  
    63  // Delete removes a node from the set
    64  func (db *NodeSet) Delete(key []byte) error {
    65  	db.lock.Lock()
    66  	defer db.lock.Unlock()
    67  
    68  	delete(db.nodes, string(key))
    69  	return nil
    70  }
    71  
    72  // Get returns a stored node
    73  func (db *NodeSet) Get(key []byte) ([]byte, error) {
    74  	db.lock.RLock()
    75  	defer db.lock.RUnlock()
    76  
    77  	if entry, ok := db.nodes[string(key)]; ok {
    78  		return entry, nil
    79  	}
    80  	return nil, errors.New("not found")
    81  }
    82  
    83  // Has returns true if the node set contains the given key
    84  func (db *NodeSet) Has(key []byte) (bool, error) {
    85  	_, err := db.Get(key)
    86  	return err == nil, nil
    87  }
    88  
    89  // KeyCount returns the number of nodes in the set
    90  func (db *NodeSet) KeyCount() int {
    91  	db.lock.RLock()
    92  	defer db.lock.RUnlock()
    93  
    94  	return len(db.nodes)
    95  }
    96  
    97  // DataSize returns the aggregated data size of nodes in the set
    98  func (db *NodeSet) DataSize() int {
    99  	db.lock.RLock()
   100  	defer db.lock.RUnlock()
   101  
   102  	return db.dataSize
   103  }
   104  
   105  // NodeList converts the node set to a NodeList
   106  func (db *NodeSet) NodeList() NodeList {
   107  	db.lock.RLock()
   108  	defer db.lock.RUnlock()
   109  
   110  	var values NodeList
   111  	for _, key := range db.order {
   112  		values = append(values, db.nodes[key])
   113  	}
   114  	return values
   115  }
   116  
   117  // Store writes the contents of the set to the given database
   118  func (db *NodeSet) Store(target ethdb.KeyValueWriter) {
   119  	db.lock.RLock()
   120  	defer db.lock.RUnlock()
   121  
   122  	for key, value := range db.nodes {
   123  		target.Put([]byte(key), value)
   124  	}
   125  }
   126  
   127  // NodeList stores an ordered list of trie nodes. It implements ethdb.KeyValueWriter.
   128  type NodeList []rlp.RawValue
   129  
   130  // Store writes the contents of the list to the given database
   131  func (n NodeList) Store(db ethdb.KeyValueWriter) {
   132  	for _, node := range n {
   133  		db.Put(crypto.Keccak256(node), node)
   134  	}
   135  }
   136  
   137  // NodeSet converts the node list to a NodeSet
   138  func (n NodeList) NodeSet() *NodeSet {
   139  	db := NewNodeSet()
   140  	n.Store(db)
   141  	return db
   142  }
   143  
   144  // Put stores a new node at the end of the list
   145  func (n *NodeList) Put(key []byte, value []byte) error {
   146  	*n = append(*n, value)
   147  	return nil
   148  }
   149  
   150  // Delete panics as there's no reason to remove a node from the list.
   151  func (n *NodeList) Delete(key []byte) error {
   152  	panic("not supported")
   153  }
   154  
   155  // DataSize returns the aggregated data size of nodes in the list
   156  func (n NodeList) DataSize() int {
   157  	var size int
   158  	for _, node := range n {
   159  		size += len(node)
   160  	}
   161  	return size
   162  }