-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathrequest_handler_engine.go
110 lines (94 loc) · 3.14 KB
/
request_handler_engine.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
package synchronization
import (
"fmt"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/component"
"github.com/onflow/flow-go/module/events"
"github.com/onflow/flow-go/network"
"github.com/onflow/flow-go/network/channels"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
type ResponseSender interface {
SendResponse(interface{}, flow.Identifier) error
}
type ResponseSenderImpl struct {
con network.Conduit
}
func (r *ResponseSenderImpl) SendResponse(res interface{}, target flow.Identifier) error {
switch res.(type) {
case *messages.BlockResponse:
err := r.con.Unicast(res, target)
if err != nil {
return fmt.Errorf("could not unicast block response to target %x: %w", target, err)
}
case *messages.SyncResponse:
err := r.con.Unicast(res, target)
if err != nil {
return fmt.Errorf("could not unicast sync response to target %x: %w", target, err)
}
default:
return fmt.Errorf("unable to unicast unexpected response %+v", res)
}
return nil
}
func NewResponseSender(con network.Conduit) *ResponseSenderImpl {
return &ResponseSenderImpl{
con: con,
}
}
// RequestHandlerEngine is an engine which operates only the request-handling portion of the block sync protocol.
// It is used by Access/Observer nodes attached to the public network, enabling them
// to provide block synchronization data to nodes on the public network, but not
// requesting any data from these nodes. (Requests are sent only on the private network.)
type RequestHandlerEngine struct {
component.Component
hotstuff.FinalizationConsumer
requestHandler *RequestHandler
}
var _ network.MessageProcessor = (*RequestHandlerEngine)(nil)
var _ component.Component = (*RequestHandlerEngine)(nil)
var _ hotstuff.FinalizationConsumer = (*RequestHandlerEngine)(nil)
func NewRequestHandlerEngine(
logger zerolog.Logger,
metrics module.EngineMetrics,
net network.EngineRegistry,
me module.Local,
state protocol.State,
blocks storage.Blocks,
core module.SyncCore,
) (*RequestHandlerEngine, error) {
e := &RequestHandlerEngine{}
con, err := net.Register(channels.PublicSyncCommittee, e)
if err != nil {
return nil, fmt.Errorf("could not register engine: %w", err)
}
finalizedHeaderCache, finalizedCacheWorker, err := events.NewFinalizedHeaderCache(state)
if err != nil {
return nil, fmt.Errorf("could not initialize finalized header cache: %w", err)
}
e.FinalizationConsumer = finalizedHeaderCache
e.requestHandler = NewRequestHandler(
logger,
metrics,
NewResponseSender(con),
me,
finalizedHeaderCache,
blocks,
core,
false,
)
builder := component.NewComponentManagerBuilder().AddWorker(finalizedCacheWorker)
for i := 0; i < defaultEngineRequestsWorkers; i++ {
builder.AddWorker(e.requestHandler.requestProcessingWorker)
}
e.Component = builder.Build()
return e, nil
}
func (r *RequestHandlerEngine) Process(channel channels.Channel, originID flow.Identifier, event interface{}) error {
return r.requestHandler.Process(channel, originID, event)
}