3 import "github.com/bytom/vapor/toolbar/osssync/util"
5 // Interval determines the number of blocks in a Gzip file in the Interval of blockHeight
6 // StartBlockHeight is the start of the Interval
7 // EndBlockHeight: the end of the Interval
8 // GzSize is the number of blocks store in a Gzip file
10 StartBlockHeight uint64
15 // NewInterval creates a new Interval from info.json
16 func NewInterval(start, end, gzSize uint64) *Interval {
18 StartBlockHeight: start,
24 // Info is a struct for info.json
26 LatestBlockHeight uint64
30 // NewInfo creates a new Info for info.json
31 func NewInfo(end, gzSize uint64) *Info {
32 newInvl := NewInterval(0, end, gzSize)
34 arr = append(arr, newInvl)
38 // GetInfoJson Download info.json
39 func (s *Sync) GetInfoJson() (*Info, error) {
40 data, err := s.GetObjToData("info.json")
46 err = util.Json2Struct(data, &info)
51 func (s *Sync) PutInfoJson(infoData *Info) error {
52 jsonData, err := util.Struct2Json(infoData)
58 return s.PutObjByteArr("info.json", jsonData)
61 // SetLatestBlockHeight set new latest blockHeight on OSS
62 func (s *Sync) SetLatestBlockHeight(newLatestBlockHeight uint64) error {
63 info, err := s.GetInfoJson()
68 info.LatestBlockHeight = newLatestBlockHeight
69 return s.PutInfoJson(info)
72 // AddInterval adds an interval to the end of info.json
73 func (s *Sync) AddInterval(end, gzSize uint64) error {
74 isJsonExist, err := s.OssBucket.IsObjectExist("info.json")
82 info, err = s.GetInfoJson()
88 prevInvl := info.Interval[len(info.Interval)-1]
89 newInvl := NewInterval(prevInvl.EndBlockHeight+1, end, gzSize)
90 info.Interval = append(info.Interval, newInvl)
92 info = NewInfo(end, gzSize)
94 return s.PutInfoJson(info)