-
Notifications
You must be signed in to change notification settings - Fork 199
/
storageEpochStartMetaBlockProcessor.go
152 lines (129 loc) · 4.71 KB
/
storageEpochStartMetaBlockProcessor.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
package bootstrap
import (
"context"
"math"
"sync"
"time"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/process"
)
var _ process.InterceptorProcessor = (*epochStartMetaBlockProcessor)(nil)
type storageEpochStartMetaBlockProcessor struct {
messenger Messenger
requestHandler RequestHandler
marshalizer marshal.Marshalizer
hasher hashing.Hasher
chanReceived chan struct{}
mutMetablock sync.Mutex
metaBlock data.MetaHeaderHandler
}
// NewStorageEpochStartMetaBlockProcessor will return an interceptor processor for epoch start meta block when importing
// data from storage
func NewStorageEpochStartMetaBlockProcessor(
messenger Messenger,
handler RequestHandler,
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
) (*storageEpochStartMetaBlockProcessor, error) {
if check.IfNil(messenger) {
return nil, epochStart.ErrNilMessenger
}
if check.IfNil(handler) {
return nil, epochStart.ErrNilRequestHandler
}
if check.IfNil(marshalizer) {
return nil, epochStart.ErrNilMarshalizer
}
if check.IfNil(hasher) {
return nil, epochStart.ErrNilHasher
}
processor := &storageEpochStartMetaBlockProcessor{
messenger: messenger,
requestHandler: handler,
marshalizer: marshalizer,
hasher: hasher,
chanReceived: make(chan struct{}, 1),
}
return processor, nil
}
// Validate will return nil as there is no need for validation
func (ses *storageEpochStartMetaBlockProcessor) Validate(_ process.InterceptedData, _ core.PeerID) error {
return nil
}
// Save will handle the consensus mechanism for the fetched metablocks
// All errors are just logged because if this function returns an error, the processing is finished. This way, we ignore
// wrong received data and wait for relevant intercepted data
func (ses *storageEpochStartMetaBlockProcessor) Save(data process.InterceptedData, _ core.PeerID, _ string) error {
if check.IfNil(data) {
log.Debug("epoch bootstrapper: nil intercepted data")
return nil
}
log.Debug("received header", "type", data.Type(), "hash", data.Hash())
interceptedHdr, ok := data.(process.HdrValidatorHandler)
if !ok {
log.Warn("saving epoch start meta block error", "error", epochStart.ErrWrongTypeAssertion)
return nil
}
metaBlock, ok := interceptedHdr.HeaderHandler().(*block.MetaBlock)
if !ok {
log.Warn("saving epoch start meta block error", "error", epochStart.ErrWrongTypeAssertion,
"header", interceptedHdr.HeaderHandler())
return nil
}
if !metaBlock.IsStartOfEpochBlock() {
log.Warn("received metablock is not of type epoch start", "error", epochStart.ErrNotEpochStartBlock)
return nil
}
log.Debug("received epoch start meta", "epoch", metaBlock.GetEpoch(), "from peer", "self")
ses.mutMetablock.Lock()
ses.metaBlock = metaBlock
ses.mutMetablock.Unlock()
select {
case ses.chanReceived <- struct{}{}:
default:
}
return nil
}
// GetEpochStartMetaBlock will return the metablock after it is confirmed or an error if the number of tries was exceeded
// This is a blocking method which will end after the consensus for the meta block is obtained or the context is done
func (ses *storageEpochStartMetaBlockProcessor) GetEpochStartMetaBlock(ctx context.Context) (data.MetaHeaderHandler, error) {
ses.requestMetaBlock()
chanRequests := time.After(durationBetweenReRequests)
for {
select {
case <-ses.chanReceived:
return ses.getMetablock()
case <-ctx.Done():
return ses.getMetablock()
case <-chanRequests:
ses.requestMetaBlock()
chanRequests = time.After(durationBetweenReRequests)
}
}
}
func (ses *storageEpochStartMetaBlockProcessor) getMetablock() (data.MetaHeaderHandler, error) {
ses.mutMetablock.Lock()
defer ses.mutMetablock.Unlock()
if check.IfNil(ses.metaBlock) {
return nil, process.ErrNilMetaBlockHeader
}
return ses.metaBlock, nil
}
func (ses *storageEpochStartMetaBlockProcessor) requestMetaBlock() {
unknownEpoch := uint32(math.MaxUint32)
ses.requestHandler.RequestStartOfEpochMetaBlock(unknownEpoch)
}
// RegisterHandler registers a callback function to be notified of incoming epoch start metablocks
func (ses *storageEpochStartMetaBlockProcessor) RegisterHandler(_ func(topic string, hash []byte, data interface{})) {
log.Error("storageEpochStartMetaBlockProcessor.RegisterHandler not implemented")
}
// IsInterfaceNil returns true if there is no value under the interface
func (ses *storageEpochStartMetaBlockProcessor) IsInterfaceNil() bool {
return ses == nil
}