-
Notifications
You must be signed in to change notification settings - Fork 199
/
interceptorsContainerFactory.go
185 lines (161 loc) · 5.4 KB
/
interceptorsContainerFactory.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package metachain
import (
"github.com/ElrondNetwork/elrond-go/core/statistics"
"github.com/ElrondNetwork/elrond-go/crypto"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/block/interceptors"
"github.com/ElrondNetwork/elrond-go/process/factory"
"github.com/ElrondNetwork/elrond-go/process/factory/containers"
"github.com/ElrondNetwork/elrond-go/process/metablock"
"github.com/ElrondNetwork/elrond-go/sharding"
)
type interceptorsContainerFactory struct {
marshalizer marshal.Marshalizer
hasher hashing.Hasher
store dataRetriever.StorageService
dataPool dataRetriever.MetaPoolsHolder
shardCoordinator sharding.Coordinator
messenger process.TopicHandler
multiSigner crypto.MultiSigner
chronologyValidator process.ChronologyValidator
tpsBenchmark *statistics.TpsBenchmark
}
// NewInterceptorsContainerFactory is responsible for creating a new interceptors factory object
func NewInterceptorsContainerFactory(
shardCoordinator sharding.Coordinator,
messenger process.TopicHandler,
store dataRetriever.StorageService,
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
multiSigner crypto.MultiSigner,
dataPool dataRetriever.MetaPoolsHolder,
chronologyValidator process.ChronologyValidator,
) (*interceptorsContainerFactory, error) {
if shardCoordinator == nil {
return nil, process.ErrNilShardCoordinator
}
if messenger == nil {
return nil, process.ErrNilMessenger
}
if store == nil {
return nil, process.ErrNilStore
}
if marshalizer == nil {
return nil, process.ErrNilMarshalizer
}
if hasher == nil {
return nil, process.ErrNilHasher
}
if multiSigner == nil {
return nil, process.ErrNilMultiSigVerifier
}
if dataPool == nil {
return nil, process.ErrNilDataPoolHolder
}
if chronologyValidator == nil {
return nil, process.ErrNilChronologyValidator
}
return &interceptorsContainerFactory{
shardCoordinator: shardCoordinator,
messenger: messenger,
store: store,
marshalizer: marshalizer,
hasher: hasher,
multiSigner: multiSigner,
dataPool: dataPool,
chronologyValidator: chronologyValidator,
}, nil
}
// Create returns an interceptor container that will hold all interceptors in the system
func (icf *interceptorsContainerFactory) Create() (process.InterceptorsContainer, error) {
container := containers.NewInterceptorsContainer()
keys, interceptorSlice, err := icf.generateMetablockInterceptor()
if err != nil {
return nil, err
}
err = container.AddMultiple(keys, interceptorSlice)
if err != nil {
return nil, err
}
keys, interceptorSlice, err = icf.generateShardHeaderInterceptors()
if err != nil {
return nil, err
}
err = container.AddMultiple(keys, interceptorSlice)
if err != nil {
return nil, err
}
return container, nil
}
func (icf *interceptorsContainerFactory) createTopicAndAssignHandler(
topic string,
interceptor process.Interceptor,
createChannel bool,
) (process.Interceptor, error) {
err := icf.messenger.CreateTopic(topic, createChannel)
if err != nil {
return nil, err
}
return interceptor, icf.messenger.RegisterMessageProcessor(topic, interceptor)
}
//------- Metablock interceptor
func (icf *interceptorsContainerFactory) generateMetablockInterceptor() ([]string, []process.Interceptor, error) {
identifierHdr := factory.MetachainBlocksTopic
metachainHeaderStorer := icf.store.GetStorer(dataRetriever.MetaBlockUnit)
interceptor, err := interceptors.NewMetachainHeaderInterceptor(
icf.marshalizer,
icf.dataPool.MetaChainBlocks(),
icf.dataPool.HeadersNonces(),
metachainHeaderStorer,
icf.multiSigner,
icf.hasher,
icf.shardCoordinator,
icf.chronologyValidator,
)
if err != nil {
return nil, nil, err
}
_, err = icf.createTopicAndAssignHandler(identifierHdr, interceptor, true)
if err != nil {
return nil, nil, err
}
return []string{identifierHdr}, []process.Interceptor{interceptor}, nil
}
//------- Shard header interceptors
func (icf *interceptorsContainerFactory) generateShardHeaderInterceptors() ([]string, []process.Interceptor, error) {
shardC := icf.shardCoordinator
noOfShards := shardC.NumberOfShards()
keys := make([]string, noOfShards)
interceptorSlice := make([]process.Interceptor, noOfShards)
//wire up to topics: shardHeadersForMetachain_0_META, shardHeadersForMetachain_1_META ...
for idx := uint32(0); idx < noOfShards; idx++ {
identifierHeader := factory.ShardHeadersForMetachainTopic + shardC.CommunicationIdentifier(idx)
interceptor, err := icf.createOneShardHeaderInterceptor(identifierHeader)
if err != nil {
return nil, nil, err
}
keys[int(idx)] = identifierHeader
interceptorSlice[int(idx)] = interceptor
}
return keys, interceptorSlice, nil
}
func (icf *interceptorsContainerFactory) createOneShardHeaderInterceptor(identifier string) (process.Interceptor, error) {
hdrStorer := icf.store.GetStorer(dataRetriever.BlockHeaderUnit)
interceptor, err := metablock.NewShardHeaderInterceptor(
icf.marshalizer,
icf.dataPool.ShardHeaders(),
icf.dataPool.HeadersNonces(),
hdrStorer,
icf.multiSigner,
icf.hasher,
icf.shardCoordinator,
icf.chronologyValidator,
)
if err != nil {
return nil, err
}
return icf.createTopicAndAssignHandler(identifier, interceptor, true)
}