github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/light/nodeset.go (about)

     1  // Copyright 2014 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package light
    18  
    19  import (
    20  	"errors"
    21  	"sync"
    22  
    23  	"github.com/SmartMeshFoundation/Spectrum/common"
    24  	"github.com/SmartMeshFoundation/Spectrum/crypto"
    25  	"github.com/SmartMeshFoundation/Spectrum/rlp"
    26  	"github.com/SmartMeshFoundation/Spectrum/trie"
    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  	db       map[string][]byte
    33  	dataSize int
    34  	lock     sync.RWMutex
    35  }
    36  
    37  // NewNodeSet creates an empty node set
    38  func NewNodeSet() *NodeSet {
    39  	return &NodeSet{
    40  		db: make(map[string][]byte),
    41  	}
    42  }
    43  
    44  // Put stores a new node in the set
    45  func (db *NodeSet) Put(key []byte, value []byte) error {
    46  	db.lock.Lock()
    47  	defer db.lock.Unlock()
    48  
    49  	if _, ok := db.db[string(key)]; !ok {
    50  		db.db[string(key)] = common.CopyBytes(value)
    51  		db.dataSize += len(value)
    52  	}
    53  	return nil
    54  }
    55  
    56  // Get returns a stored node
    57  func (db *NodeSet) Get(key []byte) ([]byte, error) {
    58  	db.lock.RLock()
    59  	defer db.lock.RUnlock()
    60  
    61  	if entry, ok := db.db[string(key)]; ok {
    62  		return entry, nil
    63  	}
    64  	return nil, errors.New("not found")
    65  }
    66  
    67  // Has returns true if the node set contains the given key
    68  func (db *NodeSet) Has(key []byte) (bool, error) {
    69  	_, err := db.Get(key)
    70  	return err == nil, nil
    71  }
    72  
    73  // KeyCount returns the number of nodes in the set
    74  func (db *NodeSet) KeyCount() int {
    75  	db.lock.RLock()
    76  	defer db.lock.RUnlock()
    77  
    78  	return len(db.db)
    79  }
    80  
    81  // DataSize returns the aggregated data size of nodes in the set
    82  func (db *NodeSet) DataSize() int {
    83  	db.lock.RLock()
    84  	defer db.lock.RUnlock()
    85  
    86  	return db.dataSize
    87  }
    88  
    89  // NodeList converts the node set to a NodeList
    90  func (db *NodeSet) NodeList() NodeList {
    91  	db.lock.RLock()
    92  	defer db.lock.RUnlock()
    93  
    94  	var values NodeList
    95  	for _, value := range db.db {
    96  		values = append(values, value)
    97  	}
    98  	return values
    99  }
   100  
   101  // Store writes the contents of the set to the given database
   102  func (db *NodeSet) Store(target trie.Database) {
   103  	db.lock.RLock()
   104  	defer db.lock.RUnlock()
   105  
   106  	for key, value := range db.db {
   107  		target.Put([]byte(key), value)
   108  	}
   109  }
   110  
   111  // NodeList stores an ordered list of trie nodes. It implements trie.DatabaseWriter.
   112  type NodeList []rlp.RawValue
   113  
   114  // Store writes the contents of the list to the given database
   115  func (n NodeList) Store(db trie.Database) {
   116  	for _, node := range n {
   117  		db.Put(crypto.Keccak256(node), node)
   118  	}
   119  }
   120  
   121  // NodeSet converts the node list to a NodeSet
   122  func (n NodeList) NodeSet() *NodeSet {
   123  	db := NewNodeSet()
   124  	n.Store(db)
   125  	return db
   126  }
   127  
   128  // Put stores a new node at the end of the list
   129  func (n *NodeList) Put(key []byte, value []byte) error {
   130  	*n = append(*n, value)
   131  	return nil
   132  }
   133  
   134  // DataSize returns the aggregated data size of nodes in the list
   135  func (n NodeList) DataSize() int {
   136  	var size int
   137  	for _, node := range n {
   138  		size += len(node)
   139  	}
   140  	return size
   141  }