-
Notifications
You must be signed in to change notification settings - Fork 202
/
bootstrapStorer.go
169 lines (137 loc) · 4.12 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package bootstrapStorage
import (
"errors"
"strconv"
"sync/atomic"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/storage"
)
// HighestRoundFromBootStorage is the key for the highest round that is saved in storage
const highestRoundFromBootStorage = "highestRoundFromBootStorage"
// 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")
//MiniBlocksInMeta is used to store all mini blocks hashes for a metablock hash
type MiniBlocksInMeta struct {
MetaHash []byte
MiniBlocksHashes [][]byte
}
//BootstrapHeaderInfo is used to store information about a header
type BootstrapHeaderInfo struct {
ShardId uint32
Nonce uint64
Hash []byte
}
// BootstrapData is used to store information that are needed for bootstrap
type BootstrapData struct {
LastHeader BootstrapHeaderInfo
LastCrossNotarizedHeaders []BootstrapHeaderInfo
LastSelfNotarizedHeaders []BootstrapHeaderInfo
ProcessedMiniBlocks []MiniBlocksInMeta
HighestFinalBlockNonce uint64
LastRound int64
}
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(&round)
if err != nil {
return err
}
err = bs.store.Put([]byte(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(highestRoundFromBootStorage))
if err != nil {
return 0
}
var round int64
err = bs.marshalizer.Unmarshal(&round, roundBytes)
if err != nil {
return 0
}
return round
}
// 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(&round)
if err != nil {
return err
}
err = bs.store.Put([]byte(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
}
// TrimHeaderInfoSlice creates a copy of the provided slice without the excess capacity
func TrimHeaderInfoSlice(in []BootstrapHeaderInfo) []BootstrapHeaderInfo {
if len(in) == 0 {
return []BootstrapHeaderInfo{}
}
ret := make([]BootstrapHeaderInfo, len(in))
copy(ret, in)
return ret
}