-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
101 lines (83 loc) · 3.32 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package state
import (
"fmt"
"github.com/fatzero/mass-core/trie"
"github.com/fatzero/mass-core/trie/common"
"github.com/fatzero/mass-core/trie/massdb"
)
// Database wraps access to tries and contract code.
type Database interface {
OpenBindingTrie(root common.Hash) (Trie, error)
// CopyTrie returns an independent copy of the given trie.
CopyTrie(Trie) Trie
// TrieDB retrieves the low level trie database used for data storage.
TrieDB() *trie.Database
}
// Trie is a Ethereum Merkle Patricia trie.
type Trie interface {
// // GetKey returns the sha3 preimage of a hashed key that was previously used
// // to store a value.
// //
// // TODO(fjl): remove this when SecureTrie is removed
// GetKey([]byte) []byte
// 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
// trie.MissingNodeError is returned.
TryGet(key []byte) ([]byte, error)
// TryUpdate associates key with value in the trie. If value has length zero, any
// existing value is deleted from the trie. 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 trie.MissingNodeError is returned.
TryUpdate(key, value []byte) error
// TryDelete removes any existing value for key from the trie. If a node was not
// found in the database, a trie.MissingNodeError is returned.
TryDelete(key []byte) error
// 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.
Hash() common.Hash
// Commit writes all nodes to the database.
Commit() (common.Hash, error)
// NodeIterator returns an iterator that returns nodes of the trie. Iteration
// starts at the key after the given start key.
NodeIterator(startKey []byte) trie.NodeIterator
// // Prove constructs a Merkle proof for key. The result contains all encoded nodes
// // on the path to the value at key. The value itself is also included in the last
// // node and can be retrieved by verifying the proof.
// //
// // If the trie does not contain a value for key, the returned proof contains all
// // nodes of the longest existing prefix of the key (at least the root), ending
// // with the node that proves the absence of the key.
// Prove(key []byte, fromLevel uint, proofDb massdb.KeyValueWriter) error
}
// NewDatabase creates a backing store for state. The returned database is safe for
// concurrent use, but does not retain any recent trie nodes in memory. To keep some
// historical state in memory, use the NewDatabaseWithConfig constructor.
func NewDatabase(db massdb.Database) Database {
return &cachingDB{
db: trie.NewDatabase(db),
}
}
type cachingDB struct {
db *trie.Database
}
// OpenTrie opens the main account trie at a specific root hash.
func (db *cachingDB) OpenBindingTrie(root common.Hash) (Trie, error) {
tr, err := trie.New(root, db.db)
if err != nil {
return nil, err
}
return tr, nil
}
// CopyTrie returns an independent copy of the given trie.
func (db *cachingDB) CopyTrie(t Trie) Trie {
switch t := t.(type) {
case *trie.Trie:
return t.Copy()
default:
panic(fmt.Errorf("unknown trie type %T", t))
}
}
// TrieDB retrieves any intermediate trie-node caching layer.
func (db *cachingDB) TrieDB() *trie.Database {
return db.db
}