Beispiel #1
0
func (s *GossipStateProviderImpl) handleStateRequest(msg comm.ReceivedMessage) {
	request := msg.GetGossipMessage().GetStateRequest()
	response := &proto.RemoteStateResponse{Payloads: make([]*proto.Payload, 0)}
	for _, seqNum := range request.SeqNums {
		s.logger.Debug("Reading block ", seqNum, " from the committer service")
		blocks := s.committer.GetBlocks([]uint64{seqNum})

		if blocks == nil || len(blocks) < 1 {
			s.logger.Errorf("Wasn't able to read block with sequence number %d from ledger, skipping....", seqNum)
			continue
		}

		blockBytes, err := pb.Marshal(blocks[0])
		if err != nil {
			s.logger.Errorf("Could not marshal block: %s", err)
		}

		if err != nil {
			s.logger.Errorf("Could not calculate hash of block: %s", err)
		}

		response.Payloads = append(response.Payloads, &proto.Payload{
			SeqNum: seqNum,
			Data:   blockBytes,
			// TODO: Check hash generation for given block from the ledger
			Hash: "",
		})
	}
	// Sending back response with missing blocks
	msg.Respond(&proto.GossipMessage{
		Content: &proto.GossipMessage_StateResponse{response},
	})
}
Beispiel #2
0
func (s *GossipStateProviderImpl) handleStateResponse(msg comm.ReceivedMessage) {
	response := msg.GetGossipMessage().GetStateResponse()
	for _, payload := range response.GetPayloads() {
		s.logger.Debugf("Received payload with sequence number %d.", payload.SeqNum)
		err := s.payloads.Push(payload)
		if err != nil {
			s.logger.Warningf("Payload with sequence number %d was received earlier", payload.SeqNum)
		}
	}
}
Beispiel #3
0
func (s *GossipStateProviderImpl) directMessage(msg comm.ReceivedMessage) {
	s.logger.Debug("[ENTER] -> directMessage")
	defer s.logger.Debug("[EXIT] ->  directMessage")

	if msg == nil {
		s.logger.Error("Got nil message via end-to-end channel, should not happen!")
		return
	}

	incoming := msg.GetGossipMessage()

	if incoming.GetStateRequest() != nil {
		s.handleStateRequest(msg)
	} else if incoming.GetStateResponse() != nil {
		s.handleStateResponse(msg)
	}
}
Beispiel #4
0
func (cs *certStore) handleMessage(msg comm.ReceivedMessage) {
	if update := msg.GetGossipMessage().GetDataUpdate(); update != nil {
		for _, m := range update.Data {
			if !m.IsIdentityMsg() {
				cs.logger.Warning("Got a non-identity message:", m, "aborting")
				return
			}
			idMsg := m.GetPeerIdentity()
			if err := cs.mcs.ValidateIdentity(api.PeerIdentityType(idMsg.Cert)); err != nil {
				cs.logger.Warning("Got invalid certificate:", err)
				return
			}

		}
	}
	cs.pull.HandleMessage(msg)
}
Beispiel #5
0
func (p *pullMediatorImpl) HandleMessage(m comm.ReceivedMessage) {
	if m.GetGossipMessage() == nil || !m.GetGossipMessage().IsPullMsg() {
		return
	}
	msg := m.GetGossipMessage()

	msgType := msg.GetPullMsgType()
	if msgType != p.config.MsgType {
		return
	}

	p.logger.Debug(msg)

	itemIds := []string{}
	items := []*proto.GossipMessage{}
	var pullMsgType PullMsgType

	if helloMsg := msg.GetHello(); helloMsg != nil {
		pullMsgType = HelloMsgType
		p.engine.OnHello(helloMsg.Nonce, m)
	}
	if digest := msg.GetDataDig(); digest != nil {
		itemIds = digest.Digests
		pullMsgType = DigestMsgType
		p.engine.OnDigest(digest.Digests, digest.Nonce, m)
	}
	if req := msg.GetDataReq(); req != nil {
		itemIds = req.Digests
		pullMsgType = RequestMsgType
		p.engine.OnReq(req.Digests, req.Nonce, m)
	}
	if res := msg.GetDataUpdate(); res != nil {
		itemIds = make([]string, len(res.Data))
		items = make([]*proto.GossipMessage, len(res.Data))
		pullMsgType = ResponseMsgType
		for i, pulledMsg := range res.Data {
			p.msgCons(pulledMsg)
			itemIds[i] = p.idExtractor(pulledMsg)
			items[i] = pulledMsg
			p.Lock()
			p.itemId2msg[itemIds[i]] = pulledMsg
			p.Unlock()
		}
		p.engine.OnRes(itemIds, res.Nonce)
	}

	// Invoke hooks for relevant message type
	for _, h := range p.hooksByMsgType(pullMsgType) {
		h(itemIds, items, m)
	}
}
Beispiel #6
0
func (g *gossipServiceImpl) forwardDiscoveryMsg(msg comm.ReceivedMessage) {
	defer func() { // can be closed while shutting down
		recover()
	}()
	g.discAdapter.incChan <- msg.GetGossipMessage()
}
Beispiel #7
0
func (g *gossipServiceImpl) handleMessage(m comm.ReceivedMessage) {
	if g.toDie() {
		return
	}
	g.logger.Info("Entering,", m)
	defer g.logger.Info("Exiting")
	if m == nil || m.GetGossipMessage() == nil {
		return
	}

	msg := m.GetGossipMessage()

	if msg.IsAliveMsg() || msg.IsDataMsg() {
		if msg.IsAliveMsg() {
			if !g.disSecAdap.ValidateAliveMsg(msg.GetAliveMsg()) {
				g.logger.Warning("AliveMessage", m.GetGossipMessage(), "isn't authentic. Discarding it")
				return
			}

			am := msg.GetAliveMsg()
			storedIdentity, _ := g.idMapper.Get(common.PKIidType(am.Membership.PkiID))
			// If peer's certificate is included inside AliveMessage, and we don't have a mapping between
			// its PKI-ID and certificate, create a mapping for it now.
			if identity := am.Identity; identity != nil && storedIdentity == nil {
				err := g.idMapper.Put(common.PKIidType(am.Membership.PkiID), api.PeerIdentityType(identity))
				if err != nil {
					g.logger.Warning("Failed adding identity of", am, "into identity store:", err)
					return
				}
				g.logger.Info("Learned identity of", am.Membership.PkiID)
			}
		}

		if msg.IsDataMsg() {
			blockMsg := msg.GetDataMsg()
			if blockMsg.Payload == nil {
				g.logger.Warning("Empty block! Discarding it")
				return
			}
			if err := g.mcs.VerifyBlock(blockMsg); err != nil {
				g.logger.Warning("Could not verify block", blockMsg.Payload.SeqNum, "Discarding it")
				return
			}
		}

		added := g.msgStore.add(msg)
		if added {
			g.emitter.Add(msg)
			if dataMsg := m.GetGossipMessage().GetDataMsg(); dataMsg != nil {
				g.blocksPuller.Add(msg)
				g.DeMultiplex(msg)
			}

		}
	}

	if selectOnlyDiscoveryMessages(m) {
		g.forwardDiscoveryMsg(m)
	}

	if msg.IsPullMsg() {
		switch msgType := msg.GetPullMsgType(); msgType {
		case proto.PullMsgType_BlockMessage:
			g.blocksPuller.HandleMessage(m)
		case proto.PullMsgType_IdentityMsg:
			g.certStore.handleMessage(m)
		default:
			g.logger.Warning("Got invalid pull message type:", msgType)
		}
	}
}