trie.go 15.4 KB
Newer Older
F
Felix Lange 已提交
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
F
Felix Lange 已提交
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
F
Felix Lange 已提交
5 6 7 8
// 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.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
F
Felix Lange 已提交
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
12 13 14
// GNU Lesser General Public License for more details.
//
// 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/>.
F
Felix Lange 已提交
16

17
// Package trie implements Merkle Patricia Tries.
O
obscuren 已提交
18
package trie
O
obscuren 已提交
19 20

import (
O
obscuren 已提交
21
	"bytes"
O
obscuren 已提交
22
	"fmt"
O
obscuren 已提交
23

O
obscuren 已提交
24
	"github.com/ethereum/go-ethereum/common"
25
	"github.com/ethereum/go-ethereum/crypto/sha3"
26
	"github.com/ethereum/go-ethereum/log"
27
	"github.com/rcrowley/go-metrics"
O
obscuren 已提交
28 29
)

F
Felix Lange 已提交
30 31 32
var (
	// This is the known root hash of an empty trie.
	emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
33
	// This is the known hash of an empty state trie entry.
34
	emptyState common.Hash
F
Felix Lange 已提交
35
)
O
obscuren 已提交
36

37 38 39 40
var (
	cacheMissCounter   = metrics.NewRegisteredCounter("trie/cachemiss", nil)
	cacheUnloadCounter = metrics.NewRegisteredCounter("trie/cacheunload", nil)
)
41 42

// CacheMisses retrieves a global counter measuring the number of cache misses
43
// the trie had since process startup. This isn't useful for anything apart from
44
// trie debugging purposes.
45 46
func CacheMisses() int64 {
	return cacheMissCounter.Count()
47 48
}

49 50 51 52 53 54 55
// CacheUnloads retrieves a global counter measuring the number of cache unloads
// the trie did since process startup. This isn't useful for anything apart from
// trie debugging purposes.
func CacheUnloads() int64 {
	return cacheUnloadCounter.Count()
}

56 57 58 59
func init() {
	sha3.NewKeccak256().Sum(emptyState[:0])
}

F
Felix Lange 已提交
60 61
// Database must be implemented by backing stores for the trie.
type Database interface {
F
Felix Lange 已提交
62
	DatabaseReader
F
Felix Lange 已提交
63
	DatabaseWriter
F
Felix Lange 已提交
64 65 66 67
}

// DatabaseReader wraps the Get method of a backing store for the trie.
type DatabaseReader interface {
F
Felix Lange 已提交
68
	Get(key []byte) (value []byte, err error)
O
obscuren 已提交
69 70
}

F
Felix Lange 已提交
71 72 73 74 75 76
// DatabaseWriter wraps the Put method of a backing store for the trie.
type DatabaseWriter interface {
	// Put stores the mapping key->value in the database.
	// Implementations must not hold onto the value bytes, the trie
	// will reuse the slice across calls to Put.
	Put(key, value []byte) error
O
obscuren 已提交
77 78
}

F
Felix Lange 已提交
79 80 81 82 83 84
// Trie is a Merkle Patricia Trie.
// The zero value is an empty trie with no database.
// Use New to create a trie that sits on top of a database.
//
// Trie is not safe for concurrent use.
type Trie struct {
Z
zsfelfoldi 已提交
85 86 87
	root         node
	db           Database
	originalRoot common.Hash
88 89

	// Cache generation values.
90
	// cachegen increases by one with each commit operation.
91 92 93 94 95 96
	// new nodes are tagged with the current generation and unloaded
	// when their generation is older than than cachegen-cachelimit.
	cachegen, cachelimit uint16
}

// SetCacheLimit sets the number of 'cache generations' to keep.
97
// A cache generation is created by a call to Commit.
98 99 100 101 102 103 104
func (t *Trie) SetCacheLimit(l uint16) {
	t.cachelimit = l
}

// newFlag returns the cache flag value for a newly created node.
func (t *Trie) newFlag() nodeFlag {
	return nodeFlag{dirty: true, gen: t.cachegen}
O
obscuren 已提交
105 106
}

