/
snapshots.go
289 lines (242 loc) · 7.7 KB
/
snapshots.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package statesync
import (
"crypto/sha256"
"fmt"
"math/rand"
"sort"
"strings"
tmsync "github.com/tendermint/tendermint/internal/libs/sync"
"github.com/tendermint/tendermint/types"
)
// snapshotKey is a snapshot key used for lookups.
type snapshotKey [sha256.Size]byte
// snapshot contains data about a snapshot.
type snapshot struct {
Height uint64
Format uint32
Chunks uint32
Hash []byte
Metadata []byte
trustedAppHash []byte // populated by light client
}
// Key generates a snapshot key, used for lookups. It takes into account not only the height and
// format, but also the chunks, hash, and metadata in case peers have generated snapshots in a
// non-deterministic manner. All fields must be equal for the snapshot to be considered the same.
func (s *snapshot) Key() snapshotKey {
// Hash.Write() never returns an error.
hasher := sha256.New()
hasher.Write([]byte(fmt.Sprintf("%v:%v:%v", s.Height, s.Format, s.Chunks)))
hasher.Write(s.Hash)
hasher.Write(s.Metadata)
var key snapshotKey
copy(key[:], hasher.Sum(nil))
return key
}
// snapshotPool discovers and aggregates snapshots across peers.
type snapshotPool struct {
tmsync.Mutex
snapshots map[snapshotKey]*snapshot
snapshotPeers map[snapshotKey]map[types.NodeID]types.NodeID
// indexes for fast searches
formatIndex map[uint32]map[snapshotKey]bool
heightIndex map[uint64]map[snapshotKey]bool
peerIndex map[types.NodeID]map[snapshotKey]bool
// blacklists for rejected items
formatBlacklist map[uint32]bool
peerBlacklist map[types.NodeID]bool
snapshotBlacklist map[snapshotKey]bool
}
// newSnapshotPool creates a new empty snapshot pool.
func newSnapshotPool() *snapshotPool {
return &snapshotPool{
snapshots: make(map[snapshotKey]*snapshot),
snapshotPeers: make(map[snapshotKey]map[types.NodeID]types.NodeID),
formatIndex: make(map[uint32]map[snapshotKey]bool),
heightIndex: make(map[uint64]map[snapshotKey]bool),
peerIndex: make(map[types.NodeID]map[snapshotKey]bool),
formatBlacklist: make(map[uint32]bool),
peerBlacklist: make(map[types.NodeID]bool),
snapshotBlacklist: make(map[snapshotKey]bool),
}
}
// Add adds a snapshot to the pool, unless the peer has already sent recentSnapshots
// snapshots. It returns true if this was a new, non-blacklisted snapshot. The
// snapshot height is verified using the light client, and the expected app hash
// is set for the snapshot.
func (p *snapshotPool) Add(peerID types.NodeID, snapshot *snapshot) (bool, error) {
key := snapshot.Key()
p.Lock()
defer p.Unlock()
switch {
case p.formatBlacklist[snapshot.Format]:
return false, nil
case p.peerBlacklist[peerID]:
return false, nil
case p.snapshotBlacklist[key]:
return false, nil
case len(p.peerIndex[peerID]) >= recentSnapshots:
return false, nil
}
if p.snapshotPeers[key] == nil {
p.snapshotPeers[key] = make(map[types.NodeID]types.NodeID)
}
p.snapshotPeers[key][peerID] = peerID
if p.peerIndex[peerID] == nil {
p.peerIndex[peerID] = make(map[snapshotKey]bool)
}
p.peerIndex[peerID][key] = true
if p.snapshots[key] != nil {
return false, nil
}
p.snapshots[key] = snapshot
if p.formatIndex[snapshot.Format] == nil {
p.formatIndex[snapshot.Format] = make(map[snapshotKey]bool)
}
p.formatIndex[snapshot.Format][key] = true
if p.heightIndex[snapshot.Height] == nil {
p.heightIndex[snapshot.Height] = make(map[snapshotKey]bool)
}
p.heightIndex[snapshot.Height][key] = true
return true, nil
}
// Best returns the "best" currently known snapshot, if any.
func (p *snapshotPool) Best() *snapshot {
ranked := p.Ranked()
if len(ranked) == 0 {
return nil
}
return ranked[0]
}
// GetPeer returns a random peer for a snapshot, if any.
func (p *snapshotPool) GetPeer(snapshot *snapshot) types.NodeID {
peers := p.GetPeers(snapshot)
if len(peers) == 0 {
return ""
}
return peers[rand.Intn(len(peers))] // nolint:gosec // G404: Use of weak random number generator
}
// GetPeers returns the peers for a snapshot.
func (p *snapshotPool) GetPeers(snapshot *snapshot) []types.NodeID {
key := snapshot.Key()
p.Lock()
defer p.Unlock()
peers := make([]types.NodeID, 0, len(p.snapshotPeers[key]))
for _, peer := range p.snapshotPeers[key] {
peers = append(peers, peer)
}
// sort results, for testability (otherwise order is random, so tests randomly fail)
sort.Slice(peers, func(a int, b int) bool {
return strings.Compare(string(peers[a]), string(peers[b])) < 0
})
return peers
}
// Ranked returns a list of snapshots ranked by preference. The current heuristic is very naïve,
// preferring the snapshot with the greatest height, then greatest format, then greatest number of
// peers. This can be improved quite a lot.
func (p *snapshotPool) Ranked() []*snapshot {
p.Lock()
defer p.Unlock()
if len(p.snapshots) == 0 {
return []*snapshot{}
}
numPeers := make([]int, 0, len(p.snapshots))
for key := range p.snapshots {
numPeers = append(numPeers, len(p.snapshotPeers[key]))
}
sort.Ints(numPeers)
median := len(numPeers) / 2
if len(numPeers)%2 == 0 {
median = (numPeers[median-1] + numPeers[median]) / 2
} else {
median = numPeers[median]
}
commonCandidates := make([]*snapshot, 0, len(p.snapshots)/2)
uncommonCandidates := make([]*snapshot, 0, len(p.snapshots)/2)
for key := range p.snapshots {
if len(p.snapshotPeers[key]) > median {
commonCandidates = append(commonCandidates, p.snapshots[key])
continue
}
uncommonCandidates = append(uncommonCandidates, p.snapshots[key])
}
sort.Slice(commonCandidates, p.sorterFactory(commonCandidates))
sort.Slice(uncommonCandidates, p.sorterFactory(uncommonCandidates))
return append(commonCandidates, uncommonCandidates...)
}
func (p *snapshotPool) sorterFactory(candidates []*snapshot) func(int, int) bool {
return func(i, j int) bool {
a := candidates[i]
b := candidates[j]
switch {
case a.Height > b.Height:
return true
case a.Height < b.Height:
return false
case len(p.snapshotPeers[a.Key()]) > len(p.snapshotPeers[b.Key()]):
return true
case a.Format > b.Format:
return true
case a.Format < b.Format:
return false
default:
return false
}
}
}
// Reject rejects a snapshot. Rejected snapshots will never be used again.
func (p *snapshotPool) Reject(snapshot *snapshot) {
key := snapshot.Key()
p.Lock()
defer p.Unlock()
p.snapshotBlacklist[key] = true
p.removeSnapshot(key)
}
// RejectFormat rejects a snapshot format. It will never be used again.
func (p *snapshotPool) RejectFormat(format uint32) {
p.Lock()
defer p.Unlock()
p.formatBlacklist[format] = true
for key := range p.formatIndex[format] {
p.removeSnapshot(key)
}
}
// RejectPeer rejects a peer. It will never be used again.
func (p *snapshotPool) RejectPeer(peerID types.NodeID) {
if len(peerID) == 0 {
return
}
p.Lock()
defer p.Unlock()
p.removePeer(peerID)
p.peerBlacklist[peerID] = true
}
// RemovePeer removes a peer from the pool, and any snapshots that no longer have peers.
func (p *snapshotPool) RemovePeer(peerID types.NodeID) {
p.Lock()
defer p.Unlock()
p.removePeer(peerID)
}
// removePeer removes a peer. The caller must hold the mutex lock.
func (p *snapshotPool) removePeer(peerID types.NodeID) {
for key := range p.peerIndex[peerID] {
delete(p.snapshotPeers[key], peerID)
if len(p.snapshotPeers[key]) == 0 {
p.removeSnapshot(key)
}
}
delete(p.peerIndex, peerID)
}
// removeSnapshot removes a snapshot. The caller must hold the mutex lock.
func (p *snapshotPool) removeSnapshot(key snapshotKey) {
snapshot := p.snapshots[key]
if snapshot == nil {
return
}
delete(p.snapshots, key)
delete(p.formatIndex[snapshot.Format], key)
delete(p.heightIndex[snapshot.Height], key)
for peerID := range p.snapshotPeers[key] {
delete(p.peerIndex[peerID], key)
}
delete(p.snapshotPeers, key)
}