-
Notifications
You must be signed in to change notification settings - Fork 202
/
peerAccountsDB.go
174 lines (143 loc) · 5.13 KB
/
peerAccountsDB.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
package state
import (
"bytes"
"fmt"
"sync"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go/common"
)
// PeerAccountsDB will save and synchronize data from peer processor, plus will synchronize with nodesCoordinator
type PeerAccountsDB struct {
*AccountsDB
}
// NewPeerAccountsDB creates a new account manager
func NewPeerAccountsDB(
trie common.Trie,
hasher hashing.Hasher,
marshalizer marshal.Marshalizer,
accountFactory AccountFactory,
storagePruningManager StoragePruningManager,
) (*PeerAccountsDB, error) {
if check.IfNil(trie) {
return nil, ErrNilTrie
}
if check.IfNil(hasher) {
return nil, ErrNilHasher
}
if check.IfNil(marshalizer) {
return nil, ErrNilMarshalizer
}
if check.IfNil(accountFactory) {
return nil, ErrNilAccountFactory
}
if check.IfNil(storagePruningManager) {
return nil, ErrNilStoragePruningManager
}
trieStorageManager := trie.GetStorageManager()
numCheckpoints := getNumCheckpoints(trieStorageManager)
adb := &PeerAccountsDB{
&AccountsDB{
mainTrie: trie,
hasher: hasher,
marshalizer: marshalizer,
accountFactory: accountFactory,
entries: make([]JournalEntry, 0),
dataTries: NewDataTriesHolder(),
mutOp: sync.RWMutex{},
numCheckpoints: numCheckpoints,
loadCodeMeasurements: &loadingMeasurements{
identifier: "load code",
},
storagePruningManager: storagePruningManager,
lastSnapshot: &snapshotInfo{},
},
}
val, err := trieStorageManager.GetFromCurrentEpoch([]byte(common.ActiveDBKey))
if err != nil || !bytes.Equal(val, []byte(common.ActiveDBVal)) {
startSnapshotAfterRestart(adb, trieStorageManager)
}
return adb, nil
}
// MarkSnapshotDone will mark that the snapshot process has been completed
func (adb *PeerAccountsDB) MarkSnapshotDone() {
trieStorageManager, epoch, err := adb.getTrieStorageManagerAndLatestEpoch()
if err != nil {
log.Error("MarkSnapshotDone error", "err", err.Error())
return
}
err = trieStorageManager.PutInEpoch([]byte(common.ActiveDBKey), []byte(common.ActiveDBVal), epoch)
handleLoggingWhenError("error while putting active DB value into main storer", err)
}
func (adb *PeerAccountsDB) getTrieStorageManagerAndLatestEpoch() (common.StorageManager, uint32, error) {
trieStorageManager := adb.mainTrie.GetStorageManager()
epoch, err := trieStorageManager.GetLatestStorageEpoch()
if err != nil {
return nil, 0, fmt.Errorf("%w while getting the latest storage epoch", err)
}
return trieStorageManager, epoch, nil
}
// SnapshotState triggers the snapshotting process of the state trie
func (adb *PeerAccountsDB) SnapshotState(rootHash []byte) {
log.Trace("peerAccountsDB.SnapshotState", "root hash", rootHash)
trieStorageManager, epoch, err := adb.getTrieStorageManagerAndLatestEpoch()
if err != nil {
log.Error("SnapshotState error", "err", err.Error())
return
}
if !trieStorageManager.ShouldTakeSnapshot() {
log.Debug("skipping snapshot for rootHash", "hash", rootHash)
return
}
log.Debug("starting snapshot", "rootHash", rootHash, "epoch", epoch)
adb.lastSnapshot.rootHash = rootHash
adb.lastSnapshot.epoch = epoch
err = trieStorageManager.Put([]byte(lastSnapshotStarted), rootHash)
if err != nil {
log.Warn("could not set lastSnapshotStarted", "err", err, "rootHash", rootHash)
}
stats := newSnapshotStatistics(0)
trieStorageManager.EnterPruningBufferingMode()
stats.NewSnapshotStarted()
trieStorageManager.TakeSnapshot(rootHash, rootHash, nil, stats, epoch)
trieStorageManager.ExitPruningBufferingMode()
go func() {
printStats(stats, "snapshotState peer trie", rootHash)
err = trieStorageManager.PutInEpoch([]byte(common.ActiveDBKey), []byte(common.ActiveDBVal), epoch)
handleLoggingWhenError("error while putting active DB value into main storer", err)
}()
if adb.processingMode == common.ImportDb {
stats.WaitForSnapshotsToFinish()
}
adb.increaseNumCheckpoints()
}
// SetStateCheckpoint triggers the checkpointing process of the state trie
func (adb *PeerAccountsDB) SetStateCheckpoint(rootHash []byte) {
log.Trace("peerAccountsDB.SetStateCheckpoint", "root hash", rootHash)
trieStorageManager := adb.mainTrie.GetStorageManager()
stats := newSnapshotStatistics(0)
trieStorageManager.EnterPruningBufferingMode()
stats.NewSnapshotStarted()
trieStorageManager.SetCheckpoint(rootHash, rootHash, nil, stats)
trieStorageManager.ExitPruningBufferingMode()
go printStats(stats, "snapshotState peer trie", rootHash)
if adb.processingMode == common.ImportDb {
stats.WaitForSnapshotsToFinish()
}
adb.increaseNumCheckpoints()
}
// RecreateAllTries recreates all the tries from the accounts DB
func (adb *PeerAccountsDB) RecreateAllTries(rootHash []byte) (map[string]common.Trie, error) {
recreatedTrie, err := adb.mainTrie.Recreate(rootHash)
if err != nil {
return nil, err
}
allTries := make(map[string]common.Trie)
allTries[string(rootHash)] = recreatedTrie
return allTries, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (adb *PeerAccountsDB) IsInterfaceNil() bool {
return adb == nil
}