-
Notifications
You must be signed in to change notification settings - Fork 198
/
accountsDBApi.go
221 lines (176 loc) · 7.08 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
package state
import (
"bytes"
"fmt"
"sync"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
chainData "github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go/common"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
type accountsDBApi struct {
innerAccountsAdapter AccountsAdapter
chainHandler chainData.ChainHandler
mutLastRootHash sync.RWMutex
lastRootHash []byte
}
// NewAccountsDBApi will create a new instance of type accountsDBApi
func NewAccountsDBApi(innerAccountsAdapter AccountsAdapter, chainHandler chainData.ChainHandler) (*accountsDBApi, error) {
if check.IfNil(innerAccountsAdapter) {
return nil, ErrNilAccountsAdapter
}
if check.IfNil(chainHandler) {
return nil, ErrNilChainHandler
}
return &accountsDBApi{
innerAccountsAdapter: innerAccountsAdapter,
chainHandler: chainHandler,
}, nil
}
func (accountsDB *accountsDBApi) recreateTrieIfNecessary() error {
targetRootHash := accountsDB.chainHandler.GetCurrentBlockRootHash()
if len(targetRootHash) == 0 {
return fmt.Errorf("%w in accountsDBApi when fetching GetCurrentBlockRootHash", ErrNilRootHash)
}
accountsDB.mutLastRootHash.RLock()
lastRootHash := accountsDB.lastRootHash
accountsDB.mutLastRootHash.RUnlock()
if bytes.Equal(lastRootHash, targetRootHash) {
return nil
}
return accountsDB.doRecreateTrie(targetRootHash)
}
func (accountsDB *accountsDBApi) doRecreateTrie(targetRootHash []byte) error {
accountsDB.mutLastRootHash.Lock()
defer accountsDB.mutLastRootHash.Unlock()
// early exit for possible multiple re-entrances here
lastRootHash := accountsDB.lastRootHash
if bytes.Equal(lastRootHash, targetRootHash) {
return nil
}
err := accountsDB.innerAccountsAdapter.RecreateTrie(targetRootHash)
if err != nil {
accountsDB.lastRootHash = nil
return err
}
accountsDB.lastRootHash = targetRootHash
return nil
}
// GetExistingAccount will call the inner accountsAdapter method after trying to recreate the trie
func (accountsDB *accountsDBApi) GetExistingAccount(address []byte) (vmcommon.AccountHandler, error) {
err := accountsDB.recreateTrieIfNecessary()
if err != nil {
return nil, err
}
return accountsDB.innerAccountsAdapter.GetExistingAccount(address)
}
// 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
}
// GetNumCheckpoints will always return 0
func (accountsDB *accountsDBApi) GetNumCheckpoints() uint32 {
return 0
}
// GetCode will call the inner accountsAdapter method after trying to recreate the trie
func (accountsDB *accountsDBApi) GetCode(codeHash []byte) []byte {
err := accountsDB.recreateTrieIfNecessary()
if err != nil {
return nil
}
return accountsDB.innerAccountsAdapter.GetCode(codeHash)
}
// RootHash will return last loaded root hash
func (accountsDB *accountsDBApi) RootHash() ([]byte, error) {
accountsDB.mutLastRootHash.RLock()
defer accountsDB.mutLastRootHash.RUnlock()
if accountsDB.lastRootHash == nil {
return nil, ErrNilRootHash
}
return accountsDB.lastRootHash, nil
}
// RecreateTrie is a not permitted operation in this implementation and thus, will return an error
func (accountsDB *accountsDBApi) RecreateTrie(_ []byte) error {
return ErrOperationNotPermitted
}
// PruneTrie is a not permitted operation in this implementation and thus, does nothing
func (accountsDB *accountsDBApi) PruneTrie(_ []byte, _ TriePruningIdentifier) {
}
// 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(rootHash []byte) (chan core.KeyValueHolder, error) {
err := accountsDB.recreateTrieIfNecessary()
if err != nil {
return nil, err
}
return accountsDB.innerAccountsAdapter.GetAllLeaves(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()
}
// IsInterfaceNil returns true if there is no value under the interface
func (accountsDB *accountsDBApi) IsInterfaceNil() bool {
return accountsDB == nil
}