github.com/braveheart12/just@v0.8.7/network/controller/pulsecontroller.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package controller
    19  
    20  import (
    21  	"context"
    22  	"sync/atomic"
    23  
    24  	"github.com/insolar/insolar/component"
    25  	"github.com/insolar/insolar/core"
    26  	"github.com/insolar/insolar/network"
    27  	"github.com/insolar/insolar/network/transport/packet"
    28  	"github.com/insolar/insolar/network/transport/packet/types"
    29  )
    30  
    31  const (
    32  	skippedPulsesLimit = 15
    33  )
    34  
    35  type PulseController interface {
    36  	component.Initer
    37  }
    38  
    39  type pulseController struct {
    40  	PulseHandler       network.PulseHandler    `inject:""`
    41  	NodeKeeper         network.NodeKeeper      `inject:""`
    42  	TerminationHandler core.TerminationHandler `inject:""`
    43  
    44  	hostNetwork  network.HostNetwork
    45  	routingTable network.RoutingTable
    46  
    47  	skippedPulses uint32
    48  }
    49  
    50  func (pc *pulseController) Init(ctx context.Context) error {
    51  	pc.hostNetwork.RegisterRequestHandler(types.Pulse, pc.processPulse)
    52  	pc.hostNetwork.RegisterRequestHandler(types.GetRandomHosts, pc.processGetRandomHosts)
    53  	return nil
    54  }
    55  
    56  func (pc *pulseController) processPulse(ctx context.Context, request network.Request) (network.Response, error) {
    57  	data := request.GetData().(*packet.RequestPulse)
    58  	// we should not process pulses in Waiting state because network can be unready to join current node,
    59  	// so we should wait for pulse from consensus phase1 packet
    60  	if pc.NodeKeeper.GetState() != core.WaitingNodeNetworkState {
    61  		go pc.PulseHandler.HandlePulse(context.Background(), data.Pulse)
    62  	} else {
    63  		skipped := atomic.AddUint32(&pc.skippedPulses, 1)
    64  		if skipped >= skippedPulsesLimit {
    65  			// we definitely failed to receive pulse via phase1 packet and should exit
    66  			pc.TerminationHandler.Abort("Failed to receive phase1 packet with pulse during bootstrap")
    67  		}
    68  	}
    69  	return pc.hostNetwork.BuildResponse(ctx, request, &packet.ResponsePulse{Success: true, Error: ""}), nil
    70  }
    71  
    72  func (pc *pulseController) processGetRandomHosts(ctx context.Context, request network.Request) (network.Response, error) {
    73  	data := request.GetData().(*packet.RequestGetRandomHosts)
    74  	randomHosts := pc.routingTable.GetRandomNodes(data.HostsNumber)
    75  	return pc.hostNetwork.BuildResponse(ctx, request, &packet.ResponseGetRandomHosts{Hosts: randomHosts}), nil
    76  }
    77  
    78  func NewPulseController(hostNetwork network.HostNetwork, routingTable network.RoutingTable) PulseController {
    79  	return &pulseController{hostNetwork: hostNetwork, routingTable: routingTable}
    80  }