func (c *Connection) CanSendKeyPacket(e *fsm.Event) { log.Printf("CanSendKeyPacket") if c.isEstablished == true { e.Cancel(errKeySendDuringEstablished) } }
func (d *Handler) beforeSyncBlocks(e *fsm.Event) { peerLogger.Debug("Received message: %s", e.Event) msg, ok := e.Args[0].(*pb.Message) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } // Forward the received SyncBlocks to the channel syncBlocks := &pb.SyncBlocks{} err := proto.Unmarshal(msg.Payload, syncBlocks) if err != nil { e.Cancel(fmt.Errorf("Error unmarshalling SyncBlocks in beforeSyncBlocks: %s", err)) return } peerLogger.Debug("Sending block onto channel for start = %d and end = %d", syncBlocks.Range.Start, syncBlocks.Range.End) // Send the message onto the channel, allow for the fact that channel may be closed on send attempt. defer func() { if x := recover(); x != nil { peerLogger.Error(fmt.Sprintf("Error sending syncBlocks to channel: %v", x)) } }() // Use non-blocking send, will WARN if missed message. select { case d.syncBlocks <- syncBlocks: default: peerLogger.Warning("Did NOT send SyncBlocks message to channel for range: %d - %d", syncBlocks.Range.Start, syncBlocks.Range.End) } }
func (d *Handler) beforeSyncStateDeltas(e *fsm.Event) { peerLogger.Debug("Received message: %s", e.Event) msg, ok := e.Args[0].(*pb.Message) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } // Forward the received SyncStateDeltas to the channel syncStateDeltas := &pb.SyncStateDeltas{} err := proto.Unmarshal(msg.Payload, syncStateDeltas) if err != nil { e.Cancel(fmt.Errorf("Error unmarshalling SyncStateDeltas in beforeSyncStateDeltas: %s", err)) return } peerLogger.Debug("Sending state delta onto channel for start = %d and end = %d", syncStateDeltas.Range.Start, syncStateDeltas.Range.End) // Send the message onto the channel, allow for the fact that channel may be closed on send attempt. defer func() { if x := recover(); x != nil { peerLogger.Error(fmt.Sprintf("Error sending syncStateDeltas to channel: %v", x)) } }() // Use non-blocking send, will WARN and close channel if missed message. d.syncStateDeltasRequestHandler.Lock() defer d.syncStateDeltasRequestHandler.Unlock() select { case d.syncStateDeltasRequestHandler.channel <- syncStateDeltas: default: // Was not able to write to the channel, in which case the SyncStateDeltasRequest stream is incomplete, and must be discarded, closing the channel peerLogger.Warning("Did NOT send SyncStateDeltas message to channel for block range %d-%d, closing channel as the message has been discarded", syncStateDeltas.Range.Start, syncStateDeltas.Range.End) d.syncStateDeltasRequestHandler.reset() } }
func (d *Handler) beforePeers(e *fsm.Event) { peerLogger.Debugf("Received %s, grabbing peers message", e.Event) // Parse out the PeerEndpoint information if _, ok := e.Args[0].(*pb.Message); !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } msg := e.Args[0].(*pb.Message) peersMessage := &pb.PeersMessage{} err := proto.Unmarshal(msg.Payload, peersMessage) if err != nil { e.Cancel(fmt.Errorf("Error unmarshalling PeersMessage: %s", err)) return } peerLogger.Debugf("Received PeersMessage with Peers: %s", peersMessage) d.Coordinator.PeersDiscovered(peersMessage) // // Can be used to demonstrate Broadcast function // if viper.GetString("peer.id") == "jdoe" { // d.Coordinator.Broadcast(&pb.Message{Type: pb.Message_UNDEFINED}) // } }
// beforeRegistered is called to handle the REGISTERED message. func (handler *Handler) beforeRegistered(e *fsm.Event) { if _, ok := e.Args[0].(*pb.ChaincodeMessage); !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debugf("Received %s, ready for invocations", pb.ChaincodeMessage_REGISTERED) }
// afterPutState handles a PUT_STATE request from the chaincode. func (handler *Handler) afterPutState(e *fsm.Event, state string) { _, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debug("Received %s in state %s, invoking put state to ledger", pb.ChaincodeMessage_PUT_STATE, state) // Put state into ledger handled within enterBusyState }
// afterDelState handles a DEL_STATE request from the chaincode. func (handler *Handler) afterDelState(e *fsm.Event, state string) { _, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debug("Received %s, invoking delete state from ledger", pb.ChaincodeMessage_DEL_STATE) // Delete state from ledger handled within enterBusyState }
// afterResponse is called to deliver a response or error to the chaincode stub. func (handler *Handler) afterResponse(e *fsm.Event) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } handler.responseChannel[msg.Uuid] <- *msg chaincodeLogger.Debug("Received %s, communicated on responseChannel(state:%s)", msg.Type, handler.FSM.Current()) }
// afterInvokeChaincode handles an INVOKE_CHAINCODE request from the chaincode. func (handler *Handler) afterInvokeChaincode(e *fsm.Event, state string) { _, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debug("Received %s in state %s, invoking another chaincode", pb.ChaincodeMessage_INVOKE_CHAINCODE, state) // Invoke another chaincode handled within enterBusyState }
// beforeCompletedEvent is invoked when chaincode has completed execution of init, invoke or query. func (handler *Handler) beforeCompletedEvent(e *fsm.Event, state string) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } // Notify on channel once into READY state chaincodeLogger.Debug("[%s]beforeCompleted - not in ready state will notify when in readystate", shortuuid(msg.Uuid)) return }
func (d *Handler) beforeBlockAdded(e *fsm.Event) { peerLogger.Debugf("Received message: %s", e.Event) msg, ok := e.Args[0].(*pb.Message) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } // Add the block and any delta state to the ledger _ = msg }
func (handler *Handler) enterReadyState(e *fsm.Event, state string) { // Now notify msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debugf("[%s]Entered state %s", shorttxid(msg.Txid), state) handler.notify(msg) }
// beforeQuery is invoked when a query message is received from the validator func (handler *Handler) beforeQuery(e *fsm.Event) { if e.Args != nil { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } handler.handleQuery(msg) } }
// beforeQuery is invoked when a query message is received from the validator func (handler *Handler) beforeQuery(e *fsm.Event) { chaincodeLogger.Debug("(beforeQuery)in state %s", handler.FSM.Current()) if e.Args != nil { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } handler.handleQuery(msg) } }
// afterGetState handles a GET_STATE request from the chaincode. func (handler *Handler) afterGetState(e *fsm.Event, state string) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debug("[%s]Received %s, invoking get state from ledger", shortuuid(msg.Uuid), pb.ChaincodeMessage_GET_STATE) // Query ledger for state handler.handleGetState(msg) }
// afterCompleted will need to handle COMPLETED event by sending message to the peer func (handler *Handler) afterCompleted(e *fsm.Event) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debugf("[%s]sending COMPLETED to validator for tid", shortuuid(msg.Uuid)) if err := handler.serialSend(msg); err != nil { e.Cancel(fmt.Errorf("send COMPLETED failed %s", err)) } }
func (handler *Handler) enterReadyState(e *fsm.Event, state string) { // Now notify msg, ok := e.Args[0].(*pb.ChaincodeMessage) handler.deleteIsTransaction(msg.Uuid) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } handler.notify(msg) chaincodeLogger.Debug("Entered state %s", state) }
// afterRangeQueryState handles a RANGE_QUERY_STATE request from the chaincode. func (handler *Handler) afterRangeQueryState(e *fsm.Event, state string) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debug("Received %s, invoking get state from ledger", pb.ChaincodeMessage_RANGE_QUERY_STATE) // Query ledger for state handler.handleRangeQueryState(msg) chaincodeLogger.Debug("Exiting GET_STATE") }
// enterTransactionState will execute chaincode's Run if coming from a TRANSACTION event. func (handler *Handler) enterTransactionState(e *fsm.Event) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debugf("[%s]Received %s, invoking transaction on chaincode(Src:%s, Dst:%s)", shortuuid(msg.Uuid), msg.Type.String(), e.Src, e.Dst) if msg.Type.String() == pb.ChaincodeMessage_TRANSACTION.String() { // Call the chaincode's Run function to invoke transaction handler.handleTransaction(msg) } }
// afterCompleted will need to handle COMPLETED event by sending message to the peer func (handler *Handler) afterCompleted(e *fsm.Event) { chaincodeLogger.Debug("(afterCompleted)Completed in state %s", handler.FSM.Current()) msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } //no need, now I AM in completed state if msg.Type.String() == pb.ChaincodeMessage_COMPLETED.String() { // now that we are comfortably in READY, send message to peer side chaincodeLogger.Debug("sending COMPLETED to validator for tid %s", msg.Uuid) handler.ChatStream.Send(msg) //} }
// enterInitState will initialize the chaincode if entering init from established. func (handler *Handler) enterInitState(e *fsm.Event) { chaincodeLogger.Debugf("Entered state %s", handler.FSM.Current()) msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debugf("[%s]Received %s, initializing chaincode", shortuuid(msg.Uuid), msg.Type.String()) if msg.Type.String() == pb.ChaincodeMessage_INIT.String() { // Call the chaincode's Run function to initialize handler.handleInit(msg) } }
func (handler *Handler) enterEndState(e *fsm.Event, state string) { defer handler.deregister() // Now notify msg, ok := e.Args[0].(*pb.ChaincodeMessage) handler.deleteIsTransaction(msg.Uuid) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debug("[%s]Entered state %s", shortuuid(msg.Uuid), state) handler.notify(msg) e.Cancel(fmt.Errorf("Entered end state")) }
// afterResponse is called to deliver a response or error to the chaincode stub. func (handler *Handler) afterResponse(e *fsm.Event) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } if err := handler.sendChannel(msg); err != nil { chaincodeLogger.Errorf("[%s]error sending %s (state:%s): %s", shortuuid(msg.Uuid), msg.Type, handler.FSM.Current(), err) } else { chaincodeLogger.Debugf("[%s]Received %s, communicated (state:%s)", shortuuid(msg.Uuid), msg.Type, handler.FSM.Current()) } }
// beforeRegisterEvent is invoked when chaincode tries to register. func (handler *Handler) beforeRegisterEvent(e *fsm.Event, state string) { chaincodeLogger.Debug("Received %s in state %s", e.Event, state) msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeID := &pb.ChaincodeID{} err := proto.Unmarshal(msg.Payload, chaincodeID) if err != nil { e.Cancel(fmt.Errorf("Error in received %s, could NOT unmarshal registration info: %s", pb.ChaincodeMessage_REGISTER, err)) return } // Now register with the chaincodeSupport handler.ChaincodeID = chaincodeID err = handler.chaincodeSupport.registerHandler(handler) if err != nil { e.Cancel(err) handler.notifyDuringStartup(false) return } chaincodeLogger.Debug("Got %s for chaincodeID = %s, sending back %s", e.Event, chaincodeID, pb.ChaincodeMessage_REGISTERED) if err := handler.serialSend(&pb.ChaincodeMessage{Type: pb.ChaincodeMessage_REGISTERED}); err != nil { e.Cancel(fmt.Errorf("Error sending %s: %s", pb.ChaincodeMessage_REGISTERED, err)) handler.notifyDuringStartup(false) return } }
func (handler *Handler) enterInitState(e *fsm.Event, state string) { ccMsg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } chaincodeLogger.Debugf("[%s]Entered state %s", shorttxid(ccMsg.Txid), state) //very first time entering init state from established, send message to chaincode if ccMsg.Type == pb.ChaincodeMessage_INIT { if err := handler.serialSend(ccMsg); err != nil { errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(fmt.Sprintf("Error sending %s: %s", pb.ChaincodeMessage_INIT, err)), Txid: ccMsg.Txid} handler.notify(errMsg) } } }
func (handler *Handler) afterError(e *fsm.Event) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } /* There are two situations in which the ERROR event can be triggered: * 1. When an error is encountered within handleInit or handleTransaction - some issue at the chaincode side; In this case there will be no responseChannel and the message has been sent to the validator. * 2. The chaincode has initiated a request (get/put/del state) to the validator and is expecting a response on the responseChannel; If ERROR is received from validator, this needs to be notified on the responseChannel. */ if handler.responseChannel[msg.Uuid] != nil { chaincodeLogger.Debug("Error received from validator %s, communicating on responseChannel(state:%s)", msg.Type, handler.FSM.Current()) handler.responseChannel[msg.Uuid] <- *msg } }
func (c *Connection) ValidateHelloPacket(e *fsm.Event) { if c.isEstablished == true { log.Printf("Received hello packet for established session. Resetting") c.resetSession() } v := reflect.ValueOf(e.Args[0]) p := v.Bytes() if len(p) < 120 { log.Println("ValidateHelloPacket: Undersize hello packet") e.Cancel(errUndersizeMessage) } }
func (handler *Handler) afterError(e *fsm.Event) { msg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } /* TODO- revisit. This may no longer be needed with the serialized/streamlined messaging model * There are two situations in which the ERROR event can be triggered: * 1. When an error is encountered within handleInit or handleTransaction - some issue at the chaincode side; In this case there will be no responseChannel and the message has been sent to the validator. * 2. The chaincode has initiated a request (get/put/del state) to the validator and is expecting a response on the responseChannel; If ERROR is received from validator, this needs to be notified on the responseChannel. */ if err := handler.sendChannel(msg); err == nil { chaincodeLogger.Debugf("[%s]Error received from validator %s, communicated(state:%s)", shortuuid(msg.Uuid), msg.Type, handler.FSM.Current()) } }
func (handler *Handler) enterInitState(e *fsm.Event, state string) { chaincodeLogger.Debug("Entered state %s", state) ccMsg, ok := e.Args[0].(*pb.ChaincodeMessage) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } //very first time entering init state from established, send message to chaincode if ccMsg.Type == pb.ChaincodeMessage_INIT { // Mark isTransaction to allow put/del state and invoke other chaincodes handler.markIsTransaction(ccMsg.Uuid, true) if err := handler.ChatStream.Send(ccMsg); err != nil { errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(fmt.Sprintf("Error sending %s: %s", pb.ChaincodeMessage_INIT, err)), Uuid: ccMsg.Uuid} handler.notify(errMsg) } } }
func (d *Handler) beforeSyncGetBlocks(e *fsm.Event) { peerLogger.Debugf("Received message: %s", e.Event) msg, ok := e.Args[0].(*pb.Message) if !ok { e.Cancel(fmt.Errorf("Received unexpected message type")) return } // Start a separate go FUNC to send the blocks per the SyncBlockRange payload syncBlockRange := &pb.SyncBlockRange{} err := proto.Unmarshal(msg.Payload, syncBlockRange) if err != nil { e.Cancel(fmt.Errorf("Error unmarshalling SyncBlockRange in GetBlocks: %s", err)) return } go d.sendBlocks(syncBlockRange) }