github.com/braveheart12/insolar-09-08-19@v0.8.7/network/controller/bootstrap/network_bootstrap.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 bootstrap 19 20 import ( 21 "context" 22 23 "github.com/insolar/insolar/core" 24 "github.com/insolar/insolar/instrumentation/instracer" 25 "github.com/insolar/insolar/log" 26 "github.com/insolar/insolar/network" 27 "github.com/insolar/insolar/network/transport/host" 28 "github.com/insolar/insolar/network/utils" 29 "github.com/pkg/errors" 30 ) 31 32 type NetworkBootstrapper interface { 33 Bootstrap(ctx context.Context) (*network.BootstrapResult, error) 34 SetLastPulse(number core.PulseNumber) 35 GetLastPulse() core.PulseNumber 36 } 37 38 type networkBootstrapper struct { 39 Certificate core.Certificate `inject:""` 40 Bootstrapper Bootstrapper `inject:""` 41 NodeKeeper network.NodeKeeper `inject:""` 42 SessionManager SessionManager `inject:""` 43 AuthController AuthorizationController `inject:""` 44 ChallengeController ChallengeResponseController `inject:""` 45 } 46 47 func (nb *networkBootstrapper) Bootstrap(ctx context.Context) (*network.BootstrapResult, error) { 48 ctx, span := instracer.StartSpan(ctx, "NetworkBootstrapper.Bootstrap") 49 defer span.End() 50 if len(nb.Certificate.GetDiscoveryNodes()) == 0 { 51 host, err := host.NewHostN(nb.NodeKeeper.GetOrigin().Address(), nb.NodeKeeper.GetOrigin().ID()) 52 if err != nil { 53 return nil, errors.Wrap(err, "failed to create a host") 54 } 55 log.Info("[ Bootstrap ] Zero bootstrap") 56 return &network.BootstrapResult{ 57 Host: host, 58 // FirstPulseTime: nb.Bootstrapper.GetFirstFakePulseTime(), 59 }, nil 60 } 61 var err error 62 var result *network.BootstrapResult 63 if utils.OriginIsDiscovery(nb.Certificate) { 64 result, err = nb.bootstrapDiscovery(ctx) 65 // if the network is up and complete, we return discovery nodes via consensus 66 if err == ErrReconnectRequired { 67 log.Debugf("[ Bootstrap ] Connecting discovery node %s as joiner", nb.NodeKeeper.GetOrigin().ID()) 68 nb.NodeKeeper.SetState(core.WaitingNodeNetworkState) 69 result, err = nb.bootstrapJoiner(ctx) 70 } 71 } else { 72 result, err = nb.bootstrapJoiner(ctx) 73 } 74 if err != nil { 75 return nil, errors.Wrap(err, "failed to bootstrap") 76 } 77 nb.NodeKeeper.SetIsBootstrapped(true) 78 return result, nil 79 } 80 81 func (nb *networkBootstrapper) SetLastPulse(number core.PulseNumber) { 82 nb.Bootstrapper.SetLastPulse(number) 83 } 84 85 func (nb *networkBootstrapper) GetLastPulse() core.PulseNumber { 86 return nb.Bootstrapper.GetLastPulse() 87 } 88 89 func (nb *networkBootstrapper) bootstrapJoiner(ctx context.Context) (*network.BootstrapResult, error) { 90 ctx, span := instracer.StartSpan(ctx, "NetworkBootstrapper.bootstrapJoiner") 91 defer span.End() 92 result, discoveryNode, err := nb.Bootstrapper.Bootstrap(ctx) 93 if err != nil { 94 return nil, errors.Wrap(err, "Error bootstrapping to discovery node") 95 } 96 sessionID, err := nb.AuthController.Authorize(ctx, discoveryNode, nb.Certificate) 97 if err != nil { 98 return nil, errors.Wrap(err, "Error authorizing on discovery node") 99 } 100 101 _, err = nb.ChallengeController.Execute(ctx, discoveryNode, sessionID) 102 if err != nil { 103 return nil, errors.Wrap(err, "Error executing double challenge response") 104 } 105 // TODO: fix Short ID assignment logic 106 // origin := nb.NodeKeeper.GetOrigin() 107 // mutableOrigin := origin.(nodenetwork.MutableNode) 108 // mutableOrigin.SetShortID(data.AssignShortID) 109 return result, nb.AuthController.Register(ctx, discoveryNode, sessionID) 110 } 111 112 func (nb *networkBootstrapper) bootstrapDiscovery(ctx context.Context) (*network.BootstrapResult, error) { 113 return nb.Bootstrapper.BootstrapDiscovery(ctx) 114 } 115 116 func NewNetworkBootstrapper() NetworkBootstrapper { 117 return &networkBootstrapper{} 118 }