github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/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/vantum/vantum/common"
    24  	"github.com/vantum/vantum/crypto"
    25  	"github.com/vantum/vantum/ethdb"
    26  	"github.com/vantum/vantum/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  // Get returns a stored node
    64  func (db *NodeSet) Get(key []byte) ([]byte, error) {
    65  	db.lock.RLock()
    66  	defer db.lock.RUnlock()
    67  
    68  	if entry, ok := db.nodes[string(key)]; ok {
    69  		return entry, nil
    70  	}
    71  	return nil, errors.New("not found")
    72  }
    73  
    74  // Has returns true if the node set contains the given key
    75  func (db *NodeSet) Has(key []byte) (bool, error) {
    76  	_, err := db.Get(key)
    77  	return err == nil, nil
    78  }
    79  
    80  // KeyCount returns the number of nodes in the set
    81  func (db *NodeSet) KeyCount() int {
    82  	db.lock.RLock()
    83  	defer db.lock.RUnlock()
    84  
    85  	return len(db.nodes)
    86  }
    87  
    88  // DataSize returns the aggregated data size of nodes in the set
    89  func (db *NodeSet) DataSize() int {
    90  	db.lock.RLock()
    91  	defer db.lock.RUnlock()
    92  
    93  	return db.dataSize
    94  }
    95  
    96  // NodeList converts the node set to a NodeList
    97  func (db *NodeSet) NodeList() NodeList {
    98  	db.lock.RLock()
    99  	defer db.lock.RUnlock()
   100  
   101  	var values NodeList
   102  	for _, key := range db.order {
   103  		values = append(values, db.nodes[key])
   104  	}
   105  	return values
   106  }
   107  
   108  // Store writes the contents of the set to the given database
   109  func (db *NodeSet) Store(target ethdb.Putter) {
   110  	db.lock.RLock()
   111  	defer db.lock.RUnlock()
   112  
   113  	for key, value := range db.nodes {
   114  		target.Put([]byte(key), value)
   115  	}
   116  }
   117  
   118  // NodeList stores an ordered list of trie nodes. It implements ethdb.Putter.
   119  type NodeList []rlp.RawValue
   120  
   121  // Store writes the contents of the list to the given database
   122  func (n NodeList) Store(db ethdb.Putter) {
   123  	for _, node := range n {
   124  		db.Put(crypto.Keccak256(node), node)
   125  	}
   126  }
   127  
   128  // NodeSet converts the node list to a NodeSet
   129  func (n NodeList) NodeSet() *NodeSet {
   130  	db := NewNodeSet()
   131  	n.Store(db)
   132  	return db
   133  }
   134  
   135  // Put stores a new node at the end of the list
   136  func (n *NodeList) Put(key []byte, value []byte) error {
   137  	*n = append(*n, value)
   138  	return nil
   139  }
   140  
   141  // DataSize returns the aggregated data size of nodes in the list
   142  func (n NodeList) DataSize() int {
   143  	var size int
   144  	for _, node := range n {
   145  		size += len(node)
   146  	}
   147  	return size
   148  }