-
Notifications
You must be signed in to change notification settings - Fork 202
/
accountsDBApi.go
262 lines (209 loc) · 9.16 KB
/
accountsDBApi.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package state
import (
"context"
"fmt"
"sync"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/common/holders"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
type accountsDBApi struct {
innerAccountsAdapter AccountsAdapter
blockInfoProvider BlockInfoProvider
mutRecreatedTrieBlockInfo sync.RWMutex
blockInfo common.BlockInfo
}
// NewAccountsDBApi will create a new instance of type accountsDBApi
func NewAccountsDBApi(innerAccountsAdapter AccountsAdapter, blockInfoProvider BlockInfoProvider) (*accountsDBApi, error) {
if check.IfNil(innerAccountsAdapter) {
return nil, ErrNilAccountsAdapter
}
if check.IfNil(blockInfoProvider) {
return nil, ErrNilBlockInfoProvider
}
return &accountsDBApi{
innerAccountsAdapter: innerAccountsAdapter,
blockInfoProvider: blockInfoProvider,
}, nil
}
func (accountsDB *accountsDBApi) recreateTrieIfNecessary() (common.BlockInfo, error) {
newBlockInfo := accountsDB.blockInfoProvider.GetBlockInfo()
if check.IfNil(newBlockInfo) {
return nil, fmt.Errorf("%w in accountsDBApi.recreateTrieIfNecessary", ErrNilBlockInfo)
}
if len(newBlockInfo.GetRootHash()) == 0 {
return nil, fmt.Errorf("%w in accountsDBApi.recreateTrieIfNecessary", ErrNilRootHash)
}
accountsDB.mutRecreatedTrieBlockInfo.RLock()
currentBlockInfo := accountsDB.blockInfo
accountsDB.mutRecreatedTrieBlockInfo.RUnlock()
if newBlockInfo.Equal(currentBlockInfo) {
return currentBlockInfo, nil
}
return accountsDB.doRecreateTrieWithBlockInfo(newBlockInfo)
}
func (accountsDB *accountsDBApi) doRecreateTrieWithBlockInfo(newBlockInfo common.BlockInfo) (common.BlockInfo, error) {
accountsDB.mutRecreatedTrieBlockInfo.Lock()
defer accountsDB.mutRecreatedTrieBlockInfo.Unlock()
// early exit for possible multiple re-entrances here
currentBlockInfo := accountsDB.blockInfo
if newBlockInfo.Equal(currentBlockInfo) {
return currentBlockInfo, nil
}
err := accountsDB.innerAccountsAdapter.RecreateTrie(newBlockInfo.GetRootHash())
if err != nil {
accountsDB.blockInfo = nil
return nil, err
}
accountsDB.blockInfo = newBlockInfo
return newBlockInfo, nil
}
// StartSnapshotIfNeeded does nothing for this implementation
func (accountsDB *accountsDBApi) StartSnapshotIfNeeded() {
}
// GetExistingAccount will call the inner accountsAdapter method after trying to recreate the trie
func (accountsDB *accountsDBApi) GetExistingAccount(address []byte) (vmcommon.AccountHandler, error) {
account, _, err := accountsDB.GetAccountWithBlockInfo(address, holders.NewRootHashHolderAsEmpty())
return account, err
}
// GetAccountFromBytes will call the inner accountsAdapter method after trying to recreate the trie
func (accountsDB *accountsDBApi) GetAccountFromBytes(address []byte, accountBytes []byte) (vmcommon.AccountHandler, error) {
_, err := accountsDB.recreateTrieIfNecessary()
if err != nil {
return nil, err
}
return accountsDB.innerAccountsAdapter.GetAccountFromBytes(address, accountBytes)
}
// LoadAccount will call the inner accountsAdapter method after trying to recreate the trie
func (accountsDB *accountsDBApi) LoadAccount(address []byte) (vmcommon.AccountHandler, error) {
_, err := accountsDB.recreateTrieIfNecessary()
if err != nil {
return nil, err
}
return accountsDB.innerAccountsAdapter.LoadAccount(address)
}
// SaveAccount is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) SaveAccount(_ vmcommon.AccountHandler) error {
return ErrOperationNotPermitted
}
// RemoveAccount is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) RemoveAccount(_ []byte) error {
return ErrOperationNotPermitted
}
// CommitInEpoch is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) CommitInEpoch(_ uint32, _ uint32) ([]byte, error) {
return nil, ErrOperationNotPermitted
}
// Commit is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) Commit() ([]byte, error) {
return nil, ErrOperationNotPermitted
}
// JournalLen will always return 0
func (accountsDB *accountsDBApi) JournalLen() int {
return 0
}
// RevertToSnapshot is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) RevertToSnapshot(_ int) error {
return ErrOperationNotPermitted
}
// GetCode will call the inner accountsAdapter method after trying to recreate the trie
func (accountsDB *accountsDBApi) GetCode(codeHash []byte) []byte {
code, _, err := accountsDB.GetCodeWithBlockInfo(codeHash, holders.NewRootHashHolderAsEmpty())
if err != nil {
log.Warn("accountsDBApi.GetCode", "error", err)
}
return code
}
// RootHash will return last loaded root hash
func (accountsDB *accountsDBApi) RootHash() ([]byte, error) {
accountsDB.mutRecreatedTrieBlockInfo.RLock()
defer accountsDB.mutRecreatedTrieBlockInfo.RUnlock()
blockInfo := accountsDB.blockInfo
if check.IfNil(blockInfo) || blockInfo.GetRootHash() == nil {
return nil, ErrNilRootHash
}
return blockInfo.GetRootHash(), nil
}
// RecreateTrie is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) RecreateTrie(_ []byte) error {
return ErrOperationNotPermitted
}
// RecreateTrieFromEpoch is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) RecreateTrieFromEpoch(_ common.RootHashHolder) error {
return ErrOperationNotPermitted
}
// PruneTrie is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApi) PruneTrie(_ []byte, _ TriePruningIdentifier, _ PruningHandler) {
}
// CancelPrune is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApi) CancelPrune(_ []byte, _ TriePruningIdentifier) {
}
// SnapshotState is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApi) SnapshotState(_ []byte) {
}
// SetStateCheckpoint is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApi) SetStateCheckpoint(_ []byte) {
}
// IsPruningEnabled will call the inner accountsAdapter method
func (accountsDB *accountsDBApi) IsPruningEnabled() bool {
return accountsDB.innerAccountsAdapter.IsPruningEnabled()
}
// GetAllLeaves will call the inner accountsAdapter method after trying to recreate the trie
func (accountsDB *accountsDBApi) GetAllLeaves(leavesChannel chan core.KeyValueHolder, ctx context.Context, rootHash []byte) error {
_, err := accountsDB.recreateTrieIfNecessary()
if err != nil {
return err
}
return accountsDB.innerAccountsAdapter.GetAllLeaves(leavesChannel, ctx, rootHash)
}
// RecreateAllTries is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) RecreateAllTries(_ []byte) (map[string]common.Trie, error) {
return nil, ErrOperationNotPermitted
}
// GetTrie will call the inner accountsAdapter method
func (accountsDB *accountsDBApi) GetTrie(rootHash []byte) (common.Trie, error) {
return accountsDB.innerAccountsAdapter.GetTrie(rootHash)
}
// GetStackDebugFirstEntry will call the inner accountsAdapter method
func (accountsDB *accountsDBApi) GetStackDebugFirstEntry() []byte {
return accountsDB.innerAccountsAdapter.GetStackDebugFirstEntry()
}
// Close will handle the closing of the underlying components
func (accountsDB *accountsDBApi) Close() error {
return accountsDB.innerAccountsAdapter.Close()
}
// GetAccountWithBlockInfo returns the account and the associated block info
func (accountsDB *accountsDBApi) GetAccountWithBlockInfo(address []byte, _ common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) {
_, err := accountsDB.recreateTrieIfNecessary()
if err != nil {
return nil, nil, err
}
// We hold the read mutex over both <getting the current block info> AND <getting the account>.
// -> desired side-effect: any concurrent "recreateTrieIfNecessary()" waits until the mutex is released.
// -> under normal circumstances (node already synchronized), performance of GET account should not be impacted.
accountsDB.mutRecreatedTrieBlockInfo.RLock()
defer accountsDB.mutRecreatedTrieBlockInfo.RUnlock()
blockInfo := accountsDB.blockInfo
account, err := accountsDB.innerAccountsAdapter.GetExistingAccount(address)
if err == ErrAccNotFound {
return nil, nil, NewErrAccountNotFoundAtBlock(blockInfo)
}
if err != nil {
return nil, nil, err
}
return account, blockInfo, nil
}
// GetCodeWithBlockInfo returns the code and the associated block info
func (accountsDB *accountsDBApi) GetCodeWithBlockInfo(codeHash []byte, _ common.RootHashHolder) ([]byte, common.BlockInfo, error) {
blockInfo, err := accountsDB.recreateTrieIfNecessary()
if err != nil {
return nil, nil, err
}
return accountsDB.innerAccountsAdapter.GetCode(codeHash), blockInfo, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (accountsDB *accountsDBApi) IsInterfaceNil() bool {
return accountsDB == nil
}