/
database.go
216 lines (186 loc) · 7.96 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Modifications Copyright 2018 The klaytn Authors
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from core/state/database.go (2018/06/04).
// Modified and improved for the klaytn development.
package state
import (
"fmt"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/storage/database"
"github.com/klaytn/klaytn/storage/statedb"
)
const (
// Number of codehash->size associations to keep
codeSizeCacheSize = 100000
// Number of shards in cache
shardsCodeSizeCache = 4096
)
// Database wraps access to tries and contract code.
type Database interface {
// OpenTrie opens the main account trie.
OpenTrie(root common.Hash) (Trie, error)
OpenTrieForPrefetching(root common.Hash) (Trie, error)
// OpenStorageTrie opens the storage trie of an account.
OpenStorageTrie(root common.Hash) (Trie, error)
OpenStorageTrieForPrefetching(root common.Hash) (Trie, error)
// CopyTrie returns an independent copy of the given trie.
CopyTrie(Trie) Trie
// ContractCode retrieves a particular contract's code.
ContractCode(codeHash common.Hash) ([]byte, error)
// ContractCodeSize retrieves a particular contracts code's size.
ContractCodeSize(codeHash common.Hash) (int, error)
// TrieDB retrieves the low level trie database used for data storage.
TrieDB() *statedb.Database
// RLockGCCachedNode locks the GC lock of CachedNode.
RLockGCCachedNode()
// RUnlockGCCachedNode unlocks the GC lock of CachedNode.
RUnlockGCCachedNode()
}
// Trie is a Klaytn 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
TryUpdateWithKeys(key, hashKey, hexKey, 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 trie's memory database, tracking the internal
// and external (for account tries) references.
Commit(onleaf statedb.LeafCallback) (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) statedb.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 database.DBManager) 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 NewDatabaseWithNewCache constructor.
func NewDatabase(db database.DBManager) Database {
return NewDatabaseWithNewCache(db, statedb.GetEmptyTrieNodeCacheConfig())
}
func getCodeSizeCache() common.Cache {
var cacheConfig common.CacheConfiger
switch common.DefaultCacheType {
case common.LRUShardCacheType:
cacheConfig = common.LRUShardConfig{CacheSize: codeSizeCacheSize, NumShards: shardsCodeSizeCache}
case common.LRUCacheType:
cacheConfig = common.LRUConfig{CacheSize: codeSizeCacheSize}
case common.FIFOCacheType:
cacheConfig = common.FIFOCacheConfig{CacheSize: codeSizeCacheSize}
default:
cacheConfig = common.FIFOCacheConfig{CacheSize: codeSizeCacheSize}
}
return common.NewCache(cacheConfig)
}
// NewDatabaseWithNewCache creates a backing store for state. The returned database
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
// large memory cache.
func NewDatabaseWithNewCache(db database.DBManager, cacheConfig *statedb.TrieNodeCacheConfig) Database {
return &cachingDB{
db: statedb.NewDatabaseWithNewCache(db, cacheConfig),
codeSizeCache: getCodeSizeCache(),
}
}
// NewDatabaseWithExistingCache creates a backing store for state with given cache. The returned database
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
// large memory cache.
func NewDatabaseWithExistingCache(db database.DBManager, cache statedb.TrieNodeCache) Database {
return &cachingDB{
db: statedb.NewDatabaseWithExistingCache(db, cache),
codeSizeCache: getCodeSizeCache(),
}
}
type cachingDB struct {
db *statedb.Database
codeSizeCache common.Cache
}
// OpenTrie opens the main account trie at a specific root hash.
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
return statedb.NewSecureTrie(root, db.db)
}
// OpenTrieForPrefetching opens the main account trie at a specific root hash.
func (db *cachingDB) OpenTrieForPrefetching(root common.Hash) (Trie, error) {
return statedb.NewSecureTrieForPrefetching(root, db.db)
}
// OpenStorageTrie opens the storage trie of an account.
func (db *cachingDB) OpenStorageTrie(root common.Hash) (Trie, error) {
return statedb.NewSecureTrie(root, db.db)
}
// OpenStorageTrieForPrefetching opens the storage trie of an account.
func (db *cachingDB) OpenStorageTrieForPrefetching(root common.Hash) (Trie, error) {
return statedb.NewSecureTrieForPrefetching(root, db.db)
}
// CopyTrie returns an independent copy of the given trie.
func (db *cachingDB) CopyTrie(t Trie) Trie {
switch t := t.(type) {
case *statedb.SecureTrie:
return t.Copy()
default:
panic(fmt.Errorf("unknown trie type %T", t))
}
}
// ContractCode retrieves a particular contract's code.
func (db *cachingDB) ContractCode(codeHash common.Hash) ([]byte, error) {
code, err := db.db.Node(codeHash)
if err == nil {
db.codeSizeCache.Add(codeHash, len(code))
}
return code, err
}
// ContractCodeSize retrieves a particular contracts code's size.
func (db *cachingDB) ContractCodeSize(codeHash common.Hash) (int, error) {
if cached, ok := db.codeSizeCache.Get(codeHash); ok {
return cached.(int), nil
}
code, err := db.ContractCode(codeHash)
return len(code), err
}
// TrieDB retrieves the low level trie database used for data storage.
func (db *cachingDB) TrieDB() *statedb.Database {
return db.db
}
// RLockGCCachedNode locks the GC lock of CachedNode.
func (db *cachingDB) RLockGCCachedNode() {
db.db.RLockGCCachedNode()
}
// RUnlockGCCachedNode unlocks the GC lock of CachedNode.
func (db *cachingDB) RUnlockGCCachedNode() {
db.db.RUnlockGCCachedNode()
}