-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
53 lines (45 loc) · 1.33 KB
/
util.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
package blockdata
import (
"github.com/pkg/errors"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/block"
"github.com/spikeekips/mitum/storage"
"github.com/spikeekips/mitum/util"
)
// Clean makes Database and Blockdata to be empty. If 'remove' is true, remove
// the Blockdata directory itself.
func Clean(db storage.Database, blockdata Blockdata, remove bool) error {
if err := db.Clean(); err != nil {
return err
} else if err := blockdata.Clean(remove); err != nil {
return err
} else {
return nil
}
}
func CleanByHeight(db storage.Database, blockdata Blockdata, height base.Height) error {
if err := db.LocalBlockdataMapsByHeight(height, func(bd block.BlockdataMap) (bool, error) {
if err := blockdata.RemoveAll(bd.Height()); err != nil {
if errors.Is(err, util.NotFoundError) {
return true, nil
}
return false, err
}
return true, nil
}); err != nil {
return err
}
return db.CleanByHeight(height)
}
func CheckBlock(db storage.Database, blockdata Blockdata, networkID base.NetworkID) (block.Manifest, error) {
m, err := storage.CheckBlock(db, networkID)
if err != nil {
return m, err
}
if found, err := blockdata.Exists(m.Height()); err != nil {
return m, err
} else if !found {
return m, util.NotFoundError.Errorf("block, %d not found in block data", m.Height())
}
return m, nil
}