github.com/koko1123/flow-go-1@v0.29.6/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/koko1123/flow-go-1/model/flow" 9 "github.com/koko1123/flow-go-1/model/messages" 10 "github.com/koko1123/flow-go-1/module" 11 "github.com/koko1123/flow-go-1/network" 12 "github.com/koko1123/flow-go-1/network/channels" 13 "github.com/koko1123/flow-go-1/storage" 14 ) 15 16 type ResponseSender interface { 17 SendResponse(interface{}, flow.Identifier) error 18 } 19 20 type ResponseSenderImpl struct { 21 con network.Conduit 22 } 23 24 func (r *ResponseSenderImpl) SendResponse(res interface{}, target flow.Identifier) error { 25 switch res.(type) { 26 case *messages.BlockResponse: 27 err := r.con.Unicast(res, target) 28 if err != nil { 29 return fmt.Errorf("could not unicast block response to target %x: %w", target, err) 30 } 31 case *messages.SyncResponse: 32 err := r.con.Unicast(res, target) 33 if err != nil { 34 return fmt.Errorf("could not unicast sync response to target %x: %w", target, err) 35 } 36 default: 37 return fmt.Errorf("unable to unicast unexpected response %+v", res) 38 } 39 40 return nil 41 } 42 43 func NewResponseSender(con network.Conduit) *ResponseSenderImpl { 44 return &ResponseSenderImpl{ 45 con: con, 46 } 47 } 48 49 type RequestHandlerEngine struct { 50 requestHandler *RequestHandler 51 } 52 53 var _ network.MessageProcessor = (*RequestHandlerEngine)(nil) 54 55 func NewRequestHandlerEngine( 56 logger zerolog.Logger, 57 metrics module.EngineMetrics, 58 net network.Network, 59 me module.Local, 60 blocks storage.Blocks, 61 core module.SyncCore, 62 finalizedHeader *FinalizedHeaderCache, 63 ) (*RequestHandlerEngine, error) { 64 e := &RequestHandlerEngine{} 65 66 con, err := net.Register(channels.PublicSyncCommittee, e) 67 if err != nil { 68 return nil, fmt.Errorf("could not register engine: %w", err) 69 } 70 71 e.requestHandler = NewRequestHandler( 72 logger, 73 metrics, 74 NewResponseSender(con), 75 me, 76 blocks, 77 core, 78 finalizedHeader, 79 false, 80 ) 81 82 return e, nil 83 } 84 85 func (r *RequestHandlerEngine) Process(channel channels.Channel, originID flow.Identifier, event interface{}) error { 86 return r.requestHandler.Process(channel, originID, event) 87 } 88 89 func (r *RequestHandlerEngine) Ready() <-chan struct{} { 90 return r.requestHandler.Ready() 91 } 92 93 func (r *RequestHandlerEngine) Done() <-chan struct{} { 94 return r.requestHandler.Done() 95 }