Remove NodeIterator from the storage interface

This commit is contained in:
Nick Johnson 2016-10-24 11:58:50 +01:00
parent cee7eae5dc
commit 2fb08643cb
11 changed files with 42 additions and 51 deletions

View file

@ -76,7 +76,7 @@ func (it *NodeIterator) step() error {
} }
// Initialize the iterator if we've just started // Initialize the iterator if we've just started
if it.stateIt == nil { if it.stateIt == nil {
it.stateIt = it.state.trie.NodeIterator() it.stateIt = trie.NewNodeIterator(it.state.trie)
} }
// If we had data nodes previously, we surely have at least state nodes // If we had data nodes previously, we surely have at least state nodes
if it.dataIt != nil { if it.dataIt != nil {
@ -119,7 +119,7 @@ func (it *NodeIterator) step() error {
if err != nil { if err != nil {
return err return err
} }
it.dataIt = dataTrie.NodeIterator() it.dataIt = trie.NewNodeIterator(dataTrie)
if !it.dataIt.Next() { if !it.dataIt.Next() {
it.dataIt = nil it.dataIt = nil
} }

View file

@ -76,7 +76,7 @@ type StateObject struct {
dbErr error dbErr error
// Write caches. // Write caches.
trie *trie.SimpleTrie // storage trie, which becomes non-nil on first access trie *trie.Trie // storage trie, which becomes non-nil on first access
storage *trie.SecureTrie // Interface to storage storage *trie.SecureTrie // Interface to storage
code Code // contract bytecode, which gets set when code is loaded code Code // contract bytecode, which gets set when code is loaded

View file

@ -62,9 +62,9 @@ type revision struct {
// * Accounts // * Accounts
type StateDB struct { type StateDB struct {
db ethdb.Database db ethdb.Database
trie *trie.SimpleTrie trie *trie.Trie
storage *trie.SecureTrie storage *trie.SecureTrie
pastTries []*trie.SimpleTrie pastTries []*trie.Trie
codeSizeCache *lru.Cache codeSizeCache *lru.Cache
// This map holds 'live' objects, which will get modified while processing a state transition. // This map holds 'live' objects, which will get modified while processing a state transition.
@ -155,7 +155,7 @@ func (self *StateDB) Reset(root common.Hash) error {
// openTrie creates a trie. It uses an existing trie if one is available // openTrie creates a trie. It uses an existing trie if one is available
// from the journal if available. // from the journal if available.
func (self *StateDB) openTrie(root common.Hash) (*trie.SimpleTrie, error) { func (self *StateDB) openTrie(root common.Hash) (*trie.Trie, error) {
for i := len(self.pastTries) - 1; i >= 0; i-- { for i := len(self.pastTries) - 1; i >= 0; i-- {
if self.pastTries[i].Hash() == root { if self.pastTries[i].Hash() == root {
tr := *self.pastTries[i] tr := *self.pastTries[i]
@ -165,7 +165,7 @@ func (self *StateDB) openTrie(root common.Hash) (*trie.SimpleTrie, error) {
return trie.New(root, self.db, MaxTrieCacheGen) return trie.New(root, self.db, MaxTrieCacheGen)
} }
func (self *StateDB) pushTrie(t *trie.SimpleTrie) { func (self *StateDB) pushTrie(t *trie.Trie) {
self.lock.Lock() self.lock.Lock()
defer self.lock.Unlock() defer self.lock.Unlock()

View file

@ -70,10 +70,6 @@ func (dc *DirectCache) Iterator() *Iterator {
return dc.storage.Iterator() return dc.storage.Iterator()
} }
func (dc *DirectCache) NodeIterator() *NodeIterator {
return dc.storage.NodeIterator()
}
func (dc *DirectCache) makeKey(key []byte) []byte { func (dc *DirectCache) makeKey(key []byte) []byte {
return append(dc.keyPrefix, key...) return append(dc.keyPrefix, key...)
} }

View file

@ -20,7 +20,7 @@ import "github.com/ethereum/go-ethereum/common"
// Iterator is a key-value trie iterator that traverses a Trie. // Iterator is a key-value trie iterator that traverses a Trie.
type Iterator struct { type Iterator struct {
trie *SimpleTrie trie *Trie
nodeIt *NodeIterator nodeIt *NodeIterator
keyBuf []byte keyBuf []byte
@ -29,7 +29,7 @@ type Iterator struct {
} }
// NewIterator creates a new key-value iterator. // NewIterator creates a new key-value iterator.
func NewIterator(trie *SimpleTrie) *Iterator { func NewIterator(trie *Trie) *Iterator {
return &Iterator{ return &Iterator{
trie: trie, trie: trie,
nodeIt: NewNodeIterator(trie), nodeIt: NewNodeIterator(trie),
@ -82,7 +82,7 @@ type nodeIteratorState struct {
// NodeIterator is an iterator to traverse the trie post-order. // NodeIterator is an iterator to traverse the trie post-order.
type NodeIterator struct { type NodeIterator struct {
trie *SimpleTrie // Trie being iterated trie *Trie // Trie being iterated
stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state
Hash common.Hash // Hash of the current node being iterated (nil if not standalone) Hash common.Hash // Hash of the current node being iterated (nil if not standalone)
@ -95,7 +95,7 @@ type NodeIterator struct {
} }
// NewNodeIterator creates an post-order trie iterator. // NewNodeIterator creates an post-order trie iterator.
func NewNodeIterator(trie *SimpleTrie) *NodeIterator { func NewNodeIterator(trie *Trie) *NodeIterator {
if trie.Hash() == emptyState { if trie.Hash() == emptyState {
return new(NodeIterator) return new(NodeIterator)
} }

View file

@ -37,7 +37,7 @@ import (
// contains all nodes of the longest existing prefix of the key // contains all nodes of the longest existing prefix of the key
// (at least the root node), ending with the node that proves the // (at least the root node), ending with the node that proves the
// absence of the key. // absence of the key.
func (t *SimpleTrie) Prove(key []byte) []rlp.RawValue { func (t *Trie) Prove(key []byte) []rlp.RawValue {
// Collect all nodes on the path to key. // Collect all nodes on the path to key.
key = compactHexDecode(key) key = compactHexDecode(key)
nodes := []node{} nodes := []node{}

View file

@ -129,7 +129,7 @@ func BenchmarkVerifyProof(b *testing.B) {
} }
} }
func randomTrie(n int) (*SimpleTrie, map[string]*kv) { func randomTrie(n int) (*Trie, map[string]*kv) {
trie, _ := New(common.Hash{}, nil, 0) trie, _ := New(common.Hash{}, nil, 0)
vals := make(map[string]*kv) vals := make(map[string]*kv)
for i := byte(0); i < 100; i++ { for i := byte(0); i < 100; i++ {

View file

@ -27,7 +27,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
) )
func newEmptySecure() (*SimpleTrie, *SecureTrie) { func newEmptySecure() (*Trie, *SecureTrie) {
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
tr, _ := New(common.Hash{}, db, 0) tr, _ := New(common.Hash{}, db, 0)
st := NewSecure(tr, db) st := NewSecure(tr, db)

View file

@ -25,7 +25,7 @@ import (
) )
// makeTestTrie create a sample test trie to test node-wise reconstruction. // makeTestTrie create a sample test trie to test node-wise reconstruction.
func makeTestTrie() (ethdb.Database, *SimpleTrie, map[string][]byte) { func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
// Create an empty trie // Create an empty trie
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
trie, _ := New(common.Hash{}, db, 0) trie, _ := New(common.Hash{}, db, 0)
@ -91,7 +91,7 @@ func TestEmptyTrieSync(t *testing.T) {
emptyA, _ := New(common.Hash{}, nil, 0) emptyA, _ := New(common.Hash{}, nil, 0)
emptyB, _ := New(emptyRoot, nil, 0) emptyB, _ := New(emptyRoot, nil, 0)
for i, trie := range []*SimpleTrie{emptyA, emptyB} { for i, trie := range []*Trie{emptyA, emptyB} {
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
if req := NewTrieSync(common.BytesToHash(trie.Root()), db, nil).Missing(1); len(req) != 0 { if req := NewTrieSync(common.BytesToHash(trie.Root()), db, nil).Missing(1); len(req) != 0 {
t.Errorf("test %d: content requested for empty trie: %v", i, req) t.Errorf("test %d: content requested for empty trie: %v", i, req)

View file

@ -80,7 +80,6 @@ type DatabaseWriter interface {
// trie is not safe for concurrent use. // trie is not safe for concurrent use.
type Storage interface { type Storage interface {
Iterator() *Iterator Iterator() *Iterator
NodeIterator() *NodeIterator
Get(key []byte) []byte Get(key []byte) []byte
TryGet(key []byte) ([]byte, error) TryGet(key []byte) ([]byte, error)
Update(key, value []byte) Update(key, value []byte)
@ -91,7 +90,7 @@ type Storage interface {
CommitTo(db DatabaseWriter) (root common.Hash, err error) CommitTo(db DatabaseWriter) (root common.Hash, err error)
} }
type SimpleTrie struct { type Trie struct {
root node root node
db Database db Database
originalRoot common.Hash originalRoot common.Hash
@ -104,7 +103,7 @@ type SimpleTrie struct {
} }
// newFlag returns the cache flag value for a newly created node. // newFlag returns the cache flag value for a newly created node.
func (t *SimpleTrie) newFlag() nodeFlag { func (t *Trie) newFlag() nodeFlag {
return nodeFlag{dirty: true, gen: t.cachegen} return nodeFlag{dirty: true, gen: t.cachegen}
} }
@ -117,11 +116,11 @@ func (t *SimpleTrie) newFlag() nodeFlag {
// //
// cacheLimit is the number of 'cache generations' to keep. // cacheLimit is the number of 'cache generations' to keep.
// A cache generations is created by a call to Commit. // A cache generations is created by a call to Commit.
func New(root common.Hash, db Database, cacheLimit uint16) (*SimpleTrie, error) { func New(root common.Hash, db Database, cacheLimit uint16) (*Trie, error) {
trie := &SimpleTrie{db: db, originalRoot: root, cachelimit: cacheLimit} trie := &Trie{db: db, originalRoot: root, cachelimit: cacheLimit}
if (root != common.Hash{}) && root != emptyRoot { if (root != common.Hash{}) && root != emptyRoot {
if db == nil { if db == nil {
panic("SimpleTrie.New: cannot use existing root without a database") panic("Trie.New: cannot use existing root without a database")
} }
rootnode, err := trie.resolveHash(root[:], nil, nil) rootnode, err := trie.resolveHash(root[:], nil, nil)
if err != nil { if err != nil {
@ -133,17 +132,13 @@ func New(root common.Hash, db Database, cacheLimit uint16) (*SimpleTrie, error)
} }
// Iterator returns an iterator over all mappings in the trie. // Iterator returns an iterator over all mappings in the trie.
func (t *SimpleTrie) Iterator() *Iterator { func (t *Trie) Iterator() *Iterator {
return NewIterator(t) return NewIterator(t)
} }
func (t *SimpleTrie) NodeIterator() *NodeIterator {
return NewNodeIterator(t)
}
// Get returns the value for key stored in the trie. // Get returns the value for key stored in the trie.
// The value bytes must not be modified by the caller. // The value bytes must not be modified by the caller.
func (t *SimpleTrie) Get(key []byte) []byte { func (t *Trie) Get(key []byte) []byte {
res, err := t.TryGet(key) res, err := t.TryGet(key)
if err != nil && glog.V(logger.Error) { if err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err) glog.Errorf("Unhandled trie error: %v", err)
@ -154,7 +149,7 @@ func (t *SimpleTrie) Get(key []byte) []byte {
// TryGet returns the value for key stored in the trie. // TryGet returns the value for key stored in the trie.
// The value bytes must not be modified by the caller. // The value bytes must not be modified by the caller.
// If a node was not found in the database, a MissingNodeError is returned. // If a node was not found in the database, a MissingNodeError is returned.
func (t *SimpleTrie) TryGet(key []byte) ([]byte, error) { func (t *Trie) TryGet(key []byte) ([]byte, error) {
key = compactHexDecode(key) key = compactHexDecode(key)
value, newroot, didResolve, err := t.tryGet(t.root, key, 0) value, newroot, didResolve, err := t.tryGet(t.root, key, 0)
if err == nil && didResolve { if err == nil && didResolve {
@ -163,7 +158,7 @@ func (t *SimpleTrie) TryGet(key []byte) ([]byte, error) {
return value, err return value, err
} }
func (t *SimpleTrie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) { func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) {
switch n := (origNode).(type) { switch n := (origNode).(type) {
case nil: case nil:
return nil, nil, false, nil return nil, nil, false, nil
@ -207,7 +202,7 @@ func (t *SimpleTrie) tryGet(origNode node, key []byte, pos int) (value []byte, n
// //
// The value bytes must not be modified by the caller while they are // The value bytes must not be modified by the caller while they are
// stored in the trie. // stored in the trie.
func (t *SimpleTrie) Update(key, value []byte) { func (t *Trie) Update(key, value []byte) {
if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) { if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err) glog.Errorf("Unhandled trie error: %v", err)
} }
@ -221,7 +216,7 @@ func (t *SimpleTrie) Update(key, value []byte) {
// stored in the trie. // stored in the trie.
// //
// If a node was not found in the database, a MissingNodeError is returned. // If a node was not found in the database, a MissingNodeError is returned.
func (t *SimpleTrie) TryUpdate(key, value []byte) error { func (t *Trie) TryUpdate(key, value []byte) error {
k := compactHexDecode(key) k := compactHexDecode(key)
if len(value) != 0 { if len(value) != 0 {
_, n, err := t.insert(t.root, nil, k, valueNode(value)) _, n, err := t.insert(t.root, nil, k, valueNode(value))
@ -239,7 +234,7 @@ func (t *SimpleTrie) TryUpdate(key, value []byte) error {
return nil return nil
} }
func (t *SimpleTrie) insert(n node, prefix, key []byte, value node) (bool, node, error) { func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
if len(key) == 0 { if len(key) == 0 {
if v, ok := n.(valueNode); ok { if v, ok := n.(valueNode); ok {
return !bytes.Equal(v, value.(valueNode)), value, nil return !bytes.Equal(v, value.(valueNode)), value, nil
@ -309,7 +304,7 @@ func (t *SimpleTrie) insert(n node, prefix, key []byte, value node) (bool, node,
} }
// Delete removes any existing value for key from the trie. // Delete removes any existing value for key from the trie.
func (t *SimpleTrie) Delete(key []byte) { func (t *Trie) Delete(key []byte) {
if err := t.TryDelete(key); err != nil && glog.V(logger.Error) { if err := t.TryDelete(key); err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err) glog.Errorf("Unhandled trie error: %v", err)
} }
@ -317,7 +312,7 @@ func (t *SimpleTrie) Delete(key []byte) {
// TryDelete removes any existing value for key from the trie. // TryDelete removes any existing value for key from the trie.
// If a node was not found in the database, a MissingNodeError is returned. // If a node was not found in the database, a MissingNodeError is returned.
func (t *SimpleTrie) TryDelete(key []byte) error { func (t *Trie) TryDelete(key []byte) error {
k := compactHexDecode(key) k := compactHexDecode(key)
_, n, err := t.delete(t.root, nil, k) _, n, err := t.delete(t.root, nil, k)
if err != nil { if err != nil {
@ -330,7 +325,7 @@ func (t *SimpleTrie) TryDelete(key []byte) error {
// delete returns the new root of the trie with key deleted. // delete returns the new root of the trie with key deleted.
// It reduces the trie to minimal form by simplifying // It reduces the trie to minimal form by simplifying
// nodes on the way up after deleting recursively. // nodes on the way up after deleting recursively.
func (t *SimpleTrie) delete(n node, prefix, key []byte) (bool, node, error) { func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
switch n := n.(type) { switch n := n.(type) {
case *shortNode: case *shortNode:
matchlen := prefixLen(key, n.Key) matchlen := prefixLen(key, n.Key)
@ -446,14 +441,14 @@ func concat(s1 []byte, s2 ...byte) []byte {
return r return r
} }
func (t *SimpleTrie) resolve(n node, prefix, suffix []byte) (node, error) { func (t *Trie) resolve(n node, prefix, suffix []byte) (node, error) {
if n, ok := n.(hashNode); ok { if n, ok := n.(hashNode); ok {
return t.resolveHash(n, prefix, suffix) return t.resolveHash(n, prefix, suffix)
} }
return n, nil return n, nil
} }
func (t *SimpleTrie) resolveHash(n hashNode, prefix, suffix []byte) (node, error) { func (t *Trie) resolveHash(n hashNode, prefix, suffix []byte) (node, error) {
cacheMissCounter.Inc(1) cacheMissCounter.Inc(1)
enc, err := t.db.Get(n) enc, err := t.db.Get(n)
@ -472,11 +467,11 @@ func (t *SimpleTrie) resolveHash(n hashNode, prefix, suffix []byte) (node, error
// Root returns the root hash of the trie. // Root returns the root hash of the trie.
// Deprecated: use Hash instead. // Deprecated: use Hash instead.
func (t *SimpleTrie) Root() []byte { return t.Hash().Bytes() } func (t *Trie) Root() []byte { return t.Hash().Bytes() }
// Hash returns the root hash of the trie. It does not write to the // Hash returns the root hash of the trie. It does not write to the
// database and can be used even if the trie doesn't have one. // database and can be used even if the trie doesn't have one.
func (t *SimpleTrie) Hash() common.Hash { func (t *Trie) Hash() common.Hash {
hash, cached, _ := t.hashRoot(nil) hash, cached, _ := t.hashRoot(nil)
t.root = cached t.root = cached
return common.BytesToHash(hash.(hashNode)) return common.BytesToHash(hash.(hashNode))
@ -487,7 +482,7 @@ func (t *SimpleTrie) Hash() common.Hash {
// //
// Committing flushes nodes from memory. // Committing flushes nodes from memory.
// Subsequent Get calls will load nodes from the database. // Subsequent Get calls will load nodes from the database.
func (t *SimpleTrie) Commit() (root common.Hash, err error) { func (t *Trie) Commit() (root common.Hash, err error) {
if t.db == nil { if t.db == nil {
panic("Commit called on trie with nil database") panic("Commit called on trie with nil database")
} }
@ -501,7 +496,7 @@ func (t *SimpleTrie) Commit() (root common.Hash, err error) {
// load nodes from the trie's database. Calling code must ensure that // load nodes from the trie's database. Calling code must ensure that
// the changes made to db are written back to the trie's attached // the changes made to db are written back to the trie's attached
// database before using the trie. // database before using the trie.
func (t *SimpleTrie) CommitTo(db DatabaseWriter) (root common.Hash, err error) { func (t *Trie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
hash, cached, err := t.hashRoot(db) hash, cached, err := t.hashRoot(db)
if err != nil { if err != nil {
return (common.Hash{}), err return (common.Hash{}), err
@ -511,7 +506,7 @@ func (t *SimpleTrie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
return common.BytesToHash(hash.(hashNode)), nil return common.BytesToHash(hash.(hashNode)), nil
} }
func (t *SimpleTrie) hashRoot(db DatabaseWriter) (node, node, error) { func (t *Trie) hashRoot(db DatabaseWriter) (node, node, error) {
if t.root == nil { if t.root == nil {
return hashNode(emptyRoot.Bytes()), nil, nil return hashNode(emptyRoot.Bytes()), nil, nil
} }

View file

@ -38,14 +38,14 @@ func init() {
} }
// Used for testing // Used for testing
func newEmpty() *SimpleTrie { func newEmpty() *Trie {
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
trie, _ := New(common.Hash{}, db, 0) trie, _ := New(common.Hash{}, db, 0)
return trie return trie
} }
func TestEmptyTrie(t *testing.T) { func TestEmptyTrie(t *testing.T) {
var trie SimpleTrie var trie Trie
res := trie.Hash() res := trie.Hash()
exp := emptyRoot exp := emptyRoot
if res != common.Hash(exp) { if res != common.Hash(exp) {
@ -54,7 +54,7 @@ func TestEmptyTrie(t *testing.T) {
} }
func TestNull(t *testing.T) { func TestNull(t *testing.T) {
var trie SimpleTrie var trie Trie
key := make([]byte, 32) key := make([]byte, 32)
value := common.FromHex("0x823140710bf13990e4500136726d8b55") value := common.FromHex("0x823140710bf13990e4500136726d8b55")
trie.Update(key, value) trie.Update(key, value)
@ -547,7 +547,7 @@ func benchGet(b *testing.B, commit bool) {
} }
} }
func benchUpdate(b *testing.B, e binary.ByteOrder) *SimpleTrie { func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
trie := newEmpty() trie := newEmpty()
k := make([]byte, 32) k := make([]byte, 32)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {