Skip to content

Commit e711e81

Browse files
committed
Disable generation of snapshots v1
1 parent 5a08619 commit e711e81

File tree

14 files changed

+521
-828
lines changed

14 files changed

+521
-828
lines changed

blockchain/blockchain.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (chain *Blockchain) loadPredefinedGenesis(network types.Network) (*types.Bl
257257
}
258258
defer stateDbData.Close()
259259

260-
if err = chain.appState.State.RecoverSnapshot(header.Height(), header.Root(), stateDbData); err != nil {
260+
if err = chain.appState.State.RecoverSnapshot2(header.Height(), header.Root(), stateDbData); err != nil {
261261
return nil, err
262262
}
263263

@@ -269,7 +269,7 @@ func (chain *Blockchain) loadPredefinedGenesis(network types.Network) (*types.Bl
269269
}
270270
defer identityStateDbData.Close()
271271

272-
if err = chain.appState.IdentityState.RecoverSnapshot(header.Height(), header.IdentityRoot(), identityStateDbData); err != nil {
272+
if err = chain.appState.IdentityState.RecoverSnapshot2(header.Height(), header.IdentityRoot(), identityStateDbData); err != nil {
273273
return nil, err
274274
}
275275

@@ -2384,12 +2384,11 @@ func (chain *Blockchain) GetIdentityDiff(height uint64) *state.IdentityStateDiff
23842384
}
23852385

23862386
func (chain *Blockchain) ReadSnapshotManifest() *snapshot.Manifest {
2387-
cid, cidV2, root, height, _ := chain.repo.LastSnapshotManifest()
2388-
if cid == nil {
2387+
cidV2, root, height, _ := chain.repo.LastSnapshotManifest()
2388+
if cidV2 == nil {
23892389
return nil
23902390
}
23912391
return &snapshot.Manifest{
2392-
Cid: cid,
23932392
CidV2: cidV2,
23942393
Root: root,
23952394
Height: height,

cmd/genext/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func main() {
8080
return err
8181
}
8282

83-
stateRoot, err := state.WriteTreeTo(stateDb, genesis, file)
83+
stateRoot, err := state.WriteTreeTo2(stateDb, genesis, file)
8484
file.Close()
8585
if err != nil {
8686
return err
@@ -91,7 +91,7 @@ func main() {
9191
return err
9292
}
9393

94-
identityRoot, err := state.WriteTreeTo(identityStateDb, genesis, file)
94+
identityRoot, err := state.WriteTreeTo2(identityStateDb, genesis, file)
9595
file.Close()
9696
if err != nil {
9797
return err

core/state/identity_statedb.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,9 @@ func (s *IdentityStateDB) SetPredefinedIdentities(state *models.ProtoPredefinedS
448448
}
449449
}
450450

451-
func (s *IdentityStateDB) RecoverSnapshot(height uint64, treeRoot common.Hash, from io.Reader) error {
451+
func (s *IdentityStateDB) RecoverSnapshot2(height uint64, treeRoot common.Hash, from io.Reader) error {
452452
pdb := dbm.NewPrefixDB(s.original, IdentityStateDbKeys.buildDbPrefix(height))
453-
return ReadTreeFrom(pdb, height, treeRoot, from)
453+
return ReadTreeFrom2(pdb, height, treeRoot, from)
454454
}
455455

456456
func (s *IdentityStateDB) CommitSnapshot(height uint64) (dropDb dbm.DB) {

core/state/manager.go

+15-40
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const (
2727

2828
type SnapshotVersion byte
2929

30-
const SnapshotVersionV1 = SnapshotVersion(1)
3130
const SnapshotVersionV2 = SnapshotVersion(2)
3231

3332
var (
@@ -95,19 +94,10 @@ func (m *SnapshotManager) createShapshotForVersion(height uint64, version Snapsh
9594
return nil, common.Hash{}, ""
9695
}
9796

98-
switch version {
99-
case SnapshotVersionV1:
100-
if root, err = m.state.WriteSnapshot(height, file); err != nil {
101-
file.Close()
102-
m.log.Error("Cannot write snapshot to file", "err", err)
103-
return nil, common.Hash{}, ""
104-
}
105-
case SnapshotVersionV2:
106-
if root, err = m.state.WriteSnapshot2(height, file); err != nil {
107-
file.Close()
108-
m.log.Error("Cannot write snapshot to file", "err", err)
109-
return nil, common.Hash{}, ""
110-
}
97+
if root, err = m.state.WriteSnapshot2(height, file); err != nil {
98+
file.Close()
99+
m.log.Error("Cannot write snapshot to file", "err", err)
100+
return nil, common.Hash{}, ""
111101
}
112102

113103
file.Close()
@@ -132,19 +122,16 @@ func (m *SnapshotManager) createShapshotForVersion(height uint64, version Snapsh
132122

133123
func (m *SnapshotManager) createSnapshot(height uint64) (root common.Hash) {
134124

135-
cidV1, root, filePath := m.createShapshotForVersion(height, SnapshotVersionV1)
136-
cidV2, _, filePath2 := m.createShapshotForVersion(height, SnapshotVersionV2)
137-
138-
if cidV1 != nil && cidV2 != nil {
139-
m.clearFs([]string{filePath, filePath2})
140-
m.writeLastManifest(cidV1, cidV2, root, height, filePath, filePath2)
125+
cidV2, _, filePath := m.createShapshotForVersion(height, SnapshotVersionV2)
126+
if cidV2 != nil {
127+
m.clearFs([]string{filePath})
128+
m.writeLastManifest(cidV2, root, height, filePath)
141129
}
142130
return root
143131
}
144132

145133
func (m *SnapshotManager) clearFs(excludedFiles []string) {
146-
if prevCid, prevCidV2, _, _, _ := m.repo.LastSnapshotManifest(); prevCid != nil {
147-
m.ipfs.Unpin(prevCid)
134+
if prevCidV2, _, _, _ := m.repo.LastSnapshotManifest(); prevCidV2 != nil {
148135
m.ipfs.Unpin(prevCidV2)
149136
}
150137
m.clearSnapshotFolder(excludedFiles)
@@ -182,16 +169,12 @@ func (m *SnapshotManager) clearSnapshotFolder(excludedFiles []string) {
182169
}
183170
}
184171

185-
func (m *SnapshotManager) writeLastManifest(snapshotCid []byte, snapshotCidV2 []byte, root common.Hash, height uint64, file string, fileV2 string) {
186-
m.repo.WriteLastSnapshotManifest(snapshotCid, snapshotCidV2, root, height, file, fileV2)
172+
func (m *SnapshotManager) writeLastManifest(snapshotCidV2 []byte, root common.Hash, height uint64, fileV2 string) {
173+
m.repo.WriteLastSnapshotManifest(snapshotCidV2, root, height, fileV2)
187174
}
188175

189176
func (m *SnapshotManager) DownloadSnapshot(snapshot *snapshot.Manifest) (filePath string, version SnapshotVersion, err error) {
190177
version = SnapshotVersionV2
191-
if len(snapshot.CidV2) == 0 {
192-
version = SnapshotVersionV1
193-
}
194-
195178
filePath, file, err := createSnapshotFile(m.cfg.DataDir, snapshot.Height, version)
196179
if err != nil {
197180
return "", 0, err
@@ -219,13 +202,7 @@ func (m *SnapshotManager) DownloadSnapshot(snapshot *snapshot.Manifest) (filePat
219202
done := make(chan struct{})
220203

221204
go func() {
222-
switch version {
223-
case SnapshotVersionV1:
224-
loadToErr = m.ipfs.LoadTo(snapshot.Cid, file, ctx, onLoading)
225-
case SnapshotVersionV2:
226-
loadToErr = m.ipfs.LoadTo(snapshot.CidV2, file, ctx, onLoading)
227-
}
228-
205+
loadToErr = m.ipfs.LoadTo(snapshot.CidV2, file, ctx, onLoading)
229206
wg.Done()
230207
close(done)
231208
}()
@@ -252,14 +229,12 @@ func (m *SnapshotManager) DownloadSnapshot(snapshot *snapshot.Manifest) (filePat
252229

253230
if loadToErr == nil {
254231
m.clearFs([]string{filePath})
255-
var filePath1, filePath2 string
256-
if version == SnapshotVersionV1 {
257-
filePath1 = filePath
258-
}
232+
var filePath2 string
233+
259234
if version == SnapshotVersionV2 {
260235
filePath2 = filePath
261236
}
262-
m.writeLastManifest(snapshot.Cid, snapshot.CidV2, snapshot.Root, snapshot.Height, filePath1, filePath2)
237+
m.writeLastManifest(snapshot.CidV2, snapshot.Root, snapshot.Height, filePath2)
263238
}
264239

265240
return filePath, version, loadToErr

core/state/snapshot/snapshot.go

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type Manifest struct {
1515

1616
func (m *Manifest) ToBytes() ([]byte, error) {
1717
protoObj := &models.ProtoManifest{
18-
Cid: m.Cid,
1918
Height: m.Height,
2019
Root: m.Root[:],
2120
CidV2: m.CidV2,
@@ -30,7 +29,6 @@ func (m *Manifest) FromBytes(data []byte) error {
3029
}
3130
m.Root = common.BytesToHash(protoObj.Root)
3231
m.Height = protoObj.Height
33-
m.Cid = protoObj.Cid
3432
m.CidV2 = protoObj.CidV2
3533
return nil
3634
}

core/state/statedb.go

-10
Original file line numberDiff line numberDiff line change
@@ -1268,16 +1268,6 @@ func (s *StateDB) RecoverSnapshot2(height uint64, treeRoot common.Hash, from io.
12681268
return ReadTreeFrom2(pdb, height, treeRoot, from)
12691269
}
12701270

1271-
1272-
func (s *StateDB) WriteSnapshot(height uint64, to io.Writer) (root common.Hash, err error) {
1273-
return WriteTreeTo(s.db, height, to)
1274-
}
1275-
1276-
func (s *StateDB) RecoverSnapshot(height uint64, treeRoot common.Hash, from io.Reader) error {
1277-
pdb := dbm.NewPrefixDB(s.original, StateDbKeys.BuildDbPrefix(height))
1278-
return ReadTreeFrom(pdb, height, treeRoot, from)
1279-
}
1280-
12811271
func (s *StateDB) CommitSnapshot(height uint64, batch dbm.Batch) (dropDb dbm.DB) {
12821272
pdb := dbm.NewPrefixDB(s.original, StateDbKeys.BuildDbPrefix(height))
12831273

core/state/statedb_test.go

-128
Original file line numberDiff line numberDiff line change
@@ -248,134 +248,6 @@ func TestStateGlobal_EmptyBlocksRatio(t *testing.T) {
248248
require.Len(t, stateDb.GetOrNewGlobalObject().data.EmptyBlocksBits.Bytes(), 4)
249249
}
250250

251-
func TestStateDB_WriteSnapshot(t *testing.T) {
252-
db := db.NewMemDB()
253-
stateDb, _ := NewLazy(db)
254-
source := rand.NewSource(1)
255-
rnd := rand.New(source)
256-
for i := 0; i < 300; i++ {
257-
key, _ := crypto.GenerateKeyFromSeed(rnd)
258-
addr := crypto.PubkeyToAddress(key.PublicKey)
259-
stateDb.SetBalance(addr, common.DnaBase)
260-
stateDb.SetState(addr, Human)
261-
}
262-
263-
repo := database.NewRepo(db)
264-
repo.WriteFinalConsensus(common.Hash{0x1})
265-
266-
stateDb.AddInvite(common.Address{}, 1)
267-
stateDb.AddInvite(common.Address{0x1}, 1)
268-
269-
stateDb.Commit(true)
270-
271-
buffer := new(bytes.Buffer)
272-
buffer2 := new(bytes.Buffer)
273-
stateDb.WriteSnapshot(1, buffer)
274-
275-
repo.WriteFinalConsensus(common.Hash{0x2})
276-
repo.WriteCanonicalHash(1, common.Hash{0x2})
277-
278-
repo.WriteBlockHeader(&types.Header{
279-
ProposedHeader: &types.ProposedHeader{
280-
Height: 1,
281-
Flags: types.ValidationFinished,
282-
Upgrade: 1,
283-
IpfsHash: []byte{1, 2, 3, 4},
284-
},
285-
})
286-
time.Sleep(time.Second * 2)
287-
stateDb.WriteSnapshot(1, buffer2)
288-
289-
require.True(t, buffer.Len() > 1000)
290-
require.Equal(t, buffer.Bytes(), buffer2.Bytes())
291-
}
292-
293-
func TestStateDB_RecoverSnapshot(t *testing.T) {
294-
//arrange
295-
database := db.NewMemDB()
296-
stateDb, _ := NewLazy(database)
297-
298-
prevStateDb := stateDb.db
299-
300-
identity := common.Address{}
301-
stateDb.AddInvite(identity, 1)
302-
303-
stateDb.Commit(true)
304-
const AddrsCount = 50000
305-
const Height = uint64(2)
306-
307-
for i := 0; i < AddrsCount; i++ {
308-
addr := common.Address{}
309-
addr.SetBytes(common.ToBytes(uint64(i)))
310-
stateDb.SetNonce(addr, uint32(i+1))
311-
}
312-
313-
stateDb.Commit(true)
314-
315-
var keys [][]byte
316-
var values [][]byte
317-
318-
stateDb.IterateAccounts(func(key []byte, value []byte) bool {
319-
keys = append(keys, key)
320-
values = append(values, value)
321-
return false
322-
})
323-
324-
require.Equal(t, AddrsCount, len(keys))
325-
326-
expectedRoot := stateDb.Root()
327-
stateDb.AddInvite(common.Address{}, 2)
328-
329-
stateDb.Commit(true)
330-
stateDb, _ = NewLazy(database)
331-
stateDb.Load(3)
332-
stateDb.tree.Hash()
333-
334-
//act
335-
336-
buffer := new(bytes.Buffer)
337-
stateDb.WriteSnapshot(Height, buffer)
338-
require.True(t, buffer.Len() > 0)
339-
340-
require.Nil(t, stateDb.RecoverSnapshot(Height, expectedRoot, buffer))
341-
342-
batch := stateDb.original.NewBatch()
343-
344-
dropDb := stateDb.CommitSnapshot(Height, batch)
345-
common.ClearDb(dropDb)
346-
batch.WriteSync()
347-
//assert
348-
349-
require.Equal(t, int64(Height), stateDb.tree.Version())
350-
require.Equal(t, expectedRoot, stateDb.Root())
351-
352-
i := 0
353-
stateDb.IterateAccounts(func(key []byte, value []byte) bool {
354-
355-
require.Equal(t, keys[i], key)
356-
require.Equal(t, values[i], value)
357-
i++
358-
return false
359-
})
360-
361-
require.Equal(t, AddrsCount, i)
362-
363-
cnt := 0
364-
365-
stateDb.IterateIdentities(func(key []byte, value []byte) bool {
366-
addr := common.Address{}
367-
addr.SetBytes(key[1:])
368-
require.Equal(t, addr, identity)
369-
cnt++
370-
return false
371-
})
372-
require.Equal(t, 1, cnt)
373-
374-
it, _ := prevStateDb.Iterator(nil, nil)
375-
defer it.Close()
376-
require.False(t, it.Valid())
377-
}
378-
379251
func TestStateDB_RecoverSnapshot2(t *testing.T) {
380252
//arrange
381253
database := db.NewMemDB()

0 commit comments

Comments
 (0)