-
Notifications
You must be signed in to change notification settings - Fork 199
/
accountsDBApiWithHistory.go
242 lines (193 loc) · 8.67 KB
/
accountsDBApiWithHistory.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
package state
import (
"bytes"
"context"
"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 accountsDBApiWithHistory struct {
innerAccountsAdapter AccountsAdapter
latestRecreatedRootHash []byte
mutRecreateAndGet sync.RWMutex
}
// NewAccountsDBApiWithHistory will create a new instance of type accountsDBApiWithHistory
func NewAccountsDBApiWithHistory(innerAccountsAdapter AccountsAdapter) (*accountsDBApiWithHistory, error) {
if check.IfNil(innerAccountsAdapter) {
return nil, ErrNilAccountsAdapter
}
return &accountsDBApiWithHistory{
innerAccountsAdapter: innerAccountsAdapter,
}, nil
}
// StartSnapshotIfNeeded is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApiWithHistory) StartSnapshotIfNeeded() {
}
// GetExistingAccount will return an error
func (accountsDB *accountsDBApiWithHistory) GetExistingAccount(_ []byte) (vmcommon.AccountHandler, error) {
return nil, ErrFunctionalityNotImplemented
}
// GetAccountFromBytes will return an error
func (accountsDB *accountsDBApiWithHistory) GetAccountFromBytes(_ []byte, _ []byte) (vmcommon.AccountHandler, error) {
return nil, ErrFunctionalityNotImplemented
}
// LoadAccount will return an error
func (accountsDB *accountsDBApiWithHistory) LoadAccount(_ []byte) (vmcommon.AccountHandler, error) {
return nil, ErrFunctionalityNotImplemented
}
// SaveAccount is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApiWithHistory) SaveAccount(_ vmcommon.AccountHandler) error {
return ErrOperationNotPermitted
}
// RemoveAccount is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApiWithHistory) RemoveAccount(_ []byte) error {
return ErrOperationNotPermitted
}
// CommitInEpoch is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApiWithHistory) 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 *accountsDBApiWithHistory) Commit() ([]byte, error) {
return nil, ErrOperationNotPermitted
}
// JournalLen will always return 0
func (accountsDB *accountsDBApiWithHistory) JournalLen() int {
return 0
}
// RevertToSnapshot is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApiWithHistory) RevertToSnapshot(_ int) error {
return ErrOperationNotPermitted
}
// GetCode will call the inner accountsAdapter method after trying to recreate the trie
func (accountsDB *accountsDBApiWithHistory) GetCode(_ []byte) []byte {
return nil
}
// RootHash will return an error
func (accountsDB *accountsDBApiWithHistory) RootHash() ([]byte, error) {
return nil, ErrOperationNotPermitted
}
// RecreateTrie is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApiWithHistory) RecreateTrie(_ []byte) error {
return ErrOperationNotPermitted
}
// RecreateTrieFromEpoch is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApiWithHistory) RecreateTrieFromEpoch(_ common.RootHashHolder) error {
return ErrOperationNotPermitted
}
// PruneTrie is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApiWithHistory) PruneTrie(_ []byte, _ TriePruningIdentifier, _ PruningHandler) {
}
// CancelPrune is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApiWithHistory) CancelPrune(_ []byte, _ TriePruningIdentifier) {
}
// SnapshotState is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApiWithHistory) SnapshotState(_ []byte) {
}
// SetStateCheckpoint is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApiWithHistory) SetStateCheckpoint(_ []byte) {
}
// IsPruningEnabled will return false
func (accountsDB *accountsDBApiWithHistory) IsPruningEnabled() bool {
return false
}
// GetAllLeaves will return an error
func (accountsDB *accountsDBApiWithHistory) GetAllLeaves(_ chan core.KeyValueHolder, _ context.Context, _ []byte) error {
return ErrOperationNotPermitted
}
// RecreateAllTries is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApiWithHistory) RecreateAllTries(_ []byte) (map[string]common.Trie, error) {
return nil, ErrOperationNotPermitted
}
// GetTrie is not implemented
func (accountsDB *accountsDBApiWithHistory) GetTrie(_ []byte) (common.Trie, error) {
return nil, ErrFunctionalityNotImplemented
}
// GetStackDebugFirstEntry returns nil
func (accountsDB *accountsDBApiWithHistory) GetStackDebugFirstEntry() []byte {
return nil
}
// Close will handle the closing of the underlying components
func (accountsDB *accountsDBApiWithHistory) Close() error {
return accountsDB.innerAccountsAdapter.Close()
}
// GetAccountWithBlockInfo returns the account and the associated block info
func (accountsDB *accountsDBApiWithHistory) GetAccountWithBlockInfo(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) {
rootHash := options.GetRootHash()
// First check to avoid re-creation:
accountsDB.mutRecreateAndGet.RLock()
if !accountsDB.shouldRecreateTrieUnprotected(rootHash) {
// Re-creation is not necessary, but make sure no other routine re-creates it.
defer accountsDB.mutRecreateAndGet.RUnlock()
return accountsDB.doGetAccountWithBlockInfoUnprotected(address, rootHash)
}
accountsDB.mutRecreateAndGet.RUnlock()
// Re-creation seems to be necessary, promote to a "write" lock.
accountsDB.mutRecreateAndGet.Lock()
defer accountsDB.mutRecreateAndGet.Unlock()
// Second check to avoid re-creation:
if accountsDB.shouldRecreateTrieUnprotected(rootHash) {
err := accountsDB.recreateTrieUnprotected(options)
if err != nil {
return nil, nil, err
}
}
return accountsDB.doGetAccountWithBlockInfoUnprotected(address, rootHash)
}
func (accountsDB *accountsDBApiWithHistory) doGetAccountWithBlockInfoUnprotected(address []byte, rootHash []byte) (vmcommon.AccountHandler, common.BlockInfo, error) {
blockInfo := holders.NewBlockInfo(nil, 0, rootHash)
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 *accountsDBApiWithHistory) GetCodeWithBlockInfo(codeHash []byte, options common.RootHashHolder) ([]byte, common.BlockInfo, error) {
rootHash := options.GetRootHash()
// First check to avoid re-creation:
accountsDB.mutRecreateAndGet.RLock()
if !accountsDB.shouldRecreateTrieUnprotected(rootHash) {
// Re-creation is not necessary, but make sure no other routine re-creates it.
defer accountsDB.mutRecreateAndGet.RUnlock()
return accountsDB.doGetCodeWithBlockInfoUnprotected(codeHash, rootHash)
}
accountsDB.mutRecreateAndGet.RUnlock()
// Re-creation seems to be necessary, promote to a "write" lock.
accountsDB.mutRecreateAndGet.Lock()
defer accountsDB.mutRecreateAndGet.Unlock()
// Second check to avoid re-creation:
if accountsDB.shouldRecreateTrieUnprotected(rootHash) {
err := accountsDB.recreateTrieUnprotected(options)
if err != nil {
return nil, nil, err
}
}
return accountsDB.doGetCodeWithBlockInfoUnprotected(codeHash, rootHash)
}
func (accountsDB *accountsDBApiWithHistory) doGetCodeWithBlockInfoUnprotected(codeHash []byte, rootHash []byte) ([]byte, common.BlockInfo, error) {
blockInfo := holders.NewBlockInfo(nil, 0, rootHash)
code := accountsDB.innerAccountsAdapter.GetCode(codeHash)
return code, blockInfo, nil
}
func (accountsDB *accountsDBApiWithHistory) shouldRecreateTrieUnprotected(rootHash []byte) bool {
return !bytes.Equal(accountsDB.latestRecreatedRootHash, rootHash)
}
func (accountsDB *accountsDBApiWithHistory) recreateTrieUnprotected(options common.RootHashHolder) error {
err := accountsDB.innerAccountsAdapter.RecreateTrieFromEpoch(options)
if err != nil {
return err
}
accountsDB.latestRecreatedRootHash = options.GetRootHash()
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (accountsDB *accountsDBApiWithHistory) IsInterfaceNil() bool {
return accountsDB == nil
}