github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/consensus_pbft/helper/handler.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package helper 18 19 import ( 20 "fmt" 21 22 23 "github.com/ethereum/go-ethereum/consensus_pbft/util" 24 "github.com/ethereum/go-ethereum/consensus_pbft/peer" 25 "github.com/ethereum/go-ethereum/log" 26 "github.com/ethereum/go-ethereum/consensus_pbft/message" 27 ) 28 29 30 31 const ( 32 // DefaultConsensusQueueSize value of 1000 33 DefaultConsensusQueueSize int = 1000 34 ) 35 36 // ConsensusHandler handles consensus messages. 37 // It also implements the Stack. 38 type ConsensusHandler struct { 39 peer.MessageHandler 40 consenterChan chan *util.Message 41 coordinator peer.MessageHandlerCoordinator 42 } 43 44 // NewConsensusHandler constructs a new MessageHandler for the plugin. 45 // Is instance of peer.HandlerFactory 46 func NewConsensusHandler(coord peer.MessageHandlerCoordinator, 47 stream peer.ChatStream, initiatedStream bool) (peer.MessageHandler, error) { 48 /* 49 peerHandler, err := peer.NewPeerHandler(coord, stream, initiatedStream) 50 if err != nil { 51 return nil, fmt.Errorf("Error creating PeerHandler: %s", err) 52 } 53 */ 54 handler := &ConsensusHandler{ 55 // MessageHandler: peerHandler, 56 coordinator: coord, 57 } 58 59 consensusQueueSize := 100//viper.GetInt("peer.validator.consensus.buffersize") 60 61 if consensusQueueSize <= 0 { 62 log.Error("peer.validator.consensus.buffersize is set to %d, but this must be a positive integer, defaulting to %d", consensusQueueSize, DefaultConsensusQueueSize) 63 consensusQueueSize = DefaultConsensusQueueSize 64 } 65 66 handler.consenterChan = make(chan *util.Message, consensusQueueSize) 67 getEngineImpl().consensusFan.AddFaninChannel(handler.consenterChan) 68 69 return handler, nil 70 } 71 72 // HandleMessage handles the incoming Fabric messages for the Peer 73 func (handler *ConsensusHandler) HandleMessage(msg *message.Message) error { 74 if msg.Type == message.Message_CONSENSUS { 75 senderPE, _ := handler.To() 76 select { 77 case handler.consenterChan <- &util.Message{ 78 Msg: msg, 79 Sender: senderPE.GetPeerId(), 80 }: 81 return nil 82 default: 83 err := fmt.Errorf("Message channel for %v full, rejecting", senderPE.GetPeerId()) 84 log.Error("Failed to queue consensus message because: %v", err) 85 return err 86 } 87 } 88 89 // if logger.IsEnabledFor(logging.DEBUG) { 90 log.Debug("Did not handle message of type %s, passing on to next MessageHandler", msg.Type) 91 // } 92 return handler.MessageHandler.HandleMessage(msg) 93 }