github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/synchronization/request_handler_engine.go (about) 1 package synchronization 2 3 import ( 4 "fmt" 5 6 "github.com/rs/zerolog" 7 8 "github.com/onflow/flow-go/consensus/hotstuff" 9 "github.com/onflow/flow-go/model/flow" 10 "github.com/onflow/flow-go/model/messages" 11 "github.com/onflow/flow-go/module" 12 "github.com/onflow/flow-go/module/component" 13 "github.com/onflow/flow-go/module/events" 14 "github.com/onflow/flow-go/network" 15 "github.com/onflow/flow-go/network/channels" 16 "github.com/onflow/flow-go/state/protocol" 17 "github.com/onflow/flow-go/storage" 18 ) 19 20 type ResponseSender interface { 21 SendResponse(interface{}, flow.Identifier) error 22 } 23 24 type ResponseSenderImpl struct { 25 con network.Conduit 26 } 27 28 func (r *ResponseSenderImpl) SendResponse(res interface{}, target flow.Identifier) error { 29 switch res.(type) { 30 case *messages.BlockResponse: 31 err := r.con.Unicast(res, target) 32 if err != nil { 33 return fmt.Errorf("could not unicast block response to target %x: %w", target, err) 34 } 35 case *messages.SyncResponse: 36 err := r.con.Unicast(res, target) 37 if err != nil { 38 return fmt.Errorf("could not unicast sync response to target %x: %w", target, err) 39 } 40 default: 41 return fmt.Errorf("unable to unicast unexpected response %+v", res) 42 } 43 44 return nil 45 } 46 47 func NewResponseSender(con network.Conduit) *ResponseSenderImpl { 48 return &ResponseSenderImpl{ 49 con: con, 50 } 51 } 52 53 // RequestHandlerEngine is an engine which operates only the request-handling portion of the block sync protocol. 54 // It is used by Access/Observer nodes attached to the public network, enabling them 55 // to provide block synchronization data to nodes on the public network, but not 56 // requesting any data from these nodes. (Requests are sent only on the private network.) 57 type RequestHandlerEngine struct { 58 component.Component 59 hotstuff.FinalizationConsumer 60 61 requestHandler *RequestHandler 62 } 63 64 var _ network.MessageProcessor = (*RequestHandlerEngine)(nil) 65 var _ component.Component = (*RequestHandlerEngine)(nil) 66 var _ hotstuff.FinalizationConsumer = (*RequestHandlerEngine)(nil) 67 68 func NewRequestHandlerEngine( 69 logger zerolog.Logger, 70 metrics module.EngineMetrics, 71 net network.EngineRegistry, 72 me module.Local, 73 state protocol.State, 74 blocks storage.Blocks, 75 core module.SyncCore, 76 ) (*RequestHandlerEngine, error) { 77 e := &RequestHandlerEngine{} 78 79 con, err := net.Register(channels.PublicSyncCommittee, e) 80 if err != nil { 81 return nil, fmt.Errorf("could not register engine: %w", err) 82 } 83 84 finalizedHeaderCache, finalizedCacheWorker, err := events.NewFinalizedHeaderCache(state) 85 if err != nil { 86 return nil, fmt.Errorf("could not initialize finalized header cache: %w", err) 87 } 88 e.FinalizationConsumer = finalizedHeaderCache 89 e.requestHandler = NewRequestHandler( 90 logger, 91 metrics, 92 NewResponseSender(con), 93 me, 94 finalizedHeaderCache, 95 blocks, 96 core, 97 false, 98 ) 99 builder := component.NewComponentManagerBuilder().AddWorker(finalizedCacheWorker) 100 for i := 0; i < defaultEngineRequestsWorkers; i++ { 101 builder.AddWorker(e.requestHandler.requestProcessingWorker) 102 } 103 e.Component = builder.Build() 104 105 return e, nil 106 } 107 108 func (r *RequestHandlerEngine) Process(channel channels.Channel, originID flow.Identifier, event interface{}) error { 109 return r.requestHandler.Process(channel, originID, event) 110 }