2018-02-14 13:49:11 +01:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-10-24 15:19:09 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2023-10-10 10:30:47 +02:00
|
|
|
package trienode
|
2017-10-24 15:19:09 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-02-05 18:40:32 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2017-10-24 15:19:09 +02:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2023-10-10 10:30:47 +02:00
|
|
|
// ProofSet stores a set of trie nodes. It implements trie.Database and can also
|
2017-10-24 15:19:09 +02:00
|
|
|
// act as a cache for another trie.Database.
|
2023-10-10 10:30:47 +02:00
|
|
|
type ProofSet struct {
|
2018-02-11 14:57:46 +02:00
|
|
|
nodes map[string][]byte
|
|
|
|
order []string
|
|
|
|
|
2017-10-24 15:19:09 +02:00
|
|
|
dataSize int
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:30:47 +02:00
|
|
|
// NewProofSet creates an empty node set
|
|
|
|
func NewProofSet() *ProofSet {
|
|
|
|
return &ProofSet{
|
2018-02-11 14:57:46 +02:00
|
|
|
nodes: make(map[string][]byte),
|
2017-10-24 15:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put stores a new node in the set
|
2023-10-10 10:30:47 +02:00
|
|
|
func (db *ProofSet) Put(key []byte, value []byte) error {
|
2017-10-24 15:19:09 +02:00
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
2018-02-11 14:57:46 +02:00
|
|
|
if _, ok := db.nodes[string(key)]; ok {
|
|
|
|
return nil
|
2017-10-24 15:19:09 +02:00
|
|
|
}
|
2018-02-11 14:57:46 +02:00
|
|
|
keystr := string(key)
|
|
|
|
|
|
|
|
db.nodes[keystr] = common.CopyBytes(value)
|
|
|
|
db.order = append(db.order, keystr)
|
|
|
|
db.dataSize += len(value)
|
|
|
|
|
2017-10-24 15:19:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:48:31 +01:00
|
|
|
// Delete removes a node from the set
|
2023-10-10 10:30:47 +02:00
|
|
|
func (db *ProofSet) Delete(key []byte) error {
|
2019-03-26 15:48:31 +01:00
|
|
|
db.lock.Lock()
|
|
|
|
defer db.lock.Unlock()
|
|
|
|
|
|
|
|
delete(db.nodes, string(key))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-24 15:19:09 +02:00
|
|
|
// Get returns a stored node
|
2023-10-10 10:30:47 +02:00
|
|
|
func (db *ProofSet) Get(key []byte) ([]byte, error) {
|
2017-10-24 15:19:09 +02:00
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
|
|
|
|
2018-02-11 14:57:46 +02:00
|
|
|
if entry, ok := db.nodes[string(key)]; ok {
|
2017-10-24 15:19:09 +02:00
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has returns true if the node set contains the given key
|
2023-10-10 10:30:47 +02:00
|
|
|
func (db *ProofSet) Has(key []byte) (bool, error) {
|
2017-10-24 15:19:09 +02:00
|
|
|
_, err := db.Get(key)
|
|
|
|
return err == nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyCount returns the number of nodes in the set
|
2023-10-10 10:30:47 +02:00
|
|
|
func (db *ProofSet) KeyCount() int {
|
2017-10-24 15:19:09 +02:00
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
|
|
|
|
2018-02-11 14:57:46 +02:00
|
|
|
return len(db.nodes)
|
2017-10-24 15:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DataSize returns the aggregated data size of nodes in the set
|
2023-10-10 10:30:47 +02:00
|
|
|
func (db *ProofSet) DataSize() int {
|
2017-10-24 15:19:09 +02:00
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
|
|
|
|
|
|
|
return db.dataSize
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:30:47 +02:00
|
|
|
// List converts the node set to a ProofList
|
|
|
|
func (db *ProofSet) List() ProofList {
|
2017-10-24 15:19:09 +02:00
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
|
|
|
|
2023-10-10 10:30:47 +02:00
|
|
|
var values ProofList
|
2018-02-11 14:57:46 +02:00
|
|
|
for _, key := range db.order {
|
|
|
|
values = append(values, db.nodes[key])
|
2017-10-24 15:19:09 +02:00
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store writes the contents of the set to the given database
|
2023-10-10 10:30:47 +02:00
|
|
|
func (db *ProofSet) Store(target ethdb.KeyValueWriter) {
|
2017-10-24 15:19:09 +02:00
|
|
|
db.lock.RLock()
|
|
|
|
defer db.lock.RUnlock()
|
|
|
|
|
2018-02-11 14:57:46 +02:00
|
|
|
for key, value := range db.nodes {
|
2017-10-24 15:19:09 +02:00
|
|
|
target.Put([]byte(key), value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:30:47 +02:00
|
|
|
// ProofList stores an ordered list of trie nodes. It implements ethdb.KeyValueWriter.
|
|
|
|
type ProofList []rlp.RawValue
|
2017-10-24 15:19:09 +02:00
|
|
|
|
|
|
|
// Store writes the contents of the list to the given database
|
2023-10-10 10:30:47 +02:00
|
|
|
func (n ProofList) Store(db ethdb.KeyValueWriter) {
|
2017-10-24 15:19:09 +02:00
|
|
|
for _, node := range n {
|
|
|
|
db.Put(crypto.Keccak256(node), node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:30:47 +02:00
|
|
|
// Set converts the node list to a ProofSet
|
|
|
|
func (n ProofList) Set() *ProofSet {
|
|
|
|
db := NewProofSet()
|
2017-10-24 15:19:09 +02:00
|
|
|
n.Store(db)
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put stores a new node at the end of the list
|
2023-10-10 10:30:47 +02:00
|
|
|
func (n *ProofList) Put(key []byte, value []byte) error {
|
2017-10-24 15:19:09 +02:00
|
|
|
*n = append(*n, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:48:31 +01:00
|
|
|
// Delete panics as there's no reason to remove a node from the list.
|
2023-10-10 10:30:47 +02:00
|
|
|
func (n *ProofList) Delete(key []byte) error {
|
2019-03-26 15:48:31 +01:00
|
|
|
panic("not supported")
|
|
|
|
}
|
|
|
|
|
2017-10-24 15:19:09 +02:00
|
|
|
// DataSize returns the aggregated data size of nodes in the list
|
2023-10-10 10:30:47 +02:00
|
|
|
func (n ProofList) DataSize() int {
|
2017-10-24 15:19:09 +02:00
|
|
|
var size int
|
|
|
|
for _, node := range n {
|
|
|
|
size += len(node)
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|