-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_regulate_dir.go
53 lines (44 loc) · 1.24 KB
/
msg_regulate_dir.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 msgqueue
import (
"encoding/json"
"fmt"
"github.com/spf13/viper"
cmn "github.com/tendermint/tendermint/libs/common"
)
const FILEHEIGHT = 10000
type NewHeightInfo struct {
ChainID string `json:"chain_id"`
Height int64 `json:"height"`
TimeStamp int64 `json:"timestamp"`
LastBlockHash cmn.HexBytes `json:"last_block_hash"`
}
type Worker interface {
Work()
}
type RegulateWriteDir struct {
MsgWriter
initChainHeight int64
}
func NewRegulateWriteDir(dir string) (*RegulateWriteDir, error) {
var (
err error
rgw = &RegulateWriteDir{initChainHeight: viper.GetInt64("genesis_block_height")}
)
if rgw.MsgWriter, err = NewDirMsgWriter(dir, getFilePathAndIndex); err != nil {
return nil, err
}
rgw.MsgWriter.(*dirMsgWriter).SetTimeToNewFile(rgw.timeToNewFile())
return rgw, nil
}
func (r *RegulateWriteDir) timeToNewFile() func(k, v []byte) bool {
return func(k, v []byte) bool {
if string(k) == ("height_info") {
var info NewHeightInfo
if err := json.Unmarshal(v, &info); err != nil {
panic(fmt.Sprintf("json unmarshal height_info failed; err: %s\n", err.Error()))
}
return info.Height != r.initChainHeight+1 && ((info.Height-r.initChainHeight-1)%FILEHEIGHT) == 0
}
return false
}
}