aboutsummaryrefslogtreecommitdiffstats
path: root/light/nodeset.go
blob: 3662596785c79f85250d949eb653b4872f9827d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package light

import (
    "errors"
    "sync"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/ethdb"
    "github.com/ethereum/go-ethereum/rlp"
)

// NodeSet stores a set of trie nodes. It implements trie.Database and can also
// act as a cache for another trie.Database.
type NodeSet struct {
    nodes map[string][]byte
    order []string

    dataSize int
    lock     sync.RWMutex
}

// NewNodeSet creates an empty node set
func NewNodeSet() *NodeSet {
    return &NodeSet{
        nodes: make(map[string][]byte),
    }
}

// Put stores a new node in the set
func (db *NodeSet) Put(key []byte, value []byte) error {
    db.lock.Lock()
    defer db.lock.Unlock()

    if _, ok := db.nodes[string(key)]; ok {
        return nil
    }
    keystr := string(key)

    db.nodes[keystr] = common.CopyBytes(value)
    db.order = append(db.order, keystr)
    db.dataSize += len(value)

    return nil
}

// Delete removes a node from the set
func (db *NodeSet) Delete(key []byte) error {
    db.lock.Lock()
    defer db.lock.Unlock()

    delete(db.nodes, string(key))
    return nil
}

// Get returns a stored node
func (db *NodeSet) Get(key []byte) ([]byte, error) {
    db.lock.RLock()
    defer db.lock.RUnlock()

    if entry, ok := db.nodes[string(key)]; ok {
        return entry, nil
    }
    return nil, errors.New("not found")
}

// Has returns true if the node set contains the given key
func (db *NodeSet) Has(key []byte) (bool, error) {
    _, err := db.Get(key)
    return err == nil, nil
}

// KeyCount returns the number of nodes in the set
func (db *NodeSet) KeyCount() int {
    db.lock.RLock()
    defer db.lock.RUnlock()

    return len(db.nodes)
}

// DataSize returns the aggregated data size of nodes in the set
func (db *NodeSet) DataSize() int {
    db.lock.RLock()
    defer db.lock.RUnlock()

    return db.dataSize
}

// NodeList converts the node set to a NodeList
func (db *NodeSet) NodeList() NodeList {
    db.lock.RLock()
    defer db.lock.RUnlock()

    var values NodeList
    for _, key := range db.order {
        values = append(values, db.nodes[key])
    }
    return values
}

// Store writes the contents of the set to the given database
func (db *NodeSet) Store(target ethdb.KeyValueWriter) {
    db.lock.RLock()
    defer db.lock.RUnlock()

    for key, value := range db.nodes {
        target.Put([]byte(key), value)
    }
}

// NodeList stores an ordered list of trie nodes. It implements ethdb.KeyValueWriter.
type NodeList []rlp.RawValue

// Store writes the contents of the list to the given database
func (n NodeList) Store(db ethdb.KeyValueWriter) {
    for _, node := range n {
        db.Put(crypto.Keccak256(node), node)
    }
}

// NodeSet converts the node list to a NodeSet
func (n NodeList) NodeSet() *NodeSet {
    db := NewNodeSet()
    n.Store(db)
    return db
}

// Put stores a new node at the end of the list
func (n *NodeList) Put(key []byte, value []byte) error {
    *n = append(*n, value)
    return nil
}

// Delete panics as there's no reason to remove a node from the list.
func (n *NodeList) Delete(key []byte) error {
    panic("not supported")
}

// DataSize returns the aggregated data size of nodes in the list
func (n NodeList) DataSize() int {
    var size int
    for _, node := range n {
        size += len(node)
    }
    return size
}