示例#1
0
func (d *DeliverService) startDeliver(committer committer.Committer) error {
	logger.Info("Starting deliver service client")
	err := d.initDeliver()

	if err != nil {
		logger.Errorf("Can't initiate deliver protocol [%s]", err)
		return err
	}

	height, err := committer.LedgerHeight()
	if err != nil {
		logger.Errorf("Can't get legder height from committer [%s]", err)
		return err
	}

	if height > 0 {
		logger.Debugf("Starting deliver with block [%d]", height)
		if err := d.seekLatestFromCommitter(height); err != nil {
			return err
		}

	} else {
		logger.Debug("Starting deliver with olders block")
		if err := d.seekOldest(); err != nil {
			return err
		}

	}

	d.readUntilClose()

	return nil
}
示例#2
0
// NewGossipStateProvider creates initialized instance of gossip state provider
func NewGossipStateProvider(g gossip.Gossip, committer committer.Committer) GossipStateProvider {
	logger, _ := logging.GetLogger("GossipStateProvider")
	logging.SetLevel(logging.DEBUG, logger.Module)

	gossipChan, _ := g.Accept(func(message interface{}) bool {
		// Get only data messages
		return message.(*proto.GossipMessage).GetDataMsg() != nil
	}, false)

	// Filter message which are only relevant for state transfer
	_, commChan := g.Accept(func(message interface{}) bool {
		return message.(comm.ReceivedMessage).GetGossipMessage().GetStateRequest() != nil ||
			message.(comm.ReceivedMessage).GetGossipMessage().GetStateResponse() != nil
	}, true)

	height, err := committer.LedgerHeight()

	if err != nil {
		logger.Error("Could not read ledger info to obtain current ledger height due to: ", err)
		// Exiting as without ledger it will be impossible
		// to deliver new blocks
		return nil
	}

	s := &GossipStateProviderImpl{
		// Instance of the gossip
		gossip: g,

		// Channel to read new messages from
		gossipChan: gossipChan,

		// Channel to read direct messages from other peers
		commChan: commChan,

		stopFlag: 0,
		// Create a queue for payload received
		payloads: NewPayloadsBuffer(height + 1),

		committer: committer,

		logger: logger,
	}

	logging.SetFormatter(logFormat)

	state := NewNodeMetastate(height)

	s.logger.Infof("Updating node metadata information, current ledger sequence is at = %d, next expected block is = %d", state.LedgerHeight, s.payloads.Next())
	bytes, err := state.Bytes()
	if err == nil {
		g.UpdateMetadata(bytes)
	} else {
		s.logger.Errorf("Unable to serialize node meta state, error = %s", err)
	}

	s.done.Add(3)

	// Listen for incoming communication
	go s.listen()
	// Deliver in order messages into the incoming channel
	go s.deliverPayloads()
	// Execute anti entropy to fill missing gaps
	go s.antiEntropy()

	return s
}