F
Felix Lange 已提交
107 108 109 110
// New 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 and does not require a database. Otherwise,
Z
zsfelfoldi 已提交
111 112
// New will panic if db is nil and returns a MissingNodeError if root does
// not exist in the database. Accessing the trie loads nodes from db on demand.
F
Felix Lange 已提交
113
func New(root common.Hash, db Database) (*Trie, error) {
Z
zsfelfoldi 已提交
114
	trie := &Trie{db: db, originalRoot: root}
F
Felix Lange 已提交
115 116 117
	if (root != common.Hash{}) && root != emptyRoot {
		if db == nil {
			panic("trie.New: cannot use existing root without a database")
O
obscuren 已提交
118
		}
119
		rootnode, err := trie.resolveHash(root[:], nil)
120 121
		if err != nil {
			return nil, err
F
Felix Lange 已提交
122
		}
123
		trie.root = rootnode
O
obscuren 已提交
124
	}
F
Felix Lange 已提交
125
	return trie, nil
O
obscuren 已提交
126 127
}

128 129 130 131
// NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at
// the key after the given start key.
func (t *Trie) NodeIterator(start []byte) NodeIterator {
	return newNodeIterator(t, start)
O
obscuren 已提交
132 133
}

F
Felix Lange 已提交
134 135 136
// Get returns the value for key stored in the trie.
// The value bytes must not be modified by the caller.
func (t *Trie) Get(key []byte) []byte {
Z
zsfelfoldi 已提交
137
	res, err := t.TryGet(key)
138 139
	if err != nil {
		log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
Z
zsfelfoldi 已提交
140 141 142 143 144 145 146 147
	}
	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 *Trie) TryGet(key []byte) ([]byte, error) {
148
	key = keybytesToHex(key)
149 150 151 152 153 154 155 156 157 158 159 160 161
	value, newroot, didResolve, err := t.tryGet(t.root, key, 0)
	if err == nil && didResolve {
		t.root = newroot
	}
	return value, err
}

func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) {
	switch n := (origNode).(type) {
	case nil:
		return nil, nil, false, nil
	case valueNode:
		return n, n, false, nil
162
	case *shortNode:
163 164 165 166 167 168
		if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) {
			// key not found in trie
			return nil, n, false, nil
		}
		value, newnode, didResolve, err = t.tryGet(n.Val, key, pos+len(n.Key))
		if err == nil && didResolve {
169
			n = n.copy()
170
			n.Val = newnode
171
			n.flags.gen = t.cachegen
172
		}
173 174 175
		return value, n, didResolve, err
	case *fullNode:
		value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1)
176
		if err == nil && didResolve {
177
			n = n.copy()
178
			n.flags.gen = t.cachegen
179 180
			n.Children[key[pos]] = newnode
		}
181
		return value, n, didResolve, err
182
	case hashNode:
183
		child, err := t.resolveHash(n, key[:pos])
184 185
		if err != nil {
			return nil, n, true, err
F
Felix Lange 已提交
186
		}
187 188 189 190
		value, newnode, _, err := t.tryGet(child, key, pos)
		return value, newnode, true, err
	default:
		panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
O
obscuren 已提交
191
	}
O
obscuren 已提交
192 193
}

F
Felix Lange 已提交
194 195 196 197 198 199 200
// 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 *Trie) Update(key, value []byte) {
201 202
	if err := t.TryUpdate(key, value); err != nil {
		log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
Z
zsfelfoldi 已提交
203 204 205 206 207 208 209 210 211 212 213 214
	}
}

// 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 *Trie) TryUpdate(key, value []byte) error {
215
	k := keybytesToHex(key)
O
obscuren 已提交
216
	if len(value) != 0 {
217
		_, n, err := t.insert(t.root, nil, k, valueNode(value))
Z
zsfelfoldi 已提交
218 219 220 221
		if err != nil {
			return err
		}
		t.root = n
222
	} else {
223
		_, n, err := t.delete(t.root, nil, k)
Z
zsfelfoldi 已提交
224 225 226 227
		if err != nil {
			return err
		}
		t.root = n
228
	}
