mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
trie: Refactor trie implementation to use an interface
This commit is contained in:
parent
890ffa05f8
commit
342312acab
17 changed files with 199 additions and 123 deletions
|
|
@ -33,6 +33,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/logger/glog"
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
"github.com/ethereum/go-ethereum/trie"
|
||||||
"github.com/syndtr/goleveldb/leveldb/util"
|
"github.com/syndtr/goleveldb/leveldb/util"
|
||||||
"gopkg.in/urfave/cli.v1"
|
"gopkg.in/urfave/cli.v1"
|
||||||
|
|
@ -271,6 +272,49 @@ func dump(ctx *cli.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var accountCacheKeyPrefix = []byte("accounthashcache:")
|
||||||
|
|
||||||
|
type cachedAccount struct {
|
||||||
|
state.Account
|
||||||
|
BlockNum uint64
|
||||||
|
BlockHash common.Hash
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildCache(ctx *cli.Context) error {
|
||||||
|
stack := makeFullNode(ctx)
|
||||||
|
chain, chainDb := utils.MakeChain(ctx, stack)
|
||||||
|
defer chainDb.Close()
|
||||||
|
|
||||||
|
num, _ := strconv.Atoi(ctx.Args()[0])
|
||||||
|
blockNum := uint64(num)
|
||||||
|
block := chain.GetBlockByNumber(blockNum)
|
||||||
|
blockHash := block.Hash()
|
||||||
|
t, err := trie.New(block.Root(), chainDb, 0)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Could not open trie: %v", err)
|
||||||
|
}
|
||||||
|
st := trie.NewSecure(t, chainDb)
|
||||||
|
iter := st.Iterator()
|
||||||
|
i := 0
|
||||||
|
for iter.Next() {
|
||||||
|
var data state.Account
|
||||||
|
if err := rlp.DecodeBytes(iter.Value, &data); err != nil {
|
||||||
|
utils.Fatalf("can't decode object at %x: %v", iter.Key[:], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
enc, _ := rlp.EncodeToBytes(cachedAccount{data, blockNum, blockHash})
|
||||||
|
if err := chainDb.Put(append(accountCacheKeyPrefix, iter.Key[:]...), enc); err != nil {
|
||||||
|
utils.Fatalf("Could not write to DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
if i % 10000 == 0 {
|
||||||
|
fmt.Printf("Processed %d accounts, at %v\n", i, common.ToHex(iter.Key))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// hashish returns true for strings that look like hashes.
|
// hashish returns true for strings that look like hashes.
|
||||||
func hashish(x string) bool {
|
func hashish(x string) bool {
|
||||||
_, err := strconv.Atoi(x)
|
_, err := strconv.Atoi(x)
|
||||||
|
|
|
||||||
|
|
@ -269,7 +269,7 @@ func (self *BlockChain) FastSyncCommitHead(hash common.Hash) error {
|
||||||
if block == nil {
|
if block == nil {
|
||||||
return fmt.Errorf("non existent block [%x…]", hash[:4])
|
return fmt.Errorf("non existent block [%x…]", hash[:4])
|
||||||
}
|
}
|
||||||
if _, err := trie.NewSecure(block.Root(), self.chainDb, 0); err != nil {
|
if _, err := trie.New(block.Root(), self.chainDb, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// If all checks out, manually set the head block
|
// If all checks out, manually set the head block
|
||||||
|
|
|
||||||
|
|
@ -115,11 +115,11 @@ func (it *NodeIterator) step() error {
|
||||||
if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob), &account); err != nil {
|
if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob), &account); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dataTrie, err := trie.New(account.Root, it.state.db)
|
dataTrie, err := trie.New(account.Root, it.state.db, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
it.dataIt = trie.NewNodeIterator(dataTrie)
|
it.dataIt = dataTrie.NodeIterator()
|
||||||
if !it.dataIt.Next() {
|
if !it.dataIt.Next() {
|
||||||
it.dataIt = nil
|
it.dataIt = nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -137,11 +137,12 @@ func (self *StateObject) markSuicided() {
|
||||||
func (c *StateObject) getTrie(db trie.Database) *trie.SecureTrie {
|
func (c *StateObject) getTrie(db trie.Database) *trie.SecureTrie {
|
||||||
if c.trie == nil {
|
if c.trie == nil {
|
||||||
var err error
|
var err error
|
||||||
c.trie, err = trie.NewSecure(c.data.Root, db, 0)
|
t, err := trie.New(c.data.Root, db, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.trie, _ = trie.NewSecure(common.Hash{}, db, 0)
|
t, _ = trie.New(common.Hash{}, nil, 0)
|
||||||
c.setError(fmt.Errorf("can't create storage trie: %v", err))
|
c.setError(fmt.Errorf("can't create storage trie: %v", err))
|
||||||
}
|
}
|
||||||
|
c.trie = trie.NewSecure(t, db)
|
||||||
}
|
}
|
||||||
return c.trie
|
return c.trie
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,14 +89,15 @@ type StateDB struct {
|
||||||
|
|
||||||
// Create a new state from a given trie
|
// Create a new state from a given trie
|
||||||
func New(root common.Hash, db ethdb.Database) (*StateDB, error) {
|
func New(root common.Hash, db ethdb.Database) (*StateDB, error) {
|
||||||
tr, err := trie.NewSecure(root, db, MaxTrieCacheGen)
|
tr, err := trie.New(root, db, MaxTrieCacheGen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
st := trie.NewSecure(tr, db)
|
||||||
csc, _ := lru.New(codeSizeCacheSize)
|
csc, _ := lru.New(codeSizeCacheSize)
|
||||||
return &StateDB{
|
return &StateDB{
|
||||||
db: db,
|
db: db,
|
||||||
trie: tr,
|
trie: st,
|
||||||
codeSizeCache: csc,
|
codeSizeCache: csc,
|
||||||
stateObjects: make(map[common.Address]*StateObject),
|
stateObjects: make(map[common.Address]*StateObject),
|
||||||
stateObjectsDirty: make(map[common.Address]struct{}),
|
stateObjectsDirty: make(map[common.Address]struct{}),
|
||||||
|
|
@ -158,7 +159,11 @@ func (self *StateDB) openTrie(root common.Hash) (*trie.SecureTrie, error) {
|
||||||
return &tr, nil
|
return &tr, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return trie.NewSecure(root, self.db, MaxTrieCacheGen)
|
t, err := trie.New(root, self.db, MaxTrieCacheGen)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return trie.NewSecure(t, self.db), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) pushTrie(t *trie.SecureTrie) {
|
func (self *StateDB) pushTrie(t *trie.SecureTrie) {
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ type DerivableList interface {
|
||||||
|
|
||||||
func DeriveSha(list DerivableList) common.Hash {
|
func DeriveSha(list DerivableList) common.Hash {
|
||||||
keybuf := new(bytes.Buffer)
|
keybuf := new(bytes.Buffer)
|
||||||
trie := new(trie.Trie)
|
trie, _ := trie.New(common.Hash{}, nil, 0)
|
||||||
for i := 0; i < list.Len(); i++ {
|
for i := 0; i < list.Len(); i++ {
|
||||||
keybuf.Reset()
|
keybuf.Reset()
|
||||||
rlp.Encode(keybuf, uint(i))
|
rlp.Encode(keybuf, uint(i))
|
||||||
|
|
|
||||||
|
|
@ -286,7 +286,7 @@ func (dl *downloadTester) headFastBlock() *types.Block {
|
||||||
func (dl *downloadTester) commitHeadBlock(hash common.Hash) error {
|
func (dl *downloadTester) commitHeadBlock(hash common.Hash) error {
|
||||||
// For now only check that the state trie is correct
|
// For now only check that the state trie is correct
|
||||||
if block := dl.getBlock(hash); block != nil {
|
if block := dl.getBlock(hash); block != nil {
|
||||||
_, err := trie.NewSecure(block.Root(), dl.stateDb, 0)
|
_, err := trie.New(block.Root(), dl.stateDb, 0)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return fmt.Errorf("non existent block: %x", hash[:4])
|
return fmt.Errorf("non existent block: %x", hash[:4])
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ func (odr *testOdr) Database() ethdb.Database {
|
||||||
func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
|
func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
|
||||||
switch req := req.(type) {
|
switch req := req.(type) {
|
||||||
case *TrieRequest:
|
case *TrieRequest:
|
||||||
t, _ := trie.New(req.root, odr.sdb)
|
t, _ := trie.New(req.root, odr.sdb, 0)
|
||||||
req.proof = t.Prove(req.key)
|
req.proof = t.Prove(req.key)
|
||||||
case *NodeDataRequest:
|
case *NodeDataRequest:
|
||||||
req.data, _ = odr.sdb.Get(req.hash[:])
|
req.data, _ = odr.sdb.Get(req.hash[:])
|
||||||
|
|
|
||||||
|
|
@ -74,15 +74,26 @@ func (t *LightTrie) do(ctx context.Context, fallbackKey []byte, fn func() error)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *LightTrie) getTrie() (ret *trie.SecureTrie, err error) {
|
||||||
|
if t.trie == nil {
|
||||||
|
var tr trie.Trie
|
||||||
|
tr, err = trie.New(t.originalRoot, t.db, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.trie = trie.NewSecure(tr, t.db)
|
||||||
|
}
|
||||||
|
return t.trie, nil
|
||||||
|
}
|
||||||
|
|
||||||
// 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 *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) {
|
func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) {
|
||||||
err = t.do(ctx, key, func() (err error) {
|
err = t.do(ctx, key, func() (err error) {
|
||||||
if t.trie == nil {
|
var tr *trie.SecureTrie
|
||||||
t.trie, err = trie.NewSecure(t.originalRoot, t.db, 0)
|
tr, err = t.getTrie()
|
||||||
}
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
res, err = t.trie.TryGet(key)
|
res, err = tr.TryGet(key)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
|
|
@ -97,11 +108,10 @@ func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error)
|
||||||
// stored in the trie.
|
// stored in the trie.
|
||||||
func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) {
|
func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) {
|
||||||
err = t.do(ctx, key, func() (err error) {
|
err = t.do(ctx, key, func() (err error) {
|
||||||
if t.trie == nil {
|
var tr *trie.SecureTrie
|
||||||
t.trie, err = trie.NewSecure(t.originalRoot, t.db, 0)
|
tr, err = t.getTrie()
|
||||||
}
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = t.trie.TryUpdate(key, value)
|
err = tr.TryUpdate(key, value)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
|
|
@ -111,11 +121,10 @@ func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) {
|
||||||
// Delete removes any existing value for key from the trie.
|
// Delete removes any existing value for key from the trie.
|
||||||
func (t *LightTrie) Delete(ctx context.Context, key []byte) (err error) {
|
func (t *LightTrie) Delete(ctx context.Context, key []byte) (err error) {
|
||||||
err = t.do(ctx, key, func() (err error) {
|
err = t.do(ctx, key, func() (err error) {
|
||||||
if t.trie == nil {
|
var tr *trie.SecureTrie
|
||||||
t.trie, err = trie.NewSecure(t.originalRoot, t.db, 0)
|
tr, err = t.getTrie()
|
||||||
}
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = t.trie.TryDelete(key)
|
err = tr.TryDelete(key)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -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 *Trie
|
trie *SimpleTrie
|
||||||
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 *Trie) *Iterator {
|
func NewIterator(trie *SimpleTrie) *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 *Trie // Trie being iterated
|
trie *SimpleTrie // 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 *Trie) *NodeIterator {
|
func NewNodeIterator(trie *SimpleTrie) *NodeIterator {
|
||||||
if trie.Hash() == emptyState {
|
if trie.Hash() == emptyState {
|
||||||
return new(NodeIterator)
|
return new(NodeIterator)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 *Trie) Prove(key []byte) []rlp.RawValue {
|
func (t *SimpleTrie) 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{}
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ func TestProof(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOneElementProof(t *testing.T) {
|
func TestOneElementProof(t *testing.T) {
|
||||||
trie := new(Trie)
|
trie, _ := New(common.Hash{}, nil, 0)
|
||||||
updateString(trie, "k", "v")
|
updateString(trie, "k", "v")
|
||||||
proof := trie.Prove([]byte("k"))
|
proof := trie.Prove([]byte("k"))
|
||||||
if proof == nil {
|
if proof == nil {
|
||||||
|
|
@ -129,8 +129,8 @@ func BenchmarkVerifyProof(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomTrie(n int) (*Trie, map[string]*kv) {
|
func randomTrie(n int) (*SimpleTrie, map[string]*kv) {
|
||||||
trie := new(Trie)
|
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++ {
|
||||||
value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
|
value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
|
||||||
|
|
|
||||||
|
|
@ -38,32 +38,22 @@ const secureKeyLength = 11 + 32 // Length of the above prefix + 32byte hash
|
||||||
// SecureTrie is not safe for concurrent use.
|
// SecureTrie is not safe for concurrent use.
|
||||||
type SecureTrie struct {
|
type SecureTrie struct {
|
||||||
trie Trie
|
trie Trie
|
||||||
|
db Database
|
||||||
hashKeyBuf [secureKeyLength]byte
|
hashKeyBuf [secureKeyLength]byte
|
||||||
secKeyBuf [200]byte
|
secKeyBuf [200]byte
|
||||||
secKeyCache map[string][]byte
|
secKeyCache map[string][]byte
|
||||||
secKeyCacheOwner *SecureTrie // Pointer to self, replace the key cache on mismatch
|
secKeyCacheOwner *SecureTrie // Pointer to self, replace the key cache on mismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSecure creates a trie with an existing root node from db.
|
// NewSecure creates a secure trie from an existing trie.
|
||||||
//
|
func NewSecure(t Trie, db Database) *SecureTrie {
|
||||||
// If root is the zero hash or the sha3 hash of an empty string, the
|
if t == nil {
|
||||||
// trie is initially empty. Otherwise, New will panic if db is nil
|
panic("NewSecure called with nil trie")
|
||||||
// and returns MissingNodeError if the root node cannot be found.
|
}
|
||||||
//
|
|
||||||
// Accessing the trie loads nodes from db on demand.
|
|
||||||
// Loaded nodes are kept around until their 'cache generation' expires.
|
|
||||||
// A new cache generation is created by each call to Commit.
|
|
||||||
// cachelimit sets the number of past cache generations to keep.
|
|
||||||
func NewSecure(root common.Hash, db Database, cachelimit uint16) (*SecureTrie, error) {
|
|
||||||
if db == nil {
|
if db == nil {
|
||||||
panic("NewSecure called with nil database")
|
panic("NewSecure called with nil database")
|
||||||
}
|
}
|
||||||
trie, err := New(root, db)
|
return &SecureTrie{trie: t, db: db}
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
trie.SetCacheLimit(cachelimit)
|
|
||||||
return &SecureTrie{trie: *trie}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the value for key stored in the trie.
|
// Get returns the value for key stored in the trie.
|
||||||
|
|
@ -134,7 +124,7 @@ func (t *SecureTrie) GetKey(shaKey []byte) []byte {
|
||||||
if key, ok := t.getSecKeyCache()[string(shaKey)]; ok {
|
if key, ok := t.getSecKeyCache()[string(shaKey)]; ok {
|
||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
key, _ := t.trie.db.Get(t.secKey(shaKey))
|
key, _ := t.db.Get(t.secKey(shaKey))
|
||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -144,7 +134,10 @@ func (t *SecureTrie) GetKey(shaKey []byte) []byte {
|
||||||
// Committing flushes nodes from memory. Subsequent Get calls will load nodes
|
// Committing flushes nodes from memory. Subsequent Get calls will load nodes
|
||||||
// from the database.
|
// from the database.
|
||||||
func (t *SecureTrie) Commit() (root common.Hash, err error) {
|
func (t *SecureTrie) Commit() (root common.Hash, err error) {
|
||||||
return t.CommitTo(t.trie.db)
|
if err := t.CommitPreimages(); err != nil {
|
||||||
|
return common.Hash{}, err
|
||||||
|
}
|
||||||
|
return t.trie.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *SecureTrie) Hash() common.Hash {
|
func (t *SecureTrie) Hash() common.Hash {
|
||||||
|
|
@ -160,7 +153,7 @@ func (t *SecureTrie) Iterator() *Iterator {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *SecureTrie) NodeIterator() *NodeIterator {
|
func (t *SecureTrie) NodeIterator() *NodeIterator {
|
||||||
return NewNodeIterator(&t.trie)
|
return t.trie.NodeIterator()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitTo writes all nodes and the secure hash pre-images to the given database.
|
// CommitTo writes all nodes and the secure hash pre-images to the given database.
|
||||||
|
|
@ -170,15 +163,22 @@ func (t *SecureTrie) NodeIterator() *NodeIterator {
|
||||||
// the trie's database. Calling code must ensure that the changes made to db are
|
// 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.
|
// written back to the trie's attached database before using the trie.
|
||||||
func (t *SecureTrie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
|
func (t *SecureTrie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
|
||||||
|
if err := t.CommitPreimages(); err != nil {
|
||||||
|
return common.Hash{}, err
|
||||||
|
}
|
||||||
|
return t.trie.CommitTo(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *SecureTrie) CommitPreimages() error {
|
||||||
if len(t.getSecKeyCache()) > 0 {
|
if len(t.getSecKeyCache()) > 0 {
|
||||||
for hk, key := range t.secKeyCache {
|
for hk, key := range t.secKeyCache {
|
||||||
if err := db.Put(t.secKey([]byte(hk)), key); err != nil {
|
if err := t.db.Put(t.secKey([]byte(hk)), key); err != nil {
|
||||||
return common.Hash{}, err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.secKeyCache = make(map[string][]byte)
|
t.secKeyCache = make(map[string][]byte)
|
||||||
}
|
}
|
||||||
return t.trie.CommitTo(db)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// secKey returns the database key for the preimage of key, as an ephemeral buffer.
|
// secKey returns the database key for the preimage of key, as an ephemeral buffer.
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,17 @@ import (
|
||||||
|
|
||||||
func newEmptySecure() *SecureTrie {
|
func newEmptySecure() *SecureTrie {
|
||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
trie, _ := NewSecure(common.Hash{}, db, 0)
|
tr, _ := New(common.Hash{}, db, 0)
|
||||||
return trie
|
st := NewSecure(tr, db)
|
||||||
|
return st
|
||||||
}
|
}
|
||||||
|
|
||||||
// makeTestSecureTrie creates a large enough secure trie for testing.
|
// makeTestSecureTrie creates a large enough secure trie for testing.
|
||||||
func makeTestSecureTrie() (ethdb.Database, *SecureTrie, map[string][]byte) {
|
func makeTestSecureTrie() (ethdb.Database, *SecureTrie, map[string][]byte) {
|
||||||
// Create an empty trie
|
// Create an empty trie
|
||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
trie, _ := NewSecure(common.Hash{}, db, 0)
|
tr, _ := New(common.Hash{}, db, 0)
|
||||||
|
trie := NewSecure(tr, db)
|
||||||
|
|
||||||
// Fill it with some arbitrary data
|
// Fill it with some arbitrary data
|
||||||
content := make(map[string][]byte)
|
content := make(map[string][]byte)
|
||||||
|
|
|
||||||
|
|
@ -25,10 +25,10 @@ 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, *Trie, map[string][]byte) {
|
func makeTestTrie() (ethdb.Database, *SimpleTrie, map[string][]byte) {
|
||||||
// Create an empty trie
|
// Create an empty trie
|
||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
trie, _ := New(common.Hash{}, db)
|
trie, _ := New(common.Hash{}, db, 0)
|
||||||
|
|
||||||
// Fill it with some arbitrary data
|
// Fill it with some arbitrary data
|
||||||
content := make(map[string][]byte)
|
content := make(map[string][]byte)
|
||||||
|
|
@ -59,7 +59,7 @@ func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
|
||||||
// content map.
|
// content map.
|
||||||
func checkTrieContents(t *testing.T, db Database, root []byte, content map[string][]byte) {
|
func checkTrieContents(t *testing.T, db Database, root []byte, content map[string][]byte) {
|
||||||
// Check root availability and trie contents
|
// Check root availability and trie contents
|
||||||
trie, err := New(common.BytesToHash(root), db)
|
trie, err := New(common.BytesToHash(root), db, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create trie at %x: %v", root, err)
|
t.Fatalf("failed to create trie at %x: %v", root, err)
|
||||||
}
|
}
|
||||||
|
|
@ -76,7 +76,7 @@ func checkTrieContents(t *testing.T, db Database, root []byte, content map[strin
|
||||||
// checkTrieConsistency checks that all nodes in a trie are indeed present.
|
// checkTrieConsistency checks that all nodes in a trie are indeed present.
|
||||||
func checkTrieConsistency(db Database, root common.Hash) error {
|
func checkTrieConsistency(db Database, root common.Hash) error {
|
||||||
// Create and iterate a trie rooted in a subnode
|
// Create and iterate a trie rooted in a subnode
|
||||||
trie, err := New(root, db)
|
trie, err := New(root, db, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil // // Consider a non existent state consistent
|
return nil // // Consider a non existent state consistent
|
||||||
}
|
}
|
||||||
|
|
@ -88,10 +88,10 @@ func checkTrieConsistency(db Database, root common.Hash) error {
|
||||||
|
|
||||||
// Tests that an empty trie is not scheduled for syncing.
|
// Tests that an empty trie is not scheduled for syncing.
|
||||||
func TestEmptyTrieSync(t *testing.T) {
|
func TestEmptyTrieSync(t *testing.T) {
|
||||||
emptyA, _ := New(common.Hash{}, nil)
|
emptyA, _ := New(common.Hash{}, nil, 0)
|
||||||
emptyB, _ := New(emptyRoot, nil)
|
emptyB, _ := New(emptyRoot, nil, 0)
|
||||||
|
|
||||||
for i, trie := range []*Trie{emptyA, emptyB} {
|
for i, trie := range []*SimpleTrie{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)
|
||||||
|
|
|
||||||
78
trie/trie.go
78
trie/trie.go
|
|
@ -14,7 +14,7 @@
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
// 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/>.
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package trie implements Merkle Patricia Tries.
|
// Package trie implements Merkle Patricia tries.
|
||||||
package trie
|
package trie
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -73,12 +73,27 @@ type DatabaseWriter interface {
|
||||||
Put(key, value []byte) error
|
Put(key, value []byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trie is a Merkle Patricia Trie.
|
// trie is a Merkle Patricia trie.
|
||||||
// The zero value is an empty trie with no database.
|
// The zero value is an empty trie with no database.
|
||||||
// Use New to create a trie that sits on top of a database.
|
// Use New to create a trie that sits on top of a database.
|
||||||
//
|
//
|
||||||
// Trie is not safe for concurrent use.
|
// trie is not safe for concurrent use.
|
||||||
type Trie struct {
|
type Trie interface {
|
||||||
|
Iterator() *Iterator
|
||||||
|
NodeIterator() *NodeIterator
|
||||||
|
Get(key []byte) []byte
|
||||||
|
TryGet(key []byte) ([]byte, error)
|
||||||
|
Update(key, value []byte)
|
||||||
|
TryUpdate(key, value []byte) error
|
||||||
|
Delete(key []byte)
|
||||||
|
TryDelete(key []byte) error
|
||||||
|
Root() []byte
|
||||||
|
Hash() common.Hash
|
||||||
|
Commit() (root common.Hash, err error)
|
||||||
|
CommitTo(db DatabaseWriter) (root common.Hash, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type SimpleTrie struct {
|
||||||
root node
|
root node
|
||||||
db Database
|
db Database
|
||||||
originalRoot common.Hash
|
originalRoot common.Hash
|
||||||
|
|
@ -90,14 +105,8 @@ type Trie struct {
|
||||||
cachegen, cachelimit uint16
|
cachegen, cachelimit uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCacheLimit sets the number of 'cache generations' to keep.
|
|
||||||
// A cache generations is created by a call to Commit.
|
|
||||||
func (t *Trie) SetCacheLimit(l uint16) {
|
|
||||||
t.cachelimit = l
|
|
||||||
}
|
|
||||||
|
|
||||||
// newFlag returns the cache flag value for a newly created node.
|
// newFlag returns the cache flag value for a newly created node.
|
||||||
func (t *Trie) newFlag() nodeFlag {
|
func (t *SimpleTrie) newFlag() nodeFlag {
|
||||||
return nodeFlag{dirty: true, gen: t.cachegen}
|
return nodeFlag{dirty: true, gen: t.cachegen}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,11 +116,14 @@ func (t *Trie) newFlag() nodeFlag {
|
||||||
// trie is initially empty and does not require a database. Otherwise,
|
// trie is initially empty and does not require a database. Otherwise,
|
||||||
// New will panic if db is nil and returns a MissingNodeError if root does
|
// 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.
|
// not exist in the database. Accessing the trie loads nodes from db on demand.
|
||||||
func New(root common.Hash, db Database) (*Trie, error) {
|
//
|
||||||
trie := &Trie{db: db, originalRoot: root}
|
// cacheLimit is the number of 'cache generations' to keep.
|
||||||
|
// A cache generations is created by a call to Commit.
|
||||||
|
func New(root common.Hash, db Database, cacheLimit uint16) (*SimpleTrie, error) {
|
||||||
|
trie := &SimpleTrie{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("trie.New: cannot use existing root without a database")
|
panic("SimpleTrie.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 {
|
||||||
|
|
@ -123,13 +135,17 @@ func New(root common.Hash, db Database) (*Trie, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterator returns an iterator over all mappings in the trie.
|
// Iterator returns an iterator over all mappings in the trie.
|
||||||
func (t *Trie) Iterator() *Iterator {
|
func (t *SimpleTrie) 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 *Trie) Get(key []byte) []byte {
|
func (t *SimpleTrie) 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)
|
||||||
|
|
@ -140,7 +156,7 @@ func (t *Trie) 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 *Trie) TryGet(key []byte) ([]byte, error) {
|
func (t *SimpleTrie) 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 {
|
||||||
|
|
@ -149,7 +165,7 @@ func (t *Trie) TryGet(key []byte) ([]byte, error) {
|
||||||
return value, err
|
return value, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) {
|
func (t *SimpleTrie) 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
|
||||||
|
|
@ -193,7 +209,7 @@ func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode
|
||||||
//
|
//
|
||||||
// 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 *Trie) Update(key, value []byte) {
|
func (t *SimpleTrie) 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)
|
||||||
}
|
}
|
||||||
|
|
@ -207,7 +223,7 @@ func (t *Trie) 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 *Trie) TryUpdate(key, value []byte) error {
|
func (t *SimpleTrie) 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))
|
||||||
|
|
@ -225,7 +241,7 @@ func (t *Trie) TryUpdate(key, value []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
|
func (t *SimpleTrie) 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
|
||||||
|
|
@ -295,7 +311,7 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete removes any existing value for key from the trie.
|
// Delete removes any existing value for key from the trie.
|
||||||
func (t *Trie) Delete(key []byte) {
|
func (t *SimpleTrie) 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)
|
||||||
}
|
}
|
||||||
|
|
@ -303,7 +319,7 @@ func (t *Trie) 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 *Trie) TryDelete(key []byte) error {
|
func (t *SimpleTrie) 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 {
|
||||||
|
|
@ -316,7 +332,7 @@ func (t *Trie) 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 *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
|
func (t *SimpleTrie) 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)
|
||||||
|
|
@ -432,14 +448,14 @@ func concat(s1 []byte, s2 ...byte) []byte {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trie) resolve(n node, prefix, suffix []byte) (node, error) {
|
func (t *SimpleTrie) 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 *Trie) resolveHash(n hashNode, prefix, suffix []byte) (node, error) {
|
func (t *SimpleTrie) 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)
|
||||||
|
|
@ -458,11 +474,11 @@ func (t *Trie) 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 *Trie) Root() []byte { return t.Hash().Bytes() }
|
func (t *SimpleTrie) 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 *Trie) Hash() common.Hash {
|
func (t *SimpleTrie) 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))
|
||||||
|
|
@ -473,7 +489,7 @@ func (t *Trie) 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 *Trie) Commit() (root common.Hash, err error) {
|
func (t *SimpleTrie) 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")
|
||||||
}
|
}
|
||||||
|
|
@ -487,7 +503,7 @@ func (t *Trie) 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 *Trie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
|
func (t *SimpleTrie) 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
|
||||||
|
|
@ -497,7 +513,7 @@ func (t *Trie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
|
||||||
return common.BytesToHash(hash.(hashNode)), nil
|
return common.BytesToHash(hash.(hashNode)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trie) hashRoot(db DatabaseWriter) (node, node, error) {
|
func (t *SimpleTrie) 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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,14 +38,14 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used for testing
|
// Used for testing
|
||||||
func newEmpty() *Trie {
|
func newEmpty() *SimpleTrie {
|
||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
trie, _ := New(common.Hash{}, db)
|
trie, _ := New(common.Hash{}, db, 0)
|
||||||
return trie
|
return trie
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyTrie(t *testing.T) {
|
func TestEmptyTrie(t *testing.T) {
|
||||||
var trie Trie
|
var trie SimpleTrie
|
||||||
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 Trie
|
var trie SimpleTrie
|
||||||
key := make([]byte, 32)
|
key := make([]byte, 32)
|
||||||
value := common.FromHex("0x823140710bf13990e4500136726d8b55")
|
value := common.FromHex("0x823140710bf13990e4500136726d8b55")
|
||||||
trie.Update(key, value)
|
trie.Update(key, value)
|
||||||
|
|
@ -63,7 +63,7 @@ func TestNull(t *testing.T) {
|
||||||
|
|
||||||
func TestMissingRoot(t *testing.T) {
|
func TestMissingRoot(t *testing.T) {
|
||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
trie, err := New(common.HexToHash("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), db)
|
trie, err := New(common.HexToHash("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), db, 0)
|
||||||
if trie != nil {
|
if trie != nil {
|
||||||
t.Error("New returned non-nil trie for invalid root")
|
t.Error("New returned non-nil trie for invalid root")
|
||||||
}
|
}
|
||||||
|
|
@ -74,36 +74,36 @@ func TestMissingRoot(t *testing.T) {
|
||||||
|
|
||||||
func TestMissingNode(t *testing.T) {
|
func TestMissingNode(t *testing.T) {
|
||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
trie, _ := New(common.Hash{}, db)
|
trie, _ := New(common.Hash{}, db, 0)
|
||||||
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
|
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
|
||||||
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
|
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
|
||||||
root, _ := trie.Commit()
|
root, _ := trie.Commit()
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
_, err := trie.TryGet([]byte("120000"))
|
_, err := trie.TryGet([]byte("120000"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
_, err = trie.TryGet([]byte("120099"))
|
_, err = trie.TryGet([]byte("120099"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
_, err = trie.TryGet([]byte("123456"))
|
_, err = trie.TryGet([]byte("123456"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
err = trie.TryUpdate([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"))
|
err = trie.TryUpdate([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
err = trie.TryDelete([]byte("123456"))
|
err = trie.TryDelete([]byte("123456"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
|
@ -111,31 +111,31 @@ func TestMissingNode(t *testing.T) {
|
||||||
|
|
||||||
db.Delete(common.FromHex("e1d943cc8f061a0c0b98162830b970395ac9315654824bf21b73b891365262f9"))
|
db.Delete(common.FromHex("e1d943cc8f061a0c0b98162830b970395ac9315654824bf21b73b891365262f9"))
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
_, err = trie.TryGet([]byte("120000"))
|
_, err = trie.TryGet([]byte("120000"))
|
||||||
if _, ok := err.(*MissingNodeError); !ok {
|
if _, ok := err.(*MissingNodeError); !ok {
|
||||||
t.Errorf("Wrong error: %v", err)
|
t.Errorf("Wrong error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
_, err = trie.TryGet([]byte("120099"))
|
_, err = trie.TryGet([]byte("120099"))
|
||||||
if _, ok := err.(*MissingNodeError); !ok {
|
if _, ok := err.(*MissingNodeError); !ok {
|
||||||
t.Errorf("Wrong error: %v", err)
|
t.Errorf("Wrong error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
_, err = trie.TryGet([]byte("123456"))
|
_, err = trie.TryGet([]byte("123456"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
err = trie.TryUpdate([]byte("120099"), []byte("zxcv"))
|
err = trie.TryUpdate([]byte("120099"), []byte("zxcv"))
|
||||||
if _, ok := err.(*MissingNodeError); !ok {
|
if _, ok := err.(*MissingNodeError); !ok {
|
||||||
t.Errorf("Wrong error: %v", err)
|
t.Errorf("Wrong error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 0)
|
||||||
err = trie.TryDelete([]byte("123456"))
|
err = trie.TryDelete([]byte("123456"))
|
||||||
if _, ok := err.(*MissingNodeError); !ok {
|
if _, ok := err.(*MissingNodeError); !ok {
|
||||||
t.Errorf("Wrong error: %v", err)
|
t.Errorf("Wrong error: %v", err)
|
||||||
|
|
@ -263,7 +263,7 @@ func TestReplication(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new trie on top of the database and check that lookups work.
|
// create a new trie on top of the database and check that lookups work.
|
||||||
trie2, err := New(exp, trie.db)
|
trie2, err := New(exp, trie.db, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't recreate trie at %x: %v", exp, err)
|
t.Fatalf("can't recreate trie at %x: %v", exp, err)
|
||||||
}
|
}
|
||||||
|
|
@ -332,8 +332,7 @@ func TestCacheUnload(t *testing.T) {
|
||||||
// The branch containing it is loaded from DB exactly two times:
|
// The branch containing it is loaded from DB exactly two times:
|
||||||
// in the 0th and 6th iteration.
|
// in the 0th and 6th iteration.
|
||||||
db := &countingDB{Database: trie.db, gets: make(map[string]int)}
|
db := &countingDB{Database: trie.db, gets: make(map[string]int)}
|
||||||
trie, _ = New(root, db)
|
trie, _ = New(root, db, 5)
|
||||||
trie.SetCacheLimit(5)
|
|
||||||
for i := 0; i < 12; i++ {
|
for i := 0; i < 12; i++ {
|
||||||
getString(trie, key1)
|
getString(trie, key1)
|
||||||
trie.Commit()
|
trie.Commit()
|
||||||
|
|
@ -417,7 +416,7 @@ func randRead(r *rand.Rand, b []byte) {
|
||||||
|
|
||||||
func runRandTest(rt randTest) bool {
|
func runRandTest(rt randTest) bool {
|
||||||
db, _ := ethdb.NewMemDatabase()
|
db, _ := ethdb.NewMemDatabase()
|
||||||
tr, _ := New(common.Hash{}, db)
|
tr, _ := New(common.Hash{}, db, 0)
|
||||||
values := make(map[string]string) // tracks content of the trie
|
values := make(map[string]string) // tracks content of the trie
|
||||||
|
|
||||||
for _, step := range rt {
|
for _, step := range rt {
|
||||||
|
|
@ -446,13 +445,13 @@ func runRandTest(rt randTest) bool {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
newtr, err := New(hash, db)
|
newtr, err := New(hash, db, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
tr = newtr
|
tr = newtr
|
||||||
case opItercheckhash:
|
case opItercheckhash:
|
||||||
checktr, _ := New(common.Hash{}, nil)
|
checktr, _ := New(common.Hash{}, nil, 0)
|
||||||
it := tr.Iterator()
|
it := tr.Iterator()
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
checktr.Update(it.Key, it.Value)
|
checktr.Update(it.Key, it.Value)
|
||||||
|
|
@ -520,10 +519,10 @@ func BenchmarkHashLE(b *testing.B) { benchHash(b, binary.LittleEndian) }
|
||||||
const benchElemCount = 20000
|
const benchElemCount = 20000
|
||||||
|
|
||||||
func benchGet(b *testing.B, commit bool) {
|
func benchGet(b *testing.B, commit bool) {
|
||||||
trie := new(Trie)
|
trie, _ := New(common.Hash{}, nil, 0)
|
||||||
if commit {
|
if commit {
|
||||||
_, tmpdb := tempDB()
|
_, tmpdb := tempDB()
|
||||||
trie, _ = New(common.Hash{}, tmpdb)
|
trie, _ = New(common.Hash{}, tmpdb, 0)
|
||||||
}
|
}
|
||||||
k := make([]byte, 32)
|
k := make([]byte, 32)
|
||||||
for i := 0; i < benchElemCount; i++ {
|
for i := 0; i < benchElemCount; i++ {
|
||||||
|
|
@ -548,7 +547,7 @@ func benchGet(b *testing.B, commit bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
|
func benchUpdate(b *testing.B, e binary.ByteOrder) *SimpleTrie {
|
||||||
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++ {
|
||||||
|
|
@ -584,14 +583,14 @@ func tempDB() (string, Database) {
|
||||||
return dir, db
|
return dir, db
|
||||||
}
|
}
|
||||||
|
|
||||||
func getString(trie *Trie, k string) []byte {
|
func getString(trie Trie, k string) []byte {
|
||||||
return trie.Get([]byte(k))
|
return trie.Get([]byte(k))
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateString(trie *Trie, k, v string) {
|
func updateString(trie Trie, k, v string) {
|
||||||
trie.Update([]byte(k), []byte(v))
|
trie.Update([]byte(k), []byte(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteString(trie *Trie, k string) {
|
func deleteString(trie Trie, k string) {
|
||||||
trie.Delete([]byte(k))
|
trie.Delete([]byte(k))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue