-
Notifications
You must be signed in to change notification settings - Fork 151
/
snapshot.go
199 lines (162 loc) · 6.37 KB
/
snapshot.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
package storage
import (
"fmt"
"time"
"github.com/iotaledger/hive.go/serializer/v2"
iotago "github.com/iotaledger/iota.go/v3"
)
type SnapshotInfo struct {
// The index of the genesis milestone of the network.
genesisMilestoneIndex iotago.MilestoneIndex
// The index of the snapshot file.
snapshotIndex iotago.MilestoneIndex
// The index of the milestone of which the SEPs within the database are from.
entryPointIndex iotago.MilestoneIndex
// The index of the milestone before which the tangle history is pruned.
// This is not the same as EntryPointIndex, so we can cleanly prune again even if the pruning was aborted last time.
pruningIndex iotago.MilestoneIndex
// The timestamp of the target milestone of the snapshot.
snapshotTimestamp time.Time
}
// GenesisMilestoneIndex returns the index of the genesis milestone of the network.
func (i *SnapshotInfo) GenesisMilestoneIndex() iotago.MilestoneIndex {
return i.genesisMilestoneIndex
}
// SnapshotIndex returns the index of the snapshot file.
func (i *SnapshotInfo) SnapshotIndex() iotago.MilestoneIndex {
return i.snapshotIndex
}
// EntryPointIndex returns the index of the milestone of which the SEPs within the database are from.
func (i *SnapshotInfo) EntryPointIndex() iotago.MilestoneIndex {
return i.entryPointIndex
}
// PruningIndex returns the index of the milestone before which the tangle history is pruned.
// This is not the same as EntryPointIndex, so we can cleanly prune again even if the pruning was aborted last time.
func (i *SnapshotInfo) PruningIndex() iotago.MilestoneIndex {
return i.pruningIndex
}
// SnapshotTimestamp returns the timestamp of the target milestone of the snapshot.
func (i *SnapshotInfo) SnapshotTimestamp() time.Time {
return i.snapshotTimestamp
}
func (i *SnapshotInfo) Deserialize(data []byte, _ serializer.DeSerializationMode, _ interface{}) (int, error) {
var (
genesisMilestoneIndex iotago.MilestoneIndex
snapshotIndex iotago.MilestoneIndex
entryPointIndex iotago.MilestoneIndex
pruningIndex iotago.MilestoneIndex
snapshotTimestamp iotago.MilestoneIndex
)
offset, err := serializer.NewDeserializer(data).
ReadNum(&genesisMilestoneIndex, func(err error) error {
return fmt.Errorf("unable to deserialize genesis milestone index: %w", err)
}).
ReadNum(&snapshotIndex, func(err error) error {
return fmt.Errorf("unable to deserialize snapshot index: %w", err)
}).
ReadNum(&entryPointIndex, func(err error) error {
return fmt.Errorf("unable to deserialize entry point index: %w", err)
}).
ReadNum(&pruningIndex, func(err error) error {
return fmt.Errorf("unable to deserialize pruning index: %w", err)
}).
ReadNum(&snapshotTimestamp, func(err error) error {
return fmt.Errorf("unable to deserialize snapshot timestamp: %w", err)
}).
Done()
if err != nil {
return offset, err
}
i.genesisMilestoneIndex = genesisMilestoneIndex
i.snapshotIndex = snapshotIndex
i.entryPointIndex = entryPointIndex
i.pruningIndex = pruningIndex
i.snapshotTimestamp = time.Unix(int64(snapshotTimestamp), 0)
return offset, nil
}
func (i *SnapshotInfo) Serialize(_ serializer.DeSerializationMode, _ interface{}) ([]byte, error) {
return serializer.NewSerializer().
WriteNum(i.genesisMilestoneIndex, func(err error) error {
return fmt.Errorf("unable to serialize genesis milestone index: %w", err)
}).
WriteNum(i.snapshotIndex, func(err error) error {
return fmt.Errorf("unable to serialize snapshot index: %w", err)
}).
WriteNum(i.entryPointIndex, func(err error) error {
return fmt.Errorf("unable to serialize entry point index: %w", err)
}).
WriteNum(i.pruningIndex, func(err error) error {
return fmt.Errorf("unable to serialize pruning index: %w", err)
}).
WriteNum(uint32(i.snapshotTimestamp.Unix()), func(err error) error {
return fmt.Errorf("unable to serialize timestamp: %w", err)
}).
Serialize()
}
func (s *Storage) loadSnapshotInfo() error {
s.snapshotMutex.Lock()
defer s.snapshotMutex.Unlock()
info, err := s.readSnapshotInfo()
if err != nil {
return err
}
s.snapshot = info
return nil
}
func (s *Storage) PrintSnapshotInfo() {
s.snapshotMutex.RLock()
defer s.snapshotMutex.RUnlock()
if s.snapshot != nil {
println(fmt.Sprintf(`SnapshotInfo:
GenesisMilestoneIndex: %d
SnapshotIndex: %d
EntryPointIndex: %d
PruningIndex: %d
Timestamp: %v`, s.snapshot.genesisMilestoneIndex, s.snapshot.snapshotIndex, s.snapshot.entryPointIndex, s.snapshot.pruningIndex, s.snapshot.snapshotTimestamp.Truncate(time.Second)))
}
}
func (s *Storage) SetInitialSnapshotInfo(genesisMilestoneIndex iotago.MilestoneIndex, snapshotIndex iotago.MilestoneIndex, entryPointIndex iotago.MilestoneIndex, pruningIndex iotago.MilestoneIndex, snapshotTimestamp time.Time) error {
s.snapshotMutex.Lock()
defer s.snapshotMutex.Unlock()
s.snapshot = &SnapshotInfo{
genesisMilestoneIndex: genesisMilestoneIndex,
snapshotIndex: snapshotIndex,
entryPointIndex: entryPointIndex,
pruningIndex: pruningIndex,
snapshotTimestamp: snapshotTimestamp,
}
return s.storeSnapshotInfo(s.snapshot)
}
func (s *Storage) UpdateSnapshotInfo(snapshotIndex iotago.MilestoneIndex, entryPointIndex iotago.MilestoneIndex, pruningIndex iotago.MilestoneIndex, snapshotTimestamp time.Time) error {
s.snapshotMutex.Lock()
defer s.snapshotMutex.Unlock()
s.snapshot.snapshotIndex = snapshotIndex
s.snapshot.entryPointIndex = entryPointIndex
s.snapshot.pruningIndex = pruningIndex
s.snapshot.snapshotTimestamp = snapshotTimestamp
return s.storeSnapshotInfo(s.snapshot)
}
func (s *Storage) SetSnapshotIndex(snapshotIndex iotago.MilestoneIndex, snapshotTimestamp time.Time) error {
s.snapshotMutex.Lock()
defer s.snapshotMutex.Unlock()
s.snapshot.snapshotIndex = snapshotIndex
s.snapshot.snapshotTimestamp = snapshotTimestamp
return s.storeSnapshotInfo(s.snapshot)
}
func (s *Storage) SetEntryPointIndex(entryPointIndex iotago.MilestoneIndex) error {
s.snapshotMutex.Lock()
defer s.snapshotMutex.Unlock()
s.snapshot.entryPointIndex = entryPointIndex
return s.storeSnapshotInfo(s.snapshot)
}
func (s *Storage) SetPruningIndex(pruningIndex iotago.MilestoneIndex) error {
s.snapshotMutex.Lock()
defer s.snapshotMutex.Unlock()
s.snapshot.pruningIndex = pruningIndex
return s.storeSnapshotInfo(s.snapshot)
}
func (s *Storage) SnapshotInfo() *SnapshotInfo {
s.snapshotMutex.RLock()
defer s.snapshotMutex.RUnlock()
return s.snapshot
}