go-ethereum/trie/secure_trie.go

148 lines
4.7 KiB
Go
Raw Normal View History

2015-07-06 19:54:22 -05:00
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
2015-07-06 19:54:22 -05:00
//
// The go-ethereum library is free software: you can redistribute it and/or modify
2015-07-06 19:54:22 -05:00
// 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,
2015-07-06 19:54:22 -05:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-07-06 19:54:22 -05:00
// 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/>.
2015-07-06 19:54:22 -05:00
2015-02-28 13:52:10 -06:00
package trie
2015-07-05 18:19:48 -05:00
import (
"hash"
2015-02-28 13:52:10 -06:00
2015-07-05 18:19:48 -05:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
2015-07-05 18:19:48 -05:00
)
2015-03-24 09:14:03 -05:00
2015-07-05 18:19:48 -05:00
var secureKeyPrefix = []byte("secure-key-")
// SecureTrie wraps a trie with key hashing. In a secure trie, all
// access operations hash the key using keccak256. This prevents
// calling code from creating long chains of nodes that
// increase the access time.
//
// Contrary to a regular trie, a SecureTrie can only be created with
// New and must have an attached database. The database also stores
// the preimage of each key.
//
// SecureTrie is not safe for concurrent use.
2015-02-28 13:52:10 -06:00
type SecureTrie struct {
*Trie
2015-07-05 18:19:48 -05:00
hash hash.Hash
secKeyBuf []byte
hashKeyBuf []byte
2015-02-28 13:52:10 -06:00
}
2015-07-05 18:19:48 -05:00
// NewSecure creates a trie with an existing root node from db.
//
// If root is the zero hash or the sha3 hash of an empty string, the
// trie is initially empty. Otherwise, New will panic if db is nil
// and returns MissingNodeError if the root node cannot be found.
2015-07-05 18:19:48 -05:00
// Accessing the trie loads nodes from db on demand.
func NewSecure(root common.Hash, db Database) (*SecureTrie, error) {
if db == nil {
panic("NewSecure called with nil database")
}
trie, err := New(root, db)
if err != nil {
return nil, err
}
return &SecureTrie{Trie: trie}, nil
2015-02-28 13:52:10 -06:00
}
2015-07-05 18:19:48 -05:00
// Get returns the value for key stored in the trie.
// The value bytes must not be modified by the caller.
func (t *SecureTrie) Get(key []byte) []byte {
res, err := t.TryGet(key)
if err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err)
}
return res
}
// TryGet returns the value for key stored in the trie.
// The value bytes must not be modified by the caller.
// If a node was not found in the database, a MissingNodeError is returned.
func (t *SecureTrie) TryGet(key []byte) ([]byte, error) {
return t.Trie.TryGet(t.hashKey(key))
2015-02-28 13:52:10 -06:00
}
2015-07-05 18:19:48 -05:00
// Update associates key with value in the trie. Subsequent calls to
// Get will return value. If value has length zero, any existing value
// is deleted from the trie and calls to Get will return nil.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
func (t *SecureTrie) Update(key, value []byte) {
if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err)
}
}
// TryUpdate associates key with value in the trie. Subsequent calls to
// Get will return value. If value has length zero, any existing value
// is deleted from the trie and calls to Get will return nil.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
//
// If a node was not found in the database, a MissingNodeError is returned.
func (t *SecureTrie) TryUpdate(key, value []byte) error {
2015-07-05 18:19:48 -05:00
hk := t.hashKey(key)
err := t.Trie.TryUpdate(hk, value)
if err != nil {
return err
}
2015-07-05 18:19:48 -05:00
t.Trie.db.Put(t.secKey(hk), key)
return nil
2015-02-28 13:52:10 -06:00
}
2015-07-05 18:19:48 -05:00
// Delete removes any existing value for key from the trie.
func (t *SecureTrie) Delete(key []byte) {
if err := t.TryDelete(key); err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err)
}
}
// TryDelete removes any existing value for key from the trie.
// If a node was not found in the database, a MissingNodeError is returned.
func (t *SecureTrie) TryDelete(key []byte) error {
return t.Trie.TryDelete(t.hashKey(key))
2015-02-28 13:52:10 -06:00
}
2015-07-05 18:19:48 -05:00
// GetKey returns the sha3 preimage of a hashed key that was
// previously used to store a value.
func (t *SecureTrie) GetKey(shaKey []byte) []byte {
key, _ := t.Trie.db.Get(t.secKey(shaKey))
return key
2015-02-28 13:52:10 -06:00
}
2015-03-03 05:25:44 -06:00
2015-07-05 18:19:48 -05:00
func (t *SecureTrie) secKey(key []byte) []byte {
t.secKeyBuf = append(t.secKeyBuf[:0], secureKeyPrefix...)
t.secKeyBuf = append(t.secKeyBuf, key...)
return t.secKeyBuf
2015-03-03 05:25:44 -06:00
}
2015-03-24 09:14:03 -05:00
2015-07-05 18:19:48 -05:00
func (t *SecureTrie) hashKey(key []byte) []byte {
if t.hash == nil {
t.hash = sha3.NewKeccak256()
t.hashKeyBuf = make([]byte, 32)
}
t.hash.Reset()
t.hash.Write(key)
t.hashKeyBuf = t.hash.Sum(t.hashKeyBuf[:0])
return t.hashKeyBuf
2015-03-24 09:14:03 -05:00
}