Z
zsfelfoldi 已提交
229
	return nil
O
obscuren 已提交
230 231
}

232
func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
O
obscuren 已提交
233
	if len(key) == 0 {
234 235 236 237
		if v, ok := n.(valueNode); ok {
			return !bytes.Equal(v, value.(valueNode)), value, nil
		}
		return true, value, nil
O
obscuren 已提交
238
	}
F
Felix Lange 已提交
239
	switch n := n.(type) {
240
	case *shortNode:
F
Felix Lange 已提交
241 242 243 244
		matchlen := prefixLen(key, n.Key)
		// If the whole key matches, keep this short node as is
		// and only update the value.
		if matchlen == len(n.Key) {
245
			dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value)
246 247
			if !dirty || err != nil {
				return false, n, err
248
			}
249
			return true, &shortNode{n.Key, nn, t.newFlag()}, nil
O
obscuren 已提交
250
		}
F
Felix Lange 已提交
251
		// Otherwise branch out at the index where they differ.
252
		branch := &fullNode{flags: t.newFlag()}
Z
zsfelfoldi 已提交
253
		var err error
254
		_, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val)
Z
zsfelfoldi 已提交
255
		if err != nil {
256
			return false, nil, err
Z
zsfelfoldi 已提交
257
		}
258
		_, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value)
Z
zsfelfoldi 已提交
259
		if err != nil {
260
			return false, nil, err
Z
zsfelfoldi 已提交
261
		}
F
Felix Lange 已提交
262 263
		// Replace this shortNode with the branch if it occurs at index 0.
		if matchlen == 0 {
264
			return true, branch, nil
O
obscuren 已提交
265
		}
F
Felix Lange 已提交
266
		// Otherwise, replace it with a short node leading up to the branch.
267
		return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil
O
obscuren 已提交
268

269
	case *fullNode:
270
		dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value)
271 272
		if !dirty || err != nil {
			return false, n, err
Z
zsfelfoldi 已提交
273
		}
274
		n = n.copy()
275 276
		n.flags = t.newFlag()
		n.Children[key[0]] = nn
277
		return true, n, nil
O
obscuren 已提交
278

F
Felix Lange 已提交
279
	case nil:
280
		return true, &shortNode{key, value, t.newFlag()}, nil
O
obscuren 已提交
281

F
Felix Lange 已提交
282 283 284 285
	case hashNode:
		// We've hit a part of the trie that isn't loaded yet. Load
		// the node and insert into it. This leaves all child nodes on
		// the path to the value in the trie.
286
		rn, err := t.resolveHash(n, prefix)
Z
zsfelfoldi 已提交
287
		if err != nil {
288 289 290
			return false, nil, err
		}
		dirty, nn, err := t.insert(rn, prefix, key, value)
291 292
		if !dirty || err != nil {
			return false, rn, err
293 294
		}
		return true, nn, nil
O
obscuren 已提交
295

O
obscuren 已提交
296
	default:
F
Felix Lange 已提交
297
		panic(fmt.Sprintf("%T: invalid node: %v", n, n))
O
obscuren 已提交
298 299 300
	}
}

F
Felix Lange 已提交
301 302
// Delete removes any existing value for key from the trie.
func (t *Trie) Delete(key []byte) {
303 304
	if err := t.TryDelete(key); err != nil {
		log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
Z
zsfelfoldi 已提交
305 306 307 308 309 310
	}
}

// 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 *Trie) TryDelete(key []byte) error {
311
	k := keybytesToHex(key)
312
	_, n, err := t.delete(t.root, nil, k)
Z
zsfelfoldi 已提交
313 314 315 316 317
	if err != nil {
		return err
	}
	t.root = n
	return nil
O
obscuren 已提交
318 319
}

