-
Notifications
You must be signed in to change notification settings - Fork 202
/
bootstrapStorer.go
134 lines (108 loc) · 3.25 KB
/
bootstrapStorer.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
//go:generate protoc -I=proto -I=$GOPATH/src -I=$GOPATH/src/github.com/ElrondNetwork/protobuf/protobuf --gogoslick_out=. bootstrapData.proto
package bootstrapStorage
import (
"errors"
"strconv"
"sync/atomic"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/storage"
)
// ErrNilMarshalizer signals that an operation has been attempted to or with a nil Marshalizer implementation
var ErrNilMarshalizer = errors.New("nil Marshalizer")
// ErrNilBootStorer signals that an operation has been attempted to or with a nil storer implementation
var ErrNilBootStorer = errors.New("nil boot storer")
type bootstrapStorer struct {
store storage.Storer
marshalizer marshal.Marshalizer
lastRound int64
}
// NewBootstrapStorer will return an instance of bootstrap storer
func NewBootstrapStorer(
marshalizer marshal.Marshalizer,
store storage.Storer,
) (*bootstrapStorer, error) {
if check.IfNil(marshalizer) {
return nil, ErrNilMarshalizer
}
if check.IfNil(store) {
return nil, ErrNilBootStorer
}
bootStorer := &bootstrapStorer{
store: store,
marshalizer: marshalizer,
}
bootStorer.lastRound = bootStorer.GetHighestRound()
return bootStorer, nil
}
// Put will save bootData in storage
func (bs *bootstrapStorer) Put(round int64, bootData BootstrapData) error {
bootData.LastRound = atomic.LoadInt64(&bs.lastRound)
// save bootstrap round information
bootDataBytes, err := bs.marshalizer.Marshal(&bootData)
if err != nil {
return err
}
key := []byte(strconv.FormatInt(round, 10))
err = bs.store.Put(key, bootDataBytes)
if err != nil {
return err
}
// save round with a static key
roundBytes, err := bs.marshalizer.Marshal(&RoundNum{Num: round})
if err != nil {
return err
}
err = bs.store.Put([]byte(core.HighestRoundFromBootStorage), roundBytes)
if err != nil {
return err
}
atomic.StoreInt64(&bs.lastRound, round)
return nil
}
// Get will read data from storage
func (bs *bootstrapStorer) Get(round int64) (BootstrapData, error) {
key := []byte(strconv.FormatInt(round, 10))
bootstrapDataBytes, err := bs.store.Get(key)
if err != nil {
return BootstrapData{}, err
}
var bootData BootstrapData
err = bs.marshalizer.Unmarshal(&bootData, bootstrapDataBytes)
if err != nil {
return BootstrapData{}, err
}
return bootData, nil
}
// GetHighestRound will return highest round saved in storage
func (bs *bootstrapStorer) GetHighestRound() int64 {
roundBytes, err := bs.store.Get([]byte(core.HighestRoundFromBootStorage))
if err != nil {
return 0
}
var round RoundNum
err = bs.marshalizer.Unmarshal(&round, roundBytes)
if err != nil {
return 0
}
return round.Num
}
// SaveLastRound will save the last round
func (bs *bootstrapStorer) SaveLastRound(round int64) error {
atomic.StoreInt64(&bs.lastRound, round)
// save round with a static key
roundBytes, err := bs.marshalizer.Marshal(&RoundNum{Num: round})
if err != nil {
return err
}
err = bs.store.Put([]byte(core.HighestRoundFromBootStorage), roundBytes)
if err != nil {
return err
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (bs *bootstrapStorer) IsInterfaceNil() bool {
return bs == nil
}