F
Felix Lange 已提交
320 321 322
// delete returns the new root of the trie with key deleted.
// It reduces the trie to minimal form by simplifying
// nodes on the way up after deleting recursively.
323
func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
F
Felix Lange 已提交
324
	switch n := n.(type) {
325
	case *shortNode:
F
Felix Lange 已提交
326 327
		matchlen := prefixLen(key, n.Key)
		if matchlen < len(n.Key) {
328
			return false, n, nil // don't replace n on mismatch
F
Felix Lange 已提交
329 330
		}
		if matchlen == len(key) {
331
			return true, nil, nil // remove n entirely for whole matches
F
Felix Lange 已提交
332 333 334 335 336
		}
		// The key is longer than n.Key. Remove the remaining suffix
		// from the subtrie. Child can never be nil here since the
		// subtrie must contain at least two other values with keys
		// longer than n.Key.
337
		dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):])
338 339
		if !dirty || err != nil {
			return false, n, err
Z
zsfelfoldi 已提交
340
		}
F
Felix Lange 已提交
341
		switch child := child.(type) {
342
		case *shortNode:
F
Felix Lange 已提交
343 344 345 346 347 348
			// Deleting from the subtrie reduced it to another
			// short node. Merge the nodes to avoid creating a
			// shortNode{..., shortNode{...}}. Use concat (which
			// always creates a new slice) instead of append to
			// avoid modifying n.Key since it might be shared with
			// other nodes.
349
			return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil
F
Felix Lange 已提交
350
		default:
351
			return true, &shortNode{n.Key, child, t.newFlag()}, nil
O
obscuren 已提交
352 353
		}

354
	case *fullNode:
355
		dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:])
356 357
		if !dirty || err != nil {
			return false, n, err
358
		}
359
		n = n.copy()
360 361
		n.flags = t.newFlag()
		n.Children[key[0]] = nn
362

F
Felix Lange 已提交
363 364 365 366 367 368 369 370 371
		// Check how many non-nil entries are left after deleting and
		// reduce the full node to a short node if only one entry is
		// left. Since n must've contained at least two children
		// before deletion (otherwise it would not be a full node) n
		// can never be reduced to nil.
		//
		// When the loop is done, pos contains the index of the single
		// value that is left in n or -2 if n contains at least two
		// values.
O
obscuren 已提交
372
		pos := -1
373
		for i, cld := range n.Children {
F
Felix Lange 已提交
374
			if cld != nil {
O
obscuren 已提交
375 376
				if pos == -1 {
					pos = i
O
obscuren 已提交
377
				} else {
O
obscuren 已提交
378
					pos = -2
F
Felix Lange 已提交
379
					break
O
obscuren 已提交
380 381 382
				}
			}
		}
F
Felix Lange 已提交
383 384 385 386 387 388 389 390
		if pos >= 0 {
			if pos != 16 {
				// If the remaining entry is a short node, it replaces
				// n and its key gets the missing nibble tacked to the
				// front. This avoids creating an invalid
				// shortNode{..., shortNode{...}}.  Since the entry
				// might not be loaded yet, resolve it just for this
				// check.
391
				cnode, err := t.resolve(n.Children[pos], prefix)
Z
zsfelfoldi 已提交
392
				if err != nil {
393
					return false, nil, err
Z
zsfelfoldi 已提交
394
				}
395
				if cnode, ok := cnode.(*shortNode); ok {
F
Felix Lange 已提交
396
					k := append([]byte{byte(pos)}, cnode.Key...)
397
					return true, &shortNode{k, cnode.Val, t.newFlag()}, nil
F
Felix Lange 已提交
398
				}
O
obscuren 已提交
399
			}
F
Felix Lange 已提交
400 401
			// Otherwise, n is replaced by a one-nibble short node
			// containing the child.
402
			return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil
O
obscuren 已提交
403
		}
F
Felix Lange 已提交
404
		// n still contains at least two values and cannot be reduced.
405
		return true, n, nil
O
obscuren 已提交
406

407 408 409
	case valueNode:
		return true, nil, nil

O
obscuren 已提交
410
	case nil:
411
		return false, nil, nil
F
Felix Lange 已提交
412 413 414 415 416

	case hashNode:
		// We've hit a part of the trie that isn't loaded yet. Load
		// the node and delete from it. This leaves all child nodes on
		// the path to the value in the trie.
417
		rn, err := t.resolveHash(n, prefix)
Z
zsfelfoldi 已提交
418
		if err != nil {
419
			return false, nil, err
Z
zsfelfoldi 已提交
420
		}
421
		dirty, nn, err := t.delete(rn, prefix, key)
422 423
		if !dirty || err != nil {
			return false, rn, err
424 425
		}
		return true, nn, nil
F
Felix Lange 已提交
426

O
obscuren 已提交
427
	default:
F
Felix Lange 已提交
428
		panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key))
O
obscuren 已提交
429
	}
O
obscuren 已提交
430 431
}

F
Felix Lange 已提交
432 433 434 435 436 437 438
func concat(s1 []byte, s2 ...byte) []byte {
	r := make([]byte, len(s1)+len(s2))
	copy(r, s1)
	copy(r[len(s1):], s2)
	return r
}

439
func (t *Trie) resolve(n node, prefix []byte) (node, error) {
F
Felix Lange 已提交
440
	if n, ok := n.(hashNode); ok {
441
		return t.resolveHash(n, prefix)
F
Felix Lange 已提交
442
	}
Z
zsfelfoldi 已提交
443
	return n, nil
F
Felix Lange 已提交
444 445
}

446
func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) {
447
	cacheMissCounter.Inc(1)
448

F
Felix Lange 已提交
449 450
	enc, err := t.db.Get(n)
	if err != nil || enc == nil {
451
		return nil, &MissingNodeError{NodeHash: common.BytesToHash(n), Path: prefix}
F
Felix Lange 已提交
452
	}
453
	dec := mustDecodeNode(n, enc, t.cachegen)
Z
zsfelfoldi 已提交
454
	return dec, nil
F
Felix Lange 已提交
455 456 457 458 459
}

// Root returns the root hash of the trie.
// Deprecated: use Hash instead.
func (t *Trie) Root() []byte { return t.Hash().Bytes() }
O
obscuren 已提交
460

F
Felix Lange 已提交
461 462 463
// 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.
func (t *Trie) Hash() common.Hash {
464 465 466
	hash, cached, _ := t.hashRoot(nil)
	t.root = cached
	return common.BytesToHash(hash.(hashNode))
O
obscuren 已提交
467 468
}

F
Felix Lange 已提交
469 470 471 472 473 474 475 476
// Commit writes all nodes to the trie's database.
// Nodes are stored with their sha3 hash as the key.
//
// Committing flushes nodes from memory.
// Subsequent Get calls will load nodes from the database.
func (t *Trie) Commit() (root common.Hash, err error) {
	if t.db == nil {
		panic("Commit called on trie with nil database")
O
obscuren 已提交
477
	}
F
Felix Lange 已提交
478
	return t.CommitTo(t.db)
O
obscuren 已提交
479 480
}

F
Felix Lange 已提交
481 482 483 484 485 486 487 488
// CommitTo writes all nodes to the given database.
// Nodes are stored with their sha3 hash as the key.
//
// Committing flushes nodes from memory. Subsequent Get calls will
// 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
// database before using the trie.
func (t *Trie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
489
	hash, cached, err := t.hashRoot(db)
F
Felix Lange 已提交
490 491 492
	if err != nil {
		return (common.Hash{}), err
	}
493
	t.root = cached
494
	t.cachegen++
495
	return common.BytesToHash(hash.(hashNode)), nil
F
Felix Lange 已提交
496
}
O
obscuren 已提交
497

498
func (t *Trie) hashRoot(db DatabaseWriter) (node, node, error) {
F
Felix Lange 已提交
499
	if t.root == nil {
500
		return hashNode(emptyRoot.Bytes()), nil, nil
F
Felix Lange 已提交
501
	}
502
	h := newHasher(t.cachegen, t.cachelimit)
503 504
	defer returnHasherToPool(h)
	return h.hash(t.root, db, true)
O
obscuren 已提交
505
}